From fafcdb316c43b77985a2822f7523f90329e92bd3 Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Mon, 15 Jul 2024 15:16:19 +0200 Subject: [PATCH] rusk: use `Slash` array instead of missing generators --- rusk/benches/block_ingestion.rs | 2 +- rusk/src/lib/chain/rusk.rs | 61 +++++++++++++++++++++++---------- rusk/src/lib/chain/vm.rs | 10 ++++-- rusk/tests/common/state.rs | 11 ++++-- 4 files changed, 59 insertions(+), 25 deletions(-) diff --git a/rusk/benches/block_ingestion.rs b/rusk/benches/block_ingestion.rs index 37c1ab97ad..f872d08e97 100644 --- a/rusk/benches/block_ingestion.rs +++ b/rusk/benches/block_ingestion.rs @@ -106,7 +106,7 @@ pub fn accept_benchmark(c: &mut Criterion) { generator, txs, None, - &[], + vec![], None, ) .expect("Accepting transactions should succeed"); diff --git a/rusk/src/lib/chain/rusk.rs b/rusk/src/lib/chain/rusk.rs index 53d1e3d989..0b59c09e2c 100644 --- a/rusk/src/lib/chain/rusk.rs +++ b/rusk/src/lib/chain/rusk.rs @@ -27,7 +27,7 @@ use execution_core::{ stake::StakeData, transfer::Transaction as PhoenixTransaction, BlsScalar, StakePublicKey, }; -use node_data::ledger::{SpentTransaction, Transaction}; +use node_data::ledger::{Slash, SpentTransaction, Transaction}; use rusk_abi::dusk::Dusk; use rusk_abi::{ CallReceipt, ContractError, Error as PiecrustError, Event, Session, @@ -99,7 +99,8 @@ impl Rusk { let block_height = params.round; let block_gas_limit = params.block_gas_limit; let generator = params.generator_pubkey.inner(); - let missed_generators = ¶ms.missed_generators[..]; + let to_slash = params.to_slash.clone(); + let voters = params.voters_pubkey.as_ref().map(|voters| &voters[..]); let mut session = self.session(block_height, None)?; @@ -177,7 +178,7 @@ impl Rusk { block_height, dusk_spent, generator, - missed_generators, + to_slash, voters, )?; update_hasher(&mut event_hasher, &coinbase_events); @@ -202,7 +203,7 @@ impl Rusk { block_gas_limit: u64, generator: &StakePublicKey, txs: &[Transaction], - missed_generators: &[StakePublicKey], + slashing: Vec, voters: Option<&[VoterWithCredits]>, ) -> Result<(Vec, VerificationOutput)> { let session = self.session(block_height, None)?; @@ -213,7 +214,7 @@ impl Rusk { block_gas_limit, generator, txs, - missed_generators, + slashing, voters, ) .map(|(a, b, _, _)| (a, b)) @@ -232,7 +233,7 @@ impl Rusk { generator: StakePublicKey, txs: Vec, consistency_check: Option, - missed_generators: &[StakePublicKey], + slashing: Vec, voters: Option<&[VoterWithCredits]>, ) -> Result<(Vec, VerificationOutput)> { let session = self.session(block_height, None)?; @@ -243,7 +244,7 @@ impl Rusk { block_gas_limit, &generator, &txs[..], - missed_generators, + slashing, voters, )?; @@ -418,7 +419,7 @@ fn accept( block_gas_limit: u64, generator: &StakePublicKey, txs: &[Transaction], - missed_generators: &[StakePublicKey], + slashing: Vec, voters: Option<&[VoterWithCredits]>, ) -> Result<( Vec, @@ -464,7 +465,7 @@ fn accept( block_height, dusk_spent, generator, - missed_generators, + slashing, voters, )?; @@ -544,7 +545,7 @@ fn reward_slash_and_update_root( block_height: u64, dusk_spent: Dusk, generator: &StakePublicKey, - slashing: &[StakePublicKey], + slashing: Vec, voters: Option<&[(StakePublicKey, usize)]>, ) -> Result> { let ( @@ -619,15 +620,7 @@ fn reward_slash_and_update_root( ) } - for to_slash in slashing { - let r = session.call::<_, ()>( - STAKE_CONTRACT, - "slash", - &(*to_slash, None::), - u64::MAX, - )?; - events.extend(r.events); - } + events.extend(slash(session, slashing)?); let r = session.call::<_, ()>( TRANSFER_CONTRACT, @@ -664,3 +657,33 @@ fn to_bs58(pk: &StakePublicKey) -> String { pk.truncate(16); pk } + +fn slash(session: &mut Session, slash: Vec) -> Result> { + let mut events = vec![]; + for s in slash { + let provisioner = s.provisioner.into_inner(); + let r = match s.r#type { + node_data::ledger::SlashType::Soft => session.call::<_, ()>( + STAKE_CONTRACT, + "slash", + &(provisioner, None::), + u64::MAX, + ), + node_data::ledger::SlashType::Hard => session.call::<_, ()>( + STAKE_CONTRACT, + "hard_slash", + &(provisioner, None::, None::), + u64::MAX, + ), + node_data::ledger::SlashType::HardWithSeverity(severity) => session + .call::<_, ()>( + STAKE_CONTRACT, + "hard_slash", + &(provisioner, None::, Some(severity)), + u64::MAX, + ), + }?; + events.extend(r.events); + } + Ok(events) +} diff --git a/rusk/src/lib/chain/vm.rs b/rusk/src/lib/chain/vm.rs index e72ee9dd2a..c0d699e4ee 100644 --- a/rusk/src/lib/chain/vm.rs +++ b/rusk/src/lib/chain/vm.rs @@ -16,7 +16,7 @@ use dusk_consensus::user::provisioners::Provisioners; use dusk_consensus::user::stake::Stake; use execution_core::{stake::StakeData, StakePublicKey}; use node::vm::VMExecution; -use node_data::ledger::{Block, SpentTransaction, Transaction}; +use node_data::ledger::{Block, Slash, SpentTransaction, Transaction}; use super::Rusk; @@ -50,13 +50,15 @@ impl VMExecution for Rusk { let generator = StakePublicKey::from_slice(&generator.0) .map_err(|e| anyhow::anyhow!("Error in from_slice {e:?}"))?; + let slashing = Slash::from_block(blk)?; + let (_, verification_output) = self .verify_transactions( blk.header().height, blk.header().gas_limit, &generator, blk.txs(), - &blk.header().failed_iterations.to_missed_generators()?, + slashing, voters, ) .map_err(|inner| anyhow::anyhow!("Cannot verify txs: {inner}!!"))?; @@ -74,6 +76,8 @@ impl VMExecution for Rusk { let generator = StakePublicKey::from_slice(&generator.0) .map_err(|e| anyhow::anyhow!("Error in from_slice {e:?}"))?; + let slashing = Slash::from_block(blk)?; + let (txs, verification_output) = self .accept_transactions( blk.header().height, @@ -84,7 +88,7 @@ impl VMExecution for Rusk { state_root: blk.header().state_hash, event_hash: blk.header().event_hash, }), - &blk.header().failed_iterations.to_missed_generators()?, + slashing, voters, ) .map_err(|inner| anyhow::anyhow!("Cannot accept txs: {inner}!!"))?; diff --git a/rusk/tests/common/state.rs b/rusk/tests/common/state.rs index 0ac06a4567..80e2e2c1b8 100644 --- a/rusk/tests/common/state.rs +++ b/rusk/tests/common/state.rs @@ -18,7 +18,9 @@ use execution_core::{ }; use node_data::{ bls::PublicKeyBytes, - ledger::{Attestation, Block, Header, IterationsInfo, SpentTransaction}, + ledger::{ + Attestation, Block, Header, IterationsInfo, Slash, SpentTransaction, + }, message::payload::Vote, }; @@ -100,11 +102,16 @@ pub fn generator_procedure( ))); } + let faults = vec![]; + + let to_slash = + Slash::from_iterations_and_faults(&failed_iterations, &faults)?; + let call_params = CallParams { round, block_gas_limit, generator_pubkey, - missed_generators, + to_slash, voters_pubkey: None, };