Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into rm/remove-orchestrato…
Browse files Browse the repository at this point in the history
…r-part-2
  • Loading branch information
rob-maron committed Dec 12, 2024
2 parents 635cfac + 43263a5 commit e10b248
Show file tree
Hide file tree
Showing 47 changed files with 1,911 additions and 587 deletions.
90 changes: 69 additions & 21 deletions crates/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,
two_static_committees::TwoStaticCommittees,
},
implementations::{CombinedNetworks, Libp2pNetwork, MemoryNetwork, PushCdnNetwork},
NodeImplementation,
Expand Down Expand Up @@ -51,6 +52,8 @@ use crate::{
/// to select our traits
pub struct TestTypes;
impl NodeType for TestTypes {
const EPOCH_HEIGHT: u64 = 10;

type AuctionResult = TestAuctionResult;
type View = ViewNumber;
type Epoch = EpochNumber;
Expand Down Expand Up @@ -81,6 +84,8 @@ impl NodeType for TestTypes {
/// to select our traits
pub struct TestTypesRandomizedLeader;
impl NodeType for TestTypesRandomizedLeader {
const EPOCH_HEIGHT: u64 = 10;

type AuctionResult = TestAuctionResult;
type View = ViewNumber;
type Epoch = EpochNumber;
Expand Down Expand Up @@ -114,6 +119,8 @@ pub struct TestTypesRandomizedCommitteeMembers<CONFIG: QuorumFilterConfig> {
}

impl<CONFIG: QuorumFilterConfig> NodeType for TestTypesRandomizedCommitteeMembers<CONFIG> {
const EPOCH_HEIGHT: u64 = 10;

type AuctionResult = TestAuctionResult;
type View = ViewNumber;
type Epoch = EpochNumber;
Expand Down Expand Up @@ -145,6 +152,8 @@ impl<CONFIG: QuorumFilterConfig> NodeType for TestTypesRandomizedCommitteeMember
/// to select our traits
pub struct TestConsecutiveLeaderTypes;
impl NodeType for TestConsecutiveLeaderTypes {
const EPOCH_HEIGHT: u64 = 10;

type AuctionResult = TestAuctionResult;
type View = ViewNumber;
type Epoch = EpochNumber;
Expand All @@ -158,6 +167,38 @@ impl NodeType for TestConsecutiveLeaderTypes {
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 TestTwoStakeTablesTypes;
impl NodeType for TestTwoStakeTablesTypes {
const EPOCH_HEIGHT: u64 = 10;

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 = TwoStaticCommittees<TestTwoStakeTablesTypes>;
type BuilderSignatureKey = BuilderKey;
}

/// The Push CDN implementation
#[derive(Clone, Debug, Deserialize, Serialize, Hash, Eq, PartialEq)]
pub struct PushCdnImpl;
Expand Down Expand Up @@ -264,7 +305,7 @@ impl Versions for EpochsTestVersions {
0, 0,
];

type Marketplace = StaticVersion<0, 99>;
type Marketplace = StaticVersion<0, 3>;

type Epochs = StaticVersion<0, 4>;
}
Expand All @@ -273,52 +314,59 @@ impl Versions for EpochsTestVersions {
mod tests {
use committable::{Commitment, Committable};
use hotshot_types::{
message::UpgradeLock, simple_vote::VersionedVoteData,
data::EpochNumber,
impl_has_epoch,
message::UpgradeLock,
simple_vote::{HasEpoch, VersionedVoteData},
traits::node_implementation::ConsensusTime,
};
use serde::{Deserialize, Serialize};

use crate::node_types::{MarketplaceTestVersions, NodeType, TestTypes};
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Hash, Eq)]
/// Dummy data used for test
struct TestData {
struct TestData<TYPES: NodeType> {
data: u64,
epoch: TYPES::Epoch,
}

impl Committable for TestData {
impl<TYPES: NodeType> Committable for TestData<TYPES> {
fn commit(&self) -> Commitment<Self> {
committable::RawCommitmentBuilder::new("Test data")
.u64(self.data)
.finalize()
}
}

impl_has_epoch!(TestData<TYPES>);

#[tokio::test(flavor = "multi_thread")]
/// Test that the view number affects the commitment post-marketplace
async fn test_versioned_commitment_includes_view() {
let upgrade_lock = UpgradeLock::new();

let data = TestData { data: 10 };
let data = TestData {
data: 10,
epoch: EpochNumber::new(0),
};

let view_0 = <TestTypes as NodeType>::View::new(0);
let view_1 = <TestTypes as NodeType>::View::new(1);

let versioned_data_0 =
VersionedVoteData::<TestTypes, TestData, MarketplaceTestVersions>::new(
data,
view_0,
&upgrade_lock,
)
.await
.unwrap();
let versioned_data_1 =
VersionedVoteData::<TestTypes, TestData, MarketplaceTestVersions>::new(
data,
view_1,
&upgrade_lock,
)
.await
.unwrap();
let versioned_data_0 = VersionedVoteData::<
TestTypes,
TestData<TestTypes>,
MarketplaceTestVersions,
>::new(data, view_0, &upgrade_lock)
.await
.unwrap();
let versioned_data_1 = VersionedVoteData::<
TestTypes,
TestData<TestTypes>,
MarketplaceTestVersions,
>::new(data, view_1, &upgrade_lock)
.await
.unwrap();

let versioned_data_commitment_0: [u8; 32] = versioned_data_0.commit().into();
let versioned_data_commitment_1: [u8; 32] = versioned_data_1.commit().into();
Expand Down
25 changes: 25 additions & 0 deletions crates/example-types/src/storage_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use hotshot_types::{
consensus::CommitmentMap,
data::{
DaProposal, DaProposal2, Leaf, Leaf2, QuorumProposal, QuorumProposal2, VidDisperseShare,
VidDisperseShare2,
},
event::HotShotAction,
message::Proposal,
Expand All @@ -36,9 +37,15 @@ type VidShares<TYPES> = HashMap<
<TYPES as NodeType>::View,
HashMap<<TYPES as NodeType>::SignatureKey, Proposal<TYPES, VidDisperseShare<TYPES>>>,
>;
type VidShares2<TYPES> = HashMap<
<TYPES as NodeType>::View,
HashMap<<TYPES as NodeType>::SignatureKey, Proposal<TYPES, VidDisperseShare2<TYPES>>>,
>;

#[derive(Clone, Debug)]
pub struct TestStorageState<TYPES: NodeType> {
vids: VidShares<TYPES>,
vid2: VidShares2<TYPES>,
das: HashMap<TYPES::View, Proposal<TYPES, DaProposal<TYPES>>>,
da2s: HashMap<TYPES::View, Proposal<TYPES, DaProposal2<TYPES>>>,
proposals: BTreeMap<TYPES::View, Proposal<TYPES, QuorumProposal<TYPES>>>,
Expand All @@ -53,6 +60,7 @@ impl<TYPES: NodeType> Default for TestStorageState<TYPES> {
fn default() -> Self {
Self {
vids: HashMap::new(),
vid2: HashMap::new(),
das: HashMap::new(),
da2s: HashMap::new(),
proposals: BTreeMap::new(),
Expand Down Expand Up @@ -131,6 +139,23 @@ impl<TYPES: NodeType> Storage<TYPES> for TestStorage<TYPES> {
Ok(())
}

async fn append_vid2(
&self,
proposal: &Proposal<TYPES, VidDisperseShare2<TYPES>>,
) -> Result<()> {
if self.should_return_err {
bail!("Failed to append VID proposal to storage");
}
Self::run_delay_settings_from_config(&self.delay_config).await;
let mut inner = self.inner.write().await;
inner
.vid2
.entry(proposal.data.view_number)
.or_default()
.insert(proposal.data.recipient_key.clone(), proposal.clone());
Ok(())
}

async fn append_da(
&self,
proposal: &Proposal<TYPES, DaProposal<TYPES>>,
Expand Down
33 changes: 19 additions & 14 deletions crates/hotshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ pub use hotshot_types::error::HotShotError;
use hotshot_types::{
consensus::{Consensus, ConsensusMetricsValue, OuterConsensus, View, ViewInner},
constants::{EVENT_CHANNEL_SIZE, EXTERNAL_EVENT_CHANNEL_SIZE},
data::{Leaf, Leaf2, QuorumProposal, QuorumProposal2},
data::{Leaf2, QuorumProposal, QuorumProposal2},
event::{EventType, LeafInfo},
message::{convert_proposal, DataMessage, Message, MessageKind, Proposal},
simple_certificate::{QuorumCertificate, QuorumCertificate2, UpgradeCertificate},
simple_certificate::{QuorumCertificate2, UpgradeCertificate},
traits::{
consensus_api::ConsensusApi,
election::Membership,
Expand All @@ -62,15 +62,16 @@ use hotshot_types::{
storage::Storage,
EncodeBytes,
},
utils::epoch_from_block_number,
HotShotConfig,
};
// -- Rexports
// External
/// Reexport rand crate
pub use rand;
use tokio::{spawn, time::sleep};
use tracing::{debug, instrument, trace};

// -- Rexports
// External
use crate::{
tasks::{add_consensus_tasks, add_network_tasks},
traits::NodeImplementation,
Expand Down Expand Up @@ -283,6 +284,10 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
)),
};

let epoch = TYPES::Epoch::new(epoch_from_block_number(
anchored_leaf.height(),
config.epoch_height,
));
// Insert the validated state to state map.
let mut validated_state_map = BTreeMap::default();
validated_state_map.insert(
Expand All @@ -292,6 +297,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
leaf: anchored_leaf.commit(),
state: Arc::clone(&validated_state),
delta: initializer.state_delta.clone(),
epoch,
},
},
);
Expand Down Expand Up @@ -394,14 +400,15 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
let event_stream = self.internal_event_stream.0.clone();
let next_view_timeout = self.config.next_view_timeout;
let start_view = self.start_view;
let start_epoch = self.start_epoch;

// Spawn a task that will sleep for the next view timeout and then send a timeout event
// if not cancelled
spawn({
async move {
sleep(Duration::from_millis(next_view_timeout)).await;
broadcast_event(
Arc::new(HotShotEvent::Timeout(start_view + 1)),
Arc::new(HotShotEvent::Timeout(start_view + 1, start_epoch + 1)),
&event_stream,
)
.await;
Expand Down Expand Up @@ -429,9 +436,11 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
TYPES::ValidatedState::genesis(&self.instance_state);

let qc = Arc::new(
QuorumCertificate::genesis::<V>(&validated_state, self.instance_state.as_ref())
.await
.to_qc2(),
QuorumCertificate2::genesis::<V>(
&validated_state,
self.instance_state.as_ref(),
)
.await,
);

broadcast_event(
Expand Down Expand Up @@ -996,14 +1005,10 @@ impl<TYPES: NodeType> HotShotInitializer<TYPES> {
instance_state: TYPES::InstanceState,
) -> Result<Self, HotShotError<TYPES>> {
let (validated_state, state_delta) = TYPES::ValidatedState::genesis(&instance_state);
let high_qc = QuorumCertificate::genesis::<V>(&validated_state, &instance_state)
.await
.to_qc2();
let high_qc = QuorumCertificate2::genesis::<V>(&validated_state, &instance_state).await;

Ok(Self {
inner: Leaf::genesis(&validated_state, &instance_state)
.await
.into(),
inner: Leaf2::genesis(&validated_state, &instance_state).await,
validated_state: Some(Arc::new(validated_state)),
state_delta: Some(Arc::new(state_delta)),
start_view: TYPES::View::new(0),
Expand Down
1 change: 1 addition & 0 deletions crates/hotshot/src/tasks/task_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState

Self {
latest_proposed_view: handle.cur_view().await,
cur_epoch: handle.cur_epoch().await,
proposal_dependencies: BTreeMap::new(),
consensus: OuterConsensus::new(consensus),
instance_state: handle.hotshot.instance_state(),
Expand Down
2 changes: 2 additions & 0 deletions crates/hotshot/src/traits/election/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub mod static_committee;

/// 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
pub mod two_static_committees;

/// general helpers
pub mod helpers;
Loading

0 comments on commit e10b248

Please sign in to comment.