Skip to content

Commit

Permalink
feat: unit test for receipts WithOtherFields (#1464)
Browse files Browse the repository at this point in the history
* feat: test_with_other_fields

* fix: test_with_other_fields test

* fix: remove insert_transaction_receipt from mongo_fuzzer

* feat: test_with_other_fields

* fix: test_with_other_fields test

* fix: remove insert_transaction_receipt from mongo_fuzzer

* fix: upsert_transaction_receipt and receipts_at

* fix: test_with_other_fields test

* fix: test_with_other_fields

---------

Co-authored-by: greged93 <82421016+greged93@users.noreply.github.com>
  • Loading branch information
eugypalu and greged93 authored Oct 22, 2024
1 parent a8117ee commit 24b6452
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/test_utils/katana/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use {
super::mongo::MongoFuzzer,
alloy_primitives::B256,
alloy_rpc_types::Header,
alloy_rpc_types::Transaction,
alloy_rpc_types::{Transaction, TransactionReceipt},
alloy_serde::WithOtherFields,
katana_node::config::{
rpc::{ApiKind, RpcConfig},
Expand Down Expand Up @@ -346,6 +346,20 @@ impl<'a> Katana {
.map(Into::into)
}

pub fn most_recent_run_out_of_resources_receipt(&self) -> Option<WithOtherFields<TransactionReceipt>> {
self.receipts
.iter()
.filter_map(|stored_receipt| {
let receipt = WithOtherFields::from(stored_receipt.clone());
if receipt.other.contains_key("isRunOutOfRessources") {
Some(receipt)
} else {
None
}
})
.max_by_key(|receipt| receipt.block_number.unwrap_or_default())
}

/// Retrieves the stored header by hash
pub fn header_by_hash(&self, hash: B256) -> Option<Header> {
self.headers.iter().find_map(
Expand Down
34 changes: 33 additions & 1 deletion src/test_utils/mongo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::providers::eth_provider::{
},
};
use alloy_primitives::{B256, U256};
use alloy_rpc_types::Transaction;
use alloy_rpc_types::{Transaction, TransactionReceipt};
use alloy_serde::WithOtherFields;
use arbitrary::Arbitrary;
use mongodb::{
bson::{self, doc, Document},
Expand Down Expand Up @@ -187,6 +188,8 @@ impl MongoFuzzer {
self.receipts.push(receipt);
}

self.add_random_transaction_with_other_field()?;

// At the end of our transaction list, for our tests, we need to add a block header with a base fee.
let mut header_with_base_fee = StoredHeader::arbitrary(&mut arbitrary::Unstructured::new(
&(0..self.rnd_bytes_size).map(|_| rand::random::<u8>()).collect::<Vec<_>>(),
Expand All @@ -201,6 +204,35 @@ impl MongoFuzzer {
Ok(())
}

pub fn add_random_transaction_with_other_field(&mut self) -> Result<(), Box<dyn std::error::Error>> {
// Build a transaction using the random byte size.
let transaction = StoredTransaction::arbitrary(&mut arbitrary::Unstructured::new(
&(0..self.rnd_bytes_size).map(|_| rand::random::<u8>()).collect::<Vec<_>>(),
))?;

// Generate a receipt for the transaction.
let receipt = self.generate_transaction_receipt(&transaction.tx);
// add an isRunOutOfRessources field to the receipt
let mut receipt_with_other_fields: WithOtherFields<TransactionReceipt> = receipt.into();
receipt_with_other_fields.other.insert("isRunOutOfRessources".to_string(), serde_json::Value::Bool(true));

let stored_receipt = StoredTransactionReceipt { receipt: receipt_with_other_fields };

// Convert the receipt into a vector of logs and append them to the existing logs collection.
self.logs.append(&mut Vec::from(stored_receipt.clone()));

// Generate a header for the transaction and add it to the headers collection.
self.headers.push(self.generate_transaction_header(&transaction.tx));

// Add the transaction to the transactions collection.
self.transactions.push(transaction);

// Add the receipt to the receipts collection.
self.receipts.push(stored_receipt);

Ok(())
}

/// Generates a transaction receipt based on the given transaction.
fn generate_transaction_receipt(&self, transaction: &Transaction) -> StoredTransactionReceipt {
let bytes: Vec<u8> = (0..self.rnd_bytes_size).map(|_| rand::random()).collect();
Expand Down
14 changes: 14 additions & 0 deletions tests/tests/eth_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,3 +1420,17 @@ async fn test_transaction_by_hash(#[future] katana_empty: Katana, _setup: ()) {
transaction.tx
);
}

#[rstest]
#[awt]
#[tokio::test(flavor = "multi_thread")]
async fn test_with_other_fields(#[future] katana: Katana, _setup: ()) {
let eth_provider = katana.eth_provider();
let run_out_of_resources_receipt = katana.most_recent_run_out_of_resources_receipt().unwrap();

let receipt_from_db =
eth_provider.transaction_receipt(run_out_of_resources_receipt.transaction_hash).await.unwrap();
// Verify the receipt
assert_eq!(run_out_of_resources_receipt.other.get("isRunOutOfRessources"), Some(&serde_json::Value::Bool(true)));
assert_eq!(receipt_from_db.unwrap(), run_out_of_resources_receipt);
}

0 comments on commit 24b6452

Please sign in to comment.