Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Sep 8, 2024
1 parent ec209bd commit 300407b
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 19 deletions.
1 change: 1 addition & 0 deletions consensus/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct CallParams {
pub generator_pubkey: node_data::bls::PublicKey,
pub to_slash: Vec<Slash>,
pub voters_pubkey: Option<Vec<Voter>>,
pub max_txs_bytes: usize,
}

#[derive(Default)]
Expand Down
23 changes: 19 additions & 4 deletions consensus/src/proposal/block_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::commons::RoundUpdate;
use crate::operations::{CallParams, Operations, Voter};
use node_data::ledger::{
to_str, Attestation, Block, Fault, IterationsInfo, Seed, Slash,
MAX_BLOCK_BODY_SIZE,
};
use std::cmp::max;

Expand Down Expand Up @@ -99,6 +100,18 @@ impl<T: Operations> Generator<T> {
faults
};

// We always write the faults len in a u32
let mut faults_size = u32::SIZE;
let faults_hashes: Vec<_> = faults
.iter()
.map(|f| {
faults_size += f.size();
f.hash()
})
.collect();

let faultroot = merkle_root(&faults_hashes);

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

Expand All @@ -107,6 +120,7 @@ impl<T: Operations> Generator<T> {
generator_pubkey: ru.pubkey_bls.clone(),
to_slash,
voters_pubkey: Some(voters.to_owned()),
max_txs_bytes: MAX_BLOCK_BODY_SIZE - faults_size,
};

let result =
Expand All @@ -119,9 +133,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 @@ -145,6 +156,10 @@ impl<T: Operations> Generator<T> {
failed_iterations,
};

Ok(Block::new(blk_header, txs, faults).expect("block should be valid"))
Block::new(blk_header, txs, faults.to_vec()).map_err(|e| {
crate::operations::Error::InvalidEST(anyhow::anyhow!(
"Cannot create new block {e}",
))
})
}
}
20 changes: 18 additions & 2 deletions node-data/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use execution_core::transfer::Transaction as ProtocolTransaction;
use crate::bls::PublicKeyBytes;
use crate::ledger::{
Attestation, Block, Fault, Header, IterationsInfo, Label, SpentTransaction,
StepVotes, Transaction,
StepVotes, Transaction, MAX_BLOCK_BODY_SIZE,
};
use crate::message::payload::{
QuorumType, Ratification, RatificationResult, ValidationResult, Vote,
Expand Down Expand Up @@ -47,19 +47,33 @@ impl Serializable for Block {

// Read transactions count
let tx_len = Self::read_u32_le(r)?;
let mut body_size = 4;

let txs = (0..tx_len)
.map(|_| Transaction::read(r))
.collect::<Result<Vec<_>, _>>()?;
body_size += txs.iter().filter_map(|t| t.size().ok()).sum::<usize>();

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

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

Block::new(header, txs, faults)
body_size += faults.iter().map(|f: &Fault| f.size()).sum::<usize>();

match header.version {
0 if body_size > MAX_BLOCK_BODY_SIZE => Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Block body too large {body_size}/{MAX_BLOCK_BODY_SIZE}"
),
))?,

_ => Block::new(header, txs, faults),
}
}
}

Expand Down Expand Up @@ -87,13 +101,15 @@ impl Serializable for Transaction {
let tx_type = Self::read_u32_le(r)?;

let protocol_tx = Self::read_var_le_bytes32(r)?;
let tx_size = protocol_tx.len();
let inner = ProtocolTransaction::from_slice(&protocol_tx[..])
.map_err(|_| io::Error::from(io::ErrorKind::InvalidData))?;

Ok(Self {
inner,
version,
r#type: tx_type,
size: Some(tx_size),
})
}
}
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 @@ -8,7 +8,7 @@ mod header;
pub use header::{Header, Seed};

mod block;
pub use block::{Block, BlockWithLabel, Hash, Label};
pub use block::*;

mod transaction;
pub use transaction::{SpendingId, SpentTransaction, Transaction};
Expand Down
4 changes: 4 additions & 0 deletions node-data/src/ledger/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

use super::*;

pub const MAX_BLOCK_SIZE: usize = 1_024 * 1_024;
pub const MAX_BLOCK_HEADER_SIZE: usize = 1530;
pub const MAX_BLOCK_BODY_SIZE: usize = MAX_BLOCK_SIZE - MAX_BLOCK_HEADER_SIZE;

pub type Hash = [u8; 32];

#[derive(Default, Debug, Clone)]
Expand Down
24 changes: 23 additions & 1 deletion node-data/src/ledger/faults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ pub enum Fault {
DoubleValidationVote(FaultData<Vote>, FaultData<Vote>),
}

impl Fault {
pub fn size(&self) -> usize {
// prev_block_hash + round + iter
const FAULT_CONSENSUS_HEADER_SIZE: usize = 32 + u64::SIZE + u8::SIZE;
// signer + signature
const FAULT_SIG_INFO_SIZE: usize =
BlsMultisigPublicKey::SIZE + BlsMultisigSignature::SIZE;

const HEADERS: usize = FAULT_CONSENSUS_HEADER_SIZE * 2;
const SIG_INFOS: usize = FAULT_SIG_INFO_SIZE * 2;
let faults_data_size = match self {
Fault::DoubleCandidate(..) => 32 * 2,
Fault::DoubleRatificationVote(a, b) => {
a.data.size() + b.data.size()
}
Fault::DoubleValidationVote(a, b) => a.data.size() + b.data.size(),
};

HEADERS + SIG_INFOS + faults_data_size
}
}

#[derive(Debug, Error)]
pub enum InvalidFault {
#[error("Inner faults have same data")]
Expand Down Expand Up @@ -64,7 +86,7 @@ impl From<BlsSigError> for InvalidFault {
}

impl Fault {
/// Hash the serialized form
/// Hash the serialized form (returning the bytes hashed)
pub fn hash(&self) -> [u8; 32] {
let mut b = vec![];
self.write(&mut b).expect("Write to a vec shall not fail");
Expand Down
21 changes: 20 additions & 1 deletion node-data/src/ledger/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,35 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_bytes::Serializable;
use std::io;

use dusk_bytes::Serializable as DuskSerializable;
use execution_core::signatures::bls;
use execution_core::transfer::Transaction as ProtocolTransaction;
use execution_core::BlsScalar;
use serde::Serialize;

use crate::Serializable;

#[derive(Debug, Clone)]
pub struct Transaction {
pub version: u32,
pub r#type: u32,
pub inner: ProtocolTransaction,
pub(crate) size: Option<usize>,
}

impl Transaction {
pub fn size(&self) -> io::Result<usize> {
match self.size {
Some(size) => Ok(size),
None => {
let mut buf = vec![];
self.write(&mut buf)?;
Ok(buf.len())
}
}
}
}

impl From<ProtocolTransaction> for Transaction {
Expand All @@ -23,6 +41,7 @@ impl From<ProtocolTransaction> for Transaction {
inner: value,
r#type: 1,
version: 1,
size: None,
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions node-data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,20 @@ pub mod payload {
NoQuorum = 3,
}

impl Vote {
pub fn size(&self) -> usize {
const ENUM_BYTE: usize = 1;

let data_size: usize = match &self {
Vote::NoCandidate => 0,
Vote::Valid(_) => 32,
Vote::Invalid(_) => 32,
Vote::NoQuorum => 0,
};
ENUM_BYTE + data_size
}
}

impl fmt::Debug for Vote {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (desc, hash) = match &self {
Expand Down
12 changes: 2 additions & 10 deletions rusk/benches/block_ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ fn load_phoenix_txs() -> Vec<Transaction> {
let line = line.unwrap();
let tx_bytes = hex::decode(line).unwrap();
let tx = ProtocolTransaction::from_slice(&tx_bytes).unwrap();
txs.push(Transaction {
version: 1,
r#type: 0,
inner: tx,
});
txs.push(tx.into());
}

preverify(&txs);
Expand All @@ -65,11 +61,7 @@ fn load_moonlight_txs() -> Vec<Transaction> {
let line = line.unwrap();
let tx_bytes = hex::decode(line).unwrap();
let tx = ProtocolTransaction::from_slice(&tx_bytes).unwrap();
txs.push(Transaction {
version: 1,
r#type: 0,
inner: tx,
});
txs.push(tx.into());
}

preverify(&txs);
Expand Down
16 changes: 16 additions & 0 deletions rusk/src/lib/node/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ impl Rusk {

let mut event_hasher = Sha3_256::new();

// We always write the faults len in a u32
let mut size_left = params.max_txs_bytes - 4;

for unspent_tx in txs {
if let Some(timeout) = self.generation_timeout {
if started.elapsed() > timeout {
Expand All @@ -150,6 +153,19 @@ impl Rusk {
continue;
}

let tx_len = unspent_tx.size().unwrap_or_default();

if tx_len == 0 {
info!("Skipping {tx_id_hex} due to error while calculating the len");
continue;
}

if tx_len > size_left {
info!("Skipping {tx_id_hex} due size greater than bytes left: {size_left}");
continue;
}
size_left -= tx_len;

match execute(
&mut session,
&unspent_tx.inner,
Expand Down
2 changes: 2 additions & 0 deletions rusk/tests/common/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use node_data::{
bls::PublicKeyBytes,
ledger::{
Attestation, Block, Header, IterationsInfo, Slash, SpentTransaction,
MAX_BLOCK_BODY_SIZE,
},
message::payload::Vote,
};
Expand Down Expand Up @@ -127,6 +128,7 @@ pub fn generator_procedure(
generator_pubkey,
to_slash,
voters_pubkey: None,
max_txs_bytes: MAX_BLOCK_BODY_SIZE,
};

let (transfer_txs, discarded, execute_output) =
Expand Down

0 comments on commit 300407b

Please sign in to comment.