Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Romsters committed Oct 31, 2023
1 parent e91d078 commit 004677b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
39 changes: 37 additions & 2 deletions src/node/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<HttpForkSource>::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::<HttpForkSource>::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::<HttpForkSource>::default();

Check warning on line 1406 in src/node/eth.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/era-test-node/era-test-node/src/node/eth.rs
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)

Check warning on line 1413 in src/node/eth.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/era-test-node/era-test-node/src/node/eth.rs
.await
.expect("failed fetching block by number")
.expect("no block");
let second_block = node.get_block_by_number(BlockNumber::from(2), false)

Check warning on line 1417 in src/node/eth.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/era-test-node/era-test-node/src/node/eth.rs
.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::<HttpForkSource>::default();
Expand Down
42 changes: 41 additions & 1 deletion src/node/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
}

/// 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,
Expand Down Expand Up @@ -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() {

Check warning on line 1748 in src/node/in_memory.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/era-test-node/era-test-node/src/node/in_memory.rs
let first_block = create_empty_block::<TransactionVariant>(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::<TransactionVariant>(0, 1000, 1, None);

Check warning on line 1763 in src/node/in_memory.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/era-test-node/era-test-node/src/node/in_memory.rs
let second_block = create_empty_block::<TransactionVariant>(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() {

Check warning on line 1773 in src/node/in_memory.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/era-test-node/era-test-node/src/node/in_memory.rs
let first_block = create_empty_block::<TransactionVariant>(0, 1000, 1, Some(compute_hash(123, H256::zero())));
let second_block = create_empty_block::<TransactionVariant>(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
);
}
}

0 comments on commit 004677b

Please sign in to comment.