CultLib/packages/heimdall-stake/tests/golden.rs
Libby 84f932ec50
Some checks failed
CultMesh QUIC native / Build win32-x64 (pull_request) Has been skipped
CultMesh QUIC native / Build linux-x64 (pull_request) Has been skipped
CultMesh QUIC native / Release native binaries (pull_request) Has been skipped
CultMesh portability and performance / ubuntu-latest one-command multiplayer onboarding (pull_request) Has been cancelled
CultMesh portability and performance / windows-latest one-command multiplayer onboarding (pull_request) Has been cancelled
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Has been cancelled
CultMesh portability and performance / windows-latest reactive semantics and leases (pull_request) Has been cancelled
CultNet cross-runtime interop / C# / TypeScript / Rust / Python / Kotlin (pull_request) Has been cancelled
CultMesh QUIC native / Verify committed binaries match SHA256SUMS (pull_request) Has been cancelled
heimdall-stake: sybil note and golden vectors
README: the constant 1 in weight = 1 + log_b(amount/unit) is paid once per identity, so splitting
always pays until the log goes negative and no base fixes it; the defence lives outside the function.
Golden vectors (single and split k=1,2,10,100, three bases) computed with 60-digit decimal ln,
pinned by tests/golden.rs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-06 02:30:58 +00:00

30 lines
1.4 KiB
Rust

use heimdall_stake::*;
const VECTORS: &str = include_str!("vectors/stake_golden.csv");
/// Pins `weight` and `total_weight` to values computed outside this crate (see the fixture header).
/// Split rows are the sybil argument in numbers: the same budget in k identities weighs more.
#[test]
fn golden_vectors() {
let mut rows = 0;
let mut split_rows = 0;
for line in VECTORS.lines().filter(|l| !l.starts_with('#') && !l.trim().is_empty()) {
let f: Vec<&str> = line.split(',').collect();
let (base, unit, amount, k, expected): (u32, u64, i64, usize, u64) =
(f[1].parse().unwrap(), f[2].parse().unwrap(), f[3].parse().unwrap(), f[4].parse().unwrap(), f[5].parse().unwrap());
let p = Params { base, unit_cents: unit, floor_cents: 0, per_identity_cap: None, min_amount_per_identity_cents: 0 };
let got = match f[0] {
"single" => weight(&p, amount).unwrap(),
"split" => {
split_rows += 1;
let (q, r) = (amount / k as i64, (amount % k as i64) as usize);
let shares: Vec<i64> = (0..k).map(|i| q + i64::from(i < r)).collect();
total_weight(&p, &shares).unwrap()
}
other => panic!("unknown vector kind {other}"),
};
assert_eq!(got, expected, "vector `{line}`");
rows += 1;
}
assert!(rows >= 20 && split_rows >= 12, "fixture shrank: {rows} rows, {split_rows} split");
}