From a9427f1a8d64a2d718ec48621729991dfdbfab6d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 8 Jul 2020 06:20:21 +0930 Subject: [PATCH] bitcoin/feerate: new exposure for feerate parsing outside lightningd. This exposes the numeric part of param_feerate() as param_feerate_val(). Signed-off-by: Rusty Russell --- bitcoin/Makefile | 1 + bitcoin/feerate.c | 40 +++++++++++++++ bitcoin/feerate.h | 10 ++++ bitcoin/test/run-bitcoin_block_from_hex.c | 3 -- bitcoin/test/run-tx-encode.c | 3 -- cli/test/run-large-input.c | 3 -- cli/test/run-remove-hint.c | 3 -- common/json_tok.c | 49 ++++++++++++++++++ common/json_tok.h | 5 ++ common/test/run-bigsize.c | 3 -- common/test/run-cryptomsg.c | 3 -- common/test/run-derive_basepoints.c | 3 -- common/test/run-features.c | 3 -- common/test/run-gossip_rcvd_filter.c | 3 -- common/test/run-ip_port_parsing.c | 3 -- common/test/run-json_remove.c | 3 -- common/test/run-key_derive.c | 3 -- common/test/run-lock.c | 3 -- common/test/run-softref.c | 3 -- common/test/run-sphinx.c | 3 -- connectd/test/run-initiator-success.c | 3 -- connectd/test/run-responder-success.c | 3 -- lightningd/chaintopology.c | 28 +--------- lightningd/chaintopology.h | 4 -- lightningd/json.c | 62 +++-------------------- lightningd/json.h | 6 +-- lightningd/test/run-jsonrpc.c | 9 ++-- plugins/Makefile | 1 + wallet/test/run-wallet.c | 2 +- 29 files changed, 121 insertions(+), 147 deletions(-) create mode 100644 bitcoin/feerate.c diff --git a/bitcoin/Makefile b/bitcoin/Makefile index a0536df0b..7d7767dd8 100644 --- a/bitcoin/Makefile +++ b/bitcoin/Makefile @@ -4,6 +4,7 @@ BITCOIN_SRC := \ bitcoin/base58.c \ bitcoin/block.c \ bitcoin/chainparams.c \ + bitcoin/feerate.c \ bitcoin/locktime.c \ bitcoin/preimage.c \ bitcoin/privkey.c \ diff --git a/bitcoin/feerate.c b/bitcoin/feerate.c new file mode 100644 index 000000000..696ec4b39 --- /dev/null +++ b/bitcoin/feerate.c @@ -0,0 +1,40 @@ +#include +#include +#include + +u32 feerate_from_style(u32 feerate, enum feerate_style style) +{ + switch (style) { + case FEERATE_PER_KSIPA: + return feerate; + case FEERATE_PER_KBYTE: + /* Everyone uses satoshi per kbyte, but we use satoshi per ksipa + * (don't round down to zero though)! */ + return (feerate + 3) / 4; + } + abort(); +} + +u32 feerate_to_style(u32 feerate_perkw, enum feerate_style style) +{ + switch (style) { + case FEERATE_PER_KSIPA: + return feerate_perkw; + case FEERATE_PER_KBYTE: + if ((u64)feerate_perkw * 4 > UINT_MAX) + return UINT_MAX; + return feerate_perkw * 4; + } + abort(); +} + +const char *feerate_style_name(enum feerate_style style) +{ + switch (style) { + case FEERATE_PER_KBYTE: + return "perkb"; + case FEERATE_PER_KSIPA: + return "perkw"; + } + abort(); +} diff --git a/bitcoin/feerate.h b/bitcoin/feerate.h index a0379a432..92e963108 100644 --- a/bitcoin/feerate.h +++ b/bitcoin/feerate.h @@ -33,6 +33,11 @@ */ #define FEERATE_FLOOR 253 +enum feerate_style { + FEERATE_PER_KSIPA, + FEERATE_PER_KBYTE +}; + static inline u32 feerate_floor(void) { /* Assert that bitcoind will see this as above minRelayTxFee */ @@ -47,4 +52,9 @@ static inline u32 feerate_floor(void) return FEERATE_FLOOR; } + +u32 feerate_from_style(u32 feerate, enum feerate_style style); +u32 feerate_to_style(u32 feerate_perkw, enum feerate_style style); +const char *feerate_style_name(enum feerate_style style); + #endif /* LIGHTNING_BITCOIN_FEERATE_H */ diff --git a/bitcoin/test/run-bitcoin_block_from_hex.c b/bitcoin/test/run-bitcoin_block_from_hex.c index 6bbcc1274..4d3057c66 100644 --- a/bitcoin/test/run-bitcoin_block_from_hex.c +++ b/bitcoin/test/run-bitcoin_block_from_hex.c @@ -21,9 +21,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/bitcoin/test/run-tx-encode.c b/bitcoin/test/run-tx-encode.c index 03de237ef..ae26cb04b 100644 --- a/bitcoin/test/run-tx-encode.c +++ b/bitcoin/test/run-tx-encode.c @@ -22,9 +22,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/cli/test/run-large-input.c b/cli/test/run-large-input.c index 2d4d01cf8..cee090de4 100644 --- a/cli/test/run-large-input.c +++ b/cli/test/run-large-input.c @@ -47,9 +47,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/cli/test/run-remove-hint.c b/cli/test/run-remove-hint.c index 0207dc5f2..f621fea31 100644 --- a/cli/test/run-remove-hint.c +++ b/cli/test/run-remove-hint.c @@ -50,9 +50,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/common/json_tok.c b/common/json_tok.c index b7d8c9bb8..8c7952157 100644 --- a/common/json_tok.c +++ b/common/json_tok.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -330,3 +331,51 @@ struct command_result *param_secrets_array(struct command *cmd, } return NULL; } + +struct command_result *param_feerate_val(struct command *cmd, + const char *name, const char *buffer, + const jsmntok_t *tok, + u32 **feerate_per_kw) +{ + jsmntok_t base = *tok, suffix = *tok; + enum feerate_style style; + unsigned int num; + + /* We have to split the number and suffix. */ + suffix.start = suffix.end; + while (suffix.start > base.start && !isdigit(buffer[suffix.start-1])) { + suffix.start--; + base.end--; + } + + if (!json_to_number(buffer, &base, &num)) { + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' prefix should be an integer, not '%.*s'", + name, base.end - base.start, + buffer + base.start); + } + + if (json_tok_streq(buffer, &suffix, "") + || json_tok_streq(buffer, &suffix, + feerate_style_name(FEERATE_PER_KBYTE))) { + style = FEERATE_PER_KBYTE; + } else if (json_tok_streq(buffer, &suffix, + feerate_style_name(FEERATE_PER_KSIPA))) { + style = FEERATE_PER_KSIPA; + } else { + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' suffix should be '%s' or '%s', not '%.*s'", + name, + feerate_style_name(FEERATE_PER_KSIPA), + feerate_style_name(FEERATE_PER_KBYTE), + suffix.end - suffix.start, + buffer + suffix.start); + } + + *feerate_per_kw = tal(cmd, u32); + **feerate_per_kw = feerate_from_style(num, style); + if (**feerate_per_kw < FEERATE_FLOOR) + **feerate_per_kw = FEERATE_FLOOR; + return NULL; +} + diff --git a/common/json_tok.h b/common/json_tok.h index 3a428bc2e..1a13b1c2d 100644 --- a/common/json_tok.h +++ b/common/json_tok.h @@ -131,4 +131,9 @@ struct command_result *param_secrets_array(struct command *cmd, const jsmntok_t *tok, struct secret **secrets); +struct command_result *param_feerate_val(struct command *cmd, + const char *name, const char *buffer, + const jsmntok_t *tok, + u32 **feerate_per_kw); + #endif /* LIGHTNING_COMMON_JSON_TOK_H */ diff --git a/common/test/run-bigsize.c b/common/test/run-bigsize.c index 33b3640d2..f9e44f8b3 100644 --- a/common/test/run-bigsize.c +++ b/common/test/run-bigsize.c @@ -27,9 +27,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/common/test/run-cryptomsg.c b/common/test/run-cryptomsg.c index 9278076e5..dced33197 100644 --- a/common/test/run-cryptomsg.c +++ b/common/test/run-cryptomsg.c @@ -22,9 +22,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/common/test/run-derive_basepoints.c b/common/test/run-derive_basepoints.c index 733c28414..572bdc5c7 100644 --- a/common/test/run-derive_basepoints.c +++ b/common/test/run-derive_basepoints.c @@ -23,9 +23,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/common/test/run-features.c b/common/test/run-features.c index ef0f3f28c..5e7ea09cb 100644 --- a/common/test/run-features.c +++ b/common/test/run-features.c @@ -22,9 +22,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/common/test/run-gossip_rcvd_filter.c b/common/test/run-gossip_rcvd_filter.c index 531ea29a0..7afc47b1d 100644 --- a/common/test/run-gossip_rcvd_filter.c +++ b/common/test/run-gossip_rcvd_filter.c @@ -19,9 +19,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/common/test/run-ip_port_parsing.c b/common/test/run-ip_port_parsing.c index 5439ddca1..0a7df6bc6 100644 --- a/common/test/run-ip_port_parsing.c +++ b/common/test/run-ip_port_parsing.c @@ -21,9 +21,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/common/test/run-json_remove.c b/common/test/run-json_remove.c index 4c318216a..572274783 100644 --- a/common/test/run-json_remove.c +++ b/common/test/run-json_remove.c @@ -19,9 +19,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/common/test/run-key_derive.c b/common/test/run-key_derive.c index 76a7e3068..f48e58cbc 100644 --- a/common/test/run-key_derive.c +++ b/common/test/run-key_derive.c @@ -24,9 +24,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/common/test/run-lock.c b/common/test/run-lock.c index d29f323e3..6b3255296 100644 --- a/common/test/run-lock.c +++ b/common/test/run-lock.c @@ -23,9 +23,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/common/test/run-softref.c b/common/test/run-softref.c index d9f7c381e..731485a67 100644 --- a/common/test/run-softref.c +++ b/common/test/run-softref.c @@ -20,9 +20,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/common/test/run-sphinx.c b/common/test/run-sphinx.c index a31e55a31..bf4ea5001 100644 --- a/common/test/run-sphinx.c +++ b/common/test/run-sphinx.c @@ -36,9 +36,6 @@ void amount_msat_from_u64(struct amount_msat *msat UNNEEDED, u64 millisatoshis U /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/connectd/test/run-initiator-success.c b/connectd/test/run-initiator-success.c index 953a65b19..4f2d27c77 100644 --- a/connectd/test/run-initiator-success.c +++ b/connectd/test/run-initiator-success.c @@ -26,9 +26,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/connectd/test/run-responder-success.c b/connectd/test/run-responder-success.c index 29fdab58a..f93d6ecc8 100644 --- a/connectd/test/run-responder-success.c +++ b/connectd/test/run-responder-success.c @@ -26,9 +26,6 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) /* Generated stub for amount_sat_eq */ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_eq called!\n"); abort(); } -/* Generated stub for amount_sat_less */ -bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_less called!\n"); abort(); } /* Generated stub for amount_sat_sub */ bool amount_sat_sub(struct amount_sat *val UNNEEDED, struct amount_sat a UNNEEDED, diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index fa32bc410..4cc46486e 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -466,32 +466,6 @@ u32 penalty_feerate(struct chain_topology *topo) return try_get_feerate(topo, FEERATE_PENALTY); } -u32 feerate_from_style(u32 feerate, enum feerate_style style) -{ - switch (style) { - case FEERATE_PER_KSIPA: - return feerate; - case FEERATE_PER_KBYTE: - /* Everyone uses satoshi per kbyte, but we use satoshi per ksipa - * (don't round down to zero though)! */ - return (feerate + 3) / 4; - } - abort(); -} - -u32 feerate_to_style(u32 feerate_perkw, enum feerate_style style) -{ - switch (style) { - case FEERATE_PER_KSIPA: - return feerate_perkw; - case FEERATE_PER_KBYTE: - if ((u64)feerate_perkw * 4 > UINT_MAX) - return UINT_MAX; - return feerate_perkw * 4; - } - abort(); -} - static struct command_result *json_feerates(struct command *cmd, const char *buffer, const jsmntok_t *obj UNNEEDED, @@ -516,7 +490,7 @@ static struct command_result *json_feerates(struct command *cmd, } response = json_stream_success(cmd); - json_object_start(response, json_feerate_style_name(*style)); + json_object_start(response, feerate_style_name(*style)); for (size_t i = 0; i < ARRAY_SIZE(feerates); i++) { if (!feerates[i] || i == FEERATE_MIN || i == FEERATE_MAX) continue; diff --git a/lightningd/chaintopology.h b/lightningd/chaintopology.h index 456296a09..4f948b15f 100644 --- a/lightningd/chaintopology.h +++ b/lightningd/chaintopology.h @@ -159,10 +159,6 @@ u32 delayed_to_us_feerate(struct chain_topology *topo); u32 htlc_resolution_feerate(struct chain_topology *topo); u32 penalty_feerate(struct chain_topology *topo); -/* We always use feerate-per-ksipa, ie. perkw */ -u32 feerate_from_style(u32 feerate, enum feerate_style style); -u32 feerate_to_style(u32 feerate_perkw, enum feerate_style style); - const char *feerate_name(enum feerate feerate); /* Set feerate_per_kw to this estimate & return NULL, or fail cmd */ diff --git a/lightningd/json.c b/lightningd/json.c index d074c0294..2f49cde3c 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -69,17 +68,6 @@ struct command_result *param_short_channel_id(struct command *cmd, json_tok_full(buffer, tok)); } -const char *json_feerate_style_name(enum feerate_style style) -{ - switch (style) { - case FEERATE_PER_KBYTE: - return "perkb"; - case FEERATE_PER_KSIPA: - return "perkw"; - } - abort(); -} - struct command_result *param_feerate_style(struct command *cmd, const char *name, const char *buffer, @@ -88,11 +76,11 @@ struct command_result *param_feerate_style(struct command *cmd, { *style = tal(cmd, enum feerate_style); if (json_tok_streq(buffer, tok, - json_feerate_style_name(FEERATE_PER_KSIPA))) { + feerate_style_name(FEERATE_PER_KSIPA))) { **style = FEERATE_PER_KSIPA; return NULL; } else if (json_tok_streq(buffer, tok, - json_feerate_style_name(FEERATE_PER_KBYTE))) { + feerate_style_name(FEERATE_PER_KBYTE))) { **style = FEERATE_PER_KBYTE; return NULL; } @@ -100,8 +88,8 @@ struct command_result *param_feerate_style(struct command *cmd, return command_fail(cmd, JSONRPC2_INVALID_PARAMS, "'%s' should be '%s' or '%s', not '%.*s'", name, - json_feerate_style_name(FEERATE_PER_KSIPA), - json_feerate_style_name(FEERATE_PER_KBYTE), + feerate_style_name(FEERATE_PER_KSIPA), + feerate_style_name(FEERATE_PER_KBYTE), json_tok_full_len(tok), json_tok_full(buffer, tok)); } @@ -109,10 +97,6 @@ struct command_result *param_feerate(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, u32 **feerate) { - jsmntok_t base = *tok, suffix = *tok; - enum feerate_style style; - unsigned int num; - for (size_t i = 0; i < NUM_FEERATES; i++) { if (json_tok_streq(buffer, tok, feerate_name(i))) return param_feerate_estimate(cmd, feerate, i); @@ -127,42 +111,8 @@ struct command_result *param_feerate(struct command *cmd, const char *name, else if (json_tok_streq(buffer, tok, "urgent")) return param_feerate_estimate(cmd, feerate, FEERATE_UNILATERAL_CLOSE); - /* We have to split the number and suffix. */ - suffix.start = suffix.end; - while (suffix.start > base.start && !isdigit(buffer[suffix.start-1])) { - suffix.start--; - base.end--; - } - - if (!json_to_number(buffer, &base, &num)) { - return command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' prefix should be an integer, not '%.*s'", - name, base.end - base.start, - buffer + base.start); - } - - if (json_tok_streq(buffer, &suffix, "") - || json_tok_streq(buffer, &suffix, - json_feerate_style_name(FEERATE_PER_KBYTE))) { - style = FEERATE_PER_KBYTE; - } else if (json_tok_streq(buffer, &suffix, - json_feerate_style_name(FEERATE_PER_KSIPA))) { - style = FEERATE_PER_KSIPA; - } else { - return command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' suffix should be '%s' or '%s', not '%.*s'", - name, - json_feerate_style_name(FEERATE_PER_KSIPA), - json_feerate_style_name(FEERATE_PER_KBYTE), - suffix.end - suffix.start, - buffer + suffix.start); - } - - *feerate = tal(cmd, u32); - **feerate = feerate_from_style(num, style); - if (**feerate < FEERATE_FLOOR) - **feerate = FEERATE_FLOOR; - return NULL; + /* It's a number... */ + return param_feerate_val(cmd, name, buffer, tok, feerate); } bool diff --git a/lightningd/json.h b/lightningd/json.h index 5055c3c06..33189975f 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -5,6 +5,7 @@ #ifndef LIGHTNING_LIGHTNINGD_JSON_H #define LIGHTNING_LIGHTNINGD_JSON_H #include "config.h" +#include #include #include #include @@ -38,11 +39,6 @@ struct command_result *param_short_channel_id(struct command *cmd, const jsmntok_t *tok, struct short_channel_id **scid); -enum feerate_style { - FEERATE_PER_KSIPA, - FEERATE_PER_KBYTE -}; - /* Extract a feerate style. */ struct command_result *param_feerate_style(struct command *cmd, const char *name, diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 243f606de..4fca70a54 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -12,9 +12,6 @@ void db_commit_transaction(struct db *db UNNEEDED) /* Generated stub for fatal */ void fatal(const char *fmt UNNEEDED, ...) { fprintf(stderr, "fatal called!\n"); abort(); } -/* Generated stub for feerate_from_style */ -u32 feerate_from_style(u32 feerate UNNEEDED, enum feerate_style style UNNEEDED) -{ fprintf(stderr, "feerate_from_style called!\n"); abort(); } /* Generated stub for feerate_name */ const char *feerate_name(enum feerate feerate UNNEEDED) { fprintf(stderr, "feerate_name called!\n"); abort(); } @@ -80,6 +77,12 @@ struct command_result *param_feerate_estimate(struct command *cmd UNNEEDED, u32 **feerate_per_kw UNNEEDED, enum feerate feerate UNNEEDED) { fprintf(stderr, "param_feerate_estimate called!\n"); abort(); } +/* Generated stub for param_feerate_val */ +struct command_result *param_feerate_val(struct command *cmd UNNEEDED, + const char *name UNNEEDED, const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + u32 **feerate_per_kw UNNEEDED) +{ fprintf(stderr, "param_feerate_val called!\n"); abort(); } /* Generated stub for param_ignore */ struct command_result *param_ignore(struct command *cmd UNNEEDED, const char *name UNNEEDED, const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, diff --git a/plugins/Makefile b/plugins/Makefile index fc9cfa032..971f589e9 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -37,6 +37,7 @@ PLUGINS := \ PLUGIN_COMMON_OBJS := \ bitcoin/base58.o \ + bitcoin/feerate.o \ bitcoin/privkey.o \ bitcoin/psbt.o \ bitcoin/pubkey.o \ diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 469de169e..c3ab72806 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -702,7 +702,7 @@ u8 *towire_incorrect_cltv_expiry(const tal_t *ctx UNNEEDED, u32 cltv_expiry UNNE u8 *towire_incorrect_or_unknown_payment_details(const tal_t *ctx UNNEEDED, struct amount_msat htlc_msat UNNEEDED, u32 height UNNEEDED) { fprintf(stderr, "towire_incorrect_or_unknown_payment_details called!\n"); abort(); } /* Generated stub for towire_invalid_onion_payload */ -u8 *towire_invalid_onion_payload(const tal_t *ctx UNNEEDED, varint type UNNEEDED, u16 offset UNNEEDED) +u8 *towire_invalid_onion_payload(const tal_t *ctx UNNEEDED, bigsize type UNNEEDED, u16 offset UNNEEDED) { fprintf(stderr, "towire_invalid_onion_payload called!\n"); abort(); } /* Generated stub for towire_invalid_realm */ u8 *towire_invalid_realm(const tal_t *ctx UNNEEDED)