From 5cd416e49ee2b7528cec34c2ca58f5215cae452e Mon Sep 17 00:00:00 2001 From: Federico Franzoni <8609060+fed-franz@users.noreply.github.com> Date: Thu, 13 Jun 2024 16:17:52 +0200 Subject: [PATCH 1/7] consensus: Rename most_recent_block (mrb) to tip --- consensus/README.md | 8 ++++---- consensus/src/aggregator.rs | 6 +++--- consensus/src/commons.rs | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/consensus/README.md b/consensus/README.md index b733a19263..8f98ceb267 100644 --- a/consensus/README.md +++ b/consensus/README.md @@ -47,15 +47,15 @@ let mut consensus = Consensus::new( Arc::new(Mutex::new(crate::mocks::SimpleDB::default())), ); -let mut most_recent_block = Block::default(); +let mut tip = Block::default(); loop { /// Provisioners list is retrieved from contract storage state. let provisioners = rusk::get_provisioners(); // Round update is the input data for any consensus round execution. - // Round update includes mostly data from most recent block. - let round_update = from(most_recent_block); + // Round update includes mostly data from the tip. + let round_update = from(tip); /// Consensus::Spin call initializes a consensus round /// and spawns main consensus tokio::tasks. @@ -76,7 +76,7 @@ loop { // Max Step Reached - happens only if no consensus is reached for up to 213 steps/71 iterations. } } - most_recent_block = winner; + tip = winner; /// Internally, consensus instance may accept future messages for next round. /// They will be drained on running the round, that's why same consensus instance is used for all round executions. diff --git a/consensus/src/aggregator.rs b/consensus/src/aggregator.rs index f6f80e1af8..1db6b57c6b 100644 --- a/consensus/src/aggregator.rs +++ b/consensus/src/aggregator.rs @@ -234,8 +234,8 @@ mod tests { // Also populate a vector of headers let mut p = Provisioners::empty(); let mut input = vec![]; - let mut mrb_header = Header::default(); - mrb_header.height = 0; + let mut tip_header = Header::default(); + tip_header.height = 0; for secret_key in sks { let pubkey_bls = node_data::bls::PublicKey::new( @@ -247,7 +247,7 @@ mod tests { let ru = RoundUpdate::new( pubkey_bls, secret_key, - &mrb_header, + &tip_header, HashMap::new(), ); diff --git a/consensus/src/commons.rs b/consensus/src/commons.rs index 3fe828e76f..07f2da4aed 100644 --- a/consensus/src/commons.rs +++ b/consensus/src/commons.rs @@ -42,17 +42,17 @@ impl RoundUpdate { pub fn new( pubkey_bls: PublicKey, secret_key: StakeSecretKey, - mrb_header: &Header, + tip_header: &Header, base_timeouts: TimeoutSet, ) -> Self { - let round = mrb_header.height + 1; + let round = tip_header.height + 1; RoundUpdate { round, pubkey_bls, secret_key, - cert: mrb_header.cert, - hash: mrb_header.hash, - seed: mrb_header.seed, + cert: tip_header.cert, + hash: tip_header.hash, + seed: tip_header.seed, base_timeouts, } } From 52ae7e659f9054b64fbb7896aba9502fb314058d Mon Sep 17 00:00:00 2001 From: Federico Franzoni <8609060+fed-franz@users.noreply.github.com> Date: Thu, 13 Jun 2024 16:18:15 +0200 Subject: [PATCH 2/7] node: Rename most_recent_block (mrb) to tip --- node/benches/accept.rs | 18 +++---- node/src/chain.rs | 14 ++--- node/src/chain/acceptor.rs | 100 ++++++++++++++++++------------------ node/src/chain/consensus.rs | 14 ++--- 4 files changed, 73 insertions(+), 73 deletions(-) diff --git a/node/benches/accept.rs b/node/benches/accept.rs index 698c3e7a67..fea46ec8dc 100644 --- a/node/benches/accept.rs +++ b/node/benches/accept.rs @@ -31,15 +31,15 @@ use rand::rngs::StdRng; use rand::SeedableRng; fn create_step_votes( - mrb_header: &ledger::Header, + tip_header: &ledger::Header, vote: &Vote, step: StepName, iteration: u8, provisioners: &Provisioners, keys: &[(node_data::bls::PublicKey, StakeSecretKey)], ) -> StepVotes { - let round = mrb_header.height + 1; - let seed = mrb_header.seed; + let round = tip_header.height + 1; + let seed = tip_header.seed; let generator = provisioners.get_generator(iteration, seed, round); @@ -56,7 +56,7 @@ fn create_step_votes( let ru = RoundUpdate::new( pk.clone(), *sk, - mrb_header, + tip_header, HashMap::default(), ); let sig = match step { @@ -118,7 +118,7 @@ pub fn verify_block_cert(c: &mut Criterion) { keys.push((pk.clone(), sk)); provisioners.add_member_with_value(pk, 1000000000000) } - let mrb_header = ledger::Header { + let tip_header = ledger::Header { seed: [5; 48].into(), ..Default::default() }; @@ -127,7 +127,7 @@ pub fn verify_block_cert(c: &mut Criterion) { let iteration = 0; let validation = create_step_votes( - &mrb_header, + &tip_header, &vote, StepName::Validation, iteration, @@ -135,7 +135,7 @@ pub fn verify_block_cert(c: &mut Criterion) { &keys[..], ); let ratification = create_step_votes( - &mrb_header, + &tip_header, &vote, StepName::Ratification, iteration, @@ -157,9 +157,9 @@ pub fn verify_block_cert(c: &mut Criterion) { b.to_async(FuturesExecutor).iter(|| async { chain::verify_block_cert( [0u8; 32], - mrb_header.seed, + tip_header.seed, &provisioners, - mrb_header.height + 1, + tip_header.height + 1, &cert, iteration, ) diff --git a/node/src/chain.rs b/node/src/chain.rs index 825213517e..17b9c4222d 100644 --- a/node/src/chain.rs +++ b/node/src/chain.rs @@ -61,15 +61,15 @@ impl db: Arc>, vm: Arc>, ) -> anyhow::Result<()> { - let mrb = Self::load_most_recent_block(db.clone(), vm.clone()).await?; + let tip = Self::load_tip(db.clone(), vm.clone()).await?; - let state_hash = mrb.inner().header().state_hash; + let state_hash = tip.inner().header().state_hash; let provisioners_list = vm.read().await.get_provisioners(state_hash)?; // Initialize Acceptor let acc = Acceptor::init_consensus( &self.keys_path, - mrb, + tip, provisioners_list, db, network.clone(), @@ -224,18 +224,18 @@ impl ChainSrv { } } - /// Load both most recent and last_finalized blocks from persisted ledger. + /// Load both the chain tip and last finalized block from persisted ledger. /// /// Panics /// /// If register entry is read but block is not found. - async fn load_most_recent_block( + async fn load_tip( db: Arc>, vm: Arc>, ) -> Result { let stored_block = db.read().await.update(|t| { - Ok(t.op_read(MD_HASH_KEY)?.and_then(|mrb_hash| { - t.fetch_block(&mrb_hash[..]) + Ok(t.op_read(MD_HASH_KEY)?.and_then(|tip_hash| { + t.fetch_block(&tip_hash[..]) .expect("block to be found if metadata is set") })) })?; diff --git a/node/src/chain/acceptor.rs b/node/src/chain/acceptor.rs index 192fa90f0f..c2167db832 100644 --- a/node/src/chain/acceptor.rs +++ b/node/src/chain/acceptor.rs @@ -54,7 +54,7 @@ pub(crate) enum RevertTarget { /// Acceptor also manages the initialization and lifespan of Consensus task. pub(crate) struct Acceptor { /// Most recently accepted block a.k.a blockchain tip - mrb: RwLock, + tip: RwLock, /// Provisioners needed to verify next block pub(crate) provisioners_list: RwLock, @@ -124,25 +124,25 @@ impl Acceptor { /// Finally it spawns a new consensus [`Task`] pub async fn init_consensus( keys_path: &str, - mrb: BlockWithLabel, + tip: BlockWithLabel, provisioners_list: Provisioners, db: Arc>, network: Arc>, vm: Arc>, ) -> anyhow::Result { - let mrb_height = mrb.inner().header().height; - let mrb_state_hash = mrb.inner().header().state_hash; + let tip_height = tip.inner().header().height; + let tip_state_hash = tip.inner().header().state_hash; let mut provisioners_list = ContextProvisioners::new(provisioners_list); - if mrb.inner().header().height > 0 { + if tip.inner().header().height > 0 { let changed_provisioners = - vm.read().await.get_changed_provisioners(mrb_state_hash)?; + vm.read().await.get_changed_provisioners(tip_state_hash)?; provisioners_list.apply_changes(changed_provisioners); } let acc = Self { - mrb: RwLock::new(mrb), + tip: RwLock::new(tip), provisioners_list: RwLock::new(provisioners_list), db: db.clone(), vm: vm.clone(), @@ -160,7 +160,7 @@ impl Acceptor { ); // Detect a consistency issue between VM and Ledger states. - if mrb_height > 0 && mrb_state_hash != state_root { + if tip_height > 0 && tip_state_hash != state_root { info!("revert to last finalized state"); // Revert to last known finalized state. acc.try_revert(RevertTarget::LastFinalizedState).await?; @@ -174,7 +174,7 @@ impl Acceptor { let base_timeouts = self.adjust_round_base_timeouts().await; self.task.write().await.spawn( - self.mrb.read().await.inner(), + self.tip.read().await.inner(), provisioners_list, &self.db, &self.vm, @@ -331,19 +331,19 @@ impl Acceptor { Ok(change) } - /// Updates most_recent_block together with provisioners list. + /// Updates tip together with provisioners list. /// /// # Arguments /// /// * `blk` - Block that already exists in ledger - pub(crate) async fn update_most_recent_block( + pub(crate) async fn update_tip( &self, blk: &Block, label: Label, ) -> anyhow::Result<()> { let mut task = self.task.write().await; - let mut mrb = self.mrb.write().await; + let mut tip = self.tip.write().await; let mut provisioners_list = self.provisioners_list.write().await; // Ensure block that will be marked as blockchain tip does exist @@ -374,7 +374,7 @@ impl Acceptor { vm.get_changed_provisioners(blk.header().state_hash)?; provisioners_list.apply_changes(changed_provisioners); - *mrb = BlockWithLabel::new_with_label(blk.clone(), label); + *tip = BlockWithLabel::new_with_label(blk.clone(), label); Ok(()) } @@ -403,16 +403,16 @@ impl Acceptor { ) -> anyhow::Result