Skip to content

Commit

Permalink
feat(no-std): add no_std support for reth-optimism-consensus (#13692
Browse files Browse the repository at this point in the history
)
  • Loading branch information
emhane authored Jan 7, 2025
1 parent fb69028 commit 93667e0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
15 changes: 15 additions & 0 deletions crates/optimism/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,19 @@ op-alloy-consensus.workspace = true
reth-optimism-chainspec.workspace = true

[features]
default = ["std"]
std = [
"reth-chainspec/std",
"reth-consensus/std",
"reth-consensus-common/std",
"reth-primitives/std",
"reth-optimism-forks/std",
"reth-optimism-chainspec/std",
"reth-optimism-primitives/std",
"alloy-eips/std",
"alloy-primitives/std",
"alloy-consensus/std",
"alloy-trie/std",
"op-alloy-consensus/std",
]
optimism = ["reth-primitives/optimism", "reth-optimism-primitives/optimism"]
57 changes: 25 additions & 32 deletions crates/optimism/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]
// The `optimism` feature must be enabled to use this crate.
#![cfg(feature = "optimism")]

extern crate alloc;

use alloc::sync::Arc;
use alloy_consensus::{BlockHeader, Header, EMPTY_OMMER_ROOT_HASH};
use alloy_eips::eip7840::BlobParams;
use alloy_primitives::{B64, U256};
Expand All @@ -26,7 +30,6 @@ use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_forks::OpHardforks;
use reth_optimism_primitives::{OpBlock, OpBlockBody, OpPrimitives, OpReceipt};
use reth_primitives::{BlockWithSenders, GotExpected, SealedBlockFor, SealedHeader};
use std::{sync::Arc, time::SystemTime};

mod proof;
pub use proof::calculate_receipt_root_no_memo_optimism;
Expand Down Expand Up @@ -157,42 +160,32 @@ impl HeaderValidator for OpBeaconConsensus {
_total_difficulty: U256,
) -> Result<(), ConsensusError> {
// with OP-stack Bedrock activation number determines when TTD (eth Merge) has been reached.
let is_post_merge = self.chain_spec.is_bedrock_active_at_block(header.number);
debug_assert!(
self.chain_spec.is_bedrock_active_at_block(header.number),
"manually import OVM blocks"
);

if is_post_merge {
if header.nonce != B64::ZERO {
return Err(ConsensusError::TheMergeNonceIsNotZero)
}
if header.nonce != B64::ZERO {
return Err(ConsensusError::TheMergeNonceIsNotZero)
}

if header.ommers_hash != EMPTY_OMMER_ROOT_HASH {
return Err(ConsensusError::TheMergeOmmerRootIsNotEmpty)
}
if header.ommers_hash != EMPTY_OMMER_ROOT_HASH {
return Err(ConsensusError::TheMergeOmmerRootIsNotEmpty)
}

// Post-merge, the consensus layer is expected to perform checks such that the block
// timestamp is a function of the slot. This is different from pre-merge, where blocks
// are only allowed to be in the future (compared to the system's clock) by a certain
// threshold.
//
// Block validation with respect to the parent should ensure that the block timestamp
// is greater than its parent timestamp.
// Post-merge, the consensus layer is expected to perform checks such that the block
// timestamp is a function of the slot. This is different from pre-merge, where blocks
// are only allowed to be in the future (compared to the system's clock) by a certain
// threshold.
//
// Block validation with respect to the parent should ensure that the block timestamp
// is greater than its parent timestamp.

// validate header extra data for all networks post merge
validate_header_extra_data(header)?;
// validate header extra data for all networks post merge
validate_header_extra_data(header)?;

// mixHash is used instead of difficulty inside EVM
// https://eips.ethereum.org/EIPS/eip-4399#using-mixhash-field-instead-of-difficulty
} else {
// Check if timestamp is in the future. Clock can drift but this can be consensus issue.
let present_timestamp =
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();

if header.exceeds_allowed_future_timestamp(present_timestamp) {
return Err(ConsensusError::TimestampIsInFuture {
timestamp: header.timestamp,
present_timestamp,
})
}
}
// mixHash is used instead of difficulty inside EVM
// https://eips.ethereum.org/EIPS/eip-4399#using-mixhash-field-instead-of-difficulty

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions crates/optimism/consensus/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Helper function for Receipt root calculation for Optimism hardforks.
use alloc::vec::Vec;
use alloy_consensus::TxReceipt;
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::B256;
Expand Down
1 change: 1 addition & 0 deletions crates/optimism/consensus/src/validation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::proof::calculate_receipt_root_optimism;
use alloc::vec::Vec;
use alloy_consensus::TxReceipt;
use alloy_primitives::{Bloom, B256};
use reth_chainspec::{ChainSpec, EthereumHardforks};
Expand Down
3 changes: 2 additions & 1 deletion crates/optimism/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ std = [
"thiserror/std",
"op-alloy-consensus/std",
"reth-chainspec/std",
"reth-consensus-common/std"
"reth-optimism-consensus/std",
"reth-consensus-common/std",
]
optimism = [
"reth-primitives/optimism",
Expand Down

0 comments on commit 93667e0

Please sign in to comment.