Skip to content

Commit

Permalink
3967 Option<Epoch> for everybody
Browse files Browse the repository at this point in the history
  • Loading branch information
pls148 committed Jan 15, 2025
1 parent 7dbc0e1 commit 2acccd1
Show file tree
Hide file tree
Showing 63 changed files with 877 additions and 629 deletions.
5 changes: 2 additions & 3 deletions crates/example-types/src/node_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ impl Versions for EpochUpgradeTestVersions {
mod tests {
use committable::{Commitment, Committable};
use hotshot_types::{
data::EpochNumber,
impl_has_epoch,
message::UpgradeLock,
simple_vote::{HasEpoch, VersionedVoteData},
Expand All @@ -333,7 +332,7 @@ mod tests {
/// Dummy data used for test
struct TestData<TYPES: NodeType> {
data: u64,
epoch: TYPES::Epoch,
epoch: Option<TYPES::Epoch>,
}

impl<TYPES: NodeType> Committable for TestData<TYPES> {
Expand All @@ -353,7 +352,7 @@ mod tests {

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

let view_0 = <TestTypes as NodeType>::View::new(0);
Expand Down
18 changes: 15 additions & 3 deletions crates/example-types/src/storage_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct TestStorageState<TYPES: NodeType> {
next_epoch_high_qc2:
Option<hotshot_types::simple_certificate::NextEpochQuorumCertificate2<TYPES>>,
action: TYPES::View,
epoch: TYPES::Epoch,
epoch: Option<TYPES::Epoch>,
}

impl<TYPES: NodeType> Default for TestStorageState<TYPES> {
Expand All @@ -71,7 +71,7 @@ impl<TYPES: NodeType> Default for TestStorageState<TYPES> {
next_epoch_high_qc2: None,
high_qc2: None,
action: TYPES::View::genesis(),
epoch: TYPES::Epoch::genesis(),
epoch: None,
}
}
}
Expand Down Expand Up @@ -112,19 +112,24 @@ impl<TYPES: NodeType> TestStorage<TYPES> {
) -> BTreeMap<TYPES::View, Proposal<TYPES, QuorumProposal2<TYPES>>> {
self.inner.read().await.proposals2.clone()
}

pub async fn high_qc_cloned(&self) -> Option<QuorumCertificate2<TYPES>> {
self.inner.read().await.high_qc2.clone()
}

pub async fn next_epoch_high_qc_cloned(&self) -> Option<NextEpochQuorumCertificate2<TYPES>> {
self.inner.read().await.next_epoch_high_qc2.clone()
}

pub async fn decided_upgrade_certificate(&self) -> Option<UpgradeCertificate<TYPES>> {
self.decided_upgrade_certificate.read().await.clone()
}

pub async fn last_actioned_view(&self) -> TYPES::View {
self.inner.read().await.action
}
pub async fn last_actioned_epoch(&self) -> TYPES::Epoch {

pub async fn last_actioned_epoch(&self) -> Option<TYPES::Epoch> {
self.inner.read().await.epoch
}
}
Expand Down Expand Up @@ -177,6 +182,7 @@ impl<TYPES: NodeType> Storage<TYPES> for TestStorage<TYPES> {
.insert(proposal.data.view_number, proposal.clone());
Ok(())
}

async fn append_da2(
&self,
proposal: &Proposal<TYPES, DaProposal2<TYPES>>,
Expand All @@ -192,6 +198,7 @@ impl<TYPES: NodeType> Storage<TYPES> for TestStorage<TYPES> {
.insert(proposal.data.view_number, proposal.clone());
Ok(())
}

async fn append_proposal(
&self,
proposal: &Proposal<TYPES, QuorumProposal<TYPES>>,
Expand All @@ -206,6 +213,7 @@ impl<TYPES: NodeType> Storage<TYPES> for TestStorage<TYPES> {
.insert(proposal.data.view_number, proposal.clone());
Ok(())
}

async fn append_proposal2(
&self,
proposal: &Proposal<TYPES, QuorumProposal2<TYPES>>,
Expand Down Expand Up @@ -274,6 +282,7 @@ impl<TYPES: NodeType> Storage<TYPES> for TestStorage<TYPES> {
}
Ok(())
}

async fn update_next_epoch_high_qc2(
&self,
new_next_epoch_high_qc: hotshot_types::simple_certificate::NextEpochQuorumCertificate2<
Expand All @@ -294,6 +303,7 @@ impl<TYPES: NodeType> Storage<TYPES> for TestStorage<TYPES> {
}
Ok(())
}

async fn update_undecided_state(
&self,
_leaves: CommitmentMap<Leaf<TYPES>>,
Expand All @@ -305,6 +315,7 @@ impl<TYPES: NodeType> Storage<TYPES> for TestStorage<TYPES> {
Self::run_delay_settings_from_config(&self.delay_config).await;
Ok(())
}

async fn update_undecided_state2(
&self,
_leaves: CommitmentMap<Leaf2<TYPES>>,
Expand All @@ -316,6 +327,7 @@ impl<TYPES: NodeType> Storage<TYPES> for TestStorage<TYPES> {
Self::run_delay_settings_from_config(&self.delay_config).await;
Ok(())
}

async fn update_decided_upgrade_certificate(
&self,
decided_upgrade_certificate: Option<UpgradeCertificate<TYPES>>,
Expand Down
11 changes: 8 additions & 3 deletions crates/examples/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ use hotshot_types::{
node_implementation::{ConsensusTime, NodeType, Versions},
states::TestableState,
},
utils::genesis_epoch_from_version,
HotShotConfig, PeerConfig, ValidatorConfig,
};
use libp2p_networking::network::{
Expand Down Expand Up @@ -524,11 +525,15 @@ pub trait RunDa<
.memberships
.read()
.await
.committee_leaders(TYPES::View::genesis(), TYPES::Epoch::genesis())
.committee_leaders(
TYPES::View::genesis(),
genesis_epoch_from_version::<V, TYPES>(),
)
.len();
let consensus_lock = context.hotshot.consensus();
let consensus = consensus_lock.read().await;
let total_num_views = usize::try_from(consensus.locked_view().u64()).unwrap();
let consensus_reader = consensus_lock.read().await;
let total_num_views = usize::try_from(consensus_reader.locked_view().u64()).unwrap();
drop(consensus_reader);
// `failed_num_views` could include uncommitted views
let failed_num_views = total_num_views - num_successful_commits;
// When posting to the orchestrator, note that the total number of views also include un-finalized views.
Expand Down
37 changes: 25 additions & 12 deletions crates/hotshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ use hotshot_types::{
states::ValidatedState,
storage::Storage,
},
utils::epoch_from_block_number,
utils::{genesis_epoch_from_version, option_epoch_from_block_number},
HotShotConfig,
};
/// Reexport rand crate
pub use rand;
use tokio::{spawn, time::sleep};
use tracing::{debug, instrument, trace};
use vbs::version::StaticVersionType;

// -- Rexports
// External
Expand Down Expand Up @@ -122,7 +123,7 @@ pub struct SystemContext<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versi
start_view: TYPES::View,

/// The epoch to enter when first starting consensus
start_epoch: TYPES::Epoch,
start_epoch: Option<TYPES::Epoch>,

/// Access to the output event stream.
output_event_stream: (Sender<Event<TYPES>>, InactiveReceiver<Event<TYPES>>),
Expand Down Expand Up @@ -291,10 +292,13 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
)),
};

let epoch = TYPES::Epoch::new(epoch_from_block_number(
// #3967 REVIEW NOTE: Should this actually be Some()? How do we know?
let epoch = option_epoch_from_block_number::<TYPES>(
V::Base::VERSION >= V::Epochs::VERSION,
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 Down Expand Up @@ -323,12 +327,17 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
saved_payloads.insert(anchored_leaf.view_number(), Arc::new(payload));
}

// #3967 REVIEW NOTE: Check the logic in the following expr
let anchored_epoch = if config.epoch_height == 0 {
TYPES::Epoch::new(0)
None
} else if anchored_leaf.height() % config.epoch_height == 0 {
TYPES::Epoch::new(anchored_leaf.height() / config.epoch_height)
Some(TYPES::Epoch::new(
anchored_leaf.height() / config.epoch_height,
))
} else {
TYPES::Epoch::new(anchored_leaf.height() / config.epoch_height + 1)
Some(TYPES::Epoch::new(
anchored_leaf.height() / config.epoch_height + 1,
))
};
let consensus = Consensus::new(
validated_state_map,
Expand Down Expand Up @@ -415,7 +424,10 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> SystemContext<T
async move {
sleep(Duration::from_millis(next_view_timeout)).await;
broadcast_event(
Arc::new(HotShotEvent::Timeout(start_view + 1, start_epoch + 1)),
Arc::new(HotShotEvent::Timeout(
start_view + 1,
start_epoch.map(|x| x + 1),
)),
&event_stream,
)
.await;
Expand Down Expand Up @@ -1003,7 +1015,7 @@ pub struct HotShotInitializer<TYPES: NodeType> {
/// Starting view number that should be equivalent to the view the node shut down with last.
start_view: TYPES::View,
/// Starting epoch number that should be equivalent to the epoch the node shut down with last.
start_epoch: TYPES::Epoch,
start_epoch: Option<TYPES::Epoch>,
/// The view we last performed an action in. An action is Proposing or voting for
/// Either the quorum or DA.
actioned_view: TYPES::View,
Expand Down Expand Up @@ -1035,11 +1047,12 @@ impl<TYPES: NodeType> HotShotInitializer<TYPES> {
let high_qc = QuorumCertificate2::genesis::<V>(&validated_state, &instance_state).await;

Ok(Self {
inner: Leaf2::genesis(&validated_state, &instance_state).await,
// CHECK IN REVIEW: does it make sense for this Leaf2 to be with_epoch=false?
inner: Leaf2::genesis::<V>(&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),
start_epoch: TYPES::Epoch::new(0),
start_epoch: genesis_epoch_from_version::<V, TYPES>(),
actioned_view: TYPES::View::new(0),
saved_proposals: BTreeMap::new(),
high_qc,
Expand All @@ -1064,7 +1077,7 @@ impl<TYPES: NodeType> HotShotInitializer<TYPES> {
instance_state: TYPES::InstanceState,
validated_state: Option<Arc<TYPES::ValidatedState>>,
start_view: TYPES::View,
start_epoch: TYPES::Epoch,
start_epoch: Option<TYPES::Epoch>,
actioned_view: TYPES::View,
saved_proposals: BTreeMap<TYPES::View, Proposal<TYPES, QuorumProposal2<TYPES>>>,
high_qc: QuorumCertificate2<TYPES>,
Expand Down
6 changes: 3 additions & 3 deletions crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ use tokio::{spawn, time::sleep};
use vbs::version::StaticVersionType;

use crate::{
tasks::task_state::CreateTaskState, types::SystemContextHandle, ConsensusApi,
ConsensusMetricsValue, ConsensusTaskRegistry, HotShotConfig, HotShotInitializer,
genesis_epoch_from_version, tasks::task_state::CreateTaskState, types::SystemContextHandle,
ConsensusApi, ConsensusMetricsValue, ConsensusTaskRegistry, HotShotConfig, HotShotInitializer,
MarketplaceConfig, NetworkTaskRegistry, SignatureKey, SystemContext, Versions,
};

Expand Down Expand Up @@ -197,7 +197,7 @@ pub fn add_network_event_task<
let network_state: NetworkEventTaskState<_, V, _, _> = NetworkEventTaskState {
network,
view: TYPES::View::genesis(),
epoch: TYPES::Epoch::genesis(),
epoch: genesis_epoch_from_version::<V, TYPES>(),
membership,
storage: Arc::clone(&handle.storage()),
consensus: OuterConsensus::new(handle.consensus()),
Expand Down
Loading

0 comments on commit 2acccd1

Please sign in to comment.