lease_rates: prepare for msats fields as raw numbers.

We would otherwise multiply them by 1000.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-06-20 19:52:10 +09:30
parent 6461815dd9
commit 5531de99de
5 changed files with 21 additions and 20 deletions

View File

@ -81,11 +81,14 @@ bool lease_rates_set_chan_fee_base_msat(struct lease_rates *rates,
amt.millisatoshis); /* Raw: conversion */
}
bool lease_rates_set_lease_fee_sat(struct lease_rates *rates,
struct amount_sat amt)
bool lease_rates_set_lease_fee_msat(struct lease_rates *rates,
struct amount_msat amt)
{
struct amount_sat sat;
if (!amount_msat_to_sat(&sat, amt))
return false;
return assign_overflow_u32(&rates->lease_fee_base_sat,
amt.satoshis); /* Raw: conversion */
sat.satoshis); /* Raw: conversion */
}
char *lease_rates_tohex(const tal_t *ctx, const struct lease_rates *rates)

View File

@ -36,7 +36,8 @@ bool lease_rates_calc_fee(const 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);
WARN_UNUSED_RESULT bool lease_rates_set_lease_fee_msat(struct lease_rates *rates,
struct amount_msat amt);
/* Convert 'lease_rates' into a hexstring */
char *lease_rates_tohex(const tal_t *ctx, const struct lease_rates *rates);

View File

@ -747,8 +747,7 @@ openchannel2_hook_deserialize(struct openchannel2_payload *payload,
payload->our_shutdown_scriptpubkey = shutdown_script;
struct amount_sat sats;
struct amount_msat msats;
struct amount_msat fee_base, fee_max_base;
payload->rates = tal(payload, struct lease_rates);
err = json_scan(payload, buffer, toks,
"{lease_fee_base_msat:%"
@ -756,10 +755,10 @@ openchannel2_hook_deserialize(struct openchannel2_payload *payload,
",channel_fee_max_base_msat:%"
",channel_fee_max_proportional_thousandths:%"
",funding_weight:%}",
JSON_SCAN(json_to_sat, &sats),
JSON_SCAN(json_to_msat, &fee_base),
JSON_SCAN(json_to_u16,
&payload->rates->lease_fee_basis),
JSON_SCAN(json_to_msat, &msats),
JSON_SCAN(json_to_msat, &fee_max_base),
JSON_SCAN(json_to_u16,
&payload->rates->channel_fee_max_proportional_thousandths),
JSON_SCAN(json_to_u16,
@ -771,11 +770,11 @@ openchannel2_hook_deserialize(struct openchannel2_payload *payload,
/* Convert to u32s */
if (payload->rates &&
!lease_rates_set_lease_fee_sat(payload->rates, sats))
fatal("Plugin sent overflowing `lease_fee_base_msat`");
!lease_rates_set_lease_fee_msat(payload->rates, fee_base))
fatal("Plugin sent overflowing/non-sat `lease_fee_base_msat`");
if (payload->rates &&
!lease_rates_set_chan_fee_base_msat(payload->rates, msats))
!lease_rates_set_chan_fee_base_msat(payload->rates, fee_max_base))
fatal("Plugin sent overflowing `channel_fee_max_base_msat`");
/* Add a serial_id to everything that doesn't have one yet */

View File

@ -363,12 +363,11 @@ static struct command_result *json_setleaserates(struct command *cmd,
{
struct json_stream *res;
struct lease_rates *rates;
struct amount_sat *lease_base_sat;
struct amount_msat *channel_fee_base_msat;
struct amount_msat *channel_fee_base_msat, *lease_base_msat;
u32 *lease_basis, *channel_fee_max_ppt, *funding_weight;
if (!param(cmd, buffer, params,
p_req("lease_fee_base_msat", param_sat, &lease_base_sat),
p_req("lease_fee_base_msat", param_msat, &lease_base_msat),
p_req("lease_fee_basis", param_number, &lease_basis),
p_req("funding_weight", param_number, &funding_weight),
p_req("channel_fee_max_base_msat", param_msat,
@ -380,7 +379,7 @@ static struct command_result *json_setleaserates(struct command *cmd,
rates = tal(tmpctx, struct lease_rates);
rates->lease_fee_basis = *lease_basis;
rates->lease_fee_base_sat = lease_base_sat->satoshis; /* Raw: conversion */
rates->lease_fee_base_sat = lease_base_msat->millisatoshis / 1000; /* Raw: conversion */
rates->channel_fee_max_base_msat = channel_fee_base_msat->millisatoshis; /* Raw: conversion */
rates->funding_weight = *funding_weight;
@ -388,7 +387,7 @@ static struct command_result *json_setleaserates(struct command *cmd,
= *channel_fee_max_ppt;
/* Gotta check that we didn't overflow */
if (lease_base_sat->satoshis > rates->lease_fee_base_sat) /* Raw: comparison */
if (lease_base_msat->millisatoshis != rates->lease_fee_base_sat * (u64)1000) /* Raw: comparison */
return command_fail_badparam(cmd, "lease_fee_base_msat",
buffer, params, "Overflow");

View File

@ -1049,16 +1049,15 @@ static void tell_lightningd_lease_rates(struct plugin *p,
struct lease_rates *rates)
{
struct json_out *jout;
struct amount_sat val;
struct amount_msat mval;
/* Tell lightningd with our lease rates*/
jout = json_out_new(NULL);
json_out_start(jout, NULL, '{');
val = amount_sat(rates->lease_fee_base_sat);
mval = amount_msat(rates->lease_fee_base_sat * 1000);
json_out_addstr(jout, "lease_fee_base_msat",
type_to_string(tmpctx, struct amount_sat, &val));
type_to_string(tmpctx, struct amount_msat, &mval));
json_out_add(jout, "lease_fee_basis", false,
"%d", rates->lease_fee_basis);
@ -1077,7 +1076,7 @@ static void tell_lightningd_lease_rates(struct plugin *p,
rpc_scan(p, "setleaserates", take(jout),
/* Unused */
"{lease_fee_base_msat:%}",
JSON_SCAN(json_to_msat_as_sats, &val));
JSON_SCAN(json_to_msat, &mval));
}