Skip to content

Commit

Permalink
add not first holocene block check
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-wang-cb committed Oct 7, 2024
1 parent ad9dd5a commit ea57b2a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
36 changes: 29 additions & 7 deletions crates/optimism/chainspec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod op_sepolia;
use std::{fmt::Display, str::FromStr};

use alloy_genesis::Genesis;
use alloy_primitives::{Parity, Signature, B256, B64, U256};
use alloy_primitives::{b64, Parity, Signature, B256, B64, U256};
pub use base::BASE_MAINNET;
pub use base_sepolia::BASE_SEPOLIA;
pub use dev::OP_DEV;
Expand All @@ -35,8 +35,9 @@ use reth_network_peers::NodeRecord;
use reth_optimism_forks::OptimismHardfork;
use reth_primitives_traits::Header;

const DENOMINATOR_MASK: u64 = 0xFFFFFFFF00000000;
const ELASTICITY_MASK: u64 = 0x00000000FFFFFFFF;
/// These are the masks for the base fee denominator and elasticity in the nonce, post Holocene
const DENOMINATOR_MASK: B64 = b64!("FFFFFFFF00000000");
const ELASTICITY_MASK: B64 = b64!("00000000FFFFFFFF");

/// OP stack chain spec type.
#[derive(Debug, Clone, Deref, Into, Constructor, PartialEq, Eq)]
Expand All @@ -61,11 +62,15 @@ impl Fee for OpChainSpec {
fn next_block_base_fee(&self, parent: &Header, timestamp: u64) -> U256 {
let is_holocene =
self.inner.is_fork_active_at_timestamp(OptimismHardfork::Holocene, timestamp);
if is_holocene {
let not_first =
self.inner.is_fork_active_at_timestamp(OptimismHardfork::Holocene, parent.timestamp);
// If we are in the Holocene, we need to use the base fee params from the parent block's
// nonce Else, use the base fee params from chainspec
if is_holocene && not_first {
// First 4 bytes of the nonce are the base fee denominator, the last 4 bytes are the
// elasticity
let denominator = parent.nonce & B64::from_str(&DENOMINATOR_MASK.to_string()).unwrap();
let elasticity = parent.nonce & B64::from_str(&ELASTICITY_MASK.to_string()).unwrap();
let denominator = parent.nonce & DENOMINATOR_MASK;
let elasticity = parent.nonce & ELASTICITY_MASK;
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())
Expand Down Expand Up @@ -603,7 +608,24 @@ mod tests {
}

#[test]
fn test_get_base_fee_holocene() {
fn test_get_base_fee_holocene_first_block() {
let op_chain_spec = &BASE_SEPOLIA;
let mut parent = Header::default();
parent.nonce = B64::from_str("0x1234567812345678").unwrap();

let base_fee = op_chain_spec.next_block_base_fee(&parent, 0);
assert_eq!(
base_fee,
U256::from(
parent
.next_block_base_fee(BaseFeeParams::new(0x12345678, 0x12345678))
.unwrap_or_default()
)
);
}

#[test]
fn test_get_base_fee_holocene_not_first_block() {
let op_chain_spec = &BASE_SEPOLIA;
let mut parent = Header::default();
parent.nonce = B64::from_str("0x1234567812345678").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/node/tests/e2e/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +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(),
eip_1559_params: Some(B64::default()),
}
}
5 changes: 2 additions & 3 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ where
);

if is_holocene && attributes.eip_1559_params.is_none() {
return Err(PayloadBuilderError::other(
OptimismPayloadBuilderError::MissingEip1559Params,
))
return Err(PayloadBuilderError::other(OptimismPayloadBuilderError::MissingEip1559Params))
}

let mut executed_txs = Vec::with_capacity(attributes.transactions.len());
Expand Down Expand Up @@ -507,6 +505,7 @@ where
logs_bloom,
timestamp: attributes.payload_attributes.timestamp,
mix_hash: attributes.payload_attributes.prev_randao,
// Post holocene, adding eip 1559 params to the nonce field
nonce: get_nonce(
is_holocene,
&attributes,
Expand Down
4 changes: 2 additions & 2 deletions crates/optimism/payload/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +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(),
eip_1559_params: attributes.eip_1559_params,
})
}

Expand Down Expand Up @@ -123,7 +123,7 @@ impl Default for OptimismPayloadBuilderAttributes {
no_tx_pool: false,
transactions: Vec::new(),
gas_limit: None,
eip_1559_params: B64::default(),
eip_1559_params: Some(B64::default()),
}
}
}
Expand Down

0 comments on commit ea57b2a

Please sign in to comment.