Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eip1559 params to payload #11473

Closed
wants to merge 16 commits into from
Closed
376 changes: 183 additions & 193 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,10 @@ alloy-transport-ipc = { version = "0.4.2", default-features = false }
alloy-transport-ws = { version = "0.4.2", default-features = false }

# op
op-alloy-rpc-types = "0.3.2"
op-alloy-rpc-types-engine = "0.3.2"
op-alloy-network = "0.3.2"
op-alloy-consensus = "0.3.2"
op-alloy-rpc-types = "0.3.3"
op-alloy-rpc-types-engine = "0.3.3"
op-alloy-network = "0.3.3"
op-alloy-consensus = "0.3.3"

# misc
aquamarine = "0.5"
Expand Down
3 changes: 1 addition & 2 deletions crates/optimism/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ reth-prune-types.workspace = true

# ethereum
alloy-primitives.workspace = true
alloy-eips.workspace = true

# Optimism
reth-optimism-consensus.workspace = true
Expand All @@ -38,8 +39,6 @@ thiserror.workspace = true
tracing.workspace = true

[dev-dependencies]
alloy-eips.workspace = true

reth-revm = { workspace = true, features = ["test-utils"] }
reth-optimism-chainspec.workspace = true
alloy-genesis.workspace = true
Expand Down
8 changes: 6 additions & 2 deletions crates/optimism/evm/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub fn revm_spec_by_timestamp_after_bedrock(
chain_spec: &ChainSpec,
timestamp: u64,
) -> revm_primitives::SpecId {
if chain_spec.fork(OptimismHardfork::Granite).active_at_timestamp(timestamp) {
if chain_spec.fork(OptimismHardfork::Holocene).active_at_timestamp(timestamp) {
revm_primitives::HOLOCENE
} else if chain_spec.fork(OptimismHardfork::Granite).active_at_timestamp(timestamp) {
revm_primitives::GRANITE
} else if chain_spec.fork(OptimismHardfork::Fjord).active_at_timestamp(timestamp) {
revm_primitives::FJORD
Expand All @@ -29,7 +31,9 @@ pub fn revm_spec_by_timestamp_after_bedrock(

/// Map the latest active hardfork at the given block to a revm [`SpecId`](revm_primitives::SpecId).
pub fn revm_spec(chain_spec: &ChainSpec, block: &Head) -> revm_primitives::SpecId {
if chain_spec.fork(OptimismHardfork::Granite).active_at_head(block) {
if chain_spec.fork(OptimismHardfork::Holocene).active_at_head(block) {
revm_primitives::HOLOCENE
} else if chain_spec.fork(OptimismHardfork::Granite).active_at_head(block) {
revm_primitives::GRANITE
} else if chain_spec.fork(OptimismHardfork::Fjord).active_at_head(block) {
revm_primitives::FJORD
Expand Down
48 changes: 39 additions & 9 deletions crates/optimism/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
// The `optimism` feature must be enabled to use this crate.
#![cfg(feature = "optimism")]

use alloy_primitives::{Address, U256};
use alloy_eips::eip1559::BaseFeeParams;
use alloy_primitives::{Address, U256, B64};
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes};
use reth_optimism_chainspec::OpChainSpec;
use reth_primitives::{
Expand All @@ -18,7 +19,7 @@ use reth_primitives::{
Head, Header, TransactionSigned,
};
use reth_revm::{inspector_handle_register, Database, Evm, EvmBuilder, GetInspector};
use std::sync::Arc;
use std::{str::FromStr, sync::Arc};

mod config;
pub use config::{revm_spec, revm_spec_by_timestamp_after_bedrock};
Expand Down Expand Up @@ -160,13 +161,7 @@ impl ConfigureEvmEnv for OptimismEvmConfig {
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(),
),
basefee: get_base_fee(spec_id, parent, self.chain_spec.clone(), attributes.timestamp),
// calculate excess gas based on parent block's blob gas usage
blob_excess_gas_and_price,
};
Expand All @@ -183,6 +178,18 @@ impl ConfigureEvmEnv for OptimismEvmConfig {
}
}

fn get_base_fee(spec_id: SpecId, parent: &Header, chain_spec: Arc<OpChainSpec>, timestamp: u64) -> U256 {
cody-wang-cb marked this conversation as resolved.
Show resolved Hide resolved
let is_holocene = spec_id.is_enabled_in(SpecId::HOLOCENE);
if is_holocene {
let denominator = parent.nonce & B64::from_str("0xFFFFFFFF00000000").unwrap();
let elasticity = parent.nonce & B64::from_str("0x00000000FFFFFFFF").unwrap();
cody-wang-cb marked this conversation as resolved.
Show resolved Hide resolved
let base_fee_params = BaseFeeParams::new(u64::from(denominator) as u128, u64::from(elasticity) as u128);
U256::from(parent.next_block_base_fee(base_fee_params).unwrap_or_default())
} else {
U256::from(parent.next_block_base_fee(chain_spec.base_fee_params_at_timestamp(timestamp)).unwrap_or_default())
}
}

impl ConfigureEvm for OptimismEvmConfig {
type DefaultExternalContext<'a> = ();

Expand Down Expand Up @@ -546,4 +553,27 @@ mod tests {
// Optimism in handler
assert_eq!(evm.handler.cfg, HandlerCfg { spec_id: SpecId::ECOTONE, is_optimism: true });
}

#[test]
fn test_get_base_fee() {
let evm_config: OptimismEvmConfig = test_evm_config();

let mut parent = Header::default();
parent.nonce = B64::from_str("0x1234567812345678").unwrap();

let base_fee = get_base_fee(SpecId::BEDROCK, &parent, evm_config.chain_spec, 0);
assert_eq!(base_fee, U256::from(parent.next_block_base_fee(BaseFeeParams::new(0, 0)).unwrap_or_default()));
}

#[test]
fn test_get_base_fee_holocene() {
let evm_config: OptimismEvmConfig = test_evm_config();

let mut parent = Header::default();
parent.nonce = B64::from_str("0x1234567812345678").unwrap();

let base_fee = get_base_fee(SpecId::HOLOCENE, &parent, evm_config.chain_spec, 0);
assert_eq!(base_fee, U256::from(parent.next_block_base_fee(BaseFeeParams::new(0x12345678, 0x12345678)).unwrap_or_default()));

}
}
4 changes: 4 additions & 0 deletions crates/optimism/hardforks/src/hardfork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ hardfork!(
Fjord,
/// Granite: <https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/superchain-upgrades.md#granite>
Granite,
/// Holocene: <https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/superchain-upgrades.md#holocene>
Holocene,
}
);

Expand Down Expand Up @@ -156,6 +158,7 @@ impl OptimismHardfork {
Self::Ecotone => Some(1708534800),
Self::Fjord => Some(1716998400),
Self::Granite => Some(1723478400),
Self::Holocene => None // TODO: update when Holocene is defined
},
)
}
Expand Down Expand Up @@ -190,6 +193,7 @@ impl OptimismHardfork {
Self::Ecotone => Some(1710374401),
Self::Fjord => Some(1720627201),
Self::Granite => Some(1726070401),
Self::Holocene => None, // TODO: update when Holocene is defined
},
)
}
Expand Down
1 change: 1 addition & 0 deletions crates/optimism/node/tests/e2e/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ pub(crate) fn optimism_payload_attributes(timestamp: u64) -> OptimismPayloadBuil
transactions: vec![],
no_tx_pool: false,
gas_limit: Some(30_000_000),
eip_1559_params: B64::default(),
}
}
12 changes: 11 additions & 1 deletion crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,16 @@ where
blob_gas_used = Some(0);
}

