Skip to content

Commit

Permalink
impl Encodable2718, Decodable2718 for TransactionSigned
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Sep 25, 2024
1 parent 63f5673 commit e61b9e4
Show file tree
Hide file tree
Showing 26 changed files with 175 additions and 259 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/reth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ reth-engine-util.workspace = true
reth-prune.workspace = true

# crypto
alloy-eips.workspace = true
alloy-rlp.workspace = true
alloy-rpc-types = { workspace = true, features = ["engine"] }
alloy-consensus.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Command for debugging block building.
use alloy_consensus::TxEip4844;
use alloy_eips::eip2718::Encodable2718;
use alloy_rlp::Decodable;
use alloy_rpc_types::engine::{BlobsBundleV1, PayloadAttributes};
use clap::Parser;
Expand Down Expand Up @@ -200,7 +201,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {

encoded_length
}
_ => transaction.length_without_header(),
_ => transaction.encode_2718_len(),
};

debug!(target: "reth::cli", ?transaction, "Adding transaction to the pool");
Expand Down
2 changes: 2 additions & 0 deletions crates/optimism/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ 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
5 changes: 3 additions & 2 deletions crates/optimism/evm/src/l1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ where

#[cfg(test)]
mod tests {
use alloy_eips::eip2718::Decodable2718;
use reth_optimism_chainspec::OP_MAINNET;
use reth_optimism_forks::OptimismHardforks;
use reth_primitives::{BlockBody, TransactionSigned};
Expand All @@ -310,7 +311,7 @@ mod tests {
use reth_primitives::{Header, TransactionSigned};

let bytes = Bytes::from_static(&hex!("7ef9015aa044bae9d41b8380d781187b426c6fe43df5fb2fb57bd4466ef6a701e1f01e015694deaddeaddeaddeaddeaddeaddeaddeaddead000194420000000000000000000000000000000000001580808408f0d18001b90104015d8eb900000000000000000000000000000000000000000000000000000000008057650000000000000000000000000000000000000000000000000000000063d96d10000000000000000000000000000000000000000000000000000000000009f35273d89754a1e0387b89520d989d3be9c37c1f32495a88faf1ea05c61121ab0d1900000000000000000000000000000000000000000000000000000000000000010000000000000000000000002d679b567db6187c0c8323fa982cfb88b74dbcc7000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240"));
let l1_info_tx = TransactionSigned::decode_enveloped(&mut bytes.as_ref()).unwrap();
let l1_info_tx = TransactionSigned::decode_2718(&mut bytes.as_ref()).unwrap();
let mock_block = Block {
header: Header::default(),
body: BlockBody { transactions: vec![l1_info_tx], ..Default::default() },
Expand Down Expand Up @@ -338,7 +339,7 @@ mod tests {
// https://optimistic.etherscan.io/getRawTx?tx=0x88501da5d5ca990347c2193be90a07037af1e3820bb40774c8154871c7669150
const TX: [u8; 251] = hex!("7ef8f8a0a539eb753df3b13b7e386e147d45822b67cb908c9ddc5618e3dbaa22ed00850b94deaddeaddeaddeaddeaddeaddeaddeaddead00019442000000000000000000000000000000000000158080830f424080b8a4440a5e2000000558000c5fc50000000000000000000000006605a89f00000000012a10d90000000000000000000000000000000000000000000000000000000af39ac3270000000000000000000000000000000000000000000000000000000d5ea528d24e582fa68786f080069bdbfe06a43f8e67bfd31b8e4d8a8837ba41da9a82a54a0000000000000000000000006887246668a3b87f54deb3b94ba47a6f63f32985");

let tx = TransactionSigned::decode_enveloped(&mut TX.as_slice()).unwrap();
let tx = TransactionSigned::decode_2718(&mut TX.as_slice()).unwrap();
let block = Block {
body: BlockBody { transactions: vec![tx], ..Default::default() },
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions crates/optimism/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ reth-optimism-consensus.workspace = true
reth-optimism-forks.workspace = true

# ethereum
alloy-eips.workspace = true
alloy-primitives.workspace = true
op-alloy-rpc-types-engine.workspace = true
alloy-rpc-types-engine.workspace = true
Expand Down
6 changes: 4 additions & 2 deletions crates/optimism/node/src/txpool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! OP transaction pool types
use alloy_eips::eip2718::Encodable2718;
use parking_lot::RwLock;
use reth_chainspec::ChainSpec;
use reth_evm_optimism::RethL1BlockInfo;
Expand Down Expand Up @@ -139,7 +140,7 @@ where
let l1_block_info = self.block_info.l1_block_info.read().clone();

let mut encoded = Vec::with_capacity(valid_tx.transaction().encoded_length());
valid_tx.transaction().clone().into_consensus().encode_enveloped(&mut encoded);
valid_tx.transaction().clone().into_consensus().encode_2718(&mut encoded);

let cost_addition = match l1_block_info.l1_tx_data_fee(
&self.chain_spec(),
Expand Down Expand Up @@ -229,6 +230,7 @@ pub struct OpL1BlockInfo {
#[cfg(test)]
mod tests {
use crate::txpool::OpTransactionValidator;
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{TxKind, U256};
use reth::primitives::Signature;
use reth_chainspec::MAINNET;
Expand Down Expand Up @@ -266,7 +268,7 @@ mod tests {
let signed_tx = TransactionSigned::from_transaction_and_signature(deposit_tx, signature);
let signed_recovered =
TransactionSignedEcRecovered::from_signed_transaction(signed_tx, signer);
let len = signed_recovered.length_without_header();
let len = signed_recovered.encode_2718_len();
let pooled_tx = EthPooledTransaction::new(signed_recovered, len);
let outcome = validator.validate_one(origin, pooled_tx);

Expand Down
1 change: 1 addition & 0 deletions crates/optimism/payload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ reth-optimism-forks.workspace = true

# ethereum
revm.workspace = true
alloy-eips.workspace = true
alloy-primitives.workspace = true
alloy-rlp.workspace = true
op-alloy-rpc-types-engine.workspace = true
Expand Down
7 changes: 6 additions & 1 deletion crates/optimism/payload/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

//! Optimism builder support

use alloy_eips::eip2718::{Decodable2718, Eip2718Error};
use alloy_primitives::{Address, B256, U256};
use alloy_rlp::Encodable;
use alloy_rpc_types_engine::{ExecutionPayloadEnvelopeV2, ExecutionPayloadV1, PayloadId};
Expand Down Expand Up @@ -52,7 +53,11 @@ impl PayloadBuilderAttributes for OptimismPayloadBuilderAttributes {
.unwrap_or_default()
.into_iter()
.map(|data| {
TransactionSigned::decode_enveloped(&mut data.as_ref())
TransactionSigned::decode_2718(&mut data.as_ref())
.map_err(|err| match err {
Eip2718Error::RlpError(err) => err,
_ => alloy_rlp::Error::Custom("invalid transaction"),
})
.map(|tx| WithEncoded::new(data, tx))
})
.collect::<Result<_, _>>()?;
Expand Down
1 change: 1 addition & 0 deletions crates/optimism/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ reth-evm-optimism.workspace = true
reth-optimism-forks.workspace = true

# ethereum
alloy-eips.workspace = true
alloy-primitives.workspace = true
alloy-rpc-types-eth.workspace = true
alloy-rpc-types.workspace = true
Expand Down
11 changes: 6 additions & 5 deletions crates/optimism/rpc/src/eth/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Loads and formats OP receipt RPC response.

use alloy_eips::eip2718::Encodable2718;
use alloy_rpc_types::{AnyReceiptEnvelope, Log, TransactionReceipt};
use op_alloy_consensus::{OpDepositReceipt, OpDepositReceiptWithBloom, OpReceiptEnvelope};
use op_alloy_rpc_types::{
Expand Down Expand Up @@ -118,7 +119,7 @@ impl OpReceiptFieldsBuilder {
tx: &TransactionSigned,
l1_block_info: revm::L1BlockInfo,
) -> Result<Self, OpEthApiError> {
let raw_tx = tx.envelope_encoded();
let raw_tx = tx.encoded_2718();
let timestamp = self.l1_block_timestamp;

self.l1_fee = Some(
Expand Down Expand Up @@ -299,6 +300,7 @@ impl OpReceiptBuilder {
#[cfg(test)]
mod test {
use alloy_primitives::hex;
use op_alloy_network::eip2718::Decodable2718;
use reth_optimism_chainspec::OP_MAINNET;
use reth_primitives::{Block, BlockBody};

Expand Down Expand Up @@ -340,14 +342,13 @@ mod test {
#[test]
fn op_receipt_fields_from_block_and_tx() {
// rig
let tx_0 = TransactionSigned::decode_enveloped(
let tx_0 = TransactionSigned::decode_2718(
&mut TX_SET_L1_BLOCK_OP_MAINNET_BLOCK_124665056.as_slice(),
)
.unwrap();

let tx_1 =
TransactionSigned::decode_enveloped(&mut TX_1_OP_MAINNET_BLOCK_124665056.as_slice())
.unwrap();
let tx_1 = TransactionSigned::decode_2718(&mut TX_1_OP_MAINNET_BLOCK_124665056.as_slice())
.unwrap();

let block = Block {
body: BlockBody { transactions: [tx_0, tx_1.clone()].to_vec(), ..Default::default() },
Expand Down
3 changes: 2 additions & 1 deletion crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use alloc::vec::Vec;
pub use alloy_eips::eip1898::{
BlockHashOrNumber, BlockId, BlockNumHash, BlockNumberOrTag, ForkBlock, RpcBlockHash,
};
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::Sealable;
use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable};
use derive_more::{Deref, DerefMut};
Expand Down Expand Up @@ -481,7 +482,7 @@ impl SealedBlock {

/// Returns a vector of transactions RLP encoded with [`TransactionSigned::encode_enveloped`].
pub fn raw_transactions(&self) -> Vec<Bytes> {
self.body.transactions().map(|tx| tx.envelope_encoded()).collect()
self.body.transactions().map(|tx| tx.encoded_2718().into()).collect()
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
ReceiptWithBloomRef, Request, TransactionSigned, Withdrawal, B256,
};
use alloc::vec::Vec;
use alloy_eips::eip7685::Encodable7685;
use alloy_eips::{eip2718::Encodable2718, eip7685::Encodable7685};
use reth_trie_common::root::{ordered_trie_root, ordered_trie_root_with_encoder};

/// Calculate a transaction root.
Expand All @@ -15,7 +15,7 @@ pub fn calculate_transaction_root<T>(transactions: &[T]) -> B256
where
T: AsRef<TransactionSigned>,
{
ordered_trie_root_with_encoder(transactions, |tx: &T, buf| tx.as_ref().encode_inner(buf, false))
ordered_trie_root_with_encoder(transactions, |tx: &T, buf| tx.as_ref().encode_2718(buf))
}

/// Calculates the root hash of the withdrawals.
Expand Down
6 changes: 1 addition & 5 deletions crates/primitives/src/transaction/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ pub trait FillTxEnv {
impl FillTxEnv for TransactionSigned {
fn fill_tx_env(&self, tx_env: &mut TxEnv, sender: Address) {
#[cfg(feature = "optimism")]
let envelope = {
let mut envelope = alloc::vec::Vec::with_capacity(self.length_without_header());
self.encode_enveloped(&mut envelope);
envelope
};
let envelope = alloy_eips::eip2718::Encodable2718::encoded_2718(self);

tx_env.caller = sender;
match self.as_ref() {
Expand Down
Loading

0 comments on commit e61b9e4

Please sign in to comment.