Skip to content

Commit

Permalink
rusk: change session opening to verify TIP instead of open old commits
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Dec 18, 2024
1 parent bb1b1d4 commit 0e237e7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions rusk/src/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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")
}
}
}
}
5 changes: 4 additions & 1 deletion rusk/src/lib/node/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,10 @@ impl Rusk {
block_height: u64,
commit: [u8; 32],
) -> Result<Session> {
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");
Expand Down
16 changes: 12 additions & 4 deletions rusk/src/lib/node/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

mod query;

use dusk_consensus::errors::VstError;
use node_data::events::contract::ContractEvent;
use tracing::info;

Expand Down Expand Up @@ -48,13 +49,14 @@ impl VMExecution for Rusk {
prev_commit: [u8; 32],
blk: &Block,
voters: &[Voter],
) -> anyhow::Result<VerificationOutput> {
) -> Result<VerificationOutput, VstError> {
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(
Expand All @@ -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)
}
Expand Down

0 comments on commit 0e237e7

Please sign in to comment.