From bf5e99403e7528e18a90075f1d7027a0c8de0e34 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 18 Aug 2020 11:52:55 +0930 Subject: [PATCH] utxopsbt: make default to only allow unreserved utxos. This more closely mirrors fundpsbt (which will only select unreserved ones) but you can turn it off if you want to e.g. rbf in future. Signed-off-by: Rusty Russell Changelog-Added: JSON-RPC: New low-level command `utxopsbt` to create PSBT from existing utxos. --- doc/lightning-utxopsbt.7 | 6 +++++- doc/lightning-utxopsbt.7.md | 5 ++++- wallet/reservation.c | 13 ++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/doc/lightning-utxopsbt.7 b/doc/lightning-utxopsbt.7 index d732c9f05..6523fa027 100644 --- a/doc/lightning-utxopsbt.7 +++ b/doc/lightning-utxopsbt.7 @@ -3,7 +3,7 @@ lightning-utxopsbt - Command to populate PSBT inputs from given UTXOs .SH SYNOPSIS -\fButxopsbt\fR \fIsatoshi\fR \fIfeerate\fR \fIstartweight\fR \fIutxos\fR [\fIreserve\fR] +\fButxopsbt\fR \fIsatoshi\fR \fIfeerate\fR \fIstartweight\fR \fIutxos\fR [\fIreserve\fR] [\fIreservedok\fR] .SH DESCRIPTION @@ -23,6 +23,10 @@ the resulting transaction plus \fIstartweight\fR at the given \fIfeerate\fR, with at least \fIsatoshi\fR left over (unless \fIsatoshi\fR is \fBall\fR, which is equivalent to setting it to zero)\. + +Unless \fIreservedok\fR is set to true (default is false) it will also fail +if any of the \fIutxos\fR are already reserved\. + .SH RETURN VALUE On success, returns the \fIpsbt\fR containing the inputs, \fIfeerate_per_kw\fR diff --git a/doc/lightning-utxopsbt.7.md b/doc/lightning-utxopsbt.7.md index 808bc3cb7..b1279ccd7 100644 --- a/doc/lightning-utxopsbt.7.md +++ b/doc/lightning-utxopsbt.7.md @@ -4,7 +4,7 @@ lightning-utxopsbt -- Command to populate PSBT inputs from given UTXOs SYNOPSIS -------- -**utxopsbt** *satoshi* *feerate* *startweight* *utxos* \[*reserve*\] +**utxopsbt** *satoshi* *feerate* *startweight* *utxos* \[*reserve*\] \[*reservedok*\] DESCRIPTION ----------- @@ -23,6 +23,9 @@ the resulting transaction plus *startweight* at the given *feerate*, with at least *satoshi* left over (unless *satoshi* is **all**, which is equivalent to setting it to zero). +Unless *reservedok* is set to true (default is false) it will also fail +if any of the *utxos* are already reserved. + RETURN VALUE ------------ diff --git a/wallet/reservation.c b/wallet/reservation.c index 7db5950d0..1ea8aa194 100644 --- a/wallet/reservation.c +++ b/wallet/reservation.c @@ -420,8 +420,9 @@ static struct command_result *json_utxopsbt(struct command *cmd, { struct utxo **utxos; u32 *feerate_per_kw, *weight; - bool all, *reserve; + bool all, *reserve, *reserved_ok; struct amount_sat *amount, input, excess; + u32 current_height; if (!param(cmd, buffer, params, p_req("satoshi", param_sat_or_all, &amount), @@ -429,15 +430,25 @@ static struct command_result *json_utxopsbt(struct command *cmd, p_req("startweight", param_number, &weight), p_req("utxos", param_txout, &utxos), p_opt_def("reserve", param_bool, &reserve, true), + p_opt_def("reservedok", param_bool, &reserved_ok, false), NULL)) return command_param_failed(); all = amount_sat_eq(*amount, AMOUNT_SAT(-1ULL)); input = AMOUNT_SAT(0); + current_height = get_block_height(cmd->ld->topology); for (size_t i = 0; i < tal_count(utxos); i++) { const struct utxo *utxo = utxos[i]; + if (!*reserved_ok && is_reserved(utxo, current_height)) + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "UTXO %s:%u already reserved", + type_to_string(tmpctx, + struct bitcoin_txid, + &utxo->txid), + utxo->outnum); + /* It supplies more input. */ if (!amount_sat_add(&input, input, utxo->amount)) return command_fail(cmd, LIGHTNINGD,