Skip to content

Commit

Permalink
hotshot: add tests for drb results
Browse files Browse the repository at this point in the history
  • Loading branch information
pls148 committed Feb 20, 2025
1 parent f7d38ef commit 5f82edc
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 9 deletions.
33 changes: 33 additions & 0 deletions hotshot-example-types/src/node_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use hotshot::traits::{
randomized_committee_members::RandomizedCommitteeMembers,
static_committee::StaticCommittee,
static_committee_leader_two_views::StaticCommitteeLeaderForTwoViews,
static_committee_with_drb::StaticCommitteeWithDrb,
two_static_committees::TwoStaticCommittees,
},
implementations::{CombinedNetworks, Libp2pNetwork, MemoryNetwork, PushCdnNetwork},
Expand Down Expand Up @@ -69,6 +70,38 @@ impl NodeType for TestTypes {
type BuilderSignatureKey = BuilderKey;
}

#[derive(
Copy,
Clone,
Debug,
Default,
Hash,
PartialEq,
Eq,
PartialOrd,
Ord,
serde::Serialize,
serde::Deserialize,
)]
/// filler struct to implement node type and allow us
/// to select our traits
pub struct TestTypesWithDrb;
impl NodeType for TestTypesWithDrb {
const UPGRADE_CONSTANTS: UpgradeConstants = TEST_UPGRADE_CONSTANTS;

type AuctionResult = TestAuctionResult;
type View = ViewNumber;
type Epoch = EpochNumber;
type BlockHeader = TestBlockHeader;
type BlockPayload = TestBlockPayload;
type SignatureKey = BLSPubKey;
type Transaction = TestTransaction;
type ValidatedState = TestValidatedState;
type InstanceState = TestInstanceState;
type Membership = StaticCommitteeWithDrb<TestTypesWithDrb>;
type BuilderSignatureKey = BuilderKey;
}

