Skip to content

Commit

Permalink
chore: reduce size of common types (#5304)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Nov 6, 2023
1 parent 2d315c2 commit f8ceda9
Show file tree
Hide file tree
Showing 55 changed files with 580 additions and 532 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/reth/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {

let genesis_hash = init_genesis(db.clone(), self.chain.clone())?;

info!(target: "reth::cli", "{}", DisplayHardforks::from(self.chain.hardforks().clone()));
info!(target: "reth::cli", "{}", DisplayHardforks::new(self.chain.hardforks()));

let consensus = self.consensus();

Expand Down
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
msrv = "1.70"
ignore-interior-mutability = ["bytes::Bytes", "reth_primitives::trie::nibbles::Nibbles"]
too-large-for-stack = 128
9 changes: 4 additions & 5 deletions crates/blockchain-tree/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use reth_interfaces::{
RethResult,
};
use reth_primitives::{
BlockHash, BlockNumber, ForkBlock, SealedBlockWithSenders, SealedHeader, U256,
BlockHash, BlockNumber, ForkBlock, GotExpected, SealedBlockWithSenders, SealedHeader, U256,
};
use reth_provider::{
providers::BundleStateProvider, BundleStateDataProvider, BundleStateWithReceipts, Chain,
Expand Down Expand Up @@ -221,10 +221,9 @@ impl AppendableChain {
// check state root
let state_root = provider.state_root(&bundle_state)?;
if block.state_root != state_root {
return Err(ConsensusError::BodyStateRootDiff {
got: state_root,
expected: block.state_root,
}
return Err(ConsensusError::BodyStateRootDiff(
GotExpected { got: state_root, expected: block.state_root }.into(),
)
.into())
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/beacon/src/engine/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum BeaconConsensusEngineEvent {
/// A block was added to the canonical chain.
CanonicalBlockAdded(Arc<SealedBlock>),
/// A canonical chain was committed.
CanonicalChainCommitted(SealedHeader, Duration),
CanonicalChainCommitted(Box<SealedHeader>, Duration),
/// A block was added to the fork chain.
ForkBlockAdded(Arc<SealedBlock>),
}
21 changes: 9 additions & 12 deletions crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,8 @@ where

let status = match make_canonical_result {
Ok(outcome) => {
match outcome {
CanonicalOutcome::AlreadyCanonical { ref header } => {
match &outcome {
CanonicalOutcome::AlreadyCanonical { header } => {
// On Optimism, the proposers are allowed to reorg their own chain at will.
cfg_if::cfg_if! {
if #[cfg(feature = "optimism")] {
Expand All @@ -679,7 +679,7 @@ where
let _ = self.update_head(header.clone());
self.listeners.notify(
BeaconConsensusEngineEvent::CanonicalChainCommitted(
header.clone(),
Box::new(header.clone()),
elapsed,
),
);
Expand All @@ -701,7 +701,7 @@ where
}
}
}
CanonicalOutcome::Committed { ref head } => {
CanonicalOutcome::Committed { head } => {
debug!(
target: "consensus::engine",
hash=?state.head_block_hash,
Expand All @@ -712,7 +712,7 @@ where
// new VALID update that moved the canonical chain forward
let _ = self.update_head(head.clone());
self.listeners.notify(BeaconConsensusEngineEvent::CanonicalChainCommitted(
head.clone(),
Box::new(head.clone()),
elapsed,
));
}
Expand Down Expand Up @@ -873,7 +873,6 @@ where
self.update_head(head)?;
self.update_finalized_block(update.finalized_block_hash)?;
self.update_safe_block(update.safe_block_hash)?;

Ok(())
}

Expand All @@ -899,9 +898,7 @@ where

head_block.total_difficulty =
self.blockchain.header_td_by_number(head_block.number)?.ok_or_else(|| {
RethError::Provider(ProviderError::TotalDifficultyNotFound {
block_number: head_block.number,
})
RethError::Provider(ProviderError::TotalDifficultyNotFound(head_block.number))
})?;
self.sync_state_updater.update_status(head_block);

Expand Down Expand Up @@ -1562,9 +1559,9 @@ where
let elapsed = self.record_make_canonical_latency(start, &make_canonical_result);
match make_canonical_result {
Ok(outcome) => {
if let CanonicalOutcome::Committed { ref head } = outcome {
if let CanonicalOutcome::Committed { head } = &outcome {
self.listeners.notify(BeaconConsensusEngineEvent::CanonicalChainCommitted(
head.clone(),
Box::new(head.clone()),
elapsed,
));
}
Expand Down Expand Up @@ -1661,7 +1658,7 @@ where
warn!(target: "consensus::engine", invalid_hash=?bad_block.hash, invalid_number=?bad_block.number, "Bad block detected in unwind");

// update the `invalid_headers` cache with the new invalid headers
self.invalid_headers.insert(bad_block);
self.invalid_headers.insert(*bad_block);
return None
}

Expand Down
57 changes: 28 additions & 29 deletions crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use reth_primitives::{
eip4844::{DATA_GAS_PER_BLOB, MAX_DATA_GAS_PER_BLOCK},
},
eip4844::calculate_excess_blob_gas,
BlockNumber, ChainSpec, Hardfork, Header, InvalidTransactionError, SealedBlock, SealedHeader,
Transaction, TransactionSignedEcRecovered, TxEip1559, TxEip2930, TxEip4844, TxLegacy,
BlockNumber, ChainSpec, GotExpected, Hardfork, Header, InvalidTransactionError, SealedBlock,
SealedHeader, Transaction, TransactionSignedEcRecovered, TxEip1559, TxEip2930, TxEip4844,
TxLegacy,
};
use reth_provider::{AccountReader, HeaderProvider, WithdrawalsProvider};
use std::collections::{hash_map::Entry, HashMap};
Expand Down Expand Up @@ -203,20 +204,18 @@ pub fn validate_block_standalone(
// Check ommers hash
let ommers_hash = reth_primitives::proofs::calculate_ommers_root(&block.ommers);
if block.header.ommers_hash != ommers_hash {
return Err(ConsensusError::BodyOmmersHashDiff {
got: ommers_hash,
expected: block.header.ommers_hash,
})
return Err(ConsensusError::BodyOmmersHashDiff(
GotExpected { got: ommers_hash, expected: block.header.ommers_hash }.into(),
))
}

// Check transaction root
// TODO(onbjerg): This should probably be accessible directly on [Block]
let transaction_root = reth_primitives::proofs::calculate_transaction_root(&block.body);
if block.header.transactions_root != transaction_root {
return Err(ConsensusError::BodyTransactionRootDiff {
got: transaction_root,
expected: block.header.transactions_root,
})
return Err(ConsensusError::BodyTransactionRootDiff(
GotExpected { got: transaction_root, expected: block.header.transactions_root }.into(),
))
}

// EIP-4895: Beacon chain push withdrawals as operations
Expand All @@ -227,10 +226,9 @@ pub fn validate_block_standalone(
let header_withdrawals_root =
block.withdrawals_root.as_ref().ok_or(ConsensusError::WithdrawalsRootMissing)?;
if withdrawals_root != *header_withdrawals_root {
return Err(ConsensusError::BodyWithdrawalsRootDiff {
got: withdrawals_root,
expected: *header_withdrawals_root,
})
return Err(ConsensusError::BodyWithdrawalsRootDiff(
GotExpected { got: withdrawals_root, expected: *header_withdrawals_root }.into(),
))
}
}

Expand All @@ -241,10 +239,10 @@ pub fn validate_block_standalone(
let header_blob_gas_used = block.blob_gas_used.ok_or(ConsensusError::BlobGasUsedMissing)?;
let total_blob_gas = block.blob_gas_used();
if total_blob_gas != header_blob_gas_used {
return Err(ConsensusError::BlobGasUsedDiff {
header_blob_gas_used,
expected_blob_gas_used: total_blob_gas,
})
return Err(ConsensusError::BlobGasUsedDiff(GotExpected {
got: header_blob_gas_used,
expected: total_blob_gas,
}))
}
}

Expand Down Expand Up @@ -300,10 +298,9 @@ pub fn validate_header_regarding_parent(
}

if parent.hash != child.parent_hash {
return Err(ConsensusError::ParentHashMismatch {
expected_parent_hash: parent.hash,
got_parent_hash: child.parent_hash,
})
return Err(ConsensusError::ParentHashMismatch(
GotExpected { got: child.parent_hash, expected: parent.hash }.into(),
))
}

// timestamp in past check
Expand Down Expand Up @@ -343,7 +340,10 @@ pub fn validate_header_regarding_parent(
.ok_or(ConsensusError::BaseFeeMissing)?
};
if expected_base_fee != base_fee {
return Err(ConsensusError::BaseFeeDiff { expected: expected_base_fee, got: base_fee })
return Err(ConsensusError::BaseFeeDiff(GotExpected {
expected: expected_base_fee,
got: base_fee,
}))
}
}

Expand Down Expand Up @@ -435,8 +435,7 @@ pub fn validate_4844_header_with_parent(
calculate_excess_blob_gas(parent_excess_blob_gas, parent_blob_gas_used);
if expected_excess_blob_gas != excess_blob_gas {
return Err(ConsensusError::ExcessBlobGasDiff {
expected: expected_excess_blob_gas,
got: excess_blob_gas,
diff: GotExpected { got: excess_blob_gas, expected: expected_excess_blob_gas },
parent_excess_blob_gas,
parent_blob_gas_used,
})
Expand Down Expand Up @@ -843,10 +842,10 @@ mod tests {
// validate blob, it should fail blob gas used validation
assert_eq!(
validate_block_standalone(&block, &chain_spec),
Err(ConsensusError::BlobGasUsedDiff {
header_blob_gas_used: 1,
expected_blob_gas_used
})
Err(ConsensusError::BlobGasUsedDiff(GotExpected {
got: 1,
expected: expected_blob_gas_used
}))
);
}
}
81 changes: 21 additions & 60 deletions crates/interfaces/src/consensus.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use async_trait::async_trait;
use reth_primitives::{
BlockHash, BlockNumber, Header, InvalidTransactionError, SealedBlock, SealedHeader, B256, U256,
BlockHash, BlockNumber, GotExpected, GotExpectedBoxed, Header, InvalidTransactionError,
SealedBlock, SealedHeader, B256, U256,
};
use std::fmt::Debug;

Expand Down Expand Up @@ -78,7 +79,6 @@ pub trait Consensus: Debug + Send + Sync {
}

/// Consensus Errors
#[allow(missing_docs)]
#[derive(thiserror::Error, Debug, PartialEq, Eq, Clone)]
pub enum ConsensusError {
/// Error when the gas used in the header exceeds the gas limit.
Expand All @@ -91,42 +91,22 @@ pub enum ConsensusError {
},

/// Error when the hash of block ommer is different from the expected hash.
#[error("block ommer hash ({got}) is different from expected ({expected})")]
BodyOmmersHashDiff {
/// The actual ommer hash.
got: B256,
/// The expected ommer hash.
expected: B256,
},
#[error("mismatched block ommer hash: {0}")]
BodyOmmersHashDiff(GotExpectedBoxed<B256>),

/// Error when the state root in the block is different from the expected state root.
#[error("block state root ({got}) is different from expected ({expected})")]
BodyStateRootDiff {
/// The actual state root.
got: B256,
/// The expected state root.
expected: B256,
},
#[error("mismatched block state root: {0}")]
BodyStateRootDiff(GotExpectedBoxed<B256>),

/// Error when the transaction root in the block is different from the expected transaction
/// root.
#[error("block transaction root ({got}) is different from expected ({expected})")]
BodyTransactionRootDiff {
/// The actual transaction root.
got: B256,
/// The expected transaction root.
expected: B256,
},
#[error("mismatched block transaction root: {0}")]
BodyTransactionRootDiff(GotExpectedBoxed<B256>),

/// Error when the withdrawals root in the block is different from the expected withdrawals
/// root.
#[error("block withdrawals root ({got}) is different from expected ({expected})")]
BodyWithdrawalsRootDiff {
/// The actual withdrawals root.
got: B256,
/// The expected withdrawals root.
expected: B256,
},
#[error("mismatched block withdrawals root: {0}")]
BodyWithdrawalsRootDiff(GotExpectedBoxed<B256>),

/// Error when a block with a specific hash and number is already known.
#[error("block with [hash={hash}, number={number}] is already known")]
Expand Down Expand Up @@ -156,13 +136,8 @@ pub enum ConsensusError {
},

/// Error when the parent hash does not match the expected parent hash.
#[error("parent hash {got_parent_hash} does not match the expected {expected_parent_hash}")]
ParentHashMismatch {
/// The expected parent hash.
expected_parent_hash: B256,
/// The actual parent hash.
got_parent_hash: B256,
},
#[error("mismatched parent hash: {0}")]
ParentHashMismatch(GotExpectedBoxed<B256>),

/// Error when the block timestamp is in the past compared to the parent timestamp.
#[error("block timestamp {timestamp} is in the past compared to the parent timestamp {parent_timestamp}")]
Expand Down Expand Up @@ -205,13 +180,8 @@ pub enum ConsensusError {
BaseFeeMissing,

/// Error when the block's base fee is different from the expected base fee.
#[error("block base fee ({got}) is different than expected: ({expected})")]
BaseFeeDiff {
/// The expected base fee.
expected: u64,
/// The actual base fee.
got: u64,
},
#[error("block base fee mismatch: {0}")]
BaseFeeDiff(GotExpected<u64>),

/// Error when there is a transaction signer recovery error.
#[error("transaction signer recovery error")]
Expand Down Expand Up @@ -293,27 +263,18 @@ pub enum ConsensusError {
},

/// Error when the blob gas used in the header does not match the expected blob gas used.
#[error(
"blob gas used in the header {header_blob_gas_used} \
does not match the expected blob gas used {expected_blob_gas_used}"
)]
BlobGasUsedDiff {
/// The blob gas used in the header.
header_blob_gas_used: u64,
/// The expected blob gas used.
expected_blob_gas_used: u64,
},
#[error("blob gas used mismatch: {0}")]
BlobGasUsedDiff(GotExpected<u64>),

/// Error when there is an invalid excess blob gas.
#[error(
"invalid excess blob gas. Expected: {expected}, got: {got}. \
Parent excess blob gas: {parent_excess_blob_gas}, parent blob gas used: {parent_blob_gas_used}"
"invalid excess blob gas: {diff}; \
parent excess blob gas: {parent_excess_blob_gas}, \
parent blob gas used: {parent_blob_gas_used}"
)]
ExcessBlobGasDiff {
/// The expected excess blob gas.
expected: u64,
/// The actual excess blob gas.
got: u64,
/// The excess blob gas diff.
diff: GotExpected<u64>,
/// The parent excess blob gas.
parent_excess_blob_gas: u64,
/// The parent blob gas used.
Expand Down
Loading

0 comments on commit f8ceda9

Please sign in to comment.