Skip to content

Commit

Permalink
tests: replace arbitrary_with_optional_fields by arbitrary impl (#…
Browse files Browse the repository at this point in the history
…1346)

tests: replace arbitrary with optional by arbitrary impl
  • Loading branch information
tcoratger authored Aug 23, 2024
1 parent a84f7fb commit af43887
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
13 changes: 7 additions & 6 deletions src/providers/eth_provider/database/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ impl EthereumBlockStore for Database {
mod tests {
use super::*;
use crate::test_utils::mongo::{MongoFuzzer, RANDOM_BYTES_SIZE};
use arbitrary::Arbitrary;
use rand::{self, Rng};

#[tokio::test(flavor = "multi_thread")]
Expand Down Expand Up @@ -243,7 +244,7 @@ mod tests {
assert_eq!(database.transaction(&first_transaction.hash).await.unwrap(), Some(first_transaction.into()));

// Generate a transaction not present in the database
let unstored_transaction = StoredTransaction::arbitrary_with_optional_fields(unstructured).unwrap();
let unstored_transaction = StoredTransaction::arbitrary(unstructured).unwrap();

// Test retrieving a non-existent transaction by its hash
assert_eq!(database.transaction(&unstored_transaction.hash).await.unwrap(), None);
Expand Down Expand Up @@ -284,7 +285,7 @@ mod tests {
async fn test_upsert_pending_transactions(unstructured: &mut arbitrary::Unstructured<'_>, database: &Database) {
// Generate 10 pending transactions and add them to the database
let pending_transactions: Vec<StoredPendingTransaction> =
(0..10).map(|_| StoredPendingTransaction::arbitrary_with_optional_fields(unstructured).unwrap()).collect();
(0..10).map(|_| StoredPendingTransaction::arbitrary(unstructured).unwrap()).collect();

// Add pending transactions to the database
for tx in &pending_transactions {
Expand All @@ -302,7 +303,7 @@ mod tests {
);

// Test retrieving a non-existent pending transaction by its hash
let unstored_transaction = StoredTransaction::arbitrary_with_optional_fields(unstructured).unwrap();
let unstored_transaction = StoredTransaction::arbitrary(unstructured).unwrap();
assert_eq!(database.pending_transaction(&unstored_transaction.hash).await.unwrap(), None);

// Test retrieving the number of retries for a pending transaction
Expand All @@ -317,14 +318,14 @@ mod tests {

async fn test_upsert_transactions(unstructured: &mut arbitrary::Unstructured<'_>, database: &Database) {
// Generate and upsert a mock transaction into the database
let mock_transaction = StoredTransaction::arbitrary_with_optional_fields(unstructured).unwrap();
let mock_transaction = StoredTransaction::arbitrary(unstructured).unwrap();
database.upsert_transaction(mock_transaction.clone().tx).await.unwrap();

// Test retrieving an upserted transaction by its hash
assert_eq!(database.transaction(&mock_transaction.hash).await.unwrap(), Some(mock_transaction.into()));

// Generate and upsert a mock pending transaction into the database
let mock_pending_transaction = StoredPendingTransaction::arbitrary_with_optional_fields(unstructured).unwrap();
let mock_pending_transaction = StoredPendingTransaction::arbitrary(unstructured).unwrap();
database
.upsert_pending_transaction(mock_pending_transaction.clone().tx, mock_pending_transaction.clone().retries)
.await
Expand Down Expand Up @@ -436,7 +437,7 @@ mod tests {
assert_eq!(database.block(rng.gen::<u64>().into(), false).await.unwrap(), None);

// test withdrawals_root raises an error
let mut faulty_header = StoredHeader::arbitrary_with_optional_fields(u).unwrap();
let mut faulty_header = StoredHeader::arbitrary(u).unwrap();
faulty_header.header.withdrawals_root = Some(rng.gen::<B256>());

let filter =
Expand Down
5 changes: 2 additions & 3 deletions src/providers/eth_provider/database/types/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use {

/// A header as stored in the database
#[derive(Debug, Serialize, Deserialize, Hash, Clone, PartialEq, Eq)]
#[cfg_attr(any(test, feature = "arbitrary", feature = "testing"), derive(arbitrary::Arbitrary))]
pub struct StoredHeader {
#[serde(deserialize_with = "crate::providers::eth_provider::database::types::serde::deserialize_intermediate")]
pub header: Header,
Expand All @@ -36,8 +35,8 @@ impl Deref for StoredHeader {
}

#[cfg(any(test, feature = "arbitrary", feature = "testing"))]
impl<'a> StoredHeader {
pub fn arbitrary_with_optional_fields(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
impl Arbitrary<'_> for StoredHeader {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
Ok(Self {
header: Header {
hash: Some(B256::arbitrary(u)?),
Expand Down
14 changes: 6 additions & 8 deletions src/providers/eth_provider/database/types/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use {

/// A full transaction as stored in the database
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
#[cfg_attr(any(test, feature = "arbitrary", feature = "testing"), derive(arbitrary::Arbitrary))]
pub struct StoredTransaction {
#[serde(deserialize_with = "crate::providers::eth_provider::database::types::serde::deserialize_intermediate")]
pub tx: Transaction,
Expand Down Expand Up @@ -45,8 +44,8 @@ impl Deref for StoredTransaction {
}

#[cfg(any(test, feature = "arbitrary", feature = "testing"))]
impl<'a> StoredTransaction {
pub fn arbitrary_with_optional_fields(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
impl Arbitrary<'_> for StoredTransaction {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
// Initialize a random number generator.
let mut rng = generators::rng();
// Generate a random integer between 0 and 2 to decide which transaction type to create.
Expand Down Expand Up @@ -130,9 +129,9 @@ impl StoredPendingTransaction {
}

#[cfg(any(test, feature = "arbitrary", feature = "testing"))]
impl<'a> StoredPendingTransaction {
pub fn arbitrary_with_optional_fields(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Self { tx: StoredTransaction::arbitrary_with_optional_fields(u)?.into(), retries: u8::arbitrary(u)? })
impl Arbitrary<'_> for StoredPendingTransaction {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
Ok(Self { tx: StoredTransaction::arbitrary(u)?.into(), retries: u8::arbitrary(u)? })
}
}

Expand Down Expand Up @@ -183,8 +182,7 @@ mod tests {
rand::thread_rng().fill(bytes.as_mut_slice());

// Generate a random transaction
let transaction =
StoredTransaction::arbitrary_with_optional_fields(&mut arbitrary::Unstructured::new(&bytes)).unwrap();
let transaction = StoredTransaction::arbitrary(&mut arbitrary::Unstructured::new(&bytes)).unwrap();

// Extract the signature from the generated transaction.
let signature = transaction.signature.unwrap();
Expand Down
11 changes: 5 additions & 6 deletions src/test_utils/mongo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl MongoFuzzer {
) -> Result<(), Box<dyn std::error::Error>> {
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();
let mut header = StoredHeader::arbitrary(&mut unstructured).unwrap();

header.header.number = Some(block_number);
header.header.base_fee_per_gas = Some(base_fee);
Expand Down Expand Up @@ -177,10 +177,9 @@ impl MongoFuzzer {
pub fn add_random_transactions(&mut self, n_transactions: usize) -> Result<(), Box<dyn std::error::Error>> {
for i in 0..n_transactions {
// Build a transaction using the random byte size.
let mut 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(&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.
//
Expand Down Expand Up @@ -240,7 +239,7 @@ impl MongoFuzzer {
fn generate_transaction_header(&self, transaction: &Transaction) -> StoredHeader {
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();
let mut header = StoredHeader::arbitrary(&mut unstructured).unwrap();

header.header.hash = transaction.block_hash;
header.header.number = transaction.block_number;
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/txpool_api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::used_underscore_binding)]
#![cfg(feature = "testing")]
use arbitrary::Arbitrary;
use jsonrpsee::server::ServerHandle;
use kakarot_rpc::{
providers::eth_provider::database::types::transaction::StoredPendingTransaction,
Expand Down Expand Up @@ -28,8 +29,7 @@ async fn initial_setup(katana: Katana) -> (SocketAddr, ServerHandle, Katana) {
// Generate 10 pending transactions and add them to the database
let mut pending_transactions = Vec::new();
for _ in 0..10 {
pending_transactions
.push(StoredPendingTransaction::arbitrary_with_optional_fields(&mut unstructured).unwrap().tx);
pending_transactions.push(StoredPendingTransaction::arbitrary(&mut unstructured).unwrap().tx);
}
katana.add_pending_transactions_to_database(pending_transactions).await;
(server_addr, server_handle, katana)
Expand Down

0 comments on commit af43887

Please sign in to comment.