channel leases: pass expected lease rates around in compat form

We need to know what the lease we're expecting is. To do this
we pass around the hex encoded portion of the wire format.

We can use this passed in expected lease rates to confirm that the peer
is, in fact, using the same rates as what we have currently.

Changelog-Added: JSON-RPC: fundchannel, multifundchannel, and openchannel_init now accept a 'compact_lease' for any requested funds
This commit is contained in:
niftynei
2021-07-02 16:19:47 -05:00
committed by neil saitug
parent 8819278a88
commit f24bbac8d9
19 changed files with 156 additions and 30 deletions

View File

@@ -761,8 +761,11 @@ static void json_add_policy(struct json_stream *stream,
json_add_num(stream, "fuzz_percent", policy->fuzz_factor);
json_add_num(stream, "fund_probability", policy->fund_probability);
if (policy->rates)
if (policy->rates) {
json_add_lease_rates(stream, policy->rates);
json_add_string(stream, "compact_lease",
lease_rates_tohex(tmpctx, policy->rates));
}
}
static struct command_result *

View File

@@ -43,6 +43,7 @@ json_fundchannel(struct command *cmd,
const jsmntok_t *push_msat;
const jsmntok_t *close_to;
const jsmntok_t *request_amt;
const jsmntok_t *compact_lease;
struct out_req *req;
@@ -56,9 +57,15 @@ json_fundchannel(struct command *cmd,
p_opt("push_msat", param_tok, &push_msat),
p_opt("close_to", param_tok, &close_to),
p_opt("request_amt", param_tok, &request_amt),
p_opt("compact_lease", param_tok, &compact_lease),
NULL))
return command_param_failed();
if (request_amt && !compact_lease)
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Must pass in 'compact_lease' if requesting"
" funds from peer");
req = jsonrpc_request_start(cmd->plugin, cmd, "multifundchannel",
&fundchannel_get_result, &forward_error,
NULL);
@@ -73,8 +80,10 @@ json_fundchannel(struct command *cmd,
json_add_tok(req->js, "push_msat", push_msat, buf);
if (close_to)
json_add_tok(req->js, "close_to", close_to, buf);
if (request_amt)
if (request_amt) {
json_add_tok(req->js, "request_amt", request_amt, buf);
json_add_tok(req->js, "compact_lease", compact_lease, buf);
}
json_object_end(req->js);
json_array_end(req->js);
if (feerate)

View File

@@ -7,6 +7,7 @@
#include <ccan/array_size/array_size.h>
#include <ccan/compiler/compiler.h>
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/json_out/json_out.h>
#include <ccan/str/str.h>
#include <ccan/take/take.h>
#include <common/addr.h>
@@ -16,6 +17,7 @@
#include <common/json_stream.h>
#include <common/json_tok.h>
#include <common/jsonrpc_errors.h>
#include <common/lease_rates.h>
#include <common/node_id.h>
#include <common/psbt_open.h>
#include <common/pseudorand.h>
@@ -1816,6 +1818,7 @@ param_destinations_array(struct command *cmd, const char *name,
struct amount_sat *amount, *request_amt;
bool *announce;
struct amount_msat *push_msat;
struct lease_rates *rates;
dest = &(*dests)[i];
@@ -1832,6 +1835,7 @@ param_destinations_array(struct command *cmd, const char *name,
&dest->close_to_str),
p_opt_def("request_amt", param_sat, &request_amt,
AMOUNT_SAT(0)),
p_opt("compact_lease", param_lease_hex, &rates),
NULL))
return command_param_failed();
@@ -1854,6 +1858,12 @@ param_destinations_array(struct command *cmd, const char *name,
json_dest,
"output would be dust");
if (!amount_sat_zero(*request_amt) && !rates)
return command_fail_badparam(cmd, name, buffer,
json_dest,
"Must pass in 'compact_"
"lease' if requesting"
" funds from peer");
dest->index = i;
dest->addrhint = addrhint;
dest->their_features = NULL;
@@ -1867,6 +1877,7 @@ param_destinations_array(struct command *cmd, const char *name,
dest->updated_psbt = NULL;
dest->protocol = FUND_CHANNEL;
dest->request_amt = *request_amt;
dest->rates = tal_steal(*dests, rates);
/* Only one destination can have "all" indicator. */
if (dest->all) {

View File

@@ -146,6 +146,9 @@ struct multifundchannel_destination {
/* amount to request peer to lease (OPEN_CHANNEL) */
struct amount_sat request_amt;
/* Channel lease rates that we expect the peer to respond with */
struct lease_rates *rates;
};

View File

@@ -1026,9 +1026,12 @@ openchannel_init_dest(struct multifundchannel_destination *dest)
&dest->push_msat));
/* Request some sats from the peer! */
if (amount_sat_greater(dest->request_amt, AMOUNT_SAT(0)))
if (!amount_sat_zero(dest->request_amt)) {
json_add_string(req->js, "request_amt",
fmt_amount_sat(tmpctx, dest->request_amt));
json_add_string(req->js, "compact_lease",
lease_rates_tohex(tmpctx, dest->rates));
}
return send_outreq(cmd->plugin, req);
}

View File

@@ -561,6 +561,11 @@ static void json_add_node(struct json_stream *js,
if (na_tlvs->option_will_fund) {
json_object_start(js, "option_will_fund");
json_add_lease_rates(js, na_tlvs->option_will_fund);
/* As a convenience, add a hexstring version
* of this info */
json_add_string(js, "compact_lease",
lease_rates_tohex(tmpctx,
na_tlvs->option_will_fund));
json_object_end(js);
}
}