Skip to content

Commit

Permalink
rusk: change accept to take block header
Browse files Browse the repository at this point in the history
See also #3437
  • Loading branch information
herr-seppia committed Jan 31, 2025
1 parent f60e135 commit 94b32d9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 47 deletions.
16 changes: 10 additions & 6 deletions rusk/benches/block_ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use criterion::{
};
use dusk_core::transfer::Transaction as ProtocolTransaction;
use node_data::bls::PublicKey;
use node_data::ledger::Transaction;
use node_data::ledger::{Header, Transaction};
use rand::prelude::StdRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;
Expand Down Expand Up @@ -116,14 +116,21 @@ fn bench_accept(
const BLOCK_GAS_LIMIT: u64 = 1_000_000_000_000;
const BLOCK_HASH: [u8; 32] = [0u8; 32];

let generator = PublicKey::new(*DUSK_CONSENSUS_KEY).into_inner();
let generator = *PublicKey::new(*DUSK_CONSENSUS_KEY).bytes();

let txs = Arc::new(txs);
let prev_root = rusk.state_root();

for n_txs in N_TXS {
let rusk = rusk.clone();
let txs = txs.clone();
let header = Header {
height: BLOCK_HEIGHT,
gas_limit: BLOCK_GAS_LIMIT,
hash: BLOCK_HASH,
generator_bls_pubkey: generator,
..Default::default()
};

group.bench_with_input(
BenchmarkId::new(name, format!("{} TXs", n_txs)),
Expand All @@ -134,10 +141,7 @@ fn bench_accept(

rusk.accept_transactions(
prev_root,
BLOCK_HEIGHT,
BLOCK_GAS_LIMIT,
BLOCK_HASH,
&generator,
&header,
&txs,
None,
vec![],
Expand Down
51 changes: 21 additions & 30 deletions rusk/src/lib/node/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::{mpsc, Arc};
use std::time::Instant;
use std::{fs, io};

use dusk_bytes::Serializable;
use dusk_bytes::{DeserializableSlice, Serializable};
use dusk_consensus::config::{
ratification_extra, ratification_quorum, validation_extra,
validation_quorum, MAX_NUMBER_OF_TRANSACTIONS,
Expand All @@ -29,7 +29,7 @@ use dusk_vm::{
execute, CallReceipt, Error as VMError, ExecutionConfig, Session, VM,
};
use node_data::events::contract::{ContractEvent, ContractTxEvent};
use node_data::ledger::{Hash, Slash, SpentTransaction, Transaction};
use node_data::ledger::{Header, Slash, SpentTransaction, Transaction};
use parking_lot::RwLock;
use rusk_profile::to_rusk_state_id_path;
use tokio::sync::broadcast;
Expand Down Expand Up @@ -239,26 +239,22 @@ impl Rusk {
}

/// Verify the given transactions are ok.
#[allow(clippy::too_many_arguments)]
pub fn verify_transactions(
&self,
prev_commit: [u8; 32],
block_height: u64,
block_hash: Hash,
block_gas_limit: u64,
header: &Header,
generator: &BlsPublicKey,
txs: &[Transaction],
slashing: Vec<Slash>,
voters: &[Voter],
) -> Result<(Vec<SpentTransaction>, VerificationOutput)> {
let block_height = header.height;
let session = self.new_block_session(block_height, prev_commit)?;
let execution_config = self.vm_config.to_execution_config(block_height);

accept(
session,
block_height,
block_hash,
block_gas_limit,
header,
generator,
txs,
slashing,
Expand All @@ -273,14 +269,10 @@ impl Rusk {
/// * `consistency_check` - represents a state_root, the caller expects to
/// be returned on successful transactions execution. Passing a None
/// value disables the check.
#[allow(clippy::too_many_arguments)]
pub fn accept_transactions(
&self,
prev_commit: [u8; 32],
block_height: u64,
block_gas_limit: u64,
block_hash: Hash,
generator: &BlsPublicKey,
header: &Header,
txs: &[Transaction],
consistency_check: Option<VerificationOutput>,
slashing: Vec<Slash>,
Expand All @@ -290,16 +282,19 @@ impl Rusk {
VerificationOutput,
Vec<ContractEvent>,
)> {
let generator = header.generator_bls_pubkey.inner();
let generator = BlsPublicKey::from_slice(generator).map_err(|e| {
Error::Other(anyhow::anyhow!("Error in from_slice {e:?}").into())
})?;
let block_height = header.height;
let session = self.new_block_session(block_height, prev_commit)?;

let execution_config = self.vm_config.to_execution_config(block_height);

let (spent_txs, verification_output, session, events) = accept(
session,
block_height,
block_hash,
block_gas_limit,
generator,
header,
&generator,
txs,
slashing,
voters,
Expand All @@ -323,7 +318,7 @@ impl Rusk {
{
let _ = self.archive_sender.try_send(ArchivalData::ArchivedEvents(
block_height,
block_hash,
header.hash,
events.clone(),
));
}
Expand Down Expand Up @@ -541,12 +536,9 @@ impl Rusk {
}
}

#[allow(clippy::too_many_arguments)]
fn accept(
session: Session,
block_height: u64,
block_hash: Hash,
block_gas_limit: u64,
header: &Header,
generator: &BlsPublicKey,
txs: &[Transaction],
slashing: Vec<Slash>,
Expand All @@ -560,7 +552,8 @@ fn accept(
)> {
let mut session = session;

let mut block_gas_left = block_gas_limit;
let mut block_gas_left = header.gas_limit;
let block_height = header.height;

let mut spent_txs = Vec::with_capacity(txs.len());
let mut dusk_spent = 0;
Expand Down Expand Up @@ -613,13 +606,11 @@ fn accept(

event_bloom.add_events(&coinbase_events);

let coinbase_events: Vec<_> = coinbase_events
.into_iter()
.map(|event| ContractTxEvent {
let coinbase_events =
coinbase_events.into_iter().map(|event| ContractTxEvent {
event: event.into(),
origin: block_hash,
})
.collect();
origin: header.hash,
});
events.extend(coinbase_events);

let state_root = session.root();
Expand Down
17 changes: 6 additions & 11 deletions rusk/src/lib/node/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ impl VMExecution for Rusk {
let (_, verification_output) = self
.verify_transactions(
prev_commit,
blk.header().height,
blk.header().hash,
blk.header().gas_limit,
blk.header(),
&generator,
blk.txs(),
slashing,
Expand Down Expand Up @@ -93,20 +91,17 @@ impl VMExecution for Rusk {
Vec<ContractEvent>,
)> {
debug!("Received accept request");
let generator = blk.header().generator_bls_pubkey.inner();
let generator = BlsPublicKey::from_slice(generator)
.map_err(|e| anyhow::anyhow!("Error in from_slice {e:?}"))?;

let slashing = Slash::from_block(blk)?;
let expected = VerificationOutput::from(blk.header());
let expected = VerificationOutput {
event_bloom: blk.header().event_bloom,
state_root: blk.header().state_hash,
};

let (txs, verification_output, stake_events) = self
.accept_transactions(
prev_root,
blk.header().height,
blk.header().gas_limit,
blk.header().hash,
&generator,
blk.header(),
blk.txs(),
Some(expected),
slashing,
Expand Down

0 comments on commit 94b32d9

Please sign in to comment.