let nonce;
if chain_spec.is_fork_active_at_timestamp(
OptimismHardfork::Holocene,
attributes.payload_attributes.timestamp,
) {
nonce = attributes.eip_1559_params;
} else {
nonce = BEACON_NONCE.into();
}

let header = Header {
parent_hash: parent_block.hash(),
ommers_hash: EMPTY_OMMER_ROOT_HASH,
Expand All @@ -496,7 +506,7 @@ where
logs_bloom,
timestamp: attributes.payload_attributes.timestamp,
mix_hash: attributes.payload_attributes.prev_randao,
nonce: BEACON_NONCE.into(),
nonce: nonce,
base_fee_per_gas: Some(base_fee),
number: parent_block.number + 1,
gas_limit: block_gas_limit,
Expand Down
5 changes: 4 additions & 1 deletion crates/optimism/payload/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Optimism builder support

use alloy_eips::eip2718::Decodable2718;
use alloy_primitives::{Address, B256, U256};
use alloy_primitives::{Address, B256, U256, B64};
use alloy_rlp::Encodable;
use alloy_rpc_types_engine::{ExecutionPayloadEnvelopeV2, ExecutionPayloadV1, PayloadId};
/// Re-export for use in downstream arguments.
Expand Down Expand Up @@ -37,6 +37,8 @@ pub struct OptimismPayloadBuilderAttributes {
pub transactions: Vec<WithEncoded<TransactionSigned>>,
/// The gas limit for the generated payload
pub gas_limit: Option<u64>,
/// EIP-1559 parameters for the generated payload
pub eip_1559_params: B64,
cody-wang-cb marked this conversation as resolved.
Show resolved Hide resolved
}

impl PayloadBuilderAttributes for OptimismPayloadBuilderAttributes {
Expand Down Expand Up @@ -81,6 +83,7 @@ impl PayloadBuilderAttributes for OptimismPayloadBuilderAttributes {
no_tx_pool: attributes.no_tx_pool.unwrap_or_default(),
transactions,
gas_limit: attributes.gas_limit,
eip_1559_params: attributes.eip_1559_params.unwrap_or_default(),
})
}

Expand Down