Skip to content

Commit 3064731

Browse files
committed
txprepare: fix all amount when other amounts also specified.
This is legal! And we actually do this in tests, but we didn't check the psbt was spendable (the next patch does, indirectly, by testing feerate): ``` # Discard prep4 and get all funds again l1.rpc.txdiscard(prep5['txid']) # You can have one which is all, but not two. prep5 = l1.rpc.txprepare([{addr: Millisatoshi(amount * 3 * 1000)}, {addr: 'all'}]) # Feerate should be ~ as we asked for > assert normal_feerate_perkw - 1 < feerate_from_psbt(bitcoind, l1, prep5['psbt']) < normal_feerate_perkw + 1 E AssertionError: assert (7500 - 1) < -1091803.9574935874 ``` Changelog-Fixed: JSON-RPC: `txprepare` with `all` as well as one or more non-all amount fields now produces a valid PSBT. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 7c30deb commit 3064731

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

plugins/txprepare.c

+14
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,20 @@ static struct command_result *psbt_created(struct command *cmd,
285285

286286
/* If we have an "all" output, we now know its value ("excess_msat") */
287287
if (txp->all_output_idx != -1) {
288+
/* Subtract any other outputs they specified! */
289+
for (size_t i = 0; i < tal_count(txp->outputs); i++) {
290+
if (i == txp->all_output_idx)
291+
continue;
292+
if (!amount_sat_sub(&excess, excess, txp->outputs[i].amount)) {
293+
return command_fail(cmd, FUND_CANNOT_AFFORD,
294+
"Could not afford output %zu", i);
295+
}
296+
}
297+
if (amount_sat_less(excess, chainparams->dust_limit)) {
298+
return command_fail(cmd, FUND_CANNOT_AFFORD,
299+
"Could not afford 'all' output: only %s left",
300+
fmt_amount_sat(tmpctx, excess));
301+
}
288302
txp->outputs[txp->all_output_idx].amount = excess;
289303
}
290304

0 commit comments

Comments
 (0)