Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
fix: use prague during payload building (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg authored Sep 18, 2024
1 parent 1610d9b commit c6daac0
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 202 deletions.
404 changes: 232 additions & 172 deletions Cargo.lock

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,35 @@ alloy-signer-local = { version = "0.3", features = ["mnemonic"] }
tokio = { version = "1.21", default-features = false }

# reth
reth = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7" }
reth-chainspec = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7", features = [
reth = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e" }
reth-chainspec = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e", features = [
"optimism",
] }
reth-cli = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7" }
reth-cli-util = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7" }
reth-node-api = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7" }
reth-node-builder = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7" }
reth-node-core = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7", features = [
reth-cli = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e" }
reth-cli-util = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e" }
reth-node-api = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e" }
reth-node-builder = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e" }
reth-node-core = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e", features = [
"optimism",
] }
reth-node-optimism = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7", features = [
reth-node-optimism = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e", features = [
"optimism",
] }
reth-optimism-cli = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7", features = [
reth-optimism-cli = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e", features = [
"optimism",
] }
reth-optimism-rpc = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7", features = [
reth-optimism-rpc = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e", features = [
"optimism",
] }
reth-primitives = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7", features = [
reth-payload-builder = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e" }
reth-primitives = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e", features = [
"optimism",
] }
reth-revm = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7", features = [
reth-revm = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e", features = [
"optimism",
] }
reth-tracing = { git = "https://github.com/paradigmxyz/reth.git", rev = "ef1d9e7" }
reth-tracing = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e" }
reth-transaction-pool = { git = "https://github.com/paradigmxyz/reth.git", rev = "5659d19e1c1e" }

# misc
clap = "4"
Expand Down
2 changes: 1 addition & 1 deletion bin/alphanet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ reth-optimism-rpc.workspace = true
clap = { workspace = true, features = ["derive"] }

[target.'cfg(not(windows))'.dependencies]
tikv-jemallocator = { version = "0.5", optional = true }
tikv-jemallocator = { version = "0.6", optional = true }

[features]
default = ["jemalloc"]
Expand Down
4 changes: 2 additions & 2 deletions bin/alphanet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use alphanet_node::{chainspec::AlphanetChainSpecParser, node::AlphaNetNode};
use clap::Parser;
use reth_node_optimism::args::RollupArgs;
use reth_optimism_cli::Cli;
use reth_optimism_rpc::eth::rpc::SequencerClient;
use reth_optimism_rpc::sequencer::SequencerClient;

