chain-state: add unit test for to_chain_notification (#10373)

Co-authored-by: Roman Krasiuk <rokrassyuk@gmail.com>
This commit is contained in:
Thomas Coratger
2024-08-17 10:03:53 -07:00
committed by GitHub
parent 57c99e80a5
commit 33985d2892

View File

@ -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
))
}
);
}
}