Skip to content

Commit

Permalink
feat: wrap TxDeposit into Sealed in OpTxEnvelope (#247)
Browse files Browse the repository at this point in the history
<!--
Thank you for your Pull Request. Please provide a description above and
review
the requirements below.

Bug fixes and new features should include tests.

Contributors guide:
https://github.com/alloy-rs/core/blob/main/CONTRIBUTING.md

The contributors guide includes instructions for running rustfmt and
building the
documentation.
-->

<!-- ** Please select "Allow edits from maintainers" in the PR Options
** -->

## Motivation

ref https://t.me/paradigm_reth/36099

Makes envelope more consistend by making all variants hold a hash

## Solution

<!--
Summarize the solution and provide any necessary context needed to
understand
the code change.
-->

## PR Checklist

- [ ] Added Tests
- [ ] Added Documentation
- [ ] Breaking changes
  • Loading branch information
klkvr authored Nov 12, 2024
1 parent 1ddf3fb commit 60d90b7
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 119 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ alloy-network-primitives = { version = "0.6.3", default-features = false }
alloy-rlp = { version = "0.3", default-features = false }

# Alloy Core
alloy-sol-types = { version = "0.8.11", default-features = false }
alloy-primitives = { version = "0.8.11", default-features = false }
alloy-sol-types = { version = "0.8.12", default-features = false }
alloy-primitives = { version = "0.8.12", default-features = false }

# Revm
revm = "18.0.0"
Expand Down
139 changes: 79 additions & 60 deletions crates/consensus/src/hardforks/ecotone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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, Address, Bytes, TxKind, U256};
use spin::Lazy;
Expand Down Expand Up @@ -72,91 +73,109 @@ impl super::Hardforks {

// Deploy the L1 Block Contract
let mut buffer = Vec::new();
OpTxEnvelope::Deposit(TxDeposit {
source_hash: DEPLOY_L1_BLOCK_SOURCE.source_hash(),
from: L1_BLOCK_DEPLOYER_ADDRESS,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
gas_limit: 375_000,
is_system_transaction: false,
input: l1_block_deployment_bytecode,
})
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: DEPLOY_L1_BLOCK_SOURCE.source_hash(),
from: L1_BLOCK_DEPLOYER_ADDRESS,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
gas_limit: 375_000,
is_system_transaction: false,
input: l1_block_deployment_bytecode,
}
.seal_slow(),
)
.encode_2718(&mut buffer);
txs.push(Bytes::from(buffer));

// Deploy the Gas Price Oracle
buffer = Vec::new();
OpTxEnvelope::Deposit(TxDeposit {
source_hash: DEPLOY_GAS_PRICE_ORACLE_SOURCE.source_hash(),
from: GAS_PRICE_ORACLE_DEPLOYER_ADDRESS,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
gas_limit: 1_000_000,
is_system_transaction: false,
input: gas_price_oracle_deployment_bytecode,
})
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: DEPLOY_GAS_PRICE_ORACLE_SOURCE.source_hash(),
from: GAS_PRICE_ORACLE_DEPLOYER_ADDRESS,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
gas_limit: 1_000_000,
is_system_transaction: false,
input: gas_price_oracle_deployment_bytecode,
}
.seal_slow(),
)
.encode_2718(&mut buffer);
txs.push(Bytes::from(buffer));

// Update the l1 block proxy
buffer = Vec::new();
OpTxEnvelope::Deposit(TxDeposit {
source_hash: UPDATE_L1_BLOCK_PROXY_SOURCE.source_hash(),
from: Address::default(),
to: TxKind::Call(L1_BLOCK_DEPLOYER_ADDRESS),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 50_000,
is_system_transaction: false,
input: Self::upgrade_to_calldata(NEW_L1_BLOCK_ADDRESS),
})
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: UPDATE_L1_BLOCK_PROXY_SOURCE.source_hash(),
from: Address::default(),
to: TxKind::Call(L1_BLOCK_DEPLOYER_ADDRESS),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 50_000,
is_system_transaction: false,
input: Self::upgrade_to_calldata(NEW_L1_BLOCK_ADDRESS),
}
.seal_slow(),
)
.encode_2718(&mut buffer);
txs.push(Bytes::from(buffer));

// Update gas price oracle proxy
buffer = Vec::new();
OpTxEnvelope::Deposit(TxDeposit {
source_hash: UPDATE_GAS_PRICE_ORACLE_SOURCE.source_hash(),
from: Address::default(),
to: TxKind::Call(GAS_PRICE_ORACLE_DEPLOYER_ADDRESS),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 50_000,
is_system_transaction: false,
input: Self::upgrade_to_calldata(GAS_PRICE_ORACLE_ADDRESS),
})
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: UPDATE_GAS_PRICE_ORACLE_SOURCE.source_hash(),
from: Address::default(),
to: TxKind::Call(GAS_PRICE_ORACLE_DEPLOYER_ADDRESS),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 50_000,
is_system_transaction: false,
input: Self::upgrade_to_calldata(GAS_PRICE_ORACLE_ADDRESS),
}
.seal_slow(),
)
.encode_2718(&mut buffer);
txs.push(Bytes::from(buffer));

// Enable ecotone
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),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 80_000,
is_system_transaction: false,
input: ENABLE_ECOTONE_INPUT.into(),
})
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: ENABLE_ECOTONE_SOURCE.source_hash(),
from: L1_BLOCK_DEPLOYER_ADDRESS,
to: TxKind::Call(GAS_PRICE_ORACLE_ADDRESS),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 80_000,
is_system_transaction: false,
input: ENABLE_ECOTONE_INPUT.into(),
}
.seal_slow(),
)
.encode_2718(&mut buffer);
txs.push(Bytes::from(buffer));

