Republished from a clean root: the compiled artifact is gone from history, and the local line of work joins the sanitized public line

The public history carried kat/__pycache__/mlkem768_reference.cpython-314.pyc,
a compiled Python artifact embedding the operator's absolute local path. Text
secret scanners do not read compiled binaries, which is exactly how it slipped
through, and removing it from the tip would have left it reachable through the
old root commits. So this repository is republished from a single clean root.

This root also carries, from the previously unpublished line of work:
- corrected LICENSE year, LICENSING.md, VERIFY-POLICY.md, and
  CITATIONS-UNRESOLVED.md remeasured 2026-08-11 (101 paths, README aligned)
- O-018: run_consensus_verification.py ran 19 of 29 models and reported PASS;
  it now runs all 29, and computemarket_smt.py gains resolveByTimeout /
  reclaimUnsettled cases plus a negative control
- O-006: the word 'audited' removed from next to Bouncy Castle, twice, after a
  concurrent edit resurrected it
- O-014: prior art named and dated - Algorand's native falcon_verify shipped
  about ten months before AERE's precompiles; the primacy claim is withdrawn
  where it was implied
- bench/ scripts parametrized so they actually run for an outsider (the
  earlier textual sanitization left $STAGING unexpanded inside Python strings)
- AIP-2/AIP-3 errata with measured figures, spec remeasurements at 2026-08-01,
  and the spec-zk-stack retractions (owner is an operational key, not the
  Foundation; 'maximally sound' withdrawn; aggregator V1 deprecated)
The redacted bench-host environment files from the sanitized line are kept
exactly as published; the unredacted local variants are not carried.
This commit is contained in:
Aere Network committed 2026-08-15 13:52:14 +03:00
commit 6cb0140fae
177 files changed
+33777

No files matched your search

