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

feat(core): Transaction::decode() sets the hash #2303

Merged
merged 1 commit into from
Apr 1, 2023
Merged
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
15 changes: 14 additions & 1 deletion ethers-core/src/types/transaction/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl Transaction {
/// Get a Transaction directly from a rlp encoded byte stream
impl Decodable for Transaction {
fn decode(rlp: &rlp::Rlp) -> Result<Self, DecoderError> {
let mut txn = Self::default();
let mut txn = Self { hash: H256(keccak256(rlp.as_raw())), ..Default::default() };
// we can get the type from the first value
let mut offset = 0;

Expand Down Expand Up @@ -881,6 +881,19 @@ mod tests {
);
}

// Reference tx hash on Ethereum mainnet:
// 0x938913ef1df8cd17e0893a85586ade463014559fb1bd2d536ac282f3b1bdea53
#[test]
fn decode_tx_assert_hash() {
let raw_tx = hex::decode("02f874018201bb8405f5e10085096a1d45b782520894d696a5c568160bbbf5a1356f8ac56ee81a190588871550f7dca7000080c080a07df2299b0181d6d5b817795a7d2eff5897d0d3914ff5f602e17d5b75d32ec25fa051833973e8a8c222e682d2dcea02ad7bf3ec5bc3a86bfbcdbbaa3b853e52ad08").unwrap();
let tx: Transaction = Transaction::decode(&Rlp::new(&raw_tx)).unwrap();
assert_eq!(
tx.hash,
H256::from_str("938913ef1df8cd17e0893a85586ade463014559fb1bd2d536ac282f3b1bdea53")
.unwrap()
)
}

#[test]
fn recover_from() {
let tx = Transaction {
Expand Down