diff --git a/rusk/src/lib/error.rs b/rusk/src/lib/error.rs index 39ea449b8..fdacc6625 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 98201dddd..db60032f0 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 649944ccf..c170d130e 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) }