+160
View File
@@ -0,0 +1,160 @@
//! A compact, from-scratch Keccak-256 (the exact hash Ethereum/EVM uses). Used
//! to fold the final committed state into a single 32-byte state root that the
//! AERE rollup sequencer commits on-chain via
//! `AereRollupSettlement.proposeStateRoot`. Keeping it dependency-free means the
//! executor produces a real EVM-compatible root with zero external crates.
//!
//! This is standard Keccak-f[1600] with rate 1088 bits (136 bytes) and the
//! original Keccak padding (0x01 .. 0x80), i.e. keccak256, NOT SHA3-256.
const RC: [u64; 24] = [
0x0000000000000001,
0x0000000000008082,
0x800000000000808a,
0x8000000080008000,
0x000000000000808b,
0x0000000080000001,
0x8000000080008081,
0x8000000000008009,
0x000000000000008a,
0x0000000000000088,
0x0000000080008009,
0x000000008000000a,
0x000000008000808b,
0x800000000000008b,
0x8000000000008089,
0x8000000000008003,
0x8000000000008002,
0x8000000000000080,
0x000000000000800a,
0x800000008000000a,
0x8000000080008081,
0x8000000000008080,
0x0000000080000001,
0x8000000080008008,
];
const ROTC: [u32; 24] = [
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44,
];
const PILN: [usize; 24] = [
10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1,
];
fn keccak_f(st: &mut [u64; 25]) {
for round in 0..24 {
// theta
let mut bc = [0u64; 5];
for i in 0..5 {
bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20];
}
for i in 0..5 {
let t = bc[(i + 4) % 5] ^ bc[(i + 1) % 5].rotate_left(1);
for j in (0..25).step_by(5) {
st[j + i] ^= t;
}
}
// rho + pi
let mut t = st[1];
for i in 0..24 {
let j = PILN[i];
let tmp = st[j];
st[j] = t.rotate_left(ROTC[i]);
t = tmp;
}
// chi
for j in (0..25).step_by(5) {
let mut bc2 = [0u64; 5];
for i in 0..5 {
bc2[i] = st[j + i];
}
for i in 0..5 {
st[j + i] ^= (!bc2[(i + 1) % 5]) & bc2[(i + 2) % 5];
}
}
// iota
st[0] ^= RC[round];
}
}
/// keccak256 of `input`.
pub fn keccak256(input: &[u8]) -> [u8; 32] {
let mut st = [0u64; 25];
const RATE: usize = 136; // 1088 bits
let mut offset = 0;
let len = input.len();
// absorb full blocks
while len - offset >= RATE {
for i in 0..RATE / 8 {
let mut lane = 0u64;
for b in 0..8 {
lane |= (input[offset + i * 8 + b] as u64) << (8 * b);
}
st[i] ^= lane;
}
keccak_f(&mut st);
offset += RATE;
}
// last block with padding
let mut block = [0u8; RATE];
let rem = len - offset;
block[..rem].copy_from_slice(&input[offset..]);
block[rem] ^= 0x01; // keccak padding start
block[RATE - 1] ^= 0x80; // padding end
for i in 0..RATE / 8 {
let mut lane = 0u64;
for b in 0..8 {
lane |= (block[i * 8 + b] as u64) << (8 * b);
}
st[i] ^= lane;
}
keccak_f(&mut st);
let mut out = [0u8; 32];
for i in 0..4 {
let lane = st[i].to_le_bytes();
out[i * 8..i * 8 + 8].copy_from_slice(&lane);
}
out
}
/// Fold a materialized state into a single keccak256 root. Keys are serialized
/// in a canonical, sorted order so the root is deterministic and independent of
/// hashmap iteration order -- exactly what a rollup state commitment needs.
pub fn state_root(state: &crate::types::StateMap) -> [u8; 32] {
use crate::types::Key;
let mut entries: Vec<(Key, u128)> = state
.iter()
.filter(|(_, v)| **v != 0) // absent == 0; omit zeros for a canonical form
.map(|(k, v)| (*k, *v))
.collect();
entries.sort();
let mut buf: Vec<u8> = Vec::with_capacity(entries.len() * 40);
for (k, v) in entries {
match k {
Key::Balance(a) => {
buf.push(0x00);
buf.extend_from_slice(&a.to_be_bytes());
}
Key::Storage(c, s) => {
buf.push(0x01);
buf.extend_from_slice(&c.to_be_bytes());
buf.extend_from_slice(&s.to_be_bytes());
}
}
buf.extend_from_slice(&v.to_be_bytes());
}
keccak256(&buf)
}
pub fn hex32(b: &[u8; 32]) -> String {
let mut s = String::with_capacity(66);
s.push_str("0x");
for byte in b {
s.push_str(&format!("{:02x}", byte));
}
s
}
+40
View File
@@ -0,0 +1,40 @@
//! aere-block-stm: a real Block-STM optimistic-concurrency parallel executor for
//! AERE rollup batches.
//!
//! HONEST SCOPE
//! ------------
//! This does NOT make AERE's Layer-1 base execution parallel. AERE's L1 is
//! Hyperledger Besu, which executes transactions sequentially; parallelizing
//! Besu itself is a Java execution-client fork and is out of scope here.
//!
//! What this IS: a standalone, correct, benchmarked parallel EVM-style
//! transaction executor that AERE's ROLLUP layer (AereRaaSFactory /
//! AereRollupSettlement / the rollup sequencer) can call to execute a batch of
//! rollup transactions across many CPU cores and commit a single state root.
//! The committed result is provably identical to sequential execution
//! (serializability), which is exactly what makes the resulting state root a
//! valid rollup commitment.
//!
//! Public API
//! ----------
//! - [`sequential::execute_block_sequential`] -- the reference/oracle.
//! - [`parallel::execute_block_parallel`] -- the Block-STM executor.
//! - [`keccak::state_root`] -- keccak256 commitment over a final state.
pub mod keccak;
pub mod mvmemory;
pub mod parallel;
pub mod predict;
pub mod scheduler;
pub mod sequential;
pub mod types;
pub mod vm;
pub mod workload;
pub use parallel::{
execute_block_parallel, execute_block_parallel_predicted, execute_block_parallel_split,
ExecStats,
};
pub use predict::compute_predicted_preds;
pub use sequential::execute_block_sequential;
pub use types::{Key, StateMap, TxKind, Txn, Value};
+986
View File
@@ -0,0 +1,986 @@
//! CLI for the AERE Block-STM executor.
//!
//! aere-block-stm harness run the correctness harness (parallel == sequential)
//! aere-block-stm bench run the speedup benchmark
//! aere-block-stm execute read a batch on stdin, emit committed state root (JSON)
//!
//! `execute` is the integration entry point the rollup sequencer shells out to.
use std::io::{Read, Write};
use std::time::Instant;
use aere_block_stm::keccak::{hex32, state_root};
use aere_block_stm::parallel::{
execute_block_parallel, execute_block_parallel_predicted, execute_block_parallel_split,
};
use aere_block_stm::predict::compute_predicted_preds;
use aere_block_stm::sequential::execute_block_sequential;
use aere_block_stm::types::{Key, StateMap, TxKind, Txn};
use aere_block_stm::workload::{generate_batch, make_base_state, WorkloadConfig};
fn main() {
let args: Vec<String> = std::env::args().collect();
let cmd = args.get(1).map(|s| s.as_str()).unwrap_or("harness");
match cmd {
"harness" => harness(),
"bench" => bench(),
"ab" => ab_experiment(),
"endtoend" | "e2e" => endtoend(),
"execute" => execute_stdin(),
other => {
eprintln!("unknown command: {other}");
eprintln!("usage: aere-block-stm [harness|bench|ab|endtoend|execute]");
std::process::exit(2);
}
}
}
/// ------------------------------------------------------------------ harness
/// Prove serializability: over a large matrix of randomized, conflicting
/// batches, the Block-STM committed state MUST equal the sequential oracle for
/// EVERY batch and EVERY thread count.
fn harness() {
let thread_counts = [1usize, 2, 4, 8, 16];
// (num_txns, num_accounts, hot_accounts, contracts, hot_slots, conflict_ppm, label)
let profiles: &[(usize, u64, u64, u64, u64, u64, &str)] = &[
(200, 500, 50, 4, 4, 200_000, "mixed-moderate"),
(500, 1000, 20, 2, 2, 500_000, "high-conflict"),
(1000, 5000, 200, 8, 8, 100_000, "low-conflict"),
(300, 64, 8, 1, 1, 900_000, "brutal-single-counter"),
(400, 200, 200, 1, 1, 1_000_000, "all-hot"),
(50, 20, 4, 1, 1, 1_000_000, "tiny-dense"),
];
let mut total_cases = 0usize;
let mut mismatches = 0usize;
let batches_per_profile = 200u64;
for (ntx, nacc, hot, ncon, hslots, conf, label) in profiles.iter().copied() {
for seed in 0..batches_per_profile {
let cfg = WorkloadConfig {
num_txns: ntx,
num_accounts: nacc,
num_hot_accounts: hot,
num_contracts: ncon,
num_hot_slots: hslots,
conflict_ppm: conf,
gas: 4, // small; correctness is independent of gas
seed: 0xAE_5E_0000 ^ (seed.wrapping_mul(0x9E3779B9)),
};
let base = make_base_state(&cfg);
let txns = generate_batch(&cfg);
let seq = execute_block_sequential(&base, &txns);
for &threads in &thread_counts {
let (par, _stats) = execute_block_parallel(&base, &txns, threads);
total_cases += 1;
if par != seq {
mismatches += 1;
eprintln!(
"MISMATCH profile={label} seed={seed} threads={threads}: parallel != sequential"
);
// show a couple of differing keys for debugging
let mut shown = 0;
for (k, v) in &seq {
let pv = par.get(k).copied().unwrap_or(0);
if pv != *v {
eprintln!(" key {:?}: seq={} par={}", k, v, pv);
shown += 1;
if shown >= 5 {
break;
}
}
}
}
}
}
eprintln!(" profile '{label}' done");
}
println!("=======================================================");
println!("CORRECTNESS HARNESS");
println!(" profiles : {}", profiles.len());
println!(" batches per profile : {batches_per_profile}");
println!(" thread counts : {:?}", thread_counts);
println!(
" total (batch x threads) comparisons: {total_cases}"
);
println!(" mismatches : {mismatches}");
if mismatches == 0 {
println!(" RESULT: PASS -- parallel Block-STM == sequential on ALL cases");
} else {
println!(" RESULT: FAIL -- serializability violated");
std::process::exit(1);
}
println!("=======================================================");
}
/// -------------------------------------------------------------------- bench
fn median(mut v: Vec<f64>) -> f64 {
v.sort_by(|a, b| a.partial_cmp(b).unwrap());
v[v.len() / 2]
}
fn time_parallel(base: &StateMap, txns: &[Txn], threads: usize, reps: usize) -> (f64, aere_block_stm::ExecStats) {
let mut times = Vec::new();
let mut last = None;
for _ in 0..reps {
let t0 = Instant::now();
let (_state, stats) = execute_block_parallel(base, txns, threads);
times.push(t0.elapsed().as_secs_f64() * 1000.0);
last = Some(stats);
}
(median(times), last.unwrap())
}
fn time_sequential(base: &StateMap, txns: &[Txn], reps: usize) -> f64 {
let mut times = Vec::new();
for _ in 0..reps {
let t0 = Instant::now();
let _ = execute_block_sequential(base, txns);
times.push(t0.elapsed().as_secs_f64() * 1000.0);
}
median(times)
}
fn report_scenario(
label: &str,
base: &StateMap,
txns: &[Txn],
thread_counts: &[usize],
reps: usize,
) {
// sanity: this exact batch is serializable (spot-check once at max threads)
let seq_state = execute_block_sequential(base, txns);
let (par_state, _) = execute_block_parallel(base, txns, 16);
let ok = seq_state == par_state;
let seq_ms = time_sequential(base, txns, reps);
println!();
println!("### {label}");
println!(" parallel==sequential check: {}", if ok { "PASS" } else { "FAIL" });
println!(" sequential baseline : {seq_ms:.2} ms");
println!(
" {:<8} {:>10} {:>10} {:>12} {:>10} {:>10}",
"threads", "time(ms)", "speedup", "vs-1thread", "aborts", "abort%"
);
let mut t1 = None;
for &threads in thread_counts {
let (ms, stats) = time_parallel(base, txns, threads, reps);
if threads == 1 {
t1 = Some(ms);
}
let speedup = seq_ms / ms;
let vs1 = t1.map(|a| a / ms).unwrap_or(1.0);
let abort_pct =
100.0 * stats.total_aborts as f64 / stats.total_executions.max(1) as f64;
println!(
" {:<8} {:>10.2} {:>9.2}x {:>11.2}x {:>10} {:>9.1}%",
threads, ms, speedup, vs1, stats.total_aborts, abort_pct
);
}
}
fn bench() {
// optional args: aere-block-stm bench [gas] [ntx]
let argv: Vec<String> = std::env::args().collect();
let gas: u32 = argv.get(2).and_then(|s| s.parse().ok()).unwrap_or(80);
let ntx: usize = argv.get(3).and_then(|s| s.parse().ok()).unwrap_or(20_000);
let thread_counts = [1usize, 2, 4, 8, 16];
let reps = 7usize;
let scenarios: &[(&str, WorkloadConfig)] = &[
(
"LOW-CONFLICT (50k accounts, few hot, ~2% hot-touch)",
WorkloadConfig {
num_txns: ntx,
num_accounts: 50_000,
num_hot_accounts: 100,
num_contracts: 64,
num_hot_slots: 64,
conflict_ppm: 20_000, // 2%
gas,
seed: 12345,
},
),
(
"MEDIUM-CONFLICT (5k accounts, ~15% hot-touch)",
WorkloadConfig {
num_txns: ntx,
num_accounts: 5_000,
num_hot_accounts: 50,
num_contracts: 16,
num_hot_slots: 16,
conflict_ppm: 150_000, // 15%
gas,
seed: 999,
},
),
(
"HIGH-CONFLICT (single hot counter, ~80% hot-touch)",
WorkloadConfig {
num_txns: ntx,
num_accounts: 2_000,
num_hot_accounts: 20,
num_contracts: 1,
num_hot_slots: 1,
conflict_ppm: 800_000, // 80%
gas,
seed: 7,
},
),
];
println!("=======================================================");
println!("BLOCK-STM SPEEDUP BENCHMARK");
println!(" machine threads available: {}", std::thread::available_parallelism().map(|n| n.get()).unwrap_or(0));
println!(" txns per batch : {ntx}");
println!(" simulated EVM gas per tx : {gas} keccak-rounds");
println!(" timing : median of {reps} runs (ms)");
println!("=======================================================");
for (label, cfg) in scenarios {
let base = make_base_state(cfg);
let txns = generate_batch(cfg);
report_scenario(label, &base, &txns, &thread_counts, reps);
}
// PATHOLOGICAL fully-serial case: every single transaction increments the
// SAME slot. There is zero available parallelism; Block-STM must serialize
// all of them and pays extra for aborted speculation. This is the honest
// worst case -- speedup should be ~1x or below.
{
let mut base = StateMap::new();
base.insert(Key::Storage(0, 0), 0);
let txns: Vec<Txn> = (0..ntx)
.map(|_| Txn {
kind: TxKind::Increment { contract: 0, slot: 0 },
gas,
})
.collect();
report_scenario(
"PATHOLOGICAL (every tx increments the SAME slot: no parallelism)",
&base,
&txns,
&thread_counts,
reps,
);
}
println!();
println!("Note: speedup is vs the sequential oracle. High-conflict batches");
println!("serialize on the hot slot and are expected to show little or no");
println!("speedup -- that is the honest, correct behavior of Block-STM.");
}
/// ------------------------------------------------------------------ ab
/// The predictive-scheduler CEILING experiment.
///
/// For each workload profile x thread count we run Block-STM TWICE on the SAME
/// batch: once as-is (WITHOUT prediction) and once WITH advisory pre-parking
/// driven by PERFECT read/write sets (the exact conflicts of the real serial
/// execution). Perfect prediction is the best any predictive scheduler could
/// ever do, so the gap between the two arms is the theoretical UPPER BOUND on
/// what prediction can buy. We report the abort-rate delta (the direct metric)
/// and the wall-time delta, NOT throughput multipliers.
fn median_f64(mut v: Vec<f64>) -> f64 {
v.sort_by(|a, b| a.partial_cmp(b).unwrap());
v[v.len() / 2]
}
fn median_usize(mut v: Vec<usize>) -> usize {
v.sort_unstable();
v[v.len() / 2]
}
/// Summary of one arm at one thread count: median wall-time (ms), median abort
/// count, median executions, median predicted-parks.
struct ArmSummary {
ms: f64,
aborts: usize,
executions: usize,
parks: usize,
}
fn run_arm(
base: &StateMap,
txns: &[Txn],
threads: usize,
reps: usize,
preds: Option<&Vec<Vec<usize>>>,
) -> ArmSummary {
let mut times = Vec::with_capacity(reps);
let mut aborts = Vec::with_capacity(reps);
let mut execs = Vec::with_capacity(reps);
let mut parks = Vec::with_capacity(reps);
for _ in 0..reps {
let t0 = Instant::now();
let (_state, stats) = match preds {
None => execute_block_parallel(base, txns, threads),
Some(p) => execute_block_parallel_predicted(base, txns, threads, p.clone()),
};
times.push(t0.elapsed().as_secs_f64() * 1000.0);
aborts.push(stats.total_aborts);
execs.push(stats.total_executions);
parks.push(stats.total_predicted_parks);
}
ArmSummary {
ms: median_f64(times),
aborts: median_usize(aborts),
executions: median_usize(execs),
parks: median_usize(parks),
}
}
fn ab_experiment() {
// optional args: aere-block-stm ab [gas] [ntx] [reps]
let argv: Vec<String> = std::env::args().collect();
let gas: u32 = argv.get(2).and_then(|s| s.parse().ok()).unwrap_or(60);
let ntx: usize = argv.get(3).and_then(|s| s.parse().ok()).unwrap_or(6_000);
let reps: usize = argv.get(4).and_then(|s| s.parse().ok()).unwrap_or(5);
let thread_counts = [1usize, 2, 4, 8, 16];
// Six workload profiles spanning the conflict spectrum. All are
// parameterizations of the same deterministic SplitMix64 generator; the
// labels describe the conflict structure each one produces, honestly.
let profiles: &[(&str, bool, WorkloadConfig)] = &[
// (label, is_low_or_medium_traffic (counts toward the headroom verdict), cfg)
(
"uniform (no designed hot set, ~0% hot-touch)",
true,
WorkloadConfig {
num_txns: ntx, num_accounts: 50_000, num_hot_accounts: 0,
num_contracts: 64, num_hot_slots: 64, conflict_ppm: 0, gas, seed: 101,
},
),
(
"low-conflict (~2% hot-touch)",
true,
WorkloadConfig {
num_txns: ntx, num_accounts: 50_000, num_hot_accounts: 100,
num_contracts: 64, num_hot_slots: 64, conflict_ppm: 20_000, gas, seed: 202,
},
),
(
"medium-conflict (~15% hot-touch)",
true,
WorkloadConfig {
num_txns: ntx, num_accounts: 5_000, num_hot_accounts: 50,
num_contracts: 16, num_hot_slots: 16, conflict_ppm: 150_000, gas, seed: 303,
},
),
(
"disjoint-hot-groups (32 contracts x 32 slots, ~40% hot-touch)",
false,
WorkloadConfig {
num_txns: ntx, num_accounts: 5_000, num_hot_accounts: 200,
num_contracts: 32, num_hot_slots: 32, conflict_ppm: 400_000, gas, seed: 404,
},
),
(
"single-hot-slot (1 contract, 1 slot, ~70% hot-touch)",
false,
WorkloadConfig {
num_txns: ntx, num_accounts: 2_000, num_hot_accounts: 20,
num_contracts: 1, num_hot_slots: 1, conflict_ppm: 700_000, gas, seed: 505,
},
),
(
"high-conflict (2 contracts, 2 slots, ~80% hot-touch)",
false,
WorkloadConfig {
num_txns: ntx, num_accounts: 2_000, num_hot_accounts: 20,
num_contracts: 2, num_hot_slots: 2, conflict_ppm: 800_000, gas, seed: 606,
},
),
];
println!("=======================================================");
println!("PREDICTIVE-SCHEDULER CEILING (perfect-prediction A/B)");
println!(" machine threads available : {}", std::thread::available_parallelism().map(|n| n.get()).unwrap_or(0));
println!(" txns per batch : {ntx}");
println!(" simulated EVM gas per tx : {gas} keccak-rounds");
println!(" timing : median of {reps} runs (ms)");
println!(" prediction : PERFECT exact-RW-set pre-parking (upper bound)");
println!(" metric of record : TOTAL_ABORTS + wall-time, WITH vs WITHOUT");
println!("=======================================================");
// Accumulators for the verdict: wall-time deltas on low/medium/uniform
// ("most real traffic"), and abort-rate deltas everywhere.
let mut lowmed_wall_deltas: Vec<f64> = Vec::new();
let mut lowmed_abort_pp: Vec<f64> = Vec::new();
let mut worst_case_wall_help = f64::NEG_INFINITY;
let mut worst_case_label = String::new();
for (label, is_lowmed, cfg) in profiles.iter() {
let base = make_base_state(cfg);
let txns = generate_batch(cfg);
// PERFECT predicted soft-dependencies for this exact batch.
let preds = compute_predicted_preds(&base, &txns);
// Correctness: both arms must equal the sequential oracle (spot-check @16).
let seq = execute_block_sequential(&base, &txns);
let (par_no, _) = execute_block_parallel(&base, &txns, 16);
let (par_yes, _) =
execute_block_parallel_predicted(&base, &txns, 16, preds.clone());
let ok = par_no == seq && par_yes == seq;
// Average predicted predecessors per txn (structure of the profile).
let avg_deps: f64 =
preds.iter().map(|p| p.len()).sum::<usize>() as f64 / preds.len() as f64;
println!();
println!("### {label}");
println!(" correctness (both arms == sequential oracle): {}", if ok { "PASS" } else { "FAIL" });
println!(" avg predicted predecessors / txn : {avg_deps:.3}");
println!(
" {:<8} {:>10} {:>10} {:>9} | {:>9} {:>9} {:>9} | {:>8}",
"threads", "no_ms", "pred_ms", "wallD%", "no_ab%", "pred_ab%", "abD_pp", "parks"
);
if !ok {
println!(" !! correctness FAILED -- numbers below are meaningless, aborting");
std::process::exit(1);
}
for &threads in &thread_counts {
let no = run_arm(&base, &txns, threads, reps, None);
let yes = run_arm(&base, &txns, threads, reps, Some(&preds));
let no_ab_pct = 100.0 * no.aborts as f64 / no.executions.max(1) as f64;
let yes_ab_pct = 100.0 * yes.aborts as f64 / yes.executions.max(1) as f64;
let abort_delta_pp = no_ab_pct - yes_ab_pct; // positive = prediction removed aborts
// wall delta: positive % = prediction was FASTER (saved wall time)
let wall_delta = 100.0 * (no.ms - yes.ms) / no.ms;
println!(
" {:<8} {:>10.2} {:>10.2} {:>8.1}% | {:>8.2}% {:>8.2}% {:>9.2} | {:>8}",
threads, no.ms, yes.ms, wall_delta, no_ab_pct, yes_ab_pct, abort_delta_pp, yes.parks
);
if *is_lowmed && threads > 1 {
lowmed_wall_deltas.push(wall_delta);
lowmed_abort_pp.push(abort_delta_pp);
}
if threads > 1 && wall_delta > worst_case_wall_help {
worst_case_wall_help = wall_delta;
worst_case_label = format!("{label} @ {threads}t");
}
}
}
let mean_lowmed_wall = if lowmed_wall_deltas.is_empty() {
0.0
} else {
lowmed_wall_deltas.iter().sum::<f64>() / lowmed_wall_deltas.len() as f64
};
let max_lowmed_wall = lowmed_wall_deltas
.iter()
.cloned()
.fold(f64::NEG_INFINITY, f64::max);
let mean_lowmed_abort_pp = if lowmed_abort_pp.is_empty() {
0.0
} else {
lowmed_abort_pp.iter().sum::<f64>() / lowmed_abort_pp.len() as f64
};
println!();
println!("=======================================================");
println!("VERDICT (perfect-prediction ceiling)");
println!(" low/medium/uniform traffic (>1 thread):");
println!(" mean wall-time saved by PERFECT prediction : {mean_lowmed_wall:+.2}%");
println!(" max wall-time saved by PERFECT prediction : {max_lowmed_wall:+.2}%");
println!(" mean abort-rate reduction : {mean_lowmed_abort_pp:+.2} pp");
println!(" best single wall-time help anywhere (>1t) : {worst_case_wall_help:+.2}% [{worst_case_label}]");
println!("=======================================================");
println!();
println!("READ THIS: The numbers above are an UPPER BOUND -- the predictor is fed the");
println!("exact conflicts of the real serial run, which no deployable predictor can match.");
println!("On low/medium-conflict traffic (the bulk of real blocks) Block-STM already");
println!("discovers dependencies dynamically and its abort rate is low, so even a PERFECT");
println!("predictor removes only a small residue of aborts and the wall-time it saves is");
println!("within noise. Prediction only visibly helps on pathological high-conflict batches,");
println!("where it converts speculative-then-aborted work into ordered waits. Because the");
println!("predictions are ADVISORY (they change only WHEN a tx is attempted, never the");
println!("committed state -- validation stays the sole authority), a wrong or approximate");
println!("prediction can never corrupt the state root; at worst it fails to help. Conclusion:");
println!("a predictive pre-parking lever is safe to add but is NOT a throughput win for");
println!("mainstream traffic; keep it advisory and off the correctness path.");
}
/// --------------------------------------------------------------- endtoend
/// END-TO-END block-production throughput.
///
/// The `bench` command times ONLY the parallel Block-STM execution phase (the
/// part that scales) and reports the well-known ~9.4x-at-16-cores figure. That
/// is an honest EXECUTION-PHASE number, but it is NOT an end-to-end block
/// throughput number: a real block is not finished when execution finishes, it
/// is finished when the resulting state is committed and a state root is
/// produced. Those tail phases are (largely) SERIAL, so by Amdahl's law the
/// end-to-end speedup must be LOWER than the execution-phase speedup.
///
/// This command measures the FULL pipeline, parallel vs sequential, at 1/2/4/8/16
/// workers and across conflict ratios, timing three phases:
/// 1. exec -- Block-STM parallel execution + validation + re-execution (PARALLEL)
/// 2. commit -- MvMemory::snapshot_final: materialize the flat state (SERIAL)
/// 3. root -- keccak256 state root over the committed state (SERIAL)
/// end-to-end = exec + commit + root. The sequential baseline is
/// execute_block_sequential (produces the live map directly) + root.
///
/// Serial-equivalence is CHECKED on every single run: the parallel committed
/// state must equal the sequential oracle AND the two keccak state roots must be
/// bit-identical. A single mismatch aborts the whole run.
fn med(mut v: Vec<f64>) -> f64 {
v.sort_by(|a, b| a.partial_cmp(b).unwrap());
v[v.len() / 2]
}
fn endtoend() {
// args: aere-block-stm endtoend [gas] [ntx] [reps]
// gas=80, ntx=20000 match the documented execution-phase regime so the
// end-to-end numbers are directly comparable to the ~9.4x figure.
let argv: Vec<String> = std::env::args().collect();
let gas: u32 = argv.get(2).and_then(|s| s.parse().ok()).unwrap_or(80);
let ntx: usize = argv.get(3).and_then(|s| s.parse().ok()).unwrap_or(20_000);
let reps: usize = argv.get(4).and_then(|s| s.parse().ok()).unwrap_or(7);
let thread_counts = [1usize, 2, 4, 8, 16];
// Conflict-ratio spectrum. `conflict_ppm` tunes how often a tx touches the
// shared hot set; with few contracts/slots the increments/AMM swaps also
// concentrate, so the labelled % is the DESIGNED hot-touch rate. The honest
// realized-conflict indicator is the measured abort% printed per row.
let scenarios: &[(&str, WorkloadConfig)] = &[
(
"0% designed conflict (disjoint accounts, 64 contracts spread)",
WorkloadConfig {
num_txns: ntx, num_accounts: 50_000, num_hot_accounts: 0,
num_contracts: 64, num_hot_slots: 64, conflict_ppm: 0, gas, seed: 101,
},
),
(
"10% designed conflict (100 hot accts, 16 contracts)",
WorkloadConfig {
num_txns: ntx, num_accounts: 20_000, num_hot_accounts: 100,
num_contracts: 16, num_hot_slots: 16, conflict_ppm: 100_000, gas, seed: 202,
},
),
(
"50% designed conflict (20 hot accts, 4 contracts)",
WorkloadConfig {
num_txns: ntx, num_accounts: 10_000, num_hot_accounts: 20,
num_contracts: 4, num_hot_slots: 4, conflict_ppm: 500_000, gas, seed: 303,
},
),
(
"PATHOLOGICAL ~100% (single hot slot, no parallelism)",
WorkloadConfig {
num_txns: ntx, num_accounts: 2_000, num_hot_accounts: 20,
num_contracts: 1, num_hot_slots: 1, conflict_ppm: 1_000_000, gas, seed: 404,
},
),
];
println!("=======================================================");
println!("BLOCK-STM END-TO-END BLOCK-PRODUCTION THROUGHPUT");
println!(" machine threads available : {}", std::thread::available_parallelism().map(|n| n.get()).unwrap_or(0));
println!(" txns per batch : {ntx}");
println!(" simulated EVM cost per tx : {gas} keccak-rounds");
println!(" timing : median of {reps} runs (ms)");
println!(" pipeline : exec (parallel) + commit (serial) + state-root (serial)");
println!(" serial-equivalence : par state == seq state AND keccak roots identical, EVERY run");
println!("=======================================================");
let mut total_equiv_checks = 0usize;
let mut total_mismatches = 0usize;
for (label, cfg) in scenarios {
let base = make_base_state(cfg);
let txns = generate_batch(cfg);
// ---- sequential oracle + baseline timing ----
let seq_state = execute_block_sequential(&base, &txns);
let seq_root = state_root(&seq_state);
let seq_root_hex = hex32(&seq_root);
let state_keys = seq_state.iter().filter(|(_, v)| **v != 0).count();
let mut seq_exec_v = Vec::with_capacity(reps);
let mut seq_root_v = Vec::with_capacity(reps);
for _ in 0..reps {
let t0 = Instant::now();
let s = execute_block_sequential(&base, &txns);
let t1 = Instant::now();
let r = state_root(&s);
let t2 = Instant::now();
std::hint::black_box(&r);
seq_exec_v.push(t1.duration_since(t0).as_secs_f64() * 1000.0);
seq_root_v.push(t2.duration_since(t1).as_secs_f64() * 1000.0);
}
let seq_exec_ms = med(seq_exec_v);
let seq_root_ms = med(seq_root_v);
let seq_e2e_ms = seq_exec_ms + seq_root_ms;
println!();
println!("### {label}");
println!(" committed state keys : {state_keys}");
println!(" sequential exec {:.2} ms | root {:.2} ms | END-TO-END {:.2} ms | root-share {:.1}%",
seq_exec_ms, seq_root_ms, seq_e2e_ms, 100.0 * seq_root_ms / seq_e2e_ms);
println!(" sequential end-to-end TPS: {:.0}", ntx as f64 / seq_e2e_ms * 1000.0);
println!(
" {:<4} | {:>8} {:>8} {:>7} {:>9} | {:>9} {:>9} | {:>7} {:>7} | {:>9} | {:>6}",
"W", "exec", "commit", "root", "e2e_ms", "execSpd", "e2eSpd", "TPS", "abort%", "root%e2e", "equiv"
);
for &w in &thread_counts {
let mut ex_v = Vec::with_capacity(reps);
let mut cm_v = Vec::with_capacity(reps);
let mut rt_v = Vec::with_capacity(reps);
let mut abort_v = Vec::with_capacity(reps);
let mut equiv_ok = true;
for _ in 0..reps {
let (state, stats, exec_dur, commit_dur) =
execute_block_parallel_split(&base, &txns, w);
let tr0 = Instant::now();
let root = state_root(&state);
let root_ms = tr0.elapsed().as_secs_f64() * 1000.0;
// serial-equivalence check, EVERY run
total_equiv_checks += 1;
let ok = state == seq_state && hex32(&root) == seq_root_hex;
if !ok {
equiv_ok = false;
total_mismatches += 1;
eprintln!(" !! SERIAL-EQUIVALENCE MISMATCH scenario='{label}' W={w}");
}
ex_v.push(exec_dur.as_secs_f64() * 1000.0);
cm_v.push(commit_dur.as_secs_f64() * 1000.0);
rt_v.push(root_ms);
abort_v.push(
100.0 * stats.total_aborts as f64 / stats.total_executions.max(1) as f64,
);
}
let exec_ms = med(ex_v);
let commit_ms = med(cm_v);
let root_ms = med(rt_v);
let e2e_ms = exec_ms + commit_ms + root_ms;
let abort_pct = med(abort_v);
let exec_spd = seq_exec_ms / exec_ms;
let e2e_spd = seq_e2e_ms / e2e_ms;
let tps = ntx as f64 / e2e_ms * 1000.0;
let root_share = 100.0 * (commit_ms + root_ms) / e2e_ms;
println!(
" {:<4} | {:>8.2} {:>8.2} {:>7.2} {:>9.2} | {:>8.2}x {:>8.2}x | {:>7.0} {:>6.1}% | {:>8.1}% | {:>6}",
w, exec_ms, commit_ms, root_ms, e2e_ms, exec_spd, e2e_spd, tps, abort_pct, root_share,
if equiv_ok { "PASS" } else { "FAIL" }
);
}
// Amdahl ceiling for END-TO-END speedup from the measured serial tail.
// Serial tail (commit + root) is fixed regardless of workers; only exec
// scales. Ceiling = seq_e2e / (serial_tail + exec_floor). We use the
// measured 16-worker commit+root as the serial tail and the measured
// 16-worker exec as the (non-zero) exec floor.
let (_st16, _ss16, ex16, cm16) = execute_block_parallel_split(&base, &txns, 16);
let rt16 = {
let s = execute_block_parallel_split(&base, &txns, 16).0;
let t = Instant::now();
let _ = state_root(&s);
t.elapsed().as_secs_f64() * 1000.0
};
let ex16ms = ex16.as_secs_f64() * 1000.0;
let cm16ms = cm16.as_secs_f64() * 1000.0;
let serial_tail = cm16ms + rt16;
let ceiling_with_floor = seq_e2e_ms / (serial_tail + ex16ms);
let ceiling_infinite_cores = seq_e2e_ms / serial_tail;
println!(
" Amdahl: serial tail (commit+root) = {:.2} ms; end-to-end speedup ceiling @16w = {:.2}x, @infinite cores = {:.2}x",
serial_tail, ceiling_with_floor, ceiling_infinite_cores
);
}
println!();
println!("=======================================================");
println!("SERIAL-EQUIVALENCE: {total_equiv_checks} checks, {total_mismatches} mismatches");
if total_mismatches == 0 {
println!(" RESULT: PASS -- every parallel run committed the exact sequential state + root");
} else {
println!(" RESULT: FAIL -- serializability violated; numbers are meaningless");
std::process::exit(1);
}
println!("=======================================================");
println!();
println!("HONEST READING:");
println!(" - execSpd is the EXECUTION-PHASE speedup (comparable to the documented ~9.4x).");
println!(" - e2eSpd is the END-TO-END block-production speedup. It is LOWER, because the");
println!(" commit (state materialization) and state-root (keccak) phases are serial and");
println!(" do not shrink with more workers -- the Amdahl serial tail.");
println!(" - TPS is the real end-to-end transactions/second of THIS pipeline on THIS box");
println!(" (shared-vCPU cloud). It is a measured rollup-batch number, NOT the 273k TPS");
println!(" documented-conditions architectural ceiling and does not reach or imply it.");
println!(" - Under total conflict (pathological) there is no parallelism; end-to-end can be");
println!(" <= 1x. That is the correct, honest behavior of Block-STM.");
}
/// ----------------------------------------------------------------- execute
/// Wire protocol (stdin), one record per line:
/// THREADS <n>
/// BASE <key> <value>
/// TX TRANSFER <from> <to> <amount>
/// TX SWEEP <from> <to>
/// TX INC <contract> <slot>
/// TX AMM <contract> <xslot> <yslot> <dx>
/// where <key> is "B:<acct>" or "S:<contract>:<slot>".
///
/// Emits a JSON object on stdout with the committed keccak256 state root and a
/// self-check that parallel execution equaled sequential execution.
fn parse_key(s: &str) -> Option<Key> {
if let Some(rest) = s.strip_prefix("B:") {
Some(Key::Balance(rest.parse().ok()?))
} else if let Some(rest) = s.strip_prefix("S:") {
let mut it = rest.split(':');
let c = it.next()?.parse().ok()?;
let slot = it.next()?.parse().ok()?;
Some(Key::Storage(c, slot))
} else {
None
}
}
fn key_to_wire(k: &Key) -> String {
match k {
Key::Balance(a) => format!("B:{a}"),
Key::Storage(c, s) => format!("S:{c}:{s}"),
}
}
fn execute_stdin() {
let mut input = String::new();
std::io::stdin().read_to_string(&mut input).unwrap();
let mut threads = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(4);
let mut gas: u32 = 0;
let mut base = StateMap::new();
let mut txns: Vec<Txn> = Vec::new();
for line in input.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
let f: Vec<&str> = line.split_whitespace().collect();
match f[0] {
"THREADS" => threads = f[1].parse().unwrap_or(threads),
"GAS" => gas = f[1].parse().unwrap_or(0),
"BASE" => {
if let Some(k) = parse_key(f[1]) {
base.insert(k, f[2].parse().unwrap_or(0));
}
}
"TX" => {
let kind = match f[1] {
"TRANSFER" => TxKind::Transfer {
from: f[2].parse().unwrap(),
to: f[3].parse().unwrap(),
amount: f[4].parse().unwrap(),
},
"SWEEP" => TxKind::Sweep {
from: f[2].parse().unwrap(),
to: f[3].parse().unwrap(),
},
"INC" => TxKind::Increment {
contract: f[2].parse().unwrap(),
slot: f[3].parse().unwrap(),
},
"AMM" => TxKind::AmmSwap {
contract: f[2].parse().unwrap(),
x_slot: f[3].parse().unwrap(),
y_slot: f[4].parse().unwrap(),
dx: f[5].parse().unwrap(),
},
_ => continue,
};
txns.push(Txn { kind, gas });
}
_ => {}
}
}
let seq = execute_block_sequential(&base, &txns);
let (par, stats) = execute_block_parallel(&base, &txns, threads);
let equal = seq == par;
let root = state_root(&par);
// emit state entries (sorted) as JSON
let mut entries: Vec<(Key, u128)> =
par.iter().filter(|(_, v)| **v != 0).map(|(k, v)| (*k, *v)).collect();
entries.sort();
let mut state_json = String::from("[");
for (i, (k, v)) in entries.iter().enumerate() {
if i > 0 {
state_json.push(',');
}
state_json.push_str(&format!("[\"{}\",\"{}\"]", key_to_wire(k), v));
}
state_json.push(']');
let out = format!(
"{{\"stateRoot\":\"{}\",\"numTxns\":{},\"threads\":{},\"executions\":{},\"validations\":{},\"aborts\":{},\"parallelEqualsSequential\":{},\"state\":{}}}",
hex32(&root),
stats.num_txns,
stats.num_threads,
stats.total_executions,
stats.total_validations,
stats.total_aborts,
equal,
state_json,
);
let mut stdout = std::io::stdout();
stdout.write_all(out.as_bytes()).unwrap();
stdout.write_all(b"\n").unwrap();
if !equal {
eprintln!("FATAL: parallel result diverged from sequential -- root not trustworthy");
std::process::exit(1);
}
}
/// ------------------------------------------------------------------- tests
#[cfg(test)]
mod tests {
use super::*;
/// The canonical serializability probe: N transactions all increment the
/// SAME counter slot. Sequential result is exactly N. Block-STM, despite
/// running them in parallel and reordering, MUST also produce exactly N.
#[test]
fn shared_counter_is_serialized() {
let contract = 0u64;
let slot = 0u64;
let n = 500usize;
let mut base = StateMap::new();
base.insert(Key::Storage(contract, slot), 0);
let txns: Vec<Txn> = (0..n)
.map(|_| Txn {
kind: TxKind::Increment { contract, slot },
gas: 0,
})
.collect();
for threads in [1usize, 2, 4, 8, 16] {
let (par, _) = execute_block_parallel(&base, &txns, threads);
assert_eq!(
par.get(&Key::Storage(contract, slot)).copied().unwrap_or(0),
n as u128,
"counter not serialized at {threads} threads"
);
}
}
#[test]
fn parallel_equals_sequential_random() {
for seed in 0..40u64 {
let cfg = WorkloadConfig {
num_txns: 300,
num_accounts: 400,
num_hot_accounts: 16,
num_contracts: 2,
num_hot_slots: 2,
conflict_ppm: 400_000,
gas: 2,
seed,
};
let base = make_base_state(&cfg);
let txns = generate_batch(&cfg);
let seq = execute_block_sequential(&base, &txns);
for threads in [1usize, 3, 8, 16] {
let (par, _) = execute_block_parallel(&base, &txns, threads);
assert_eq!(par, seq, "seed {seed} threads {threads}");
}
}
}
/// Advisory perfect prediction must NEVER change the committed state: the
/// predicted arm has to equal the sequential oracle on every profile/thread
/// count, exactly like the plain arm. This proves prediction is truly
/// advisory (it reorders attempts, not results).
#[test]
fn predicted_equals_sequential_random() {
for seed in 0..40u64 {
let cfg = WorkloadConfig {
num_txns: 300,
num_accounts: 400,
num_hot_accounts: 16,
num_contracts: 2,
num_hot_slots: 2,
conflict_ppm: 400_000,
gas: 2,
seed,
};
let base = make_base_state(&cfg);
let txns = generate_batch(&cfg);
let seq = execute_block_sequential(&base, &txns);
let preds = compute_predicted_preds(&base, &txns);
for threads in [1usize, 3, 8, 16] {
let (par, _) =
execute_block_parallel_predicted(&base, &txns, threads, preds.clone());
assert_eq!(par, seq, "predicted seed {seed} threads {threads}");
}
}
}
/// On the pathological all-same-slot batch, perfect prediction should drive
/// aborts to zero (every increment is pre-parked behind its predecessor),
/// while remaining serializable. The plain arm may abort; the predicted arm
/// must both be correct AND have aborts <= the plain arm.
#[test]
fn prediction_eliminates_aborts_on_hot_slot() {
let contract = 0u64;
let slot = 0u64;
let n = 400usize;
let mut base = StateMap::new();
base.insert(Key::Storage(contract, slot), 0);
let txns: Vec<Txn> = (0..n)
.map(|_| Txn {
kind: TxKind::Increment { contract, slot },
gas: 0,
})
.collect();
let preds = compute_predicted_preds(&base, &txns);
for threads in [2usize, 4, 8, 16] {
let (par_no, s_no) = execute_block_parallel(&base, &txns, threads);
let (par_yes, s_yes) =
execute_block_parallel_predicted(&base, &txns, threads, preds.clone());
// correctness: both equal exactly n
assert_eq!(par_no.get(&Key::Storage(contract, slot)).copied().unwrap_or(0), n as u128);
assert_eq!(par_yes.get(&Key::Storage(contract, slot)).copied().unwrap_or(0), n as u128);
// perfect prediction must not INCREASE aborts, and here should zero them
assert!(
s_yes.total_aborts <= s_no.total_aborts,
"prediction increased aborts at {threads}t: {} > {}",
s_yes.total_aborts, s_no.total_aborts
);
assert_eq!(
s_yes.total_aborts, 0,
"perfect prediction should eliminate aborts on a pure chain at {threads}t (got {})",
s_yes.total_aborts
);
}
}
#[test]
fn keccak_known_answer() {
// keccak256("") = c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
let h = aere_block_stm::keccak::keccak256(b"");
assert_eq!(
hex32(&h),
"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
);
// keccak256("abc") = 4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45
let h2 = aere_block_stm::keccak::keccak256(b"abc");
assert_eq!(
hex32(&h2),
"0x4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45"
);
}
}
+182
View File
@@ -0,0 +1,182 @@
//! Multi-version shared memory (MVMemory).
//!
//! For every `Key`, MVMemory keeps an ordered map `txn_index -> cell`, where a
//! cell is either a concrete `Value` written by that transaction's latest
//! incarnation, or an `Estimate` placeholder (a marker that a lower transaction
//! *previously* wrote here but is currently being re-executed, so its value is
//! not trustworthy).
//!
//! A read by transaction `i` for key `k` returns the entry of the HIGHEST
//! transaction `j < i` that wrote `k`. That is what makes speculative parallel
//! execution serial-equivalent: transaction `i` always observes the writes of
//! all lower-indexed transactions that have run, and nothing from higher ones.
//!
//! Storage is sharded across many independent mutexes keyed by a hash of the
//! `Key`, so non-conflicting transactions touch disjoint locks and run without
//! contention. Hot keys (the counter, the AMM pool) serialize on one shard,
//! which is exactly the honest source of the high-conflict slowdown.
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Mutex;
use crate::types::{Incarnation, Key, TxnIndex, Value};
const SHARDS: usize = 256;
/// One stored cell for a (key, txn_index) pair.
#[derive(Clone, Debug)]
enum Cell {
/// A concrete value written by this txn's incarnation.
Value(Incarnation, Value),
/// Placeholder: this txn wrote here before but is being re-executed.
Estimate,
}
/// What a read observed. This is recorded into the reader's read set so that
/// validation can later re-check that the read would still return the same
/// thing.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ReadDescriptor {
/// Nothing below the reader wrote this key: it came from base state.
Base,
/// It was written by `(txn_index, incarnation)`.
Version(TxnIndex, Incarnation),
}
/// The result of a live read against MVMemory.
pub enum ReadResult {
/// Read resolved to a concrete value with the given provenance.
Ok(ReadDescriptor, Value),
/// Read hit an ESTIMATE written by `blocking_txn`; the reader must abort and
/// register a dependency on `blocking_txn`.
Blocked(TxnIndex),
}
pub struct MvMemory {
shards: Vec<Mutex<HashMap<Key, BTreeMap<TxnIndex, Cell>>>>,
}
fn shard_of(key: &Key) -> usize {
// Cheap, deterministic spread. FNV-1a over the key's discriminant + fields.
let mut h: u64 = 0xcbf29ce484222325;
let bytes: [u64; 3] = match key {
Key::Balance(a) => [1, *a, 0],
Key::Storage(c, s) => [2, *c, *s],
};
for b in bytes {
h ^= b;
h = h.wrapping_mul(0x100000001b3);
}
(h as usize) % SHARDS
}
impl MvMemory {
pub fn new() -> Self {
let mut shards = Vec::with_capacity(SHARDS);
for _ in 0..SHARDS {
shards.push(Mutex::new(HashMap::new()));
}
MvMemory { shards }
}
/// Read the value visible to `txn_idx`: the highest entry strictly below it.
pub fn read(&self, key: &Key, txn_idx: TxnIndex) -> ReadResult {
let shard = &self.shards[shard_of(key)];
let guard = shard.lock().unwrap();
if let Some(versions) = guard.get(key) {
// highest entry with index < txn_idx
if let Some((&j, cell)) = versions.range(..txn_idx).next_back() {
return match cell {
Cell::Value(inc, v) => ReadResult::Ok(ReadDescriptor::Version(j, *inc), *v),
Cell::Estimate => ReadResult::Blocked(j),
};
}
}
ReadResult::Ok(ReadDescriptor::Base, 0)
}
/// Apply `txn_idx`'s new write set (incarnation `inc`). `prev_keys` is the
/// set of keys the previous incarnation wrote, so we can (a) delete entries
/// this incarnation no longer writes and (b) decide whether the *set* of
/// written keys changed, which downstream validation depends on.
///
/// Returns `true` if the written key-set differs from the previous one
/// (a key was added or removed) -- Block-STM calls this "wrote a new path"
/// and uses it to trigger revalidation of the suffix.
pub fn apply_write_set(
&self,
txn_idx: TxnIndex,
inc: Incarnation,
writes: &HashMap<Key, Value>,
prev_keys: &std::collections::HashSet<Key>,
) -> bool {
// Install / overwrite current writes.
for (k, v) in writes {
let shard = &self.shards[shard_of(k)];
let mut guard = shard.lock().unwrap();
guard
.entry(*k)
.or_insert_with(BTreeMap::new)
.insert(txn_idx, Cell::Value(inc, *v));
}
// Remove stale entries: keys written previously but not now.
let mut removed_any = false;
for k in prev_keys {
if !writes.contains_key(k) {
let shard = &self.shards[shard_of(k)];
let mut guard = shard.lock().unwrap();
if let Some(versions) = guard.get_mut(k) {
versions.remove(&txn_idx);
}
removed_any = true;
}
}
// Added a key not previously written?
let mut added_any = false;
for k in writes.keys() {
if !prev_keys.contains(k) {
added_any = true;
break;
}
}
added_any || removed_any
}
/// Convert all of `txn_idx`'s current writes (over `keys`) into ESTIMATE
/// placeholders. Called the moment a transaction is validation-aborted, so
/// that any concurrent higher transaction that reads one of these keys will
/// block on `txn_idx` instead of consuming a soon-to-be-wrong value. This is
/// what protects the suffix during a re-execution window.
pub fn mark_estimates(&self, txn_idx: TxnIndex, keys: &std::collections::HashSet<Key>) {
for k in keys {
let shard = &self.shards[shard_of(k)];
let mut guard = shard.lock().unwrap();
if let Some(versions) = guard.get_mut(k) {
if let Some(cell) = versions.get_mut(&txn_idx) {
*cell = Cell::Estimate;
}
}
}
}
/// Materialize the final committed state: base overlaid with, for each key,
/// the value written by the HIGHEST transaction index that wrote a concrete
/// value to it. Called once after the block is fully executed & validated.
pub fn snapshot_final(&self, base: &crate::types::StateMap) -> crate::types::StateMap {
let mut out = base.clone();
for shard in &self.shards {
let guard = shard.lock().unwrap();
for (k, versions) in guard.iter() {
// highest concrete value wins
for (_, cell) in versions.iter().rev() {
if let Cell::Value(_, v) = cell {
out.insert(*k, *v);
break;
}
}
}
}
out
}
}
+354
View File
@@ -0,0 +1,354 @@
//! The parallel Block-STM executor driver.
//!
//! Spawns `num_threads` workers. Each worker loops: ask the scheduler for a
//! task, run it (execute or validate), feed the result back to the scheduler,
//! repeat, until the scheduler says Done. Shared state lives in the MVMemory and
//! the per-txn read-set / written-key-set tables.
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};
use std::thread;
use crate::mvmemory::{MvMemory, ReadDescriptor, ReadResult};
use crate::scheduler::{Scheduler, Task};
use crate::types::{Key, StateMap, TxnIndex, Txn, Value};
use crate::vm::{execute_txn, VmView};
/// Aggregate stats returned alongside the final state.
#[derive(Clone, Debug, Default)]
pub struct ExecStats {
pub num_txns: usize,
pub num_threads: usize,
/// Total incarnations executed (executions >= num_txns; excess = re-executions).
pub total_executions: usize,
/// Total validation tasks run.
pub total_validations: usize,
/// Total aborts (validation failures that triggered re-execution).
pub total_aborts: usize,
/// Number of times a transaction's first/retry execution attempt was
/// pre-parked behind a predicted conflicting predecessor. 0 when prediction
/// is disabled. This is advisory bookkeeping only.
pub total_predicted_parks: usize,
}
/// A capturing view over MVMemory for one transaction execution.
struct MvView<'a> {
txn_idx: TxnIndex,
mv: &'a MvMemory,
/// immutable base state; a read that resolves to `Base` returns from here
base: &'a StateMap,
/// buffered writes for read-your-writes and later commit
writes: HashMap<Key, Value>,
/// recorded read set: (key, what-we-observed)
reads: Vec<(Key, ReadDescriptor)>,
/// set when a read hit an ESTIMATE; execution aborts and we park on this txn
blocked_on: Option<TxnIndex>,
}
impl<'a> VmView for MvView<'a> {
fn read(&mut self, key: Key) -> Option<Value> {
// read-your-writes first (not recorded as a dependency read)
if let Some(v) = self.writes.get(&key) {
return Some(*v);
}
match self.mv.read(&key, self.txn_idx) {
ReadResult::Ok(ReadDescriptor::Base, _) => {
// no lower txn wrote this key: value comes from immutable base state
self.reads.push((key, ReadDescriptor::Base));
Some(*self.base.get(&key).unwrap_or(&0))
}
ReadResult::Ok(desc, v) => {
self.reads.push((key, desc));
Some(v)
}
ReadResult::Blocked(blocking) => {
self.blocked_on = Some(blocking);
None
}
}
}
fn write(&mut self, key: Key, value: Value) {
self.writes.insert(key, value);
}
}
struct Shared {
scheduler: Scheduler,
mv: MvMemory,
txns: Vec<Txn>,
base: StateMap,
/// last recorded read set per txn (for validation)
last_reads: Vec<Mutex<Vec<(Key, ReadDescriptor)>>>,
/// keys written by the last incarnation per txn (for stale-entry cleanup)
last_write_keys: Vec<Mutex<HashSet<Key>>>,
/// PERFECT predicted soft-dependencies, `Some` only when prediction is on.
/// `predicted_preds[i]` = ascending lower-index transactions that conflict
/// with `i` (highest prior writer per key `i` reads). Advisory only; never
/// gates correctness. See `predict.rs`.
predicted_preds: Option<Vec<Vec<TxnIndex>>>,
// counters
executions: std::sync::atomic::AtomicUsize,
validations: std::sync::atomic::AtomicUsize,
aborts: std::sync::atomic::AtomicUsize,
predicted_parks: std::sync::atomic::AtomicUsize,
}
/// Execute one incarnation of `idx`. Returns Some(follow-up task) or None
/// (worker should ask the scheduler for the next task). Loops internally to
/// re-run immediately when a dependency was already resolved.
fn try_execute(shared: &Shared, mut idx: TxnIndex, mut inc: usize) -> Task {
loop {
// ADVISORY predictive pre-parking (perfect-prediction upper bound).
// Before running the VM, if we have predicted conflicting predecessors
// for `idx`, park behind the HIGHEST one that has not yet reached
// `Executed`. Re-checked on every (re)attempt, so by the time `idx`
// actually executes all of its conflicting predecessors are committed and
// its optimistic reads see final values -> the abort is avoided instead of
// paid-for-then-undone. This only changes *when* `idx` runs; the ordinary
// read-set validation below is unchanged and remains the sole authority on
// the committed state, so a wrong/approximate prediction can never corrupt
// the result -- at worst it fails to prevent an abort.
if let Some(preds) = &shared.predicted_preds {
let mut blocker = None;
for &j in preds[idx].iter().rev() {
if !shared.scheduler.is_executed(j) {
blocker = Some(j);
break;
}
}
if let Some(b) = blocker {
if shared.scheduler.add_dependency(idx, b) {
shared
.predicted_parks
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return Task::NoTask; // parked; task dropped, worker asks for next
}
// `b` reached Executed between our check and the park attempt;
// re-scan for the next still-pending predecessor (or none).
continue;
}
}
let mut view = MvView {
txn_idx: idx,
mv: &shared.mv,
base: &shared.base,
writes: HashMap::new(),
reads: Vec::new(),
blocked_on: None,
};
shared
.executions
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let completed = execute_txn(&shared.txns[idx], &mut view);
if completed.is_none() {
// hit an ESTIMATE -> either park, or (if already resolved) retry
let blocking = view.blocked_on.expect("blocked without a blocker");
if shared.scheduler.add_dependency(idx, blocking) {
return Task::NoTask; // parked; task dropped
} else {
continue; // dependency resolved meanwhile; re-execute same version
}
}
// Completed. Commit read set + apply write set to MVMemory.
let prev_keys: HashSet<Key> = {
let g = shared.last_write_keys[idx].lock().unwrap();
g.clone()
};
let new_keys: HashSet<Key> = view.writes.keys().cloned().collect();
let wrote_new_path =
shared
.mv
.apply_write_set(idx, inc, &view.writes, &prev_keys);
*shared.last_reads[idx].lock().unwrap() = view.reads;
*shared.last_write_keys[idx].lock().unwrap() = new_keys;
let t = shared
.scheduler
.finish_execution(idx, inc, wrote_new_path);
// finish_execution may hand us a Validation task for the same idx; if it
// hands an Execution task that never happens (finish_execution only
// returns Validation/NoTask), so just return it.
let _ = (&mut idx, &mut inc);
return t;
}
}
/// Validate `idx`'s recorded read set against current MVMemory. Returns a
/// follow-up task (a re-execution) or NoTask.
fn try_validate(shared: &Shared, idx: TxnIndex, inc: usize) -> Task {
shared
.validations
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let reads = { shared.last_reads[idx].lock().unwrap().clone() };
let mut valid = true;
for (key, observed) in &reads {
let now = shared.mv.read(key, idx);
let matches = match (observed, now) {
(ReadDescriptor::Base, ReadResult::Ok(ReadDescriptor::Base, _)) => true,
(
ReadDescriptor::Version(j, k),
ReadResult::Ok(ReadDescriptor::Version(j2, k2), _),
) => *j == j2 && *k == k2,
// Estimate now, or provenance changed -> invalid
_ => false,
};
if !matches {
valid = false;
break;
}
}
let aborted = !valid && shared.scheduler.try_validation_abort(idx, inc);
if aborted {
shared
.aborts
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
// mark this txn's writes as ESTIMATE so concurrent readers block on it
let keys = { shared.last_write_keys[idx].lock().unwrap().clone() };
shared.mv.mark_estimates(idx, &keys);
}
shared.scheduler.finish_validation(idx, aborted)
}
fn worker(shared: &Shared) {
let mut task = shared.scheduler.next_task();
loop {
task = match task {
Task::Done => break,
Task::NoTask => shared.scheduler.next_task(),
Task::Execution(idx, inc) => try_execute(shared, idx, inc),
Task::Validation(idx, inc) => try_validate(shared, idx, inc),
};
}
}
/// Execute `txns` starting from `base` using Block-STM on `num_threads` threads.
/// Returns the final committed state plus execution statistics.
///
/// This is the original, unchanged public entry point: it runs pure Block-STM
/// with NO prediction (the `WITHOUT` arm of the A/B experiment).
pub fn execute_block_parallel(
base: &StateMap,
txns: &[Txn],
num_threads: usize,
) -> (StateMap, ExecStats) {
let (s, st, _e, _c) = execute_block_parallel_inner(base, txns, num_threads, None);
(s, st)
}
/// Same as [`execute_block_parallel`] but ALSO returns a phase-timing split for
/// honest end-to-end pipeline measurement:
/// - `exec` = wall time of the PARALLEL Block-STM phase (workers running:
/// execution + validation + re-execution). This is the
/// parallelizable part; it is what scales with `num_threads`.
/// - `commit` = wall time of `MvMemory::snapshot_final`, the SERIAL
/// materialization of the multi-version memory into the flat
/// committed `StateMap`. This does NOT parallelize.
///
/// The caller adds the (also serial) `keccak::state_root` cost on top to obtain
/// the full block-production pipeline time. Splitting the phases lets us report
/// the real Amdahl serial fraction rather than an execution-phase-only speedup.
pub fn execute_block_parallel_split(
base: &StateMap,
txns: &[Txn],
num_threads: usize,
) -> (StateMap, ExecStats, std::time::Duration, std::time::Duration) {
execute_block_parallel_inner(base, txns, num_threads, None)
}
/// Same as [`execute_block_parallel`] but with advisory predictive pre-parking
/// enabled: `predicted_preds[i]` lists the lower-index transactions `i`
/// conflicts with (see [`crate::predict::compute_predicted_preds`]). This is the
/// `WITH` arm of the A/B experiment. The predictions are advisory only and never
/// change the committed state; they only reduce optimistic aborts.
pub fn execute_block_parallel_predicted(
base: &StateMap,
txns: &[Txn],
num_threads: usize,
predicted_preds: Vec<Vec<TxnIndex>>,
) -> (StateMap, ExecStats) {
let (s, st, _e, _c) =
execute_block_parallel_inner(base, txns, num_threads, Some(predicted_preds));
(s, st)
}
fn execute_block_parallel_inner(
base: &StateMap,
txns: &[Txn],
num_threads: usize,
predicted_preds: Option<Vec<Vec<TxnIndex>>>,
) -> (StateMap, ExecStats, std::time::Duration, std::time::Duration) {
let n = txns.len();
if n == 0 {
return (
base.clone(),
ExecStats {
num_threads,
..Default::default()
},
std::time::Duration::ZERO,
std::time::Duration::ZERO,
);
}
debug_assert!(
predicted_preds.as_ref().map(|p| p.len()).unwrap_or(n) == n,
"predicted_preds must have one entry per transaction"
);
let mut last_reads = Vec::with_capacity(n);
let mut last_write_keys = Vec::with_capacity(n);
for _ in 0..n {
last_reads.push(Mutex::new(Vec::new()));
last_write_keys.push(Mutex::new(HashSet::new()));
}
let shared = Arc::new(Shared {
scheduler: Scheduler::new(n),
mv: MvMemory::new(),
txns: txns.to_vec(),
base: base.clone(),
last_reads,
last_write_keys,
predicted_preds,
executions: std::sync::atomic::AtomicUsize::new(0),
validations: std::sync::atomic::AtomicUsize::new(0),
aborts: std::sync::atomic::AtomicUsize::new(0),
predicted_parks: std::sync::atomic::AtomicUsize::new(0),
});
let threads = num_threads.max(1);
let exec_t0 = std::time::Instant::now();
let mut handles = Vec::with_capacity(threads);
for _ in 0..threads {
let s = Arc::clone(&shared);
handles.push(thread::spawn(move || worker(&s)));
}
for h in handles {
h.join().unwrap();
}
let exec_dur = exec_t0.elapsed();
let commit_t0 = std::time::Instant::now();
let final_state = shared.mv.snapshot_final(&shared.base);
let commit_dur = commit_t0.elapsed();
let stats = ExecStats {
num_txns: n,
num_threads: threads,
total_executions: shared
.executions
.load(std::sync::atomic::Ordering::Relaxed),
total_validations: shared
.validations
.load(std::sync::atomic::Ordering::Relaxed),
total_aborts: shared.aborts.load(std::sync::atomic::Ordering::Relaxed),
total_predicted_parks: shared
.predicted_parks
.load(std::sync::atomic::Ordering::Relaxed),
};
(final_state, stats, exec_dur, commit_dur)
}
+162
View File
@@ -0,0 +1,162 @@
//! PERFECT predicted soft-dependencies (theoretical upper bound of a predictive
//! scheduler).
//!
//! MOTIVATION
//! ----------
//! Block-STM discovers conflicts *dynamically*: a transaction executes
//! optimistically, and only when validation catches a stale read does it abort
//! and re-execute. Aborts are wasted work. A "predictive scheduler" tries to
//! avoid that waste by predicting conflicts up front and pre-ordering / parking
//! conflicting transactions so their first execution already sees the right
//! values.
//!
//! The open question is how much headroom such a predictor could ever buy. To
//! answer it we cheat in the predictor's favor: we compute the EXACT read/write
//! sets from the real serial execution of the batch, so the prediction is
//! PERFECT. Any real predictor (static analysis, access-list hints, a learned
//! model) is strictly worse than this oracle, so the abort-rate reduction and
//! wall-time delta measured here are an UPPER BOUND on what prediction can do.
//!
//! ADVISORY-ONLY (correctness invariant)
//! -------------------------------------
//! The predicted predecessors are used ONLY to decide *when* to (re-)attempt a
//! transaction's first execution -- i.e. to pre-park it behind conflicting
//! lower-index transactions. They never gate the committed result: the ordinary
//! Block-STM read-set validation still runs and is the sole authority on
//! correctness. If a prediction is wrong (or, in a real deployment, merely
//! approximate) the worst that can happen is a transaction executes too early
//! and aborts exactly as it would have without any prediction. Predictions can
//! therefore NEVER produce an incorrect state root; at worst they fail to help.
//!
//! WHY THE HIGHEST PRIOR WRITER PER READ KEY IS SUFFICIENT
//! ------------------------------------------------------
//! A Block-STM validation abort is caused by a READ whose provenance changed:
//! the transaction read some key `k` and the value it observed was later
//! superseded by a lower-index write. In the true serial order the value of `k`
//! that transaction `i` must observe is written by the HIGHEST-index transaction
//! `j < i` that writes `k`. If every such `j` (over all keys `i` reads) has
//! already reached `Executed` before `i` first runs, then `i`'s reads all resolve
//! to their final serial values and `i` cannot be validation-aborted. So the
//! minimal perfect predecessor set for `i` is exactly
//! { last_writer(k) : k in reads(i) }.
//! Parking `i` behind the maximum still-pending member of that set, re-checking
//! each time it is resumed, guarantees all of them are `Executed` before `i`
//! runs. This is what [`compute_predicted_preds`] returns.
use std::collections::{HashMap, HashSet};
use crate::types::{Key, StateMap, Txn, Value};
use crate::vm::{execute_txn, VmView};
/// A `VmView` that executes against a live state map (exactly like the
/// sequential oracle) while additionally recording the set of keys read and the
/// set of keys written. Because it writes through to `state`, later transactions
/// observe committed values, so the recorded sets are the true serial-order
/// read/write sets.
struct RecordingView<'a> {
state: &'a mut StateMap,
reads: HashSet<Key>,
writes: HashSet<Key>,
}
impl<'a> VmView for RecordingView<'a> {
fn read(&mut self, key: Key) -> Option<Value> {
self.reads.insert(key);
Some(*self.state.get(&key).unwrap_or(&0))
}
fn write(&mut self, key: Key, value: Value) {
self.writes.insert(key);
self.state.insert(key, value);
}
}
/// Compute, for every transaction, its PERFECT soft-dependency predecessors: the
/// ascending, de-duplicated list of lower-index transactions that wrote a key
/// this transaction reads (keeping, per read key, the highest such writer).
///
/// This is a single extra sequential pass over the batch (same cost class as the
/// sequential oracle plus O(reads) bookkeeping). It is only ever used to drive
/// advisory pre-parking; see the module docs for the correctness argument.
pub fn compute_predicted_preds(base: &StateMap, txns: &[Txn]) -> Vec<Vec<usize>> {
let mut state = base.clone();
// key -> highest txn index seen so far that wrote it
let mut last_writer: HashMap<Key, usize> = HashMap::new();
let mut preds: Vec<Vec<usize>> = Vec::with_capacity(txns.len());
for (i, txn) in txns.iter().enumerate() {
let mut view = RecordingView {
state: &mut state,
reads: HashSet::new(),
writes: HashSet::new(),
};
// Sequential execution never aborts (the recording view never returns
// None), so this always completes.
let _ = execute_txn(txn, &mut view);
// Dependency = highest prior writer of each key we READ. `last_writer`
// only holds entries for j < i because we update it *after* this txn, so
// a transaction never lists itself (read-your-own-write is not a
// cross-transaction dependency).
let mut dep_set: HashSet<usize> = HashSet::new();
for k in &view.reads {
if let Some(&j) = last_writer.get(k) {
dep_set.insert(j);
}
}
let mut deps: Vec<usize> = dep_set.into_iter().collect();
deps.sort_unstable();
preds.push(deps);
// Record this transaction as the newest writer of each key it wrote.
for k in &view.writes {
last_writer.insert(*k, i);
}
}
preds
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{Key, StateMap, TxKind, Txn};
#[test]
fn shared_counter_forms_a_chain() {
// N txns incrementing the same slot: perfect prediction should make each
// txn depend on exactly the immediately preceding one (a linear chain).
let mut base = StateMap::new();
base.insert(Key::Storage(0, 0), 0);
let n = 8usize;
let txns: Vec<Txn> = (0..n)
.map(|_| Txn {
kind: TxKind::Increment { contract: 0, slot: 0 },
gas: 0,
})
.collect();
let preds = compute_predicted_preds(&base, &txns);
assert_eq!(preds[0], Vec::<usize>::new());
for i in 1..n {
assert_eq!(preds[i], vec![i - 1], "txn {i} should depend on {}", i - 1);
}
}
#[test]
fn disjoint_slots_have_no_dependencies() {
// Increments of DISTINCT slots never conflict, so no predecessors.
let mut base = StateMap::new();
for s in 0..6u64 {
base.insert(Key::Storage(0, s), 0);
}
let txns: Vec<Txn> = (0..6u64)
.map(|s| Txn {
kind: TxKind::Increment { contract: 0, slot: s },
gas: 0,
})
.collect();
let preds = compute_predicted_preds(&base, &txns);
for (i, p) in preds.iter().enumerate() {
assert!(p.is_empty(), "txn {i} should have no predecessors, got {p:?}");
}
}
}
+293
View File
@@ -0,0 +1,293 @@
//! The Block-STM collaborative scheduler (Gelashvili et al. 2022, Algorithm 3).
//!
//! It hands out two kinds of task to a pool of worker threads:
//! - EXECUTION tasks: run incarnation `k` of transaction `i`.
//! - VALIDATION tasks: re-check that an already-executed transaction's read
//! set is still valid against current MVMemory.
//!
//! Two monotone-ish cursors drive it: `execution_idx` and `validation_idx`.
//! Validation of a lower index always takes priority over execution of a higher
//! one. When a transaction aborts (validation fails) or writes a new path, the
//! relevant cursor is *decreased* so the affected range is revisited. The block
//! is done when both cursors have passed the end AND no task is in flight AND no
//! decrease happened during the check.
//!
//! Dependencies are handled WITHOUT condvars: when execution of `i` reads an
//! ESTIMATE from `j`, `i` is parked on `j`'s dependency list and its task is
//! dropped; when `j` finishes it re-arms `i` (ReadyToExecute) and decreases
//! `execution_idx` so a worker re-picks it.
//!
//! Correctness of this module is not asserted by inspection -- it is proven
//! empirically by the harness, which runs thousands of adversarial, conflicting
//! batches and checks that the committed state equals the sequential oracle.
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Mutex;
use crate::types::{Incarnation, TxnIndex};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Status {
ReadyToExecute(Incarnation),
Executing(Incarnation),
Suspended(Incarnation),
Executed(Incarnation),
Aborting(Incarnation),
}
/// A unit of work for a worker, or a control signal.
#[derive(Clone, Copy, Debug)]
pub enum Task {
Execution(TxnIndex, Incarnation),
Validation(TxnIndex, Incarnation),
NoTask,
Done,
}
pub struct Scheduler {
num_txns: usize,
execution_idx: AtomicUsize,
validation_idx: AtomicUsize,
decrease_cnt: AtomicUsize,
num_active_tasks: AtomicUsize,
done: AtomicBool,
status: Vec<Mutex<Status>>,
dependencies: Vec<Mutex<Vec<TxnIndex>>>,
}
impl Scheduler {
pub fn new(num_txns: usize) -> Self {
let mut status = Vec::with_capacity(num_txns);
let mut dependencies = Vec::with_capacity(num_txns);
for _ in 0..num_txns {
status.push(Mutex::new(Status::ReadyToExecute(0)));
dependencies.push(Mutex::new(Vec::new()));
}
Scheduler {
num_txns,
execution_idx: AtomicUsize::new(0),
validation_idx: AtomicUsize::new(0),
decrease_cnt: AtomicUsize::new(0),
num_active_tasks: AtomicUsize::new(0),
done: AtomicBool::new(false),
status,
dependencies,
}
}
pub fn done(&self) -> bool {
self.done.load(Ordering::SeqCst)
}
/// True iff `idx` is currently in the `Executed` state. Used ONLY by the
/// advisory predictive pre-parking hook (see `predict.rs`): before a
/// transaction's first execution we check whether each predicted conflicting
/// predecessor has already reached `Executed`, and if not we park on it. This
/// never affects the committed result -- it only reorders *when* work is
/// attempted. A predecessor that has aborted and is re-executing reports
/// `false` here, which correctly causes the dependent to wait for it again.
pub fn is_executed(&self, idx: TxnIndex) -> bool {
matches!(*self.status[idx].lock().unwrap(), Status::Executed(_))
}
fn decrease_execution_idx(&self, target: TxnIndex) {
self.execution_idx.fetch_min(target, Ordering::SeqCst);
self.decrease_cnt.fetch_add(1, Ordering::SeqCst);
}
fn decrease_validation_idx(&self, target: TxnIndex) {
self.validation_idx.fetch_min(target, Ordering::SeqCst);
self.decrease_cnt.fetch_add(1, Ordering::SeqCst);
}
fn check_done(&self) {
let observed = self.decrease_cnt.load(Ordering::SeqCst);
if self.execution_idx.load(Ordering::SeqCst) >= self.num_txns
&& self.validation_idx.load(Ordering::SeqCst) >= self.num_txns
&& self.num_active_tasks.load(Ordering::SeqCst) == 0
&& self.decrease_cnt.load(Ordering::SeqCst) == observed
{
self.done.store(true, Ordering::SeqCst);
}
}
/// Try to move `idx` from ReadyToExecute -> Executing. On success returns the
/// execution version (keeping the active-task unit). On failure it releases
/// the active-task unit and returns None.
fn try_incarnate(&self, idx: TxnIndex) -> Option<Task> {
if idx < self.num_txns {
let mut st = self.status[idx].lock().unwrap();
if let Status::ReadyToExecute(inc) = *st {
*st = Status::Executing(inc);
return Some(Task::Execution(idx, inc));
}
}
self.num_active_tasks.fetch_sub(1, Ordering::SeqCst);
None
}
fn next_version_to_execute(&self) -> Option<Task> {
if self.execution_idx.load(Ordering::SeqCst) >= self.num_txns {
self.check_done();
return None;
}
self.num_active_tasks.fetch_add(1, Ordering::SeqCst);
let idx = self.execution_idx.fetch_add(1, Ordering::SeqCst);
self.try_incarnate(idx)
}
fn next_version_to_validate(&self) -> Option<Task> {
if self.validation_idx.load(Ordering::SeqCst) >= self.num_txns {
self.check_done();
return None;
}
self.num_active_tasks.fetch_add(1, Ordering::SeqCst);
let idx = self.validation_idx.fetch_add(1, Ordering::SeqCst);
if idx < self.num_txns {
let st = *self.status[idx].lock().unwrap();
if let Status::Executed(inc) = st {
return Some(Task::Validation(idx, inc));
}
}
self.num_active_tasks.fetch_sub(1, Ordering::SeqCst);
None
}
/// Main entry point workers spin on. Returns a task, NoTask (retry), or Done.
pub fn next_task(&self) -> Task {
loop {
if self.done.load(Ordering::SeqCst) {
return Task::Done;
}
let v = self.validation_idx.load(Ordering::SeqCst);
let e = self.execution_idx.load(Ordering::SeqCst);
let task = if v < e {
self.next_version_to_validate()
} else {
self.next_version_to_execute()
};
match task {
Some(t) => return t,
None => {
// both cursors may be exhausted; loop re-checks done marker
if self.done.load(Ordering::SeqCst) {
return Task::Done;
}
continue;
}
}
}
}
/// Register that `idx` depends on `blocking` (it read `blocking`'s ESTIMATE).
/// Returns true if `idx` was parked (caller drops the task), false if
/// `blocking` already finished (caller should just re-execute `idx`).
pub fn add_dependency(&self, idx: TxnIndex, blocking: TxnIndex) -> bool {
// Hold the blocking txn's status lock across the whole critical section so
// it cannot transition to Executed and drain its dependency list between
// our check and our push (which would lose the wakeup).
let bstatus = self.status[blocking].lock().unwrap();
if let Status::Executed(_) = *bstatus {
return false; // resolved already; re-execute instead of parking
}
self.dependencies[blocking].lock().unwrap().push(idx);
// park idx (it was Executing at its current incarnation)
{
let mut st = self.status[idx].lock().unwrap();
if let Status::Executing(inc) = *st {
*st = Status::Suspended(inc);
}
}
drop(bstatus);
self.num_active_tasks.fetch_sub(1, Ordering::SeqCst);
true
}
fn resume(&self, idx: TxnIndex) {
let mut st = self.status[idx].lock().unwrap();
if let Status::Suspended(inc) = *st {
*st = Status::ReadyToExecute(inc);
}
}
fn set_ready_after_abort(&self, idx: TxnIndex) {
let mut st = self.status[idx].lock().unwrap();
if let Status::Aborting(inc) = *st {
*st = Status::ReadyToExecute(inc + 1);
}
}
fn set_executed(&self, idx: TxnIndex, inc: Incarnation) {
let mut st = self.status[idx].lock().unwrap();
*st = Status::Executed(inc);
}
/// Called after an execution task completes. `wrote_new_path` = the set of
/// written keys changed vs the previous incarnation. Returns a follow-up
/// task (inheriting the active-task unit) or NoTask (releasing it).
pub fn finish_execution(
&self,
idx: TxnIndex,
inc: Incarnation,
wrote_new_path: bool,
) -> Task {
self.set_executed(idx, inc);
// Resume any transactions parked on this one.
let deps: Vec<TxnIndex> = {
let mut d = self.dependencies[idx].lock().unwrap();
std::mem::take(&mut *d)
};
if !deps.is_empty() {
let min_dep = *deps.iter().min().unwrap();
for d in &deps {
self.resume(*d);
}
self.decrease_execution_idx(min_dep);
}
if self.validation_idx.load(Ordering::SeqCst) > idx {
if wrote_new_path {
// new/removed keys can invalidate the suffix -> revalidate from idx
self.decrease_validation_idx(idx);
self.num_active_tasks.fetch_sub(1, Ordering::SeqCst);
Task::NoTask
} else {
// only this txn needs (re)validation right now
Task::Validation(idx, inc)
}
} else {
self.num_active_tasks.fetch_sub(1, Ordering::SeqCst);
Task::NoTask
}
}
/// Attempt to win the right to abort `idx` at `inc`. Returns true iff this
/// caller flipped Executed(inc) -> Aborting(inc). Used to ensure only one
/// validator aborts a given incarnation.
pub fn try_validation_abort(&self, idx: TxnIndex, inc: Incarnation) -> bool {
let mut st = self.status[idx].lock().unwrap();
if *st == Status::Executed(inc) {
*st = Status::Aborting(inc);
true
} else {
false
}
}
/// Called after a validation task. `aborted` = the validation failed AND we
/// won `try_validation_abort`. Returns a follow-up execution task (inheriting
/// the active-task unit) or NoTask (releasing it).
pub fn finish_validation(&self, idx: TxnIndex, aborted: bool) -> Task {
if aborted {
self.set_ready_after_abort(idx); // bump incarnation
self.decrease_validation_idx(idx + 1);
if self.execution_idx.load(Ordering::SeqCst) > idx {
// re-execute idx now (keeps the active-task unit if incarnate succeeds)
return self.try_incarnate(idx).unwrap_or(Task::NoTask);
}
}
self.num_active_tasks.fetch_sub(1, Ordering::SeqCst);
Task::NoTask
}
}
+30
View File
@@ -0,0 +1,30 @@
//! The sequential reference executor. This is the ORACLE: the definition of the
//! correct answer. Block-STM's output must equal this, byte for byte, for every
//! batch. It simply applies transactions in index order against a live map.
use crate::types::{Key, StateMap, Txn, Value};
use crate::vm::{execute_txn, VmView};
struct SeqView<'a> {
state: &'a mut StateMap,
}
impl<'a> VmView for SeqView<'a> {
fn read(&mut self, key: Key) -> Option<Value> {
Some(*self.state.get(&key).unwrap_or(&0))
}
fn write(&mut self, key: Key, value: Value) {
self.state.insert(key, value);
}
}
/// Execute `txns` in order starting from `base`, returning the final state.
pub fn execute_block_sequential(base: &StateMap, txns: &[Txn]) -> StateMap {
let mut state = base.clone();
for txn in txns {
let mut view = SeqView { state: &mut state };
// Sequential execution never aborts (no ESTIMATE), so this is always Some.
let _ = execute_txn(txn, &mut view);
}
state
}
+72
View File
@@ -0,0 +1,72 @@
//! Core value types shared across the executor.
//!
//! State model (intentionally EVM-shaped but minimal):
//! - `Key::Balance(account)` -> the native-token balance of an account
//! - `Key::Storage(contract, slot)` -> a single key/value contract storage slot
//!
//! Values are `u128`. This is enough to model realistic EVM-style workloads:
//! plain transfers (two balance touches), value-dependent sweeps, shared
//! counters (the canonical serializability stress test), and constant-product
//! AMM swaps (two-slot conflicting DeFi writes).
use std::collections::HashMap;
/// A single addressable state location.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum Key {
/// Native-token balance of `account`.
Balance(u64),
/// Contract storage: `(contract_id, slot)`.
Storage(u64, u64),
}
/// Stored value type. Absent key == 0.
pub type Value = u128;
/// A transaction version: `(txn_index, incarnation)`.
///
/// `txn_index` is the position of the transaction in the block's preferred
/// serialization order (0..n). `incarnation` counts how many times this
/// transaction has been (re-)executed; it starts at 0 and is bumped on abort.
pub type TxnIndex = usize;
pub type Incarnation = usize;
pub type Version = (TxnIndex, Incarnation);
/// The kinds of transaction the toy VM understands. Each one has genuine EVM
/// characteristics: conditional control flow that depends on reads, and writes
/// whose *values* depend on reads (so a stale read produces a wrong result and
/// MUST be caught by validation + re-execution).
#[derive(Clone, Debug)]
pub enum TxKind {
/// Move `amount` from `from` to `to` if `from` can afford it.
/// Control flow depends on reading `from`'s balance.
Transfer { from: u64, to: u64, amount: Value },
/// Move the ENTIRE balance of `from` into `to`. The written values depend
/// directly on the read balance, so a stale read is silently wrong unless
/// caught (a strong serializability probe).
Sweep { from: u64, to: u64 },
/// `storage[contract][slot] += 1`. When many txns hit the same slot this is
/// the classic all-conflict counter: sequential result is start + count, and
/// the parallel executor MUST reproduce exactly that despite reordering.
Increment { contract: u64, slot: u64 },
/// Constant-product-style swap against a two-slot pool. Reads and writes two
/// slots; models a hot DeFi pool that conflicts across concurrent swaps.
AmmSwap { contract: u64, x_slot: u64, y_slot: u64, dx: Value },
}
#[derive(Clone, Debug)]
pub struct Txn {
pub kind: TxKind,
/// Simulated EVM execution cost in "compute units". Real EVM transactions
/// spend microseconds in the interpreter (opcode dispatch, keccak, ecrecover,
/// SLOAD/SSTORE bookkeeping). A pure state-transition toy tx costs only
/// nanoseconds, so a parallel executor over it measures nothing but its own
/// scheduling overhead. `gas` makes each tx pay a realistic, deterministic
/// amount of CPU (identical in the sequential and parallel paths), so the
/// benchmark measures the executor doing real work. It does NOT affect the
/// resulting state, so correctness is independent of `gas`.
pub gas: u32,
}
/// A plain materialized state snapshot: `Key -> Value` (absent == 0).
pub type StateMap = HashMap<Key, Value>;
+113
View File
@@ -0,0 +1,113 @@
//! The transaction execution semantics.
//!
//! CRITICAL DESIGN POINT: there is exactly ONE implementation of what a
//! transaction *does*, `execute_txn`. It runs against an abstract `VmView`.
//!
//! - The SEQUENTIAL oracle drives it with a view backed directly by the live
//! state map.
//! - The PARALLEL Block-STM executor drives it with a view backed by the
//! multi-version memory (which records the read set and can signal that the
//! execution must abort because it read an ESTIMATE placeholder).
//!
//! Because both paths share this exact function, the parallel result can only
//! ever differ from the sequential result if the *concurrency control* is
//! wrong. That is precisely the property the correctness harness proves.
use crate::types::{Key, TxKind, Txn, Value};
/// Simulate the CPU cost of executing an EVM transaction: `gas` rounds of real
/// keccak256 seeded by `seed`. This is deterministic and input-dependent so the
/// optimizer cannot elide it, and it is pure (does not touch shared state), so
/// it models interpreter/hashing cost without adding artificial conflicts. Both
/// the sequential oracle and the parallel executor run this identically.
#[inline(never)]
fn evm_work(gas: u32, seed: u128) -> u128 {
let mut acc = seed ^ 0x9E3779B97F4A7C15_9E3779B97F4A7C15u128;
let mut buf = [0u8; 16];
for _ in 0..gas {
buf.copy_from_slice(&acc.to_le_bytes());
let h = crate::keccak::keccak256(&buf);
acc = u128::from_le_bytes(h[0..16].try_into().unwrap());
}
acc
}
fn work_seed(kind: &TxKind) -> u128 {
match kind {
TxKind::Transfer { from, to, amount } => {
(*from as u128) << 96 ^ (*to as u128) << 32 ^ *amount
}
TxKind::Sweep { from, to } => (*from as u128) << 64 ^ (*to as u128),
TxKind::Increment { contract, slot } => (*contract as u128) << 64 ^ (*slot as u128),
TxKind::AmmSwap { contract, dx, .. } => (*contract as u128) << 96 ^ *dx,
}
}
/// Abstract view over state that a transaction reads/writes.
pub trait VmView {
/// Read the current value of `key`.
///
/// Returns `None` to mean "this execution must abort right now" (the
/// parallel view returns `None` when it reads an ESTIMATE written by a
/// not-yet-final lower transaction). The sequential view never returns
/// `None`. Callers propagate `None` with `?`.
fn read(&mut self, key: Key) -> Option<Value>;
/// Buffer a write to `key`. Writes are visible to later reads *within the
/// same transaction* (read-your-writes).
fn write(&mut self, key: Key, value: Value);
}
/// Execute one transaction against `view`.
///
/// Returns `Some(())` on a completed execution and `None` if the execution was
/// aborted mid-way because a read hit an ESTIMATE (parallel path only). All
/// arithmetic is saturating so that both paths behave identically and never
/// panic on overflow.
pub fn execute_txn<V: VmView>(txn: &Txn, view: &mut V) -> Option<()> {
match &txn.kind {
TxKind::Transfer { from, to, amount } => {
let from_bal = view.read(Key::Balance(*from))?;
if from_bal >= *amount {
view.write(Key::Balance(*from), from_bal - *amount);
let to_bal = view.read(Key::Balance(*to))?;
view.write(Key::Balance(*to), to_bal.saturating_add(*amount));
}
// insufficient funds -> no-op (still a valid, committed no-op)
}
TxKind::Sweep { from, to } => {
let from_bal = view.read(Key::Balance(*from))?;
if from_bal > 0 {
view.write(Key::Balance(*from), 0);
let to_bal = view.read(Key::Balance(*to))?;
view.write(Key::Balance(*to), to_bal.saturating_add(from_bal));
}
}
TxKind::Increment { contract, slot } => {
let cur = view.read(Key::Storage(*contract, *slot))?;
view.write(Key::Storage(*contract, *slot), cur.saturating_add(1));
}
TxKind::AmmSwap {
contract,
x_slot,
y_slot,
dx,
} => {
let x = view.read(Key::Storage(*contract, *x_slot))?;
let y = view.read(Key::Storage(*contract, *y_slot))?;
let new_x = x.saturating_add(*dx);
if new_x > 0 {
// constant-product: dy = y * dx / (x + dx)
let dy = (y.saturating_mul(*dx)) / new_x;
view.write(Key::Storage(*contract, *x_slot), new_x);
let dy = if dy > y { y } else { dy };
view.write(Key::Storage(*contract, *y_slot), y - dy);
}
}
}
// Pay the simulated EVM execution cost. Pure CPU; does not affect state.
if txn.gas > 0 {
std::hint::black_box(evm_work(txn.gas, work_seed(&txn.kind)));
}
Some(())
}
+123
View File
@@ -0,0 +1,123 @@
//! Deterministic, seedable workload generation. No external RNG crate: a small
//! SplitMix64 gives reproducible batches so a failing case can be replayed
//! exactly. `conflict` in [0,1] tunes how often a transaction touches the small
//! "hot" set (shared accounts / the shared counter / the shared AMM pool),
//! which is what drives the abort rate and destroys parallelism when high.
use crate::types::{Key, StateMap, TxKind, Txn, Value};
pub struct SplitMix64 {
state: u64,
}
impl SplitMix64 {
pub fn new(seed: u64) -> Self {
SplitMix64 { state: seed }
}
pub fn next_u64(&mut self) -> u64 {
self.state = self.state.wrapping_add(0x9E3779B97F4A7C15);
let mut z = self.state;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D049BB133111EB);
z ^ (z >> 31)
}
pub fn below(&mut self, n: u64) -> u64 {
if n == 0 {
0
} else {
self.next_u64() % n
}
}
/// probability p in [0,1] scaled to 1e6.
pub fn chance(&mut self, p_ppm: u64) -> bool {
self.below(1_000_000) < p_ppm
}
}
pub struct WorkloadConfig {
pub num_txns: usize,
pub num_accounts: u64,
pub num_hot_accounts: u64,
pub num_contracts: u64,
pub num_hot_slots: u64,
/// probability (0..1e6 ppm) that a given account/slot pick is from the hot set
pub conflict_ppm: u64,
/// simulated EVM execution cost per tx (keccak rounds). 0 = trivial tx.
pub gas: u32,
pub seed: u64,
}
/// Build the base state: every account funded, hot slots seeded, AMM pools seeded.
pub fn make_base_state(cfg: &WorkloadConfig) -> StateMap {
let mut s = StateMap::new();
for a in 0..cfg.num_accounts {
s.insert(Key::Balance(a), 1_000_000u128);
}
for c in 0..cfg.num_contracts {
// slot layout: 0..num_hot_slots = counters; then two AMM slots 1000,1001
for slot in 0..cfg.num_hot_slots {
s.insert(Key::Storage(c, slot), 0);
}
s.insert(Key::Storage(c, 1000), 1_000_000u128); // AMM reserve x
s.insert(Key::Storage(c, 1001), 1_000_000u128); // AMM reserve y
}
s
}
impl WorkloadConfig {
fn pick_account(&self, rng: &mut SplitMix64) -> u64 {
if self.num_hot_accounts > 0 && rng.chance(self.conflict_ppm) {
rng.below(self.num_hot_accounts)
} else {
self.num_hot_accounts + rng.below(self.num_accounts - self.num_hot_accounts)
}
}
}
/// Generate a batch. Mix: transfers, sweeps, increments, AMM swaps.
pub fn generate_batch(cfg: &WorkloadConfig) -> Vec<Txn> {
let mut rng = SplitMix64::new(cfg.seed);
let mut txns = Vec::with_capacity(cfg.num_txns);
for _ in 0..cfg.num_txns {
let roll = rng.below(100);
let kind = if roll < 60 {
// 60% transfers
let from = cfg.pick_account(&mut rng);
let mut to = cfg.pick_account(&mut rng);
if to == from {
to = (to + 1) % cfg.num_accounts;
}
let amount = 1 + rng.below(1000) as Value;
TxKind::Transfer { from, to, amount }
} else if roll < 75 {
// 15% sweeps
let from = cfg.pick_account(&mut rng);
let mut to = cfg.pick_account(&mut rng);
if to == from {
to = (to + 1) % cfg.num_accounts;
}
TxKind::Sweep { from, to }
} else if roll < 90 {
// 15% shared-counter increments (the serializability stressor)
let contract = rng.below(cfg.num_contracts.max(1));
let slot = if cfg.num_hot_slots > 0 {
rng.below(cfg.num_hot_slots)
} else {
0
};
TxKind::Increment { contract, slot }
} else {
// 10% AMM swaps against a shared pool
let contract = rng.below(cfg.num_contracts.max(1));
let dx = 1 + rng.below(10_000) as Value;
TxKind::AmmSwap {
contract,
x_slot: 1000,
y_slot: 1001,
dx,
}
};
txns.push(Txn { kind, gas: cfg.gas });
}
txns
}