Skip to content

Commit

Permalink
primitives: use alloy Header struct (paradigmxyz#10691)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored and 0xForerunner committed Sep 25, 2024
1 parent 4795346 commit 05b3aa3
Show file tree
Hide file tree
Showing 85 changed files with 826 additions and 991 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions bin/reth-bench/src/bench/new_payload_fcu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ impl Command {
)
.await?;

let new_payload_result = NewPayloadResult { gas_used, latency: start.elapsed() };
let new_payload_result =
NewPayloadResult { gas_used: gas_used as u64, latency: start.elapsed() };

call_forkchoice_updated(&auth_provider, message_version, forkchoice_state, None)
.await?;
Expand All @@ -118,7 +119,8 @@ impl Command {
info!(%combined_result);

// record the current result
let gas_row = TotalGasRow { block_number, gas_used, time: current_duration };
let gas_row =
TotalGasRow { block_number, gas_used: gas_used as u64, time: current_duration };
results.push((gas_row, combined_result));
}

Expand Down
6 changes: 4 additions & 2 deletions bin/reth-bench/src/bench/new_payload_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ impl Command {
call_new_payload(&auth_provider, payload, parent_beacon_block_root, versioned_hashes)
.await?;

let new_payload_result = NewPayloadResult { gas_used, latency: start.elapsed() };
let new_payload_result =
NewPayloadResult { gas_used: gas_used as u64, latency: start.elapsed() };
info!(%new_payload_result);

// current duration since the start of the benchmark
let current_duration = total_benchmark_duration.elapsed();

// record the current result
let row = TotalGasRow { block_number, gas_used, time: current_duration };
let row =
TotalGasRow { block_number, gas_used: gas_used as u64, time: current_duration };
results.push((row, new_payload_result));
}

Expand Down
62 changes: 33 additions & 29 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,9 @@ where

let parent_header = provider
.header(&block.parent_hash)?
.ok_or_else(|| BlockchainTreeError::CanonicalChain { block_hash: block.parent_hash })?
.seal(block.parent_hash);
.ok_or_else(|| BlockchainTreeError::CanonicalChain { block_hash: block.parent_hash })?;

let parent_sealed_header = SealedHeader::new(parent_header, block.parent_hash);

let canonical_chain = self.state.block_indices.canonical_chain();

Expand All @@ -427,7 +428,7 @@ where

let chain = AppendableChain::new_canonical_fork(
block,
&parent_header,
&parent_sealed_header,
canonical_chain.inner(),
parent,
&self.externals,
Expand Down Expand Up @@ -993,7 +994,7 @@ where
header = provider.header(hash)?
}

Ok(header.map(|header| header.seal(*hash)))
Ok(header.map(|header| SealedHeader::new(header, *hash)))
}

