From a84f7fb1a1607da8934b0eb20728120304e52689 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Thu, 22 Aug 2024 00:27:06 -0700 Subject: [PATCH] mongo-fuzzer: rm add_hardcoded_block_header_range (#1342) * mongo-fuzzer: rm add_hardcoded_block_header_range * fix clippy * fix tests * clean up * fix tests --- src/test_utils/katana/mod.rs | 2 -- src/test_utils/mongo/mod.rs | 32 +++++++++++++------------------- tests/tests/eth_provider.rs | 4 +++- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/test_utils/katana/mod.rs b/src/test_utils/katana/mod.rs index d5e0da477..a2b69d2d1 100644 --- a/src/test_utils/katana/mod.rs +++ b/src/test_utils/katana/mod.rs @@ -119,8 +119,6 @@ impl<'a> Katana { // Add random transactions to the MongoDB database. mongo_fuzzer.add_random_transactions(10).expect("Failed to add documents in the database"); - // Add a hardcoded block header range to the MongoDB database. - mongo_fuzzer.add_hardcoded_block_header_range(0..4).expect("Failed to add block range in the database"); // Add a hardcoded logs to the MongoDB database. mongo_fuzzer.add_random_logs(2).expect("Failed to logs in the database"); // Add a hardcoded header to the MongoDB database. diff --git a/src/test_utils/mongo/mod.rs b/src/test_utils/mongo/mod.rs index e92b586ee..57d5dfedc 100644 --- a/src/test_utils/mongo/mod.rs +++ b/src/test_utils/mongo/mod.rs @@ -16,7 +16,7 @@ use mongodb::{ use reth_primitives::{TxType, B256, U256}; use reth_rpc_types::Transaction; use serde::Serialize; -use std::{ops::Range, sync::LazyLock}; +use std::sync::LazyLock; use strum::{EnumIter, IntoEnumIterator}; use testcontainers::{ core::{IntoContainerPort, WaitFor}, @@ -144,20 +144,6 @@ impl MongoFuzzer { Ok(()) } - /// Adds a hardcoded block header range to the collection of headers. - pub fn add_hardcoded_block_header_range(&mut self, range: Range) -> Result<(), Box> { - for i in range { - let bytes: Vec = (0..self.rnd_bytes_size).map(|_| rand::random()).collect(); - let mut unstructured = arbitrary::Unstructured::new(&bytes); - let mut header = StoredHeader::arbitrary_with_optional_fields(&mut unstructured).unwrap(); - - header.header.number = Some(i as u64); - - self.headers.push(header); - } - Ok(()) - } - /// Adds random logs to the collection of logs. pub fn add_random_logs(&mut self, n_logs: usize) -> Result<(), Box> { for _ in 0..n_logs { @@ -189,11 +175,19 @@ impl MongoFuzzer { /// Adds random transactions to the collection of transactions. pub fn add_random_transactions(&mut self, n_transactions: usize) -> Result<(), Box> { - for _ in 0..n_transactions { + for i in 0..n_transactions { // Build a transaction using the random byte size. - let transaction = StoredTransaction::arbitrary_with_optional_fields(&mut arbitrary::Unstructured::new( - &(0..self.rnd_bytes_size).map(|_| rand::random::()).collect::>(), - ))?; + let mut transaction = + StoredTransaction::arbitrary_with_optional_fields(&mut arbitrary::Unstructured::new( + &(0..self.rnd_bytes_size).map(|_| rand::random::()).collect::>(), + ))?; + + // For the first transaction, set the block number to 0 to mimic a genesis block. + // + // We need to have a block number of 0 for our tests (when testing the `EARLIEST` block number). + if i == 0 { + transaction.tx.block_number = Some(0); + } // Generate a receipt for the transaction. let receipt = self.generate_transaction_receipt(&transaction.tx); diff --git a/tests/tests/eth_provider.rs b/tests/tests/eth_provider.rs index 1f3103637..d57c678a7 100644 --- a/tests/tests/eth_provider.rs +++ b/tests/tests/eth_provider.rs @@ -359,7 +359,9 @@ async fn test_get_logs_block_filter(#[future] katana: Katana, _setup: ()) { // Verify logs filtered by a range of blocks. assert_eq!(filter_logs(Filter::default().select(0..u64::MAX / 2), provider.clone()).await, logs_katana_block_range); // Verify that filtering by an empty range returns an empty result. - assert!(filter_logs(Filter::default().select(0..0), provider.clone()).await.is_empty()); + // + // We skip the 0 block because we hardcoded it via our Mongo Fuzzer and so it can contain logs. + assert!(filter_logs(Filter::default().select(1..1), provider.clone()).await.is_empty()); } #[rstest]