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 6958d94 commit 1bd4c03
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 26 deletions.
4 changes: 2 additions & 2 deletions consensus/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::fmt;
use std::time::Duration;

use execution_core::StakePublicKey;
use node_data::ledger::{Block, Header, SpentTransaction, Transaction};
use node_data::ledger::{Block, Header, Slash, SpentTransaction, Transaction};
use node_data::StepName;

pub type StateRoot = [u8; 32];
Expand All @@ -26,7 +26,7 @@ pub struct CallParams {
pub round: u64,
pub block_gas_limit: u64,
pub generator_pubkey: node_data::bls::PublicKey,
pub missed_generators: Vec<StakePublicKey>,
pub to_slash: Vec<Slash>,
pub voters_pubkey: Option<Vec<VoterWithCredits>>,
}

Expand Down
30 changes: 25 additions & 5 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,
to_str, Attestation, Block, Fault, IterationsInfo, Seed, Slash, SlashType,
};
use std::cmp::max;

Expand Down Expand Up @@ -52,6 +52,7 @@ impl<T: Operations> Generator<T> {
Seed::from(seed),
iteration,
failed_iterations,
vec![],
ru.att_voters(),
)
.await?;
Expand Down Expand Up @@ -88,17 +89,36 @@ impl<T: Operations> Generator<T> {
seed: Seed,
iteration: u8,
failed_iterations: IterationsInfo,
faults: Vec<Fault>,
voters: &[VoterWithCredits],
) -> Result<Block, crate::operations::Error> {
let missed_generators = failed_iterations
.to_missed_generators()
.map_err(|_| crate::operations::Error::InvalidIterationInfo)?;
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 call_params = CallParams {
round: ru.round,
block_gas_limit: config::DEFAULT_BLOCK_GAS_LIMIT,
generator_pubkey: ru.pubkey_bls.clone(),
missed_generators,
to_slash,
voters_pubkey: Some(voters.to_owned()),
};

Expand Down
4 changes: 4 additions & 0 deletions node-data/src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl PublicKey {
&self.inner
}

pub fn into_inner(self) -> BlsPublicKey {
self.inner
}

/// Truncated base58 representation of inner data
pub fn to_bs58(&self) -> String {
self.bytes().to_bs58()
Expand Down
2 changes: 1 addition & 1 deletion node-data/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod transaction;
pub use transaction::{SpentTransaction, Transaction};

mod faults;
pub use faults::Fault;
pub use faults::{Fault, Slash, SlashType};

mod attestation;
pub use attestation::{
Expand Down
54 changes: 51 additions & 3 deletions node-data/src/ledger/faults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::message::{
payload::{Candidate, Ratification, Validation, Vote},
ConsensusHeader, SignInfo, StepMessage,
use crate::{
bls::PublicKey,
message::{
payload::{Candidate, Ratification, RatificationResult, Validation, Vote},
ConsensusHeader, SignInfo, StepMessage,
},
};

use dusk_bytes::Serializable as DuskSerializeble;
Expand Down Expand Up @@ -54,6 +57,14 @@ impl Fault {
}
}

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

/// Get the ConsensusHeader related to the inner FaultDatas
pub fn consensus_header(&self) -> (&ConsensusHeader, &ConsensusHeader) {
match self {
Expand Down Expand Up @@ -200,3 +211,40 @@ impl Serializable for Fault {
Ok(fault)
}
}

#[derive(Clone, Debug)]
pub struct Slash {
pub provisioner: PublicKey,
pub r#type: SlashType,
}

#[derive(Clone, Debug)]
pub enum SlashType {
Soft,
Hard,
HardWithSeverity(u8),
}

impl Slash {
pub fn from_iteration_info(
value: &IterationInfo,
) -> Result<Option<Self>, dusk_bytes::Error> {
let (attestation, provisioner) = value;
let slash = match attestation.result {
RatificationResult::Fail(Vote::NoCandidate) => SlashType::Soft,
RatificationResult::Fail(Vote::Invalid(_)) => SlashType::Hard,
_ => {
return Ok(None);
}
};
let provisioner = (*provisioner.inner()).try_into().map_err(|e|{
tracing::error!("Unable to generate missing generators from failed_iterations: {e:?}");
e
// io::Error::new(io::ErrorKind::InvalidData, "Error in deserialize")
})?;
Ok(Some(Self {
provisioner,
r#type: slash,
}))
}
}
48 changes: 34 additions & 14 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::{SpentTransaction, Transaction};
use node_data::ledger::{Fault, 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,7 @@ 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;
let voters = params.voters_pubkey.as_ref().map(|voters| &voters[..]);

let mut session = self.session(block_height, None)?;
Expand Down Expand Up @@ -172,7 +172,7 @@ impl Rusk {
block_height,
dusk_spent,
generator,
missed_generators,
to_slash,
voters,
)?;
update_hasher(&mut event_hasher, &coinbase_events);
Expand Down Expand Up @@ -539,7 +539,6 @@ fn reward_slash_and_update_root(
block_height: u64,
dusk_spent: Dusk,
generator: &StakePublicKey,
slashing: &[StakePublicKey],
voters: Option<&[(StakePublicKey, usize)]>,
) -> Result<Vec<Event>> {
let (dusk_value, generator_reward, voters_reward) =
Expand Down Expand Up @@ -601,16 +600,6 @@ 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);
}

let r = session.call::<_, ()>(
TRANSFER_CONTRACT,
"update_root",
Expand All @@ -627,3 +616,34 @@ 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 slash_type = s.r#type;
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>),
u64::MAX,
),
node_data::ledger::SlashType::HardWithSeverity(severity) => session
.call::<_, ()>(
STAKE_CONTRACT,
"hard_slash",
&(provisioner, Some(severity)),
u64::MAX,
),
}?;
events.extend(r.events);
}
Ok(events)
}
6 changes: 5 additions & 1 deletion 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, Fault, Header, IterationsInfo, SpentTransaction,
},
message::payload::Vote,
};

Expand Down Expand Up @@ -70,6 +72,7 @@ pub fn generator_procedure(
block_height: u64,
block_gas_limit: u64,
missed_generators: Vec<StakePublicKey>,
faults: Vec<Fault>,
expected: Option<ExecuteResult>,
) -> anyhow::Result<Vec<SpentTransaction>> {
let expected = expected.unwrap_or(ExecuteResult {
Expand Down Expand Up @@ -105,6 +108,7 @@ pub fn generator_procedure(
block_gas_limit,
generator_pubkey,
missed_generators,
faults,
voters_pubkey: None,
};

Expand Down

0 comments on commit 1bd4c03

Please sign in to comment.