Skip to content

Commit

Permalink
feat(consensus): Isthmus Network Upgrade Txs (#405)
Browse files Browse the repository at this point in the history
### Description

Drafts up the network upgrade transactions for the Isthmus hardfork.

- [EIP-2935](https://eips.ethereum.org/EIPS/eip-2935#deployment) ->
block hash contract
- ~~[EIP-7002](https://eips.ethereum.org/EIPS/eip-7002#deployment) ->
withdrawals request contract~~
- ~~[EIP-7251](https://eips.ethereum.org/EIPS/eip-7251#deployment) ->
consolidation requests contract~~
  • Loading branch information
refcell authored Feb 3, 2025
1 parent 7c07895 commit d92679a
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 4 deletions.
Binary file added crates/consensus/eip2935_isthmus.hex
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
60538060095f395ff33373fffffffffffffffffffffffffffffffffffffffe14604657602036036042575f35600143038111604257611fff81430311604257611fff9006545f5260205ff35b5f5ffd5b5f35611fff60014303065500
1 change: 1 addition & 0 deletions crates/consensus/src/hardforks/bytecode/isthmus_tx_1.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7ef89ca0ebd6fc97ea8145b1e7b2e763e76bee378575ef495aa16eafbcf7325525bc1b47943462413af4609098e1e27a490f554f260213d6858080808303d09080b85c60538060095f395ff33373fffffffffffffffffffffffffffffffffffffffe14604657602036036042575f35600143038111604257611fff81430311604257611fff9006545f5260205ff35b5f5ffd5b5f35611fff60014303065500
19 changes: 16 additions & 3 deletions crates/consensus/src/hardforks/forks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Contains all hardforks represented in the [crate::Hardfork] type.
use crate::{Ecotone, Fjord};
use crate::{Ecotone, Fjord, Isthmus};

/// Optimism Hardforks
///
Expand All @@ -23,16 +23,26 @@ use crate::{Ecotone, Fjord};
/// let fjord_upgrade_txs = Hardforks::FJORD.txs();
/// assert_eq!(fjord_upgrade_txs.collect::<Vec<_>>().len(), 3);
/// ```
///
/// Build isthmus hardfork upgrade transaction:
/// ```rust
/// use op_alloy_consensus::{Hardfork, Hardforks};
/// let isthmus_upgrade_tx = Hardforks::ISTHMUS.txs();
/// assert_eq!(isthmus_upgrade_tx.collect::<Vec<_>>().len(), 1);
/// ```
#[derive(Debug, Default, Clone, Copy)]
#[non_exhaustive]
pub struct Hardforks;

impl Hardforks {
/// The ecotone hardfork upgrade transactions.
/// The Ecotone hardfork upgrade transactions.
pub const ECOTONE: Ecotone = Ecotone;

/// The fjord hardfork upgrade transactions.
/// The Fjord hardfork upgrade transactions.
pub const FJORD: Fjord = Fjord;

/// The Isthmus hardfork upgrade transactions.
pub const ISTHMUS: Isthmus = Isthmus;
}

#[cfg(test)]
Expand All @@ -48,5 +58,8 @@ mod tests {

let fjord_upgrade_txs = Hardforks::FJORD.txs();
assert_eq!(fjord_upgrade_txs.collect::<Vec<_>>().len(), 3);

let isthmus_upgrade_tx = Hardforks::ISTHMUS.txs();
assert_eq!(isthmus_upgrade_tx.collect::<Vec<_>>().len(), 1);
}
}
86 changes: 86 additions & 0 deletions crates/consensus/src/hardforks/isthmus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//! Module containing a [Transaction] builder for the Isthmus network upgrade transactions.
//!
//! Isthmus network upgrade transactions are defined in the [OP Stack Specs][specs].
//!
//! [specs]: https://specs.optimism.io/protocol/isthmus/derivation.html#network-upgrade-automation-transactions
//! [Transaction]: alloy_consensus::Transaction
use crate::UpgradeDepositSource;
use alloc::{string::String, vec::Vec};
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{address, hex, Address, Bytes, TxKind, B256, U256};

use crate::{Hardfork, TxDeposit};

/// The Isthmus network upgrade transactions.
#[derive(Debug, Default, Clone, Copy)]
pub struct Isthmus;

impl Isthmus {
/// EIP-2935 From Address
pub const EIP2935_FROM: Address = address!("3462413Af4609098e1E27A490f554f260213D685");

/// Returns the source hash for the Isthmus Deposit Contract deployment.
pub fn deposit_contract_source() -> B256 {
UpgradeDepositSource { intent: String::from("Isthmus: deposit contract deployment") }
.source_hash()
}

/// Returns the EIP-2935 creation data.
pub fn eip2935_creation_data() -> Bytes {
let contents = core::str::from_utf8(include_bytes!("./bytecode/eip2935_isthmus.hex"))
.expect("eip-2935 creation data is not valid UTF-8");
let contents = contents.replace("\n", "");
let decoded = hex::decode(contents).expect("eip-2935 creation data is not valid hex");
decoded.into()
}

/// Returns the list of [TxDeposit]s for the network upgrade.
pub fn deposits() -> impl Iterator<Item = TxDeposit> {
([TxDeposit {
source_hash: Self::deposit_contract_source(),
from: Self::EIP2935_FROM,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
gas_limit: 250_000,
is_system_transaction: false,
input: Self::eip2935_creation_data(),
}])
.into_iter()
}
}

impl Hardfork for Isthmus {
/// Constructs the network upgrade transactions.
fn txs(&self) -> impl Iterator<Item = Bytes> + '_ {
Self::deposits().map(|tx| {
let mut encoded = Vec::new();
tx.encode_2718(&mut encoded);
Bytes::from(encoded)
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;

#[test]
fn test_isthmus_txs_encoded() {
let isthmus_upgrade_tx = Isthmus.txs().collect::<Vec<_>>();
assert_eq!(isthmus_upgrade_tx.len(), 1);

let expected_first_tx = include_bytes!("./bytecode/isthmus_tx_1.hex");
let expected_first_tx =
core::str::from_utf8(expected_first_tx).expect("expected_first_tx is not valid UTF-8");
let expected_first_tx = expected_first_tx.replace("\n", "");
let expected_first_tx =
hex::decode(expected_first_tx).expect("expected_first_tx is not valid hex");
let expected_txs: Vec<Bytes> = vec![expected_first_tx.into()];
for (i, expected) in expected_txs.iter().enumerate() {
assert_eq!(isthmus_upgrade_tx[i], *expected);
}
}
}
3 changes: 3 additions & 0 deletions crates/consensus/src/hardforks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ pub use fjord::Fjord;
mod ecotone;
pub use ecotone::Ecotone;

mod isthmus;
pub use isthmus::Isthmus;

mod utils;
pub(crate) use utils::upgrade_to_calldata;
2 changes: 1 addition & 1 deletion crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use eip1559::{
};

mod hardforks;
pub use hardforks::{Ecotone, Fjord, Hardfork, Hardforks};
pub use hardforks::{Ecotone, Fjord, Hardfork, Hardforks, Isthmus};

mod source;
pub use source::*;
Expand Down

0 comments on commit d92679a

Please sign in to comment.