Skip to content

Commit

Permalink
test: add support for seeded rng (paradigmxyz#3270)
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg authored and yuichiroaoki committed Jun 22, 2023
1 parent 7b7f768 commit 8dedff5
Show file tree
Hide file tree
Showing 23 changed files with 323 additions and 175 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ env:
RUSTFLAGS: -D warnings
CARGO_TERM_COLOR: always
GETH_BUILD: 1.12.0-e501b3b0
SEED: rustethereumethereumrust

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
env:
RUSTFLAGS: -D warnings
CARGO_TERM_COLOR: always
SEED: rustethereumethereumrust

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,21 @@ cargo test --workspace

# With Geth
cargo test --workspace --features geth-tests

# With Ethereum Foundation tests
#
# Note: Requires cloning https://github.com/ethereum/tests
#
# cd testing/ef-tests && git clone https://github.com/ethereum/tests ethereum-tests
cargo test --workspace --features ef-tests
```

We recommend using [`cargo nextest`](https://nexte.st/) to speed up testing. With nextest installed, simply substitute `cargo test` with `cargo nextest run`.

> **Note**
>
> Some tests use random number generators to generate test data. If you want to use a deterministic seed, you can set the `SEED` environment variable.
## Getting Help

If you have any questions, first see if the answer to your question can be found in the [book][book].
Expand Down
96 changes: 57 additions & 39 deletions crates/blockchain-tree/src/block_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,23 @@ impl BlockBuffer {

#[cfg(test)]
mod tests {
use reth_interfaces::test_utils::generators;
use std::collections::HashMap;

use reth_interfaces::test_utils::generators::random_block;
use reth_interfaces::test_utils::generators::{random_block, Rng};
use reth_primitives::{BlockHash, BlockNumHash, SealedBlockWithSenders};

use crate::BlockBuffer;

fn create_block(number: u64, parent: BlockHash) -> SealedBlockWithSenders {
let block = random_block(number, Some(parent), None, None);
fn create_block<R: Rng>(rng: &mut R, number: u64, parent: BlockHash) -> SealedBlockWithSenders {
let block = random_block(rng, number, Some(parent), None, None);
block.seal_with_senders().unwrap()
}

#[test]
fn simple_insertion() {
let block1 = create_block(10, BlockHash::random());
let mut rng = generators::rng();
let block1 = create_block(&mut rng, 10, BlockHash::random());
let mut buffer = BlockBuffer::new(3);

buffer.insert_block(block1.clone());
Expand All @@ -240,11 +242,13 @@ mod tests {

#[test]
fn take_all_chain_of_childrens() {
let mut rng = generators::rng();

let main_parent = BlockNumHash::new(9, BlockHash::random());
let block1 = create_block(10, main_parent.hash);
let block2 = create_block(11, block1.hash);
let block3 = create_block(12, block2.hash);
let block4 = create_block(14, BlockHash::random());
let block1 = create_block(&mut rng, 10, main_parent.hash);
let block2 = create_block(&mut rng, 11, block1.hash);
let block3 = create_block(&mut rng, 12, block2.hash);
let block4 = create_block(&mut rng, 14, BlockHash::random());

let mut buffer = BlockBuffer::new(5);

Expand All @@ -267,11 +271,13 @@ mod tests {

#[test]
fn take_all_multi_level_childrens() {
let mut rng = generators::rng();

let main_parent = BlockNumHash::new(9, BlockHash::random());
let block1 = create_block(10, main_parent.hash);
let block2 = create_block(11, block1.hash);
let block3 = create_block(11, block1.hash);
let block4 = create_block(12, block2.hash);
let block1 = create_block(&mut rng, 10, main_parent.hash);
let block2 = create_block(&mut rng, 11, block1.hash);
let block3 = create_block(&mut rng, 11, block1.hash);
let block4 = create_block(&mut rng, 12, block2.hash);

let mut buffer = BlockBuffer::new(5);

Expand Down Expand Up @@ -299,11 +305,13 @@ mod tests {

#[test]
fn take_self_with_childs() {
let mut rng = generators::rng();

let main_parent = BlockNumHash::new(9, BlockHash::random());
let block1 = create_block(10, main_parent.hash);
let block2 = create_block(11, block1.hash);
let block3 = create_block(11, block1.hash);
let block4 = create_block(12, block2.hash);
let block1 = create_block(&mut rng, 10, main_parent.hash);
let block2 = create_block(&mut rng, 11, block1.hash);
let block3 = create_block(&mut rng, 11, block1.hash);
let block4 = create_block(&mut rng, 12, block2.hash);

let mut buffer = BlockBuffer::new(5);

Expand Down Expand Up @@ -331,11 +339,13 @@ mod tests {

#[test]
fn clean_chain_of_children() {
let mut rng = generators::rng();

let main_parent = BlockNumHash::new(9, BlockHash::random());
let block1 = create_block(10, main_parent.hash);
let block2 = create_block(11, block1.hash);
let block3 = create_block(12, block2.hash);
let block4 = create_block(14, BlockHash::random());
let block1 = create_block(&mut rng, 10, main_parent.hash);
let block2 = create_block(&mut rng, 11, block1.hash);
let block3 = create_block(&mut rng, 12, block2.hash);
let block4 = create_block(&mut rng, 14, BlockHash::random());

let mut buffer = BlockBuffer::new(5);

Expand All @@ -351,11 +361,13 @@ mod tests {

#[test]
fn clean_all_multi_level_childrens() {
let mut rng = generators::rng();

let main_parent = BlockNumHash::new(9, BlockHash::random());
let block1 = create_block(10, main_parent.hash);
let block2 = create_block(11, block1.hash);
let block3 = create_block(11, block1.hash);
let block4 = create_block(12, block2.hash);
let block1 = create_block(&mut rng, 10, main_parent.hash);
let block2 = create_block(&mut rng, 11, block1.hash);
let block3 = create_block(&mut rng, 11, block1.hash);
let block4 = create_block(&mut rng, 12, block2.hash);

let mut buffer = BlockBuffer::new(5);

Expand All @@ -371,14 +383,16 @@ mod tests {

#[test]
fn clean_multi_chains() {
let mut rng = generators::rng();

let main_parent = BlockNumHash::new(9, BlockHash::random());
let block1 = create_block(10, main_parent.hash);
let block1a = create_block(10, main_parent.hash);
let block2 = create_block(11, block1.hash);
let block2a = create_block(11, block1.hash);
let random_block1 = create_block(10, BlockHash::random());
let random_block2 = create_block(11, BlockHash::random());
let random_block3 = create_block(12, BlockHash::random());
let block1 = create_block(&mut rng, 10, main_parent.hash);
let block1a = create_block(&mut rng, 10, main_parent.hash);
let block2 = create_block(&mut rng, 11, block1.hash);
let block2a = create_block(&mut rng, 11, block1.hash);
let random_block1 = create_block(&mut rng, 10, BlockHash::random());
let random_block2 = create_block(&mut rng, 11, BlockHash::random());
let random_block3 = create_block(&mut rng, 12, BlockHash::random());

let mut buffer = BlockBuffer::new(10);

Expand Down Expand Up @@ -420,11 +434,13 @@ mod tests {

#[test]
fn evict_with_gap() {
let mut rng = generators::rng();

let main_parent = BlockNumHash::new(9, BlockHash::random());
let block1 = create_block(10, main_parent.hash);
let block2 = create_block(11, block1.hash);
let block3 = create_block(12, block2.hash);
let block4 = create_block(13, BlockHash::random());
let block1 = create_block(&mut rng, 10, main_parent.hash);
let block2 = create_block(&mut rng, 11, block1.hash);
let block3 = create_block(&mut rng, 12, block2.hash);
let block4 = create_block(&mut rng, 13, BlockHash::random());

let mut buffer = BlockBuffer::new(3);

Expand Down Expand Up @@ -454,11 +470,13 @@ mod tests {

#[test]
fn simple_eviction() {
let mut rng = generators::rng();

let main_parent = BlockNumHash::new(9, BlockHash::random());
let block1 = create_block(10, main_parent.hash);
let block2 = create_block(11, block1.hash);
let block3 = create_block(12, block2.hash);
let block4 = create_block(13, BlockHash::random());
let block1 = create_block(&mut rng, 10, main_parent.hash);
let block2 = create_block(&mut rng, 11, block1.hash);
let block3 = create_block(&mut rng, 12, block2.hash);
let block4 = create_block(&mut rng, 13, BlockHash::random());

let mut buffer = BlockBuffer::new(3);

Expand Down
Loading

0 comments on commit 8dedff5

Please sign in to comment.