Repository navigation
Expand file tree
/
Copy pathvalidation_cost.rs
More file actions
252 lines (232 loc) · 10.4 KB
/
Copy pathvalidation_cost.rs
File metadata and controls
252 lines (232 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! What a real block costs *outside* execution.
//!
//! `loadtest` measures execution and the commit, and deliberately nothing
//! else: it hands `execute_block` the senders rather than recovering them,
//! and builds a header with `difficulty: 1` and no merged-mining fields.
//! That is the right shape for an instrument -- adding the rest would put
//! noise between a change and its effect -- but it means its transactions
//! per second are a ceiling for the state half of a block, not a node's
//! throughput.
//!
//! So measure the missing pieces on their own, on real mainnet data, and
//! add them back arithmetically.
//!
//! Two pieces are measured here:
//!
//! * **Header verification**, the full `HeaderVerifier::default_rsk` set
//! against the real parent -- gas used and limit bounds, extra data,
//! block number, timestamp, parent gas limit, previous minimum gas
//! price, difficulty, and the merged-mining proof of work.
//! * **Sender recovery**, `Transaction::recover_sender`, which a node
//! runs once per transaction and `loadtest` never runs at all. Timed
//! twice: once as the node does it today, through the pure-Rust
//! `k256`, and once through `secp256k1` -- the rust-bitcoin bindings
//! to Pieter Wuille's C library, reached here via the `bitcoin`
//! crate, which already depends on it. Both must agree on every
//! address or the comparison is meaningless.
//!
//! Both are measured on headers and transactions read from the running
//! node's own store, read-only.
//!
//! Usage: cargo run --release --example validation_cost -- [data-dir] [iters]
use anyhow::{Context, Result};
use rustock_core::config::ChainConfig;
use rustock_core::{Header, Transaction};
use rustock_core::validation::HeaderVerifier;
use rustock_storage::BlockStore;
use std::sync::Arc;
use std::time::Instant;
const MAINNET_CHAIN_ID: u64 = 30;
/// Median and mean of a sample, in microseconds.
fn stats(mut us: Vec<f64>) -> (f64, f64, f64, f64) {
us.sort_by(|a, b| a.partial_cmp(b).unwrap());
let n = us.len();
let median = if n.is_multiple_of(2) { (us[n / 2 - 1] + us[n / 2]) / 2.0 } else { us[n / 2] };
let mean = us.iter().sum::<f64>() / n as f64;
(median, mean, us[0], us[n - 1])
}
/// `Transaction::recover_sender`, done through libsecp256k1 instead.
///
/// Deliberately the same shape as the `k256` version in
/// `crates/core/src/types/transaction.rs`, down to the keccak of the
/// uncompressed key's tail, so the two are timed doing the same job.
fn recover_with_libsecp(
secp: &bitcoin::secp256k1::Secp256k1<bitcoin::secp256k1::All>,
tx: &Transaction,
chain_id: u64,
) -> Option<alloy_primitives::Address> {
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, RecoveryId};
use bitcoin::secp256k1::Message;
use sha3::{Digest, Keccak256};
let hash = tx.signing_hash(chain_id);
let recovery_id = if tx.is_eip155(chain_id) {
let rid = tx.v as i64 - chain_id as i64 * 2 - 35;
if !(0..=1).contains(&rid) {
return None;
}
rid as i32
} else {
if tx.v != 27 && tx.v != 28 {
return None;
}
(tx.v - 27) as i32
};
let mut sig_bytes = [0u8; 64];
sig_bytes[..32].copy_from_slice(&tx.r.to_be_bytes::<32>());
sig_bytes[32..].copy_from_slice(&tx.s.to_be_bytes::<32>());
let recid = RecoveryId::from_i32(recovery_id).ok()?;
let sig = RecoverableSignature::from_compact(&sig_bytes, recid).ok()?;
let msg = Message::from_digest(hash.0);
let key = secp.recover_ecdsa(&msg, &sig).ok()?;
let uncompressed = key.serialize_uncompressed();
let digest = Keccak256::digest(&uncompressed[1..]);
Some(alloy_primitives::Address::from_slice(&digest[12..]))
}
fn main() -> Result<()> {
let mut args = std::env::args().skip(1);
let dir = args.next().unwrap_or_else(|| "/var/lib/rustock".into());
let iters: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(200);
let store = BlockStore::open_read_only(&dir)
.with_context(|| format!("opening the block store at {dir} read-only"))?;
let head = store.head()?.context("the store has no head")?;
let head_header = store.header(head)?.context("the head header is missing")?;
let top = head_header.number;
println!("store {dir}\n head #{top}");
// Walk back from the head collecting (header, parent) pairs that carry
// merged-mining fields, and every transaction found along the way.
// Mainnet blocks are mostly near-empty, so the transactions have to be
// gathered over a range rather than taken from one block.
let mut pairs: Vec<(Header, Header)> = Vec::new();
let mut txs: Vec<Transaction> = Vec::new();
let mut blocks_seen = 0u64;
let mut txs_seen = 0u64;
let scan = 2_000u64;
for n in (top.saturating_sub(scan)..=top).rev() {
let Some(hash) = store.canonical_hash(n)? else { continue };
let Some(header) = store.header(hash)? else { continue };
let Some(parent) = store.header(header.parent_hash)? else { continue };
blocks_seen += 1;
if let Some((block_txs, _)) = store.body(hash)? {
txs_seen += block_txs.len() as u64;
if txs.len() < iters {
txs.extend(block_txs);
}
}
if pairs.len() < iters && header.bitcoin_merged_mining_header.is_some() {
pairs.push((header, parent));
}
if pairs.len() >= iters && txs.len() >= iters {
break;
}
}
anyhow::ensure!(!pairs.is_empty(), "no merged-mined headers in the last {scan} blocks");
anyhow::ensure!(!txs.is_empty(), "no transactions in the last {scan} blocks");
println!(
" sampled {} headers and {} transactions over {blocks_seen} blocks",
pairs.len(),
txs.len()
);
let txs_per_block = txs_seen as f64 / blocks_seen.max(1) as f64;
println!(" mainnet averaged {txs_per_block:.2} transactions per block over that range");
let config = Arc::new(ChainConfig::mainnet());
let verifier = HeaderVerifier::default_rsk(config);
// Headers memoise their own hash and their merged-mining hash on first
// use, and proof of work is the expensive half of verification -- so a
// loop over one header would measure the cache, not the check. Each
// iteration gets a header with both caches cleared.
let mut header_us = Vec::with_capacity(pairs.len());
let mut accepted = 0usize;
for (header, parent) in &pairs {
let mut h = header.clone();
h.cached_hash = None;
h.cached_hash_for_merged_mining = None;
let at = Instant::now();
let ok = verifier.verify(&h, Some(parent)).is_ok();
header_us.push(at.elapsed().as_secs_f64() * 1e6);
if ok {
accepted += 1;
}
}
// Same care: `signing_hash` can go through `cached_rlp`.
//
// Failures are timed separately rather than mixed in. A transaction
// whose `v` is not a valid recovery id -- REMASC's, among others --
// bails before touching the curve, in well under a microsecond, and
// averaging those together with real recoveries would halve the
// figure while measuring nothing. A node still pays the failure, but
// it pays it once and cheaply.
let mut recover_us = Vec::with_capacity(txs.len());
let mut rejected_us = Vec::new();
for tx in &txs {
let mut t = tx.clone();
t.cached_rlp = None;
let at = Instant::now();
let ok = t.recover_sender(MAINNET_CHAIN_ID).is_ok();
let us = at.elapsed().as_secs_f64() * 1e6;
if ok { recover_us.push(us) } else { rejected_us.push(us) }
}
let recovered = recover_us.len();
anyhow::ensure!(!recover_us.is_empty(), "no transaction's sender could be recovered");
// The same recoveries through libsecp256k1. The context holds
// precomputed tables and is built once, as a node would: building it
// per call would measure the setup rather than the recovery.
let secp = bitcoin::secp256k1::Secp256k1::new();
let mut secp_us = Vec::with_capacity(txs.len());
let mut agreed = 0usize;
let mut disagreed = 0usize;
for tx in &txs {
let mut t = tx.clone();
t.cached_rlp = None;
let Ok(expected) = t.recover_sender(MAINNET_CHAIN_ID) else { continue };
let at = Instant::now();
let got = recover_with_libsecp(&secp, &t, MAINNET_CHAIN_ID);
secp_us.push(at.elapsed().as_secs_f64() * 1e6);
match got {
Some(a) if a == expected => agreed += 1,
_ => disagreed += 1,
}
}
let (h_med, h_mean, h_min, h_max) = stats(header_us);
let (r_med, r_mean, r_min, r_max) = stats(recover_us);
let (s_med, s_mean, s_min, s_max) = stats(secp_us);
println!("\n{:<22}{:>10}{:>10}{:>10}{:>10}", "", "median", "mean", "min", "max");
println!("{:<22}{h_med:>10.1}{h_mean:>10.1}{h_min:>10.1}{h_max:>10.1}", "header verify, us");
println!("{:<22}{r_med:>10.1}{r_mean:>10.1}{r_min:>10.1}{r_max:>10.1}", "recover, k256 us");
println!("{:<22}{s_med:>10.1}{s_mean:>10.1}{s_min:>10.1}{s_max:>10.1}", "recover, secp256k1");
println!(
" libsecp256k1 is {:.2}x the speed of k256 on the same transactions",
r_med / s_med.max(f64::MIN_POSITIVE)
);
anyhow::ensure!(
disagreed == 0,
"{disagreed} of {} recoveries disagreed between the two backends; \
the comparison means nothing until that is explained",
agreed + disagreed
);
println!(" both backends agreed on all {agreed} addresses");
println!(
"\n {accepted}/{} headers verified, {recovered}/{} senders recovered",
pairs.len(),
txs.len()
);
if !rejected_us.is_empty() {
let (j_med, _, _, _) = stats(rejected_us.clone());
println!(
" {} transactions carried no recoverable signature (REMASC and the like), \n \
rejected in {j_med:.1} us each -- excluded from the figures above",
rejected_us.len()
);
}
// What it adds to a block, at the block shapes we have measured.
println!("\n added cost per block, by transaction count:");
println!(" {:>6} {:>14} {:>16}", "txs", "header, ms", "recovery, ms");
let shapes = [txs_per_block.round().max(1.0) as u64, 50, 100, 200];
for n in shapes {
println!(
" {n:>6} {:>14.2} {:>16.2}",
h_med / 1000.0,
r_med * n as f64 / 1000.0
);
}
Ok(())
}