Skip to content

Commit

Permalink
wip: move Faults into Block::header
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Jul 10, 2024
1 parent 745349e commit c7d5adc
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 86 deletions.
7 changes: 2 additions & 5 deletions consensus/src/proposal/block_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ impl<T: Operations> Generator<T> {
let txs: Vec<_> = result.txs.into_iter().map(|t| t.inner).collect();
let txroot = merkle_root(&tx_hashes[..]);

let faults = Vec::<Fault>::new();
let faults_hashes: Vec<_> = faults.iter().map(|f| f.hash()).collect();
let faultroot = merkle_root(&faults_hashes);
let timestamp =
max(ru.timestamp() + MINIMUM_BLOCK_TIME, get_current_timestamp());

Expand All @@ -136,11 +133,11 @@ impl<T: Operations> Generator<T> {
att: Attestation::default(),
prev_block_cert: *ru.att(),
txroot,
faultroot,
faults,
iteration,
failed_iterations,
};

Ok(Block::new(blk_header, txs, faults).expect("block should be valid"))
Ok(Block::new(blk_header, txs).expect("block should be valid"))
}
}
24 changes: 7 additions & 17 deletions node-data/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use execution_core::transfer::Transaction as PhoenixTransaction;

use crate::bls::PublicKeyBytes;
use crate::ledger::{
Attestation, Block, Fault, Header, IterationsInfo, Label, SpentTransaction,
Attestation, Block, Header, IterationsInfo, Label, SpentTransaction,
StepVotes, Transaction,
};
use crate::message::payload::{
Expand All @@ -30,12 +30,6 @@ impl Serializable for Block {
t.write(w)?;
}

let faults_len = self.faults().len() as u32;
w.write_all(&faults_len.to_le_bytes())?;

for f in self.faults().iter() {
f.write(w)?;
}
Ok(())
}

Expand All @@ -52,14 +46,7 @@ impl Serializable for Block {
.map(|_| Transaction::read(r))
.collect::<Result<Vec<_>, _>>()?;

// Read faults count
let faults_len = Self::read_u32_le(r)?;

let faults = (0..faults_len)
.map(|_| Fault::read(r))
.collect::<Result<Vec<_>, _>>()?;

Block::new(header, txs, faults)
Block::new(header, txs)
}
}

Expand Down Expand Up @@ -416,7 +403,10 @@ impl Serializable for QuorumType {

#[cfg(test)]
mod tests {
use crate::message::payload::{Candidate, Validation};
use crate::{
ledger::Fault,
message::payload::{Candidate, Validation},
};

use super::*;
use fake::{Dummy, Fake, Faker};
Expand Down Expand Up @@ -482,7 +472,7 @@ mod tests {
}

#[test]
fn test_encoding_faul() {
fn test_encoding_fault() {
assert_serializable::<Fault>();
}
}
26 changes: 5 additions & 21 deletions node-data/src/ledger/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub type Hash = [u8; 32];
pub struct Block {
header: Header,
txs: Vec<Transaction>,
faults: Vec<Fault>,
}

impl PartialEq<Self> for Block {
Expand All @@ -25,16 +24,8 @@ impl Eq for Block {}

impl Block {
/// Creates a new block and calculates block hash, if missing.
pub fn new(
header: Header,
txs: Vec<Transaction>,
faults: Vec<Fault>,
) -> io::Result<Self> {
let mut b = Block {
header,
txs,
faults,
};
pub fn new(header: Header, txs: Vec<Transaction>) -> io::Result<Self> {
let mut b = Block { header, txs };
b.calculate_hash()?;
Ok(b)
}
Expand All @@ -58,13 +49,6 @@ impl Block {
pub fn txs(&self) -> &Vec<Transaction> {
&self.txs
}
pub fn faults(&self) -> &Vec<Fault> {
&self.faults
}

pub fn into_faults(self) -> Vec<Fault> {
self.faults
}

pub fn set_attestation(&mut self, att: Attestation) {
self.header.att = att;
Expand Down Expand Up @@ -115,10 +99,10 @@ pub mod faker {
gen_dummy_tx(rng.gen()),
gen_dummy_tx(rng.gen()),
];
let header: Header = Faker.fake();
let faults = vec![Faker.fake(), Faker.fake(), Faker.fake()];
let mut header: Header = Faker.fake();
header.faults = vec![Faker.fake(), Faker.fake(), Faker.fake()];

Block::new(header, txs, faults).expect("valid hash")
Block::new(header, txs).expect("valid hash")
}
}
}
6 changes: 3 additions & 3 deletions node-data/src/ledger/faults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@ impl Slash {
}))
}

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

Expand Down
24 changes: 18 additions & 6 deletions node-data/src/ledger/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use super::*;

pub type Seed = Signature;
#[derive(Default, Eq, PartialEq, Clone)]
#[derive(Default, Clone)]
#[cfg_attr(any(feature = "faker", test), derive(Dummy))]
pub struct Header {
// Hashable fields
Expand All @@ -20,11 +20,11 @@ pub struct Header {
pub event_hash: Hash,
pub generator_bls_pubkey: PublicKeyBytes,
pub txroot: Hash,
pub faultroot: Hash,
pub gas_limit: u64,
pub iteration: u8,
pub prev_block_cert: Attestation,
pub failed_iterations: IterationsInfo,
pub faults: Vec<Fault>,

// Block hash
pub hash: Hash,
Expand Down Expand Up @@ -52,7 +52,7 @@ impl std::fmt::Debug for Header {
.field("hash", &to_str(&self.hash))
.field("att", &self.att)
.field("tx_root", &to_str(&self.txroot))
.field("fault_root", &to_str(&self.faultroot))
.field("fault_root", &self.faults)
.finish()
}
}
Expand All @@ -74,12 +74,18 @@ impl Header {
w.write_all(&self.event_hash)?;
w.write_all(self.generator_bls_pubkey.inner())?;
w.write_all(&self.txroot)?;
w.write_all(&self.faultroot)?;
w.write_all(&self.gas_limit.to_le_bytes())?;
w.write_all(&self.iteration.to_le_bytes())?;
self.prev_block_cert.write(w)?;
self.failed_iterations.write(w)?;

let faults_len = self.faults.len() as u32;
w.write_all(&faults_len.to_le_bytes())?;

for f in self.faults.iter() {
f.write(w)?;
}

Ok(())
}

Expand All @@ -94,13 +100,19 @@ impl Header {
let event_hash = Self::read_bytes(r)?;
let generator_bls_pubkey = Self::read_bytes(r)?;
let txroot = Self::read_bytes(r)?;
let faultroot = Self::read_bytes(r)?;
let gas_limit = Self::read_u64_le(r)?;
let iteration = Self::read_u8(r)?;

let prev_block_cert = Attestation::read(r)?;
let failed_iterations = IterationsInfo::read(r)?;

// Read faults count
let faults_len = Self::read_u32_le(r)?;

let faults = (0..faults_len)
.map(|_| Fault::read(r))
.collect::<Result<Vec<_>, _>>()?;

Ok(Header {
version,
height,
Expand All @@ -113,7 +125,7 @@ impl Header {
state_hash,
event_hash,
txroot,
faultroot,
faults,
hash: [0; 32],
att: Default::default(),
prev_block_cert,
Expand Down
8 changes: 5 additions & 3 deletions node-data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,8 @@ impl std::fmt::Debug for SignInfo {
#[cfg(test)]
#[allow(unused)]
mod tests {
use fake::Fake;

use self::payload::ValidationResult;

use super::*;
Expand Down Expand Up @@ -1338,7 +1340,7 @@ mod tests {
event_hash: [5; 32],
hash: [6; 32],
txroot: [7; 32],
faultroot: [8; 32],
faults: vec![],
att: Attestation {
validation: ledger::StepVotes::new([6; 48], 22222222),
ratification: ledger::StepVotes::new([7; 48], 3333333),
Expand All @@ -1353,8 +1355,8 @@ mod tests {
failed_iterations: Default::default(),
};

let sample_block = ledger::Block::new(header, vec![], vec![])
.expect("should be valid block");
let sample_block =
ledger::Block::new(header, vec![]).expect("should be valid block");

let sign_info = SignInfo {
signer: bls::PublicKey::from_sk_seed_u64(3),
Expand Down
7 changes: 1 addition & 6 deletions node/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,7 @@ impl<N: Network, DB: database::DB, VM: vm::VMExecution> ChainSrv<N, DB, VM> {
let genesis_blk = genesis::generate_state(state);
db.write().await.update(|t| {
// Persist genesis block
t.store_block(
genesis_blk.header(),
&[],
&[],
Label::Final(0),
)
t.store_block(genesis_blk.header(), &[], Label::Final(0))
})?;

BlockWithLabel::new_with_label(genesis_blk, Label::Final(0))
Expand Down
10 changes: 5 additions & 5 deletions node/src/chain/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
let mut changed_provisioners = vec![reward, dusk_reward];

// Update provisioners if a slash has been applied
let slashed = Slash::from_block(blk)?
let slashed = Slash::from_header(blk.header())?
.into_iter()
.map(|f| ProvisionerChange::Slash(f.provisioner));
changed_provisioners.extend(slashed);
Expand Down Expand Up @@ -441,7 +441,8 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
)
.await?;

header_validation::verify_faults(self.db.clone(), blk.faults()).await?;
header_validation::verify_faults(self.db.clone(), &blk.header().faults)
.await?;

// Elapsed time header verification
histogram!("dusk_block_header_elapsed")
Expand Down Expand Up @@ -470,8 +471,7 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {

let label = rolling_results.0;
// Store block with updated transactions with Error and GasSpent
block_size_on_disk =
db.store_block(header, &txs, blk.faults(), label)?;
block_size_on_disk = db.store_block(header, &txs, label)?;

Ok((txs, rolling_results))
})?;
Expand All @@ -483,7 +483,7 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
header.height,
);

for slashed in Slash::from_block(blk)? {
for slashed in Slash::from_header(blk.header())? {
info!("Slashed {}", slashed.provisioner.to_base58());
slashed_count += 1;
}
Expand Down
1 change: 0 additions & 1 deletion node/src/chain/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub(crate) fn generate_state(state_hash: [u8; 32]) -> Block {
..Default::default()
},
vec![],
vec![],
)
.expect("block should be valid")
}
1 change: 0 additions & 1 deletion node/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub trait Ledger {
&self,
header: &ledger::Header,
txs: &[SpentTransaction],
faults: &[Fault],
label: Label,
) -> Result<usize>;

Expand Down
Loading

0 comments on commit c7d5adc

Please sign in to comment.