Skip to content

Commit

Permalink
nit: scope the committee lookback logic
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu committed Feb 16, 2024
1 parent bbd466f commit 870cd70
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
28 changes: 15 additions & 13 deletions ledger/src/advance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,23 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
// Determine the timestamp for the next block.
let next_timestamp = match subdag {
Some(subdag) => {
// Calculate the penultimate round, which is the round before the anchor round.
let penultimate_round = subdag.anchor_round().saturating_sub(1);
// Get the round number for the previous committee. Note, we subtract 2 from odd rounds,
// because committees are updated in even rounds.
let previous_penultimate_round = match penultimate_round % 2 == 0 {
true => penultimate_round.saturating_sub(1),
false => penultimate_round.saturating_sub(2),
};
// Get the previous committee lookback round.
let penultimate_committee_lookback_round =
previous_penultimate_round.saturating_sub(Committee::<N>::COMMITTEE_LOOKBACK_RANGE);
// Retrieve the previous committee lookback.
let previous_committee_lookback =
let previous_committee_lookback = {
// Calculate the penultimate round, which is the round before the anchor round.
let penultimate_round = subdag.anchor_round().saturating_sub(1);
// Get the round number for the previous committee. Note, we subtract 2 from odd rounds,
// because committees are updated in even rounds.
let previous_penultimate_round = match penultimate_round % 2 == 0 {
true => penultimate_round.saturating_sub(1),
false => penultimate_round.saturating_sub(2),
};
// Get the previous committee lookback round.
let penultimate_committee_lookback_round =
previous_penultimate_round.saturating_sub(Committee::<N>::COMMITTEE_LOOKBACK_RANGE);
// Output the previous committee lookback.
self.get_committee_for_round(penultimate_committee_lookback_round)?
.ok_or(anyhow!("Failed to fetch committee for round {penultimate_committee_lookback_round}"))?;
.ok_or(anyhow!("Failed to fetch committee for round {penultimate_committee_lookback_round}"))?
};
// Return the timestamp for the given committee lookback.
subdag.timestamp(&previous_committee_lookback)
}
Expand Down
54 changes: 29 additions & 25 deletions ledger/src/check_next_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,38 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
let ratified_finalize_operations =
self.vm.check_speculate(state, block.ratifications(), block.solutions(), block.transactions())?;

// Get the round number for the previous committee. Note, we subtract 2 from odd rounds,
// because committees are updated in even rounds.
let previous_round = match block.round() % 2 == 0 {
true => block.round().saturating_sub(1),
false => block.round().saturating_sub(2),
};
// Get the committee lookback round.
let committee_lookback_round = previous_round.saturating_sub(Committee::<N>::COMMITTEE_LOOKBACK_RANGE);
// Retrieve the committee lookback.
let committee_lookback = self
.get_committee_for_round(committee_lookback_round)?
.ok_or(anyhow!("Failed to fetch committee for round {committee_lookback_round}"))?;

// Calculate the penultimate round, which is the round before the anchor round.
let penultimate_round = block.round().saturating_sub(1);
// Get the round number for the previous committee. Note, we subtract 2 from odd rounds,
// because committees are updated in even rounds.
let previous_penultimate_round = match penultimate_round % 2 == 0 {
true => penultimate_round.saturating_sub(1),
false => penultimate_round.saturating_sub(2),
let committee_lookback = {
// Determine the round number for the previous committee. Note, we subtract 2 from odd rounds,
// because committees are updated in even rounds.
let previous_round = match block.round() % 2 == 0 {
true => block.round().saturating_sub(1),
false => block.round().saturating_sub(2),
};
// Determine the committee lookback round.
let committee_lookback_round = previous_round.saturating_sub(Committee::<N>::COMMITTEE_LOOKBACK_RANGE);
// Output the committee lookback.
self.get_committee_for_round(committee_lookback_round)?
.ok_or(anyhow!("Failed to fetch committee for round {committee_lookback_round}"))?
};
// Get the previous committee lookback round.
let penultimate_committee_lookback_round =
previous_penultimate_round.saturating_sub(Committee::<N>::COMMITTEE_LOOKBACK_RANGE);

// Retrieve the previous committee lookback.
let previous_committee_lookback = self
.get_committee_for_round(penultimate_committee_lookback_round)?
.ok_or(anyhow!("Failed to fetch committee for round {penultimate_committee_lookback_round}"))?;
let previous_committee_lookback = {
// Calculate the penultimate round, which is the round before the anchor round.
let penultimate_round = block.round().saturating_sub(1);
// Determine the round number for the previous committee. Note, we subtract 2 from odd rounds,
// because committees are updated in even rounds.
let previous_penultimate_round = match penultimate_round % 2 == 0 {
true => penultimate_round.saturating_sub(1),
false => penultimate_round.saturating_sub(2),
};
// Determine the previous committee lookback round.
let penultimate_committee_lookback_round =
previous_penultimate_round.saturating_sub(Committee::<N>::COMMITTEE_LOOKBACK_RANGE);
// Output the previous committee lookback.
self.get_committee_for_round(penultimate_committee_lookback_round)?
.ok_or(anyhow!("Failed to fetch committee for round {penultimate_committee_lookback_round}"))?
};

// Ensure the block is correct.
let (expected_existing_solution_ids, expected_existing_transaction_ids) = block.verify(
Expand Down

0 comments on commit 870cd70

Please sign in to comment.