From 4b7518391c3d98386e46a4a53cded949b871979f Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 17 Jul 2024 07:10:50 +0800 Subject: [PATCH] refine rlp encoding logic --- src/eth/eip_1559_transaction.rs | 37 ++++++++++++++++---------------- src/eth/eip_155_transaction.rs | 24 +++++++++++---------- src/eth/homestead_transaction.rs | 24 +++++++++++---------- src/eth/mod.rs | 10 +++++++++ src/rpc/methods/eth.rs | 2 +- src/rpc/methods/eth/eth_tx.rs | 14 +++--------- 6 files changed, 59 insertions(+), 52 deletions(-) diff --git a/src/eth/eip_1559_transaction.rs b/src/eth/eip_1559_transaction.rs index a150b115f7a..12de4f5faf9 100644 --- a/src/eth/eip_1559_transaction.rs +++ b/src/eth/eip_1559_transaction.rs @@ -63,25 +63,26 @@ impl EthEip1559TxArgs { } pub fn rlp_signed_message(&self) -> anyhow::Result> { - let mut stream = rlp::RlpStream::new_list(12); - stream.append(&format_u64(self.chain_id)); - stream.append(&format_u64(self.nonce)); - stream.append(&format_bigint(&self.max_priority_fee_per_gas)?); - stream.append(&format_bigint(&self.max_fee_per_gas)?); - stream.append(&format_u64(self.gas_limit)); - stream.append(&format_address(&self.to)); - stream.append(&format_bigint(&self.value)?); - stream.append(&self.input); + // https://github.com/filecoin-project/lotus/blob/v1.27.1/chain/types/ethtypes/eth_1559_transactions.go#L72 + let prefix = [EIP_1559_TX_TYPE as u8].as_slice(); let access_list: &[u8] = &[]; - stream.append_list(access_list); - - stream.append(&format_bigint(&self.v)?); - stream.append(&format_bigint(&self.r)?); - stream.append(&format_bigint(&self.s)?); - - let mut rlp = stream.out().to_vec(); - rlp.insert(0, 2); - Ok(rlp) + let mut stream = rlp::RlpStream::new_with_buffer(prefix.into()); + stream + .begin_unbounded_list() + .append(&format_u64(self.chain_id)) + .append(&format_u64(self.nonce)) + .append(&format_bigint(&self.max_priority_fee_per_gas)?) + .append(&format_bigint(&self.max_fee_per_gas)?) + .append(&format_u64(self.gas_limit)) + .append(&format_address(&self.to)) + .append(&format_bigint(&self.value)?) + .append(&self.input) + .append_list(access_list) + .append(&format_bigint(&self.v)?) + .append(&format_bigint(&self.r)?) + .append(&format_bigint(&self.s)?); + stream.finalize_unbounded_list(); + Ok(stream.out().to_vec()) } } diff --git a/src/eth/eip_155_transaction.rs b/src/eth/eip_155_transaction.rs index 5c462fcd921..0eeb0b39dd2 100644 --- a/src/eth/eip_155_transaction.rs +++ b/src/eth/eip_155_transaction.rs @@ -85,17 +85,19 @@ impl EthLegacyEip155TxArgs { } pub fn rlp_signed_message(&self) -> anyhow::Result> { - let mut stream = rlp::RlpStream::new_list(9); - stream.append(&format_u64(self.nonce)); - stream.append(&format_bigint(&self.gas_price)?); - stream.append(&format_u64(self.gas_limit)); - stream.append(&format_address(&self.to)); - stream.append(&format_bigint(&self.value)?); - stream.append(&self.input); - stream.append(&format_bigint(&self.v)?); - stream.append(&format_bigint(&self.r)?); - stream.append(&format_bigint(&self.s)?); - + let mut stream = rlp::RlpStream::new(); + stream + .begin_unbounded_list() + .append(&format_u64(self.nonce)) + .append(&format_bigint(&self.gas_price)?) + .append(&format_u64(self.gas_limit)) + .append(&format_address(&self.to)) + .append(&format_bigint(&self.value)?) + .append(&self.input) + .append(&format_bigint(&self.v)?) + .append(&format_bigint(&self.r)?) + .append(&format_bigint(&self.s)?); + stream.finalize_unbounded_list(); Ok(stream.out().to_vec()) } } diff --git a/src/eth/homestead_transaction.rs b/src/eth/homestead_transaction.rs index b1561815e50..45efa7faeb7 100644 --- a/src/eth/homestead_transaction.rs +++ b/src/eth/homestead_transaction.rs @@ -70,17 +70,19 @@ impl EthLegacyHomesteadTxArgs { } pub fn rlp_signed_message(&self) -> anyhow::Result> { - let mut stream = rlp::RlpStream::new_list(9); - stream.append(&format_u64(self.nonce)); - stream.append(&format_bigint(&self.gas_price)?); - stream.append(&format_u64(self.gas_limit)); - stream.append(&format_address(&self.to)); - stream.append(&format_bigint(&self.value)?); - stream.append(&self.input); - stream.append(&format_bigint(&self.v)?); - stream.append(&format_bigint(&self.r)?); - stream.append(&format_bigint(&self.s)?); - + let mut stream = rlp::RlpStream::new(); + stream + .begin_unbounded_list() + .append(&format_u64(self.nonce)) + .append(&format_bigint(&self.gas_price)?) + .append(&format_u64(self.gas_limit)) + .append(&format_address(&self.to)) + .append(&format_bigint(&self.value)?) + .append(&self.input) + .append(&format_bigint(&self.v)?) + .append(&format_bigint(&self.r)?) + .append(&format_bigint(&self.s)?); + stream.finalize_unbounded_list(); Ok(stream.out().to_vec()) } } diff --git a/src/eth/mod.rs b/src/eth/mod.rs index 6328e7029f0..5130f5853ad 100644 --- a/src/eth/mod.rs +++ b/src/eth/mod.rs @@ -19,3 +19,13 @@ use crate::{ message::Message, }, }; + +/// Ethereum Improvement Proposals 1559 transaction type. This EIP changed Ethereum’s fee market mechanism. +/// Transaction type can have 3 distinct values: +/// - 0 for legacy transactions +/// - 1 for transactions introduced in EIP-2930 +/// - 2 for transactions introduced in EIP-1559 +pub const EIP_LEGACY_TX_TYPE: u64 = 0; +pub const EIP_1559_TX_TYPE: u64 = 2; + +pub const ETH_LEGACY_HOMESTEAD_TX_CHAIN_ID: u64 = 0; diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index 95795d48b5a..02010d0528c 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -13,7 +13,7 @@ use crate::chain_sync::SyncStage; use crate::cid_collections::CidHashSet; use crate::eth::{ EAMMethod, EVMMethod, EthChainId as EthChainIdType, EthEip1559TxArgs, EthLegacyEip155TxArgs, - EthLegacyHomesteadTxArgs, EthTx, + EthLegacyHomesteadTxArgs, }; use crate::lotus_json::{lotus_json_with_self, HasLotusJson}; use crate::message::{ChainMessage, Message as _, SignedMessage}; diff --git a/src/rpc/methods/eth/eth_tx.rs b/src/rpc/methods/eth/eth_tx.rs index 7e45a4741a6..5e638590c9f 100644 --- a/src/rpc/methods/eth/eth_tx.rs +++ b/src/rpc/methods/eth/eth_tx.rs @@ -2,17 +2,9 @@ // SPDX-License-Identifier: Apache-2.0, MIT use super::*; -use crate::eth::EthTx; - -/// Ethereum Improvement Proposals 1559 transaction type. This EIP changed Ethereum’s fee market mechanism. -/// Transaction type can have 3 distinct values: -/// - 0 for legacy transactions -/// - 1 for transactions introduced in EIP-2930 -/// - 2 for transactions introduced in EIP-1559 -pub const EIP_LEGACY_TX_TYPE: u64 = 0; -pub const EIP_1559_TX_TYPE: u64 = 2; - -pub const ETH_LEGACY_HOMESTEAD_TX_CHAIN_ID: u64 = 0; +pub use crate::eth::{ + EthTx, EIP_1559_TX_TYPE, EIP_LEGACY_TX_TYPE, ETH_LEGACY_HOMESTEAD_TX_CHAIN_ID, +}; impl From for ApiEthTx { fn from(