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 11, 2025
1 parent 5034a64 commit 64559d9
Show file tree
Hide file tree
Showing 61 changed files with 852 additions and 634 deletions.
4 changes: 2 additions & 2 deletions crates/example-types/src/node_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,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 +353,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
7 changes: 4 additions & 3 deletions crates/examples/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,12 @@ pub trait RunDa<
.memberships
.read()
.await
.committee_leaders(TYPES::View::genesis(), TYPES::Epoch::genesis())
.committee_leaders(TYPES::View::genesis(), None)
.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
32 changes: 21 additions & 11 deletions crates/hotshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,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 +291,11 @@ 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 = Some(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 Down Expand Up @@ -323,12 +324,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 +421,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 +1012,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 +1044,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: None,
actioned_view: TYPES::View::new(0),
saved_proposals: BTreeMap::new(),
high_qc,
Expand All @@ -1064,7 +1074,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
2 changes: 1 addition & 1 deletion crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
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: None,
membership,
storage: Arc::clone(&handle.storage()),
consensus: OuterConsensus::new(handle.consensus()),
Expand Down
34 changes: 17 additions & 17 deletions crates/hotshot/src/traits/election/randomized_committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
/// Get the stake table for the current view
fn stake_table(
&self,
_epoch: <TYPES as NodeType>::Epoch,
_epoch: Option<<TYPES as NodeType>::Epoch>,
) -> Vec<<<TYPES as NodeType>::SignatureKey as SignatureKey>::StakeTableEntry> {
self.stake_table.clone()
}

/// Get the stake table for the current view
fn da_stake_table(
&self,
_epoch: <TYPES as NodeType>::Epoch,
_epoch: Option<<TYPES as NodeType>::Epoch>,
) -> Vec<<<TYPES as NodeType>::SignatureKey as SignatureKey>::StakeTableEntry> {
self.da_stake_table.clone()
}
Expand All @@ -121,7 +121,7 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
fn committee_members(
&self,
_view_number: <TYPES as NodeType>::View,
_epoch: <TYPES as NodeType>::Epoch,
_epoch: Option<<TYPES as NodeType>::Epoch>,
) -> std::collections::BTreeSet<<TYPES as NodeType>::SignatureKey> {
self.stake_table
.iter()
Expand All @@ -133,7 +133,7 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
fn da_committee_members(
&self,
_view_number: <TYPES as NodeType>::View,
_epoch: <TYPES as NodeType>::Epoch,
_epoch: Option<<TYPES as NodeType>::Epoch>,
) -> std::collections::BTreeSet<<TYPES as NodeType>::SignatureKey> {
self.da_stake_table
.iter()
Expand All @@ -145,7 +145,7 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
fn committee_leaders(
&self,
_view_number: <TYPES as NodeType>::View,
_epoch: <TYPES as NodeType>::Epoch,
_epoch: Option<<TYPES as NodeType>::Epoch>,
) -> std::collections::BTreeSet<<TYPES as NodeType>::SignatureKey> {
self.eligible_leaders
.iter()
Expand All @@ -157,7 +157,7 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
fn stake(
&self,
pub_key: &<TYPES as NodeType>::SignatureKey,
_epoch: <TYPES as NodeType>::Epoch,
_epoch: Option<<TYPES as NodeType>::Epoch>,
) -> Option<<TYPES::SignatureKey as SignatureKey>::StakeTableEntry> {
// Only return the stake if it is above zero
self.indexed_stake_table.get(pub_key).cloned()
Expand All @@ -167,7 +167,7 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
fn da_stake(
&self,
pub_key: &<TYPES as NodeType>::SignatureKey,
_epoch: <TYPES as NodeType>::Epoch,
_epoch: Option<<TYPES as NodeType>::Epoch>,
) -> Option<<TYPES::SignatureKey as SignatureKey>::StakeTableEntry> {
// Only return the stake if it is above zero
self.indexed_da_stake_table.get(pub_key).cloned()
Expand All @@ -177,7 +177,7 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
fn has_stake(
&self,
pub_key: &<TYPES as NodeType>::SignatureKey,
_epoch: <TYPES as NodeType>::Epoch,
_epoch: Option<<TYPES as NodeType>::Epoch>,
) -> bool {
self.indexed_stake_table
.get(pub_key)
Expand All @@ -188,7 +188,7 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
fn has_da_stake(
&self,
pub_key: &<TYPES as NodeType>::SignatureKey,
_epoch: <TYPES as NodeType>::Epoch,
_epoch: Option<<TYPES as NodeType>::Epoch>,
) -> bool {
self.indexed_da_stake_table
.get(pub_key)
Expand All @@ -203,8 +203,8 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
/// Index the vector of public keys with the current view number
fn lookup_leader(
&self,
view_number: TYPES::View,
_epoch: <TYPES as NodeType>::Epoch,
view_number: <TYPES as NodeType>::View,
_epoch: Option<<TYPES as NodeType>::Epoch>,
) -> Result<TYPES::SignatureKey> {
let mut rng: StdRng = rand::SeedableRng::seed_from_u64(*view_number);

Expand All @@ -218,30 +218,30 @@ impl<TYPES: NodeType> Membership<TYPES> for RandomizedCommittee<TYPES> {
}

/// Get the total number of nodes in the committee
fn total_nodes(&self, _epoch: <TYPES as NodeType>::Epoch) -> usize {
fn total_nodes(&self, _epoch: Option<<TYPES as NodeType>::Epoch>) -> usize {
self.stake_table.len()
}
/// Get the total number of nodes in the committee
fn da_total_nodes(&self, _epoch: <TYPES as NodeType>::Epoch) -> usize {
fn da_total_nodes(&self, _epoch: Option<<TYPES as NodeType>::Epoch>) -> usize {
self.da_stake_table.len()
}
/// Get the voting success threshold for the committee
fn success_threshold(&self, _epoch: <TYPES as NodeType>::Epoch) -> NonZeroU64 {
fn success_threshold(&self, _epoch: Option<<TYPES as NodeType>::Epoch>) -> NonZeroU64 {
NonZeroU64::new(((self.stake_table.len() as u64 * 2) / 3) + 1).unwrap()
}

/// Get the voting success threshold for the committee
fn da_success_threshold(&self, _epoch: <TYPES as NodeType>::Epoch) -> NonZeroU64 {
fn da_success_threshold(&self, _epoch: Option<<TYPES as NodeType>::Epoch>) -> NonZeroU64 {
NonZeroU64::new(((self.da_stake_table.len() as u64 * 2) / 3) + 1).unwrap()
}

/// Get the voting failure threshold for the committee
fn failure_threshold(&self, _epoch: <TYPES as NodeType>::Epoch) -> NonZeroU64 {
fn failure_threshold(&self, _epoch: Option<<TYPES as NodeType>::Epoch>) -> NonZeroU64 {
NonZeroU64::new(((self.stake_table.len() as u64) / 3) + 1).unwrap()
}

/// Get the voting upgrade threshold for the committee
fn upgrade_threshold(&self, _epoch: <TYPES as NodeType>::Epoch) -> NonZeroU64 {
fn upgrade_threshold(&self, _epoch: Option<<TYPES as NodeType>::Epoch>) -> NonZeroU64 {
NonZeroU64::new(max(
(self.stake_table.len() as u64 * 9) / 10,
((self.stake_table.len() as u64 * 2) / 3) + 1,
Expand Down
Loading

0 comments on commit 64559d9

Please sign in to comment.