lease rates: helpers for parsing, serializing + equating lease_rates

We're gonna need them
This commit is contained in:
niftynei 2021-07-02 16:00:17 -05:00 committed by neil saitug
parent fd223c39ed
commit 8819278a88
9 changed files with 115 additions and 3 deletions

View File

@ -654,3 +654,19 @@ param_routehint_array(struct command *cmd, const char *name, const char *buffer,
}
return NULL;
}
struct command_result *param_lease_hex(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct lease_rates **rates)
{
if (!lease_rates_fromhex(cmd, buffer + tok->start,
tok->end - tok->start,
rates))
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Could not decode '%s' %.*s",
name, json_tok_full_len(tok),
json_tok_full(buffer, tok));
return NULL;
}

View File

@ -5,6 +5,7 @@
#include <ccan/short_types/short_types.h>
#include <common/bolt11.h>
#include <common/json.h>
#include <common/lease_rates.h>
#include <common/node_id.h>
#include <common/sphinx.h>
#include <wire/wire.h>
@ -204,4 +205,12 @@ struct command_result *
param_routehint_array(struct command *cmd, const char *name, const char *buffer,
const jsmntok_t *tok, struct route_info ***ris);
/**
* Parse a 'compact-lease' (serialized lease_rates) back into lease_rates
*/
struct command_result *param_lease_hex(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct lease_rates **rates);
#endif /* LIGHTNING_COMMON_JSON_TOK_H */

View File

