Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Correctly encode non-legacy mempool transactions #415

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 60 additions & 12 deletions ethers-core/src/types/transaction/response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Transaction types
use super::{eip2930::AccessList, rlp_opt, NUM_TX_FIELDS};
use super::{eip2930::AccessList, rlp_opt};
use crate::{
types::{Address, Bloom, Bytes, Log, H256, U256, U64},
utils::keccak256,
Expand Down Expand Up @@ -141,22 +141,70 @@ impl Transaction {

pub fn rlp(&self) -> Bytes {
let mut rlp = RlpStream::new();
rlp.begin_list(NUM_TX_FIELDS);
rlp.append(&self.nonce);
rlp_opt(&mut rlp, &self.gas_price);
rlp.append(&self.gas);
rlp.begin_unbounded_list();

match self.transaction_type {
// EIP-2930 (0x01)
Some(x) if x == U64::from(1) => {
rlp_opt(&mut rlp, &self.chain_id);
rlp.append(&self.nonce);
rlp_opt(&mut rlp, &self.gas_price);
rlp.append(&self.gas);

#[cfg(feature = "celo")]
self.inject_celo_metadata(&mut rlp);

rlp_opt(&mut rlp, &self.to);
rlp.append(&self.value);
rlp.append(&self.input.as_ref());
rlp.append(&self.access_list);
}
// EIP-1559 (0x02)
Some(x) if x == U64::from(2) => {
rlp_opt(&mut rlp, &self.chain_id);
rlp.append(&self.nonce);
rlp_opt(&mut rlp, &self.max_priority_fee_per_gas);
rlp_opt(&mut rlp, &self.max_fee_per_gas);
rlp.append(&self.gas);
rlp_opt(&mut rlp, &self.to);
rlp.append(&self.value);
rlp.append(&self.input.as_ref());
rlp.append(&self.access_list);
}
// Legacy (0x00)
_ => {
rlp.append(&self.nonce);
rlp_opt(&mut rlp, &self.gas_price);
rlp.append(&self.gas);

#[cfg(feature = "celo")]
self.inject_celo_metadata(&mut rlp);

rlp_opt(&mut rlp, &self.to);
rlp.append(&self.value);
rlp.append(&self.input.as_ref());
}
}

#[cfg(feature = "celo")]
self.inject_celo_metadata(&mut rlp);

rlp_opt(&mut rlp, &self.to);
rlp.append(&self.value);
rlp.append(&self.input.as_ref());
rlp.append(&self.v);
rlp.append(&self.r);
rlp.append(&self.s);

rlp.out().freeze().into()
let rlp_bytes: Bytes = rlp.out().freeze().into();
let mut encoded = vec![];
match self.transaction_type {
Some(x) if x == U64::from(1) => {
encoded.extend_from_slice(&[0x1]);
encoded.extend_from_slice(rlp_bytes.as_ref());
encoded.into()
}
Some(x) if x == U64::from(2) => {
encoded.extend_from_slice(&[0x2]);
encoded.extend_from_slice(rlp_bytes.as_ref());
encoded.into()
}
_ => rlp_bytes,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about matching Some(x) if x == U64::from(0) and encoding with the 0x0 prefix, and default to rlp_bytes only when self.transaction_type is not present OR if the legacy feature is turned on?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NVM. You are right, forgot that we skip it for legacy txs:

Legacy(ref tx) => {
encoded.extend_from_slice(tx.rlp_signed(signature).as_ref());
}

}
}
}

Expand Down