feat(core): `Transaction::decode()` sets the hash

Fixes: #1849
This commit is contained in:
achillelamb 2023-03-26 16:30:16 +02:00
parent 36dac5864c
commit 438f930b67
1 changed files with 14 additions and 1 deletions

View File

@ -335,7 +335,7 @@ impl Transaction {
/// Get a Transaction directly from a rlp encoded byte stream /// Get a Transaction directly from a rlp encoded byte stream
impl Decodable for Transaction { impl Decodable for Transaction {
fn decode(rlp: &rlp::Rlp) -> Result<Self, DecoderError> { 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 // we can get the type from the first value
let mut offset = 0; let mut offset = 0;
@ -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] #[test]
fn recover_from() { fn recover_from() {
let tx = Transaction { let tx = Transaction {