// We use jemalloc for performance reasons.
#[cfg(all(feature = "jemalloc", unix))]
Expand All @@ -53,7 +53,7 @@ fn main() {
if let Some(sequencer_http) = rollup_args.sequencer_http.clone() {
ctx.registry
.eth_api()
.set_sequencer_client(SequencerClient::new(sequencer_http));
.set_sequencer_client(SequencerClient::new(sequencer_http))?;
}

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions crates/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ reth-node-builder.workspace = true
reth-node-core.workspace = true
reth-node-optimism.workspace = true
reth-chainspec.workspace = true
reth-payload-builder.workspace = true
reth-primitives.workspace = true
reth-revm.workspace = true
reth-transaction-pool.workspace = true
serde_json.workspace = true

eyre.workspace = true
Expand Down
17 changes: 13 additions & 4 deletions crates/node/src/chainspec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use once_cell::sync::Lazy;
use reth_chainspec::{
BaseFeeParams, BaseFeeParamsKind, Chain, ChainHardforks, ChainSpec, EthereumHardfork,
ForkCondition, OptimismHardfork,
once_cell_set, BaseFeeParams, BaseFeeParamsKind, Chain, ChainHardforks, ChainSpec,
EthereumHardfork, ForkCondition, OptimismHardfork,
};
use reth_cli::chainspec::ChainSpecParser;
use reth_node_core::args::utils::parse_custom_chain_spec;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub static ALPHANET_DEV: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
chain: Chain::dev(),
genesis: serde_json::from_str(include_str!("../../../etc/alphanet-genesis.json"))
.expect("Can't deserialize alphanet genesis json"),
genesis_hash: Some(b256!(
genesis_hash: once_cell_set(b256!(
"2f980576711e3617a5e4d83dd539548ec0f7792007d505a3d2e9674833af2d7c"
)),
paris_block_and_final_difficulty: Some((0, U256::from(0))),
Expand All @@ -64,7 +64,7 @@ pub static ALPHANET_MAINNET: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
// manually from trusted source
genesis: serde_json::from_str(include_str!("../../../etc/alphanet-genesis.json"))
.expect("Can't deserialize alphanet genesis json"),
genesis_hash: Some(b256!(
genesis_hash: once_cell_set(b256!(
"2f980576711e3617a5e4d83dd539548ec0f7792007d505a3d2e9674833af2d7c"
)),
paris_block_and_final_difficulty: Some((0, U256::from(0))),
Expand Down Expand Up @@ -104,6 +104,15 @@ impl ChainSpecParser for AlphanetChainSpecParser {
// a good way to do it.
chainspec.hardforks.insert(EthereumHardfork::Prague, ForkCondition::Timestamp(0));

// NOTE(onbjerg): op-node will fetch the genesis block and check that the hash
// matches whatever is in the L2 rollup config, which it will not when we activate
// Prague, since the autogenerated genesis header will include a requests root of
// `EMPTY_ROOT`. To circumvent this without modifying the OP stack genesis
// generator, we simply remove the requests root manually here.
let mut header = chainspec.genesis_header().clone();
header.requests_root = None;
chainspec.genesis_header = once_cell_set(header);

Arc::new(chainspec)
}
})
Expand Down
93 changes: 90 additions & 3 deletions crates/node/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use alphanet_precompile::secp256r1;
use reth_chainspec::{ChainSpec, EthereumHardfork, Head, OptimismHardfork};
use reth_node_api::{ConfigureEvm, ConfigureEvmEnv};
use reth_node_api::{ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes};
use reth_primitives::{
revm_primitives::{CfgEnvWithHandlerCfg, TxEnv},
transaction::FillTxEnv,
Expand All @@ -22,7 +22,10 @@ use reth_revm::{
handler::register::EvmHandler,
inspector_handle_register,
precompile::PrecompileSpecId,
primitives::{AnalysisKind, Env, OptimismFields},
primitives::{
AnalysisKind, BlobExcessGasAndPrice, BlockEnv, CfgEnv, Env, HandlerCfg, OptimismFields,
SpecId,
},
ContextPrecompiles, Database, Evm, EvmBuilder, GetInspector,
};
use std::sync::Arc;
Expand Down Expand Up @@ -65,6 +68,8 @@ impl AlphaNetEvmConfig {
}

impl ConfigureEvmEnv for AlphaNetEvmConfig {
type Header = Header;

fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
transaction.fill_tx_env(tx_env, sender);
}
Expand Down Expand Up @@ -135,7 +140,89 @@ impl ConfigureEvmEnv for AlphaNetEvmConfig {
cfg_env.perf_analyse_created_bytecodes = AnalysisKind::Analyse;

cfg_env.handler_cfg.spec_id = spec_id;
cfg_env.handler_cfg.is_optimism = self.chain_spec.is_optimism();
cfg_env.handler_cfg.is_optimism = true;
}

fn fill_block_env(&self, block_env: &mut BlockEnv, header: &Self::Header, after_merge: bool) {
block_env.number = U256::from(header.number);
block_env.coinbase = header.beneficiary;
block_env.timestamp = U256::from(header.timestamp);
if after_merge {
block_env.prevrandao = Some(header.mix_hash);
block_env.difficulty = U256::ZERO;
} else {
block_env.difficulty = header.difficulty;
block_env.prevrandao = None;
}
block_env.basefee = U256::from(header.base_fee_per_gas.unwrap_or_default());
block_env.gas_limit = U256::from(header.gas_limit);

// EIP-4844 excess blob gas of this block, introduced in Cancun
if let Some(excess_blob_gas) = header.excess_blob_gas {
block_env.set_blob_excess_gas_and_price(excess_blob_gas);
}
}

fn next_cfg_and_block_env(
&self,
parent: &Self::Header,
attributes: NextBlockEnvAttributes,
) -> (CfgEnvWithHandlerCfg, BlockEnv) {
// configure evm env based on parent block
let cfg = CfgEnv::default().with_chain_id(self.chain_spec.chain().id());

// ensure we're not missing any timestamp based hardforks
let spec_id = revm_spec(
&self.chain_spec,
&Head {
number: parent.number + 1,
timestamp: attributes.timestamp,
..Default::default()
},
);

// if the parent block did not have excess blob gas (i.e. it was pre-cancun), but it is
// cancun now, we need to set the excess blob gas to the default value
let blob_excess_gas_and_price = parent
.next_block_excess_blob_gas()
.or_else(|| {
if spec_id.is_enabled_in(SpecId::CANCUN) {
// default excess blob gas is zero
Some(0)
} else {
None
}
})
.map(BlobExcessGasAndPrice::new);

let block_env = BlockEnv {
number: U256::from(parent.number + 1),
coinbase: attributes.suggested_fee_recipient,
timestamp: U256::from(attributes.timestamp),
difficulty: U256::ZERO,
prevrandao: Some(attributes.prev_randao),
gas_limit: U256::from(parent.gas_limit),
// calculate basefee based on parent block's gas usage
basefee: U256::from(
parent
.next_block_base_fee(
self.chain_spec.base_fee_params_at_timestamp(attributes.timestamp),
)
.unwrap_or_default(),
),
// calculate excess gas based on parent block's blob gas usage
blob_excess_gas_and_price,
};

let cfg_with_handler_cfg;
{
cfg_with_handler_cfg = CfgEnvWithHandlerCfg {
cfg_env: cfg,
handler_cfg: HandlerCfg { spec_id, is_optimism: true },
};
}

(cfg_with_handler_cfg, block_env)
}
}

Expand Down
43 changes: 39 additions & 4 deletions crates/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::evm::AlphaNetEvmConfig;
use reth_chainspec::ChainSpec;
use reth_node_api::{FullNodeTypes, NodeTypesWithEngine};
use reth_node_builder::{
components::{ComponentsBuilder, ExecutorBuilder},
components::{ComponentsBuilder, ExecutorBuilder, PayloadServiceBuilder},
BuilderContext, Node, NodeTypes,
};
use reth_node_optimism::{
Expand All @@ -18,6 +18,8 @@ use reth_node_optimism::{
},
OpExecutorProvider, OptimismEngineTypes,
};
use reth_payload_builder::PayloadBuilderHandle;
use reth_transaction_pool::TransactionPool;

/// Type configuration for a regular AlphaNet node.
#[derive(Debug, Clone, Default)]
Expand All @@ -38,7 +40,7 @@ impl AlphaNetNode {
) -> ComponentsBuilder<
Node,
OptimismPoolBuilder,
OptimismPayloadBuilder,
AlphaNetPayloadBuilder,
OptimismNetworkBuilder,
AlphaNetExecutorBuilder,
OptimismConsensusBuilder,
Expand All @@ -52,7 +54,7 @@ impl AlphaNetNode {
ComponentsBuilder::default()
.node_types::<Node>()
.pool(OptimismPoolBuilder::default())
.payload(OptimismPayloadBuilder::new(compute_pending_block))
.payload(AlphaNetPayloadBuilder::new(compute_pending_block))
.network(OptimismNetworkBuilder {
disable_txpool_gossip,
disable_discovery_v4: !discovery_v4,
Expand Down Expand Up @@ -81,7 +83,7 @@ where
type ComponentsBuilder = ComponentsBuilder<
N,
OptimismPoolBuilder,
OptimismPayloadBuilder,
AlphaNetPayloadBuilder,
OptimismNetworkBuilder,
AlphaNetExecutorBuilder,
OptimismConsensusBuilder,
Expand Down Expand Up @@ -118,3 +120,36 @@ where
Ok((evm_config, executor))
}
}

/// The AlphaNet payload service builder.
///
/// This service wraps the default Optimism payload builder, but replaces the default evm config
/// with AlphaNet's own.
#[derive(Debug, Default, Clone)]
pub struct AlphaNetPayloadBuilder {
/// Inner Optimism payload builder service.
inner: OptimismPayloadBuilder,
}

impl AlphaNetPayloadBuilder {
/// Create a new instance with the given `compute_pending_block` flag.
pub const fn new(compute_pending_block: bool) -> Self {
Self { inner: OptimismPayloadBuilder::new(compute_pending_block) }
}
}

impl<Node, Pool> PayloadServiceBuilder<Node, Pool> for AlphaNetPayloadBuilder
where
Node: FullNodeTypes<
Types: NodeTypesWithEngine<Engine = OptimismEngineTypes, ChainSpec = ChainSpec>,
>,
Pool: TransactionPool + Unpin + 'static,
{
async fn spawn_payload_service(
self,
ctx: &BuilderContext<Node>,
pool: Pool,
) -> eyre::Result<PayloadBuilderHandle<OptimismEngineTypes>> {
self.inner.spawn(AlphaNetEvmConfig::new(ctx.chain_spec().clone()), ctx, pool)
}
}
19 changes: 16 additions & 3 deletions crates/precompile/src/secp256r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
//! The precompile can be inserted in a custom EVM like this:
//! ```
//! use alphanet_precompile::secp256r1;
//! use reth_node_api::{ConfigureEvm, ConfigureEvmEnv};
//! use reth_primitives::{TransactionSigned, U256};
//! use reth_node_api::{ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes};
//! use reth_primitives::{Header, TransactionSigned, U256};
//! use reth_revm::{
//! precompile::{PrecompileSpecId, Precompiles},
//! primitives::{Address, Bytes, CfgEnvWithHandlerCfg, Env, TxEnv},
//! primitives::{Address, BlockEnv, Bytes, CfgEnvWithHandlerCfg, Env, TxEnv},
//! ContextPrecompiles, Database, Evm, EvmBuilder,
//! };
//! use std::sync::Arc;
Expand Down Expand Up @@ -46,6 +46,8 @@
//! }
//!
//! impl ConfigureEvmEnv for AlphaNetEvmConfig {
//! type Header = Header;
//!
//! fn fill_tx_env(
//! &self,
//! tx_env: &mut TxEnv,
Expand All @@ -54,10 +56,21 @@
//! ) {
//! todo!()
//! }
//!
//! fn fill_cfg_env(&self, _: &mut CfgEnvWithHandlerCfg, _: &reth_primitives::Header, _: U256) {
//! todo!()
//! }
//!
//! fn fill_block_env(&self, _: &mut BlockEnv, _: &Self::Header, _: bool) {}
//!
//! fn next_cfg_and_block_env(
//! &self,
//! _: &Self::Header,
//! _: NextBlockEnvAttributes,
//! ) -> (CfgEnvWithHandlerCfg, BlockEnv) {
//! todo!()
//! }
//!
//! fn fill_tx_env_system_contract_call(&self, _: &mut Env, _: Address, _: Address, _: Bytes) {
//! todo!()
//! }
Expand Down

0 comments on commit c6daac0

Please sign in to comment.