Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Jul 8, 2024
1 parent 0dd44e2 commit 363ecd4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 38 deletions.
28 changes: 6 additions & 22 deletions consensus/src/proposal/block_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::commons::{get_current_timestamp, RoundUpdate};
use crate::operations::{CallParams, Operations, VoterWithCredits};
use node_data::ledger::{
to_str, Attestation, Block, Fault, IterationsInfo, Seed, Slash, SlashType,
to_str, Attestation, Block, Fault, IterationsInfo, Seed, Slash,
};
use std::cmp::max;

Expand Down Expand Up @@ -92,27 +92,11 @@ impl<T: Operations> Generator<T> {
faults: Vec<Fault>,
voters: &[VoterWithCredits],
) -> Result<Block, crate::operations::Error> {
let mut to_slash = failed_iterations
.att_list
.iter()
.flatten()
.flat_map(Slash::from_iteration_info)
.flatten()
.collect::<Vec<_>>();
for fault in faults {
let provisioner = fault.faultee().clone();
let slash_type = match fault {
Fault::DoubleCandidate(_, _)
| Fault::DoubleRatificationVote(_, _)
| Fault::DoubleValidationVote(_, _) => {
SlashType::HardWithSeverity(2u8)
}
};
to_slash.push(Slash {
provisioner,
r#type: slash_type,
})
}
let mut to_slash = failed_iterations.to_slash().map_err(|e| {
tracing::error!("Error while failed_iterations.to_slash() {e:?}");
crate::operations::Error::InvalidIterationInfo
})?;
to_slash.extend(faults.iter().map(Slash::from));

let call_params = CallParams {
round: ru.round,
Expand Down
10 changes: 10 additions & 0 deletions node-data/src/ledger/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ impl IterationsInfo {
}
}

pub fn to_slash(&self) -> Result<Vec<Slash>, io::Error> {
Ok(self
.att_list
.iter()
.flatten()
.flat_map(Slash::from_iteration_info)
.flatten()
.collect())
}

pub fn to_missed_generators(&self) -> Result<Vec<BlsPublicKey>, io::Error> {
self.to_missed_generators_bytes()
.map(|pk| BlsPublicKey::from_slice(pk.inner()).map_err(|e|{
Expand Down
52 changes: 48 additions & 4 deletions node-data/src/ledger/faults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use crate::{
bls::PublicKey,
message::{
payload::{Candidate, Ratification, RatificationResult, Validation, Vote},
payload::{
Candidate, Ratification, RatificationResult, Validation, Vote,
},
ConsensusHeader, SignInfo, StepMessage,
},
};
Expand Down Expand Up @@ -57,11 +59,19 @@ impl Fault {
}
}

pub fn faultee(&self) -> &PublicKey {
pub fn to_faultee(&self) -> PublicKey {
match self {
Fault::DoubleRatificationVote(a, _)
| Fault::DoubleValidationVote(a, _) => &a.sig.signer,
Fault::DoubleCandidate(a, _) => &a.sig.signer,
| Fault::DoubleValidationVote(a, _) => a.sig.signer.clone(),
Fault::DoubleCandidate(a, _) => a.sig.signer.clone(),
}
}

pub fn into_faultee(self) -> PublicKey {
match self {
Fault::DoubleRatificationVote(a, _)
| Fault::DoubleValidationVote(a, _) => a.sig.signer,
Fault::DoubleCandidate(a, _) => a.sig.signer,
}
}

Expand Down Expand Up @@ -248,3 +258,37 @@ impl Slash {
}))
}
}

impl From<&Fault> for Slash {
fn from(value: &Fault) -> Self {
let slash_type = match value {
Fault::DoubleCandidate(_, _)
| Fault::DoubleRatificationVote(_, _)
| Fault::DoubleValidationVote(_, _) => {
SlashType::HardWithSeverity(2u8)
}
};
let provisioner = value.to_faultee();
Self {
provisioner,
r#type: slash_type,
}
}
}

impl From<Fault> for Slash {
fn from(value: Fault) -> Self {
let slash_type = match value {
Fault::DoubleCandidate(_, _)
| Fault::DoubleRatificationVote(_, _)
| Fault::DoubleValidationVote(_, _) => {
SlashType::HardWithSeverity(2u8)
}
};
let provisioner = value.into_faultee();
Self {
provisioner,
r#type: slash_type,
}
}
}
21 changes: 12 additions & 9 deletions rusk/src/lib/chain/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use execution_core::{
stake::StakeData, transfer::Transaction as PhoenixTransaction, BlsScalar,
StakePublicKey,
};
use node_data::ledger::{Fault, Slash, 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 @@ -94,7 +94,8 @@ impl Rusk {
let block_height = params.round;
let block_gas_limit = params.block_gas_limit;
let generator = params.generator_pubkey.inner();
let to_slash = params.to_slash;
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 @@ -197,7 +198,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 @@ -208,7 +209,7 @@ impl Rusk {
block_gas_limit,
generator,
txs,
missed_generators,
slashing,
voters,
)
.map(|(a, b, _, _)| (a, b))
Expand All @@ -227,7 +228,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 @@ -238,7 +239,7 @@ impl Rusk {
block_gas_limit,
&generator,
&txs[..],
missed_generators,
slashing,
voters,
)?;

Expand Down Expand Up @@ -413,7 +414,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 @@ -459,7 +460,7 @@ fn accept(
block_height,
dusk_spent,
generator,
missed_generators,
slashing,
voters,
)?;

Expand Down Expand Up @@ -539,6 +540,7 @@ fn reward_slash_and_update_root(
block_height: u64,
dusk_spent: Dusk,
generator: &StakePublicKey,
slashing: Vec<Slash>,
voters: Option<&[(StakePublicKey, usize)]>,
) -> Result<Vec<Event>> {
let (dusk_value, generator_reward, voters_reward) =
Expand Down Expand Up @@ -600,6 +602,8 @@ fn reward_slash_and_update_root(
)
}

events.extend(slash(session, slashing)?);

let r = session.call::<_, ()>(
TRANSFER_CONTRACT,
"update_root",
Expand All @@ -620,7 +624,6 @@ fn to_bs58(pk: &StakePublicKey) -> String {
fn slash(session: &mut Session, slash: Vec<Slash>) -> Result<Vec<Event>> {
let mut events = vec![];
for s in slash {
let slash_type = s.r#type;
let provisioner = s.provisioner.into_inner();
let r = match s.r#type {
node_data::ledger::SlashType::Soft => session.call::<_, ()>(
Expand Down
12 changes: 9 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,16 @@ impl VMExecution for Rusk {
let generator = StakePublicKey::from_slice(&generator.0)
.map_err(|e| anyhow::anyhow!("Error in from_slice {e:?}"))?;

let mut slashing = blk.header().failed_iterations.to_slash()?;
slashing.extend(blk.faults().iter().map(Slash::from));

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 +77,9 @@ impl VMExecution for Rusk {
let generator = StakePublicKey::from_slice(&generator.0)
.map_err(|e| anyhow::anyhow!("Error in from_slice {e:?}"))?;

let mut slashing = blk.header().failed_iterations.to_slash()?;
slashing.extend(blk.faults().iter().map(Slash::from));

let (txs, verification_output) = self
.accept_transactions(
blk.header().height,
Expand All @@ -84,7 +90,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

0 comments on commit 363ecd4

Please sign in to comment.