Skip to content

Commit

Permalink
fix hash calculation on decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Nov 9, 2022
1 parent cb4d043 commit 5b1b852
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions crates/net/eth-wire/src/types/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,10 @@ mod test {
};

// checking tx by tx for easier debugging if there are any regressions
for (expected, decoded) in
for (decoded, expected) in
decoded_transactions.message.0.iter().zip(expected_transactions.message.0.iter())
{
assert_eq!(expected, decoded);
assert_eq!(decoded, expected);
}

assert_eq!(decoded_transactions, expected_transactions);
Expand Down
14 changes: 9 additions & 5 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod util;

use crate::{Address, Bytes, ChainId, TxHash, H256, U256};
pub use access_list::{AccessList, AccessListItem};
use bytes::{Buf, BufMut, BytesMut};
use bytes::{Buf, BytesMut};
use derive_more::{AsRef, Deref};
use ethers_core::utils::keccak256;
use reth_codecs::main_codec;
Expand Down Expand Up @@ -477,14 +477,18 @@ impl Decodable for TransactionSigned {
let first_header = Header::decode(buf)?;
// if the transaction is encoded as a string then it is a typed transaction
if !first_header.list {
// Bytes that are going to be used to create a hash of transaction.
// For eip2728 types transaction header is not used inside hash
let original_encoding = *buf;

let tx_type = *buf
.first()
.ok_or(DecodeError::Custom("typed tx cannot be decoded from an empty slice"))?;
buf.advance(1);
// decode the list header for the rest of the transaction
let header = Header::decode(buf)?;
if !header.list {
return Err(DecodeError::Custom("typed tx fields must be encoded as a list"));
return Err(DecodeError::Custom("typed tx fields must be encoded as a list"))
}

// decode common fields
Expand Down Expand Up @@ -520,8 +524,7 @@ impl Decodable for TransactionSigned {
};

let mut signed = TransactionSigned { transaction, hash: Default::default(), signature };
let tx_length = first_header.payload_length + first_header.length();
signed.hash = keccak256(&original_encoding[..tx_length]).into();
signed.hash = keccak256(&original_encoding[..first_header.payload_length]).into();
Ok(signed)
} else {
let mut transaction = Transaction::Legacy {
Expand Down Expand Up @@ -617,7 +620,8 @@ impl TransactionSigned {
}
}

/// Calculate transaction hash, eip2728 transaction does not contain rlp header and start with tx type.
/// Calculate transaction hash, eip2728 transaction does not contain rlp header and start with
/// tx type.
pub fn recalculate_hash(&self) -> H256 {
let mut buf = Vec::new();
self.encode_inner(&mut buf, false);
Expand Down

0 comments on commit 5b1b852

Please sign in to comment.