Skip to content

Commit

Permalink
benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
0xForerunner committed Oct 17, 2024
1 parent 642bcdf commit 8cb8957
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 150 deletions.
66 changes: 64 additions & 2 deletions world-chain-builder/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion world-chain-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ dotenvy = "0.15.7"
tikv-jemallocator = { version = "0.6.0", optional = true }
bytes = "1.7.2"
hex = "0.4.3"
tempfile = "3"

[dev-dependencies]
criterion = { version = "0.5", features = ["async_tokio"] }
serial_test = "3"
tempfile = "3"
test-case = "3"
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
reth-provider = { git = "https://github.com/paradigmxyz/reth", rev = "c35b8be", features = [
Expand All @@ -114,3 +115,7 @@ jemalloc = ["tikv-jemallocator"]
[[bin]]
name = "world-chain-builder"
path = "bin/world-chain-builder.rs"

[[bench]]
name = "validate_transaction"
harness = false
192 changes: 192 additions & 0 deletions world-chain-builder/benches/validate_transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
use std::future::Future;

use criterion::{criterion_group, criterion_main, Criterion};
use reth::transaction_pool::blobstore::InMemoryBlobStore;
use reth::transaction_pool::{Pool, PoolTransaction as _, TransactionPool, TransactionValidator};
use reth_primitives::{BlockBody, SealedBlock, SealedHeader};
use reth_provider::test_utils::{ExtendedAccount, MockEthProvider};
use semaphore::Field;
use tokio::runtime::{Builder, Runtime};
use world_chain_builder::pool::ordering::WorldChainOrdering;
use world_chain_builder::pool::root::{LATEST_ROOT_SLOT, OP_WORLD_ID};
use world_chain_builder::pool::tx::WorldChainPooledTransaction;
use world_chain_builder::pool::validator::WorldChainTransactionValidator;
use world_chain_builder::test::{
get_non_pbh_transaction, get_pbh_transaction, world_chain_validator,
};

type PoolType = Pool<
WorldChainTransactionValidator<MockEthProvider, WorldChainPooledTransaction>,
WorldChainOrdering<WorldChainPooledTransaction>,
InMemoryBlobStore,
>;

#[derive(Clone)]
struct Setup {
pool: PoolType,
transaction: WorldChainPooledTransaction,
}

fn async_setup<F>(rt: &Runtime, f: F) -> F::Output
where
F: Future,
{
rt.block_on(f)
}

fn validator_setup() -> WorldChainTransactionValidator<MockEthProvider, WorldChainPooledTransaction>
{
let validator = world_chain_validator();
let transaction = get_pbh_transaction();
validator.inner().client().add_account(
transaction.sender(),
ExtendedAccount::new(transaction.nonce(), alloy_primitives::U256::MAX),
);
// Insert a world id root into the OpWorldId Account
validator.inner().client().add_account(
OP_WORLD_ID,
ExtendedAccount::new(0, alloy_primitives::U256::ZERO).extend_storage(vec![(
LATEST_ROOT_SLOT.into(),
transaction.pbh_payload.clone().unwrap().root,
)]),
);

let header = SealedHeader::default();
let body = BlockBody::default();
let block = SealedBlock::new(header, body);

// Propogate the block to the root validator
validator.on_new_head_block(&block);

validator
}

fn pool_setup() -> PoolType {
let validator = validator_setup();
let ordering = WorldChainOrdering::default();

Pool::new(
validator.clone(),
ordering,
InMemoryBlobStore::default(),
Default::default(),
)
}

fn non_pbh_setup() -> Setup {
let pool = pool_setup();
let transaction = get_non_pbh_transaction();

Setup { pool, transaction }
}

fn pbh_setup() -> Setup {
let pool = pool_setup();
let transaction = get_pbh_transaction();

Setup { pool, transaction }
}

fn spoofed_nullifier_setup() -> Setup {
let pool = pool_setup();
let mut transaction = get_pbh_transaction();
let pbh_payload = transaction.pbh_payload.as_mut().unwrap();
pbh_payload.nullifier_hash = Field::default();
Setup { pool, transaction }
}

async fn repeat_non_pbh_setup() -> Setup {
let setup = non_pbh_setup();
setup
.pool
.add_external_transaction(setup.transaction.clone())
.await
.unwrap();
setup
}

async fn repeat_pbh_setup() -> Setup {
let setup = pbh_setup();
setup
.pool
.add_external_transaction(setup.transaction.clone())
.await
.unwrap();
setup
}

async fn run(setup: Setup) {
setup
.pool
.add_external_transaction(setup.transaction)
.await
.unwrap();
}

async fn run_err(setup: Setup) {
setup
.pool
.add_external_transaction(setup.transaction)
.await
.unwrap_err();
}

fn non_pbh_bench(c: &mut Criterion) {
let rt = Builder::new_multi_thread().enable_all().build().unwrap();
c.bench_function("Non PBH Transaction", |b| {
b.to_async(&rt)
.iter_batched(non_pbh_setup, run, criterion::BatchSize::SmallInput)
});
}

fn repeat_non_pbh_bench(c: &mut Criterion) {
c.bench_function("Repeat Non PBH Transaction", |b| {
let rt = Builder::new_multi_thread().enable_all().build().unwrap();
let setup = async_setup(&rt, repeat_non_pbh_setup());
b.to_async(rt)
.iter_batched(|| setup.clone(), run_err, criterion::BatchSize::SmallInput)
});
}

fn pbh_bench(c: &mut Criterion) {
let rt = Builder::new_multi_thread().enable_all().build().unwrap();
c.bench_function("PBH Transaction", |b| {
b.to_async(&rt)
.iter_batched(pbh_setup, run, criterion::BatchSize::SmallInput)
});
}

fn repeat_pbh_bench(c: &mut Criterion) {
c.bench_function("Repeat PBH Transaction", |b| {
let rt = Builder::new_multi_thread().enable_all().build().unwrap();
let setup = async_setup(&rt, repeat_pbh_setup());
b.to_async(rt)
.iter_batched(|| setup.clone(), run_err, criterion::BatchSize::SmallInput)
});
}

fn spoofed_nullifier_bench(c: &mut Criterion) {
c.bench_function("Spoofed Nullifier", |b| {
let rt = Builder::new_multi_thread().enable_all().build().unwrap();
b.to_async(rt).iter_batched(
spoofed_nullifier_setup,
run_err,
criterion::BatchSize::SmallInput,
)
});
}

fn criterion_config() -> Criterion {
Criterion::default().sample_size(30)
}

criterion_group!(
name = benches;
config = criterion_config();
targets = non_pbh_bench,
repeat_non_pbh_bench,
pbh_bench,
repeat_pbh_bench,
spoofed_nullifier_bench
);
criterion_main!(benches);
3 changes: 1 addition & 2 deletions world-chain-builder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#[cfg(test)]
pub mod e2e_tests;
pub mod node;
pub mod payload;
pub mod pbh;
pub mod pool;
pub mod primitives;
pub mod rpc;
pub mod test;
2 changes: 1 addition & 1 deletion world-chain-builder/src/pool/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ mod tests {

use super::*;
use reth_primitives::Block;
fn world_chain_root_validator() -> eyre::Result<WorldChainRootValidator<MockEthProvider>> {
pub fn world_chain_root_validator() -> eyre::Result<WorldChainRootValidator<MockEthProvider>> {
let client = MockEthProvider::default();
let root_validator = WorldChainRootValidator::new(client)?;
Ok(root_validator)
Expand Down
Loading

0 comments on commit 8cb8957

Please sign in to comment.