Skip to content

Commit 004677b

Browse files
committed
test: add tests
1 parent e91d078 commit 004677b

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

src/node/eth.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -1375,19 +1375,54 @@ mod tests {
13751375
}
13761376

13771377
#[tokio::test]
1378-
async fn test_node_run_has_genesis_block() {
1378+
async fn test_node_has_genesis_block() {
13791379
let node = InMemoryNode::<HttpForkSource>::default();
13801380

13811381
let block = node
13821382
.get_block_by_number(BlockNumber::Latest, false)
13831383
.await
1384-
.expect("failed fetching block by hash")
1384+
.expect("failed fetching block by number")
13851385
.expect("no block");
13861386

13871387
assert_eq!(0, block.number.as_u64());
13881388
assert_eq!(compute_hash(0, H256::zero()), block.hash);
13891389
}
13901390

1391+
#[tokio::test]
1392+
async fn test_node_creates_genesis_block_with_hash_and_zero_parent_hash() {
1393+
let node = InMemoryNode::<HttpForkSource>::default();
1394+
1395+
let block = node
1396+
.get_block_by_hash(compute_hash(0, H256::zero()), false)
1397+
.await
1398+
.expect("failed fetching block by hash")
1399+
.expect("no block");
1400+
1401+
assert_eq!(block.parent_hash, H256::zero());
1402+
}
1403+
1404+
#[tokio::test]
1405+
async fn test_node_produces_blocks_with_parent_hash_links() {
1406+
let node = InMemoryNode::<HttpForkSource>::default();
1407+
testing::apply_tx(&node, H256::repeat_byte(0x01));
1408+
1409+
let genesis_block = node.get_block_by_number(BlockNumber::from(0), false)
1410+
.await
1411+
.expect("failed fetching block by number")
1412+
.expect("no block");
1413+
let first_block = node.get_block_by_number(BlockNumber::from(1), false)
1414+
.await
1415+
.expect("failed fetching block by number")
1416+
.expect("no block");
1417+
let second_block = node.get_block_by_number(BlockNumber::from(2), false)
1418+
.await
1419+
.expect("failed fetching block by number")
1420+
.expect("no block");
1421+
1422+
assert_eq!(genesis_block.hash, first_block.parent_hash);
1423+
assert_eq!(first_block.hash, second_block.parent_hash);
1424+
}
1425+
13911426
#[tokio::test]
13921427
async fn test_get_block_by_hash_for_produced_block() {
13931428
let node = InMemoryNode::<HttpForkSource>::default();

src/node/in_memory.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
12481248
}
12491249

12501250
/// Executes the given L2 transaction and returns all the VM logs.
1251-
pub fn run_l2_tx_inner(
1251+
fn run_l2_tx_inner(
12521252
&self,
12531253
l2_tx: L2Tx,
12541254
execution_mode: TxExecutionMode,
@@ -1743,4 +1743,44 @@ mod tests {
17431743
Some("max priority fee per gas higher than max fee per gas".into())
17441744
);
17451745
}
1746+
1747+
#[tokio::test]
1748+
async fn test_create_empty_block_creates_genesis_block_with_hash_and_zero_parent_hash() {
1749+
let first_block = create_empty_block::<TransactionVariant>(0, 1000, 1, None);
1750+
1751+
assert_eq!(
1752+
first_block.hash,
1753+
compute_hash(0, H256::zero())
1754+
);
1755+
assert_eq!(
1756+
first_block.parent_hash,
1757+
H256::zero()
1758+
);
1759+
}
1760+
1761+
#[tokio::test]
1762+
async fn test_create_empty_block_creates_block_with_parent_hash_link_to_prev_block() {
1763+
let first_block = create_empty_block::<TransactionVariant>(0, 1000, 1, None);
1764+
let second_block = create_empty_block::<TransactionVariant>(1, 1000, 1, None);
1765+
1766+
assert_eq!(
1767+
second_block.parent_hash,
1768+
first_block.hash
1769+
);
1770+
}
1771+
1772+
#[tokio::test]
1773+
async fn test_create_empty_block_creates_block_with_parent_hash_link_to_provided_parent_hash() {
1774+
let first_block = create_empty_block::<TransactionVariant>(0, 1000, 1, Some(compute_hash(123, H256::zero())));
1775+
let second_block = create_empty_block::<TransactionVariant>(1, 1000, 1, Some(first_block.hash));
1776+
1777+
assert_eq!(
1778+
first_block.parent_hash,
1779+
compute_hash(123, H256::zero())
1780+
);
1781+
assert_eq!(
1782+
second_block.parent_hash,
1783+
first_block.hash
1784+
);
1785+
}
17461786
}

0 commit comments

Comments
 (0)