|
| 1 | +use alloy_primitives::Bytes; |
| 2 | +use criterion::{ |
| 3 | + criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion, |
| 4 | +}; |
| 5 | +use proptest::{prelude::*, strategy::ValueTree as _, test_runner::TestRunner}; |
| 6 | + |
| 7 | +use impls::SszTransactionProof; |
| 8 | + |
| 9 | +criterion_main!(ssz_proof); |
| 10 | +criterion_group!(ssz_proof, ssz_proof_bench); |
| 11 | + |
| 12 | +fn ssz_proof_bench(c: &mut Criterion) { |
| 13 | + let mut group = c.benchmark_group("ssz_proof"); |
| 14 | + |
| 15 | + // Start with asserting equivalence of all implementations. |
| 16 | + impls::assert_equivalence(); |
| 17 | + |
| 18 | + for num_txs in [100, 500, 1_000] { |
| 19 | + let target = num_txs - 1; |
| 20 | + |
| 21 | + for tx_size in [128, 1_024] { |
| 22 | + let mut runner = TestRunner::deterministic(); |
| 23 | + let txs = generate_test_data(&mut runner, num_txs, tx_size); |
| 24 | + |
| 25 | + run_bench::<impls::VanillaSszTxProof>(&mut group, &txs, target); |
| 26 | + run_bench::<impls::VanillaBufferedSszTxProof>(&mut group, &txs, target); |
| 27 | + run_bench::<impls::CompactSszTxProof>(&mut group, &txs, target); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +fn run_bench<T: SszTransactionProof>( |
| 33 | + group: &mut BenchmarkGroup<'_, WallTime>, |
| 34 | + txs: &[Bytes], |
| 35 | + target: usize, |
| 36 | +) { |
| 37 | + let tx_size = txs.first().unwrap().0.len(); |
| 38 | + let id = format!( |
| 39 | + "{} | num txs {} | tx size {} bytes", |
| 40 | + T::description(), |
| 41 | + txs.len(), |
| 42 | + tx_size |
| 43 | + ); |
| 44 | + group.bench_function(id, |b| { |
| 45 | + b.iter_with_setup( |
| 46 | + || T::default(), |
| 47 | + |mut gen| { |
| 48 | + gen.generate(txs, target); |
| 49 | + }, |
| 50 | + ) |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +fn generate_test_data(runner: &mut TestRunner, num_txs: usize, tx_size: usize) -> Vec<Bytes> { |
| 55 | + proptest::collection::vec(proptest::collection::vec(any::<u8>(), tx_size), num_txs) |
| 56 | + .new_tree(runner) |
| 57 | + .unwrap() |
| 58 | + .current() |
| 59 | + .into_iter() |
| 60 | + .map(Bytes::from) |
| 61 | + .collect::<Vec<_>>() |
| 62 | +} |
| 63 | + |
| 64 | +mod impls { |
| 65 | + use super::*; |
| 66 | + use alloy_primitives::{Bytes, B256}; |
| 67 | + use rbuilder_primitives::mev_boost::ssz_roots::{ |
| 68 | + sha_pair, tx_ssz_leaf_root, CompactSszTransactionTree, |
| 69 | + }; |
| 70 | + |
| 71 | + const TREE_DEPTH: usize = 20; // log₂(MAX_TRANSACTIONS_PER_PAYLOAD) |
| 72 | + const MAX_CHUNK_COUNT: usize = 1 << TREE_DEPTH; |
| 73 | + |
| 74 | + pub fn assert_equivalence() { |
| 75 | + let num_txs = 100; |
| 76 | + let proof_target = num_txs - 1; |
| 77 | + let tx_size = 1_024; |
| 78 | + let mut runner = TestRunner::deterministic(); |
| 79 | + |
| 80 | + let mut vanilla = VanillaSszTxProof::default(); |
| 81 | + let mut vanilla_buf = VanillaBufferedSszTxProof::default(); |
| 82 | + let mut compact = CompactSszTxProof::default(); |
| 83 | + for _ in 0..100 { |
| 84 | + let txs = generate_test_data(&mut runner, num_txs, tx_size); |
| 85 | + let expected = vanilla.generate(&txs, proof_target); |
| 86 | + assert_eq!(expected, vanilla_buf.generate(&txs, proof_target)); |
| 87 | + assert_eq!(expected, compact.generate(&txs, proof_target)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + pub trait SszTransactionProof: Default { |
| 92 | + fn description() -> &'static str; |
| 93 | + |
| 94 | + fn generate(&mut self, txs: &[Bytes], target: usize) -> Vec<B256>; |
| 95 | + } |
| 96 | + |
| 97 | + /// === VanillaSszTransactionProof === |
| 98 | + #[derive(Default)] |
| 99 | + pub struct VanillaSszTxProof; |
| 100 | + |
| 101 | + impl SszTransactionProof for VanillaSszTxProof { |
| 102 | + fn description() -> &'static str { |
| 103 | + "vanilla" |
| 104 | + } |
| 105 | + |
| 106 | + fn generate(&mut self, txs: &[Bytes], target: usize) -> Vec<B256> { |
| 107 | + vanilla_transaction_proof_ssz(txs, target, &mut Vec::new(), &mut Vec::new()) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /// === VanillaBufferedSszTransactionProof === |
| 112 | + #[derive(Default)] |
| 113 | + pub struct VanillaBufferedSszTxProof { |
| 114 | + current_buf: Vec<B256>, |
| 115 | + next_buf: Vec<B256>, |
| 116 | + } |
| 117 | + |
| 118 | + impl SszTransactionProof for VanillaBufferedSszTxProof { |
| 119 | + fn description() -> &'static str { |
| 120 | + "vanilla with buffers" |
| 121 | + } |
| 122 | + |
| 123 | + fn generate(&mut self, txs: &[Bytes], target: usize) -> Vec<B256> { |
| 124 | + vanilla_transaction_proof_ssz(txs, target, &mut self.current_buf, &mut self.next_buf) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + fn vanilla_transaction_proof_ssz( |
| 129 | + txs: &[Bytes], |
| 130 | + target: usize, |
| 131 | + current_buf: &mut Vec<B256>, |
| 132 | + next_buf: &mut Vec<B256>, |
| 133 | + ) -> Vec<B256> { |
| 134 | + current_buf.clear(); |
| 135 | + for idx in 0..MAX_CHUNK_COUNT { |
| 136 | + let leaf = txs |
| 137 | + .get(idx) |
| 138 | + .map(|tx| tx_ssz_leaf_root(&tx)) |
| 139 | + .unwrap_or(B256::ZERO); |
| 140 | + current_buf.insert(idx, leaf); |
| 141 | + } |
| 142 | + |
| 143 | + let mut branch = Vec::new(); |
| 144 | + let (current_level, next_level) = (current_buf, next_buf); |
| 145 | + let mut current_index = target; |
| 146 | + |
| 147 | + for _level in 0..TREE_DEPTH { |
| 148 | + let sibling_index = current_index ^ 1; |
| 149 | + branch.push(current_level[sibling_index]); |
| 150 | + |
| 151 | + next_level.clear(); |
| 152 | + for i in (0..current_level.len()).step_by(2) { |
| 153 | + let left = current_level[i]; |
| 154 | + let right = current_level[i + 1]; |
| 155 | + next_level.push(sha_pair(&left, &right)); |
| 156 | + } |
| 157 | + |
| 158 | + std::mem::swap(current_level, next_level); |
| 159 | + current_index /= 2; |
| 160 | + |
| 161 | + if current_level.len() == 1 { |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + branch |
| 167 | + } |
| 168 | + |
| 169 | + /// === CompactSszTxProof === |
| 170 | + #[derive(Default)] |
| 171 | + pub struct CompactSszTxProof; |
| 172 | + |
| 173 | + impl SszTransactionProof for CompactSszTxProof { |
| 174 | + fn description() -> &'static str { |
| 175 | + "compact" |
| 176 | + } |
| 177 | + |
| 178 | + fn generate(&mut self, txs: &[Bytes], target: usize) -> Vec<B256> { |
| 179 | + let mut leaves = Vec::with_capacity(txs.len()); |
| 180 | + for tx in txs { |
| 181 | + leaves.push(tx_ssz_leaf_root(tx)); |
| 182 | + } |
| 183 | + CompactSszTransactionTree::from_leaves(leaves).proof(target) |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments