diff options
| author | Adam Soltys <[email protected]> | 2026-11-25 08:33:00 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-11-25 08:33:00 -0700 |
| commit | b23b02bdaae233f51b543bc60fa38a025e07a743 (patch) | |
| tree | 10e20e92b97d4942663007b6365e995b3f856d42 /tools/ark/isolate-dust-test.js | |
| parent | 2cd42f811d33a74c3ca28094176c009c0388da9b (diff) | |
| download | coinosv3-b23b02bdaae233f51b543bc60fa38a025e07a743.tar.gz coinosv3-b23b02bdaae233f51b543bc60fa38a025e07a743.zip | |
Ark sends follow server-0.9.0's dust rule, and a renewal checks its new coins before forfeiting for them. "yes go ahead" (Adam, 2026-11-25, on fixing the send code and the refresh check before rehearsing captaind 0.9.0).HEADmaster
Dust: 0.9.0 (!2822) refuses a cosign whose checkpoint carries an output under 330 sat unless the input itself is dust. Our split, a port of bark ≤0.8's new_isolate_dust, "accepted the mix" in two cases 0.9.0 now isolates: every output dust (100 sent from a 400-sat coin, 300 change), and dust no output can pad (100 from 500, the 400 change isolated whole). isolateDust now matches 0.9.0 case for case (upstream's eight isolate_dust tests ported). 0.8.0 refuses exactly those two new shapes instead ("At least one output is required"), so the split follows the server it talks to: a server advertising ArkInfo field 23 (max_round_participation_outputs, new in 0.9.0 and never 0 there) gets the new rule, any other the old one. Every arkoor build (sends, Lightning HTLC sends, refunds, sweeps, claims) passes it through.
Renewals: bark !2803 (reported by MrHodl) — bark didn't check that the unlock hash of its forfeits matched the one locking its new round coins; neither did we. A round's coins are now checked before the leaf cosign and the forfeit: a hash-locked round leaf locked by our unlock hash, anchored in this round's funding transaction, paid to our output key, worth at least what we asked for. On any mismatch nothing is signed, and the action keeps the reason.
Regtest: new dust-shapes-test sends all four shapes (all dust, isolate whole, pad, dust input) from single-coin wallets, delivers them and spends the change onward — passes on captaind 0.8.0 and on 0.9.0 (master's code fails the two new shapes on 0.9.0). manager, dust-gift, refresh-reprice, rescue-claimed, expired-dust, send-failure, custody, offboard-split and board tests pass on 0.9.0; ln-test and ln-e2e (25-sat zap-size sends, receives) pass on 0.9.0 with CLN v26.06.9; manager and dust-gift pass on 0.8.0. send, refresh, receive, scheduled-refresh and ln-pending-history fail the same way on master (stale fixtures). New isolate-dust-test (upstream vectors, 200k random payments against both servers' ported checks) and round-outputs-check-test pass; rescue-claimed-test stubs the new check (its fake rounds carry no coins). features-smoke and feature-registry-check pass.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VMan6Upic5L6MMJzkTn8zg
Diffstat (limited to 'tools/ark/isolate-dust-test.js')
| -rw-r--r-- | tools/ark/isolate-dust-test.js | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/tools/ark/isolate-dust-test.js b/tools/ark/isolate-dust-test.js new file mode 100644 index 0000000..1170d68 --- /dev/null +++ b/tools/ark/isolate-dust-test.js @@ -0,0 +1,98 @@ +// Dust isolation matches bark's ArkoorBuilder::new_isolate_dust (server-0.9.0) +// case for case, and every split it makes passes the arkoor amount checks of +// both server-0.8.0 and server-0.9.0 (lib/src/arkoor/mod.rs validate_amounts, +// ported below). 0.9.0 (!2822) refuses a checkpoint with a sub-dust output +// unless the input is dust; the old split "accepted the mix" in two cases. +// Run: bun tools/ark/isolate-dust-test.js +import assert from 'node:assert/strict'; +import { isolateDust } from '../../src/ark/send.js'; +import { ArkManager } from '../../src/ark/manager.js'; + +const D = 330; +const P = (n) => ({ type: 'pubkey', userPubkey: 'k' + n }); +const outs = (...a) => a.map((amountSat, i) => ({ amountSat, policy: P(i) })); +const amts = (l) => l.map((o) => o.amountSat); +const check = (name, ok, detail = '') => { assert(ok, name + (detail ? ' — ' + detail : '')); console.log(' ✓ ' + name); }; + +// validate_amounts, both versions. Returns null or the error. +function validate(inputSat, normal, iso, version) { + const sum = (l) => l.reduce((n, o) => n + o.amountSat, 0); + if (sum(normal) + sum(iso) !== inputSat) return 'Unbalanced'; + if (version === '0.9' && [...normal, ...iso].some((o) => o.amountSat === 0)) return 'ZeroValueOutput'; + if (iso.length && sum(iso) < D) return 'Dust'; + if (iso.some((o) => o.amountSat >= 2 * D)) return 'IsolationNotNeeded'; + if (iso.length && iso.every((o) => o.amountSat >= D)) return 'IsolationNotNeeded'; + if (version === '0.9') { + if (inputSat >= D && normal.some((o) => o.amountSat < D)) return 'Dust'; + } else { + // seen on regtest 0.8.0: "An error occurred while building arkoor: At least one output is required" + if (!normal.length) return 'NoOutputs'; + if (iso.length && normal.some((o) => o.amountSat < D)) return 'Dust'; + if (normal.some((o) => o.amountSat < D) && normal.some((o) => o.amountSat >= 2 * D)) return 'Dust'; + } + return null; +} + +// upstream's tests (lib/src/arkoor/mod.rs, isolate_dust_*) +const cases = [ + ['all non-dust', 1000, [500, 500], [500, 500], []], + ['all dust', 400, [200, 200], [], [200, 200]], + ['all dust from a dust input', 300, [200, 100], [200, 100], []], + ['sufficient dust', 1000, [600, 200, 200], [600], [200, 200]], + ['split successful', 1000, [800, 100, 100], [670], [130, 100, 100]], + ['split impossible', 600, [400, 100, 100], [], [400, 100, 100]], + ['split impossible isolates the smallest', 950, [450, 400, 100], [450], [400, 100]], + ['exactly at the boundary', 1000, [660, 170, 170], [660], [170, 170]], +]; +for (const [name, input, o, wantOuts, wantIso] of cases) { + const r = isolateDust(outs(...o), input); + check(`${name}: ${o.join('+')} → [${wantOuts}] + isolated [${wantIso}]`, + JSON.stringify(amts(r.outs)) === JSON.stringify(wantOuts) && JSON.stringify(amts(r.isolated)) === JSON.stringify(wantIso), + `got [${amts(r.outs)}] + [${amts(r.isolated)}]`); + check(`...which 0.9.0 accepts`, !validate(input, r.outs, r.isolated, '0.9')); + const l = isolateDust(outs(...o), input, { legacy: true }); + check(`...and the legacy split for the same payment, 0.8.0`, !validate(input, l.outs, l.isolated, '0.8'), `[${amts(l.outs)}] + [${amts(l.isolated)}]`); +} + +// the two shapes the old split sent that 0.9.0 refuses, as our payments make +// them (destination first, change last) +{ + const r = isolateDust(outs(100, 300), 400); + check('100 sent from 400 (all dust): both isolated, none on the checkpoint', !r.outs.length && amts(r.isolated).join() === '100,300'); + check('...which the old split left on the checkpoint, refused by 0.9.0', validate(400, outs(100, 300), [], '0.9') === 'Dust'); + const r2 = isolateDust(outs(100, 400), 500); + check('100 sent from 500 (change 400 can\'t pad it): the change isolated whole', !r2.outs.length && amts(r2.isolated).join() === '400,100'); + check('...which the old split also left mixed, refused by 0.9.0', validate(500, outs(100, 400), [], '0.9') === 'Dust'); +} +// the split keeps each output's policy: the pad piece is paid to the output it came from +{ + const o = outs(800, 100, 100); + const r = isolateDust(o, 1000); + check('the pad piece keeps the policy of the output it came from', r.isolated[0].policy === o[0].policy && r.outs[0].policy === o[0].policy); +} + +// random payments: each rule's split passes its own server's checks +let n = 0; +for (let i = 0; i < 200000; i++) { + const input = 1 + Math.floor(Math.random() ** 3 * 5000); + const k = 1 + Math.floor(Math.random() * 3); + const cuts = Array.from({ length: k - 1 }, () => 1 + Math.floor(Math.random() * (input - 1))).sort((a, b) => a - b); + const parts = [...cuts, input].map((c, j, a) => c - (j ? a[j - 1] : 0)).filter((x) => x > 0); + if (parts.reduce((a, b) => a + b, 0) !== input) continue; + for (const [v, legacy] of [['0.9', false], ['0.8', true]]) { + const r = isolateDust(outs(...parts), input, { legacy }); + const err = validate(input, r.outs, r.isolated, v); + assert(!err, `${parts.join('+')} from ${input} → [${amts(r.outs)}] + [${amts(r.isolated)}] refused by ${v}: ${err}`); + } + n++; +} +check(`${n.toLocaleString('en-US')} random payments (1–3 outputs, 1–5,000 sat) pass 0.9.0's checks with the new split and 0.8.0's with the legacy one`, n > 100000); +// which rule a wallet uses: the server's own info decides +{ + const m = Object.create(ArkManager.prototype); + m.info = { maxVtxoExitDepth: 100 }; + const before = m._legacyDust(); + m.info = { maxVtxoExitDepth: 100, maxRoundParticipationOutputs: 4 }; + check('a server without ArkInfo field 23 (before 0.9.0) gets the legacy split; one with it, the new', before === true && m._legacyDust() === false); +} +console.log('\n✅ dust isolation matches server-0.9.0, and 0.8.0 keeps its own'); |
