Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add support for seeded rng #3270

Merged
merged 9 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

**Modular, contributor-friendly and blazing-fast implementation of the Ethereum protocol**

![](./assets/reth.jpg)
![reth](./assets/reth.jpg)

**[Install](https://paradigmxyz.github.io/reth/installation/installation.html)**
| [User Book](https://paradigmxyz.github.io/reth)
Expand Down 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 @@ -207,21 +207,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 @@ -232,11 +234,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 @@ -259,11 +263,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 @@ -291,11 +297,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 @@ -323,11 +331,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 @@ -343,11 +353,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 @@ -363,14 +375,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 @@ -412,11 +426,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 @@ -446,11 +462,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