Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into alexey/blockchain-pro…
Browse files Browse the repository at this point in the history
…vider-changeset-reader
  • Loading branch information
shekhirin committed Aug 21, 2024
2 parents 32a0bbb + ebb2d55 commit 61dbbcd
Show file tree
Hide file tree
Showing 32 changed files with 1,036 additions and 306 deletions.
188 changes: 124 additions & 64 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ bytes = "1.5"
clap = "4"
const_format = { version = "0.2.32", features = ["rust_1_64"] }
dashmap = "6.0"
derive_more = "0.99.17"
derive_more = { version = "1", features = ["full"] }
dyn-clone = "1.0.17"
eyre = "0.6"
fdlimit = "0.3.0"
Expand Down
4 changes: 2 additions & 2 deletions crates/chain-state/src/chain_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ impl ChainInfoTracker {
}

/// Subscribe to the finalized block.
pub fn subscribe_to_finalized_block(&self) -> watch::Receiver<Option<SealedHeader>> {
pub fn subscribe_finalized_block(&self) -> watch::Receiver<Option<SealedHeader>> {
self.inner.finalized_block.subscribe()
}

/// Subscribe to the safe block.
pub fn subscribe_to_safe_block(&self) -> watch::Receiver<Option<SealedHeader>> {
pub fn subscribe_safe_block(&self) -> watch::Receiver<Option<SealedHeader>> {
self.inner.safe_block.subscribe()
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/chain-state/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ impl InMemoryState {

/// Returns the pending state corresponding to the current head plus one,
/// from the payload received in newPayload that does not have a FCU yet.
pub(crate) fn pending_state(&self) -> Option<Arc<BlockState>> {
self.pending.borrow().as_ref().map(|state| Arc::new(state.clone()))
pub(crate) fn pending_state(&self) -> Option<BlockState> {
self.pending.borrow().clone()
}

#[cfg(test)]
Expand Down Expand Up @@ -347,7 +347,7 @@ impl CanonicalInMemoryState {
}

/// Returns the in memory pending state.
pub fn pending_state(&self) -> Option<Arc<BlockState>> {
pub fn pending_state(&self) -> Option<BlockState> {
self.inner.in_memory_state.pending_state()
}

Expand Down Expand Up @@ -465,12 +465,12 @@ impl CanonicalInMemoryState {

/// Subscribe to new safe block events.
pub fn subscribe_safe_block(&self) -> watch::Receiver<Option<SealedHeader>> {
self.inner.chain_info_tracker.subscribe_to_safe_block()
self.inner.chain_info_tracker.subscribe_safe_block()
}

/// Subscribe to new finalized block events.
pub fn subscribe_finalized_block(&self) -> watch::Receiver<Option<SealedHeader>> {
self.inner.chain_info_tracker.subscribe_to_finalized_block()
self.inner.chain_info_tracker.subscribe_finalized_block()
}

/// Attempts to send a new [`CanonStateNotification`] to all active Receiver handles.
Expand Down Expand Up @@ -1131,7 +1131,7 @@ mod tests {
// Check the pending state
assert_eq!(
state.pending_state().unwrap(),
Arc::new(BlockState::with_parent(block2.clone(), Some(BlockState::new(block1))))
BlockState::with_parent(block2.clone(), Some(BlockState::new(block1)))
);

// Check the pending block
Expand Down
13 changes: 9 additions & 4 deletions crates/chain-state/src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,19 @@ pub struct ForkChoiceNotifications(pub watch::Receiver<Option<SealedHeader>>);
/// and get notified when a new fork choice is available.
pub trait ForkChoiceSubscriptions: Send + Sync {
/// Get notified when a new safe block of the chain is selected.
fn subscribe_to_safe_block(&self) -> ForkChoiceNotifications;
fn subscribe_safe_block(&self) -> ForkChoiceNotifications;

/// Get notified when a new finalized block of the chain is selected.
fn subscribe_to_finalized_block(&self) -> ForkChoiceNotifications;
fn subscribe_finalized_block(&self) -> ForkChoiceNotifications;

/// Convenience method to get a stream of the new safe blocks of the chain.
fn fork_choice_stream(&self) -> ForkChoiceStream {
ForkChoiceStream { st: WatchStream::new(self.subscribe_to_safe_block().0) }
fn safe_block_stream(&self) -> ForkChoiceStream {
ForkChoiceStream { st: WatchStream::new(self.subscribe_safe_block().0) }
}

/// Convenience method to get a stream of the new finalized blocks of the chain.
fn finalized_block_stream(&self) -> ForkChoiceStream {
ForkChoiceStream { st: WatchStream::new(self.subscribe_finalized_block().0) }
}
}

Expand Down
92 changes: 46 additions & 46 deletions crates/consensus/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub trait Consensus: Debug + Send + Sync {
#[derive(Debug, PartialEq, Eq, Clone, derive_more::Display)]
pub enum ConsensusError {
/// Error when the gas used in the header exceeds the gas limit.
#[display(fmt = "block used gas ({gas_used}) is greater than gas limit ({gas_limit})")]
#[display("block used gas ({gas_used}) is greater than gas limit ({gas_limit})")]
HeaderGasUsedExceedsGasLimit {
/// The gas used in the block header.
gas_used: u64,
Expand All @@ -140,7 +140,7 @@ pub enum ConsensusError {

/// Error when block gas used doesn't match expected value
#[display(
fmt = "block gas used mismatch: {gas}; gas spent by each transaction: {gas_spent_by_tx:?}"
"block gas used mismatch: {gas}; gas spent by each transaction: {gas_spent_by_tx:?}"
)]
BlockGasUsed {
/// The gas diff.
Expand All @@ -150,38 +150,38 @@ pub enum ConsensusError {
},

/// Error when the hash of block ommer is different from the expected hash.
#[display(fmt = "mismatched block ommer hash: {_0}")]
#[display("mismatched block ommer hash: {_0}")]
BodyOmmersHashDiff(GotExpectedBoxed<B256>),

/// Error when the state root in the block is different from the expected state root.
#[display(fmt = "mismatched block state root: {_0}")]
#[display("mismatched block state root: {_0}")]
BodyStateRootDiff(GotExpectedBoxed<B256>),

/// Error when the transaction root in the block is different from the expected transaction
/// root.
#[display(fmt = "mismatched block transaction root: {_0}")]
#[display("mismatched block transaction root: {_0}")]
BodyTransactionRootDiff(GotExpectedBoxed<B256>),

/// Error when the receipt root in the block is different from the expected receipt root.
#[display(fmt = "receipt root mismatch: {_0}")]
#[display("receipt root mismatch: {_0}")]
BodyReceiptRootDiff(GotExpectedBoxed<B256>),

/// Error when header bloom filter is different from the expected bloom filter.
#[display(fmt = "header bloom filter mismatch: {_0}")]
#[display("header bloom filter mismatch: {_0}")]
BodyBloomLogDiff(GotExpectedBoxed<Bloom>),

/// Error when the withdrawals root in the block is different from the expected withdrawals
/// root.
#[display(fmt = "mismatched block withdrawals root: {_0}")]
#[display("mismatched block withdrawals root: {_0}")]
BodyWithdrawalsRootDiff(GotExpectedBoxed<B256>),

/// Error when the requests root in the block is different from the expected requests
/// root.
#[display(fmt = "mismatched block requests root: {_0}")]
#[display("mismatched block requests root: {_0}")]
BodyRequestsRootDiff(GotExpectedBoxed<B256>),

/// Error when a block with a specific hash and number is already known.
#[display(fmt = "block with [hash={hash}, number={number}] is already known")]
#[display("block with [hash={hash}, number={number}] is already known")]
BlockKnown {
/// The hash of the known block.
hash: BlockHash,
Expand All @@ -190,15 +190,15 @@ pub enum ConsensusError {
},

/// Error when the parent hash of a block is not known.
#[display(fmt = "block parent [hash={hash}] is not known")]
#[display("block parent [hash={hash}] is not known")]
ParentUnknown {
/// The hash of the unknown parent block.
hash: BlockHash,
},

/// Error when the block number does not match the parent block number.
#[display(
fmt = "block number {block_number} does not match parent block number {parent_block_number}"
"block number {block_number} does not match parent block number {parent_block_number}"
)]
ParentBlockNumberMismatch {
/// The parent block number.
Expand All @@ -208,12 +208,12 @@ pub enum ConsensusError {
},

/// Error when the parent hash does not match the expected parent hash.
#[display(fmt = "mismatched parent hash: {_0}")]
#[display("mismatched parent hash: {_0}")]
ParentHashMismatch(GotExpectedBoxed<B256>),

/// Error when the block timestamp is in the future compared to our clock time.
#[display(
fmt = "block timestamp {timestamp} is in the future compared to our clock time {present_timestamp}"
"block timestamp {timestamp} is in the future compared to our clock time {present_timestamp}"
)]
TimestampIsInFuture {
/// The block's timestamp.
Expand All @@ -223,84 +223,82 @@ pub enum ConsensusError {
},

/// Error when the base fee is missing.
#[display(fmt = "base fee missing")]
#[display("base fee missing")]
BaseFeeMissing,

/// Error when there is a transaction signer recovery error.
#[display(fmt = "transaction signer recovery error")]
#[display("transaction signer recovery error")]
TransactionSignerRecoveryError,

/// Error when the extra data length exceeds the maximum allowed.
#[display(fmt = "extra data {len} exceeds max length")]
#[display("extra data {len} exceeds max length")]
ExtraDataExceedsMax {
/// The length of the extra data.
len: usize,
},

/// Error when the difficulty after a merge is not zero.
#[display(fmt = "difficulty after merge is not zero")]
#[display("difficulty after merge is not zero")]
TheMergeDifficultyIsNotZero,

/// Error when the nonce after a merge is not zero.
#[display(fmt = "nonce after merge is not zero")]
#[display("nonce after merge is not zero")]
TheMergeNonceIsNotZero,

/// Error when the ommer root after a merge is not empty.
#[display(fmt = "ommer root after merge is not empty")]
#[display("ommer root after merge is not empty")]
TheMergeOmmerRootIsNotEmpty,

/// Error when the withdrawals root is missing.
#[display(fmt = "missing withdrawals root")]
#[display("missing withdrawals root")]
WithdrawalsRootMissing,

/// Error when the requests root is missing.
#[display(fmt = "missing requests root")]
#[display("missing requests root")]
RequestsRootMissing,

/// Error when an unexpected withdrawals root is encountered.
#[display(fmt = "unexpected withdrawals root")]
#[display("unexpected withdrawals root")]
WithdrawalsRootUnexpected,

/// Error when an unexpected requests root is encountered.
#[display(fmt = "unexpected requests root")]
#[display("unexpected requests root")]
RequestsRootUnexpected,

/// Error when withdrawals are missing.
#[display(fmt = "missing withdrawals")]
#[display("missing withdrawals")]
BodyWithdrawalsMissing,

/// Error when requests are missing.
#[display(fmt = "missing requests")]
#[display("missing requests")]
BodyRequestsMissing,

/// Error when blob gas used is missing.
#[display(fmt = "missing blob gas used")]
#[display("missing blob gas used")]
BlobGasUsedMissing,

/// Error when unexpected blob gas used is encountered.
#[display(fmt = "unexpected blob gas used")]
#[display("unexpected blob gas used")]
BlobGasUsedUnexpected,

/// Error when excess blob gas is missing.
#[display(fmt = "missing excess blob gas")]
#[display("missing excess blob gas")]
ExcessBlobGasMissing,

/// Error when unexpected excess blob gas is encountered.
#[display(fmt = "unexpected excess blob gas")]
#[display("unexpected excess blob gas")]
ExcessBlobGasUnexpected,

/// Error when the parent beacon block root is missing.
#[display(fmt = "missing parent beacon block root")]
#[display("missing parent beacon block root")]
ParentBeaconBlockRootMissing,

/// Error when an unexpected parent beacon block root is encountered.
#[display(fmt = "unexpected parent beacon block root")]
#[display("unexpected parent beacon block root")]
ParentBeaconBlockRootUnexpected,

/// Error when blob gas used exceeds the maximum allowed.
#[display(
fmt = "blob gas used {blob_gas_used} exceeds maximum allowance {max_blob_gas_per_block}"
)]
#[display("blob gas used {blob_gas_used} exceeds maximum allowance {max_blob_gas_per_block}")]
BlobGasUsedExceedsMaxBlobGasPerBlock {
/// The actual blob gas used.
blob_gas_used: u64,
Expand All @@ -310,7 +308,7 @@ pub enum ConsensusError {

/// Error when blob gas used is not a multiple of blob gas per blob.
#[display(
fmt = "blob gas used {blob_gas_used} is not a multiple of blob gas per blob {blob_gas_per_blob}"
"blob gas used {blob_gas_used} is not a multiple of blob gas per blob {blob_gas_per_blob}"
)]
BlobGasUsedNotMultipleOfBlobGasPerBlob {
/// The actual blob gas used.
Expand All @@ -321,7 +319,7 @@ pub enum ConsensusError {

/// Error when excess blob gas is not a multiple of blob gas per blob.
#[display(
fmt = "excess blob gas {excess_blob_gas} is not a multiple of blob gas per blob {blob_gas_per_blob}"
"excess blob gas {excess_blob_gas} is not a multiple of blob gas per blob {blob_gas_per_blob}"
)]
ExcessBlobGasNotMultipleOfBlobGasPerBlob {
/// The actual excess blob gas.
Expand All @@ -331,20 +329,22 @@ pub enum ConsensusError {
},

/// Error when the blob gas used in the header does not match the expected blob gas used.
#[display(fmt = "blob gas used mismatch: {_0}")]
#[display("blob gas used mismatch: {_0}")]
BlobGasUsedDiff(GotExpected<u64>),

/// Error for a transaction that violates consensus.
InvalidTransaction(InvalidTransactionError),

/// Error when the block's base fee is different from the expected base fee.
#[display(fmt = "block base fee mismatch: {_0}")]
#[display("block base fee mismatch: {_0}")]
BaseFeeDiff(GotExpected<u64>),

/// Error when there is an invalid excess blob gas.
#[display(fmt = "invalid excess blob gas: {diff}; \
#[display(
"invalid excess blob gas: {diff}; \
parent excess blob gas: {parent_excess_blob_gas}, \
parent blob gas used: {parent_blob_gas_used}")]
parent blob gas used: {parent_blob_gas_used}"
)]
ExcessBlobGasDiff {
/// The excess blob gas diff.
diff: GotExpected<u64>,
Expand All @@ -355,7 +355,7 @@ pub enum ConsensusError {
},

/// Error when the child gas limit exceeds the maximum allowed increase.
#[display(fmt = "child gas_limit {child_gas_limit} max increase is {parent_gas_limit}/1024")]
#[display("child gas_limit {child_gas_limit} max increase is {parent_gas_limit}/1024")]
GasLimitInvalidIncrease {
/// The parent gas limit.
parent_gas_limit: u64,
Expand All @@ -367,15 +367,15 @@ pub enum ConsensusError {
///
/// This error occurs when the child gas limit is less than the specified minimum gas limit.
#[display(
fmt = "child gas limit {child_gas_limit} is below the minimum allowed limit ({MINIMUM_GAS_LIMIT})"
"child gas limit {child_gas_limit} is below the minimum allowed limit ({MINIMUM_GAS_LIMIT})"
)]
GasLimitInvalidMinimum {
/// The child gas limit.
child_gas_limit: u64,
},

/// Error when the child gas limit exceeds the maximum allowed decrease.
#[display(fmt = "child gas_limit {child_gas_limit} max decrease is {parent_gas_limit}/1024")]
#[display("child gas_limit {child_gas_limit} max decrease is {parent_gas_limit}/1024")]
GasLimitInvalidDecrease {
/// The parent gas limit.
parent_gas_limit: u64,
Expand All @@ -385,7 +385,7 @@ pub enum ConsensusError {

/// Error when the block timestamp is in the past compared to the parent timestamp.
#[display(
fmt = "block timestamp {timestamp} is in the past compared to the parent timestamp {parent_timestamp}"
"block timestamp {timestamp} is in the past compared to the parent timestamp {parent_timestamp}"
)]
TimestampIsInPast {
/// The parent block's timestamp.
Expand Down Expand Up @@ -420,7 +420,7 @@ impl From<InvalidTransactionError> for ConsensusError {

/// `HeaderConsensusError` combines a `ConsensusError` with the `SealedHeader` it relates to.
#[derive(derive_more::Display, Debug)]
#[display(fmt = "Consensus error: {_0}, Invalid header: {_1:?}")]
#[display("Consensus error: {_0}, Invalid header: {_1:?}")]
pub struct HeaderConsensusError(ConsensusError, SealedHeader);

#[cfg(feature = "std")]
Expand Down
Loading

0 comments on commit 61dbbcd

Please sign in to comment.