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

fix(core): decode to correctly in Transaction #1946

Merged
merged 1 commit into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion ethers-core/src/types/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl Eip1559TransactionRequest {
*offset += 1;
tx.gas = Some(rlp.val_at(*offset)?);
*offset += 1;
tx.to = decode_to(rlp, offset)?;
tx.to = decode_to(rlp, offset)?.map(NameOrAddress::Address);
tx.value = Some(rlp.val_at(*offset)?);
*offset += 1;
let data = rlp::Rlp::new(rlp.at(*offset)?.as_raw()).data()?;
Expand Down
2 changes: 1 addition & 1 deletion ethers-core/src/types/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn decode_signature(
fn decode_to(
rlp: &rlp::Rlp,
offset: &mut usize,
) -> Result<Option<super::NameOrAddress>, rlp::DecoderError> {
) -> Result<Option<super::Address>, rlp::DecoderError> {
let to = {
let to = rlp.at(*offset)?;
if to.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion ethers-core/src/types/transaction/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl TransactionRequest {
*offset += 1;
}

txn.to = decode_to(rlp, offset)?;
txn.to = decode_to(rlp, offset)?.map(NameOrAddress::Address);
txn.value = Some(rlp.at(*offset)?.as_val()?);
*offset += 1;

Expand Down
35 changes: 26 additions & 9 deletions ethers-core/src/types/transaction/response.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Transaction types
use super::{
decode_signature, eip2718::TypedTransaction, eip2930::AccessList, normalize_v, rlp_opt,
rlp_opt_list,
decode_signature, decode_to, eip2718::TypedTransaction, eip2930::AccessList, normalize_v,
rlp_opt, rlp_opt_list,
};
use crate::{
types::{
Expand Down Expand Up @@ -248,8 +248,7 @@ impl Transaction {
*offset += 1;
self.gas = rlp.val_at(*offset)?;
*offset += 1;
self.to = Some(rlp.val_at(*offset)?);
*offset += 1;
self.to = decode_to(rlp, offset)?;
self.value = rlp.val_at(*offset)?;
*offset += 1;
let input = rlp::Rlp::new(rlp.at(*offset)?.as_raw()).data()?;
Expand Down Expand Up @@ -279,8 +278,7 @@ impl Transaction {
#[cfg(feature = "celo")]
self.decode_celo_metadata(rlp, offset)?;

self.to = Some(rlp.val_at(*offset)?);
*offset += 1;
self.to = decode_to(rlp, offset)?;
self.value = rlp.val_at(*offset)?;
*offset += 1;
let input = rlp::Rlp::new(rlp.at(*offset)?.as_raw()).data()?;
Expand Down Expand Up @@ -310,8 +308,7 @@ impl Transaction {
#[cfg(feature = "celo")]
self.decode_celo_metadata(rlp, offset)?;

self.to = Some(rlp.val_at(*offset)?);
*offset += 1;
self.to = decode_to(rlp, offset)?;
self.value = rlp.val_at(*offset)?;
*offset += 1;
let input = rlp::Rlp::new(rlp.at(*offset)?.as_raw()).data()?;
Expand Down Expand Up @@ -473,7 +470,7 @@ impl PartialOrd<Self> for TransactionReceipt {
#[cfg(test)]
#[cfg(not(feature = "celo"))]
mod tests {
use rlp::Encodable;
use rlp::{Encodable, Rlp};

use crate::types::transaction::eip2930::AccessListItem;

Expand Down Expand Up @@ -1093,4 +1090,24 @@ mod tests {
let r = rlp::Rlp::new(b.as_slice());
Transaction::decode(&r).unwrap();
}

#[test]
fn test_rlp_decoding_create_roundtrip() {
let tx = Transaction {
block_hash: None,
block_number: None,
from: Address::from_str("c26ad91f4e7a0cad84c4b9315f420ca9217e315d").unwrap(),
gas: U256::from_str_radix("0x10e2b", 16).unwrap(),
gas_price: Some(U256::from_str_radix("0x12ec276caf", 16).unwrap()),
hash: H256::from_str("929ff27a5c7833953df23103c4eb55ebdfb698678139d751c51932163877fada").unwrap(),
input: Bytes::from(
hex::decode("a9059cbb000000000000000000000000fdae129ecc2c27d166a3131098bc05d143fa258e0000000000000000000000000000000000000000000000000000000002faf080").unwrap()
),
nonce: U256::zero(),
transaction_index: None,
value: U256::zero(),
..Default::default()
};
Transaction::decode(&Rlp::new(&tx.rlp())).unwrap();
}
}