chore: unify decode enveloped fn args (#6904)

This commit is contained in:
Matthias Seitz
2024-03-01 12:32:15 +01:00
committed by GitHub
parent cdca09e15a
commit defe34f04f
5 changed files with 13 additions and 16 deletions

View File

@ -2,7 +2,7 @@
use alloy_rlp::{Decodable, Encodable};
use reth_eth_wire::{EthVersion, PooledTransactions, ProtocolMessage};
use reth_primitives::{hex, Bytes, PooledTransactionsElement};
use reth_primitives::{hex, PooledTransactionsElement};
use std::{fs, path::PathBuf};
use test_fuzz::test_fuzz;
@ -70,6 +70,6 @@ fn decode_blob_rpc_transaction() {
let network_data_path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/rpc_blob_transaction");
let data = fs::read_to_string(network_data_path).expect("Unable to read file");
let hex_data = Bytes::from(hex::decode(data.trim()).unwrap());
let _txs = PooledTransactionsElement::decode_enveloped(hex_data).unwrap();
let hex_data = hex::decode(data.trim()).unwrap();
let _txs = PooledTransactionsElement::decode_enveloped(&mut hex_data.as_ref()).unwrap();
}

View File

@ -192,9 +192,7 @@ impl PooledTransactionsElement {
///
/// Where `tx_payload_body` is encoded as a RLP list:
/// `[chain_id, nonce, max_priority_fee_per_gas, ..., y_parity, r, s]`
pub fn decode_enveloped(tx: Bytes) -> alloy_rlp::Result<Self> {
let mut data = tx.as_ref();
pub fn decode_enveloped(data: &mut &[u8]) -> alloy_rlp::Result<Self> {
if data.is_empty() {
return Err(RlpError::InputTooShort)
}
@ -203,7 +201,7 @@ impl PooledTransactionsElement {
if data[0] >= EMPTY_LIST_CODE {
// decode as legacy transaction
let (transaction, hash, signature) =
TransactionSigned::decode_rlp_legacy_transaction_tuple(&mut data)?;
TransactionSigned::decode_rlp_legacy_transaction_tuple(data)?;
Ok(Self::Legacy { transaction, signature, hash })
} else {
@ -225,12 +223,12 @@ impl PooledTransactionsElement {
// Now, we decode the inner blob transaction:
// `rlp([[chain_id, nonce, ...], blobs, commitments, proofs])`
let blob_tx = BlobTransaction::decode_inner(&mut data)?;
let blob_tx = BlobTransaction::decode_inner(data)?;
Ok(PooledTransactionsElement::BlobTransaction(blob_tx))
} else {
// DO NOT advance the buffer for the type, since we want the enveloped decoding to
// decode it again and advance the buffer on its own.
let typed_tx = TransactionSigned::decode_enveloped_typed_transaction(&mut data)?;
let typed_tx = TransactionSigned::decode_enveloped_typed_transaction(data)?;
// because we checked the tx type, we can be sure that the transaction is not a
// blob transaction or legacy
@ -726,8 +724,7 @@ mod tests {
// this is a legacy tx so we can attempt the same test with decode_enveloped
let input_rlp = &mut &hex_data[..];
let res =
PooledTransactionsElement::decode_enveloped(Bytes::copy_from_slice(input_rlp));
let res = PooledTransactionsElement::decode_enveloped(input_rlp);
assert!(
res.is_err(),
@ -743,7 +740,7 @@ mod tests {
let data = hex!("02f903d382426882ba09832dc6c0848674742682ed9694714b6a4ea9b94a8a7d9fd362ed72630688c8898c80b90364492d24749189822d8512430d3f3ff7a2ede675ac08265c08e2c56ff6fdaa66dae1cdbe4a5d1d7809f3e99272d067364e597542ac0c369d69e22a6399c3e9bee5da4b07e3f3fdc34c32c3d88aa2268785f3e3f8086df0934b10ef92cfffc2e7f3d90f5e83302e31382e302d64657600000000000000000000000000000000000000000000569e75fc77c1a856f6daaf9e69d8a9566ca34aa47f9133711ce065a571af0cfd000000000000000000000000e1e210594771824dad216568b91c9cb4ceed361c00000000000000000000000000000000000000000000000000000000000546e00000000000000000000000000000000000000000000000000000000000e4e1c00000000000000000000000000000000000000000000000000000000065d6750c00000000000000000000000000000000000000000000000000000000000f288000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002cf600000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000000f1628e56fa6d8c50e5b984a58c0df14de31c7b857ce7ba499945b99252976a93d06dcda6776fc42167fbe71cb59f978f5ef5b12577a90b132d14d9c6efa528076f0161d7bf03643cfc5490ec5084f4a041db7f06c50bd97efa08907ba79ddcac8b890f24d12d8db31abbaaf18985d54f400449ee0559a4452afe53de5853ce090000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000003e800000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000064ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000c080a01428023fc54a27544abc421d5d017b9a7c5936ad501cbdecd0d9d12d04c1a033a0753104bbf1c87634d6ff3f0ffa0982710612306003eb022363b57994bdef445a"
);
let res = PooledTransactionsElement::decode_enveloped(data.into()).unwrap();
let res = PooledTransactionsElement::decode_enveloped(&mut &data[..]).unwrap();
assert_eq!(
res.into_transaction().to(),
Some(address!("714b6a4ea9b94a8a7d9fd362ed72630688c8898c"))
@ -777,7 +774,7 @@ mod tests {
assert!(input_rlp.is_empty());
// we can also decode_enveloped
let res = PooledTransactionsElement::decode_enveloped(Bytes::copy_from_slice(data));
let res = PooledTransactionsElement::decode_enveloped(&mut &data[..]);
assert_matches!(res, Ok(_tx));
}
}

View File

@ -13,7 +13,7 @@ pub(crate) fn recover_raw_transaction(
return Err(EthApiError::EmptyRawTransactionData)
}
let transaction = PooledTransactionsElement::decode_enveloped(data)
let transaction = PooledTransactionsElement::decode_enveloped(&mut data.as_ref())
.map_err(|_| EthApiError::FailedToDecodeSignedTransaction)?;
transaction.try_into_ecrecovered().or(Err(EthApiError::InvalidTransactionSignature))

View File

@ -691,7 +691,7 @@ mod tests {
let temp_dir = tempfile::tempdir().unwrap();
let transactions_path = temp_dir.path().join(FILENAME).with_extension(EXTENSION);
let tx_bytes = hex!("02f87201830655c2808505ef61f08482565f94388c818ca8b9251b393131c08a736a67ccb192978801049e39c4b5b1f580c001a01764ace353514e8abdfb92446de356b260e3c1225b73fc4c8876a6258d12a129a04f02294aa61ca7676061cd99f29275491218b4754b46a0248e5e42bc5091f507");
let tx = PooledTransactionsElement::decode_enveloped(tx_bytes.into()).unwrap();
let tx = PooledTransactionsElement::decode_enveloped(&mut &tx_bytes[..]).unwrap();
let provider = MockEthProvider::default();
let transaction = EthPooledTransaction::from_recovered_pooled_transaction(
tx.try_into_ecrecovered().unwrap(),

File diff suppressed because one or more lines are too long