/// Determines whether or not a block is canonical, checking the db if necessary.
Expand Down Expand Up @@ -1375,7 +1376,7 @@ where
mod tests {
use super::*;
use alloy_genesis::{Genesis, GenesisAccount};
use alloy_primitives::{keccak256, Address, B256};
use alloy_primitives::{keccak256, Address, Sealable, B256};
use assert_matches::assert_matches;
use linked_hash_set::LinkedHashSet;
use reth_chainspec::{ChainSpecBuilder, MAINNET, MIN_TRANSACTION_GAS};
Expand Down Expand Up @@ -1598,32 +1599,35 @@ mod tests {
// receipts root computation is different for OP
let receipts_root = calculate_receipt_root(&receipts);

let sealed = Header {
number,
parent_hash: parent.unwrap_or_default(),
gas_used: (body.len() as u64 * MIN_TRANSACTION_GAS) as u128,
gas_limit: chain_spec.max_gas_limit.into(),
mix_hash: B256::random(),
base_fee_per_gas: Some(EIP1559_INITIAL_BASE_FEE.into()),
transactions_root,
receipts_root,
state_root: state_root_unhashed(HashMap::from([(
signer,
(
AccountInfo {
balance: initial_signer_balance -
(single_tx_cost * U256::from(num_of_signer_txs)),
nonce: num_of_signer_txs,
..Default::default()
},
EMPTY_ROOT_HASH,
),
)])),
..Default::default()
}
.seal_slow();
let (header, seal) = sealed.into_parts();

SealedBlockWithSenders::new(
SealedBlock {
header: Header {
number,
parent_hash: parent.unwrap_or_default(),
gas_used: body.len() as u64 * MIN_TRANSACTION_GAS,
gas_limit: chain_spec.max_gas_limit,
mix_hash: B256::random(),
base_fee_per_gas: Some(EIP1559_INITIAL_BASE_FEE),
transactions_root,
receipts_root,
state_root: state_root_unhashed(HashMap::from([(
signer,
(
AccountInfo {
balance: initial_signer_balance -
(single_tx_cost * U256::from(num_of_signer_txs)),
nonce: num_of_signer_txs,
..Default::default()
},
EMPTY_ROOT_HASH,
),
)])),
..Default::default()
}
.seal_slow(),
header: SealedHeader::new(header, seal),
body: body.clone().into_iter().map(|tx| tx.into_signed()).collect(),
ommers: Vec::new(),
withdrawals: Some(Withdrawals::default()),
Expand Down
13 changes: 11 additions & 2 deletions crates/chain-state/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,18 @@ impl CanonicalInMemoryState {
index: index as u64,
block_hash: block_state.hash(),
block_number: block_state.block().block.number,
base_fee: block_state.block().block().header.base_fee_per_gas,
base_fee: block_state
.block()
.block()
.header
.base_fee_per_gas
.map(|base_fee| base_fee as u64),
timestamp: block_state.block().block.timestamp,
excess_blob_gas: block_state.block().block.excess_blob_gas,
excess_blob_gas: block_state
.block()
.block
.excess_blob_gas
.map(|excess_blob| excess_blob as u64),
};
return Some((tx.clone(), meta))
}
Expand Down
16 changes: 10 additions & 6 deletions crates/chain-state/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use rand::{thread_rng, Rng};
use reth_chainspec::{ChainSpec, EthereumHardfork, MIN_TRANSACTION_GAS};
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_primitives::{
alloy_primitives::Sealable,
constants::{EIP1559_INITIAL_BASE_FEE, EMPTY_ROOT_HASH},
proofs::{calculate_receipt_root, calculate_transaction_root, calculate_withdrawals_root},
Header, Receipt, Receipts, Requests, SealedBlock, SealedBlockWithSenders, Signature,
Transaction, TransactionSigned, TransactionSignedEcRecovered, TxEip1559,
Header, Receipt, Receipts, Requests, SealedBlock, SealedBlockWithSenders, SealedHeader,
Signature, Transaction, TransactionSigned, TransactionSignedEcRecovered, TxEip1559,
};
use reth_trie::{root::state_root_unhashed, updates::TrieUpdates, HashedPostState};
use revm::{db::BundleState, primitives::AccountInfo};
Expand Down Expand Up @@ -138,10 +139,10 @@ impl TestBlockBuilder {
let header = Header {
number,
parent_hash,
gas_used: transactions.len() as u64 * MIN_TRANSACTION_GAS,
gas_limit: self.chain_spec.max_gas_limit,
gas_used: transactions.len() as u128 * MIN_TRANSACTION_GAS as u128,
gas_limit: self.chain_spec.max_gas_limit.into(),
mix_hash: B256::random(),
base_fee_per_gas: Some(EIP1559_INITIAL_BASE_FEE),
base_fee_per_gas: Some(EIP1559_INITIAL_BASE_FEE.into()),
transactions_root: calculate_transaction_root(&transactions),
receipts_root: calculate_receipt_root(&receipts),
beneficiary: Address::random(),
Expand All @@ -166,8 +167,11 @@ impl TestBlockBuilder {
..Default::default()
};

let sealed = header.seal_slow();
let (header, seal) = sealed.into_parts();

let block = SealedBlock {
header: header.seal_slow(),
header: SealedHeader::new(header, seal),
body: transactions.into_iter().map(|tx| tx.into_signed()).collect(),
ommers: Vec::new(),
withdrawals: Some(vec![].into()),
Expand Down
10 changes: 5 additions & 5 deletions crates/chainspec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,19 @@ impl ChainSpec {
};

Header {
gas_limit: self.genesis.gas_limit as u64,
gas_limit: self.genesis.gas_limit,
difficulty: self.genesis.difficulty,
nonce: self.genesis.nonce,
nonce: self.genesis.nonce.into(),
extra_data: self.genesis.extra_data.clone(),
state_root: state_root_ref_unhashed(&self.genesis.alloc),
timestamp: self.genesis.timestamp,
mix_hash: self.genesis.mix_hash,
beneficiary: self.genesis.coinbase,
base_fee_per_gas,
base_fee_per_gas: base_fee_per_gas.map(Into::into),
withdrawals_root,
parent_beacon_block_root,
blob_gas_used,
excess_blob_gas,
blob_gas_used: blob_gas_used.map(Into::into),
excess_blob_gas: excess_blob_gas.map(Into::into),
requests_root,
..Default::default()
}
Expand Down
21 changes: 13 additions & 8 deletions crates/consensus/auto-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ impl StorageInner {
withdrawals_root: withdrawals.map(|w| proofs::calculate_withdrawals_root(w)),
difficulty: U256::from(2),
number: self.best_block + 1,
gas_limit: chain_spec.max_gas_limit,
gas_limit: chain_spec.max_gas_limit.into(),
timestamp,
base_fee_per_gas,
blob_gas_used,
blob_gas_used: blob_gas_used.map(Into::into),
requests_root: requests.map(|r| proofs::calculate_requests_root(&r.0)),
..Default::default()
};
Expand All @@ -317,8 +317,13 @@ impl StorageInner {
}
_ => (0, 0),
};
header.excess_blob_gas =
Some(calculate_excess_blob_gas(parent_excess_blob_gas, parent_blob_gas_used))
header.excess_blob_gas = Some(
calculate_excess_blob_gas(
parent_excess_blob_gas as u64,
parent_blob_gas_used as u64,
)
.into(),
)
}

header
Expand Down Expand Up @@ -392,7 +397,7 @@ impl StorageInner {

// now we need to update certain header fields with the results of the execution
header.state_root = db.state_root(hashed_state)?;
header.gas_used = gas_used;
header.gas_used = gas_used.into();

let receipts = execution_outcome.receipts_by_block(header.number);

Expand Down Expand Up @@ -420,7 +425,7 @@ impl StorageInner {
self.insert_new_block(header.clone(), body);

// set new header with hash that should have been updated by insert_new_block
let new_header = header.seal(self.best_hash);
let new_header = SealedHeader::new(header, self.best_hash);

Ok((new_header, execution_outcome))
}
Expand Down Expand Up @@ -574,7 +579,7 @@ mod tests {
assert_eq!(header.parent_hash, best_block_hash);
assert_eq!(header.number, best_block_number + 1);
assert_eq!(header.timestamp, timestamp);
assert_eq!(header.gas_limit, chain_spec.max_gas_limit);
assert_eq!(header.gas_limit, chain_spec.max_gas_limit.into());
}

#[test]
Expand Down Expand Up @@ -668,7 +673,7 @@ mod tests {
withdrawals_root: None,
difficulty: U256::from(2),
number: 1,
gas_limit: chain_spec.max_gas_limit,
gas_limit: chain_spec.max_gas_limit.into(),
timestamp,
base_fee_per_gas: None,
blob_gas_used: Some(0),
Expand Down
5 changes: 4 additions & 1 deletion crates/consensus/beacon/src/engine/invalid_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ struct InvalidHeaderCacheMetrics {
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::Sealable;

#[test]
fn test_hit_eviction() {
let mut cache = InvalidHeaderCache::new(10);
let header = Header::default().seal_slow();
let sealed = Header::default().seal_slow();
let (header, seal) = sealed.into_parts();
let header = SealedHeader::new(header, seal);
cache.insert(header.clone());
assert_eq!(cache.headers.get(&header.hash()).unwrap().hit_count, 0);

Expand Down
5 changes: 3 additions & 2 deletions crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ where
.blockchain
.find_block_by_hash(safe_block_hash, BlockSource::Any)?
.ok_or_else(|| ProviderError::UnknownBlockHash(safe_block_hash))?;
self.blockchain.set_safe(safe.header.seal(safe_block_hash));
self.blockchain.set_safe(SealedHeader::new(safe.header, safe_block_hash));
}
Ok(())
}
Expand All @@ -967,7 +967,8 @@ where
.find_block_by_hash(finalized_block_hash, BlockSource::Any)?
.ok_or_else(|| ProviderError::UnknownBlockHash(finalized_block_hash))?;
self.blockchain.finalize_block(finalized.number)?;
self.blockchain.set_finalized(finalized.header.seal(finalized_block_hash));
self.blockchain
.set_finalized(SealedHeader::new(finalized.header, finalized_block_hash));
}
Ok(())
}
Expand Down
11 changes: 8 additions & 3 deletions crates/consensus/beacon/src/engine/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ impl<N: ProviderNodeTypes> PipelineState<N> {
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::Sealable;
use assert_matches::assert_matches;
use futures::poll;
use reth_chainspec::{ChainSpec, ChainSpecBuilder, MAINNET};
Expand Down Expand Up @@ -598,7 +599,9 @@ mod tests {
header.parent_hash = hash;
header.number += 1;
header.timestamp += 1;
sealed_header = header.seal_slow();
let sealed = header.seal_slow();
let (header, seal) = sealed.into_parts();
sealed_header = SealedHeader::new(header, seal);
client.insert(sealed_header.clone(), body.clone());
}
}
Expand All @@ -614,12 +617,14 @@ mod tests {
);

let client = TestFullBlockClient::default();
let header = Header {
let sealed = Header {
base_fee_per_gas: Some(7),
gas_limit: chain_spec.max_gas_limit,
gas_limit: chain_spec.max_gas_limit.into(),
..Default::default()
}
.seal_slow();
let (header, seal) = sealed.into_parts();
let header = SealedHeader::new(header, seal);
insert_headers_into_client(&client, header, 0..10);

// set up a pipeline
Expand Down
Loading

0 comments on commit 05b3aa3

Please sign in to comment.