Skip to content

Commit

Permalink
mongo-fuzzer: rm add_hardcoded_block_header_range (#1342)
Browse files Browse the repository at this point in the history
* mongo-fuzzer: rm add_hardcoded_block_header_range

* fix clippy

* fix tests

* clean up

* fix tests
  • Loading branch information
tcoratger authored Aug 22, 2024
1 parent fddab3b commit a84f7fb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
2 changes: 0 additions & 2 deletions src/test_utils/katana/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 13 additions & 19 deletions src/test_utils/mongo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<usize>) -> Result<(), Box<dyn std::error::Error>> {
for i in range {
let bytes: Vec<u8> = (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<dyn std::error::Error>> {
for _ in 0..n_logs {
Expand Down Expand Up @@ -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<dyn std::error::Error>> {
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::<u8>()).collect::<Vec<_>>(),
))?;
let mut transaction =
StoredTransaction::arbitrary_with_optional_fields(&mut arbitrary::Unstructured::new(
&(0..self.rnd_bytes_size).map(|_| rand::random::<u8>()).collect::<Vec<_>>(),
))?;

// 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);
Expand Down
4 changes: 3 additions & 1 deletion tests/tests/eth_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit a84f7fb

Please sign in to comment.