Fix Transaction decoding (#1773)

* fix(core): accept both typed transaction formats

* fix equality sign for rlp type check

* remove printlns

* remove redundant transaction_type assignment
This commit is contained in:
Dan Cline 2022-10-09 01:39:05 -04:00 committed by GitHub
parent 441b983ec6
commit 123e2def9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 2 deletions

View File

@ -343,8 +343,8 @@ impl Decodable for Transaction {
let mut txn = Self::default();
// we can get the type from the first value
let mut offset = 0;
let data = rlp.data()?;
let first = *data.first().ok_or(DecoderError::Custom("empty slice"))?;
// only untyped legacy transactions are lists
if rlp.is_list() {
// Legacy (0x00)
// use the original rlp
@ -356,6 +356,16 @@ impl Decodable for Transaction {
// extract chain id if legacy
txn.chain_id = extract_chain_id(sig.v).map(|id| id.as_u64().into());
} else {
// if it is not enveloped then we need to use rlp.as_raw instead of rlp.data
let first_byte = rlp.as_raw()[0];
let (first, data) = if first_byte <= 0x7f {
(first_byte, rlp.as_raw())
} else {
let data = rlp.data()?;
let first = *data.first().ok_or(DecoderError::Custom("empty slice"))?;
(first, data)
};
let bytes = data.get(1..).ok_or(DecoderError::Custom("no tx body"))?;
let rest = rlp::Rlp::new(bytes);
match first {
@ -1046,4 +1056,19 @@ mod tests {
a.transaction_index = 1u64.into();
assert!(a > b);
}
// from https://github.com/gakonst/ethers-rs/issues/1762
#[test]
fn test_rlp_decoding_type_2() {
use crate::types::*;
let raw_tx = "0x02f906f20103843b9aca0085049465153e830afdd19468b3465833fb72a70ecdf485e0e4c7bd8665fc4580b906845ae401dc00000000000000000000000000000000000000000000000000000000633c4c730000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000460000000000000000000000000000000000000000000000000000000000000058000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000012b6893ce26ea6341919fe289212ef77e51688c800000000000000000000000000000000000000000000000000000000000027100000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000017754984000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000124b858183f000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000017754984000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000042dac17f958d2ee523a2206206994597c13d831ec7000bb8c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200271012b6893ce26ea6341919fe289212ef77e51688c8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000104b858183f00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000006d78ac6800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002bdac17f958d2ee523a2206206994597c13d831ec70001f4c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e4472b43f30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001f8aa12f280116c88954000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000012b6893ce26ea6341919fe289212ef77e51688c8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000064df2ab5bb00000000000000000000000012b6893ce26ea6341919fe289212ef77e51688c8000000000000000000000000000000000000000000002d092097defac5b7a01a000000000000000000000000f69a7cd9649a5b5477fa0e5395385fad03ac639f00000000000000000000000000000000000000000000000000000000c001a0127484994706ff8605f1da80e7bdf0efa3e26192a094413e58d409551398b0b5a06fd706e38eebeba2f235e37ceb0acb426f1e6c91702add97810ee677a15d1980";
let mut decoded_tx = crate::utils::rlp::decode::<Transaction>(
&raw_tx.parse::<Bytes>().expect("unable to parse raw tx"),
)
.expect("unable to decode raw tx");
decoded_tx.recover_from_mut().unwrap();
decoded_tx.hash = decoded_tx.hash();
dbg!(&decoded_tx);
}
}