aboutsummaryrefslogtreecommitdiffstats
path: root/src/ark
diff options
context:
space:
mode:
Diffstat (limited to 'src/ark')
-rw-r--r--src/ark/migrate.js25
1 files changed, 15 insertions, 10 deletions
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);