From c846375a5141f617732dce610dfe58e1c7c64721 Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Sat, 6 Jul 2024 11:47:58 +0200 Subject: [PATCH] wip --- consensus/src/proposal/block_generator.rs | 13 +++--- node-data/src/ledger.rs | 1 - node-data/src/ledger/attestation.rs | 35 +--------------- node-data/src/ledger/faults.rs | 50 +++++++++++------------ node/src/chain/acceptor.rs | 19 ++++----- rusk/benches/block_ingestion.rs | 2 +- rusk/src/lib/chain/vm.rs | 6 +-- rusk/tests/common/state.rs | 9 ++-- 8 files changed, 48 insertions(+), 87 deletions(-) diff --git a/consensus/src/proposal/block_generator.rs b/consensus/src/proposal/block_generator.rs index cd6af73b2d..b5d4e65ee7 100644 --- a/consensus/src/proposal/block_generator.rs +++ b/consensus/src/proposal/block_generator.rs @@ -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 { executor: Arc, @@ -92,11 +92,12 @@ impl Generator { faults: Vec, voters: &[VoterWithCredits], ) -> Result { - 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, diff --git a/node-data/src/ledger.rs b/node-data/src/ledger.rs index 46a8a106a6..1cfc4a89c9 100644 --- a/node-data/src/ledger.rs +++ b/node-data/src/ledger.rs @@ -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}; diff --git a/node-data/src/ledger/attestation.rs b/node-data/src/ledger/attestation.rs index 3664303cce..5d532de817 100644 --- a/node-data/src/ledger/attestation.rs +++ b/node-data/src/ledger/attestation.rs @@ -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))] @@ -95,37 +93,6 @@ impl IterationsInfo { att_list: attestations, } } - - pub fn to_slash(&self) -> Result, io::Error> { - Ok(self - .att_list - .iter() - .flatten() - .flat_map(Slash::from_iteration_info) - .flatten() - .collect()) - } - - pub fn to_missed_generators(&self) -> Result, 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 { - self.att_list - .iter() - .flatten() - .filter(|(c, _)| { - c.result == RatificationResult::Fail(Vote::NoCandidate) - }) - .map(|(_, pk)| pk) - } } #[cfg(any(feature = "faker", test))] diff --git a/node-data/src/ledger/faults.rs b/node-data/src/ledger/faults.rs index a7faa18d7f..2f681c567d 100644 --- a/node-data/src/ledger/faults.rs +++ b/node-data/src/ledger/faults.rs @@ -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 { @@ -236,7 +228,7 @@ pub enum SlashType { } impl Slash { - pub fn from_iteration_info( + fn from_iteration_info( value: &IterationInfo, ) -> Result, dusk_bytes::Error> { let (attestation, provisioner) = value; @@ -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, 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, io::Error> { + let mut slashing = failed_iterations + .att_list + .iter() + .flatten() + .flat_map(Slash::from_iteration_info) + .flatten() + .collect::>(); + slashing.extend(faults.iter().map(Slash::from)); + Ok(slashing) + } } impl From<&Fault> for Slash { @@ -275,20 +288,3 @@ impl From<&Fault> for Slash { } } } - -impl From 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, - } - } -} diff --git a/node/src/chain/acceptor.rs b/node/src/chain/acceptor.rs index ae7e498217..c48f500eb7 100644 --- a/node/src/chain/acceptor.rs +++ b/node/src/chain/acceptor.rs @@ -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; @@ -294,18 +294,16 @@ impl Acceptor { 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 @@ -483,9 +481,8 @@ impl Acceptor { 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; } diff --git a/rusk/benches/block_ingestion.rs b/rusk/benches/block_ingestion.rs index 7417b66430..152f964daf 100644 --- a/rusk/benches/block_ingestion.rs +++ b/rusk/benches/block_ingestion.rs @@ -104,7 +104,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/vm.rs b/rusk/src/lib/chain/vm.rs index 46f4de6825..c0d699e4ee 100644 --- a/rusk/src/lib/chain/vm.rs +++ b/rusk/src/lib/chain/vm.rs @@ -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( @@ -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( diff --git a/rusk/tests/common/state.rs b/rusk/tests/common/state.rs index 64abe8017e..f96ea95d17 100644 --- a/rusk/tests/common/state.rs +++ b/rusk/tests/common/state.rs @@ -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, }; @@ -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, };