From 004677bac8fb73e6ca913801dcb5c2b408f68366 Mon Sep 17 00:00:00 2001 From: Roman Petriv Date: Tue, 31 Oct 2023 23:23:27 +0200 Subject: [PATCH] test: add tests --- src/node/eth.rs | 39 +++++++++++++++++++++++++++++++++++++-- src/node/in_memory.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/node/eth.rs b/src/node/eth.rs index 5a57edbd..7db57930 100644 --- a/src/node/eth.rs +++ b/src/node/eth.rs @@ -1375,19 +1375,54 @@ mod tests { } #[tokio::test] - async fn test_node_run_has_genesis_block() { + async fn test_node_has_genesis_block() { let node = InMemoryNode::::default(); let block = node .get_block_by_number(BlockNumber::Latest, false) .await - .expect("failed fetching block by hash") + .expect("failed fetching block by number") .expect("no block"); assert_eq!(0, block.number.as_u64()); assert_eq!(compute_hash(0, H256::zero()), block.hash); } + #[tokio::test] + async fn test_node_creates_genesis_block_with_hash_and_zero_parent_hash() { + let node = InMemoryNode::::default(); + + let block = node + .get_block_by_hash(compute_hash(0, H256::zero()), false) + .await + .expect("failed fetching block by hash") + .expect("no block"); + + assert_eq!(block.parent_hash, H256::zero()); + } + + #[tokio::test] + async fn test_node_produces_blocks_with_parent_hash_links() { + let node = InMemoryNode::::default(); + testing::apply_tx(&node, H256::repeat_byte(0x01)); + + let genesis_block = node.get_block_by_number(BlockNumber::from(0), false) + .await + .expect("failed fetching block by number") + .expect("no block"); + let first_block = node.get_block_by_number(BlockNumber::from(1), false) + .await + .expect("failed fetching block by number") + .expect("no block"); + let second_block = node.get_block_by_number(BlockNumber::from(2), false) + .await + .expect("failed fetching block by number") + .expect("no block"); + + assert_eq!(genesis_block.hash, first_block.parent_hash); + assert_eq!(first_block.hash, second_block.parent_hash); + } + #[tokio::test] async fn test_get_block_by_hash_for_produced_block() { let node = InMemoryNode::::default(); diff --git a/src/node/in_memory.rs b/src/node/in_memory.rs index 418f87b0..5a297f9b 100644 --- a/src/node/in_memory.rs +++ b/src/node/in_memory.rs @@ -1248,7 +1248,7 @@ impl InMemoryNode { } /// Executes the given L2 transaction and returns all the VM logs. - pub fn run_l2_tx_inner( + fn run_l2_tx_inner( &self, l2_tx: L2Tx, execution_mode: TxExecutionMode, @@ -1743,4 +1743,44 @@ mod tests { Some("max priority fee per gas higher than max fee per gas".into()) ); } + + #[tokio::test] + async fn test_create_empty_block_creates_genesis_block_with_hash_and_zero_parent_hash() { + let first_block = create_empty_block::(0, 1000, 1, None); + + assert_eq!( + first_block.hash, + compute_hash(0, H256::zero()) + ); + assert_eq!( + first_block.parent_hash, + H256::zero() + ); + } + + #[tokio::test] + async fn test_create_empty_block_creates_block_with_parent_hash_link_to_prev_block() { + let first_block = create_empty_block::(0, 1000, 1, None); + let second_block = create_empty_block::(1, 1000, 1, None); + + assert_eq!( + second_block.parent_hash, + first_block.hash + ); + } + + #[tokio::test] + async fn test_create_empty_block_creates_block_with_parent_hash_link_to_provided_parent_hash() { + let first_block = create_empty_block::(0, 1000, 1, Some(compute_hash(123, H256::zero()))); + let second_block = create_empty_block::(1, 1000, 1, Some(first_block.hash)); + + assert_eq!( + first_block.parent_hash, + compute_hash(123, H256::zero()) + ); + assert_eq!( + second_block.parent_hash, + first_block.hash + ); + } }