Skip to content

Commit

Permalink
rusk: use Slash array instead of missing generators
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Jul 17, 2024
1 parent a4c89dc commit fafcdb3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 25 deletions.
2 changes: 1 addition & 1 deletion rusk/benches/block_ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn accept_benchmark(c: &mut Criterion) {
generator,
txs,
None,
&[],
vec![],
None,
)
.expect("Accepting transactions should succeed");
Expand Down
61 changes: 42 additions & 19 deletions rusk/src/lib/chain/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = &params.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)?;
Expand Down Expand Up @@ -177,7 +178,7 @@ impl Rusk {
block_height,
dusk_spent,
generator,
missed_generators,
to_slash,
voters,
)?;
update_hasher(&mut event_hasher, &coinbase_events);
Expand All @@ -202,7 +203,7 @@ impl Rusk {
block_gas_limit: u64,
generator: &StakePublicKey,
txs: &[Transaction],
missed_generators: &[StakePublicKey],
slashing: Vec<Slash>,
voters: Option<&[VoterWithCredits]>,
) -> Result<(Vec<SpentTransaction>, VerificationOutput)> {
let session = self.session(block_height, None)?;
Expand All @@ -213,7 +214,7 @@ impl Rusk {
block_gas_limit,
generator,
txs,
missed_generators,
slashing,
voters,
)
.map(|(a, b, _, _)| (a, b))
Expand All @@ -232,7 +233,7 @@ impl Rusk {
generator: StakePublicKey,
txs: Vec<Transaction>,
consistency_check: Option<VerificationOutput>,
missed_generators: &[StakePublicKey],
slashing: Vec<Slash>,
voters: Option<&[VoterWithCredits]>,
) -> Result<(Vec<SpentTransaction>, VerificationOutput)> {
let session = self.session(block_height, None)?;
Expand All @@ -243,7 +244,7 @@ impl Rusk {
block_gas_limit,
&generator,
&txs[..],
missed_generators,
slashing,
voters,
)?;

Expand Down Expand Up @@ -418,7 +419,7 @@ fn accept(
block_gas_limit: u64,
generator: &StakePublicKey,
txs: &[Transaction],
missed_generators: &[StakePublicKey],
slashing: Vec<Slash>,
voters: Option<&[VoterWithCredits]>,
) -> Result<(
Vec<SpentTransaction>,
Expand Down Expand Up @@ -464,7 +465,7 @@ fn accept(
block_height,
dusk_spent,
generator,
missed_generators,
slashing,
voters,
)?;

Expand Down Expand Up @@ -544,7 +545,7 @@ fn reward_slash_and_update_root(
block_height: u64,
dusk_spent: Dusk,
generator: &StakePublicKey,
slashing: &[StakePublicKey],
slashing: Vec<Slash>,
voters: Option<&[(StakePublicKey, usize)]>,
) -> Result<Vec<Event>> {
let (
Expand Down Expand Up @@ -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>),
u64::MAX,
)?;
events.extend(r.events);
}
events.extend(slash(session, slashing)?);

let r = session.call::<_, ()>(
TRANSFER_CONTRACT,
Expand Down Expand Up @@ -664,3 +657,33 @@ fn to_bs58(pk: &StakePublicKey) -> String {
pk.truncate(16);
pk
}

fn slash(session: &mut Session, slash: Vec<Slash>) -> Result<Vec<Event>> {
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>),
u64::MAX,
),
node_data::ledger::SlashType::Hard => session.call::<_, ()>(
STAKE_CONTRACT,
"hard_slash",
&(provisioner, None::<u64>, None::<u64>),
u64::MAX,
),
node_data::ledger::SlashType::HardWithSeverity(severity) => session
.call::<_, ()>(
STAKE_CONTRACT,
"hard_slash",
&(provisioner, None::<u64>, Some(severity)),
u64::MAX,
),
}?;
events.extend(r.events);
}
Ok(events)
}
10 changes: 7 additions & 3 deletions rusk/src/lib/chain/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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}!!"))?;
Expand All @@ -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,
Expand All @@ -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}!!"))?;
Expand Down
11 changes: 9 additions & 2 deletions rusk/tests/common/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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,
};

Expand Down

0 comments on commit fafcdb3

Please sign in to comment.