// 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');