From 870cd70f1d5da48722d767ec0415361aba1ea2f2 Mon Sep 17 00:00:00 2001 From: Howard Wu <9260812+howardwu@users.noreply.github.com> Date: Fri, 16 Feb 2024 10:36:50 -0800 Subject: [PATCH] nit: scope the committee lookback logic --- ledger/src/advance.rs | 28 ++++++++++-------- ledger/src/check_next_block.rs | 54 ++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/ledger/src/advance.rs b/ledger/src/advance.rs index 1b2a7b9140..62a2775832 100644 --- a/ledger/src/advance.rs +++ b/ledger/src/advance.rs @@ -240,21 +240,23 @@ impl> Ledger { // 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::::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::::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) } diff --git a/ledger/src/check_next_block.rs b/ledger/src/check_next_block.rs index 3a22a21129..c23797f8d4 100644 --- a/ledger/src/check_next_block.rs +++ b/ledger/src/check_next_block.rs @@ -82,34 +82,38 @@ impl> Ledger { 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::::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::::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::::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::::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(