aboutsummaryrefslogtreecommitdiffstats
path: root/tools/ark/rescue-claimed-test.js
blob: 7d3c68b3293b0178cb65f748b719d2f1b4bf406e (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
// A finished renewal adopted a second time — its done action pruned, then the
// round's mailbox breadcrumb read again — must not sit "Renewal in progress"
// forever. The issued step retires it without a row (its first claim has one),
// and neither the breadcrumb nor rescueParticipation adopts a claimed round.
// Run: bun tools/ark/rescue-claimed-test.js
import assert from 'node:assert/strict';
import { mock } from 'bun:test';

const refresh = await import('../../src/ark/refresh.js');
let claims = 0, statusAsks = 0;
mock.module('../../src/ark/refresh.js', () => ({
  ...refresh,
  requestForfeitNonces: async () => { claims++; throw new Error('network'); },
  roundParticipationStatus: async () => { statusAsks++; throw new Error('network'); },
}));
const { ArkManager } = await import('../../src/ark/manager.js');
const mgr = new ArkManager({ storage: { save() {} }, arkUrl: 'http://test.invalid' });
mgr._refreshOutputs = () => [];
mgr._key = () => ({});
mgr._decoded = () => ({ point: { raw: new Uint8Array(36) } });
// version 2, one input, one 1000-sat OP_TRUE output
const rawTx = '02000000' + '01' + '11'.repeat(32) + '00000000' + '00' + 'ffffffff' + '01' + 'e803000000000000' + '0151' + '00000000';
const U = 'a6'.repeat(32);
const fresh = () => ({
  id: 'refresh-rescue-1', type: 'refresh', step: 'issued', inputIds: ['a:0', 'b:0'], outKeyIndex: 3,
  outAmountSat: 900, feeSat: 2, unlockHash: U, fundingTxHex: rawTx, outputVtxos: [],
});
// input b:0 has lost its bytes to pruning, as on Adam's wallet
const coins = () => [{ id: 'a:0', amountSat: 500, state: 'spent', bytes: 'AA==' }, { id: 'b:0', amountSat: 402, state: 'spent' }];

{
  const action = fresh();
  mgr.state = { nextKeyIndex: 4, actions: [action], vtxos: coins(),
    movements: [{ id: 'm1', type: 'refresh', status: 'complete', amountSat: 900, unlockHash: U, inputIds: ['a:0', 'b:0'] }] };
  await mgr._driveRefresh(action);
  assert.equal(action.step, 'failed');
  assert.equal(action.superseded, true, 'retired without a history row');
  assert.equal(claims, 0, 'never asks the server to claim again');
  assert.equal(mgr.state.movements.length, 1, 'no second "Renewed" row');
  console.log('✓ A second adoption of a finished renewal retires on the next sync');
}

{
  await assert.rejects(mgr.rescueParticipation(U), /already claimed/);
  assert.equal(statusAsks, 0, 'refused before asking the server');
  console.log('✓ rescueParticipation refuses a round the history already shows as renewed');
}

{
  const action = fresh();
  mgr.state = { nextKeyIndex: 4, actions: [action], vtxos: coins(), movements: [] };
  await assert.rejects(mgr._driveRefresh(action), /network/);
  assert.equal(claims, 1, 'an unclaimed round still goes on to claim');
  assert.equal(action.step, 'issued');
  console.log('✓ A round with no claim on record still drives its claim');
}
console.log('\n✅ claimed-round rescue passes');