From e81d78ec4cad288f1ae42aff935e59e149fb4b9e Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 17 Nov 2020 19:45:23 -0600 Subject: [PATCH] fundpsbt/utxopsbt: new `min_witness_weight` param Needed for v2 of channel opens, where the minimum weight is 110; a 'simple utxo' (sig + key) weighs in at 107, so we a need a way to establish a floor for this case. Changelog-Added: JSON-RPC: fundpsbt/utxopsbt have new param, `min_witness_utxo`, which sets a floor for the weight calculation of an added input --- common/utxo.c | 11 +++++++++-- common/utxo.h | 2 +- doc/lightning-fundpsbt.7 | 9 +++++++-- doc/lightning-fundpsbt.7.md | 6 +++++- doc/lightning-utxopsbt.7 | 9 +++++++-- doc/lightning-utxopsbt.7.md | 6 +++++- wallet/reservation.c | 14 ++++++++++---- 7 files changed, 44 insertions(+), 13 deletions(-) diff --git a/common/utxo.c b/common/utxo.c index 25fa4dd59..dd6a09e35 100644 --- a/common/utxo.c +++ b/common/utxo.c @@ -64,7 +64,14 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max) return utxo; } -size_t utxo_spend_weight(const struct utxo *utxo) +size_t utxo_spend_weight(const struct utxo *utxo, size_t min_witness_weight) { - return bitcoin_tx_simple_input_weight(utxo->is_p2sh); + size_t wit_weight = bitcoin_tx_simple_input_witness_weight(); + /* If the min is less than what we'd use for a 'normal' tx, + * we return the value with the greater added/calculated */ + if (wit_weight < min_witness_weight) + return bitcoin_tx_input_weight(utxo->is_p2sh, + min_witness_weight); + + return bitcoin_tx_input_weight(utxo->is_p2sh, wit_weight); } diff --git a/common/utxo.h b/common/utxo.h index f779328ea..bcf07fb6f 100644 --- a/common/utxo.h +++ b/common/utxo.h @@ -74,5 +74,5 @@ void towire_utxo(u8 **pptr, const struct utxo *utxo); struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max); /* Estimate of (signed) UTXO weight in transaction */ -size_t utxo_spend_weight(const struct utxo *utxo); +size_t utxo_spend_weight(const struct utxo *utxo, size_t min_witness_weight); #endif /* LIGHTNING_COMMON_UTXO_H */ diff --git a/doc/lightning-fundpsbt.7 b/doc/lightning-fundpsbt.7 index 53ff19d1d..633d32c4b 100644 --- a/doc/lightning-fundpsbt.7 +++ b/doc/lightning-fundpsbt.7 @@ -3,7 +3,7 @@ lightning-fundpsbt - Command to populate PSBT inputs from the wallet .SH SYNOPSIS -\fBfundpsbt\fR \fIsatoshi\fR \fIfeerate\fR \fIstartweight\fR [\fIminconf\fR] [\fIreserve\fR] [\fIlocktime\fR] +\fBfundpsbt\fR \fIsatoshi\fR \fIfeerate\fR \fIstartweight\fR [\fIminconf\fR] [\fIreserve\fR] [\fIlocktime\fR] [\fImin_witness_weight\fR] .SH DESCRIPTION @@ -43,6 +43,11 @@ called (successfully, with \fIexclusive\fR true) on the returned PSBT\. \fIlocktime\fR is an optional locktime: if not set, it is set to a recent block height\. + +\fImin_witness_weight\fR is an optional minimum weight to use for a UTXO's +witness\. If the actual witness weight is greater than the provided minimum, +the actual witness weight will be used\. + .SH EXAMPLE USAGE Let's assume the caller is trying to produce a 100,000 satoshi output\. @@ -104,4 +109,4 @@ Rusty Russell \fI is mainly responsible\. Main web site: \fIhttps://github.com/ElementsProject/lightning\fR -\" SHA256STAMP:8eb6970e2d625198db9b8e5ab7f90f9fb141744a837f58ee68ef54d28c7066b0 +\" SHA256STAMP:a8b9705274638127c2f5ec4e97ed94e6d7f6b6b10a76c2248e8bc8b36dd804ff diff --git a/doc/lightning-fundpsbt.7.md b/doc/lightning-fundpsbt.7.md index e338f62b8..1b7cd3939 100644 --- a/doc/lightning-fundpsbt.7.md +++ b/doc/lightning-fundpsbt.7.md @@ -4,7 +4,7 @@ lightning-fundpsbt -- Command to populate PSBT inputs from the wallet SYNOPSIS -------- -**fundpsbt** *satoshi* *feerate* *startweight* \[*minconf*\] \[*reserve*\] \[*locktime*\] +**fundpsbt** *satoshi* *feerate* *startweight* \[*minconf*\] \[*reserve*\] \[*locktime*\] \[*min_witness_weight*\] DESCRIPTION ----------- @@ -39,6 +39,10 @@ called (successfully, with *exclusive* true) on the returned PSBT. *locktime* is an optional locktime: if not set, it is set to a recent block height. +*min_witness_weight* is an optional minimum weight to use for a UTXO's +witness. If the actual witness weight is greater than the provided minimum, +the actual witness weight will be used. + EXAMPLE USAGE ------------- diff --git a/doc/lightning-utxopsbt.7 b/doc/lightning-utxopsbt.7 index 05d100ad5..50354766b 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] [\fIreservedok\fR] [\fIlocktime\fR] +\fButxopsbt\fR \fIsatoshi\fR \fIfeerate\fR \fIstartweight\fR \fIutxos\fR [\fIreserve\fR] [\fIreservedok\fR] [\fIlocktime\fR] [\fImin_witness_weight\fR] .SH DESCRIPTION @@ -31,6 +31,11 @@ if any of the \fIutxos\fR are already reserved\. \fIlocktime\fR is an optional locktime: if not set, it is set to a recent block height\. + +\fImin_witness_weight\fR is an optional minimum weight to use for a UTXO's +witness\. If the actual witness weight is greater than the provided minimum, +the actual witness weight will be used\. + .SH RETURN VALUE On success, returns the \fIpsbt\fR containing the inputs, \fIfeerate_per_kw\fR @@ -70,4 +75,4 @@ Rusty Russell \fI is mainly responsible\. Main web site: \fIhttps://github.com/ElementsProject/lightning\fR -\" SHA256STAMP:e47331be08a0911c74e142f2255bf62a83b70b818c1cf39a9314aab8c17a6e47 +\" SHA256STAMP:777710bb963f435193e92a55344c740c123d7aa4d54bf573c99a616f59eeee54 diff --git a/doc/lightning-utxopsbt.7.md b/doc/lightning-utxopsbt.7.md index 1dfc26b85..f4706ff89 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*\] \[*reservedok*\] \[*locktime*\] +**utxopsbt** *satoshi* *feerate* *startweight* *utxos* \[*reserve*\] \[*reservedok*\] \[*locktime*\] \[*min_witness_weight*\] DESCRIPTION ----------- @@ -29,6 +29,10 @@ if any of the *utxos* are already reserved. *locktime* is an optional locktime: if not set, it is set to a recent block height. +*min_witness_weight* is an optional minimum weight to use for a UTXO's +witness. If the actual witness weight is greater than the provided minimum, +the actual witness weight will be used. + RETURN VALUE ------------ diff --git a/wallet/reservation.c b/wallet/reservation.c index 307bf6166..d934228f7 100644 --- a/wallet/reservation.c +++ b/wallet/reservation.c @@ -386,7 +386,7 @@ static struct command_result *json_fundpsbt(struct command *cmd, { struct utxo **utxos; u32 *feerate_per_kw; - u32 *minconf, *weight; + u32 *minconf, *weight, *min_witness_weight; struct amount_sat *amount, input, diff; bool all, *reserve; u32 *locktime, maxheight; @@ -398,6 +398,8 @@ static struct command_result *json_fundpsbt(struct command *cmd, p_opt_def("minconf", param_number, &minconf, 1), p_opt_def("reserve", param_bool, &reserve, true), p_opt("locktime", param_number, &locktime), + p_opt_def("min_witness_weight", param_number, + &min_witness_weight, 0), NULL)) return command_param_failed(); @@ -427,7 +429,9 @@ static struct command_result *json_fundpsbt(struct command *cmd, "impossible UTXO value"); /* But also adds weight */ - *weight += utxo_spend_weight(utxo); + fprintf(stderr, "min_witness_weight is %u\n", + *min_witness_weight); + *weight += utxo_spend_weight(utxo, *min_witness_weight); continue; } @@ -555,7 +559,7 @@ static struct command_result *json_utxopsbt(struct command *cmd, const jsmntok_t *params) { struct utxo **utxos; - u32 *feerate_per_kw, *weight; + u32 *feerate_per_kw, *weight, *min_witness_weight; bool all, *reserve, *reserved_ok; struct amount_sat *amount, input, excess; u32 current_height, *locktime; @@ -568,6 +572,8 @@ static struct command_result *json_utxopsbt(struct command *cmd, p_opt_def("reserve", param_bool, &reserve, true), p_opt_def("reservedok", param_bool, &reserved_ok, false), p_opt("locktime", param_number, &locktime), + p_opt_def("min_witness_weight", param_number, + &min_witness_weight, 0), NULL)) return command_param_failed(); @@ -592,7 +598,7 @@ static struct command_result *json_utxopsbt(struct command *cmd, "impossible UTXO value"); /* But also adds weight */ - *weight += utxo_spend_weight(utxo); + *weight += utxo_spend_weight(utxo, *min_witness_weight); } /* For all, anything above 0 is "excess" */