@@ -17,6 +17,8 @@ use super::*;
1717
1818use crate :: narwhal:: BatchHeader ;
1919
20+ use anyhow:: { Context , bail} ;
21+
2022impl < N : Network , C : ConsensusStorage < N > > Ledger < N , C > {
2123 /// Checks the given block is valid next block.
2224 pub fn check_next_block < R : CryptoRng + Rng > ( & self , block : & Block < N > , rng : & mut R ) -> Result < ( ) > {
@@ -40,29 +42,6 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
4042 }
4143 }
4244
43- // TODO (howardwu): Remove this after moving the total supply into credits.aleo.
44- {
45- // // Retrieve the latest total supply.
46- // let latest_total_supply = self.latest_total_supply_in_microcredits();
47- // // Retrieve the block reward from the first block ratification.
48- // let block_reward = match block.ratifications()[0] {
49- // Ratify::BlockReward(block_reward) => block_reward,
50- // _ => bail!("Block {height} is invalid - the first ratification must be a block reward"),
51- // };
52- // // Retrieve the puzzle reward from the second block ratification.
53- // let puzzle_reward = match block.ratifications()[1] {
54- // Ratify::PuzzleReward(puzzle_reward) => puzzle_reward,
55- // _ => bail!("Block {height} is invalid - the second ratification must be a puzzle reward"),
56- // };
57- // // Compute the next total supply in microcredits.
58- // let next_total_supply_in_microcredits =
59- // update_total_supply(latest_total_supply, block_reward, puzzle_reward, block.transactions())?;
60- // // Ensure the total supply in microcredits is correct.
61- // if next_total_supply_in_microcredits != block.total_supply_in_microcredits() {
62- // bail!("Invalid total supply in microcredits")
63- // }
64- }
65-
6645 // Construct the finalize state.
6746 let state = FinalizeGlobalState :: new :: < N > (
6847 block. round ( ) ,
@@ -74,14 +53,17 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
7453
7554 // Ensure speculation over the unconfirmed transactions is correct and ensure each transaction is well-formed and unique.
7655 let time_since_last_block = block. timestamp ( ) . saturating_sub ( self . latest_timestamp ( ) ) ;
77- let ratified_finalize_operations = self . vm . check_speculate (
78- state,
79- time_since_last_block,
80- block. ratifications ( ) ,
81- block. solutions ( ) ,
82- block. transactions ( ) ,
83- rng,
84- ) ?;
56+ let ratified_finalize_operations = self
57+ . vm
58+ . check_speculate (
59+ state,
60+ time_since_last_block,
61+ block. ratifications ( ) ,
62+ block. solutions ( ) ,
63+ block. transactions ( ) ,
64+ rng,
65+ )
66+ . with_context ( || "Failed to speculate over unconfirmed transactions" ) ?;
8567
8668 // Retrieve the committee lookback.
8769 let committee_lookback = self
@@ -98,16 +80,18 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
9880 } ;
9981
10082 // Ensure the block is correct.
101- let ( expected_existing_solution_ids, expected_existing_transaction_ids) = block. verify (
102- & latest_block,
103- self . latest_state_root ( ) ,
104- & previous_committee_lookback,
105- & committee_lookback,
106- self . puzzle ( ) ,
107- self . latest_epoch_hash ( ) ?,
108- OffsetDateTime :: now_utc ( ) . unix_timestamp ( ) ,
109- ratified_finalize_operations,
110- ) ?;
83+ let ( expected_existing_solution_ids, expected_existing_transaction_ids) = block
84+ . verify (
85+ & latest_block,
86+ self . latest_state_root ( ) ,
87+ & previous_committee_lookback,
88+ & committee_lookback,
89+ self . puzzle ( ) ,
90+ self . latest_epoch_hash ( ) ?,
91+ OffsetDateTime :: now_utc ( ) . unix_timestamp ( ) ,
92+ ratified_finalize_operations,
93+ )
94+ . with_context ( || "Failed to verify block" ) ?;
11195
11296 // Ensure that the provers are within their stake bounds.
11397 if let Some ( solutions) = block. solutions ( ) . deref ( ) {
@@ -130,7 +114,7 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
130114 // Determine if the block subdag is correctly constructed and is not a combination of multiple subdags.
131115 self . check_block_subdag_atomicity ( block) ?;
132116
133- // Ensure that all leafs of the subdag point to valid batches in other subdags/blocks.
117+ // Ensure that all leaves of the subdag point to valid batches in other subdags/blocks.
134118 self . check_block_subdag_leaves ( block) ?;
135119
136120 // Ensure that each existing solution ID from the block exists in the ledger.
@@ -204,8 +188,9 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
204188 cfg_iter ! ( subdag) . try_for_each ( |( round, certificates) | {
205189 // Retrieve the committee lookback for the round.
206190 let committee_lookback = self
207- . get_committee_lookback_for_round ( * round) ?
208- . ok_or_else ( || anyhow ! ( "No committee lookback found for round {round}" ) ) ?;
191+ . get_committee_lookback_for_round ( * round)
192+ . with_context ( || format ! ( "Failed to get committee lookback for round {round}" ) ) ?
193+ . ok_or_else ( || anyhow ! ( "No committee lookback for round {round}" ) ) ?;
209194
210195 // Check that each certificate for this round has met quorum requirements.
211196 // Note that we do not need to check the quorum requirement for the previous certificates
@@ -273,7 +258,7 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
273258 // Compute the leader for the commit round.
274259 let computed_leader = previous_committee_lookback
275260 . get_leader ( round)
276- . map_err ( |e| anyhow ! ( "Failed to compute leader for round {round}: {e }" ) ) ?;
261+ . with_context ( || format ! ( "Failed to compute leader for round {round}" ) ) ?;
277262
278263 // Retrieve the previous leader certificates.
279264 let previous_certificate = match subdag. get ( & round) . and_then ( |certificates| {
0 commit comments