#[derive(
Copy,
Clone,
Expand Down
15 changes: 8 additions & 7 deletions hotshot-testing/tests/tests_6/test_epochs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use hotshot_example_types::{
node_types::{
CombinedImpl, EpochUpgradeTestVersions, EpochsTestVersions, Libp2pImpl, MemoryImpl,
PushCdnImpl, RandomOverlapQuorumFilterConfig, StableQuorumFilterConfig,
TestConsecutiveLeaderTypes, TestTwoStakeTablesTypes, TestTypes,
TestConsecutiveLeaderTypes, TestTwoStakeTablesTypes, TestTypes,TestTypesWithDrb,
TestTypesRandomizedCommitteeMembers, TestTypesRandomizedLeader,
},
testable_delay::{DelayConfig, DelayOptions, DelaySettings, SupportedTraitTypesForAsyncDelay},
Expand Down Expand Up @@ -60,6 +60,7 @@ cross_tests!(
TestTypesRandomizedCommitteeMembers<StableQuorumFilterConfig<123, 5>>, // Overlap = 2F+1
TestTypesRandomizedCommitteeMembers<StableQuorumFilterConfig<123, 6>>, // Overlap = 3F
TestTypesRandomizedCommitteeMembers<RandomOverlapQuorumFilterConfig<123, 4, 7, 0, 2>>, // Overlap = Dynamic
TestTypesWithDrb,
],
Versions: [EpochsTestVersions],
Ignore: false,
Expand All @@ -79,7 +80,7 @@ cross_tests!(
cross_tests!(
TestName: test_success_with_async_delay_with_epochs,
Impls: [Libp2pImpl, PushCdnImpl, CombinedImpl],
Types: [TestTypes, TestTwoStakeTablesTypes],
Types: [TestTypes, TestTwoStakeTablesTypes, TestTypesWithDrb],
Versions: [EpochsTestVersions],
Ignore: false,
Metadata: {
Expand Down Expand Up @@ -111,7 +112,7 @@ cross_tests!(
cross_tests!(
TestName: test_success_with_async_delay_2_with_epochs,
Impls: [Libp2pImpl, PushCdnImpl, CombinedImpl],
Types: [TestTypes, TestTwoStakeTablesTypes],
Types: [TestTypes, TestTwoStakeTablesTypes, TestTypesWithDrb],
Versions: [EpochsTestVersions],
Ignore: false,
Metadata: {
Expand Down Expand Up @@ -168,7 +169,7 @@ cross_tests!(
cross_tests!(
TestName: test_epoch_end,
Impls: [CombinedImpl, Libp2pImpl, PushCdnImpl],
Types: [TestTypes, TestTwoStakeTablesTypes],
Types: [TestTypes, TestTwoStakeTablesTypes, TestTypesWithDrb],
Versions: [EpochsTestVersions],
Ignore: false,
Metadata: {
Expand All @@ -192,7 +193,7 @@ cross_tests!(
cross_tests!(
TestName: test_shorter_decide,
Impls: [Libp2pImpl, PushCdnImpl, CombinedImpl],
Types: [TestTypes, TestTwoStakeTablesTypes],
Types: [TestTypes, TestTwoStakeTablesTypes, TestTypesWithDrb],
Versions: [EpochsTestVersions],
Ignore: false,
Metadata: {
Expand Down Expand Up @@ -495,7 +496,7 @@ cross_tests!(
cross_tests!(
TestName: test_all_restart_epochs,
Impls: [CombinedImpl, PushCdnImpl],
Types: [TestTypes, TestTypesRandomizedLeader, TestTwoStakeTablesTypes],
Types: [TestTypes, TestTypesRandomizedLeader, TestTwoStakeTablesTypes, TestTypesWithDrb],
Versions: [EpochsTestVersions],
Ignore: false,
Metadata: {
Expand Down Expand Up @@ -544,7 +545,7 @@ cross_tests!(
cross_tests!(
TestName: test_all_restart_one_da_with_epochs,
Impls: [CombinedImpl],
Types: [TestTypes, TestTwoStakeTablesTypes],
Types: [TestTypes, TestTwoStakeTablesTypes, TestTypesWithDrb],
Versions: [EpochsTestVersions],
Ignore: false,
Metadata: {
Expand Down
35 changes: 33 additions & 2 deletions hotshot-types/src/drb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
// You should have received a copy of the MIT License
// along with the HotShot repository. If not, see <https://mit-license.org/>.

use std::collections::BTreeMap;
use std::{
collections::BTreeMap,
hash::{DefaultHasher, Hash, Hasher},
};

use sha2::{Digest, Sha256};
use tokio::task::JoinHandle;

use crate::traits::node_implementation::{ConsensusTime, NodeType};
use crate::traits::{
node_implementation::{ConsensusTime, NodeType},
signature_key::SignatureKey,
};

// TODO: Add the following consts once we bench the hash time.
// <https://github.com/EspressoSystems/HotShot/issues/3880>
Expand Down Expand Up @@ -70,6 +76,31 @@ pub fn compute_drb_result<TYPES: NodeType>(drb_seed_input: DrbSeedInput) -> DrbR
drb_result
}

/// Use the DRB result to get the leader.
///
/// The DRB result is the output of a spawned `compute_drb_result` call.
#[must_use]
pub fn leader<TYPES: NodeType>(
view_number: TYPES::View,
stake_table: &[<TYPES::SignatureKey as SignatureKey>::StakeTableEntry],
drb_result: DrbResult,
) -> TYPES::SignatureKey {
let mut hasher = DefaultHasher::new();

drb_result.hash(&mut hasher);
view_number.hash(&mut hasher);

#[allow(clippy::cast_possible_truncation)]
let hash = hasher.finish() as usize;

// TODO: Use the total stake rather than `len()` and update the indexing after switching to
// a weighted stake table.
// <https://github.com/EspressoSystems/HotShot/issues/3898>
let index = hash % stake_table.len();
let entry = stake_table[index].clone();
TYPES::SignatureKey::public_key(&entry)
}

/// Alias for in-progress DRB computation task, if there's any.
pub type DrbComputation<TYPES> = Option<(<TYPES as NodeType>::Epoch, JoinHandle<DrbResult>)>;

Expand Down
3 changes: 3 additions & 0 deletions hotshot/src/traits/election/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub mod randomized_committee_members;
/// static (round robin) committee election
pub mod static_committee;

/// static (round robin) committee election with DRB
pub mod static_committee_with_drb;

/// static (round robin leader for 2 consecutive views) committee election
pub mod static_committee_leader_two_views;
/// two static (round robin) committees for even and odd epochs
Expand Down
Loading

0 comments on commit 5f82edc

Please sign in to comment.