Skip to content

Commit

Permalink
rusk: use prev_state_root for verification
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Dec 16, 2024
1 parent c85b12a commit 39fcb60
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
2 changes: 2 additions & 0 deletions rusk/benches/block_ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ fn bench_accept(
let generator = PublicKey::new(*DUSK_CONSENSUS_KEY).into_inner();

let txs = Arc::new(txs);
let prev_root = rusk.state_root();

for n_txs in N_TXS {
let rusk = rusk.clone();
Expand All @@ -131,6 +132,7 @@ fn bench_accept(
let txs = txs[..*n_txs].to_vec();

rusk.accept_transactions(
prev_root,
BLOCK_HEIGHT,
BLOCK_GAS_LIMIT,
BLOCK_HASH,
Expand Down
17 changes: 11 additions & 6 deletions rusk/src/lib/node/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ impl Rusk {
let block_gas_limit = self.block_gas_limit;
let generator = params.generator_pubkey.inner();
let to_slash = params.to_slash.clone();
let prev_state_root = params.prev_state_root;

let voters = &params.voters_pubkey[..];

let mut session = self.new_block_session(block_height, None)?;
let mut session =
self.new_block_session(block_height, prev_state_root)?;

let mut block_gas_left = block_gas_limit;

Expand Down Expand Up @@ -179,7 +181,8 @@ impl Rusk {
// transaction, since it is technically valid.
if gas_spent > block_gas_left {
info!("Skipping {tx_id_hex} due gas_spent {gas_spent} greater than left: {block_gas_left}");
session = self.new_block_session(block_height, None)?;
session = self
.new_block_session(block_height, prev_state_root)?;

for spent_tx in &spent_txs {
// We know these transactions were correctly
Expand Down Expand Up @@ -259,6 +262,7 @@ impl Rusk {
#[allow(clippy::too_many_arguments)]
pub fn verify_transactions(
&self,
prev_commit: [u8; 32],
block_height: u64,
block_hash: Hash,
block_gas_limit: u64,
Expand All @@ -267,7 +271,7 @@ impl Rusk {
slashing: Vec<Slash>,
voters: &[Voter],
) -> Result<(Vec<SpentTransaction>, VerificationOutput)> {
let session = self.new_block_session(block_height, None)?;
let session = self.new_block_session(block_height, prev_commit)?;

accept(
session,
Expand All @@ -292,6 +296,7 @@ impl Rusk {
#[allow(clippy::too_many_arguments)]
pub fn accept_transactions(
&self,
prev_commit: [u8; 32],
block_height: u64,
block_gas_limit: u64,
block_hash: Hash,
Expand All @@ -305,7 +310,7 @@ impl Rusk {
VerificationOutput,
Vec<ContractEvent>,
)> {
let session = self.new_block_session(block_height, None)?;
let session = self.new_block_session(block_height, prev_commit)?;

let (spent_txs, verification_output, session, events) = accept(
session,
Expand Down Expand Up @@ -472,9 +477,9 @@ impl Rusk {
pub(crate) fn new_block_session(
&self,
block_height: u64,
commit: Option<[u8; 32]>,
commit: [u8; 32],
) -> Result<Session> {
let mut session = self._session(block_height, commit)?;
let mut session = self._session(block_height, Some(commit))?;
let _: CallReceipt<()> = session
.call(STAKE_CONTRACT, "before_state_transition", &(), u64::MAX)
.expect("before_state_transition to success");
Expand Down
4 changes: 4 additions & 0 deletions rusk/src/lib/node/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl VMExecution for Rusk {

fn verify_state_transition(
&self,
prev_commit: [u8; 32],
blk: &Block,
voters: &[Voter],
) -> anyhow::Result<VerificationOutput> {
Expand All @@ -57,6 +58,7 @@ impl VMExecution for Rusk {

let (_, verification_output) = self
.verify_transactions(
prev_commit,
blk.header().height,
blk.header().hash,
blk.header().gas_limit,
Expand All @@ -72,6 +74,7 @@ impl VMExecution for Rusk {

fn accept(
&self,
prev_root: [u8; 32],
blk: &Block,
voters: &[Voter],
) -> anyhow::Result<(
Expand All @@ -88,6 +91,7 @@ impl VMExecution for Rusk {

let (txs, verification_output, stake_events) = self
.accept_transactions(
prev_root,
blk.header().height,
blk.header().gas_limit,
blk.header().hash,
Expand Down
16 changes: 12 additions & 4 deletions rusk/tests/common/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub fn generator_procedure(
missed_generators: Vec<BlsPublicKey>,
expected: Option<ExecuteResult>,
) -> anyhow::Result<Vec<SpentTransaction>> {
let prev_root = rusk.state_root();
let expected = expected.unwrap_or(ExecuteResult {
executed: txs.len(),
discarded: 0,
Expand Down Expand Up @@ -146,6 +147,7 @@ pub fn generator_procedure(
to_slash,
voters_pubkey: voters.clone(),
max_txs_bytes: usize::MAX,
prev_state_root: prev_root,
};

let (transfer_txs, discarded, execute_output) =
Expand Down Expand Up @@ -176,10 +178,12 @@ pub fn generator_procedure(
)
.expect("valid block");

let verify_output = rusk.verify_state_transition(&block, &voters)?;
let verify_output =
rusk.verify_state_transition(prev_root, &block, &voters)?;
info!("verify_state_transition new verification: {verify_output}",);

let (accept_txs, accept_output, _) = rusk.accept(&block, &voters)?;
let (accept_txs, accept_output, _) =
rusk.accept(prev_root, &block, &voters)?;

assert_eq!(accept_txs.len(), expected.executed, "all txs accepted");

Expand Down Expand Up @@ -210,6 +214,7 @@ pub fn generator_procedure2(
expected: Option<ExecuteResult>,
generator: Option<BlsPublicKey>,
) -> anyhow::Result<(Vec<SpentTransaction>, [u8; 32])> {
let prev_root = rusk.state_root();
let expected = expected.unwrap_or(ExecuteResult {
executed: txs.len(),
discarded: 0,
Expand Down Expand Up @@ -254,6 +259,7 @@ pub fn generator_procedure2(
to_slash,
voters_pubkey: voters.clone(),
max_txs_bytes: usize::MAX,
prev_state_root: prev_root,
};

let (transfer_txs, discarded, execute_output) =
Expand Down Expand Up @@ -284,10 +290,12 @@ pub fn generator_procedure2(
)
.expect("valid block");

let verify_output = rusk.verify_state_transition(&block, &voters)?;
let verify_output =
rusk.verify_state_transition(prev_root, &block, &voters)?;
info!("verify_state_transition new verification: {verify_output}",);

let (accept_txs, accept_output, _) = rusk.accept(&block, &voters)?;
let (accept_txs, accept_output, _) =
rusk.accept(prev_root, &block, &voters)?;

assert_eq!(accept_txs.len(), expected.executed, "all txs accepted");

Expand Down

0 comments on commit 39fcb60

Please sign in to comment.