From 33985d28927542d8284b2e9496e778233d63d277 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Sat, 17 Aug 2024 10:03:53 -0700 Subject: [PATCH] chain-state: add unit test for `to_chain_notification` (#10373) Co-authored-by: Roman Krasiuk --- crates/chain-state/src/in_memory.rs | 55 ++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index 68329be09d..c2e1f96a8b 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -802,7 +802,7 @@ mod tests { use rand::Rng; use reth_errors::ProviderResult; use reth_primitives::{ - Account, BlockNumber, Bytecode, Bytes, Receipt, StorageKey, StorageValue, + Account, BlockNumber, Bytecode, Bytes, Receipt, Requests, StorageKey, StorageValue, }; use reth_storage_api::{ AccountReader, BlockHashReader, StateProofProvider, StateProvider, StateRootProvider, @@ -1278,4 +1278,57 @@ mod tests { assert_eq!(block_state_chain.len(), 1); assert_eq!(block_state_chain[0].block().block.number, 1); } + + #[test] + fn test_to_chain_notification() { + // Generate 4 blocks + let mut test_block_builder = TestBlockBuilder::default(); + let block0 = test_block_builder.get_executed_block_with_number(0, B256::random()); + let block1 = test_block_builder.get_executed_block_with_number(1, block0.block.hash()); + let block1a = test_block_builder.get_executed_block_with_number(1, block0.block.hash()); + let block2 = test_block_builder.get_executed_block_with_number(2, block1.block.hash()); + let block2a = test_block_builder.get_executed_block_with_number(2, block1.block.hash()); + + let sample_execution_outcome = ExecutionOutcome { + receipts: Receipts::from_iter([vec![], vec![]]), + requests: vec![Requests::default(), Requests::default()], + ..Default::default() + }; + + // Test commit notification + let chain_commit = NewCanonicalChain::Commit { new: vec![block0.clone(), block1.clone()] }; + + assert_eq!( + chain_commit.to_chain_notification(), + CanonStateNotification::Commit { + new: Arc::new(Chain::new( + vec![block0.sealed_block_with_senders(), block1.sealed_block_with_senders()], + sample_execution_outcome.clone(), + None + )) + } + ); + + // Test reorg notification + let chain_reorg = NewCanonicalChain::Reorg { + new: vec![block1a.clone(), block2a.clone()], + old: vec![block1.clone(), block2.clone()], + }; + + assert_eq!( + chain_reorg.to_chain_notification(), + CanonStateNotification::Reorg { + old: Arc::new(Chain::new( + vec![block1.sealed_block_with_senders(), block2.sealed_block_with_senders()], + sample_execution_outcome.clone(), + None + )), + new: Arc::new(Chain::new( + vec![block1a.sealed_block_with_senders(), block2a.sealed_block_with_senders()], + sample_execution_outcome, + None + )) + } + ); + } }