@ -2,10 +2,29 @@
#include <bitcoin/pubkey.h>
#include <ccan/ccan/crypto/sha256/sha256.h>
#include <ccan/ccan/mem/mem.h>
#include <ccan/ccan/tal/str/str.h>
#include <common/amount.h>
#include <common/lease_rates.h>
#include <common/type_to_string.h>
#include <wire/peer_wire.h>
/* FIXME: Is there a better way to do this ? */
bool lease_rates_eq(struct lease_rates *l1,
struct lease_rates *l2)
{
if (!l1 != !l2)
return false;
if (!l1)
return true;
return l1->funding_weight == l2->funding_weight
&& l1->channel_fee_max_base_msat == l2->channel_fee_max_base_msat
&& l1->channel_fee_max_proportional_thousandths == l2->channel_fee_max_proportional_thousandths
&& l1->lease_fee_base_sat == l2->lease_fee_base_sat
&& l1->lease_fee_basis == l2->lease_fee_basis;
}
bool lease_rates_empty(struct lease_rates *rates)
{
if (!rates)
@ -93,3 +112,45 @@ bool lease_rates_set_lease_fee_sat(struct lease_rates *rates,
rates->lease_fee_base_sat = amt.satoshis; /* Raw: conversion */
return rates->lease_fee_base_sat == amt.satoshis; /* Raw: comparsion */
}
char *lease_rates_tohex(const tal_t *ctx, const struct lease_rates *rates)
{
char *hex;
u8 *data = tal_arr(NULL, u8, 0);
towire_lease_rates(&data, rates);
hex = tal_hex(ctx, data);
tal_free(data);
return hex;
}
bool lease_rates_fromhex(const tal_t *ctx,
const char *hexdata, size_t hexlen,
struct lease_rates **rates)
{
const u8 *data = tal_hexdata(ctx, hexdata, hexlen);
size_t len = tal_bytelen(data);
*rates = tal(ctx, struct lease_rates);
fromwire_lease_rates(&data, &len, *rates);
if (data == NULL) {
tal_free(*rates);
return false;
}
return true;
}
char *lease_rates_fmt(const tal_t *ctx, const struct lease_rates *rates)
{
return tal_fmt(ctx, "{channel_fee_max_base_msat=%u,"
"channel_fee_max_ppt=%u,"
"funding_weight=%u,"
"lease_fee_base_sat=%u,"
"lease_fee_basis=%u}",
rates->channel_fee_max_base_msat,
rates->channel_fee_max_proportional_thousandths,
rates->funding_weight,
rates->lease_fee_base_sat,
rates->lease_fee_basis);
}

View File

@ -19,6 +19,9 @@ void lease_rates_get_commitment(struct pubkey *pubkey,
u16 chan_fee_ppt,
struct sha256 *sha);
bool lease_rates_eq(struct lease_rates *l1,
struct lease_rates *l2);
bool lease_rates_calc_fee(struct lease_rates *rates,
struct amount_sat accept_funding_sats,
struct amount_sat requested_sats,
@ -28,4 +31,15 @@ bool lease_rates_calc_fee(struct lease_rates *rates,
WARN_UNUSED_RESULT bool lease_rates_set_chan_fee_base_msat(struct lease_rates *rates, struct amount_msat amt);
WARN_UNUSED_RESULT bool lease_rates_set_lease_fee_sat(struct lease_rates *rates, struct amount_sat amt);
/* Convert 'lease_rates' into a hexstring */
char *lease_rates_tohex(const tal_t *ctx, const struct lease_rates *rates);
/* Convert 'lease_rates' from a hexstring */
bool lease_rates_fromhex(const tal_t *ctx,
const char *hexdata, size_t len,
struct lease_rates **rates);
/* Format a string describing the passed in lease_rates */
char *lease_rates_fmt(const tal_t *ctx, const struct lease_rates *rates);
#endif /* LIGHTNING_COMMON_LEASE_RATES_H */

View File

@ -25,12 +25,17 @@ common/test/run-json: \
common/amount.o \
common/base32.o \
common/bigsize.o \
common/channel_id.o \
common/json.o \
common/json_stream.o \
common/lease_rates.o \
common/node_id.o \
common/pseudorand.o \
common/wireaddr.o \
common/type_to_string.o \
wire/fromwire.o \
wire/onion$(EXP)_wiregen.o \
wire/peer$(EXP)_wiregen.o \
wire/towire.o
common/test/run-route common/test/run-route-specific: \

View File

@ -15,9 +15,6 @@ bool fromwire_tlv(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED,
void *record UNNEEDED, struct tlv_field **fields UNNEEDED)
{ fprintf(stderr, "fromwire_tlv called!\n"); abort(); }
/* Generated stub for node_id_from_hexstr */
bool node_id_from_hexstr(const char *str UNNEEDED, size_t slen UNNEEDED, struct node_id *id UNNEEDED)
{ fprintf(stderr, "node_id_from_hexstr called!\n"); abort(); }
/* Generated stub for tlv_fields_valid */
bool tlv_fields_valid(const struct tlv_field *fields UNNEEDED, u64 *allow_extra UNNEEDED,
size_t *err_index UNNEEDED)

View File

@ -18,6 +18,9 @@ bool fromwire_bool(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for fromwire_fail */
void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_fail called!\n"); abort(); }
/* Generated stub for fromwire_lease_rates */
void fromwire_lease_rates(const u8 **cursor UNNEEDED, size_t *plen UNNEEDED, struct lease_rates *lease_rates UNNEEDED)
{ fprintf(stderr, "fromwire_lease_rates called!\n"); abort(); }
/* Generated stub for fromwire_secp256k1_ecdsa_signature */
void fromwire_secp256k1_ecdsa_signature(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
secp256k1_ecdsa_signature *signature UNNEEDED)
@ -50,6 +53,9 @@ void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
/* Generated stub for towire_bool */
void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED)
{ fprintf(stderr, "towire_bool called!\n"); abort(); }
/* Generated stub for towire_lease_rates */
void towire_lease_rates(u8 **p UNNEEDED, const struct lease_rates *lease_rates UNNEEDED)
{ fprintf(stderr, "towire_lease_rates called!\n"); abort(); }
/* Generated stub for towire_secp256k1_ecdsa_signature */
void towire_secp256k1_ecdsa_signature(u8 **pptr UNNEEDED,
const secp256k1_ecdsa_signature *signature UNNEEDED)

View File

@ -14,6 +14,7 @@ GOSSIPD_TEST_COMMON_OBJS := \
common/node_id.o \
common/json.o \
common/json_helpers.o \
common/lease_rates.o \
common/pseudorand.o \
common/setup.o \
common/type_to_string.o \

View File

@ -94,7 +94,9 @@ ALL_PROGRAMS += $(PLUGINS)
PLUGIN_COMMON_OBJS := \
bitcoin/base58.o \
bitcoin/block.o \
bitcoin/feerate.o \
bitcoin/preimage.o \
bitcoin/privkey.o \
bitcoin/psbt.o \
bitcoin/pubkey.o \
@ -133,6 +135,7 @@ PLUGIN_COMMON_OBJS := \
common/wireaddr.o \
wire/fromwire.o \
wire/onion$(EXP)_wiregen.o \
wire/peer$(EXP)_wiregen.o \
wire/tlvstream.o \
wire/towire.o