aboutsummaryrefslogtreecommitdiffstats
path: root/tools/ark/dust-shapes-test.js
blob: d5a9f5ff7e24343574ac09ce2a3482990205ee22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// The arkoor shapes whose dust isolation changed for server-0.9.0 (!2822),
// sent through a real server: each from a wallet holding ONE coin, so the
// split is exactly the one named.
//   400 → 100 (+300 change)  every output dust: both isolated
//   500 → 100 (+400 change)  the change can't pad the dust: isolated whole
//  1000 → 100 (+900 change)  the change pads the dust (unchanged)
//   300 → 100 (+200 change)  dust input: nothing isolated (unchanged)
// Each payment arrives, and the change is spent onward (the coin is real).
// Usage: bun tools/ark/dust-shapes-test.js   (ARK_URL to point at another captaind)
import { execSync } from 'node:child_process';
import { generateMnemonic, mnemonicToSeedSync } from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';
import { HDKey } from '@scure/bip32';
import { ArkManager } from '../../src/ark/manager.js';

const ARK = process.env.ARK_URL || 'http://127.0.0.1:3535';
const ESPLORA = 'http://127.0.0.1:30002';
const ALICE = `${process.env.HOME}/bark/target/debug/bark --datadir ${process.env.HOME}/ark-regtest/alice`;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
let ok = true;
const check = (name, cond, detail = '') => { console.log(` ${cond ? '✓' : '✗'} ${name}${detail ? ' — ' + detail : ''}`); if (!cond) ok = false; };

const newWallet = async () => {
  let raw = null;
  const account = HDKey.fromMasterSeed(mnemonicToSeedSync(generateMnemonic(wordlist))).derive("m/86'/0'/9'");
  return new ArkManager({ account, storage: { load: () => (raw ? JSON.parse(raw) : null), save: (s) => { raw = JSON.stringify(s); } }, arkUrl: ARK, esploraUrl: ESPLORA }).init();
};
const waitBalance = async (m, want, tries = 15) => {
  for (let i = 0; i < tries && m.balance().spendableSat < want; i++) { await m.sync(); await sleep(1500); }
  return m.balance().spendableSat;
};

const sink = await newWallet();
let sunk = 0;
// a bank of ours with one big coin pays each wallet: one coin each, always
// (alice's CLI wallet pays from whatever coins it holds, sometimes several)
const bank = await newWallet();
execSync(`${ALICE} -q send ${bank.address()} "20000 sat"`);
check('bank funded', (await waitBalance(bank, 20000)) >= 20000);
if (bank.state.vtxos.filter((v) => v.state === 'spendable').length > 1) { await bank.refresh(); for (let i = 0; i < 40 && bank.state.vtxos.filter((v) => v.state === 'spendable').length !== 1; i++) { execSync('docker exec bc bitcoin-cli -rpcwallet=coinos -generate 1'); await bank.sync(); await sleep(2000); } }
check('...in one coin', bank.state.vtxos.filter((v) => v.state === 'spendable').length === 1);
for (const [coin, pay, shape] of [[400, 100, 'all dust'], [500, 100, 'change isolated whole'], [1000, 100, 'change pads the dust'], [300, 100, 'dust input']]) {
  console.log(`\n[${coin} → ${pay}] ${shape}`);
  const w = await newWallet(), to = await newWallet();
  await bank.send(w.address(), coin);
  check(`funded with one ${coin}-sat coin`, (await waitBalance(w, coin)) === coin && w.state.vtxos.filter((v) => v.state === 'spendable').length === 1);
  let err = null;
  try { await w.send(to.address(), pay); } catch (e) { err = e.message; }
  check(`${pay} sent, server cosigned`, !err, err || '');
  check(`${pay} arrived`, (await waitBalance(to, pay)) === pay);
  const change = coin - pay;
  check(`${change} change spendable`, (await waitBalance(w, change)) === change);
  let err2 = null;
  try { await w.send(sink.address(), change); sunk += change; } catch (e) { err2 = e.message; }
  check(`...and spent onward`, !err2, err2 || '');
}
check('every onward spend arrived', (await waitBalance(sink, sunk)) === sunk, `${sink.balance().spendableSat}/${sunk}`);
console.log(ok ? '\n✅ every dust shape goes through ' + ARK : '\n❌ a dust shape failed on ' + ARK);
process.exit(ok ? 0 : 1);