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 363ecd4 commit 2671e4b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 87 deletions.
13 changes: 7 additions & 6 deletions consensus/src/proposal/block_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use node_data::message::payload::Candidate;
use node_data::message::{ConsensusHeader, Message, SignInfo, StepMessage};
use std::sync::Arc;
use std::time::Instant;
use tracing::{debug, info};
use tracing::{debug, error, info};

pub struct Generator<T: Operations> {
executor: Arc<T>,
Expand Down Expand Up @@ -92,11 +92,12 @@ impl<T: Operations> Generator<T> {
faults: Vec<Fault>,
voters: &[VoterWithCredits],
) -> Result<Block, crate::operations::Error> {
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 to_slash =
Slash::from_iterations_and_faults(&failed_iterations, &faults)
.map_err(|e| {
error!("Error while from_iterations_and_faults {e:?}");
crate::operations::Error::InvalidIterationInfo
})?;

let call_params = CallParams {
round: ru.round,
Expand Down
1 change: 0 additions & 1 deletion node-data/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub use attestation::{
use crate::bls::PublicKeyBytes;
use crate::Serializable;

use dusk_bytes::DeserializableSlice;
use rusk_abi::hash::Hasher;
use sha3::Digest;
use std::io::{self, Read, Write};
Expand Down
35 changes: 1 addition & 34 deletions node-data/src/ledger/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use super::*;

use execution_core::BlsPublicKey;

use crate::message::payload::{RatificationResult, Vote};
use crate::message::payload::RatificationResult;

#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
#[cfg_attr(any(feature = "faker", test), derive(Dummy))]
Expand Down Expand Up @@ -95,37 +93,6 @@ impl IterationsInfo {
att_list: attestations,
}
}

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|{
tracing::error!("Unable to generate missing generators from failed_iterations: {e:?}");
io::Error::new(io::ErrorKind::InvalidData, "Error in deserialize")
}))
.collect()
}

pub fn to_missed_generators_bytes(
&self,
) -> impl Iterator<Item = &PublicKeyBytes> {
self.att_list
.iter()
.flatten()
.filter(|(c, _)| {
c.result == RatificationResult::Fail(Vote::NoCandidate)
})
.map(|(_, pk)| pk)
}
}

#[cfg(any(feature = "faker", test))]
Expand Down
50 changes: 23 additions & 27 deletions node-data/src/ledger/faults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ impl Fault {
}
}

pub fn into_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 @@ -236,7 +228,7 @@ pub enum SlashType {
}

impl Slash {
pub fn from_iteration_info(
fn from_iteration_info(
value: &IterationInfo,
) -> Result<Option<Self>, dusk_bytes::Error> {
let (attestation, provisioner) = value;
Expand All @@ -250,13 +242,34 @@ impl Slash {
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,
}))
}

pub fn from_block(blk: &Block) -> Result<Vec<Slash>, io::Error> {
Self::from_iterations_and_faults(
&blk.header().failed_iterations,
blk.faults(),
)
}

pub fn from_iterations_and_faults(
failed_iterations: &IterationsInfo,
faults: &[Fault],
) -> Result<Vec<Slash>, io::Error> {
let mut slashing = failed_iterations
.att_list
.iter()
.flatten()
.flat_map(Slash::from_iteration_info)
.flatten()
.collect::<Vec<_>>();
slashing.extend(faults.iter().map(Slash::from));
Ok(slashing)
}
}

impl From<&Fault> for Slash {
Expand All @@ -275,20 +288,3 @@ impl From<&Fault> for 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.into_faultee();
Self {
provisioner,
r#type: slash_type,
}
}
}
19 changes: 8 additions & 11 deletions node/src/chain/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use dusk_consensus::config::{MAX_STEP_TIMEOUT, MIN_STEP_TIMEOUT};
use dusk_consensus::user::provisioners::{ContextProvisioners, Provisioners};
use node_data::bls::PublicKey;
use node_data::ledger::{
self, to_str, Block, BlockWithLabel, Label, Seed, SpentTransaction,
self, to_str, Block, BlockWithLabel, Label, Seed, Slash, SpentTransaction,
};
use node_data::message::AsyncQueue;
use node_data::message::Payload;
Expand Down Expand Up @@ -294,18 +294,16 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
let generator = generator
.try_into()
.map_err(|e| anyhow::anyhow!("Cannot deserialize bytes {e:?}"))?;
// FIX ME: We should track voters reward too
let reward = ProvisionerChange::Reward(generator);
let dusk_reward = ProvisionerChange::Reward(DUSK_KEY.clone());
let mut changed_provisioners = vec![reward, dusk_reward];

// Update provisioners if a slash has been applied
for bytes in blk.header().failed_iterations.to_missed_generators_bytes()
{
let slashed = bytes.0.try_into().map_err(|e| {
anyhow::anyhow!("Cannot deserialize bytes {e:?}")
})?;
changed_provisioners.push(ProvisionerChange::Slash(slashed));
}
let slashed = Slash::from_block(blk)?
.into_iter()
.map(|f| ProvisionerChange::Slash(f.provisioner));
changed_provisioners.extend(slashed);

// FIX_ME: This relies on the stake contract being called only by the
// transfer contract. We should change this once third-party contracts
Expand Down Expand Up @@ -483,9 +481,8 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
header.height,
);

for slashed in header.failed_iterations.to_missed_generators_bytes()
{
info!("Slashed {}", slashed.to_base58());
for slashed in Slash::from_block(blk)? {
info!("Slashed {}", slashed.provisioner.to_base58());
slashed_count += 1;
}

Expand Down
2 changes: 1 addition & 1 deletion rusk/benches/block_ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn accept_benchmark(c: &mut Criterion) {
generator,
txs,
None,
&[],
vec![],
None,
)
.expect("Accepting transactions should succeed");
Expand Down
6 changes: 2 additions & 4 deletions rusk/src/lib/chain/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ 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 slashing = Slash::from_block(blk)?;

let (_, verification_output) = self
.verify_transactions(
Expand All @@ -77,8 +76,7 @@ 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 slashing = Slash::from_block(blk)?;

let (txs, verification_output) = self
.accept_transactions(
Expand Down
9 changes: 6 additions & 3 deletions rusk/tests/common/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use execution_core::{
use node_data::{
bls::PublicKeyBytes,
ledger::{
Attestation, Block, Fault, Header, IterationsInfo, SpentTransaction,
Attestation, Block, Fault, Header, IterationsInfo, Slash,
SpentTransaction,
},
message::payload::Vote,
};
Expand Down Expand Up @@ -103,12 +104,14 @@ pub fn generator_procedure(
)));
}

let to_slash =
Slash::from_iterations_and_faults(&failed_iterations, &faults)?;

let call_params = CallParams {
round,
block_gas_limit,
generator_pubkey,
missed_generators,
faults,
to_slash,
voters_pubkey: None,
};

Expand Down

0 comments on commit 2671e4b

Please sign in to comment.