plugins/renepay: use struct short_channel_id_dir.

No changes, just use this convenience type for handing
around, and arrays.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-08-21 15:07:07 +09:30
parent a6c2cd77a1
commit 7287ade705
6 changed files with 88 additions and 104 deletions

View file

@ -120,39 +120,38 @@ static void chan_extra_can_send_(
chan_extra_adjust_half(ce,!dir);
}
void chan_extra_can_send(
struct chan_extra_map *chan_extra_map,
struct short_channel_id scid,
int dir,
const struct short_channel_id_dir *scidd,
struct amount_msat x)
{
struct chan_extra *ce = chan_extra_map_get(chan_extra_map,
scid);
scidd->scid);
if(!ce)
{
debug_err("%s unexpected chan_extra ce is NULL",
__PRETTY_FUNCTION__);
}
if(!amount_msat_add(&x,x,ce->half[dir].htlc_total))
if(!amount_msat_add(&x,x,ce->half[scidd->dir].htlc_total))
{
debug_err("%s (line %d) cannot add x=%s and htlc_total=%s",
__PRETTY_FUNCTION__,__LINE__,
type_to_string(tmpctx,struct amount_msat,&x),
type_to_string(tmpctx,struct amount_msat,&ce->half[dir].htlc_total));
type_to_string(tmpctx,struct amount_msat,&ce->half[scidd->dir].htlc_total));
}
chan_extra_can_send_(ce,dir,x);
chan_extra_can_send_(ce,scidd->dir,x);
}
/* Update the knowledge that this (channel,direction) cannot send x msat.*/
void chan_extra_cannot_send(
struct payment *p,
struct chan_extra_map *chan_extra_map,
struct short_channel_id scid,
int dir,
const struct short_channel_id_dir *scidd,
struct amount_msat x)
{
struct chan_extra *ce = chan_extra_map_get(chan_extra_map,
scid);
scidd->scid);
if(!ce)
{
debug_err("%s (line %d) unexpected chan_extra ce is NULL",
@ -161,12 +160,12 @@ void chan_extra_cannot_send(
/* If a channel cannot send x it means that the upper bound for the
* liquidity is MAX_L < x + htlc_total */
if(!amount_msat_add(&x,x,ce->half[dir].htlc_total))
if(!amount_msat_add(&x,x,ce->half[scidd->dir].htlc_total))
{
debug_err("%s (line %d) cannot add x=%s and htlc_total=%s",
__PRETTY_FUNCTION__,__LINE__,
type_to_string(tmpctx,struct amount_msat,&x),
type_to_string(tmpctx,struct amount_msat,&ce->half[dir].htlc_total));
type_to_string(tmpctx,struct amount_msat,&ce->half[scidd->dir].htlc_total));
}
if(!amount_msat_sub(&x,x,AMOUNT_MSAT(1)))
@ -179,23 +178,21 @@ void chan_extra_cannot_send(
}
/* If we "knew" the capacity was at least this, we just showed we're wrong! */
if (amount_msat_less_eq(x, ce->half[dir].known_min)) {
debug_paynote(p, "Expected scid=%s, dir=%d min %s, but %s failed! Setting min to 0",
type_to_string(tmpctx,struct short_channel_id,&scid),
dir,
type_to_string(tmpctx,struct amount_msat,&ce->half[dir].known_min),
if (amount_msat_less_eq(x, ce->half[scidd->dir].known_min)) {
debug_paynote(p, "Expected scid=%s min %s, but %s failed! Setting min to 0",
type_to_string(tmpctx,struct short_channel_id_dir,scidd),
type_to_string(tmpctx,struct amount_msat,&ce->half[scidd->dir].known_min),
type_to_string(tmpctx,struct amount_msat,&x));
ce->half[dir].known_min = AMOUNT_MSAT(0);
ce->half[scidd->dir].known_min = AMOUNT_MSAT(0);
}
ce->half[dir].known_max = amount_msat_min(ce->half[dir].known_max,x);
ce->half[scidd->dir].known_max = amount_msat_min(ce->half[scidd->dir].known_max,x);
debug_paynote(p,"Update chan knowledge scid=%s, dir=%d: [%s,%s]",
type_to_string(tmpctx,struct short_channel_id,&scid),
dir,
type_to_string(tmpctx,struct amount_msat,&ce->half[dir].known_min),
type_to_string(tmpctx,struct amount_msat,&ce->half[dir].known_max));
debug_paynote(p,"Update chan knowledge scid=%s: [%s,%s]",
type_to_string(tmpctx,struct short_channel_id_dir,scidd),
type_to_string(tmpctx,struct amount_msat,&ce->half[scidd->dir].known_min),
type_to_string(tmpctx,struct amount_msat,&ce->half[scidd->dir].known_max));
chan_extra_adjust_half(ce,!dir);
chan_extra_adjust_half(ce,!scidd->dir);
}
/* Update the knowledge that this (channel,direction) has liquidity x.*/
static void chan_extra_set_liquidity_(
@ -220,18 +217,17 @@ static void chan_extra_set_liquidity_(
}
void chan_extra_set_liquidity(
struct chan_extra_map *chan_extra_map,
struct short_channel_id scid,
int dir,
const struct short_channel_id_dir *scidd,
struct amount_msat x)
{
struct chan_extra *ce = chan_extra_map_get(chan_extra_map,
scid);
scidd->scid);
if(!ce)
{
debug_err("%s unexpected chan_extra ce is NULL",
__PRETTY_FUNCTION__);
}
chan_extra_set_liquidity_(ce,dir,x);
chan_extra_set_liquidity_(ce,scidd->dir,x);
}
/* Update the knowledge that this (channel,direction) has sent x msat.*/
static void chan_extra_sent_success_(
@ -263,18 +259,17 @@ static void chan_extra_sent_success_(
}
void chan_extra_sent_success(
struct chan_extra_map *chan_extra_map,
struct short_channel_id scid,
int dir,
const struct short_channel_id_dir *scidd,
struct amount_msat x)
{
struct chan_extra *ce = chan_extra_map_get(chan_extra_map,
scid);
scidd->scid);
if(!ce)
{
debug_err("%s unexpected chan_extra ce is NULL",
__PRETTY_FUNCTION__);
}
chan_extra_sent_success_(ce,dir,x);
chan_extra_sent_success_(ce,scidd->dir,x);
}
/* Forget a bit about this (channel,direction) state. */
static void chan_extra_relax_(
@ -298,19 +293,18 @@ static void chan_extra_relax_(
}
void chan_extra_relax(
struct chan_extra_map *chan_extra_map,
struct short_channel_id scid,
int dir,
const struct short_channel_id_dir *scidd,
struct amount_msat x,
struct amount_msat y)
{
struct chan_extra *ce = chan_extra_map_get(chan_extra_map,
scid);
scidd->scid);
if(!ce)
{
debug_err("%s unexpected chan_extra ce is NULL",
__PRETTY_FUNCTION__);
}
chan_extra_relax_(ce,dir,x,y);
chan_extra_relax_(ce,scidd->dir,x,y);
}
/* Forget the channel information by a fraction of the capacity. */
@ -334,15 +328,14 @@ void chan_extra_relax_fraction(
/* Returns either NULL, or an entry from the hash */
struct chan_extra_half *
get_chan_extra_half_by_scid(struct chan_extra_map *chan_extra_map,
const struct short_channel_id scid,
int dir)
const struct short_channel_id_dir *scidd)
{
struct chan_extra *ce;
ce = chan_extra_map_get(chan_extra_map, scid);
ce = chan_extra_map_get(chan_extra_map, scidd->scid);
if (!ce)
return NULL;
return &ce->half[dir];
return &ce->half[scidd->dir];
}
/* Helper if we have a gossmap_chan */
struct chan_extra_half *
@ -351,9 +344,11 @@ get_chan_extra_half_by_chan(const struct gossmap *gossmap,
const struct gossmap_chan *chan,
int dir)
{
return get_chan_extra_half_by_scid(chan_extra_map,
gossmap_chan_scid(gossmap, chan),
dir);
struct short_channel_id_dir scidd;
scidd.scid = gossmap_chan_scid(gossmap, chan);
scidd.dir = dir;
return get_chan_extra_half_by_scid(chan_extra_map, &scidd);
}
@ -371,9 +366,12 @@ get_chan_extra_half_by_chan_verify(
int dir)
{
const struct short_channel_id scid = gossmap_chan_scid(gossmap,chan);
struct short_channel_id_dir scidd;
scidd.scid = gossmap_chan_scid(gossmap,chan);
scidd.dir = dir;
struct chan_extra_half *h = get_chan_extra_half_by_scid(
chan_extra_map,scid,dir);
chan_extra_map,&scidd);
if (!h) {
struct amount_sat cap;
struct amount_msat cap_msat;
@ -386,7 +384,7 @@ get_chan_extra_half_by_chan_verify(
__PRETTY_FUNCTION__,
__LINE__);
}
h = & new_chan_extra(chan_extra_map,scid,cap_msat)->half[dir];
h = & new_chan_extra(chan_extra_map,scidd.scid,cap_msat)->half[scidd.dir];
}
return h;

View file

@ -148,33 +148,28 @@ static inline struct amount_msat amount_msat_max(
/* Update the knowledge that this (channel,direction) can send x msat.*/
void chan_extra_can_send(struct chan_extra_map *chan_extra_map,
struct short_channel_id scid,
int dir,
const struct short_channel_id_dir *scidd,
struct amount_msat x);
/* Update the knowledge that this (channel,direction) cannot send x msat.*/
void chan_extra_cannot_send(struct payment* p,
struct chan_extra_map *chan_extra_map,
struct short_channel_id scid,
int dir,
const struct short_channel_id_dir *scidd,
struct amount_msat x);
/* Update the knowledge that this (channel,direction) has liquidity x.*/
void chan_extra_set_liquidity(struct chan_extra_map *chan_extra_map,
struct short_channel_id scid,
int dir,
const struct short_channel_id_dir *scidd,
struct amount_msat x);
/* Update the knowledge that this (channel,direction) has sent x msat.*/
void chan_extra_sent_success(struct chan_extra_map *chan_extra_map,
struct short_channel_id scid,
int dir,
const struct short_channel_id_dir *scidd,
struct amount_msat x);
/* Forget a bit about this (channel,direction) state. */
void chan_extra_relax(struct chan_extra_map *chan_extra_map,
struct short_channel_id scid,
int dir,
const struct short_channel_id_dir *scidd,
struct amount_msat down,
struct amount_msat up);
@ -186,8 +181,7 @@ void chan_extra_relax_fraction(
/* Returns either NULL, or an entry from the hash */
struct chan_extra_half *get_chan_extra_half_by_scid(struct chan_extra_map *chan_extra_map,
const struct short_channel_id scid,
int dir);
const struct short_channel_id_dir *scidd);
/* If the channel is not registered, then a new entry is created. scid must be
* present in the gossmap. */
struct chan_extra_half *

View file

@ -144,10 +144,10 @@ static struct pf_result *handle_unhandleable_error(struct pay_flow *pf,
/* Assume it's not the destination */
n = pseudorand(n-1);
tal_arr_expand(&pf->payment->disabled, pf->path_scids[n]);
tal_arr_expand(&pf->payment->disabled, pf->path_scidds[n].scid);
debug_paynote(pf->payment, "... eliminated %s",
type_to_string(tmpctx, struct short_channel_id,
&pf->path_scids[n]));
&pf->path_scidds[n].scid));
return pay_flow_failed(pf);
}
@ -317,7 +317,7 @@ static struct command_result *flow_sendpay_failed(struct command *cmd,
/* There is no new knowledge from this kind of failure.
* We just disable this scid. */
tal_arr_expand(&payment->disabled, pf->path_scids[0]);
tal_arr_expand(&payment->disabled, pf->path_scidds[0].scid);
pay_flow_failed(pf);
return command_still_pending(cmd);
@ -350,11 +350,11 @@ static void sendpay_new_flows(struct payment *p)
json_add_node_id(req->js, "id",
&pf->path_nodes[j]);
json_add_short_channel_id(req->js, "channel",
&pf->path_scids[j]);
&pf->path_scidds[j].scid);
json_add_amount_msat(req->js, "amount_msat",
pf->amounts[j]);
json_add_num(req->js, "direction",
pf->path_dirs[j]);
pf->path_scidds[j].dir);
json_add_u32(req->js, "delay",
pf->cltv_delays[j]);
json_add_string(req->js,"style","tlv");
@ -1068,7 +1068,7 @@ static struct pf_result *handle_sendpay_failure_payment(struct pay_flow *pf STEA
debug_assert(p);
/* Final node is usually a hard failure */
if (erridx == tal_count(pf->path_scids)) {
if (erridx == tal_count(pf->path_scidds)) {
debug_paynote(p,
"onion error %s from final node #%u: %s",
onion_wire_name(onionerr),
@ -1083,7 +1083,7 @@ static struct pf_result *handle_sendpay_failure_payment(struct pay_flow *pf STEA
return pay_flow_failed_final(pf, PAY_DESTINATION_PERM_FAIL, message);
}
errscid = pf->path_scids[erridx];
errscid = pf->path_scidds[erridx].scid;
debug_paynote(p,
"onion error %s from node #%u %s: %s",
onion_wire_name(onionerr),
@ -1170,9 +1170,9 @@ static void handle_sendpay_failure_flow(struct pay_flow *pf,
"%s",
onion_wire_name(onionerr),
erridx,
erridx == tal_count(pf->path_scids)
erridx == tal_count(pf->path_scidds)
? "final"
: type_to_string(tmpctx, struct short_channel_id, &pf->path_scids[erridx]),
: type_to_string(tmpctx, struct short_channel_id_dir, &pf->path_scidds[erridx]),
msg);
/* we know that all channels before erridx where able to commit to this payment */
@ -1183,14 +1183,13 @@ static void handle_sendpay_failure_flow(struct pay_flow *pf,
/* Insufficient funds (not from final, that's weird!) */
if((enum onion_wire)onionerr == WIRE_TEMPORARY_CHANNEL_FAILURE
&& erridx < tal_count(pf->path_scids))
&& erridx < tal_count(pf->path_scidds))
{
plugin_log(pay_plugin->plugin,LOG_DBG,
"sendpay_failure says insufficient funds!");
chan_extra_cannot_send(p,pay_plugin->chan_extra_map,
pf->path_scids[erridx],
pf->path_dirs[erridx],
&pf->path_scidds[erridx],
/* This channel can't send all that was
* commited in HTLCs.
* Had we removed the commited amount then
@ -1300,10 +1299,10 @@ static struct pf_result *sendpay_failure(struct pay_flow *pf,
return handle_unhandleable_error(pf, err);
/* Answer must be sane: but note, erridx can be final node! */
if (erridx > tal_count(pf->path_scids)) {
if (erridx > tal_count(pf->path_scidds)) {
plugin_err(pay_plugin->plugin,
"Erring channel %u/%zu in path %s",
erridx, tal_count(pf->path_scids),
erridx, tal_count(pf->path_scidds),
flow_path_to_str(tmpctx, pf));
}

View file

@ -38,11 +38,10 @@ static void remove_htlc_payflow(
struct chan_extra_map *chan_extra_map,
struct pay_flow *pf)
{
for (size_t i = 0; i < tal_count(pf->path_scids); i++) {
for (size_t i = 0; i < tal_count(pf->path_scidds); i++) {
struct chan_extra_half *h = get_chan_extra_half_by_scid(
chan_extra_map,
pf->path_scids[i],
pf->path_dirs[i]);
&pf->path_scidds[i]);
if(!h)
{
plugin_err(pay_plugin->plugin,
@ -74,11 +73,10 @@ static void commit_htlc_payflow(
struct chan_extra_map *chan_extra_map,
const struct pay_flow *pf)
{
for (size_t i = 0; i < tal_count(pf->path_scids); i++) {
for (size_t i = 0; i < tal_count(pf->path_scidds); i++) {
struct chan_extra_half *h = get_chan_extra_half_by_scid(
chan_extra_map,
pf->path_scids[i],
pf->path_dirs[i]);
&pf->path_scidds[i]);
if(!h)
{
plugin_err(pay_plugin->plugin,
@ -288,14 +286,15 @@ static void convert_and_attach_flows(struct payment *payment,
pf->key.payment_hash = payment->payment_hash;
/* Convert gossmap_chan into scids and nodes */
pf->path_scids = tal_arr(pf, struct short_channel_id, plen);
pf->path_scidds = tal_arr(pf, struct short_channel_id_dir, plen);
pf->path_nodes = tal_arr(pf, struct node_id, plen);
for (size_t j = 0; j < plen; j++) {
struct gossmap_node *n;
n = gossmap_nth_node(gossmap, f->path[j], !f->dirs[j]);
gossmap_node_get_id(gossmap, n, &pf->path_nodes[j]);
pf->path_scids[j]
pf->path_scidds[j].scid
= gossmap_chan_scid(gossmap, f->path[j]);
pf->path_scidds[j].dir = f->dirs[j];
}
/* Calculate cumulative delays (backwards) */
@ -306,7 +305,6 @@ static void convert_and_attach_flows(struct payment *payment,
+ f->path[j]->half[f->dirs[j]].delay;
}
pf->amounts = tal_steal(pf, f->amounts);
pf->path_dirs = tal_steal(pf, f->dirs);
pf->success_prob = f->success_prob;
/* Payment keeps a list of its flows. */
@ -539,10 +537,10 @@ const char *add_payflows(const tal_t *ctx,
const char *flow_path_to_str(const tal_t *ctx, const struct pay_flow *flow)
{
char *s = tal_strdup(ctx, "");
for (size_t i = 0; i < tal_count(flow->path_scids); i++) {
for (size_t i = 0; i < tal_count(flow->path_scidds); i++) {
tal_append_fmt(&s, "-%s->",
type_to_string(tmpctx, struct short_channel_id,
&flow->path_scids[i]));
&flow->path_scidds[i].scid));
}
return s;
}

View file

@ -50,9 +50,8 @@ struct pay_flow {
} key;
/* The series of channels and nodes to traverse. */
struct short_channel_id *path_scids;
struct short_channel_id_dir *path_scidds;
struct node_id *path_nodes;
int *path_dirs;
/* CLTV delays for each hop */
u32 *cltv_delays;
/* The amounts at each step */

View file

@ -188,12 +188,11 @@ void uncertainty_network_flow_success(
struct chan_extra_map *chan_extra_map,
struct pay_flow *pf)
{
for (size_t i = 0; i < tal_count(pf->path_scids); i++)
for (size_t i = 0; i < tal_count(pf->path_scidds); i++)
{
chan_extra_sent_success(
chan_extra_map,
pf->path_scids[i],
pf->path_dirs[i],
&pf->path_scidds[i],
pf->amounts[i]);
}
}
@ -207,9 +206,7 @@ void uncertainty_network_channel_can_send(
for (size_t i = 0; i < erridx; i++)
{
chan_extra_can_send(chan_extra_map,
pf->path_scids[i],
pf->path_dirs[i],
&pf->path_scidds[i],
/* This channel can send all that was
* commited in HTLCs.
* Had we removed the commited amount then
@ -238,14 +235,14 @@ bool uncertainty_network_update_from_listpeerchannels(
goto malformed;
json_for_each_arr(i, channel, channels) {
struct short_channel_id scid;
struct short_channel_id_dir scidd;
const jsmntok_t *scidtok = json_get_member(buf, channel, "short_channel_id");
/* If channel is still opening, this won't be there.
* Also it won't be in the gossmap, so there is
* no need to mark it as disabled. */
if (!scidtok)
continue;
if (!json_to_short_channel_id(buf, scidtok, &scid))
if (!json_to_short_channel_id(buf, scidtok, &scidd.scid))
goto malformed;
bool connected;
@ -259,15 +256,14 @@ bool uncertainty_network_update_from_listpeerchannels(
" peer disconnected",
type_to_string(tmpctx,
struct short_channel_id,
&scid));
tal_arr_expand(&p->disabled, scid);
&scidd.scid));
tal_arr_expand(&p->disabled, scidd.scid);
continue;
}
const jsmntok_t *spendabletok, *dirtok,*statetok, *totaltok,
*peeridtok;
struct amount_msat spendable,capacity;
int dir;
const struct node_id src=my_id;
struct node_id dst;
@ -285,7 +281,7 @@ bool uncertainty_network_update_from_listpeerchannels(
goto malformed;
if (!json_to_msat(buf, totaltok, &capacity))
goto malformed;
if (!json_to_int(buf, dirtok,&dir))
if (!json_to_int(buf, dirtok, &scidd.dir))
goto malformed;
if(!json_to_node_id(buf,peeridtok,&dst))
goto malformed;
@ -293,24 +289,24 @@ bool uncertainty_network_update_from_listpeerchannels(
/* Don't report opening/closing channels */
if (!json_tok_streq(buf, statetok, "CHANNELD_NORMAL")
&& !json_tok_streq(buf, statetok, "CHANNELD_AWAITING_SPLICE")) {
tal_arr_expand(&p->disabled, scid);
tal_arr_expand(&p->disabled, scidd.scid);
continue;
}
struct chan_extra *ce = chan_extra_map_get(chan_extra_map,
scid);
scidd.scid);
if(!ce)
{
/* this channel is not public, but it belongs to us */
ce = new_chan_extra(chan_extra_map,
scid,
scidd.scid,
capacity);
/* FIXME: features? */
gossmap_local_addchan(p->local_gossmods,
&src, &dst, &scid, NULL);
&src, &dst, &scidd.scid, NULL);
gossmap_local_updatechan(p->local_gossmods,
&scid,
&scidd.scid,
/* TODO(eduardo): does it
* matter to consider HTLC
@ -323,14 +319,14 @@ bool uncertainty_network_update_from_listpeerchannels(
* matter to set this delay? */
/*delay=*/0,
true,
dir);
scidd.dir);
}
// TODO(eduardo): this includes pending HTLC of previous
// payments!
/* We know min and max liquidity exactly now! */
chan_extra_set_liquidity(chan_extra_map,
scid,dir,spendable);
&scidd,spendable);
}
return true;