From 43ae30df215e83a48638aa7714ec6da5a1b39731 Mon Sep 17 00:00:00 2001 From: niftynei Date: Wed, 23 Jun 2021 14:31:47 -0500 Subject: [PATCH] chaintopology: new command 'parsefeerate' Useful for parsing a passed in feerate before calling lightningd with it, e.g. when you need to know what the feerate is for a fundpsbt before calling fundpsbt Changelog-Added: JSON-RPC: new command `parsefeerate` which takes a feerate string and returns the calculated perkw/perkb --- doc/Makefile | 1 + doc/index.rst | 1 + doc/lightning-feerates.7 | 11 ++----- doc/lightning-feerates.7.md | 4 +-- doc/lightning-parsefeerate.7 | 44 +++++++++++++++++++++++++++ doc/lightning-parsefeerate.7.md | 45 ++++++++++++++++++++++++++++ doc/schemas/parsefeerate.schema.json | 13 ++++++++ lightningd/chaintopology.c | 27 +++++++++++++++++ tests/test_misc.py | 12 ++++++++ 9 files changed, 148 insertions(+), 10 deletions(-) create mode 100644 doc/lightning-parsefeerate.7 create mode 100644 doc/lightning-parsefeerate.7.md create mode 100644 doc/schemas/parsefeerate.schema.json diff --git a/doc/Makefile b/doc/Makefile index 1af44a323..4e5b8ba26 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -54,6 +54,7 @@ MANPAGES := doc/lightning-cli.1 \ doc/lightning-openchannel_signed.7 \ doc/lightning-openchannel_update.7 \ doc/lightning-pay.7 \ + doc/lightning-parsefeerate.7 \ doc/lightning-plugin.7 \ doc/lightning-reserveinputs.7 \ doc/lightning-sendinvoice.7 \ diff --git a/doc/index.rst b/doc/index.rst index 8371e18fc..d66d886fd 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -82,6 +82,7 @@ c-lightning Documentation lightning-openchannel_init lightning-openchannel_signed lightning-openchannel_update + lightning-parsefeerate lightning-pay lightning-ping lightning-plugin diff --git a/doc/lightning-feerates.7 b/doc/lightning-feerates.7 index 7ded49c9b..beef39522 100644 --- a/doc/lightning-feerates.7 +++ b/doc/lightning-feerates.7 @@ -16,7 +16,6 @@ C-lightning will also smoothen feerate estimations from the backend\. \fIstyle\fR is either of the two strings: - .RS .IP \[bu] \fIperkw\fR - provide feerate in units of satoshis per 1000 weight\. @@ -27,7 +26,6 @@ C-lightning will also smoothen feerate estimations from the backend\. Bitcoin transactions have non-witness and witness bytes: - .RS .IP \[bu] Non-witness bytes count as 4 weight, 1 virtual byte\. @@ -58,7 +56,6 @@ which will override the recommended feerates returned by \fBfeerates\fR\. On success, an object is returned, containing: - .RS .IP \[bu] \fBperkb\fR (object, optional): If \fIstyle\fR parameter was perkb: @@ -125,7 +122,6 @@ On success, an object is returned, containing: The following warnings may also be returned: - .RS .IP \[bu] \fBwarning_missing_feerates\fR: Some fee estimates are missing @@ -143,7 +139,6 @@ Many other commands have a \fIfeerate\fR parameter, which can be the strings \fIurgent\fR, \fInormal\fR, or \fIslow\fR\. These are mapped to the \fBfeerates\fR outputs as: - .RS .IP \[bu] \fIurgent\fR - equal to \fIunilateral_close\fR @@ -167,11 +162,11 @@ manpage\. .SH SEE ALSO -\fBlightning-fundchannel\fR(7), \fBlightning-withdraw\fR(7), \fBlightning-txprepare\fR(7), -\fBlightning-fundchannel_start\fR(7)\. +\fBlightning-parsefeerate\fR(7), \fBlightning-fundchannel\fR(7), \fBlightning-withdraw\fR(7), +\fBlightning-txprepare\fR(7), \fBlightning-fundchannel_start\fR(7)\. .SH RESOURCES Main web site: \fIhttps://github.com/ElementsProject/lightning\fR -\" SHA256STAMP:227e0f9f9b4d3c2b80d58466ed4fae768a5a2e7c2234e43105e21efc4a0b1861 +\" SHA256STAMP:3c68e2e2b47bc4107d71e263372b3ec3ac77434d7319c670f293d3be88703fe7 diff --git a/doc/lightning-feerates.7.md b/doc/lightning-feerates.7.md index 50cc43c5f..0cd49794b 100644 --- a/doc/lightning-feerates.7.md +++ b/doc/lightning-feerates.7.md @@ -110,8 +110,8 @@ manpage. SEE ALSO -------- -lightning-fundchannel(7), lightning-withdraw(7), lightning-txprepare(7), -lightning-fundchannel_start(7). +lightning-parsefeerate(7), lightning-fundchannel(7), lightning-withdraw(7), +lightning-txprepare(7), lightning-fundchannel_start(7). RESOURCES --------- diff --git a/doc/lightning-parsefeerate.7 b/doc/lightning-parsefeerate.7 new file mode 100644 index 000000000..69a7c3adf --- /dev/null +++ b/doc/lightning-parsefeerate.7 @@ -0,0 +1,44 @@ +.TH "LIGHTNING-PARSEFEERATE" "7" "" "" "lightning-parsefeerate" +.SH NAME +lightning-parsefeerate - Command for parsing a feerate string to a feerate +.SH SYNOPSIS + +\fBparsefeerate\fR \fIfeerate_str\fR + +.SH DESCRIPTION + +The \fBparsefeerate\fR command returns the current feerate for any valid +\fIfeerate_str\fR\. This is useful for finding the current feerate that a +\fBfundpsbt\fR or \fButxopsbt\fR command might use\. + +.SH RETURN VALUE + +On success, an object is returned, containing: + +.RS +.IP \[bu] +\fBperkw\fR (u32, optional): Value of \fIfeerate_str\fR in kilosipa + +.RE +.SH ERRORS + +The \fBparsefeerate\fR command will error if the \fIfeerate_str\fR format is +not recognized\. + +.RS +.IP \[bu] +-32602: If the given parameters are wrong\. + +.RE +.SH TRIVIA + +In C-lightning we like to call the weight unit "sipa" +in honor of Pieter Wuille, +who uses the name "sipa" on IRC and elsewhere\. +Internally we call the \fIperkw\fR style as "feerate per kilosipa"\. + +.SH RESOURCES + +Main web site: \fIhttps://github.com/ElementsProject/lightning\fR + +\" SHA256STAMP:1395d3fe85f6eb1f09c3706c50a9660dd9ade04e71435bde3fe1f9660f7b2655 diff --git a/doc/lightning-parsefeerate.7.md b/doc/lightning-parsefeerate.7.md new file mode 100644 index 000000000..6c649f751 --- /dev/null +++ b/doc/lightning-parsefeerate.7.md @@ -0,0 +1,45 @@ +lightning-parsefeerate -- Command for parsing a feerate string to a feerate +=========================================================================== + +SYNOPSIS +-------- + +**parsefeerate** *feerate_str* + +DESCRIPTION +----------- + +The **parsefeerate** command returns the current feerate for any valid +*feerate_str*. This is useful for finding the current feerate that a +**fundpsbt** or **utxopsbt** command might use. + +RETURN VALUE +------------ + +[comment]: # (GENERATE-FROM-SCHEMA-START) +On success, an object is returned, containing: +- **perkw** (u32, optional): Value of *feerate_str* in kilosipa +[comment]: # (GENERATE-FROM-SCHEMA-END) + +ERRORS +------ + +The **parsefeerate** command will error if the *feerate_str* format is +not recognized. + +- -32602: If the given parameters are wrong. + +TRIVIA +------ + +In C-lightning we like to call the weight unit "sipa" +in honor of Pieter Wuille, +who uses the name "sipa" on IRC and elsewhere. +Internally we call the *perkw* style as "feerate per kilosipa". + +RESOURCES +--------- + +Main web site: + +[comment]: # ( SHA256STAMP:db3351466f8d2675cecf6f5909c1f3ff264b6ffa865b2d64eb02bf0b45a31a4d) diff --git a/doc/schemas/parsefeerate.schema.json b/doc/schemas/parsefeerate.schema.json new file mode 100644 index 000000000..0af834cfe --- /dev/null +++ b/doc/schemas/parsefeerate.schema.json @@ -0,0 +1,13 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "additionalProperties": false, + "required": [ ], + "properties": { + "perkw": { + "type": "u32", + "description": "Value of *feerate_str* in kilosipa", + "additionalProperties": false + } + } +} diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 949e0f0f6..6f624c839 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -553,6 +553,33 @@ static const struct json_command feerates_command = { }; AUTODATA(json_command, &feerates_command); +static struct command_result *json_parse_feerate(struct command *cmd, + const char *buffer, + const jsmntok_t *obj UNNEEDED, + const jsmntok_t *params) +{ + struct json_stream *response; + u32 *feerate; + + if (!param(cmd, buffer, params, + p_req("feerate", param_feerate, &feerate), + NULL)) + return command_param_failed(); + + response = json_stream_success(cmd); + json_add_num(response, feerate_style_name(FEERATE_PER_KSIPA), + feerate_to_style(*feerate, FEERATE_PER_KSIPA)); + return command_success(cmd, response); +} + +static const struct json_command parse_feerate_command = { + "parsefeerate", + "bitcoin", + json_parse_feerate, + "Return current feerate in perkw + perkb for given feerate string." +}; +AUTODATA(json_command, &parse_feerate_command); + static void next_updatefee_timer(struct chain_topology *topo) { /* This takes care of its own lifetime. */ diff --git a/tests/test_misc.py b/tests/test_misc.py index 1b24d5e7e..44ff94e88 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -1462,6 +1462,11 @@ def test_feerates(node_factory): types = ["opening", "mutual_close", "unilateral_close", "delayed_to_us", "htlc_resolution", "penalty"] + # Try parsing the feerates, won't work because can't estimate + for t in types: + with pytest.raises(RpcError, match=r'Cannot estimate fees'): + feerate = l1.rpc.parsefeerate(t) + # Query feerates (shouldn't give any!) wait_for(lambda: len(l1.rpc.feerates('perkw')['perkw']) == 2) feerates = l1.rpc.feerates('perkw') @@ -1539,6 +1544,13 @@ def test_feerates(node_factory): htlc_feerate = feerates["perkw"]["htlc_resolution"] htlc_timeout_cost = feerates["onchain_fee_estimates"]["htlc_timeout_satoshis"] htlc_success_cost = feerates["onchain_fee_estimates"]["htlc_success_satoshis"] + + # Try parsing the feerates, won't work because can't estimate + for t in types: + feerate = l1.rpc.parsefeerate(t) + assert feerate['perkw'] + assert 'perkb' not in feerate + if EXPERIMENTAL_FEATURES: # option_anchor_outputs assert htlc_timeout_cost == htlc_feerate * 666 // 1000