// Deploy EIP4788
buffer = Vec::new();
OpTxEnvelope::Deposit(TxDeposit {
source_hash: BEACON_ROOTS_SOURCE.source_hash(),
from: EIP4788_FROM,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
gas_limit: 250_000,
is_system_transaction: false,
input: eip4788_creation_data,
})
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: BEACON_ROOTS_SOURCE.source_hash(),
from: EIP4788_FROM,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
gas_limit: 250_000,
is_system_transaction: false,
input: eip4788_creation_data,
}
.seal_slow(),
)
.encode_2718(&mut buffer);
txs.push(Bytes::from(buffer));

Expand Down
70 changes: 40 additions & 30 deletions crates/consensus/src/hardforks/fjord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::{OpTxEnvelope, TxDeposit};
use alloc::{string::String, vec, vec::Vec};
use alloy_consensus::Sealable;
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{address, Address, Bytes, TxKind, U256};
use spin::Lazy;
Expand Down Expand Up @@ -58,46 +59,55 @@ impl super::Hardforks {
let mut txs = vec![];

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,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
gas_limit: 1_450_000,
is_system_transaction: false,
input: Self::gas_price_oracle_deployment_bytecode(),
})
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: DEPLOY_FJORD_GAS_PRICE_ORACLE_SOURCE.source_hash(),
from: GAS_PRICE_ORACLE_FJORD_DEPLOYER_ADDRESS,
to: TxKind::Create,
mint: 0.into(),
value: U256::ZERO,
gas_limit: 1_450_000,
is_system_transaction: false,
input: Self::gas_price_oracle_deployment_bytecode(),
}
.seal_slow(),
)
.encode_2718(&mut buffer);
txs.push(Bytes::from(buffer));

// Update the gas price oracle proxy.
buffer = Vec::new();
OpTxEnvelope::Deposit(TxDeposit {
source_hash: UPDATE_FJORD_GAS_PRICE_ORACLE_SOURCE.source_hash(),
from: Address::ZERO,
to: TxKind::Call(GAS_PRICE_ORACLE_ADDRESS),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 50_000,
is_system_transaction: false,
input: Self::upgrade_to_calldata(FJORD_GAS_PRICE_ORACLE_ADDRESS),
})
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: UPDATE_FJORD_GAS_PRICE_ORACLE_SOURCE.source_hash(),
from: Address::ZERO,
to: TxKind::Call(GAS_PRICE_ORACLE_ADDRESS),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 50_000,
is_system_transaction: false,
input: Self::upgrade_to_calldata(FJORD_GAS_PRICE_ORACLE_ADDRESS),
}
.seal_slow(),
)
.encode_2718(&mut buffer);
txs.push(Bytes::from(buffer));

// Enable Fjord
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),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 90_000,
is_system_transaction: false,
input: SET_FJORD_METHOD_SIGNATURE.into(),
})
OpTxEnvelope::Deposit(
TxDeposit {
source_hash: ENABLE_FJORD_SOURCE.source_hash(),
from: L1_INFO_DEPOSITER_ADDRESS,
to: TxKind::Call(GAS_PRICE_ORACLE_ADDRESS),
mint: 0.into(),
value: U256::ZERO,
gas_limit: 90_000,
is_system_transaction: false,
input: SET_FJORD_METHOD_SIGNATURE.into(),
}
.seal_slow(),
)
.encode_2718(&mut buffer);
txs.push(Bytes::from(buffer));

Expand Down
26 changes: 20 additions & 6 deletions crates/consensus/src/transaction/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

use super::OpTxType;
use crate::DepositTransaction;
use alloy_consensus::Transaction;
use alloc::vec::Vec;
use alloy_consensus::{Sealable, Transaction};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{
Address, Bytes, ChainId, PrimitiveSignature as Signature, TxKind, B256, U256,
keccak256, Address, Bytes, ChainId, PrimitiveSignature as Signature, TxHash, TxKind, B256, U256,
};
use alloy_rlp::{
Buf, BufMut, Decodable, Encodable, Error as DecodeError, Header, EMPTY_STRING_CODE,
Expand Down Expand Up @@ -214,6 +215,13 @@ impl TxDeposit {
self.eip2718_encode(out);
}

/// Calculate the transaction hash.
pub fn tx_hash(&self) -> TxHash {
let mut buf = Vec::with_capacity(self.eip2718_encoded_length());
self.eip2718_encode(&mut buf);
keccak256(&buf)
}

/// Returns the signature for the optimism deposit transactions, which don't include a
/// signature.
pub fn signature() -> Signature {
Expand Down Expand Up @@ -305,22 +313,28 @@ impl Decodable for TxDeposit {
}
}

impl Sealable for TxDeposit {
fn hash_slow(&self) -> B256 {
self.tx_hash()
}
}

/// Deposit transactions don't have a signature, however, we include an empty signature in the
/// response for better compatibility.
///
/// This function can be used as `serialize_with` serde attribute for the [`TxDeposit`] and will
/// flatten [`TxDeposit::signature`] into response.
#[cfg(feature = "serde")]
pub fn serde_deposit_tx_rpc<S: serde::Serializer>(
value: &TxDeposit,
pub fn serde_deposit_tx_rpc<T: serde::Serialize, S: serde::Serializer>(
value: &T,
serializer: S,
) -> Result<S::Ok, S::Error> {
use serde::Serialize;

#[derive(Serialize)]
struct SerdeHelper<'a> {
struct SerdeHelper<'a, T> {
#[serde(flatten)]
value: &'a TxDeposit,
value: &'a T,
#[serde(flatten)]
signature: Signature,
}
Expand Down
Loading

0 comments on commit 60d90b7

Please sign in to comment.