From f15e3b23c94fb36886e5201984d27a2509428cc9 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 4 Dec 2026 13:33:15 -0700 Subject: ark migrate: batches are the manager's own next coins, so no send goes over the input cap. The first real sweep (names' old forwarder, 126 coins, many past expiry) failed: a batch of the 48 largest coins was assembled by send() from live coins first, far more than 48. Each batch is now the set _accumulate picks (live coins first, largest first) capped at 48, and its exact total is sent. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VMan6WoqaQDYdpWkatPCar --- src/ark/migrate.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'src/ark') diff --git a/src/ark/migrate.js b/src/ark/migrate.js index 694b692..a05e60d 100644 --- a/src/ark/migrate.js +++ b/src/ark/migrate.js @@ -12,13 +12,19 @@ export const MAX_BATCH_INPUTS = 48; // manager.js MAX_SEND_INPUTS -// Spendable coins the old server will still cosign, largest first. -// (A coin at the server's exit-depth cap can't be an input; the server refuses -// to cosign it. Those stay behind and only a unilateral exit can move them.) -function movableCoins(oldMgr) { - return oldMgr.vtxos() - .filter((v) => v.state === 'spendable' && !v.expiryRejected && !oldMgr._capped(v)) - .sort((a, b) => b.amountSat - a.amountSat); +// The next batch: the coins the manager's own selection would take first, +// at most maxInputs of them. Sending exactly their total makes send() pick +// those same coins (it accumulates in the same order: coins still inside +// their timelock first, largest first), so no batch goes over the input cap. +// A coin at the server's exit-depth cap is never an input; those stay behind +// and only a unilateral exit can move them. +function nextBatch(mgr, maxInputs) { + const tip = mgr._tipH || 0; + const busy = mgr._remoteBusy(); + const spendable = mgr.state.vtxos.filter((v) => mgr._free(v, busy) && !mgr._expired(v, tip) && !mgr._capped(v)); + if (!spendable.length) return null; + const n = Math.min(maxInputs, spendable.length); + return mgr._accumulate(spendable, tip, (set) => set.length >= n); } // Ask the migrator for a deposit address that pays `dest` on the new server. @@ -44,9 +50,8 @@ export async function migrateWallet(oldMgr, { migratorUrl, dest, fetchFn = fetch const session = await startMigration(migratorUrl, dest, fetchFn); let sentSat = 0; for (let round = 0; round < 50; round++) { - const coins = movableCoins(oldMgr); - if (!coins.length) break; - const batch = coins.slice(0, maxInputs); + const batch = nextBatch(oldMgr, maxInputs); + if (!batch?.length) break; const amount = batch.reduce((t, v) => t + v.amountSat, 0); try { await oldMgr.send(session.depositAddress, amount); -- cgit v1.2.3