Skip to content

Commit

Permalink
refine rlp encoding logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 committed Jul 16, 2024
1 parent 0a3144d commit 36c03a0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 52 deletions.
37 changes: 19 additions & 18 deletions src/eth/eip_1559_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,26 @@ impl EthEip1559TxArgs {
}

pub fn rlp_signed_message(&self) -> anyhow::Result<Vec<u8>> {
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)?)
.finalize_unbounded_list();
Ok(stream.out().to_vec())
}
}

Expand Down
24 changes: 13 additions & 11 deletions src/eth/eip_155_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,19 @@ impl EthLegacyEip155TxArgs {
}

pub fn rlp_signed_message(&self) -> anyhow::Result<Vec<u8>> {
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)?)
.finalize_unbounded_list();
Ok(stream.out().to_vec())
}
}
Expand Down
24 changes: 13 additions & 11 deletions src/eth/homestead_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,19 @@ impl EthLegacyHomesteadTxArgs {
}

pub fn rlp_signed_message(&self) -> anyhow::Result<Vec<u8>> {
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)?)
.finalize_unbounded_list();
Ok(stream.out().to_vec())
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/rpc/methods/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
14 changes: 3 additions & 11 deletions src/rpc/methods/eth/eth_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EthLegacyHomesteadTxArgs> for ApiEthTx {
fn from(
Expand Down

0 comments on commit 36c03a0

Please sign in to comment.