Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: roundtrip fuzz harness for PooledTransactions #5125

Merged
merged 2 commits into from
Oct 30, 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
36 changes: 26 additions & 10 deletions crates/net/eth-wire/tests/pooled_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@ use alloy_rlp::{Decodable, Encodable};
use reth_eth_wire::{EthVersion, PooledTransactions, ProtocolMessage};
use reth_primitives::{hex, Bytes, PooledTransactionsElement};
use std::{fs, path::PathBuf};
use test_fuzz::test_fuzz;

#[test]
fn decode_pooled_transactions_data() {
let network_data_path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/pooled_transactions_with_blob");
let data = fs::read_to_string(network_data_path).expect("Unable to read file");
let hex_data = hex::decode(data.trim()).unwrap();
let txs = PooledTransactions::decode(&mut &hex_data[..]).unwrap();
/// Helper function to ensure encode-decode roundtrip works for [`PooledTransactions`].
#[test_fuzz]
fn roundtrip_pooled_transactions(hex_data: Vec<u8>) -> Result<(), alloy_rlp::Error> {
let input_rlp = &mut &hex_data[..];
let txs = match PooledTransactions::decode(input_rlp) {
Ok(txs) => txs,
Err(e) => return Err(e),
};

// get the amount of bytes decoded in `decode` by subtracting the length of the original buf,
// from the length of the remaining bytes
let decoded_len = hex_data.len() - input_rlp.len();
let expected_encoding = hex_data[..decoded_len].to_vec();

// do a roundtrip test
let mut buf = Vec::new();
txs.encode(&mut buf);
if hex_data != buf {
panic!("mixed pooled transaction roundtrip failed");
}
assert_eq!(expected_encoding, buf);

// now do another decoding, on what we encoded - this should succeed
let txs2 = PooledTransactions::decode(&mut &buf[..]).unwrap();
Expand All @@ -27,6 +32,17 @@ fn decode_pooled_transactions_data() {

// ensure that the length is equal to the length of the encoded data
assert_eq!(txs.length(), buf.len());

Ok(())
}

#[test]
fn decode_pooled_transactions_data() {
let network_data_path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/pooled_transactions_with_blob");
let data = fs::read_to_string(network_data_path).expect("Unable to read file");
let hex_data = hex::decode(data.trim()).expect("Unable to decode hex");
assert!(roundtrip_pooled_transactions(hex_data).is_ok());
}

#[test]
Expand Down