From 169ae695d9496e9ca9bde88687db05662fb23f65 Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Wed, 18 Dec 2024 16:11:25 +0100 Subject: [PATCH] rusk: change session opening to verify TIP instead of open old commits --- rusk/src/lib/error.rs | 5 +++++ rusk/src/lib/node/rusk.rs | 5 ++++- rusk/src/lib/node/vm.rs | 16 ++++++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/rusk/src/lib/error.rs b/rusk/src/lib/error.rs index 39ea449b83..fdacc66250 100644 --- a/rusk/src/lib/error.rs +++ b/rusk/src/lib/error.rs @@ -58,6 +58,8 @@ pub enum Error { InvalidCreditsCount(u64, usize), /// Memo too large MemoTooLarge(usize), + /// Chain tip different from the expected one + TipChanged, } impl std::error::Error for Error {} @@ -182,6 +184,9 @@ impl fmt::Display for Error { Error::MemoTooLarge(size) => { write!(f, "The memo size {size} is too large") } + Error::TipChanged => { + write!(f, "Chain tip different from the expected one") + } } } } diff --git a/rusk/src/lib/node/rusk.rs b/rusk/src/lib/node/rusk.rs index bc0b72fcd7..878c739dc4 100644 --- a/rusk/src/lib/node/rusk.rs +++ b/rusk/src/lib/node/rusk.rs @@ -479,7 +479,10 @@ impl Rusk { block_height: u64, commit: [u8; 32], ) -> Result { - let mut session = self._session(block_height, Some(commit))?; + let mut session = self._session(block_height, None)?; + if session.root() != commit { + return Err(Error::TipChanged); + } let _: CallReceipt<()> = session .call(STAKE_CONTRACT, "before_state_transition", &(), u64::MAX) .expect("before_state_transition to success"); diff --git a/rusk/src/lib/node/vm.rs b/rusk/src/lib/node/vm.rs index 87da365186..0f60abb2db 100644 --- a/rusk/src/lib/node/vm.rs +++ b/rusk/src/lib/node/vm.rs @@ -6,6 +6,7 @@ mod query; +use dusk_consensus::errors::VstError; use node_data::events::contract::ContractEvent; use tracing::info; @@ -48,13 +49,14 @@ impl VMExecution for Rusk { prev_commit: [u8; 32], blk: &Block, voters: &[Voter], - ) -> anyhow::Result { + ) -> Result { info!("Received verify_state_transition request"); let generator = blk.header().generator_bls_pubkey; let generator = BlsPublicKey::from_slice(&generator.0) - .map_err(|e| anyhow::anyhow!("Error in from_slice {e:?}"))?; + .map_err(VstError::InvalidGenerator)?; - let slashing = Slash::from_block(blk)?; + let slashing = + Slash::from_block(blk).map_err(VstError::InvalidSlash)?; let (_, verification_output) = self .verify_transactions( @@ -67,7 +69,13 @@ impl VMExecution for Rusk { slashing, voters, ) - .map_err(|inner| anyhow::anyhow!("Cannot verify txs: {inner}!!"))?; + .map_err(|inner| { + if let crate::Error::TipChanged = inner { + VstError::TipChanged + } else { + VstError::Generic(format!("Cannot verify txs: {inner}!!")) + } + })?; Ok(verification_output) }