Skip to content

Commit

Permalink
chore(consensus): Re-export and Hardfork Cleanup (#274)
Browse files Browse the repository at this point in the history
### Description

Cleans up re-exports in `op-alloy-consensus`.

Moves towards a world where addresses can be customizable for
`Hardforks`.
Would like to see the `AddressList` come into play here in there future
for extensibility.
Should also update `Hardforks` to provide a more extensible pattern to
add future hardforks.

Might be worth renaming `Hardforks` to "Protocol Upgrades" to be in-line
with the OP Stack [specs](https://specs.optimism.io).
  • Loading branch information
refcell authored Nov 18, 2024
1 parent 4f5812b commit cbac051
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 111 deletions.
1 change: 0 additions & 1 deletion crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ alloy-consensus.workspace = true
alloy-primitives = { workspace = true, features = ["rlp"] }

# misc
spin.workspace = true
thiserror.workspace = true
derive_more = { workspace = true, features = ["display"] }

Expand Down
110 changes: 57 additions & 53 deletions crates/consensus/src/hardforks/ecotone.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,69 @@
//! Module containing a [Transaction] builder for the Ecotone network updgrade transactions.
//! Module containing a [Transaction] builder for the Ecotone network upgrade transactions.
//!
//! [Transaction]: alloy_consensus::Transaction

use crate::{OpTxEnvelope, TxDeposit};
use alloc::{string::String, vec, vec::Vec};
use alloy_consensus::Sealable;
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{address, bytes, hex, Address, Bytes, TxKind, U256};
use spin::Lazy;
use alloy_primitives::{address, bytes, hex, Address, Bytes, TxKind, B256, U256};

use crate::UpgradeDepositSource;

/// The UpdgradeTo Function Signature
pub const UPDGRADE_TO_FUNC_SIGNATURE: &str = "upgradeTo(address)";
use crate::{OpTxEnvelope, TxDeposit, UpgradeDepositSource, GAS_PRICE_ORACLE};

/// L1 Block Deployer Address
pub const L1_BLOCK_DEPLOYER_ADDRESS: Address = address!("4210000000000000000000000000000000000000");
pub const L1_BLOCK_DEPLOYER: Address = address!("4210000000000000000000000000000000000000");

/// The Gas Price Oracle Deployer Address
pub const GAS_PRICE_ORACLE_DEPLOYER_ADDRESS: Address =
address!("4210000000000000000000000000000000000001");
pub const GAS_PRICE_ORACLE_DEPLOYER: Address = address!("4210000000000000000000000000000000000001");

/// The new L1 Block Address
/// This is computed by using go-ethereum's `crypto.CreateAddress` function,
/// with the L1 Block Deployer Address and nonce 0.
pub const NEW_L1_BLOCK_ADDRESS: Address = address!("07dbe8500fc591d1852b76fee44d5a05e13097ff");

/// The Gas Price Oracle Address
/// This is computed by using go-ethereum's `crypto.CreateAddress` function,
/// with the Gas Price Oracle Deployer Address and nonce 0.
pub const GAS_PRICE_ORACLE_ADDRESS: Address = address!("b528d11cc114e026f138fe568744c6d45ce6da7a");

/// The Enable Ecotone Input Method 4Byte Signature
pub const ENABLE_ECOTONE_INPUT: [u8; 4] = hex!("22b908b3");
pub const NEW_L1_BLOCK: Address = address!("07dbe8500fc591d1852b76fee44d5a05e13097ff");

/// EIP-4788 From Address
pub const EIP4788_FROM: Address = address!("0B799C86a49DEeb90402691F1041aa3AF2d3C875");

static DEPLOY_L1_BLOCK_SOURCE: Lazy<UpgradeDepositSource> =
Lazy::new(|| UpgradeDepositSource { intent: String::from("Ecotone: L1 Block Deployment") });
impl super::Hardforks {
/// The Enable Ecotone Input Method 4Byte Signature
pub const ENABLE_ECOTONE_INPUT: [u8; 4] = hex!("22b908b3");

static DEPLOY_GAS_PRICE_ORACLE_SOURCE: Lazy<UpgradeDepositSource> = Lazy::new(|| {
UpgradeDepositSource { intent: String::from("Ecotone: Gas Price Oracle Deployment") }
});
/// Returns the source hash for the deployment of the l1 block contract.
pub fn deploy_l1_block_source() -> B256 {
UpgradeDepositSource { intent: String::from("Ecotone: L1 Block Deployment") }.source_hash()
}

static UPDATE_L1_BLOCK_PROXY_SOURCE: Lazy<UpgradeDepositSource> =
Lazy::new(|| UpgradeDepositSource { intent: String::from("Ecotone: L1 Block Proxy Update") });
/// Returns the source hash for the deployment of the gas price oracle contract.
pub fn deploy_gas_price_oracle_source() -> B256 {
UpgradeDepositSource { intent: String::from("Ecotone: Gas Price Oracle Deployment") }
.source_hash()
}

static UPDATE_GAS_PRICE_ORACLE_SOURCE: Lazy<UpgradeDepositSource> = Lazy::new(|| {
UpgradeDepositSource { intent: String::from("Ecotone: Gas Price Oracle Proxy Update") }
});
/// Returns the source hash for the update of the l1 block proxy.
pub fn update_l1_block_source() -> B256 {
UpgradeDepositSource { intent: String::from("Ecotone: L1 Block Proxy Update") }
.source_hash()
}

/// Returns the source hash for the update of the gas price oracle proxy.
pub fn update_gas_price_oracle_source() -> B256 {
UpgradeDepositSource { intent: String::from("Ecotone: Gas Price Oracle Proxy Update") }
.source_hash()
}

static ENABLE_ECOTONE_SOURCE: Lazy<UpgradeDepositSource> = Lazy::new(|| UpgradeDepositSource {
intent: String::from("Ecotone: Gas Price Oracle Set Ecotone"),
});
/// Returns the source hash for the Ecotone Beacon Block Roots Contract deployment.
pub fn beacon_roots_source() -> B256 {
UpgradeDepositSource {
intent: String::from("Ecotone: beacon block roots contract deployment"),
}
.source_hash()
}

static BEACON_ROOTS_SOURCE: Lazy<UpgradeDepositSource> = Lazy::new(|| UpgradeDepositSource {
intent: String::from("Ecotone: beacon block roots contract deployment"),
});
/// Returns the source hash for the Ecotone Gas Price Oracle activation.
pub fn enable_ecotone_source() -> B256 {
UpgradeDepositSource { intent: String::from("Ecotone: Gas Price Oracle Set Ecotone") }
.source_hash()
}

impl super::Hardforks {
/// Constructs the Ecotone network upgrade transactions.
pub fn ecotone_txs() -> Vec<Bytes> {
let mut txs = vec![];
Expand All @@ -75,8 +79,8 @@ impl super::Hardforks {
let mut buffer = Vec::new();
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: DEPLOY_L1_BLOCK_SOURCE.source_hash(),
from: L1_BLOCK_DEPLOYER_ADDRESS,
source_hash: Self::deploy_l1_block_source(),
from: L1_BLOCK_DEPLOYER,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
Expand All @@ -93,8 +97,8 @@ impl super::Hardforks {
buffer = Vec::new();
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: DEPLOY_GAS_PRICE_ORACLE_SOURCE.source_hash(),
from: GAS_PRICE_ORACLE_DEPLOYER_ADDRESS,
source_hash: Self::deploy_gas_price_oracle_source(),
from: GAS_PRICE_ORACLE_DEPLOYER,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
Expand All @@ -111,14 +115,14 @@ impl super::Hardforks {
buffer = Vec::new();
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: UPDATE_L1_BLOCK_PROXY_SOURCE.source_hash(),
source_hash: Self::update_l1_block_source(),
from: Address::default(),
to: TxKind::Call(L1_BLOCK_DEPLOYER_ADDRESS),
to: TxKind::Call(L1_BLOCK_DEPLOYER),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 50_000,
is_system_transaction: false,
input: Self::upgrade_to_calldata(NEW_L1_BLOCK_ADDRESS),
input: Self::upgrade_to_calldata(NEW_L1_BLOCK),
}
.seal_slow(),
)
Expand All @@ -129,14 +133,14 @@ impl super::Hardforks {
buffer = Vec::new();
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: UPDATE_GAS_PRICE_ORACLE_SOURCE.source_hash(),
source_hash: Self::update_gas_price_oracle_source(),
from: Address::default(),
to: TxKind::Call(GAS_PRICE_ORACLE_DEPLOYER_ADDRESS),
to: TxKind::Call(GAS_PRICE_ORACLE_DEPLOYER),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 50_000,
is_system_transaction: false,
input: Self::upgrade_to_calldata(GAS_PRICE_ORACLE_ADDRESS),
input: Self::upgrade_to_calldata(GAS_PRICE_ORACLE),
}
.seal_slow(),
)
Expand All @@ -147,25 +151,25 @@ impl super::Hardforks {
buffer = Vec::new();
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: ENABLE_ECOTONE_SOURCE.source_hash(),
from: L1_BLOCK_DEPLOYER_ADDRESS,
to: TxKind::Call(GAS_PRICE_ORACLE_ADDRESS),
source_hash: Self::enable_ecotone_source(),
from: L1_BLOCK_DEPLOYER,
to: TxKind::Call(GAS_PRICE_ORACLE),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 80_000,
is_system_transaction: false,
input: ENABLE_ECOTONE_INPUT.into(),
input: Self::ENABLE_ECOTONE_INPUT.into(),
}
.seal_slow(),
)
.encode_2718(&mut buffer);
txs.push(Bytes::from(buffer));

// Deploy EIP4788
// Deploy EIP-4788
buffer = Vec::new();
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: BEACON_ROOTS_SOURCE.source_hash(),
source_hash: Self::beacon_roots_source(),
from: EIP4788_FROM,
to: TxKind::Create,
mint: 0.into(),
Expand Down
75 changes: 34 additions & 41 deletions crates/consensus/src/hardforks/fjord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,44 @@ use crate::{OpTxEnvelope, TxDeposit};
use alloc::{string::String, vec, vec::Vec};
use alloy_consensus::Sealable;
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{address, hex, Address, Bytes, TxKind, U256};
use spin::Lazy;
use alloy_primitives::{address, hex, Address, Bytes, TxKind, B256, U256};

use crate::UpgradeDepositSource;
use crate::{UpgradeDepositSource, GAS_PRICE_ORACLE};

/// The L1 Info Depositer Address.
pub const L1_INFO_DEPOSITER_ADDRESS: Address = address!("deaddeaddeaddeaddeaddeaddeaddeaddead0001");
pub const L1_INFO_DEPOSITER: Address = address!("deaddeaddeaddeaddeaddeaddeaddeaddead0001");

/// Fjord Gas Price Oracle Deployer Address.
pub const GAS_PRICE_ORACLE_FJORD_DEPLOYER_ADDRESS: Address =
pub const GAS_PRICE_ORACLE_FJORD_DEPLOYER: Address =
address!("4210000000000000000000000000000000000002");

/// The Gas Price Oracle Address
/// This is computed by using go-ethereum's `crypto.CreateAddress` function,
/// with the Gas Price Oracle Deployer Address and nonce 0.
pub const GAS_PRICE_ORACLE_ADDRESS: Address = address!("b528d11cc114e026f138fe568744c6d45ce6da7a");

/// Fjord Gas Price Oracle source.
static DEPLOY_FJORD_GAS_PRICE_ORACLE_SOURCE: Lazy<UpgradeDepositSource> = Lazy::new(|| {
UpgradeDepositSource { intent: String::from("Fjord: Gas Price Oracle Deployment") }
});

/// [UpgradeDepositSource] for the source code update to the Fjord Gas Price Oracle.
static UPDATE_FJORD_GAS_PRICE_ORACLE_SOURCE: Lazy<UpgradeDepositSource> = Lazy::new(|| {
UpgradeDepositSource { intent: String::from("Fjord: Gas Price Oracle Proxy Update") }
});

/// Fjord Gas Price Oracle address.
pub const FJORD_GAS_PRICE_ORACLE_ADDRESS: Address =
address!("a919894851548179a0750865e7974da599c0fac7");
pub const FJORD_GAS_PRICE_ORACLE: Address = address!("a919894851548179a0750865e7974da599c0fac7");

/// [UpgradeDepositSource] for setting the Fjord Gas Price Oracle.
static ENABLE_FJORD_SOURCE: Lazy<UpgradeDepositSource> = Lazy::new(|| UpgradeDepositSource {
intent: String::from("Fjord: Gas Price Oracle Set Fjord"),
});
impl super::Hardforks {
/// The Set Fjord Four Byte Method Signature.
pub const SET_FJORD_METHOD_SIGNATURE: [u8; 4] = hex!("8e98b106");

/// Input data for setting the Fjord Gas Price Oracle.
pub const ENABLE_FJORD_FUNC_SIGNATURE: &str = "setFjord()";
/// Returns the source hash for the deployment of the Fjord Gas Price Oracle.
pub fn deploy_fjord_gas_price_oracle_source() -> B256 {
UpgradeDepositSource { intent: String::from("Fjord: Gas Price Oracle Deployment") }
.source_hash()
}

/// The Set Fjord Four Byte Method Signature.
pub const SET_FJORD_METHOD_SIGNATURE: [u8; 4] = hex!("8e98b106");
/// Returns the source hash for the update of the Fjord Gas Price Oracle.
pub fn update_fjord_gas_price_oracle_source() -> B256 {
UpgradeDepositSource { intent: String::from("Fjord: Gas Price Oracle Proxy Update") }
.source_hash()
}

/// [UpgradeDepositSource] for setting the Fjord Gas Price Oracle.
pub fn enable_fjord_source() -> B256 {
UpgradeDepositSource { intent: String::from("Fjord: Gas Price Oracle Set Fjord") }
.source_hash()
}

impl super::Hardforks {
/// Returns the fjord gas price oracle deployment bytecode.
pub(crate) fn gas_price_oracle_deployment_bytecode() -> alloy_primitives::Bytes {
pub fn gas_price_oracle_deployment_bytecode() -> alloy_primitives::Bytes {
include_bytes!("./gas_price_oracle_bytecode.hex").into()
}

Expand All @@ -61,8 +54,8 @@ impl super::Hardforks {
let mut buffer = Vec::new();
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: DEPLOY_FJORD_GAS_PRICE_ORACLE_SOURCE.source_hash(),
from: GAS_PRICE_ORACLE_FJORD_DEPLOYER_ADDRESS,
source_hash: Self::deploy_fjord_gas_price_oracle_source(),
from: GAS_PRICE_ORACLE_FJORD_DEPLOYER,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
Expand All @@ -79,14 +72,14 @@ impl super::Hardforks {
buffer = Vec::new();
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: UPDATE_FJORD_GAS_PRICE_ORACLE_SOURCE.source_hash(),
source_hash: Self::update_fjord_gas_price_oracle_source(),
from: Address::ZERO,
to: TxKind::Call(GAS_PRICE_ORACLE_ADDRESS),
to: TxKind::Call(GAS_PRICE_ORACLE),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 50_000,
is_system_transaction: false,
input: Self::upgrade_to_calldata(FJORD_GAS_PRICE_ORACLE_ADDRESS),
input: Self::upgrade_to_calldata(FJORD_GAS_PRICE_ORACLE),
}
.seal_slow(),
)
Expand All @@ -97,14 +90,14 @@ impl super::Hardforks {
buffer = Vec::new();
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: ENABLE_FJORD_SOURCE.source_hash(),
from: L1_INFO_DEPOSITER_ADDRESS,
to: TxKind::Call(GAS_PRICE_ORACLE_ADDRESS),
source_hash: Self::enable_fjord_source(),
from: L1_INFO_DEPOSITER,
to: TxKind::Call(GAS_PRICE_ORACLE),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 90_000,
is_system_transaction: false,
input: SET_FJORD_METHOD_SIGNATURE.into(),
input: Self::SET_FJORD_METHOD_SIGNATURE.into(),
}
.seal_slow(),
)
Expand Down
20 changes: 15 additions & 5 deletions crates/consensus/src/hardforks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
//! OP Stack Hardfork Transaction Updates

pub mod ecotone;
pub mod fjord;
use alloy_primitives::{address, Address};

/// UpgradeTo Function 4Byte Signature
pub(crate) const UPGRADE_TO_FUNC_BYTES_4: [u8; 4] = alloy_primitives::hex!("3659cfe6");
mod fjord;
pub use fjord::{FJORD_GAS_PRICE_ORACLE, GAS_PRICE_ORACLE_FJORD_DEPLOYER, L1_INFO_DEPOSITER};

mod ecotone;
pub use ecotone::{EIP4788_FROM, GAS_PRICE_ORACLE_DEPLOYER, L1_BLOCK_DEPLOYER, NEW_L1_BLOCK};

/// The Gas Price Oracle Address
/// This is computed by using go-ethereum's `crypto.CreateAddress` function,
/// with the Gas Price Oracle Deployer Address and nonce 0.
pub const GAS_PRICE_ORACLE: Address = address!("b528d11cc114e026f138fe568744c6d45ce6da7a");

/// Optimism Hardforks
///
Expand All @@ -31,9 +38,12 @@ pub(crate) const UPGRADE_TO_FUNC_BYTES_4: [u8; 4] = alloy_primitives::hex!("3659
pub struct Hardforks;

impl Hardforks {
/// UpgradeTo Function 4Byte Signature
pub const UPGRADE_TO_FUNC_BYTES_4: [u8; 4] = alloy_primitives::hex!("3659cfe6");

/// Turns the given address into calldata for the `upgradeTo` function.
pub(crate) fn upgrade_to_calldata(addr: alloy_primitives::Address) -> alloy_primitives::Bytes {
let mut v = UPGRADE_TO_FUNC_BYTES_4.to_vec();
let mut v = Self::UPGRADE_TO_FUNC_BYTES_4.to_vec();
v.extend_from_slice(addr.as_slice());
alloy_primitives::Bytes::from(v)
}
Expand Down
11 changes: 8 additions & 3 deletions crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ pub use transaction::{
DEPOSIT_TX_TYPE_ID,
};

pub mod eip1559;
pub mod hardforks;
pub use hardforks::Hardforks;
mod eip1559;
pub use eip1559::{decode_eip_1559_params, decode_holocene_extra_data, EIP1559ParamError};

mod hardforks;
pub use hardforks::{
Hardforks, EIP4788_FROM, FJORD_GAS_PRICE_ORACLE, GAS_PRICE_ORACLE, GAS_PRICE_ORACLE_DEPLOYER,
GAS_PRICE_ORACLE_FJORD_DEPLOYER, L1_BLOCK_DEPLOYER, L1_INFO_DEPOSITER, NEW_L1_BLOCK,
};

mod block;
pub use block::OpBlock;
Expand Down
5 changes: 0 additions & 5 deletions crates/protocol/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ impl TraceStorage {
.collect()
}

/// Locks the storage and returns the items.
pub fn lock(&self) -> spin::MutexGuard<'_, Vec<(Level, String)>> {
self.0.lock()
}

/// Returns if the storage is empty.
pub fn is_empty(&self) -> bool {
self.0.lock().is_empty()
Expand Down
Loading

0 comments on commit cbac051

Please sign in to comment.