2016-06-28 23:19:21 +02:00
|
|
|
#include "routing.h"
|
2017-01-23 13:55:55 +01:00
|
|
|
#include <arpa/inet.h>
|
2017-08-22 07:25:01 +02:00
|
|
|
#include <bitcoin/block.h>
|
2020-05-16 03:29:05 +02:00
|
|
|
#include <bitcoin/chainparams.h>
|
2018-01-04 12:40:58 +01:00
|
|
|
#include <bitcoin/script.h>
|
2016-06-28 23:19:21 +02:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2017-01-23 13:55:55 +01:00
|
|
|
#include <ccan/endian/endian.h>
|
2018-05-18 13:20:28 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2017-01-23 13:55:55 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2018-01-12 15:10:21 +01:00
|
|
|
#include <common/features.h>
|
2018-11-21 23:41:49 +01:00
|
|
|
#include <common/memleak.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/pseudorand.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/status.h>
|
2019-09-27 02:04:34 +02:00
|
|
|
#include <common/timeout.h>
|
2017-08-28 18:03:01 +02:00
|
|
|
#include <common/type_to_string.h>
|
2018-03-08 05:10:26 +01:00
|
|
|
#include <common/wire_error.h>
|
2017-10-23 06:17:38 +02:00
|
|
|
#include <common/wireaddr.h>
|
2019-09-18 03:05:14 +02:00
|
|
|
#include <gossipd/gossip_generation.h>
|
2020-08-25 04:05:45 +02:00
|
|
|
#include <gossipd/gossip_store_wiregen.h>
|
2019-10-08 03:13:24 +02:00
|
|
|
#include <gossipd/gossipd.h>
|
2020-08-25 04:05:45 +02:00
|
|
|
#include <gossipd/gossipd_peerd_wiregen.h>
|
|
|
|
#include <gossipd/gossipd_wiregen.h>
|
2016-06-28 23:19:21 +02:00
|
|
|
#include <inttypes.h>
|
2020-09-07 23:06:50 +02:00
|
|
|
#include <wire/peer_wire.h>
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2017-12-18 05:15:31 +01:00
|
|
|
#ifndef SUPERVERBOSE
|
|
|
|
#define SUPERVERBOSE(...)
|
|
|
|
#endif
|
|
|
|
|
2016-09-06 09:17:48 +02:00
|
|
|
/* 365.25 * 24 * 60 / 10 */
|
|
|
|
#define BLOCKS_PER_YEAR 52596
|
|
|
|
|
2018-02-02 19:49:12 +01:00
|
|
|
struct pending_node_announce {
|
2019-04-10 09:31:29 +02:00
|
|
|
struct routing_state *rstate;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id nodeid;
|
2019-04-10 09:31:29 +02:00
|
|
|
size_t refcount;
|
2018-02-02 19:49:12 +01:00
|
|
|
u8 *node_announcement;
|
|
|
|
u32 timestamp;
|
2019-05-29 06:35:43 +02:00
|
|
|
u32 index;
|
2019-10-08 03:13:24 +02:00
|
|
|
/* Automagically turns to NULL if peer freed */
|
|
|
|
struct peer *peer_softref;
|
2018-02-02 19:49:12 +01:00
|
|
|
};
|
|
|
|
|
2019-09-16 12:44:00 +02:00
|
|
|
/* We consider a reasonable gossip rate to be 1 per day, with burst of
|
|
|
|
* 4 per day. So we use a granularity of one hour. */
|
|
|
|
#define TOKENS_PER_MSG 24
|
|
|
|
#define TOKEN_MAX (24 * 4)
|
|
|
|
|
2019-10-08 03:28:24 +02:00
|
|
|
static u8 update_tokens(const struct routing_state *rstate,
|
|
|
|
u8 tokens, u32 prev_timestamp, u32 new_timestamp)
|
2019-09-16 12:44:00 +02:00
|
|
|
{
|
2019-10-08 03:28:24 +02:00
|
|
|
u64 num_tokens = tokens;
|
2019-09-16 12:44:00 +02:00
|
|
|
|
|
|
|
assert(new_timestamp >= prev_timestamp);
|
|
|
|
|
2019-10-08 03:28:24 +02:00
|
|
|
num_tokens += ((new_timestamp - prev_timestamp)
|
|
|
|
/ GOSSIP_TOKEN_TIME(rstate->dev_fast_gossip));
|
2019-09-16 12:44:00 +02:00
|
|
|
if (num_tokens > TOKEN_MAX)
|
|
|
|
num_tokens = TOKEN_MAX;
|
2019-10-08 03:28:24 +02:00
|
|
|
return num_tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ratelimit(const struct routing_state *rstate,
|
|
|
|
u8 *tokens, u32 prev_timestamp, u32 new_timestamp)
|
|
|
|
{
|
|
|
|
*tokens = update_tokens(rstate, *tokens, prev_timestamp, new_timestamp);
|
2019-09-16 12:44:00 +02:00
|
|
|
|
|
|
|
/* Now, if we can afford it, pass this message. */
|
|
|
|
if (*tokens >= TOKENS_PER_MSG) {
|
|
|
|
*tokens -= TOKENS_PER_MSG;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
static const struct node_id *
|
2018-02-02 19:49:12 +01:00
|
|
|
pending_node_announce_keyof(const struct pending_node_announce *a)
|
|
|
|
{
|
2018-07-04 07:29:56 +02:00
|
|
|
return &a->nodeid;
|
2018-02-02 19:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool pending_node_announce_eq(const struct pending_node_announce *pna,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *pc)
|
2018-02-02 19:49:12 +01:00
|
|
|
{
|
2019-04-08 11:58:32 +02:00
|
|
|
return node_id_eq(&pna->nodeid, pc);
|
2018-02-02 19:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
HTABLE_DEFINE_TYPE(struct pending_node_announce, pending_node_announce_keyof,
|
|
|
|
node_map_hash_key, pending_node_announce_eq,
|
|
|
|
pending_node_map);
|
|
|
|
|
2019-04-11 07:15:13 +02:00
|
|
|
/* We keep around announcements for channels until we have an
|
|
|
|
* update for them (which gives us their timestamp) */
|
|
|
|
struct unupdated_channel {
|
|
|
|
/* The channel_announcement message */
|
|
|
|
const u8 *channel_announce;
|
2020-05-04 02:18:34 +02:00
|
|
|
/* The feature bitmap within it */
|
|
|
|
const u8 *features;
|
2019-04-11 07:15:13 +02:00
|
|
|
/* The short_channel_id */
|
|
|
|
struct short_channel_id scid;
|
|
|
|
/* The ids of the nodes */
|
|
|
|
struct node_id id[2];
|
|
|
|
/* When we added, so we can discard old ones */
|
|
|
|
struct timeabs added;
|
|
|
|
/* If we loaded from the store, this is where. */
|
|
|
|
u32 index;
|
|
|
|
/* Channel capacity */
|
|
|
|
struct amount_sat sat;
|
2019-10-08 03:13:24 +02:00
|
|
|
/* Automagically turns to NULL of peer freed */
|
|
|
|
struct peer *peer_softref;
|
2019-04-11 07:15:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct unupdated_channel *
|
|
|
|
get_unupdated_channel(const struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *scid)
|
|
|
|
{
|
|
|
|
return uintmap_get(&rstate->unupdated_chanmap, scid->u64);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_unupdated_channel(struct unupdated_channel *uc,
|
|
|
|
struct routing_state *rstate)
|
|
|
|
{
|
|
|
|
uintmap_del(&rstate->unupdated_chanmap, uc->scid.u64);
|
|
|
|
}
|
|
|
|
|
2019-04-14 13:54:05 +02:00
|
|
|
static struct node_map *new_node_map(const tal_t *ctx)
|
2017-09-01 06:18:55 +02:00
|
|
|
{
|
|
|
|
struct node_map *map = tal(ctx, struct node_map);
|
|
|
|
node_map_init(map);
|
|
|
|
tal_add_destructor(map, node_map_clear);
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2019-04-08 06:44:43 +02:00
|
|
|
/* We use a simple array (with NULL entries) until we have too many. */
|
2019-04-08 06:43:43 +02:00
|
|
|
static bool node_uses_chan_map(const struct node *node)
|
|
|
|
{
|
|
|
|
/* This is a layering violation: last entry in htable is the table ptr,
|
|
|
|
* which is never NULL */
|
|
|
|
return node->chans.arr[NUM_IMMEDIATE_CHANS] != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When simple array fills, use a htable. */
|
|
|
|
static void convert_node_to_chan_map(struct node *node)
|
|
|
|
{
|
|
|
|
struct chan *chans[NUM_IMMEDIATE_CHANS];
|
|
|
|
|
|
|
|
memcpy(chans, node->chans.arr, sizeof(chans));
|
|
|
|
chan_map_init_sized(&node->chans.map, NUM_IMMEDIATE_CHANS + 1);
|
|
|
|
assert(node_uses_chan_map(node));
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(chans); i++)
|
|
|
|
chan_map_add(&node->chans.map, chans[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_chan(struct node *node, struct chan *chan)
|
|
|
|
{
|
|
|
|
if (!node_uses_chan_map(node)) {
|
|
|
|
for (size_t i = 0; i < NUM_IMMEDIATE_CHANS; i++) {
|
|
|
|
if (node->chans.arr[i] == NULL) {
|
|
|
|
node->chans.arr[i] = chan;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
convert_node_to_chan_map(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
chan_map_add(&node->chans.map, chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct chan *next_chan_arr(const struct node *node,
|
|
|
|
struct chan_map_iter *i)
|
|
|
|
{
|
|
|
|
while (i->i.off < NUM_IMMEDIATE_CHANS) {
|
|
|
|
if (node->chans.arr[i->i.off])
|
|
|
|
return node->chans.arr[i->i.off];
|
|
|
|
i->i.off++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-08 06:42:43 +02:00
|
|
|
struct chan *first_chan(const struct node *node, struct chan_map_iter *i)
|
|
|
|
{
|
2019-04-08 06:43:43 +02:00
|
|
|
if (!node_uses_chan_map(node)) {
|
|
|
|
i->i.off = 0;
|
|
|
|
return next_chan_arr(node, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return chan_map_first(&node->chans.map, i);
|
2019-04-08 06:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct chan *next_chan(const struct node *node, struct chan_map_iter *i)
|
|
|
|
{
|
2019-04-08 06:43:43 +02:00
|
|
|
if (!node_uses_chan_map(node)) {
|
|
|
|
i->i.off++;
|
|
|
|
return next_chan_arr(node, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return chan_map_next(&node->chans.map, i);
|
2019-04-08 06:42:43 +02:00
|
|
|
}
|
|
|
|
|
2019-05-21 09:14:09 +02:00
|
|
|
static void destroy_routing_state(struct routing_state *rstate)
|
|
|
|
{
|
|
|
|
/* Since we omitted destructors on these, clean up manually */
|
|
|
|
u64 idx;
|
|
|
|
for (struct chan *chan = uintmap_first(&rstate->chanmap, &idx);
|
|
|
|
chan;
|
|
|
|
chan = uintmap_after(&rstate->chanmap, &idx))
|
|
|
|
free_chan(rstate, chan);
|
2019-09-16 12:43:51 +02:00
|
|
|
|
|
|
|
/* Free up our htables */
|
|
|
|
pending_cannouncement_map_clear(&rstate->pending_cannouncements);
|
|
|
|
local_chan_map_clear(&rstate->local_chan_map);
|
2019-05-21 09:14:09 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 03:04:56 +02:00
|
|
|
/* We don't check this when loading from the gossip_store: that would break
|
|
|
|
* our canned tests, and usually old gossip is better than no gossip */
|
|
|
|
static bool timestamp_reasonable(struct routing_state *rstate, u32 timestamp)
|
|
|
|
{
|
|
|
|
u64 now = gossip_time_now(rstate).ts.tv_sec;
|
|
|
|
|
|
|
|
/* More than one day ahead? */
|
|
|
|
if (timestamp > now + 24*60*60)
|
|
|
|
return false;
|
|
|
|
/* More than 2 weeks behind? */
|
2019-09-26 04:00:20 +02:00
|
|
|
if (timestamp < now - GOSSIP_PRUNE_INTERVAL(rstate->dev_fast_gossip_prune))
|
2019-09-18 03:04:56 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:42:05 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
static void memleak_help_routing_tables(struct htable *memtable,
|
|
|
|
struct routing_state *rstate)
|
|
|
|
{
|
|
|
|
struct node *n;
|
|
|
|
struct node_map_iter nit;
|
|
|
|
|
|
|
|
memleak_remove_htable(memtable, &rstate->nodes->raw);
|
|
|
|
memleak_remove_htable(memtable, &rstate->pending_node_map->raw);
|
|
|
|
memleak_remove_htable(memtable, &rstate->pending_cannouncements.raw);
|
2019-09-16 12:43:51 +02:00
|
|
|
memleak_remove_htable(memtable, &rstate->local_chan_map.raw);
|
2019-09-26 04:04:09 +02:00
|
|
|
memleak_remove_uintmap(memtable, &rstate->unupdated_chanmap);
|
2019-09-06 06:42:05 +02:00
|
|
|
|
|
|
|
for (n = node_map_first(rstate->nodes, &nit);
|
|
|
|
n;
|
|
|
|
n = node_map_next(rstate->nodes, &nit)) {
|
|
|
|
if (node_uses_chan_map(n))
|
|
|
|
memleak_remove_htable(memtable, &n->chans.map.raw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* DEVELOPER */
|
|
|
|
|
2019-09-27 02:04:34 +02:00
|
|
|
/* Once an hour, or at 10000 entries, we expire old ones */
|
|
|
|
static void txout_failure_age(struct routing_state *rstate)
|
|
|
|
{
|
|
|
|
uintmap_clear(&rstate->txout_failures_old);
|
|
|
|
rstate->txout_failures_old = rstate->txout_failures;
|
|
|
|
uintmap_init(&rstate->txout_failures);
|
|
|
|
rstate->num_txout_failures = 0;
|
|
|
|
|
|
|
|
rstate->txout_failure_timer = new_reltimer(rstate->timers,
|
|
|
|
rstate, time_from_sec(3600),
|
|
|
|
txout_failure_age, rstate);
|
|
|
|
}
|
|
|
|
|
2019-11-06 03:16:09 +01:00
|
|
|
void add_to_txout_failures(struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *scid)
|
2019-09-27 02:04:34 +02:00
|
|
|
{
|
|
|
|
if (uintmap_add(&rstate->txout_failures, scid->u64, true)
|
|
|
|
&& ++rstate->num_txout_failures == 10000) {
|
|
|
|
tal_free(rstate->txout_failure_timer);
|
|
|
|
txout_failure_age(rstate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool in_txout_failures(struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *scid)
|
|
|
|
{
|
|
|
|
if (uintmap_get(&rstate->txout_failures, scid->u64))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* If we were going to expire it, we no longer are. */
|
|
|
|
if (uintmap_get(&rstate->txout_failures_old, scid->u64)) {
|
|
|
|
add_to_txout_failures(rstate, scid);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:25:01 +02:00
|
|
|
struct routing_state *new_routing_state(const tal_t *ctx,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *local_id,
|
2019-04-11 07:16:57 +02:00
|
|
|
struct list_head *peers,
|
2019-09-27 02:04:34 +02:00
|
|
|
struct timers *timers,
|
2019-09-18 03:05:05 +02:00
|
|
|
const u32 *dev_gossip_time TAKES,
|
2019-09-26 04:00:20 +02:00
|
|
|
bool dev_fast_gossip,
|
|
|
|
bool dev_fast_gossip_prune)
|
2017-01-19 23:46:07 +01:00
|
|
|
{
|
|
|
|
struct routing_state *rstate = tal(ctx, struct routing_state);
|
2019-04-14 13:54:05 +02:00
|
|
|
rstate->nodes = new_node_map(rstate);
|
2019-09-27 02:04:34 +02:00
|
|
|
rstate->timers = timers;
|
2017-12-02 23:28:19 +01:00
|
|
|
rstate->local_id = *local_id;
|
2020-10-20 05:59:30 +02:00
|
|
|
rstate->gs = gossip_store_new(rstate, peers);
|
2018-06-04 06:15:25 +02:00
|
|
|
rstate->local_channel_announced = false;
|
2019-10-08 03:30:24 +02:00
|
|
|
rstate->last_timestamp = 0;
|
2019-04-08 17:31:59 +02:00
|
|
|
|
|
|
|
pending_cannouncement_map_init(&rstate->pending_cannouncements);
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
uintmap_init(&rstate->chanmap);
|
2019-04-11 07:15:13 +02:00
|
|
|
uintmap_init(&rstate->unupdated_chanmap);
|
2019-09-16 12:43:51 +02:00
|
|
|
local_chan_map_init(&rstate->local_chan_map);
|
2019-09-27 02:04:34 +02:00
|
|
|
rstate->num_txout_failures = 0;
|
2019-03-27 15:40:25 +01:00
|
|
|
uintmap_init(&rstate->txout_failures);
|
2019-09-27 02:04:34 +02:00
|
|
|
uintmap_init(&rstate->txout_failures_old);
|
|
|
|
txout_failure_age(rstate);
|
2018-02-02 19:49:12 +01:00
|
|
|
rstate->pending_node_map = tal(ctx, struct pending_node_map);
|
|
|
|
pending_node_map_init(rstate->pending_node_map);
|
|
|
|
|
2019-04-08 01:51:30 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
if (dev_gossip_time) {
|
|
|
|
rstate->gossip_time = tal(rstate, struct timeabs);
|
|
|
|
rstate->gossip_time->ts.tv_sec = *dev_gossip_time;
|
|
|
|
rstate->gossip_time->ts.tv_nsec = 0;
|
|
|
|
} else
|
|
|
|
rstate->gossip_time = NULL;
|
2019-09-18 03:05:05 +02:00
|
|
|
rstate->dev_fast_gossip = dev_fast_gossip;
|
2019-09-26 04:00:20 +02:00
|
|
|
rstate->dev_fast_gossip_prune = dev_fast_gossip_prune;
|
2019-04-08 01:51:30 +02:00
|
|
|
#endif
|
2019-05-21 09:14:09 +02:00
|
|
|
tal_add_destructor(rstate, destroy_routing_state);
|
2019-09-06 06:42:05 +02:00
|
|
|
memleak_add_helper(rstate, memleak_help_routing_tables);
|
2019-04-08 01:51:30 +02:00
|
|
|
|
2019-09-12 02:22:12 +02:00
|
|
|
if (taken(dev_gossip_time))
|
|
|
|
tal_free(dev_gossip_time);
|
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
return rstate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *node_map_keyof_node(const struct node *n)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
2018-07-04 07:29:56 +02:00
|
|
|
return &n->id;
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
size_t node_map_hash_key(const struct node_id *pc)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
2019-04-08 11:58:32 +02:00
|
|
|
return siphash24(siphash_seed(), pc->k, sizeof(pc->k));
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
bool node_map_node_eq(const struct node *n, const struct node_id *pc)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
2019-04-08 11:58:32 +02:00
|
|
|
return node_id_eq(&n->id, pc);
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
2016-09-28 16:52:03 +02:00
|
|
|
|
2019-04-08 06:43:43 +02:00
|
|
|
|
2018-02-27 20:59:48 +01:00
|
|
|
static void destroy_node(struct node *node, struct routing_state *rstate)
|
2016-08-18 06:55:13 +02:00
|
|
|
{
|
2019-04-08 01:51:30 +02:00
|
|
|
struct chan_map_iter i;
|
|
|
|
struct chan *c;
|
2018-02-27 20:59:48 +01:00
|
|
|
node_map_del(rstate->nodes, node);
|
|
|
|
|
2019-04-08 06:43:43 +02:00
|
|
|
/* These remove themselves from chans[]. */
|
2019-04-08 06:42:43 +02:00
|
|
|
while ((c = first_chan(node, &i)) != NULL)
|
2019-05-21 09:13:28 +02:00
|
|
|
free_chan(rstate, c);
|
2019-04-08 06:43:43 +02:00
|
|
|
|
|
|
|
/* Free htable if we need. */
|
|
|
|
if (node_uses_chan_map(node))
|
|
|
|
chan_map_clear(&node->chans.map);
|
2016-08-18 06:55:13 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node *get_node(struct routing_state *rstate,
|
|
|
|
const struct node_id *id)
|
2017-09-01 06:18:55 +02:00
|
|
|
{
|
2018-07-04 07:29:56 +02:00
|
|
|
return node_map_get(rstate->nodes, id);
|
2017-09-01 06:18:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct node *new_node(struct routing_state *rstate,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
|
|
|
struct node *n;
|
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
assert(!get_node(rstate, id));
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
n = tal(rstate, struct node);
|
2016-06-28 23:19:21 +02:00
|
|
|
n->id = *id;
|
2019-04-08 06:43:43 +02:00
|
|
|
memset(n->chans.arr, 0, sizeof(n->chans.arr));
|
2019-04-10 09:31:29 +02:00
|
|
|
broadcastable_init(&n->bcast);
|
2019-11-14 01:16:53 +01:00
|
|
|
/* We don't know, so assume legacy. */
|
|
|
|
n->hop_style = ROUTE_HOP_LEGACY;
|
2019-09-16 12:44:00 +02:00
|
|
|
n->tokens = TOKEN_MAX;
|
2017-01-19 23:46:07 +01:00
|
|
|
node_map_add(rstate->nodes, n);
|
2018-02-27 20:59:48 +01:00
|
|
|
tal_add_destructor2(n, destroy_node, rstate);
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
/* We've received a channel_announce for a channel attached to this node:
|
|
|
|
* otherwise it's in the map only because it's a peer, or us. */
|
2018-06-08 08:31:55 +02:00
|
|
|
static bool node_has_public_channels(struct node *node)
|
|
|
|
{
|
2019-04-08 01:51:30 +02:00
|
|
|
struct chan_map_iter i;
|
|
|
|
struct chan *c;
|
|
|
|
|
2019-04-08 06:42:43 +02:00
|
|
|
for (c = first_chan(node, &i); c; c = next_chan(node, &i)) {
|
2019-04-08 01:51:30 +02:00
|
|
|
if (is_chan_public(c))
|
2018-06-08 08:31:55 +02:00
|
|
|
return true;
|
2019-04-08 01:51:30 +02:00
|
|
|
}
|
2018-06-08 08:31:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can *send* a channel_announce for a channel attached to this node:
|
|
|
|
* we only send once we have a channel_update. */
|
|
|
|
static bool node_has_broadcastable_channels(struct node *node)
|
|
|
|
{
|
2019-04-08 01:51:30 +02:00
|
|
|
struct chan_map_iter i;
|
|
|
|
struct chan *c;
|
2016-08-18 06:55:13 +02:00
|
|
|
|
2019-04-08 06:42:43 +02:00
|
|
|
for (c = first_chan(node, &i); c; c = next_chan(node, &i)) {
|
2019-04-08 01:51:30 +02:00
|
|
|
if (!is_chan_public(c))
|
2016-08-18 06:55:13 +02:00
|
|
|
continue;
|
2019-04-08 01:51:30 +02:00
|
|
|
if (is_halfchan_defined(&c->half[0])
|
|
|
|
|| is_halfchan_defined(&c->half[1]))
|
|
|
|
return true;
|
2016-08-18 06:55:13 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:31:55 +02:00
|
|
|
static bool node_announce_predates_channels(const struct node *node)
|
2016-08-18 06:55:13 +02:00
|
|
|
{
|
2019-04-08 01:51:30 +02:00
|
|
|
struct chan_map_iter i;
|
|
|
|
struct chan *c;
|
|
|
|
|
2019-04-08 06:42:43 +02:00
|
|
|
for (c = first_chan(node, &i); c; c = next_chan(node, &i)) {
|
2019-04-11 07:15:22 +02:00
|
|
|
if (!is_chan_public(c))
|
2018-06-08 08:31:55 +02:00
|
|
|
continue;
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
if (c->bcast.index < node->bcast.index)
|
2018-06-08 08:31:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_chan_from_node(struct routing_state *rstate,
|
|
|
|
struct node *node, const struct chan *chan)
|
|
|
|
{
|
2019-04-08 06:43:43 +02:00
|
|
|
size_t num_chans;
|
|
|
|
|
|
|
|
if (!node_uses_chan_map(node)) {
|
|
|
|
num_chans = 0;
|
|
|
|
for (size_t i = 0; i < NUM_IMMEDIATE_CHANS; i++) {
|
|
|
|
if (node->chans.arr[i] == chan)
|
|
|
|
node->chans.arr[i] = NULL;
|
|
|
|
else if (node->chans.arr[i] != NULL)
|
|
|
|
num_chans++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!chan_map_del(&node->chans.map, chan))
|
|
|
|
abort();
|
2019-08-21 05:52:43 +02:00
|
|
|
num_chans = chan_map_count(&node->chans.map);
|
2019-04-08 06:43:43 +02:00
|
|
|
}
|
2018-03-02 09:59:16 +01:00
|
|
|
|
2018-06-08 08:31:55 +02:00
|
|
|
/* Last channel? Simply delete node (and associated announce) */
|
2019-04-08 06:43:43 +02:00
|
|
|
if (num_chans == 0) {
|
2019-06-03 20:22:25 +02:00
|
|
|
gossip_store_delete(rstate->gs,
|
2019-06-03 20:09:25 +02:00
|
|
|
&node->bcast,
|
|
|
|
WIRE_NODE_ANNOUNCEMENT);
|
2018-06-08 08:31:55 +02:00
|
|
|
tal_free(node);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
if (!node->bcast.index)
|
2018-06-08 08:31:55 +02:00
|
|
|
return;
|
2018-03-02 09:59:16 +01:00
|
|
|
|
2018-06-08 08:31:55 +02:00
|
|
|
/* Removed only public channel? Remove node announcement. */
|
|
|
|
if (!node_has_broadcastable_channels(node)) {
|
2019-06-03 20:22:25 +02:00
|
|
|
gossip_store_delete(rstate->gs,
|
2019-06-03 20:09:25 +02:00
|
|
|
&node->bcast,
|
|
|
|
WIRE_NODE_ANNOUNCEMENT);
|
2018-06-08 08:31:55 +02:00
|
|
|
} else if (node_announce_predates_channels(node)) {
|
2019-04-11 07:15:22 +02:00
|
|
|
const u8 *announce;
|
|
|
|
|
2019-06-03 20:22:25 +02:00
|
|
|
announce = gossip_store_get(tmpctx, rstate->gs,
|
2019-04-11 07:15:22 +02:00
|
|
|
node->bcast.index);
|
|
|
|
|
2018-06-08 08:31:55 +02:00
|
|
|
/* node announcement predates all channel announcements?
|
|
|
|
* Move to end (we could, in theory, move to just past next
|
|
|
|
* channel_announce, but we don't care that much about spurious
|
|
|
|
* retransmissions in this corner case */
|
2019-06-03 20:22:25 +02:00
|
|
|
gossip_store_delete(rstate->gs,
|
2019-06-03 20:09:25 +02:00
|
|
|
&node->bcast,
|
|
|
|
WIRE_NODE_ANNOUNCEMENT);
|
2019-06-03 20:22:25 +02:00
|
|
|
node->bcast.index = gossip_store_add(rstate->gs,
|
|
|
|
announce,
|
|
|
|
node->bcast.timestamp,
|
2019-11-04 01:37:05 +01:00
|
|
|
false,
|
2019-06-03 20:22:25 +02:00
|
|
|
NULL);
|
2018-06-08 08:31:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 02:24:01 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
/* We make sure that free_chan is called on this chan! */
|
|
|
|
static void destroy_chan_check(struct chan *chan)
|
|
|
|
{
|
2019-10-21 12:02:04 +02:00
|
|
|
assert(chan->sat.satoshis == (unsigned long)chan); /* Raw: dev-hack */
|
2019-09-12 02:24:01 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-21 09:13:28 +02:00
|
|
|
/* We used to make this a tal_add_destructor2, but that costs 40 bytes per
|
|
|
|
* chan, and we only ever explicitly free it anyway. */
|
|
|
|
void free_chan(struct routing_state *rstate, struct chan *chan)
|
2018-06-08 08:31:55 +02:00
|
|
|
{
|
|
|
|
remove_chan_from_node(rstate, chan->nodes[0], chan);
|
|
|
|
remove_chan_from_node(rstate, chan->nodes[1], chan);
|
|
|
|
|
|
|
|
uintmap_del(&rstate->chanmap, chan->scid.u64);
|
2019-04-11 07:15:22 +02:00
|
|
|
|
2019-09-12 02:24:01 +02:00
|
|
|
#if DEVELOPER
|
2019-10-21 12:02:04 +02:00
|
|
|
chan->sat.satoshis = (unsigned long)chan; /* Raw: dev-hack */
|
2019-09-12 02:24:01 +02:00
|
|
|
#endif
|
2019-05-21 09:13:28 +02:00
|
|
|
tal_free(chan);
|
2016-08-18 06:55:13 +02:00
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
static void init_half_chan(struct routing_state *rstate,
|
|
|
|
struct chan *chan,
|
2018-09-20 02:59:46 +02:00
|
|
|
int channel_idx)
|
2016-12-12 14:55:46 +01:00
|
|
|
{
|
2018-09-20 02:59:46 +02:00
|
|
|
struct half_chan *c = &chan->half[channel_idx];
|
2018-03-02 09:59:16 +01:00
|
|
|
|
2018-09-20 02:59:46 +02:00
|
|
|
/* Set the channel direction */
|
|
|
|
c->channel_flags = channel_idx;
|
|
|
|
// TODO: wireup message_flags
|
|
|
|
c->message_flags = 0;
|
2019-04-10 09:31:29 +02:00
|
|
|
broadcastable_init(&c->bcast);
|
2019-09-16 12:44:00 +02:00
|
|
|
c->tokens = TOKEN_MAX;
|
2016-12-12 14:55:46 +01:00
|
|
|
}
|
|
|
|
|
2019-10-08 03:14:24 +02:00
|
|
|
static void bad_gossip_order(const u8 *msg,
|
|
|
|
const struct peer *peer,
|
2018-05-18 07:49:08 +02:00
|
|
|
const char *details)
|
2018-05-17 07:09:59 +02:00
|
|
|
{
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Bad gossip order: %s before announcement %s",
|
2020-08-31 03:13:25 +02:00
|
|
|
peer_wire_name(fromwire_peektype(msg)),
|
2019-11-18 01:26:17 +01:00
|
|
|
details);
|
2018-05-17 07:09:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 12:43:51 +02:00
|
|
|
static void destroy_local_chan(struct local_chan *local_chan,
|
|
|
|
struct routing_state *rstate)
|
|
|
|
{
|
|
|
|
if (!local_chan_map_del(&rstate->local_chan_map, local_chan))
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2019-09-18 03:05:14 +02:00
|
|
|
static void maybe_add_local_chan(struct routing_state *rstate,
|
|
|
|
struct chan *chan)
|
2019-09-16 12:43:51 +02:00
|
|
|
{
|
|
|
|
int direction;
|
|
|
|
struct local_chan *local_chan;
|
|
|
|
|
|
|
|
if (node_id_eq(&chan->nodes[0]->id, &rstate->local_id))
|
|
|
|
direction = 0;
|
|
|
|
else if (node_id_eq(&chan->nodes[1]->id, &rstate->local_id))
|
|
|
|
direction = 1;
|
|
|
|
else
|
2019-09-18 03:05:14 +02:00
|
|
|
return;
|
2019-09-16 12:43:51 +02:00
|
|
|
|
|
|
|
local_chan = tal(chan, struct local_chan);
|
|
|
|
local_chan->chan = chan;
|
|
|
|
local_chan->direction = direction;
|
|
|
|
local_chan->local_disabled = false;
|
2019-09-16 12:43:59 +02:00
|
|
|
local_chan->channel_update_timer = NULL;
|
2019-09-16 12:43:51 +02:00
|
|
|
|
|
|
|
local_chan_map_add(&rstate->local_chan_map, local_chan);
|
|
|
|
tal_add_destructor2(local_chan, destroy_local_chan, rstate);
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *new_chan(struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *scid,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id1,
|
|
|
|
const struct node_id *id2,
|
2020-05-04 02:18:34 +02:00
|
|
|
struct amount_sat satoshis,
|
|
|
|
const u8 *features)
|
2018-03-02 09:59:17 +01:00
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan = tal(rstate, struct chan);
|
2019-04-08 11:58:32 +02:00
|
|
|
int n1idx = node_id_idx(id1, id2);
|
2018-03-02 09:59:17 +01:00
|
|
|
struct node *n1, *n2;
|
|
|
|
|
2019-09-12 02:24:01 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
tal_add_destructor(chan, destroy_chan_check);
|
|
|
|
#endif
|
2018-05-01 20:18:04 +02:00
|
|
|
/* We should never add a channel twice */
|
|
|
|
assert(!uintmap_get(&rstate->chanmap, scid->u64));
|
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Create nodes on demand */
|
|
|
|
n1 = get_node(rstate, id1);
|
|
|
|
if (!n1)
|
|
|
|
n1 = new_node(rstate, id1);
|
|
|
|
n2 = get_node(rstate, id2);
|
|
|
|
if (!n2)
|
|
|
|
n2 = new_node(rstate, id2);
|
2018-03-02 09:59:17 +01:00
|
|
|
|
|
|
|
chan->scid = *scid;
|
|
|
|
chan->nodes[n1idx] = n1;
|
|
|
|
chan->nodes[!n1idx] = n2;
|
2019-04-10 09:31:29 +02:00
|
|
|
broadcastable_init(&chan->bcast);
|
2019-06-03 20:07:25 +02:00
|
|
|
/* This is how we indicate it's not public yet. */
|
|
|
|
chan->bcast.timestamp = 0;
|
2019-02-21 04:45:55 +01:00
|
|
|
chan->sat = satoshis;
|
2018-03-02 09:59:17 +01:00
|
|
|
|
2019-04-08 06:43:43 +02:00
|
|
|
add_chan(n2, chan);
|
|
|
|
add_chan(n1, chan);
|
2018-03-02 09:59:17 +01:00
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Populate with (inactive) connections */
|
2018-03-04 03:26:59 +01:00
|
|
|
init_half_chan(rstate, chan, n1idx);
|
|
|
|
init_half_chan(rstate, chan, !n1idx);
|
2018-03-02 09:59:17 +01:00
|
|
|
|
2020-05-04 02:18:34 +02:00
|
|
|
/* Stash hint here about whether we have features */
|
|
|
|
chan->half[0].any_features = tal_bytelen(features) != 0;
|
2018-03-04 03:26:59 +01:00
|
|
|
uintmap_add(&rstate->chanmap, scid->u64, chan);
|
2019-09-16 12:43:51 +02:00
|
|
|
|
|
|
|
/* Initialize shadow structure if it's local */
|
2019-09-18 03:05:14 +02:00
|
|
|
maybe_add_local_chan(rstate, chan);
|
2018-03-02 09:59:17 +01:00
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
/* Too big to reach, but don't overflow if added. */
|
2019-02-21 04:45:55 +01:00
|
|
|
#define INFINITE AMOUNT_MSAT(0x3FFFFFFFFFFFFFFFULL)
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
/* We hack a multimap into a uintmap to implement a minheap by cost.
|
|
|
|
* This is relatively inefficient, containing an array for each cost
|
2019-04-17 09:35:33 +02:00
|
|
|
* value, assuming there aren't too many at same cost.
|
|
|
|
*
|
|
|
|
* We further optimize by never freeing or shrinking these entries,
|
|
|
|
* but delete by replacing with NULL. This means that we cache the
|
|
|
|
* lowest index which actually contains something, since others may
|
|
|
|
* contain empty arrays. */
|
|
|
|
struct unvisited {
|
|
|
|
u64 min_index;
|
|
|
|
UINTMAP(struct node **) map;
|
|
|
|
};
|
2019-04-17 09:35:33 +02:00
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
/* Risk of passing through this channel.
|
|
|
|
*
|
|
|
|
* There are two ways this function is used:
|
|
|
|
*
|
|
|
|
* 1. Normally, riskbias = 1. A tiny bias here in order to prefer
|
|
|
|
* shorter routes, all things equal.
|
|
|
|
* 2. Trying to find a shorter route, riskbias > 1. By adding an extra
|
|
|
|
* cost to every hop, we're trying to bias against overlength routes.
|
|
|
|
*/
|
2019-02-21 04:45:55 +01:00
|
|
|
static WARN_UNUSED_RESULT bool risk_add_fee(struct amount_msat *risk,
|
|
|
|
struct amount_msat msat,
|
2019-04-17 09:35:33 +02:00
|
|
|
u32 delay, double riskfactor,
|
|
|
|
u64 riskbias)
|
2016-09-06 09:17:48 +02:00
|
|
|
{
|
2020-08-05 05:56:32 +02:00
|
|
|
struct amount_msat riskfee;
|
2019-02-21 04:45:55 +01:00
|
|
|
|
2020-08-05 05:56:32 +02:00
|
|
|
if (!amount_msat_scale(&riskfee, msat, riskfactor * delay))
|
2019-02-21 04:45:55 +01:00
|
|
|
return false;
|
2020-08-05 05:56:32 +02:00
|
|
|
if (!amount_msat_add(&riskfee, riskfee, amount_msat(riskbias)))
|
|
|
|
return false;
|
|
|
|
return amount_msat_add(risk, *risk, riskfee);
|
2016-09-06 09:17:48 +02:00
|
|
|
}
|
|
|
|
|
2018-09-22 02:34:03 +02:00
|
|
|
/* Check that we can fit through this channel's indicated
|
|
|
|
* maximum_ and minimum_msat requirements.
|
|
|
|
*/
|
2019-02-21 04:45:55 +01:00
|
|
|
static bool hc_can_carry(const struct half_chan *hc,
|
|
|
|
struct amount_msat requiredcap)
|
|
|
|
{
|
|
|
|
return amount_msat_greater_eq(hc->htlc_maximum, requiredcap) &&
|
|
|
|
amount_msat_less_eq(hc->htlc_minimum, requiredcap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Theoretically, this could overflow. */
|
2019-04-17 09:35:33 +02:00
|
|
|
static bool fuzz_fee(u64 *fee,
|
|
|
|
const struct short_channel_id *scid,
|
|
|
|
double fuzz, const struct siphash_seed *base_seed)
|
2018-09-22 02:34:03 +02:00
|
|
|
{
|
2019-04-17 09:35:33 +02:00
|
|
|
u64 fuzzed_fee, h;
|
|
|
|
double fee_scale;
|
|
|
|
|
|
|
|
if (fuzz == 0.0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
h = siphash24(base_seed, scid, sizeof(*scid));
|
|
|
|
|
|
|
|
/* Scale fees for this channel */
|
|
|
|
/* rand = (h / UINT64_MAX) random number between 0.0 -> 1.0
|
|
|
|
* 2*fuzz*rand random number between 0.0 -> 2*fuzz
|
|
|
|
* 2*fuzz*rand - fuzz random number between -fuzz -> +fuzz
|
|
|
|
*/
|
2020-03-17 16:03:39 +01:00
|
|
|
fee_scale = 1.0 + (2.0 * fuzz * h / (double)UINT64_MAX) - fuzz;
|
2019-04-17 09:35:33 +02:00
|
|
|
fuzzed_fee = *fee * fee_scale;
|
2019-02-21 04:45:55 +01:00
|
|
|
if (fee_scale > 1.0 && fuzzed_fee < *fee)
|
|
|
|
return false;
|
|
|
|
*fee = fuzzed_fee;
|
|
|
|
return true;
|
2018-09-22 02:34:03 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:01 +02:00
|
|
|
/* Can we carry this amount across the channel? If so, returns true and
|
|
|
|
* sets newtotal and newrisk */
|
|
|
|
static bool can_reach(const struct half_chan *c,
|
2019-04-17 09:35:33 +02:00
|
|
|
const struct short_channel_id *scid,
|
2019-05-31 09:30:33 +02:00
|
|
|
bool no_charge,
|
2019-04-17 09:35:01 +02:00
|
|
|
struct amount_msat total,
|
|
|
|
struct amount_msat risk,
|
2019-04-17 09:35:33 +02:00
|
|
|
double riskfactor,
|
2019-04-17 09:35:33 +02:00
|
|
|
u64 riskbias,
|
2019-04-17 09:35:33 +02:00
|
|
|
double fuzz, const struct siphash_seed *base_seed,
|
2019-04-17 09:35:01 +02:00
|
|
|
struct amount_msat *newtotal, struct amount_msat *newrisk)
|
|
|
|
{
|
|
|
|
/* FIXME: Bias against smaller channels. */
|
|
|
|
struct amount_msat fee;
|
|
|
|
|
|
|
|
if (!amount_msat_fee(&fee, total, c->base_fee, c->proportional_fee))
|
|
|
|
return false;
|
|
|
|
|
2019-05-31 09:30:33 +02:00
|
|
|
if (!fuzz_fee(&fee.millisatoshis, scid, fuzz, base_seed)) /* Raw: double manipulation */
|
2019-04-17 09:35:01 +02:00
|
|
|
return false;
|
|
|
|
|
2019-05-31 09:30:33 +02:00
|
|
|
if (no_charge) {
|
|
|
|
*newtotal = total;
|
|
|
|
|
|
|
|
/* We still want to consider the "charge", since it's indicative
|
|
|
|
* of a bias (we discounted one channel for a reason), but we
|
|
|
|
* don't pay it. So we count it as additional risk. */
|
|
|
|
if (!amount_msat_add(newrisk, risk, fee))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
*newrisk = risk;
|
|
|
|
|
|
|
|
if (!amount_msat_add(newtotal, total, fee))
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 09:35:01 +02:00
|
|
|
|
|
|
|
/* Skip a channel if it indicated that it won't route the
|
|
|
|
* requested amount. */
|
|
|
|
if (!hc_can_carry(c, *newtotal))
|
|
|
|
return false;
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
if (!risk_add_fee(newrisk, *newtotal, c->delay, riskfactor, riskbias))
|
2019-04-17 09:35:01 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
/* Returns false on overflow (shouldn't happen!) */
|
|
|
|
typedef bool WARN_UNUSED_RESULT costfn_t(struct amount_msat *,
|
|
|
|
struct amount_msat,
|
|
|
|
struct amount_msat);
|
|
|
|
|
|
|
|
static WARN_UNUSED_RESULT bool
|
|
|
|
normal_cost_function(struct amount_msat *cost,
|
|
|
|
struct amount_msat total, struct amount_msat risk)
|
|
|
|
{
|
|
|
|
if (amount_msat_add(cost, total, risk))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
status_broken("Can't add cost of node %s + %s",
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &total),
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &risk));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WARN_UNUSED_RESULT bool
|
|
|
|
shortest_cost_function(struct amount_msat *cost,
|
|
|
|
struct amount_msat total, struct amount_msat risk)
|
|
|
|
{
|
|
|
|
/* We add 1, so cost is never 0, for our hacky uintmap-as-minheap. */
|
|
|
|
if (amount_msat_add(cost, risk, AMOUNT_MSAT(1)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
status_broken("Can't add 1 to risk of node %s",
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &risk));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:01 +02:00
|
|
|
/* Does totala+riska add up to less than totalb+riskb?
|
|
|
|
* Saves sums if you want them.
|
|
|
|
*/
|
|
|
|
static bool costs_less(struct amount_msat totala,
|
|
|
|
struct amount_msat riska,
|
|
|
|
struct amount_msat *costa,
|
|
|
|
struct amount_msat totalb,
|
|
|
|
struct amount_msat riskb,
|
2019-04-17 09:35:33 +02:00
|
|
|
struct amount_msat *costb,
|
|
|
|
costfn_t *costfn)
|
2019-04-17 09:35:01 +02:00
|
|
|
{
|
|
|
|
struct amount_msat suma, sumb;
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
if (!costfn(&suma, totala, riska))
|
2019-04-17 09:35:01 +02:00
|
|
|
return false;
|
2019-04-17 09:35:33 +02:00
|
|
|
if (!costfn(&sumb, totalb, riskb))
|
2019-04-17 09:35:01 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (costa)
|
|
|
|
*costa = suma;
|
|
|
|
if (costb)
|
|
|
|
*costb = sumb;
|
|
|
|
return amount_msat_less(suma, sumb);
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
/* Determine if the given half_chan is routable */
|
2019-04-11 07:15:22 +02:00
|
|
|
static bool hc_is_routable(struct routing_state *rstate,
|
|
|
|
const struct chan *chan, int idx)
|
2018-02-02 02:06:26 +01:00
|
|
|
{
|
2019-04-11 07:15:22 +02:00
|
|
|
return is_halfchan_enabled(&chan->half[idx])
|
|
|
|
&& !is_chan_local_disabled(rstate, chan);
|
2018-02-02 02:06:26 +01:00
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
static void unvisited_add(struct unvisited *unvisited, struct amount_msat cost,
|
|
|
|
struct node **arr)
|
|
|
|
{
|
|
|
|
u64 idx = cost.millisatoshis; /* Raw: uintmap needs u64 index */
|
|
|
|
if (idx < unvisited->min_index) {
|
|
|
|
assert(idx); /* We don't allow sending 0 satoshis */
|
|
|
|
unvisited->min_index = idx - 1;
|
|
|
|
}
|
|
|
|
uintmap_add(&unvisited->map, idx, arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct node **unvisited_get(const struct unvisited *unvisited,
|
|
|
|
struct amount_msat cost)
|
|
|
|
{
|
|
|
|
return uintmap_get(&unvisited->map, cost.millisatoshis); /* Raw: uintmap */
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct node **unvisited_del(struct unvisited *unvisited,
|
|
|
|
struct amount_msat cost)
|
|
|
|
{
|
|
|
|
return uintmap_del(&unvisited->map, cost.millisatoshis); /* Raw: uintmap */
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
static bool is_unvisited(const struct node *node,
|
2019-04-17 09:35:33 +02:00
|
|
|
const struct unvisited *unvisited,
|
|
|
|
costfn_t *costfn)
|
2019-04-17 09:35:33 +02:00
|
|
|
{
|
|
|
|
struct node **arr;
|
|
|
|
struct amount_msat cost;
|
|
|
|
|
|
|
|
/* If it's infinite, definitely unvisited */
|
|
|
|
if (amount_msat_eq(node->dijkstra.total, INFINITE))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Shouldn't happen! */
|
2019-04-17 09:35:33 +02:00
|
|
|
if (!costfn(&cost, node->dijkstra.total, node->dijkstra.risk))
|
2019-04-17 09:35:33 +02:00
|
|
|
return false;
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
arr = unvisited_get(unvisited, cost);
|
2019-04-17 09:35:33 +02:00
|
|
|
for (size_t i = 0; i < tal_count(arr); i++) {
|
|
|
|
if (arr[i] == node)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
static void unvisited_del_node(struct unvisited *unvisited,
|
2019-04-17 09:35:33 +02:00
|
|
|
struct amount_msat cost,
|
|
|
|
const struct node *node)
|
|
|
|
{
|
|
|
|
struct node **arr;
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
arr = unvisited_get(unvisited, cost);
|
|
|
|
for (size_t i = 0; i < tal_count(arr); i++) {
|
|
|
|
if (arr[i] == node) {
|
|
|
|
arr[i] = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abort();
|
2019-04-17 09:35:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void adjust_unvisited(struct node *node,
|
2019-04-17 09:35:33 +02:00
|
|
|
struct unvisited *unvisited,
|
2019-04-17 09:35:33 +02:00
|
|
|
struct amount_msat cost_before,
|
|
|
|
struct amount_msat total,
|
|
|
|
struct amount_msat risk,
|
|
|
|
struct amount_msat cost_after)
|
|
|
|
{
|
|
|
|
struct node **arr;
|
|
|
|
|
|
|
|
/* If it was in unvisited map, remove it. */
|
|
|
|
if (!amount_msat_eq(node->dijkstra.total, INFINITE))
|
|
|
|
unvisited_del_node(unvisited, cost_before, node);
|
|
|
|
|
|
|
|
/* Update node */
|
|
|
|
node->dijkstra.total = total;
|
|
|
|
node->dijkstra.risk = risk;
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
SUPERVERBOSE("%s now cost %s",
|
|
|
|
type_to_string(tmpctx, struct node_id, &node->id),
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &cost_after));
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
/* Update map of unvisited nodes */
|
2019-04-17 09:35:33 +02:00
|
|
|
arr = unvisited_get(unvisited, cost_after);
|
|
|
|
if (arr) {
|
|
|
|
struct node **old_arr;
|
|
|
|
/* Try for empty slot */
|
|
|
|
for (size_t i = 0; i < tal_count(arr); i++) {
|
|
|
|
if (arr[i] == NULL) {
|
|
|
|
arr[i] = node;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Nope, expand */
|
|
|
|
old_arr = arr;
|
|
|
|
tal_arr_expand(&arr, node);
|
|
|
|
if (arr == old_arr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Realloc moved it; del and add again. */
|
|
|
|
unvisited_del(unvisited, cost_after);
|
|
|
|
} else {
|
2019-04-17 09:35:33 +02:00
|
|
|
arr = tal_arr(unvisited, struct node *, 1);
|
|
|
|
arr[0] = node;
|
2019-04-17 09:35:33 +02:00
|
|
|
}
|
2019-04-17 09:35:33 +02:00
|
|
|
|
|
|
|
unvisited_add(unvisited, cost_after, arr);
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
static void remove_unvisited(struct node *node, struct unvisited *unvisited,
|
|
|
|
costfn_t *costfn)
|
2019-04-17 09:35:33 +02:00
|
|
|
{
|
|
|
|
struct amount_msat cost;
|
|
|
|
|
|
|
|
/* Shouldn't happen! */
|
2019-04-17 09:35:33 +02:00
|
|
|
if (!costfn(&cost, node->dijkstra.total, node->dijkstra.risk))
|
2019-04-17 09:35:33 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
unvisited_del_node(unvisited, cost, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_unvisited_neighbors(struct routing_state *rstate,
|
|
|
|
struct node *cur,
|
2019-05-31 09:30:33 +02:00
|
|
|
const struct node *me,
|
2019-04-17 09:35:33 +02:00
|
|
|
double riskfactor,
|
2019-04-17 09:35:33 +02:00
|
|
|
u64 riskbias,
|
2019-04-17 09:35:33 +02:00
|
|
|
double fuzz,
|
|
|
|
const struct siphash_seed *base_seed,
|
2019-04-17 09:35:33 +02:00
|
|
|
struct unvisited *unvisited,
|
|
|
|
costfn_t *costfn)
|
2019-04-17 09:35:33 +02:00
|
|
|
{
|
|
|
|
struct chan_map_iter i;
|
|
|
|
struct chan *chan;
|
|
|
|
|
|
|
|
/* Consider all neighbors */
|
|
|
|
for (chan = first_chan(cur, &i); chan; chan = next_chan(cur, &i)) {
|
|
|
|
struct amount_msat total, risk, cost_before, cost_after;
|
|
|
|
int idx = half_chan_to(cur, chan);
|
|
|
|
struct node *peer = chan->nodes[idx];
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
SUPERVERBOSE("CONSIDERING: %s -> %s (%s/%s)",
|
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&cur->id),
|
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&peer->id),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&peer->dijkstra.total),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&peer->dijkstra.risk));
|
|
|
|
|
|
|
|
if (!hc_is_routable(rstate, chan, idx)) {
|
|
|
|
SUPERVERBOSE("... not routable");
|
2019-04-17 09:35:33 +02:00
|
|
|
continue;
|
2019-04-17 09:35:33 +02:00
|
|
|
}
|
2019-04-17 09:35:33 +02:00
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
if (!is_unvisited(peer, unvisited, costfn)) {
|
|
|
|
SUPERVERBOSE("... already visited");
|
2019-04-17 09:35:33 +02:00
|
|
|
continue;
|
2019-04-17 09:35:33 +02:00
|
|
|
}
|
2019-04-17 09:35:33 +02:00
|
|
|
|
2019-05-31 09:30:33 +02:00
|
|
|
/* We're looking at channels *backwards*, so peer == me
|
|
|
|
* is the right test here for whether we don't charge fees. */
|
|
|
|
if (!can_reach(&chan->half[idx], &chan->scid, peer == me,
|
2019-04-17 09:35:33 +02:00
|
|
|
cur->dijkstra.total, cur->dijkstra.risk,
|
2019-04-17 09:35:33 +02:00
|
|
|
riskfactor, riskbias, fuzz, base_seed,
|
2019-04-17 09:35:33 +02:00
|
|
|
&total, &risk)) {
|
|
|
|
SUPERVERBOSE("... can't reach");
|
2019-04-17 09:35:33 +02:00
|
|
|
continue;
|
2019-04-17 09:35:33 +02:00
|
|
|
}
|
2019-04-17 09:35:33 +02:00
|
|
|
|
|
|
|
/* This effectively adds it to the map if it was infinite */
|
|
|
|
if (costs_less(total, risk, &cost_after,
|
|
|
|
peer->dijkstra.total, peer->dijkstra.risk,
|
2019-04-17 09:35:33 +02:00
|
|
|
&cost_before,
|
|
|
|
costfn)) {
|
2019-04-17 09:35:33 +02:00
|
|
|
SUPERVERBOSE("...%s can reach %s"
|
|
|
|
" total %s risk %s",
|
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&cur->id),
|
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&peer->id),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&total),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&risk));
|
|
|
|
adjust_unvisited(peer, unvisited,
|
|
|
|
cost_before, total, risk, cost_after);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
static struct node *first_unvisited(struct unvisited *unvisited)
|
2019-04-17 09:35:33 +02:00
|
|
|
{
|
2019-04-17 09:35:33 +02:00
|
|
|
struct node **arr;
|
|
|
|
|
|
|
|
while ((arr = uintmap_after(&unvisited->map, &unvisited->min_index))) {
|
2019-04-17 09:35:33 +02:00
|
|
|
for (size_t i = 0; i < tal_count(arr); i++) {
|
|
|
|
if (arr[i]) {
|
|
|
|
unvisited->min_index--;
|
2019-04-17 09:35:33 +02:00
|
|
|
return arr[i];
|
2019-04-17 09:35:33 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-17 09:35:33 +02:00
|
|
|
}
|
2019-04-17 09:35:33 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dijkstra(struct routing_state *rstate,
|
|
|
|
const struct node *dst,
|
2019-05-31 09:30:33 +02:00
|
|
|
const struct node *me,
|
2019-04-17 09:35:33 +02:00
|
|
|
double riskfactor,
|
2019-04-17 09:35:33 +02:00
|
|
|
u64 riskbias,
|
2019-04-17 09:35:33 +02:00
|
|
|
double fuzz, const struct siphash_seed *base_seed,
|
2019-04-17 09:35:33 +02:00
|
|
|
struct unvisited *unvisited,
|
|
|
|
costfn_t *costfn)
|
2019-04-17 09:35:33 +02:00
|
|
|
{
|
|
|
|
struct node *cur;
|
|
|
|
|
|
|
|
while ((cur = first_unvisited(unvisited)) != NULL) {
|
2019-05-31 09:30:33 +02:00
|
|
|
update_unvisited_neighbors(rstate, cur, me,
|
|
|
|
riskfactor, riskbias,
|
2019-04-17 09:35:33 +02:00
|
|
|
fuzz, base_seed, unvisited, costfn);
|
2019-04-17 09:35:33 +02:00
|
|
|
remove_unvisited(cur, unvisited, costfn);
|
2019-04-17 09:35:33 +02:00
|
|
|
if (cur == dst)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note that we calculated route *backwards*, for fees. So "from"
|
|
|
|
* here has a high cost, "to" has a cost of exact amount sent. */
|
|
|
|
static struct chan **build_route(const tal_t *ctx,
|
|
|
|
struct routing_state *rstate,
|
|
|
|
const struct node *from,
|
|
|
|
const struct node *to,
|
2019-05-31 09:30:33 +02:00
|
|
|
const struct node *me,
|
2019-04-17 09:35:33 +02:00
|
|
|
double riskfactor,
|
2019-04-17 09:35:33 +02:00
|
|
|
u64 riskbias,
|
2019-04-17 09:35:33 +02:00
|
|
|
double fuzz,
|
|
|
|
const struct siphash_seed *base_seed,
|
2019-04-17 09:35:33 +02:00
|
|
|
struct amount_msat *fee)
|
|
|
|
{
|
|
|
|
const struct node *i;
|
|
|
|
struct chan **route, *chan;
|
|
|
|
|
|
|
|
SUPERVERBOSE("Building route from %s (%s) -> %s (%s)",
|
|
|
|
type_to_string(tmpctx, struct node_id, &from->id),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&from->dijkstra.total),
|
|
|
|
type_to_string(tmpctx, struct node_id, &to->id),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&to->dijkstra.total));
|
|
|
|
/* Never reached? */
|
|
|
|
if (amount_msat_eq(from->dijkstra.total, INFINITE))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Walk to find which neighbors we used */
|
|
|
|
route = tal_arr(ctx, struct chan *, 0);
|
|
|
|
for (i = from; i != to; i = other_node(i, chan)) {
|
|
|
|
struct chan_map_iter it;
|
|
|
|
|
|
|
|
/* Consider all neighbors */
|
|
|
|
for (chan = first_chan(i, &it); chan; chan = next_chan(i, &it)) {
|
|
|
|
struct node *peer = other_node(i, chan);
|
|
|
|
struct half_chan *hc = half_chan_from(i, chan);
|
|
|
|
struct amount_msat total, risk;
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
SUPERVERBOSE("CONSIDER: %s -> %s (%s/%s)",
|
2019-04-17 09:35:33 +02:00
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&i->id),
|
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&peer->id),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
2019-04-17 09:35:33 +02:00
|
|
|
&peer->dijkstra.total),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&peer->dijkstra.risk));
|
2019-04-17 09:35:33 +02:00
|
|
|
|
|
|
|
/* If traversing this wasn't possible, ignore */
|
2019-04-17 09:35:33 +02:00
|
|
|
if (!hc_is_routable(rstate, chan, !half_chan_to(i, chan))) {
|
2019-04-17 09:35:33 +02:00
|
|
|
continue;
|
2019-04-17 09:35:33 +02:00
|
|
|
}
|
2019-04-17 09:35:33 +02:00
|
|
|
|
2019-05-31 09:30:33 +02:00
|
|
|
if (!can_reach(hc, &chan->scid, i == me,
|
2019-04-17 09:35:33 +02:00
|
|
|
peer->dijkstra.total, peer->dijkstra.risk,
|
2019-04-17 09:35:33 +02:00
|
|
|
riskfactor,
|
2019-04-17 09:35:33 +02:00
|
|
|
riskbias,
|
2019-04-17 09:35:33 +02:00
|
|
|
fuzz, base_seed,
|
2019-04-17 09:35:33 +02:00
|
|
|
&total, &risk))
|
2019-04-17 09:35:33 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* If this was the path we took, we're done (if there are
|
|
|
|
* two identical ones, it doesn't matter which) */
|
|
|
|
if (amount_msat_eq(total, i->dijkstra.total)
|
|
|
|
&& amount_msat_eq(risk, i->dijkstra.risk))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!chan) {
|
|
|
|
status_broken("Could not find hop to %s",
|
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&i->id));
|
|
|
|
return tal_free(route);
|
|
|
|
}
|
|
|
|
tal_arr_expand(&route, chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't charge ourselves fees, so skip first hop */
|
|
|
|
if (!amount_msat_sub(fee,
|
|
|
|
other_node(from, route[0])->dijkstra.total,
|
|
|
|
to->dijkstra.total)) {
|
|
|
|
status_broken("Could not subtract %s - %s for fee",
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&other_node(from, route[0])
|
|
|
|
->dijkstra.total),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&to->dijkstra.total));
|
|
|
|
return tal_free(route);
|
|
|
|
}
|
|
|
|
|
|
|
|
return route;
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
static struct unvisited *dijkstra_prepare(const tal_t *ctx,
|
|
|
|
struct routing_state *rstate,
|
|
|
|
struct node *src,
|
2019-04-17 09:35:33 +02:00
|
|
|
struct amount_msat msat,
|
|
|
|
costfn_t *costfn)
|
2019-04-17 09:35:33 +02:00
|
|
|
{
|
|
|
|
struct node_map_iter it;
|
2019-04-17 09:35:33 +02:00
|
|
|
struct unvisited *unvisited;
|
2019-04-17 09:35:33 +02:00
|
|
|
struct node *n;
|
|
|
|
struct node **arr;
|
2019-04-17 09:35:33 +02:00
|
|
|
struct amount_msat cost;
|
2019-04-17 09:35:33 +02:00
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
unvisited = tal(tmpctx, struct unvisited);
|
|
|
|
uintmap_init(&unvisited->map);
|
|
|
|
unvisited->min_index = UINT64_MAX;
|
2019-04-17 09:35:33 +02:00
|
|
|
|
|
|
|
/* Reset all the information. */
|
|
|
|
for (n = node_map_first(rstate->nodes, &it);
|
|
|
|
n;
|
|
|
|
n = node_map_next(rstate->nodes, &it)) {
|
|
|
|
if (n == src)
|
|
|
|
continue;
|
|
|
|
n->dijkstra.total = INFINITE;
|
2019-04-17 09:35:33 +02:00
|
|
|
n->dijkstra.risk = INFINITE;
|
2019-04-17 09:35:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark start cost: place in unvisited map. */
|
|
|
|
src->dijkstra.total = msat;
|
|
|
|
src->dijkstra.risk = AMOUNT_MSAT(0);
|
|
|
|
arr = tal_arr(unvisited, struct node *, 1);
|
|
|
|
arr[0] = src;
|
2019-04-17 09:35:33 +02:00
|
|
|
/* Adding 0 can never fail */
|
|
|
|
if (!costfn(&cost, src->dijkstra.total, src->dijkstra.risk))
|
|
|
|
abort();
|
|
|
|
unvisited_add(unvisited, cost, arr);
|
2019-04-17 09:35:33 +02:00
|
|
|
|
|
|
|
return unvisited;
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
static void dijkstra_cleanup(struct unvisited *unvisited)
|
2019-04-17 09:35:33 +02:00
|
|
|
{
|
|
|
|
struct node **arr;
|
|
|
|
u64 idx;
|
|
|
|
|
|
|
|
/* uintmap uses malloc, so manual cleaning needed */
|
2019-04-17 09:35:33 +02:00
|
|
|
while ((arr = uintmap_first(&unvisited->map, &idx)) != NULL) {
|
2019-04-17 09:35:33 +02:00
|
|
|
tal_free(arr);
|
2019-04-17 09:35:33 +02:00
|
|
|
uintmap_del(&unvisited->map, idx);
|
2019-04-17 09:35:33 +02:00
|
|
|
}
|
|
|
|
tal_free(unvisited);
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
/* We need to start biassing against long routes. */
|
|
|
|
static struct chan **
|
|
|
|
find_shorter_route(const tal_t *ctx, struct routing_state *rstate,
|
|
|
|
struct node *src, struct node *dst,
|
2019-05-31 09:30:33 +02:00
|
|
|
const struct node *me,
|
2019-04-17 09:35:33 +02:00
|
|
|
struct amount_msat msat,
|
2019-09-24 05:33:59 +02:00
|
|
|
u32 max_hops,
|
2019-04-17 09:35:33 +02:00
|
|
|
double fuzz, const struct siphash_seed *base_seed,
|
2019-04-17 09:35:33 +02:00
|
|
|
struct chan **long_route,
|
|
|
|
struct amount_msat *fee)
|
|
|
|
{
|
|
|
|
struct unvisited *unvisited;
|
2019-05-24 12:03:32 +02:00
|
|
|
struct chan **short_route = NULL;
|
2019-04-17 09:35:33 +02:00
|
|
|
struct amount_msat long_cost, short_cost, cost_diff;
|
|
|
|
u64 min_bias, max_bias;
|
|
|
|
double riskfactor;
|
|
|
|
|
|
|
|
/* We traverse backwards, so dst has largest total */
|
|
|
|
if (!amount_msat_sub(&long_cost,
|
|
|
|
dst->dijkstra.total, src->dijkstra.total))
|
|
|
|
goto bad_total;
|
|
|
|
tal_free(long_route);
|
|
|
|
|
|
|
|
/* FIXME: It's hard to juggle both the riskfactor and riskbias here,
|
|
|
|
* so we set our riskfactor to rougly equate to 1 millisatoshi
|
|
|
|
* per block delay, which is close enough to zero to not break
|
|
|
|
* this algorithm, but still provide some bias towards
|
|
|
|
* low-delay routes. */
|
2020-08-05 05:56:32 +02:00
|
|
|
riskfactor = amount_msat_ratio(AMOUNT_MSAT(1), msat);
|
2019-04-17 09:35:33 +02:00
|
|
|
|
|
|
|
/* First, figure out if a short route is even possible.
|
|
|
|
* We set the cost function to ignore total, riskbias 1 and riskfactor
|
|
|
|
* ~0 so risk simply operates as a simple hop counter. */
|
|
|
|
unvisited = dijkstra_prepare(tmpctx, rstate, src, msat,
|
|
|
|
shortest_cost_function);
|
|
|
|
SUPERVERBOSE("Running shortest path from %s -> %s",
|
|
|
|
type_to_string(tmpctx, struct node_id, &dst->id),
|
|
|
|
type_to_string(tmpctx, struct node_id, &src->id));
|
2019-05-31 09:30:33 +02:00
|
|
|
dijkstra(rstate, dst, NULL, riskfactor, 1, fuzz, base_seed,
|
2019-04-17 09:35:33 +02:00
|
|
|
unvisited, shortest_cost_function);
|
2019-04-17 09:35:33 +02:00
|
|
|
dijkstra_cleanup(unvisited);
|
|
|
|
|
2020-02-14 01:12:28 +01:00
|
|
|
/* This will usually succeed, since we found a route before; however
|
|
|
|
* it's possible that it fails in corner cases. Consider that the reduced
|
|
|
|
* riskfactor may make us select a more fee-expensive route, which then
|
|
|
|
* makes us unable to complete the route due to htlc_max restrictions. */
|
2019-05-31 09:30:33 +02:00
|
|
|
short_route = build_route(ctx, rstate, dst, src, me, riskfactor, 1,
|
2019-04-17 09:35:33 +02:00
|
|
|
fuzz, base_seed, fee);
|
2020-02-14 01:12:28 +01:00
|
|
|
if (!short_route) {
|
|
|
|
status_info("Could't find short enough route %s->%s",
|
|
|
|
type_to_string(tmpctx, struct node_id, &dst->id),
|
|
|
|
type_to_string(tmpctx, struct node_id, &src->id));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
if (!amount_msat_sub(&short_cost,
|
|
|
|
dst->dijkstra.total, src->dijkstra.total))
|
|
|
|
goto bad_total;
|
|
|
|
|
|
|
|
/* Still too long? Oh well. */
|
|
|
|
if (tal_count(short_route) > max_hops) {
|
|
|
|
status_info("Minimal possible route %s->%s is %zu",
|
|
|
|
type_to_string(tmpctx, struct node_id, &dst->id),
|
|
|
|
type_to_string(tmpctx, struct node_id, &src->id),
|
|
|
|
tal_count(short_route));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OK, so it's possible, just more expensive. */
|
|
|
|
min_bias = 0;
|
|
|
|
|
|
|
|
if (!amount_msat_sub(&cost_diff, short_cost, long_cost)) {
|
|
|
|
status_broken("Short cost %s < long cost %s?",
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&short_cost),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&long_cost));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a gross overestimate, but it works. */
|
|
|
|
max_bias = cost_diff.millisatoshis; /* Raw: bias calc */
|
|
|
|
|
|
|
|
SUPERVERBOSE("maxbias %"PRIu64" gave rlen %zu",
|
|
|
|
max_bias, tal_count(short_route));
|
|
|
|
|
|
|
|
/* Now, binary search */
|
|
|
|
while (min_bias < max_bias) {
|
|
|
|
struct chan **route;
|
|
|
|
struct amount_msat this_fee;
|
|
|
|
u64 riskbias = (min_bias + max_bias) / 2;
|
|
|
|
|
|
|
|
unvisited = dijkstra_prepare(tmpctx, rstate, src, msat,
|
|
|
|
normal_cost_function);
|
2019-05-31 09:30:33 +02:00
|
|
|
dijkstra(rstate, dst, me, riskfactor, riskbias, fuzz, base_seed,
|
2019-04-17 09:35:33 +02:00
|
|
|
unvisited, normal_cost_function);
|
2019-04-17 09:35:33 +02:00
|
|
|
dijkstra_cleanup(unvisited);
|
|
|
|
|
2019-05-31 09:30:33 +02:00
|
|
|
route = build_route(ctx, rstate, dst, src, me,
|
|
|
|
riskfactor, riskbias,
|
2019-04-17 09:35:33 +02:00
|
|
|
fuzz, base_seed, &this_fee);
|
2019-04-17 09:35:33 +02:00
|
|
|
|
|
|
|
SUPERVERBOSE("riskbias %"PRIu64" rlen %zu",
|
|
|
|
riskbias, tal_count(route));
|
|
|
|
/* Too long still? This is our new min_bias */
|
|
|
|
if (tal_count(route) > max_hops) {
|
|
|
|
tal_free(route);
|
|
|
|
min_bias = riskbias + 1;
|
|
|
|
} else {
|
|
|
|
/* This route is acceptable. */
|
|
|
|
tal_free(short_route);
|
|
|
|
short_route = route;
|
|
|
|
/* Save this fee in case we exit loop */
|
|
|
|
*fee = this_fee;
|
|
|
|
max_bias = riskbias;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return short_route;
|
|
|
|
|
|
|
|
bad_total:
|
|
|
|
status_broken("dst total %s < src total %s?",
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&dst->dijkstra.total),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&src->dijkstra.total));
|
|
|
|
out:
|
|
|
|
tal_free(short_route);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-18 05:15:41 +01:00
|
|
|
/* riskfactor is already scaled to per-block amount */
|
2018-03-04 03:26:59 +01:00
|
|
|
static struct chan **
|
2019-04-17 09:35:33 +02:00
|
|
|
find_route(const tal_t *ctx, struct routing_state *rstate,
|
|
|
|
const struct node_id *from, const struct node_id *to,
|
|
|
|
struct amount_msat msat,
|
|
|
|
double riskfactor,
|
|
|
|
double fuzz, const struct siphash_seed *base_seed,
|
2019-09-24 05:33:59 +02:00
|
|
|
u32 max_hops,
|
2019-04-17 09:35:33 +02:00
|
|
|
struct amount_msat *fee)
|
2019-04-17 09:35:33 +02:00
|
|
|
{
|
|
|
|
struct node *src, *dst;
|
2019-05-31 09:30:33 +02:00
|
|
|
const struct node *me;
|
2019-04-17 09:35:33 +02:00
|
|
|
struct unvisited *unvisited;
|
2019-04-17 09:35:33 +02:00
|
|
|
struct chan **route;
|
2019-04-17 09:35:33 +02:00
|
|
|
|
|
|
|
/* Note: we map backwards, since we know the amount of satoshi we want
|
|
|
|
* at the end, and need to derive how much we need to send. */
|
|
|
|
src = get_node(rstate, to);
|
|
|
|
|
2019-05-31 09:30:33 +02:00
|
|
|
/* If from is NULL, that's means it's us. */
|
|
|
|
if (!from)
|
|
|
|
me = dst = get_node(rstate, &rstate->local_id);
|
|
|
|
else {
|
|
|
|
dst = get_node(rstate, from);
|
|
|
|
me = NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
if (!src) {
|
|
|
|
status_info("find_route: cannot find %s",
|
|
|
|
type_to_string(tmpctx, struct node_id, to));
|
|
|
|
return NULL;
|
|
|
|
} else if (!dst) {
|
2019-05-31 09:30:33 +02:00
|
|
|
status_info("find_route: cannot find source (%s)",
|
2019-04-17 09:35:33 +02:00
|
|
|
type_to_string(tmpctx, struct node_id, to));
|
|
|
|
return NULL;
|
|
|
|
} else if (dst == src) {
|
|
|
|
status_info("find_route: this is %s, refusing to create empty route",
|
|
|
|
type_to_string(tmpctx, struct node_id, to));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
unvisited = dijkstra_prepare(tmpctx, rstate, src, msat,
|
|
|
|
normal_cost_function);
|
2019-05-31 09:30:33 +02:00
|
|
|
dijkstra(rstate, dst, me, riskfactor, 1, fuzz, base_seed,
|
2019-04-17 09:35:33 +02:00
|
|
|
unvisited, normal_cost_function);
|
2019-04-17 09:35:33 +02:00
|
|
|
dijkstra_cleanup(unvisited);
|
|
|
|
|
2019-05-31 09:30:33 +02:00
|
|
|
route = build_route(ctx, rstate, dst, src, me, riskfactor, 1,
|
2019-04-17 09:35:33 +02:00
|
|
|
fuzz, base_seed, fee);
|
2019-04-17 09:35:33 +02:00
|
|
|
if (tal_count(route) <= max_hops)
|
|
|
|
return route;
|
|
|
|
|
|
|
|
/* This is the far more unlikely case */
|
2019-05-31 09:30:33 +02:00
|
|
|
return find_shorter_route(ctx, rstate, src, dst, me, msat,
|
2019-04-17 09:35:33 +02:00
|
|
|
max_hops, fuzz, base_seed, route, fee);
|
2019-04-17 09:35:33 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
/* Checks that key is valid, and signed this hash */
|
|
|
|
static bool check_signed_hash_nodeid(const struct sha256_double *hash,
|
|
|
|
const secp256k1_ecdsa_signature *signature,
|
|
|
|
const struct node_id *id)
|
|
|
|
{
|
|
|
|
struct pubkey key;
|
|
|
|
|
|
|
|
return pubkey_from_node_id(&key, id)
|
|
|
|
&& check_signed_hash(hash, signature, &key);
|
|
|
|
}
|
|
|
|
|
2017-04-03 05:59:03 +02:00
|
|
|
/* Verify the signature of a channel_update message */
|
2018-03-08 05:10:33 +01:00
|
|
|
static u8 *check_channel_update(const tal_t *ctx,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *node_id,
|
2018-03-08 05:10:33 +01:00
|
|
|
const secp256k1_ecdsa_signature *node_sig,
|
|
|
|
const u8 *update)
|
2017-04-03 05:59:03 +02:00
|
|
|
{
|
|
|
|
/* 2 byte msg type + 64 byte signatures */
|
|
|
|
int offset = 66;
|
|
|
|
struct sha256_double hash;
|
2018-07-28 08:00:16 +02:00
|
|
|
sha256_double(&hash, update + offset, tal_count(update) - offset);
|
2017-04-03 05:59:03 +02:00
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
if (!check_signed_hash_nodeid(&hash, node_sig, node_id))
|
2018-03-08 05:10:33 +01:00
|
|
|
return towire_errorfmt(ctx, NULL,
|
|
|
|
"Bad signature for %s hash %s"
|
|
|
|
" on channel_update %s",
|
|
|
|
type_to_string(ctx,
|
|
|
|
secp256k1_ecdsa_signature,
|
|
|
|
node_sig),
|
|
|
|
type_to_string(ctx,
|
|
|
|
struct sha256_double,
|
|
|
|
&hash),
|
|
|
|
tal_hex(ctx, update));
|
|
|
|
return NULL;
|
2017-04-03 05:59:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 05:10:31 +01:00
|
|
|
static u8 *check_channel_announcement(const tal_t *ctx,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *node1_id, const struct node_id *node2_id,
|
2018-04-20 10:09:50 +02:00
|
|
|
const struct pubkey *bitcoin1_key, const struct pubkey *bitcoin2_key,
|
|
|
|
const secp256k1_ecdsa_signature *node1_sig,
|
|
|
|
const secp256k1_ecdsa_signature *node2_sig,
|
|
|
|
const secp256k1_ecdsa_signature *bitcoin1_sig,
|
|
|
|
const secp256k1_ecdsa_signature *bitcoin2_sig, const u8 *announcement)
|
2017-04-03 05:58:03 +02:00
|
|
|
{
|
|
|
|
/* 2 byte msg type + 256 byte signatures */
|
|
|
|
int offset = 258;
|
|
|
|
struct sha256_double hash;
|
|
|
|
sha256_double(&hash, announcement + offset,
|
2018-07-28 08:00:16 +02:00
|
|
|
tal_count(announcement) - offset);
|
2017-04-03 05:58:03 +02:00
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
if (!check_signed_hash_nodeid(&hash, node1_sig, node1_id)) {
|
2018-03-08 05:10:31 +01:00
|
|
|
return towire_errorfmt(ctx, NULL,
|
|
|
|
"Bad node_signature_1 %s hash %s"
|
2020-05-19 22:40:24 +02:00
|
|
|
" on channel_announcement %s",
|
2018-03-08 05:10:31 +01:00
|
|
|
type_to_string(ctx,
|
|
|
|
secp256k1_ecdsa_signature,
|
|
|
|
node1_sig),
|
|
|
|
type_to_string(ctx,
|
|
|
|
struct sha256_double,
|
|
|
|
&hash),
|
|
|
|
tal_hex(ctx, announcement));
|
|
|
|
}
|
2019-04-08 11:58:32 +02:00
|
|
|
if (!check_signed_hash_nodeid(&hash, node2_sig, node2_id)) {
|
2018-03-08 05:10:31 +01:00
|
|
|
return towire_errorfmt(ctx, NULL,
|
|
|
|
"Bad node_signature_2 %s hash %s"
|
2020-05-19 22:40:24 +02:00
|
|
|
" on channel_announcement %s",
|
2018-03-08 05:10:31 +01:00
|
|
|
type_to_string(ctx,
|
|
|
|
secp256k1_ecdsa_signature,
|
|
|
|
node2_sig),
|
|
|
|
type_to_string(ctx,
|
|
|
|
struct sha256_double,
|
|
|
|
&hash),
|
|
|
|
tal_hex(ctx, announcement));
|
|
|
|
}
|
|
|
|
if (!check_signed_hash(&hash, bitcoin1_sig, bitcoin1_key)) {
|
|
|
|
return towire_errorfmt(ctx, NULL,
|
|
|
|
"Bad bitcoin_signature_1 %s hash %s"
|
2020-05-19 22:40:24 +02:00
|
|
|
" on channel_announcement %s",
|
2018-03-08 05:10:31 +01:00
|
|
|
type_to_string(ctx,
|
|
|
|
secp256k1_ecdsa_signature,
|
|
|
|
bitcoin1_sig),
|
|
|
|
type_to_string(ctx,
|
|
|
|
struct sha256_double,
|
|
|
|
&hash),
|
|
|
|
tal_hex(ctx, announcement));
|
|
|
|
}
|
|
|
|
if (!check_signed_hash(&hash, bitcoin2_sig, bitcoin2_key)) {
|
|
|
|
return towire_errorfmt(ctx, NULL,
|
|
|
|
"Bad bitcoin_signature_2 %s hash %s"
|
2020-05-19 22:40:24 +02:00
|
|
|
" on channel_announcement %s",
|
2018-03-08 05:10:31 +01:00
|
|
|
type_to_string(ctx,
|
|
|
|
secp256k1_ecdsa_signature,
|
|
|
|
bitcoin2_sig),
|
|
|
|
type_to_string(ctx,
|
|
|
|
struct sha256_double,
|
|
|
|
&hash),
|
|
|
|
tal_hex(ctx, announcement));
|
|
|
|
}
|
|
|
|
return NULL;
|
2017-04-03 05:58:03 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
/* We allow node announcements for this node if it doesn't otherwise exist, so
|
|
|
|
* we can process them once it does exist (a channel_announce is being
|
|
|
|
* validated right now).
|
|
|
|
*
|
|
|
|
* If we attach one, remove it on destruction of @ctx.
|
|
|
|
*/
|
|
|
|
static void del_pending_node_announcement(const tal_t *ctx UNUSED,
|
|
|
|
struct pending_node_announce *pna)
|
|
|
|
{
|
|
|
|
if (--pna->refcount == 0) {
|
|
|
|
pending_node_map_del(pna->rstate->pending_node_map, pna);
|
|
|
|
tal_free(pna);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void catch_node_announcement(const tal_t *ctx,
|
|
|
|
struct routing_state *rstate,
|
|
|
|
struct node_id *nodeid)
|
2018-02-02 19:49:12 +01:00
|
|
|
{
|
2019-04-10 09:31:29 +02:00
|
|
|
struct pending_node_announce *pna;
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
/* No need if we already know about the node. We might, however, only
|
|
|
|
* know about it because it's a peer (maybe with private or
|
|
|
|
* not-yet-announced channels), so check for that too. */
|
|
|
|
node = get_node(rstate, nodeid);
|
|
|
|
if (node && node_has_public_channels(node))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* We can have multiple channels announced at same time for nodes;
|
|
|
|
* but we can only have one of these in the map. */
|
|
|
|
pna = pending_node_map_get(rstate->pending_node_map, nodeid);
|
|
|
|
if (!pna) {
|
|
|
|
pna = tal(rstate, struct pending_node_announce);
|
|
|
|
pna->rstate = rstate;
|
|
|
|
pna->nodeid = *nodeid;
|
|
|
|
pna->node_announcement = NULL;
|
|
|
|
pna->timestamp = 0;
|
2019-05-29 06:35:43 +02:00
|
|
|
pna->index = 0;
|
2019-04-10 09:31:29 +02:00
|
|
|
pna->refcount = 0;
|
2019-10-08 03:13:24 +02:00
|
|
|
pna->peer_softref = NULL;
|
2019-04-10 09:31:29 +02:00
|
|
|
pending_node_map_add(rstate->pending_node_map, pna);
|
|
|
|
}
|
|
|
|
pna->refcount++;
|
|
|
|
tal_add_destructor2(ctx, del_pending_node_announcement, pna);
|
2018-02-02 19:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void process_pending_node_announcement(struct routing_state *rstate,
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id *nodeid)
|
2018-02-02 19:49:12 +01:00
|
|
|
{
|
2018-07-04 07:29:56 +02:00
|
|
|
struct pending_node_announce *pna = pending_node_map_get(rstate->pending_node_map, nodeid);
|
2018-02-02 19:49:12 +01:00
|
|
|
if (!pna)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pna->node_announcement) {
|
2018-02-03 17:40:09 +01:00
|
|
|
SUPERVERBOSE(
|
|
|
|
"Processing deferred node_announcement for node %s",
|
2019-04-08 11:58:32 +02:00
|
|
|
type_to_string(pna, struct node_id, nodeid));
|
2018-03-08 05:10:29 +01:00
|
|
|
|
2019-09-22 03:29:01 +02:00
|
|
|
/* Can fail it timestamp is now too old */
|
2019-05-29 06:35:43 +02:00
|
|
|
if (!routing_add_node_announcement(rstate,
|
|
|
|
pna->node_announcement,
|
2019-10-08 03:13:24 +02:00
|
|
|
pna->index,
|
2019-10-08 03:32:24 +02:00
|
|
|
pna->peer_softref, NULL))
|
2019-09-22 03:29:01 +02:00
|
|
|
status_unusual("pending node_announcement %s too old?",
|
|
|
|
tal_hex(tmpctx, pna->node_announcement));
|
2019-06-13 02:47:36 +02:00
|
|
|
/* Never send this again. */
|
|
|
|
pna->node_announcement = tal_free(pna->node_announcement);
|
2018-02-02 19:49:12 +01:00
|
|
|
}
|
2019-06-13 02:47:36 +02:00
|
|
|
|
|
|
|
/* We don't need to catch any more node_announcements, since we've
|
|
|
|
* accepted the public channel now. But other pending announcements
|
|
|
|
* may still hold a reference they use in
|
|
|
|
* del_pending_node_announcement, so simply delete it from the map. */
|
2019-10-15 04:39:13 +02:00
|
|
|
pending_node_map_del(rstate->pending_node_map, notleak(pna));
|
2018-02-02 19:49:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 09:59:13 +01:00
|
|
|
static struct pending_cannouncement *
|
|
|
|
find_pending_cannouncement(struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *scid)
|
|
|
|
{
|
2019-04-08 17:31:59 +02:00
|
|
|
struct pending_cannouncement *pann;
|
2018-03-02 09:59:13 +01:00
|
|
|
|
2019-04-08 17:31:59 +02:00
|
|
|
pann = pending_cannouncement_map_get(&rstate->pending_cannouncements, scid);
|
|
|
|
|
|
|
|
return pann;
|
2018-03-02 09:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_pending_cannouncement(struct pending_cannouncement *pending,
|
|
|
|
struct routing_state *rstate)
|
|
|
|
{
|
2019-04-08 17:31:59 +02:00
|
|
|
pending_cannouncement_map_del(&rstate->pending_cannouncements, pending);
|
2018-03-02 09:59:13 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 06:15:25 +02:00
|
|
|
static bool is_local_channel(const struct routing_state *rstate,
|
|
|
|
const struct chan *chan)
|
|
|
|
{
|
2019-04-08 11:58:32 +02:00
|
|
|
return node_id_eq(&chan->nodes[0]->id, &rstate->local_id)
|
|
|
|
|| node_id_eq(&chan->nodes[1]->id, &rstate->local_id);
|
2018-06-04 06:15:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void add_channel_announce_to_broadcast(struct routing_state *rstate,
|
2018-06-04 06:24:25 +02:00
|
|
|
struct chan *chan,
|
2019-04-11 07:15:22 +02:00
|
|
|
const u8 *channel_announce,
|
2019-04-11 07:15:13 +02:00
|
|
|
u32 timestamp,
|
|
|
|
u32 index)
|
2018-06-04 06:15:25 +02:00
|
|
|
{
|
2019-05-16 21:55:17 +02:00
|
|
|
u8 *addendum = towire_gossip_store_channel_amount(tmpctx, chan->sat);
|
2019-11-04 01:37:05 +01:00
|
|
|
bool is_local = is_local_channel(rstate, chan);
|
2019-05-16 21:55:17 +02:00
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
chan->bcast.timestamp = timestamp;
|
2019-04-11 07:15:13 +02:00
|
|
|
/* 0, unless we're loading from store */
|
2019-06-03 20:22:25 +02:00
|
|
|
if (index)
|
|
|
|
chan->bcast.index = index;
|
|
|
|
else
|
|
|
|
chan->bcast.index = gossip_store_add(rstate->gs,
|
|
|
|
channel_announce,
|
|
|
|
chan->bcast.timestamp,
|
2019-11-04 01:37:05 +01:00
|
|
|
is_local,
|
2019-06-03 20:22:25 +02:00
|
|
|
addendum);
|
2019-11-04 01:37:05 +01:00
|
|
|
rstate->local_channel_announced |= is_local;
|
2018-06-04 06:15:25 +02:00
|
|
|
}
|
|
|
|
|
2018-04-11 01:03:35 +02:00
|
|
|
bool routing_add_channel_announcement(struct routing_state *rstate,
|
2019-02-21 04:45:55 +01:00
|
|
|
const u8 *msg TAKES,
|
2019-04-10 09:31:29 +02:00
|
|
|
struct amount_sat sat,
|
2019-10-08 03:13:24 +02:00
|
|
|
u32 index,
|
|
|
|
struct peer *peer)
|
2018-03-22 15:11:24 +01:00
|
|
|
{
|
|
|
|
struct chan *chan;
|
|
|
|
secp256k1_ecdsa_signature node_signature_1, node_signature_2;
|
|
|
|
secp256k1_ecdsa_signature bitcoin_signature_1, bitcoin_signature_2;
|
|
|
|
u8 *features;
|
|
|
|
struct bitcoin_blkid chain_hash;
|
|
|
|
struct short_channel_id scid;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id node_id_1;
|
|
|
|
struct node_id node_id_2;
|
2018-03-22 15:11:24 +01:00
|
|
|
struct pubkey bitcoin_key_1;
|
|
|
|
struct pubkey bitcoin_key_2;
|
2019-04-11 07:15:13 +02:00
|
|
|
struct unupdated_channel *uc;
|
|
|
|
const u8 *private_updates[2] = { NULL, NULL };
|
2018-04-11 01:03:35 +02:00
|
|
|
|
2019-04-10 09:31:18 +02:00
|
|
|
/* Make sure we own msg, even if we don't save it. */
|
|
|
|
if (taken(msg))
|
|
|
|
tal_steal(tmpctx, msg);
|
|
|
|
|
2018-05-17 07:08:11 +02:00
|
|
|
if (!fromwire_channel_announcement(
|
|
|
|
tmpctx, msg, &node_signature_1, &node_signature_2,
|
|
|
|
&bitcoin_signature_1, &bitcoin_signature_2, &features, &chain_hash,
|
2019-04-08 11:58:44 +02:00
|
|
|
&scid, &node_id_1, &node_id_2, &bitcoin_key_1, &bitcoin_key_2))
|
2018-05-17 07:08:11 +02:00
|
|
|
return false;
|
|
|
|
|
2018-03-22 15:11:24 +01:00
|
|
|
/* The channel may already exist if it was non-public from
|
|
|
|
* local_add_channel(); normally we don't accept new
|
|
|
|
* channel_announcements. See handle_channel_announcement. */
|
|
|
|
chan = get_channel(rstate, &scid);
|
|
|
|
|
2019-04-11 07:15:13 +02:00
|
|
|
/* private updates will exist in the store before the announce: we
|
|
|
|
* can't index those for broadcast since they would predate it, so we
|
2019-06-03 20:24:25 +02:00
|
|
|
* add fresh ones. */
|
|
|
|
if (chan) {
|
|
|
|
/* If this was in the gossip_store, gossip_store is bad! */
|
|
|
|
if (index) {
|
|
|
|
status_broken("gossip_store channel_announce"
|
|
|
|
" %u replaces %u!",
|
|
|
|
index, chan->bcast.index);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-11 07:16:30 +02:00
|
|
|
/* Reload any private updates */
|
|
|
|
if (chan->half[0].bcast.index)
|
|
|
|
private_updates[0]
|
2019-06-03 20:05:25 +02:00
|
|
|
= gossip_store_get_private_update(NULL,
|
2019-06-03 20:22:25 +02:00
|
|
|
rstate->gs,
|
2019-04-11 07:16:30 +02:00
|
|
|
chan->half[0].bcast.index);
|
|
|
|
if (chan->half[1].bcast.index)
|
|
|
|
private_updates[1]
|
2019-06-03 20:05:25 +02:00
|
|
|
= gossip_store_get_private_update(NULL,
|
2019-06-03 20:22:25 +02:00
|
|
|
rstate->gs,
|
2019-04-11 07:16:30 +02:00
|
|
|
chan->half[1].bcast.index);
|
2019-04-11 07:15:13 +02:00
|
|
|
|
2019-06-03 20:09:25 +02:00
|
|
|
remove_channel_from_store(rstate, chan);
|
2019-05-21 09:13:28 +02:00
|
|
|
free_chan(rstate, chan);
|
2019-06-03 20:09:25 +02:00
|
|
|
}
|
2019-04-11 07:15:13 +02:00
|
|
|
|
|
|
|
uc = tal(rstate, struct unupdated_channel);
|
2020-02-27 03:17:01 +01:00
|
|
|
uc->channel_announce = tal_dup_talarr(uc, u8, msg);
|
2020-05-04 02:18:34 +02:00
|
|
|
uc->features = tal_steal(uc, features);
|
2019-09-12 02:22:12 +02:00
|
|
|
uc->added = gossip_time_now(rstate);
|
2019-04-11 07:15:13 +02:00
|
|
|
uc->index = index;
|
|
|
|
uc->sat = sat;
|
|
|
|
uc->scid = scid;
|
|
|
|
uc->id[0] = node_id_1;
|
|
|
|
uc->id[1] = node_id_2;
|
2019-10-08 03:13:24 +02:00
|
|
|
set_softref(uc, &uc->peer_softref, peer);
|
2019-04-11 07:15:13 +02:00
|
|
|
uintmap_add(&rstate->unupdated_chanmap, scid.u64, uc);
|
|
|
|
tal_add_destructor2(uc, destroy_unupdated_channel, rstate);
|
|
|
|
|
|
|
|
/* If a node_announcement comes along, save it for once we're updated */
|
|
|
|
catch_node_announcement(uc, rstate, &node_id_1);
|
|
|
|
catch_node_announcement(uc, rstate, &node_id_2);
|
|
|
|
|
|
|
|
/* If we had private updates, they'll immediately create the channel. */
|
|
|
|
if (private_updates[0])
|
2019-10-08 03:13:24 +02:00
|
|
|
routing_add_channel_update(rstate, take(private_updates[0]), 0,
|
|
|
|
peer);
|
2019-04-11 07:15:13 +02:00
|
|
|
if (private_updates[1])
|
2019-10-08 03:13:24 +02:00
|
|
|
routing_add_channel_update(rstate, take(private_updates[1]), 0,
|
|
|
|
peer);
|
2019-04-10 09:31:29 +02:00
|
|
|
|
2018-04-11 01:03:35 +02:00
|
|
|
return true;
|
2018-03-22 15:11:24 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 05:10:31 +01:00
|
|
|
u8 *handle_channel_announcement(struct routing_state *rstate,
|
|
|
|
const u8 *announce TAKES,
|
2019-09-22 04:21:19 +02:00
|
|
|
u32 current_blockheight,
|
2019-10-08 03:13:24 +02:00
|
|
|
const struct short_channel_id **scid,
|
|
|
|
struct peer *peer)
|
2017-02-01 15:09:26 +01:00
|
|
|
{
|
2018-01-04 12:40:58 +01:00
|
|
|
struct pending_cannouncement *pending;
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid chain_hash;
|
2018-03-08 05:10:31 +01:00
|
|
|
u8 *features, *err;
|
2018-01-04 12:40:58 +01:00
|
|
|
secp256k1_ecdsa_signature node_signature_1, node_signature_2;
|
|
|
|
secp256k1_ecdsa_signature bitcoin_signature_1, bitcoin_signature_2;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
2017-02-01 15:09:26 +01:00
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
pending = tal(rstate, struct pending_cannouncement);
|
2019-10-08 03:13:24 +02:00
|
|
|
set_softref(pending, &pending->peer_softref, peer);
|
2018-01-12 18:55:29 +01:00
|
|
|
pending->updates[0] = NULL;
|
|
|
|
pending->updates[1] = NULL;
|
2019-10-08 03:13:24 +02:00
|
|
|
pending->update_peer_softref[0] = pending->update_peer_softref[1] = NULL;
|
2020-02-27 03:17:01 +01:00
|
|
|
pending->announce = tal_dup_talarr(pending, u8, announce);
|
2018-02-02 14:55:54 +01:00
|
|
|
pending->update_timestamps[0] = pending->update_timestamps[1] = 0;
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_channel_announcement(pending, pending->announce,
|
2018-01-04 12:40:58 +01:00
|
|
|
&node_signature_1,
|
|
|
|
&node_signature_2,
|
2017-02-01 15:09:26 +01:00
|
|
|
&bitcoin_signature_1,
|
|
|
|
&bitcoin_signature_2,
|
2017-08-03 03:45:04 +02:00
|
|
|
&features,
|
2017-08-22 07:25:01 +02:00
|
|
|
&chain_hash,
|
2018-01-04 12:40:58 +01:00
|
|
|
&pending->short_channel_id,
|
2019-04-08 11:58:44 +02:00
|
|
|
&pending->node_id_1,
|
|
|
|
&pending->node_id_2,
|
2018-01-04 12:40:58 +01:00
|
|
|
&pending->bitcoin_key_1,
|
|
|
|
&pending->bitcoin_key_2)) {
|
2018-03-08 05:10:31 +01:00
|
|
|
err = towire_errorfmt(rstate, NULL,
|
|
|
|
"Malformed channel_announcement %s",
|
|
|
|
tal_hex(pending, pending->announce));
|
2018-03-13 00:06:00 +01:00
|
|
|
goto malformed;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 01:18:43 +01:00
|
|
|
/* We don't use features */
|
|
|
|
tal_free(features);
|
|
|
|
|
2019-09-22 04:21:19 +02:00
|
|
|
/* If we know the blockheight, and it's in the future, reject
|
|
|
|
* out-of-hand. Remember, it should be 6 deep before they tell us
|
|
|
|
* anyway. */
|
|
|
|
if (current_blockheight != 0
|
|
|
|
&& short_channel_id_blocknum(&pending->short_channel_id) > current_blockheight) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Ignoring future channel_announcment for %s"
|
|
|
|
" (current block %u)",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&pending->short_channel_id),
|
|
|
|
current_blockheight);
|
2019-09-22 04:21:19 +02:00
|
|
|
goto ignored;
|
|
|
|
}
|
|
|
|
|
2019-03-27 15:40:25 +01:00
|
|
|
/* If a prior txout lookup failed there is little point it trying
|
2019-11-06 03:16:09 +01:00
|
|
|
* again. Just drop the announcement and walk away whistling. */
|
|
|
|
if (in_txout_failures(rstate, &pending->short_channel_id)) {
|
2019-03-27 15:40:25 +01:00
|
|
|
SUPERVERBOSE(
|
|
|
|
"Ignoring channel_announcement of %s due to a prior txout "
|
|
|
|
"query failure. The channel was likely closed on-chain.",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&pending->short_channel_id));
|
|
|
|
goto ignored;
|
|
|
|
}
|
|
|
|
|
2018-01-30 20:27:14 +01:00
|
|
|
/* Check if we know the channel already (no matter in what
|
|
|
|
* state, we stop here if yes). */
|
2018-03-01 10:22:28 +01:00
|
|
|
chan = get_channel(rstate, &pending->short_channel_id);
|
2018-05-10 16:00:38 +02:00
|
|
|
if (chan != NULL && is_chan_public(chan)) {
|
2018-03-02 09:59:13 +01:00
|
|
|
SUPERVERBOSE("%s: %s already has public channel",
|
|
|
|
__func__,
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
2018-03-02 09:59:13 +01:00
|
|
|
&pending->short_channel_id));
|
2018-03-13 00:06:00 +01:00
|
|
|
goto ignored;
|
2018-01-30 20:27:14 +01:00
|
|
|
}
|
2019-04-11 07:15:13 +02:00
|
|
|
if (get_unupdated_channel(rstate, &pending->short_channel_id)) {
|
|
|
|
SUPERVERBOSE("%s: %s already has unupdated channel",
|
|
|
|
__func__,
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&pending->short_channel_id));
|
|
|
|
goto ignored;
|
|
|
|
}
|
2018-03-02 09:59:13 +01:00
|
|
|
|
|
|
|
/* We don't replace previous ones, since we might validate that and
|
|
|
|
* think this one is OK! */
|
|
|
|
if (find_pending_cannouncement(rstate, &pending->short_channel_id)) {
|
|
|
|
SUPERVERBOSE("%s: %s already has pending cannouncement",
|
|
|
|
__func__,
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
2018-03-02 09:59:13 +01:00
|
|
|
&pending->short_channel_id));
|
2018-03-13 00:06:00 +01:00
|
|
|
goto ignored;
|
2018-03-02 09:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Handle duplicates as per BOLT #7 */
|
2018-01-30 20:27:14 +01:00
|
|
|
|
2017-08-22 07:25:01 +02:00
|
|
|
/* BOLT #7:
|
2019-01-14 03:22:05 +01:00
|
|
|
* The receiving node:
|
2018-06-17 12:13:44 +02:00
|
|
|
*...
|
|
|
|
* - if the specified `chain_hash` is unknown to the receiver:
|
|
|
|
* - MUST ignore the message.
|
2017-08-22 07:25:01 +02:00
|
|
|
*/
|
2019-11-20 02:54:47 +01:00
|
|
|
if (!bitcoin_blkid_eq(&chain_hash, &chainparams->genesis_blockhash)) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
2017-12-21 10:53:37 +01:00
|
|
|
"Received channel_announcement %s for unknown chain %s",
|
2018-02-01 03:41:19 +01:00
|
|
|
type_to_string(pending, struct short_channel_id,
|
|
|
|
&pending->short_channel_id),
|
2018-01-04 12:40:58 +01:00
|
|
|
type_to_string(pending, struct bitcoin_blkid, &chain_hash));
|
2018-03-13 00:06:00 +01:00
|
|
|
goto ignored;
|
2017-08-22 07:25:01 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 11:58:44 +02:00
|
|
|
/* Note that if node_id_1 or node_id_2 are malformed, it's caught here */
|
2018-03-08 05:10:31 +01:00
|
|
|
err = check_channel_announcement(rstate,
|
|
|
|
&pending->node_id_1,
|
|
|
|
&pending->node_id_2,
|
|
|
|
&pending->bitcoin_key_1,
|
|
|
|
&pending->bitcoin_key_2,
|
|
|
|
&node_signature_1,
|
|
|
|
&node_signature_2,
|
|
|
|
&bitcoin_signature_1,
|
|
|
|
&bitcoin_signature_2,
|
|
|
|
pending->announce);
|
|
|
|
if (err) {
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - if `bitcoin_signature_1`, `bitcoin_signature_2`,
|
|
|
|
* `node_signature_1` OR `node_signature_2` are invalid OR NOT
|
|
|
|
* correct:
|
|
|
|
* - SHOULD fail the connection.
|
|
|
|
*/
|
2018-03-13 00:06:00 +01:00
|
|
|
goto malformed;
|
2018-01-04 12:40:58 +01:00
|
|
|
}
|
2017-12-21 10:53:37 +01:00
|
|
|
|
2019-09-22 04:21:34 +02:00
|
|
|
/* Don't add an infinite number of pending announcements. If we're
|
|
|
|
* catching up with the bitcoin chain, though, they can definitely
|
|
|
|
* pile up. */
|
|
|
|
if (pending_cannouncement_map_count(&rstate->pending_cannouncements)
|
|
|
|
> 100000) {
|
|
|
|
static bool warned = false;
|
|
|
|
if (!warned) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_unusual(peer ? &peer->id : NULL,
|
|
|
|
"Flooded by channel_announcements:"
|
|
|
|
" ignoring some");
|
2019-09-22 04:21:34 +02:00
|
|
|
warned = true;
|
|
|
|
}
|
|
|
|
goto ignored;
|
|
|
|
}
|
|
|
|
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Received channel_announcement for channel %s",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&pending->short_channel_id));
|
2017-11-24 15:47:14 +01:00
|
|
|
|
2018-02-02 19:49:12 +01:00
|
|
|
/* Add both endpoints to the pending_node_map so we can stash
|
|
|
|
* node_announcements while we wait for the txout check */
|
2019-04-10 09:31:29 +02:00
|
|
|
catch_node_announcement(pending, rstate, &pending->node_id_1);
|
|
|
|
catch_node_announcement(pending, rstate, &pending->node_id_2);
|
2018-02-02 19:49:12 +01:00
|
|
|
|
2019-04-08 17:31:59 +02:00
|
|
|
pending_cannouncement_map_add(&rstate->pending_cannouncements, pending);
|
2018-03-02 09:59:13 +01:00
|
|
|
tal_add_destructor2(pending, destroy_pending_cannouncement, rstate);
|
|
|
|
|
2018-03-13 00:06:00 +01:00
|
|
|
/* Success */
|
2019-02-10 22:18:17 +01:00
|
|
|
// MSC: Cppcheck 1.86 gets this false positive
|
|
|
|
// cppcheck-suppress autoVariables
|
2018-03-08 05:10:31 +01:00
|
|
|
*scid = &pending->short_channel_id;
|
|
|
|
return NULL;
|
2018-03-13 00:06:00 +01:00
|
|
|
|
|
|
|
malformed:
|
|
|
|
tal_free(pending);
|
|
|
|
*scid = NULL;
|
|
|
|
return err;
|
|
|
|
|
|
|
|
ignored:
|
|
|
|
tal_free(pending);
|
|
|
|
*scid = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-08 03:13:24 +02:00
|
|
|
static void process_pending_channel_update(struct daemon *daemon,
|
|
|
|
struct routing_state *rstate,
|
2018-03-13 00:06:00 +01:00
|
|
|
const struct short_channel_id *scid,
|
2019-10-08 03:13:24 +02:00
|
|
|
const u8 *cupdate,
|
|
|
|
struct peer *peer)
|
2018-03-13 00:06:00 +01:00
|
|
|
{
|
|
|
|
u8 *err;
|
|
|
|
|
|
|
|
if (!cupdate)
|
|
|
|
return;
|
|
|
|
|
2019-10-08 03:14:24 +02:00
|
|
|
err = handle_channel_update(rstate, cupdate, peer, NULL);
|
2018-03-13 00:06:00 +01:00
|
|
|
if (err) {
|
2019-10-08 03:14:24 +02:00
|
|
|
/* FIXME: We could send this error back to peer if != NULL */
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Pending channel_update for %s: %s",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
scid),
|
|
|
|
sanitize_error(tmpctx, err, NULL));
|
2018-03-13 00:06:00 +01:00
|
|
|
tal_free(err);
|
|
|
|
}
|
2018-01-04 12:40:58 +01:00
|
|
|
}
|
2017-12-02 23:38:43 +01:00
|
|
|
|
2019-10-08 03:13:24 +02:00
|
|
|
bool handle_pending_cannouncement(struct daemon *daemon,
|
|
|
|
struct routing_state *rstate,
|
2018-01-04 12:40:58 +01:00
|
|
|
const struct short_channel_id *scid,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat sat,
|
2018-01-04 12:40:58 +01:00
|
|
|
const u8 *outscript)
|
|
|
|
{
|
|
|
|
const u8 *s;
|
|
|
|
struct pending_cannouncement *pending;
|
2019-11-18 01:26:17 +01:00
|
|
|
const struct node_id *src;
|
2018-01-31 17:53:50 +01:00
|
|
|
|
2018-03-02 09:59:13 +01:00
|
|
|
pending = find_pending_cannouncement(rstate, scid);
|
2018-02-27 21:00:45 +01:00
|
|
|
if (!pending)
|
2019-06-12 01:27:07 +02:00
|
|
|
return false;
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2019-11-18 01:26:17 +01:00
|
|
|
src = pending->peer_softref ? &pending->peer_softref->id : NULL;
|
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2019-01-14 03:22:05 +01:00
|
|
|
* The receiving node:
|
2018-06-17 12:13:44 +02:00
|
|
|
*...
|
|
|
|
* - if the `short_channel_id`'s output... is spent:
|
|
|
|
* - MUST ignore the message.
|
2018-01-04 12:40:58 +01:00
|
|
|
*/
|
2018-07-28 08:00:16 +02:00
|
|
|
if (tal_count(outscript) == 0) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(src,
|
|
|
|
"channel_announcement: no unspent txout %s",
|
|
|
|
type_to_string(pending,
|
|
|
|
struct short_channel_id,
|
|
|
|
scid));
|
2018-01-04 12:40:58 +01:00
|
|
|
tal_free(pending);
|
2019-09-27 02:04:34 +02:00
|
|
|
add_to_txout_failures(rstate, scid);
|
2019-06-12 01:27:07 +02:00
|
|
|
return false;
|
2018-01-04 12:40:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2019-01-14 03:22:05 +01:00
|
|
|
* The receiving node:
|
2018-06-17 12:13:44 +02:00
|
|
|
*...
|
|
|
|
* - if the `short_channel_id`'s output does NOT correspond to a P2WSH
|
|
|
|
* (using `bitcoin_key_1` and `bitcoin_key_2`, as specified in
|
|
|
|
* [BOLT #3](03-transactions.md#funding-transaction-output)) ...
|
|
|
|
* - MUST ignore the message.
|
2018-01-04 12:40:58 +01:00
|
|
|
*/
|
|
|
|
s = scriptpubkey_p2wsh(pending,
|
|
|
|
bitcoin_redeem_2of2(pending,
|
|
|
|
&pending->bitcoin_key_1,
|
|
|
|
&pending->bitcoin_key_2));
|
|
|
|
|
|
|
|
if (!scripteq(s, outscript)) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(src,
|
|
|
|
"channel_announcement: txout %s expected %s, got %s",
|
|
|
|
type_to_string(
|
|
|
|
pending, struct short_channel_id,
|
|
|
|
scid),
|
|
|
|
tal_hex(tmpctx, s),
|
|
|
|
tal_hex(tmpctx, outscript));
|
2018-01-04 12:40:58 +01:00
|
|
|
tal_free(pending);
|
2019-06-12 01:27:07 +02:00
|
|
|
return false;
|
2017-04-03 05:58:03 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
/* Remove pending now, so below functions don't see it. */
|
2019-04-08 17:31:59 +02:00
|
|
|
pending_cannouncement_map_del(&rstate->pending_cannouncements, pending);
|
2019-04-10 09:31:29 +02:00
|
|
|
tal_del_destructor2(pending, destroy_pending_cannouncement, rstate);
|
|
|
|
|
2019-09-22 03:29:01 +02:00
|
|
|
/* Can fail if channel_announcement too old */
|
2019-10-08 03:13:24 +02:00
|
|
|
if (!routing_add_channel_announcement(rstate, pending->announce, sat, 0,
|
|
|
|
pending->peer_softref))
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_unusual(src,
|
|
|
|
"Could not add channel_announcement %s: too old?",
|
|
|
|
tal_hex(tmpctx, pending->announce));
|
2019-09-22 03:29:01 +02:00
|
|
|
else {
|
|
|
|
/* Did we have an update waiting? If so, apply now. */
|
2019-10-08 03:13:24 +02:00
|
|
|
process_pending_channel_update(daemon, rstate, scid, pending->updates[0],
|
|
|
|
pending->update_peer_softref[0]);
|
|
|
|
process_pending_channel_update(daemon, rstate, scid, pending->updates[1],
|
|
|
|
pending->update_peer_softref[1]);
|
2019-09-22 03:29:01 +02:00
|
|
|
}
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
tal_free(pending);
|
2019-06-12 01:27:07 +02:00
|
|
|
return true;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 09:59:13 +01:00
|
|
|
static void update_pending(struct pending_cannouncement *pending,
|
2018-02-27 20:58:20 +01:00
|
|
|
u32 timestamp, const u8 *update,
|
2019-10-15 01:59:44 +02:00
|
|
|
const u8 direction,
|
|
|
|
struct peer *peer)
|
2018-01-04 12:40:58 +01:00
|
|
|
{
|
2019-01-15 05:11:27 +01:00
|
|
|
SUPERVERBOSE("Deferring update for pending channel %s/%d",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
2018-03-02 09:59:13 +01:00
|
|
|
&pending->short_channel_id), direction);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2018-05-21 06:36:55 +02:00
|
|
|
if (pending->update_timestamps[direction] < timestamp) {
|
2018-02-02 14:55:54 +01:00
|
|
|
if (pending->updates[direction]) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Replacing existing update");
|
2018-02-02 14:55:54 +01:00
|
|
|
tal_free(pending->updates[direction]);
|
|
|
|
}
|
2020-02-27 03:17:01 +01:00
|
|
|
pending->updates[direction]
|
|
|
|
= tal_dup_talarr(pending, u8, update);
|
2018-02-02 14:55:54 +01:00
|
|
|
pending->update_timestamps[direction] = timestamp;
|
2019-10-15 01:59:44 +02:00
|
|
|
clear_softref(pending, &pending->update_peer_softref[direction]);
|
|
|
|
set_softref(pending, &pending->update_peer_softref[direction],
|
|
|
|
peer);
|
2018-01-12 18:55:29 +01:00
|
|
|
}
|
2018-01-04 12:40:58 +01:00
|
|
|
}
|
|
|
|
|
2018-05-10 16:00:05 +02:00
|
|
|
static void set_connection_values(struct chan *chan,
|
|
|
|
int idx,
|
|
|
|
u32 base_fee,
|
|
|
|
u32 proportional_fee,
|
|
|
|
u32 delay,
|
2018-09-20 02:59:46 +02:00
|
|
|
u8 message_flags,
|
|
|
|
u8 channel_flags,
|
2019-04-10 00:47:20 +02:00
|
|
|
u32 timestamp,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat htlc_minimum,
|
|
|
|
struct amount_msat htlc_maximum)
|
2018-03-02 09:59:17 +01:00
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
struct half_chan *c = &chan->half[idx];
|
2018-03-02 09:59:17 +01:00
|
|
|
|
|
|
|
c->delay = delay;
|
2019-02-21 04:45:55 +01:00
|
|
|
c->htlc_minimum = htlc_minimum;
|
|
|
|
c->htlc_maximum = htlc_maximum;
|
2018-03-02 09:59:17 +01:00
|
|
|
c->base_fee = base_fee;
|
|
|
|
c->proportional_fee = proportional_fee;
|
2018-09-20 02:59:46 +02:00
|
|
|
c->message_flags = message_flags;
|
|
|
|
c->channel_flags = channel_flags;
|
2019-04-10 09:31:29 +02:00
|
|
|
c->bcast.timestamp = timestamp;
|
2018-09-20 02:59:46 +02:00
|
|
|
assert((c->channel_flags & ROUTING_FLAGS_DIRECTION) == idx);
|
2018-03-02 09:59:17 +01:00
|
|
|
|
2019-01-15 05:11:27 +01:00
|
|
|
SUPERVERBOSE("Channel %s/%d was updated.",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct short_channel_id, &chan->scid),
|
2018-03-02 09:59:17 +01:00
|
|
|
idx);
|
|
|
|
}
|
|
|
|
|
2018-04-11 01:03:35 +02:00
|
|
|
bool routing_add_channel_update(struct routing_state *rstate,
|
2019-04-10 09:31:29 +02:00
|
|
|
const u8 *update TAKES,
|
2019-10-08 03:13:24 +02:00
|
|
|
u32 index,
|
|
|
|
struct peer *peer)
|
2018-03-22 15:11:24 +01:00
|
|
|
{
|
|
|
|
secp256k1_ecdsa_signature signature;
|
|
|
|
struct short_channel_id short_channel_id;
|
|
|
|
u32 timestamp;
|
2018-09-20 02:59:46 +02:00
|
|
|
u8 message_flags, channel_flags;
|
2018-03-22 15:11:24 +01:00
|
|
|
u16 expiry;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat htlc_minimum, htlc_maximum;
|
2018-03-22 15:11:24 +01:00
|
|
|
u32 fee_base_msat;
|
|
|
|
u32 fee_proportional_millionths;
|
|
|
|
struct bitcoin_blkid chain_hash;
|
|
|
|
struct chan *chan;
|
2019-04-10 09:31:29 +02:00
|
|
|
struct half_chan *hc;
|
2019-04-11 07:15:13 +02:00
|
|
|
struct unupdated_channel *uc;
|
2018-03-22 15:11:24 +01:00
|
|
|
u8 direction;
|
2019-04-11 07:15:13 +02:00
|
|
|
struct amount_sat sat;
|
2018-03-22 15:11:24 +01:00
|
|
|
|
2019-04-10 09:31:18 +02:00
|
|
|
/* Make sure we own msg, even if we don't save it. */
|
|
|
|
if (taken(update))
|
|
|
|
tal_steal(tmpctx, update);
|
|
|
|
|
2018-10-04 22:34:53 +02:00
|
|
|
if (!fromwire_channel_update(update, &signature, &chain_hash,
|
2018-09-20 02:59:46 +02:00
|
|
|
&short_channel_id, ×tamp,
|
|
|
|
&message_flags, &channel_flags,
|
2019-02-21 04:45:55 +01:00
|
|
|
&expiry, &htlc_minimum, &fee_base_msat,
|
2018-10-04 22:34:53 +02:00
|
|
|
&fee_proportional_millionths))
|
|
|
|
return false;
|
|
|
|
/* If it's flagged as containing the optional field, reparse for
|
|
|
|
* the optional field */
|
|
|
|
if ((message_flags & ROUTING_OPT_HTLC_MAX_MSAT) &&
|
|
|
|
!fromwire_channel_update_option_channel_htlc_max(
|
|
|
|
update, &signature, &chain_hash,
|
|
|
|
&short_channel_id, ×tamp,
|
|
|
|
&message_flags, &channel_flags,
|
2019-02-21 04:45:55 +01:00
|
|
|
&expiry, &htlc_minimum, &fee_base_msat,
|
2018-10-04 22:34:53 +02:00
|
|
|
&fee_proportional_millionths,
|
2019-02-21 04:45:55 +01:00
|
|
|
&htlc_maximum))
|
2018-04-11 01:03:35 +02:00
|
|
|
return false;
|
2019-04-10 09:31:29 +02:00
|
|
|
|
|
|
|
direction = channel_flags & 0x1;
|
2018-03-22 15:11:24 +01:00
|
|
|
chan = get_channel(rstate, &short_channel_id);
|
2019-04-11 07:15:13 +02:00
|
|
|
|
|
|
|
if (chan) {
|
|
|
|
uc = NULL;
|
|
|
|
sat = chan->sat;
|
|
|
|
} else {
|
|
|
|
/* Maybe announcement was waiting for this update? */
|
|
|
|
uc = get_unupdated_channel(rstate, &short_channel_id);
|
|
|
|
if (!uc) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sat = uc->sat;
|
|
|
|
}
|
2018-04-11 01:03:35 +02:00
|
|
|
|
2018-10-05 21:49:09 +02:00
|
|
|
if (message_flags & ROUTING_OPT_HTLC_MAX_MSAT) {
|
|
|
|
/* Reject update if the `htlc_maximum_msat` is greater
|
|
|
|
* than the total available channel satoshis */
|
2019-04-11 07:15:13 +02:00
|
|
|
if (amount_msat_greater_sat(htlc_maximum, sat))
|
2018-10-05 21:49:09 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
/* If not indicated, set htlc_max_msat to channel capacity */
|
2019-04-11 07:15:13 +02:00
|
|
|
if (!amount_sat_to_msat(&htlc_maximum, sat)) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_broken(peer ? &peer->id : NULL,
|
|
|
|
"Channel capacity %s overflows!",
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
|
|
|
&sat));
|
2019-02-21 04:45:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-05 21:49:09 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 03:04:56 +02:00
|
|
|
/* Check timestamp is sane (unless from store). */
|
|
|
|
if (!index && !timestamp_reasonable(rstate, timestamp)) {
|
2020-08-10 15:19:46 +02:00
|
|
|
SUPERVERBOSE("Ignoring update timestamp %u for %s/%u",
|
|
|
|
timestamp,
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
direction);
|
2019-09-18 03:04:56 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-11 07:15:13 +02:00
|
|
|
/* OK, we're going to accept this, so create chan if doesn't exist */
|
|
|
|
if (uc) {
|
|
|
|
assert(!chan);
|
|
|
|
chan = new_chan(rstate, &short_channel_id,
|
2020-05-04 02:18:34 +02:00
|
|
|
&uc->id[0], &uc->id[1], sat, uc->features);
|
2019-04-11 07:15:13 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
/* Discard older updates */
|
|
|
|
hc = &chan->half[direction];
|
2019-06-03 20:24:25 +02:00
|
|
|
|
2019-09-16 12:44:00 +02:00
|
|
|
if (is_halfchan_defined(hc)) {
|
|
|
|
/* If we're loading from store, duplicate entries are a bug. */
|
|
|
|
if (index != 0) {
|
|
|
|
status_broken("gossip_store channel_update %u replaces %u!",
|
|
|
|
index, hc->bcast.index);
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-03 20:24:25 +02:00
|
|
|
|
2019-09-16 12:44:00 +02:00
|
|
|
if (timestamp <= hc->bcast.timestamp) {
|
|
|
|
SUPERVERBOSE("Ignoring outdated update.");
|
|
|
|
/* Ignoring != failing */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow redundant updates once every 7 days */
|
2019-09-26 04:00:20 +02:00
|
|
|
if (timestamp < hc->bcast.timestamp + GOSSIP_PRUNE_INTERVAL(rstate->dev_fast_gossip_prune) / 2
|
2019-09-16 12:44:00 +02:00
|
|
|
&& !cupdate_different(rstate->gs, hc, update)) {
|
2020-08-10 15:19:46 +02:00
|
|
|
SUPERVERBOSE("Ignoring redundant update for %s/%u"
|
|
|
|
" (last %u, now %u)",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
direction, hc->bcast.timestamp, timestamp);
|
2019-09-16 12:44:00 +02:00
|
|
|
/* Ignoring != failing */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure it's not spamming us. */
|
2019-09-18 03:05:10 +02:00
|
|
|
if (!ratelimit(rstate,
|
|
|
|
&hc->tokens, hc->bcast.timestamp, timestamp)) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Ignoring spammy update for %s/%u"
|
|
|
|
" (last %u, now %u)",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
direction,
|
|
|
|
hc->bcast.timestamp, timestamp);
|
2019-09-16 12:44:00 +02:00
|
|
|
/* Ignoring != failing */
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-10 09:31:29 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 11:36:22 +01:00
|
|
|
/* FIXME: https://github.com/lightningnetwork/lightning-rfc/pull/512
|
|
|
|
* says we MUST NOT exceed 2^32-1, but c-lightning did, so just trim
|
|
|
|
* rather than rejecting. */
|
2019-11-20 02:54:47 +01:00
|
|
|
if (amount_msat_greater(htlc_maximum, chainparams->max_payment))
|
|
|
|
htlc_maximum = chainparams->max_payment;
|
2018-11-21 11:36:22 +01:00
|
|
|
|
2018-03-22 15:11:24 +01:00
|
|
|
set_connection_values(chan, direction, fee_base_msat,
|
|
|
|
fee_proportional_millionths, expiry,
|
2018-09-20 02:59:46 +02:00
|
|
|
message_flags, channel_flags,
|
2019-02-21 04:45:55 +01:00
|
|
|
timestamp, htlc_minimum, htlc_maximum);
|
2018-03-22 15:11:24 +01:00
|
|
|
|
2019-06-03 20:09:25 +02:00
|
|
|
/* Safe even if was never added, but if it's a private channel it
|
|
|
|
* would be a WIRE_GOSSIP_STORE_PRIVATE_UPDATE. */
|
2019-06-03 20:23:25 +02:00
|
|
|
gossip_store_delete(rstate->gs, &hc->bcast,
|
2019-06-03 20:09:25 +02:00
|
|
|
is_chan_public(chan)
|
|
|
|
? WIRE_CHANNEL_UPDATE
|
|
|
|
: WIRE_GOSSIP_STORE_PRIVATE_UPDATE);
|
2019-04-10 09:31:29 +02:00
|
|
|
|
2019-04-11 07:15:13 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
* - MUST consider the `timestamp` of the `channel_announcement` to be
|
|
|
|
* the `timestamp` of a corresponding `channel_update`.
|
|
|
|
* - MUST consider whether to send the `channel_announcement` after
|
|
|
|
* receiving the first corresponding `channel_update`.
|
|
|
|
*/
|
|
|
|
if (uc) {
|
2019-04-11 07:15:22 +02:00
|
|
|
add_channel_announce_to_broadcast(rstate, chan,
|
|
|
|
uc->channel_announce,
|
|
|
|
timestamp,
|
2019-04-11 07:15:13 +02:00
|
|
|
uc->index);
|
2019-04-11 07:15:22 +02:00
|
|
|
} else if (!is_chan_public(chan)) {
|
2019-04-11 07:15:13 +02:00
|
|
|
/* For private channels, we get updates without an announce: don't
|
|
|
|
* broadcast them! But save local ones to store anyway. */
|
2019-04-11 07:15:22 +02:00
|
|
|
assert(is_local_channel(rstate, chan));
|
2019-06-03 20:23:25 +02:00
|
|
|
/* Don't save if we're loading from store */
|
2019-04-11 07:15:22 +02:00
|
|
|
if (!index) {
|
2019-06-03 20:05:25 +02:00
|
|
|
hc->bcast.index
|
2019-06-03 20:22:25 +02:00
|
|
|
= gossip_store_add_private_update(rstate->gs,
|
2019-06-03 20:05:25 +02:00
|
|
|
update);
|
2019-04-11 07:15:13 +02:00
|
|
|
} else
|
|
|
|
hc->bcast.index = index;
|
2018-05-10 09:50:03 +02:00
|
|
|
return true;
|
2019-04-08 01:52:19 +02:00
|
|
|
}
|
2018-05-10 09:50:03 +02:00
|
|
|
|
2019-04-11 07:15:13 +02:00
|
|
|
/* If we're loading from store, this means we don't re-add to store. */
|
2019-06-03 20:22:25 +02:00
|
|
|
if (index)
|
2019-06-03 20:23:25 +02:00
|
|
|
hc->bcast.index = index;
|
2019-10-08 03:13:24 +02:00
|
|
|
else {
|
2019-06-03 20:23:25 +02:00
|
|
|
hc->bcast.index
|
2019-06-03 20:22:25 +02:00
|
|
|
= gossip_store_add(rstate->gs, update,
|
2019-06-03 20:23:25 +02:00
|
|
|
hc->bcast.timestamp,
|
2019-11-04 01:37:05 +01:00
|
|
|
is_local_channel(rstate, chan),
|
2019-06-03 20:22:25 +02:00
|
|
|
NULL);
|
2019-10-08 03:30:24 +02:00
|
|
|
if (hc->bcast.timestamp > rstate->last_timestamp
|
|
|
|
&& hc->bcast.timestamp < time_now().ts.tv_sec)
|
|
|
|
rstate->last_timestamp = hc->bcast.timestamp;
|
|
|
|
|
2019-10-08 03:26:24 +02:00
|
|
|
peer_supplied_good_gossip(peer, 1);
|
2019-10-08 03:13:24 +02:00
|
|
|
}
|
2019-04-11 07:15:13 +02:00
|
|
|
|
|
|
|
if (uc) {
|
|
|
|
/* If we were waiting for these nodes to appear (or gain a
|
|
|
|
public channel), process node_announcements now */
|
|
|
|
process_pending_node_announcement(rstate, &chan->nodes[0]->id);
|
|
|
|
process_pending_node_announcement(rstate, &chan->nodes[1]->id);
|
|
|
|
tal_free(uc);
|
|
|
|
}
|
2020-08-10 15:19:46 +02:00
|
|
|
|
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Received channel_update for channel %s/%d now %s",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
channel_flags & 0x01,
|
|
|
|
channel_flags & ROUTING_FLAGS_DISABLED ? "DISABLED" : "ACTIVE");
|
|
|
|
|
2018-04-11 01:03:35 +02:00
|
|
|
return true;
|
2018-03-22 15:11:24 +01:00
|
|
|
}
|
|
|
|
|
2019-10-08 03:28:24 +02:00
|
|
|
bool would_ratelimit_cupdate(struct routing_state *rstate,
|
|
|
|
const struct half_chan *hc,
|
|
|
|
u32 timestamp)
|
|
|
|
{
|
|
|
|
return update_tokens(rstate, hc->tokens, hc->bcast.timestamp, timestamp)
|
|
|
|
>= TOKENS_PER_MSG;
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
static const struct node_id *get_channel_owner(struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *scid,
|
|
|
|
int direction)
|
|
|
|
{
|
|
|
|
struct chan *chan = get_channel(rstate, scid);
|
2019-04-11 07:15:13 +02:00
|
|
|
struct unupdated_channel *uc;
|
2019-04-10 09:31:29 +02:00
|
|
|
|
|
|
|
if (chan)
|
|
|
|
return &chan->nodes[direction]->id;
|
2019-04-11 07:15:13 +02:00
|
|
|
|
|
|
|
/* Might be unupdated channel */
|
|
|
|
uc = get_unupdated_channel(rstate, scid);
|
|
|
|
if (uc)
|
|
|
|
return &uc->id[direction];
|
2019-04-10 09:31:29 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:09:25 +02:00
|
|
|
void remove_channel_from_store(struct routing_state *rstate,
|
|
|
|
struct chan *chan)
|
|
|
|
{
|
|
|
|
int update_type, announcment_type;
|
|
|
|
|
|
|
|
if (is_chan_public(chan)) {
|
|
|
|
update_type = WIRE_CHANNEL_UPDATE;
|
|
|
|
announcment_type = WIRE_CHANNEL_ANNOUNCEMENT;
|
|
|
|
} else {
|
|
|
|
update_type = WIRE_GOSSIP_STORE_PRIVATE_UPDATE;
|
2020-10-20 05:59:30 +02:00
|
|
|
announcment_type = WIRE_GOSSIP_STORE_PRIVATE_CHANNEL;
|
2019-06-03 20:09:25 +02:00
|
|
|
}
|
2020-10-20 05:59:30 +02:00
|
|
|
gossip_store_mark_channel_deleted(rstate->gs, &chan->scid);
|
2019-06-03 20:09:25 +02:00
|
|
|
|
|
|
|
/* If these aren't in the store, these are noops. */
|
2019-06-03 20:22:25 +02:00
|
|
|
gossip_store_delete(rstate->gs,
|
2019-06-03 20:09:25 +02:00
|
|
|
&chan->bcast, announcment_type);
|
2019-06-03 20:22:25 +02:00
|
|
|
gossip_store_delete(rstate->gs,
|
2019-06-03 20:09:25 +02:00
|
|
|
&chan->half[0].bcast, update_type);
|
2019-06-03 20:22:25 +02:00
|
|
|
gossip_store_delete(rstate->gs,
|
2019-06-03 20:09:25 +02:00
|
|
|
&chan->half[1].bcast, update_type);
|
|
|
|
}
|
|
|
|
|
2018-09-25 07:43:56 +02:00
|
|
|
u8 *handle_channel_update(struct routing_state *rstate, const u8 *update TAKES,
|
2019-10-08 03:13:24 +02:00
|
|
|
struct peer *peer,
|
2019-06-12 01:27:07 +02:00
|
|
|
struct short_channel_id *unknown_scid)
|
2017-02-01 15:09:26 +01:00
|
|
|
{
|
|
|
|
u8 *serialized;
|
2019-04-10 09:31:29 +02:00
|
|
|
const struct node_id *owner;
|
2017-02-01 15:09:26 +01:00
|
|
|
secp256k1_ecdsa_signature signature;
|
2017-03-02 13:21:49 +01:00
|
|
|
struct short_channel_id short_channel_id;
|
2017-02-01 15:09:26 +01:00
|
|
|
u32 timestamp;
|
2018-09-20 02:59:46 +02:00
|
|
|
u8 message_flags, channel_flags;
|
2017-02-01 15:09:26 +01:00
|
|
|
u16 expiry;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat htlc_minimum;
|
2017-02-01 15:09:26 +01:00
|
|
|
u32 fee_base_msat;
|
|
|
|
u32 fee_proportional_millionths;
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid chain_hash;
|
2018-01-12 18:55:29 +01:00
|
|
|
u8 direction;
|
2018-07-28 08:00:16 +02:00
|
|
|
size_t len = tal_count(update);
|
2019-04-10 09:31:29 +02:00
|
|
|
struct pending_cannouncement *pending;
|
2018-03-08 05:10:33 +01:00
|
|
|
u8 *err;
|
2017-02-01 15:09:26 +01:00
|
|
|
|
|
|
|
serialized = tal_dup_arr(tmpctx, u8, update, len, 0);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_channel_update(serialized, &signature,
|
2017-08-22 07:25:01 +02:00
|
|
|
&chain_hash, &short_channel_id,
|
2018-09-20 02:59:46 +02:00
|
|
|
×tamp, &message_flags,
|
|
|
|
&channel_flags, &expiry,
|
2019-02-21 04:45:55 +01:00
|
|
|
&htlc_minimum, &fee_base_msat,
|
2017-02-01 15:09:26 +01:00
|
|
|
&fee_proportional_millionths)) {
|
2018-03-08 05:10:33 +01:00
|
|
|
err = towire_errorfmt(rstate, NULL,
|
|
|
|
"Malformed channel_update %s",
|
|
|
|
tal_hex(tmpctx, serialized));
|
|
|
|
return err;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
2018-09-20 02:59:46 +02:00
|
|
|
direction = channel_flags & 0x1;
|
2017-02-01 15:09:26 +01:00
|
|
|
|
2017-08-22 07:25:01 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2019-01-14 03:22:05 +01:00
|
|
|
* The receiving node:
|
2018-06-17 12:13:44 +02:00
|
|
|
*...
|
|
|
|
* - if the specified `chain_hash` value is unknown (meaning it isn't
|
|
|
|
* active on the specified chain):
|
|
|
|
* - MUST ignore the channel update.
|
|
|
|
*/
|
2019-11-20 02:54:47 +01:00
|
|
|
if (!bitcoin_blkid_eq(&chain_hash, &chainparams->genesis_blockhash)) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Received channel_update for unknown chain %s",
|
|
|
|
type_to_string(tmpctx, struct bitcoin_blkid,
|
|
|
|
&chain_hash));
|
2018-03-08 05:10:33 +01:00
|
|
|
return NULL;
|
2017-08-22 07:25:01 +02:00
|
|
|
}
|
2017-02-01 15:09:26 +01:00
|
|
|
|
2019-03-27 15:40:25 +01:00
|
|
|
/* If we dropped the matching announcement for this channel due to the
|
|
|
|
* txout query failing, don't report failure, it's just too noisy on
|
|
|
|
* mainnet */
|
2019-09-27 02:04:34 +02:00
|
|
|
if (in_txout_failures(rstate, &short_channel_id))
|
2019-03-27 15:40:25 +01:00
|
|
|
return NULL;
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
/* If we have an unvalidated channel, just queue on that */
|
|
|
|
pending = find_pending_cannouncement(rstate, &short_channel_id);
|
|
|
|
if (pending) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Updated pending announce with update %s/%u",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
direction);
|
2019-10-15 01:59:44 +02:00
|
|
|
update_pending(pending, timestamp, serialized, direction, peer);
|
2018-06-04 06:25:25 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
owner = get_channel_owner(rstate, &short_channel_id, direction);
|
|
|
|
if (!owner) {
|
2019-06-12 01:27:07 +02:00
|
|
|
if (unknown_scid)
|
|
|
|
*unknown_scid = short_channel_id;
|
2019-04-10 09:31:29 +02:00
|
|
|
bad_gossip_order(serialized,
|
2019-10-08 03:14:24 +02:00
|
|
|
peer,
|
2019-04-10 09:31:29 +02:00
|
|
|
tal_fmt(tmpctx, "%s/%u",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
direction));
|
2018-03-08 05:10:33 +01:00
|
|
|
return NULL;
|
2018-02-27 20:58:20 +01:00
|
|
|
}
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
err = check_channel_update(rstate, owner, &signature, serialized);
|
2018-03-08 05:10:33 +01:00
|
|
|
if (err) {
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - if `signature` is not a valid signature, using `node_id`
|
|
|
|
* of the double-SHA256 of the entire message following the
|
|
|
|
* `signature` field (including unknown fields following
|
|
|
|
* `fee_proportional_millionths`):
|
|
|
|
* - MUST NOT process the message further.
|
|
|
|
* - SHOULD fail the connection.
|
|
|
|
*/
|
|
|
|
return err;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
2019-10-08 03:13:24 +02:00
|
|
|
routing_add_channel_update(rstate, take(serialized), 0, peer);
|
2018-03-08 05:10:33 +01:00
|
|
|
return NULL;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
2019-04-11 07:15:21 +02:00
|
|
|
struct wireaddr *read_addresses(const tal_t *ctx, const u8 *ser)
|
2017-05-08 21:26:46 +02:00
|
|
|
{
|
|
|
|
const u8 *cursor = ser;
|
2018-08-05 07:46:32 +02:00
|
|
|
size_t len = tal_count(ser);
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr *wireaddrs = tal_arr(ctx, struct wireaddr, 0);
|
2018-08-05 07:46:32 +02:00
|
|
|
|
|
|
|
while (cursor && len) {
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr wireaddr;
|
2017-09-01 06:18:55 +02:00
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2019-01-14 03:22:05 +01:00
|
|
|
* The receiving node:
|
2018-06-17 12:13:44 +02:00
|
|
|
*...
|
|
|
|
* - SHOULD ignore the first `address descriptor` that does
|
|
|
|
* NOT match the types defined above.
|
2017-09-01 06:18:55 +02:00
|
|
|
*/
|
2018-08-05 07:46:32 +02:00
|
|
|
if (!fromwire_wireaddr(&cursor, &len, &wireaddr)) {
|
2017-09-01 06:18:55 +02:00
|
|
|
if (!cursor)
|
|
|
|
/* Parsing address failed */
|
2017-10-23 06:17:38 +02:00
|
|
|
return tal_free(wireaddrs);
|
2017-09-01 06:18:55 +02:00
|
|
|
/* Unknown type, stop there. */
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("read_addresses: unknown address type %u",
|
2018-08-05 07:46:32 +02:00
|
|
|
cursor[0]);
|
2017-09-01 06:18:55 +02:00
|
|
|
break;
|
2017-05-08 21:26:46 +02:00
|
|
|
}
|
2017-09-01 06:18:55 +02:00
|
|
|
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(&wireaddrs, wireaddr);
|
2017-05-08 21:26:46 +02:00
|
|
|
}
|
2017-10-23 06:17:38 +02:00
|
|
|
return wireaddrs;
|
2017-05-08 21:26:46 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
bool routing_add_node_announcement(struct routing_state *rstate,
|
|
|
|
const u8 *msg TAKES,
|
2019-10-08 03:13:24 +02:00
|
|
|
u32 index,
|
2019-10-08 03:32:24 +02:00
|
|
|
struct peer *peer,
|
|
|
|
bool *was_unknown)
|
2018-03-22 15:11:24 +01:00
|
|
|
{
|
2018-04-20 10:09:50 +02:00
|
|
|
struct node *node;
|
|
|
|
secp256k1_ecdsa_signature signature;
|
|
|
|
u32 timestamp;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id node_id;
|
2018-04-20 10:09:50 +02:00
|
|
|
u8 rgb_color[3];
|
|
|
|
u8 alias[32];
|
|
|
|
u8 *features, *addresses;
|
2018-05-17 07:08:11 +02:00
|
|
|
|
2019-10-08 03:32:24 +02:00
|
|
|
if (was_unknown)
|
|
|
|
*was_unknown = false;
|
|
|
|
|
2019-04-10 09:31:18 +02:00
|
|
|
/* Make sure we own msg, even if we don't save it. */
|
|
|
|
if (taken(msg))
|
|
|
|
tal_steal(tmpctx, msg);
|
|
|
|
|
2019-04-08 11:58:44 +02:00
|
|
|
/* Note: validity of node_id is already checked. */
|
2018-05-17 07:08:11 +02:00
|
|
|
if (!fromwire_node_announcement(tmpctx, msg,
|
|
|
|
&signature, &features, ×tamp,
|
2019-04-08 11:58:44 +02:00
|
|
|
&node_id, rgb_color, alias,
|
2019-04-11 07:15:22 +02:00
|
|
|
&addresses)) {
|
2018-05-17 07:08:11 +02:00
|
|
|
return false;
|
2019-04-11 07:15:22 +02:00
|
|
|
}
|
2018-04-20 10:09:50 +02:00
|
|
|
|
|
|
|
node = get_node(rstate, &node_id);
|
|
|
|
|
2019-05-29 06:35:43 +02:00
|
|
|
if (node == NULL || !node_has_broadcastable_channels(node)) {
|
|
|
|
struct pending_node_announce *pna;
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - if `node_id` is NOT previously known from a
|
|
|
|
* `channel_announcement` message, OR if `timestamp` is NOT
|
|
|
|
* greater than the last-received `node_announcement` from
|
|
|
|
* this `node_id`:
|
|
|
|
* - SHOULD ignore the message.
|
|
|
|
*/
|
|
|
|
/* Check if we are currently verifying the txout for a
|
|
|
|
* matching channel */
|
|
|
|
pna = pending_node_map_get(rstate->pending_node_map,
|
|
|
|
&node_id);
|
|
|
|
if (!pna) {
|
2019-10-08 03:32:24 +02:00
|
|
|
if (was_unknown)
|
|
|
|
*was_unknown = true;
|
2019-10-08 03:14:24 +02:00
|
|
|
bad_gossip_order(msg, peer,
|
2019-05-29 06:35:43 +02:00
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&node_id));
|
|
|
|
return false;
|
|
|
|
} else if (timestamp <= pna->timestamp)
|
2019-06-20 04:56:52 +02:00
|
|
|
/* Ignore old ones: they're OK (unless from store). */
|
|
|
|
return index == 0;
|
2019-04-11 07:15:22 +02:00
|
|
|
|
2019-05-29 06:35:43 +02:00
|
|
|
SUPERVERBOSE("Deferring node_announcement for node %s",
|
|
|
|
type_to_string(tmpctx, struct node_id, &node_id));
|
|
|
|
pna->timestamp = timestamp;
|
|
|
|
pna->index = index;
|
|
|
|
tal_free(pna->node_announcement);
|
2019-10-08 03:13:24 +02:00
|
|
|
clear_softref(pna, &pna->peer_softref);
|
2020-02-27 03:17:01 +01:00
|
|
|
pna->node_announcement = tal_dup_talarr(pna, u8, msg);
|
2019-10-08 03:13:24 +02:00
|
|
|
set_softref(pna, &pna->peer_softref, peer);
|
2019-05-29 06:35:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-16 12:44:00 +02:00
|
|
|
if (node->bcast.index) {
|
|
|
|
if (index != 0) {
|
|
|
|
status_broken("gossip_store node_announcement %u replaces %u!",
|
|
|
|
index, node->bcast.index);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->bcast.timestamp >= timestamp) {
|
|
|
|
SUPERVERBOSE("Ignoring node announcement, it's outdated.");
|
|
|
|
/* OK unless we're loading from store */
|
|
|
|
return index == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow redundant updates once every 7 days */
|
2019-09-26 04:00:20 +02:00
|
|
|
if (timestamp < node->bcast.timestamp + GOSSIP_PRUNE_INTERVAL(rstate->dev_fast_gossip_prune) / 2
|
2019-09-16 12:44:00 +02:00
|
|
|
&& !nannounce_different(rstate->gs, node, msg)) {
|
2020-08-10 15:19:46 +02:00
|
|
|
SUPERVERBOSE(
|
|
|
|
"Ignoring redundant nannounce for %s"
|
|
|
|
" (last %u, now %u)",
|
|
|
|
type_to_string(tmpctx, struct node_id, &node_id),
|
|
|
|
node->bcast.timestamp, timestamp);
|
2019-09-16 12:44:00 +02:00
|
|
|
/* Ignoring != failing */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure it's not spamming us. */
|
2019-09-18 03:05:10 +02:00
|
|
|
if (!ratelimit(rstate,
|
|
|
|
&node->tokens, node->bcast.timestamp, timestamp)) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Ignoring spammy nannounce for %s"
|
|
|
|
" (last %u, now %u)",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct node_id,
|
|
|
|
&node_id),
|
|
|
|
node->bcast.timestamp, timestamp);
|
2019-09-16 12:44:00 +02:00
|
|
|
/* Ignoring != failing */
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-29 06:35:43 +02:00
|
|
|
}
|
2018-04-20 10:09:50 +02:00
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
/* Harmless if it was never added */
|
2019-06-03 20:22:25 +02:00
|
|
|
gossip_store_delete(rstate->gs,
|
2019-06-03 20:09:25 +02:00
|
|
|
&node->bcast,
|
|
|
|
WIRE_NODE_ANNOUNCEMENT);
|
2019-04-10 09:31:29 +02:00
|
|
|
|
|
|
|
node->bcast.timestamp = timestamp;
|
2019-10-08 03:30:24 +02:00
|
|
|
if (node->bcast.timestamp > rstate->last_timestamp
|
|
|
|
&& node->bcast.timestamp < time_now().ts.tv_sec)
|
|
|
|
rstate->last_timestamp = node->bcast.timestamp;
|
|
|
|
|
2019-11-14 01:16:53 +01:00
|
|
|
if (feature_offered(features, OPT_VAR_ONION))
|
|
|
|
node->hop_style = ROUTE_HOP_TLV;
|
|
|
|
else
|
|
|
|
/* Reset it in case they no longer offer the feature */
|
|
|
|
node->hop_style = ROUTE_HOP_LEGACY;
|
|
|
|
|
2019-06-03 20:22:25 +02:00
|
|
|
if (index)
|
|
|
|
node->bcast.index = index;
|
2019-10-08 03:13:24 +02:00
|
|
|
else {
|
2019-06-03 20:22:25 +02:00
|
|
|
node->bcast.index
|
|
|
|
= gossip_store_add(rstate->gs, msg,
|
2019-11-04 01:37:05 +01:00
|
|
|
node->bcast.timestamp,
|
|
|
|
node_id_eq(&node_id,
|
|
|
|
&rstate->local_id),
|
|
|
|
NULL);
|
2019-10-08 03:26:24 +02:00
|
|
|
peer_supplied_good_gossip(peer, 1);
|
2019-10-08 03:13:24 +02:00
|
|
|
}
|
2020-08-10 15:19:46 +02:00
|
|
|
|
|
|
|
/* Only log this if *not* loading from store. */
|
|
|
|
if (!index)
|
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Received node_announcement for node %s",
|
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&node_id));
|
|
|
|
|
2018-06-04 06:16:25 +02:00
|
|
|
return true;
|
2018-04-11 01:03:12 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 03:13:24 +02:00
|
|
|
u8 *handle_node_announcement(struct routing_state *rstate, const u8 *node_ann,
|
2019-10-08 03:32:24 +02:00
|
|
|
struct peer *peer, bool *was_unknown)
|
2017-02-01 15:09:26 +01:00
|
|
|
{
|
|
|
|
u8 *serialized;
|
|
|
|
struct sha256_double hash;
|
|
|
|
secp256k1_ecdsa_signature signature;
|
|
|
|
u32 timestamp;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id node_id;
|
2017-02-01 15:09:26 +01:00
|
|
|
u8 rgb_color[3];
|
|
|
|
u8 alias[32];
|
|
|
|
u8 *features, *addresses;
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr *wireaddrs;
|
2018-07-28 08:00:16 +02:00
|
|
|
size_t len = tal_count(node_ann);
|
2017-02-01 15:09:26 +01:00
|
|
|
|
2019-10-08 03:32:24 +02:00
|
|
|
if (was_unknown)
|
|
|
|
*was_unknown = false;
|
|
|
|
|
2017-02-01 15:09:26 +01:00
|
|
|
serialized = tal_dup_arr(tmpctx, u8, node_ann, len, 0);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_node_announcement(tmpctx, serialized,
|
2017-08-03 03:45:04 +02:00
|
|
|
&signature, &features, ×tamp,
|
2019-04-08 11:58:44 +02:00
|
|
|
&node_id, rgb_color, alias,
|
2017-02-01 15:09:26 +01:00
|
|
|
&addresses)) {
|
2018-03-08 05:10:26 +01:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - if `node_id` is NOT a valid compressed public key:
|
|
|
|
* - SHOULD fail the connection.
|
|
|
|
* - MUST NOT process the message further.
|
|
|
|
*/
|
|
|
|
u8 *err = towire_errorfmt(rstate, NULL,
|
|
|
|
"Malformed node_announcement %s",
|
|
|
|
tal_hex(tmpctx, node_ann));
|
|
|
|
return err;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sha256_double(&hash, serialized + 66, tal_count(serialized) - 66);
|
2019-04-08 11:58:44 +02:00
|
|
|
/* If node_id is invalid, it fails here */
|
2019-04-08 11:58:32 +02:00
|
|
|
if (!check_signed_hash_nodeid(&hash, &signature, &node_id)) {
|
2018-03-08 05:10:26 +01:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2019-01-14 03:22:05 +01:00
|
|
|
* - if `signature` is not a valid signature, using
|
|
|
|
* `node_id` of the double-SHA256 of the entire
|
|
|
|
* message following the `signature` field
|
|
|
|
* (including unknown fields following
|
|
|
|
* `fee_proportional_millionths`):
|
2018-03-08 05:10:26 +01:00
|
|
|
* - MUST NOT process the message further.
|
2019-01-14 03:22:05 +01:00
|
|
|
* - SHOULD fail the connection.
|
2018-03-08 05:10:26 +01:00
|
|
|
*/
|
|
|
|
u8 *err = towire_errorfmt(rstate, NULL,
|
|
|
|
"Bad signature for %s hash %s"
|
|
|
|
" on node_announcement %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
secp256k1_ecdsa_signature,
|
|
|
|
&signature),
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct sha256_double,
|
|
|
|
&hash),
|
|
|
|
tal_hex(tmpctx, node_ann));
|
|
|
|
return err;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
2018-02-02 19:49:12 +01:00
|
|
|
|
2018-03-08 05:10:29 +01:00
|
|
|
wireaddrs = read_addresses(tmpctx, addresses);
|
|
|
|
if (!wireaddrs) {
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - if `addrlen` is insufficient to hold the address
|
|
|
|
* descriptors of the known types:
|
|
|
|
* - SHOULD fail the connection.
|
|
|
|
*/
|
|
|
|
u8 *err = towire_errorfmt(rstate, NULL,
|
|
|
|
"Malformed wireaddrs %s in %s.",
|
|
|
|
tal_hex(tmpctx, wireaddrs),
|
|
|
|
tal_hex(tmpctx, node_ann));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-05-29 06:35:43 +02:00
|
|
|
/* May still fail, if we don't know the node. */
|
2019-10-08 03:32:24 +02:00
|
|
|
routing_add_node_announcement(rstate, serialized, 0, peer, was_unknown);
|
2018-03-08 05:10:26 +01:00
|
|
|
return NULL;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
2017-03-15 12:44:01 +01:00
|
|
|
|
2020-04-11 05:22:43 +02:00
|
|
|
struct route_hop **get_route(const tal_t *ctx, struct routing_state *rstate,
|
|
|
|
const struct node_id *source,
|
|
|
|
const struct node_id *destination,
|
|
|
|
struct amount_msat msat, double riskfactor,
|
|
|
|
u32 final_cltv,
|
|
|
|
double fuzz, u64 seed,
|
|
|
|
struct exclude_entry **excluded,
|
|
|
|
u32 max_hops)
|
2017-03-15 12:44:01 +01:00
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan **route;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat total_amount;
|
2017-03-15 12:44:01 +01:00
|
|
|
unsigned int total_delay;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat fee;
|
2020-04-11 05:22:43 +02:00
|
|
|
struct route_hop **hops;
|
2018-03-04 03:23:32 +01:00
|
|
|
struct node *n;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat *saved_capacity;
|
2019-08-31 15:57:08 +02:00
|
|
|
struct short_channel_id_dir *excluded_chan;
|
2019-02-01 03:36:18 +01:00
|
|
|
struct siphash_seed base_seed;
|
2019-01-15 05:04:27 +01:00
|
|
|
|
2019-08-31 15:57:08 +02:00
|
|
|
saved_capacity = tal_arr(tmpctx, struct amount_msat, 0);
|
|
|
|
excluded_chan = tal_arr(tmpctx, struct short_channel_id_dir, 0);
|
2019-01-15 05:04:27 +01:00
|
|
|
|
2019-02-01 03:36:18 +01:00
|
|
|
base_seed.u.u64[0] = base_seed.u.u64[1] = seed;
|
|
|
|
|
2019-04-17 09:35:33 +02:00
|
|
|
if (amount_msat_eq(msat, AMOUNT_MSAT(0)))
|
|
|
|
return NULL;
|
|
|
|
|
2019-08-31 15:57:08 +02:00
|
|
|
/* Temporarily set the capacity of the excluded channels and the incoming channels
|
|
|
|
* of excluded nodes to zero. */
|
2019-01-15 05:04:27 +01:00
|
|
|
for (size_t i = 0; i < tal_count(excluded); i++) {
|
2019-08-31 15:57:08 +02:00
|
|
|
if (excluded[i]->type == EXCLUDE_CHANNEL) {
|
|
|
|
struct short_channel_id_dir *chan_id = &excluded[i]->u.chan_id;
|
|
|
|
struct chan *chan = get_channel(rstate, &chan_id->scid);
|
|
|
|
if (!chan)
|
|
|
|
continue;
|
|
|
|
tal_arr_expand(&saved_capacity, chan->half[chan_id->dir].htlc_maximum);
|
|
|
|
tal_arr_expand(&excluded_chan, *chan_id);
|
|
|
|
chan->half[chan_id->dir].htlc_maximum = AMOUNT_MSAT(0);
|
|
|
|
} else {
|
|
|
|
assert(excluded[i]->type == EXCLUDE_NODE);
|
|
|
|
|
|
|
|
struct node *node = get_node(rstate, &excluded[i]->u.node_id);
|
|
|
|
if (!node)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct chan_map_iter i;
|
|
|
|
struct chan *chan;
|
|
|
|
for (chan = first_chan(node, &i); chan; chan = next_chan(node, &i)) {
|
|
|
|
int dir = half_chan_to(node, chan);
|
|
|
|
tal_arr_expand(&saved_capacity, chan->half[dir].htlc_maximum);
|
|
|
|
|
|
|
|
struct short_channel_id_dir id;
|
|
|
|
id.scid = chan->scid;
|
|
|
|
id.dir = dir;
|
|
|
|
tal_arr_expand(&excluded_chan, id);
|
|
|
|
|
|
|
|
chan->half[dir].htlc_maximum = AMOUNT_MSAT(0);
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 05:04:27 +01:00
|
|
|
}
|
2017-03-15 12:44:01 +01:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
route = find_route(ctx, rstate, source, destination, msat,
|
2019-02-01 06:53:39 +01:00
|
|
|
riskfactor / BLOCKS_PER_YEAR / 100,
|
2019-02-01 03:36:18 +01:00
|
|
|
fuzz, &base_seed, max_hops, &fee);
|
2017-03-15 12:44:01 +01:00
|
|
|
|
2019-01-15 05:04:27 +01:00
|
|
|
/* Now restore the capacity. */
|
2019-08-02 14:29:58 +02:00
|
|
|
/* Restoring is done in reverse order, in order to properly
|
|
|
|
* handle the case where a channel is indicated twice in
|
|
|
|
* our input.
|
|
|
|
* Entries in `saved_capacity` of that channel beyond the
|
|
|
|
* first entry will be 0, only the first entry of that
|
|
|
|
* channel will be the correct capacity.
|
|
|
|
* By restoring in reverse order we ensure we can restore
|
|
|
|
* the correct capacity.
|
|
|
|
*/
|
2019-08-31 15:57:08 +02:00
|
|
|
for (ssize_t i = tal_count(excluded_chan) - 1; i >= 0; i--) {
|
|
|
|
struct chan *chan = get_channel(rstate, &excluded_chan[i].scid);
|
2019-01-15 05:04:27 +01:00
|
|
|
if (!chan)
|
|
|
|
continue;
|
2019-08-31 15:57:08 +02:00
|
|
|
chan->half[excluded_chan[i].dir].htlc_maximum = saved_capacity[i];
|
2019-01-15 05:04:27 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:54 +01:00
|
|
|
if (!route) {
|
2017-03-15 12:44:01 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fees, delays need to be calculated backwards along route. */
|
2020-04-11 05:22:43 +02:00
|
|
|
hops = tal_arr(ctx, struct route_hop *, tal_count(route));
|
2019-02-21 04:45:55 +01:00
|
|
|
total_amount = msat;
|
2017-10-23 06:16:57 +02:00
|
|
|
total_delay = final_cltv;
|
2017-03-15 12:44:01 +01:00
|
|
|
|
2018-03-04 03:23:32 +01:00
|
|
|
/* Start at destination node. */
|
|
|
|
n = get_node(rstate, destination);
|
2019-01-15 05:04:27 +01:00
|
|
|
for (int i = tal_count(route) - 1; i >= 0; i--) {
|
2018-03-04 03:26:59 +01:00
|
|
|
const struct half_chan *c;
|
2019-02-21 04:45:55 +01:00
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
int idx = half_chan_to(n, route[i]);
|
|
|
|
c = &route[i]->half[idx];
|
2020-04-11 05:22:43 +02:00
|
|
|
hops[i] = tal(hops, struct route_hop);
|
|
|
|
hops[i]->channel_id = route[i]->scid;
|
|
|
|
hops[i]->nodeid = n->id;
|
|
|
|
hops[i]->amount = total_amount;
|
|
|
|
hops[i]->delay = total_delay;
|
|
|
|
hops[i]->direction = idx;
|
|
|
|
hops[i]->style = n->hop_style;
|
|
|
|
hops[i]->blinding = NULL;
|
|
|
|
hops[i]->enctlv = NULL;
|
2019-02-21 04:45:55 +01:00
|
|
|
|
|
|
|
/* Since we calculated this route, it should not overflow! */
|
|
|
|
if (!amount_msat_add_fee(&total_amount,
|
|
|
|
c->base_fee, c->proportional_fee)) {
|
|
|
|
status_broken("Route overflow step %i: %s + %u/%u!?",
|
|
|
|
i, type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&total_amount),
|
|
|
|
c->base_fee, c->proportional_fee);
|
|
|
|
return tal_free(hops);
|
|
|
|
}
|
2018-03-04 03:23:32 +01:00
|
|
|
total_delay += c->delay;
|
2018-03-04 03:26:54 +01:00
|
|
|
n = other_node(n, route[i]);
|
2017-03-15 12:44:01 +01:00
|
|
|
}
|
2019-05-31 09:30:33 +02:00
|
|
|
assert(node_id_eq(&n->id, source ? source : &rstate->local_id));
|
2017-10-23 06:16:57 +02:00
|
|
|
|
2017-03-15 12:44:01 +01:00
|
|
|
return hops;
|
|
|
|
}
|
2018-01-18 00:32:36 +01:00
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
void route_prune(struct routing_state *rstate)
|
|
|
|
{
|
2019-04-08 01:51:30 +02:00
|
|
|
u64 now = gossip_time_now(rstate).ts.tv_sec;
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Anything below this highwater mark ought to be pruned */
|
2019-09-26 04:00:20 +02:00
|
|
|
const s64 highwater = now - GOSSIP_PRUNE_INTERVAL(rstate->dev_fast_gossip_prune);
|
2019-05-21 09:13:28 +02:00
|
|
|
struct chan **pruned = tal_arr(tmpctx, struct chan *, 0);
|
2018-03-02 09:59:17 +01:00
|
|
|
u64 idx;
|
|
|
|
|
|
|
|
/* Now iterate through all channels and see if it is still alive */
|
2019-07-28 09:17:44 +02:00
|
|
|
for (struct chan *chan = uintmap_first(&rstate->chanmap, &idx);
|
2018-03-02 09:59:17 +01:00
|
|
|
chan;
|
2018-03-04 03:26:59 +01:00
|
|
|
chan = uintmap_after(&rstate->chanmap, &idx)) {
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Local-only? Don't prune. */
|
2018-05-10 16:00:38 +02:00
|
|
|
if (!is_chan_public(chan))
|
2018-03-02 09:59:17 +01:00
|
|
|
continue;
|
|
|
|
|
2020-08-20 08:55:22 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
* - if a channel's oldest `channel_update`s `timestamp` is
|
|
|
|
* older than two weeks (1209600 seconds):
|
|
|
|
* - MAY prune the channel.
|
|
|
|
*/
|
|
|
|
/* This is a fancy way of saying "both ends must refresh!" */
|
|
|
|
if (!is_halfchan_defined(&chan->half[0])
|
|
|
|
|| chan->half[0].bcast.timestamp < highwater
|
|
|
|
|| !is_halfchan_defined(&chan->half[1])
|
|
|
|
|| chan->half[1].bcast.timestamp < highwater) {
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug(
|
2018-03-02 09:59:17 +01:00
|
|
|
"Pruning channel %s from network view (ages %"PRIu64" and %"PRIu64"s)",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
2018-03-02 09:59:17 +01:00
|
|
|
&chan->scid),
|
2019-07-14 01:59:55 +02:00
|
|
|
is_halfchan_defined(&chan->half[0])
|
|
|
|
? now - chan->half[0].bcast.timestamp : 0,
|
|
|
|
is_halfchan_defined(&chan->half[1])
|
|
|
|
? now - chan->half[1].bcast.timestamp : 0);
|
2018-03-02 09:59:17 +01:00
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
/* This may perturb iteration so do outside loop. */
|
2019-05-21 09:13:28 +02:00
|
|
|
tal_arr_expand(&pruned, chan);
|
2018-03-02 09:59:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 07:15:13 +02:00
|
|
|
/* Look for channels we had an announcement for, but no update. */
|
2019-07-28 09:17:44 +02:00
|
|
|
for (struct unupdated_channel *uc
|
|
|
|
= uintmap_first(&rstate->unupdated_chanmap, &idx);
|
2019-04-11 07:15:13 +02:00
|
|
|
uc;
|
|
|
|
uc = uintmap_after(&rstate->unupdated_chanmap, &idx)) {
|
|
|
|
if (uc->added.ts.tv_sec < highwater) {
|
2019-07-28 09:17:44 +02:00
|
|
|
tal_free(uc);
|
2019-04-11 07:15:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 09:13:28 +02:00
|
|
|
/* Now free all the chans and maybe even nodes. */
|
2019-06-03 20:09:25 +02:00
|
|
|
for (size_t i = 0; i < tal_count(pruned); i++) {
|
|
|
|
remove_channel_from_store(rstate, pruned[i]);
|
2019-05-21 09:13:28 +02:00
|
|
|
free_chan(rstate, pruned[i]);
|
2019-06-03 20:09:25 +02:00
|
|
|
}
|
2018-03-02 09:59:17 +01:00
|
|
|
}
|
2018-04-21 12:13:33 +02:00
|
|
|
|
2020-10-20 05:59:30 +02:00
|
|
|
bool routing_add_private_channel(struct routing_state *rstate,
|
|
|
|
const struct peer *peer,
|
|
|
|
const u8 *msg, u64 index)
|
2018-04-21 12:13:33 +02:00
|
|
|
{
|
|
|
|
struct short_channel_id scid;
|
2020-10-20 05:59:30 +02:00
|
|
|
struct node_id node_id[2];
|
|
|
|
struct pubkey ignorekey;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat sat;
|
2019-06-03 20:07:25 +02:00
|
|
|
struct chan *chan;
|
2020-10-20 05:59:30 +02:00
|
|
|
u8 *features, *chan_ann;
|
|
|
|
secp256k1_ecdsa_signature ignoresig;
|
|
|
|
struct bitcoin_blkid chain_hash;
|
2018-04-21 12:13:33 +02:00
|
|
|
|
2020-10-20 05:59:30 +02:00
|
|
|
if (!fromwire_gossip_store_private_channel(tmpctx, msg,
|
|
|
|
&sat, &chan_ann))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
if (!fromwire_channel_announcement(tmpctx, chan_ann,
|
|
|
|
&ignoresig,
|
|
|
|
&ignoresig,
|
|
|
|
&ignoresig,
|
|
|
|
&ignoresig,
|
|
|
|
&features,
|
|
|
|
&chain_hash,
|
|
|
|
&scid,
|
|
|
|
&node_id[0],
|
|
|
|
&node_id[1],
|
|
|
|
&ignorekey,
|
|
|
|
&ignorekey))
|
2018-11-05 02:21:51 +01:00
|
|
|
return false;
|
2018-04-21 12:13:33 +02:00
|
|
|
|
2018-04-26 06:51:02 +02:00
|
|
|
/* Can happen on channeld restart. */
|
2018-04-21 12:13:33 +02:00
|
|
|
if (get_channel(rstate, &scid)) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"Attempted to local_add_channel a known channel");
|
2018-11-05 02:21:51 +01:00
|
|
|
return true;
|
2018-04-21 12:13:33 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(peer ? &peer->id : NULL,
|
|
|
|
"local_add_channel %s",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id, &scid));
|
2018-05-17 07:09:59 +02:00
|
|
|
|
2018-05-17 07:09:59 +02:00
|
|
|
/* Create new (unannounced) channel */
|
2020-10-20 05:59:30 +02:00
|
|
|
chan = new_chan(rstate, &scid, &node_id[0], &node_id[1], sat,
|
2020-05-04 02:18:34 +02:00
|
|
|
features);
|
2019-06-03 20:07:25 +02:00
|
|
|
if (!index)
|
2019-11-04 01:37:05 +01:00
|
|
|
index = gossip_store_add(rstate->gs, msg, 0, false, NULL);
|
2019-06-03 20:07:25 +02:00
|
|
|
chan->bcast.index = index;
|
2018-11-05 02:21:51 +01:00
|
|
|
return true;
|
2018-04-21 12:13:33 +02:00
|
|
|
}
|
2019-04-08 01:51:30 +02:00
|
|
|
|
|
|
|
struct timeabs gossip_time_now(const struct routing_state *rstate)
|
|
|
|
{
|
|
|
|
#if DEVELOPER
|
|
|
|
if (rstate->gossip_time)
|
|
|
|
return *rstate->gossip_time;
|
|
|
|
#endif
|
|
|
|
return time_now();
|
|
|
|
}
|
2019-06-14 05:30:56 +02:00
|
|
|
|
2019-06-20 04:57:52 +02:00
|
|
|
const char *unfinalized_entries(const tal_t *ctx, struct routing_state *rstate)
|
2019-06-14 05:30:56 +02:00
|
|
|
{
|
2019-06-20 04:57:52 +02:00
|
|
|
struct unupdated_channel *uc;
|
|
|
|
u64 index;
|
|
|
|
struct pending_node_announce *pna;
|
|
|
|
struct pending_node_map_iter it;
|
2019-06-14 05:30:56 +02:00
|
|
|
|
2019-06-20 04:57:52 +02:00
|
|
|
uc = uintmap_first(&rstate->unupdated_chanmap, &index);
|
|
|
|
if (uc)
|
|
|
|
return tal_fmt(ctx, "Unupdated channel_announcement at %u",
|
|
|
|
uc->index);
|
2019-06-14 05:30:56 +02:00
|
|
|
|
2019-06-20 04:57:52 +02:00
|
|
|
pna = pending_node_map_first(rstate->pending_node_map, &it);
|
|
|
|
if (pna)
|
|
|
|
return tal_fmt(ctx, "Waiting node_announcement at %u",
|
|
|
|
pna->index);
|
2019-06-14 05:30:56 +02:00
|
|
|
|
2019-06-20 04:57:52 +02:00
|
|
|
return NULL;
|
2019-06-14 05:30:56 +02:00
|
|
|
}
|
|
|
|
|
2019-06-20 04:57:52 +02:00
|
|
|
/* Gossip store was corrupt, forget anything we loaded. */
|
|
|
|
void remove_all_gossip(struct routing_state *rstate)
|
2019-06-14 05:30:56 +02:00
|
|
|
{
|
2019-06-20 04:57:52 +02:00
|
|
|
struct node *n;
|
|
|
|
struct node_map_iter nit;
|
|
|
|
struct chan *c;
|
2019-06-14 05:30:56 +02:00
|
|
|
struct unupdated_channel *uc;
|
|
|
|
u64 index;
|
2019-06-20 04:57:52 +02:00
|
|
|
struct pending_cannouncement *pca;
|
|
|
|
struct pending_cannouncement_map_iter pit;
|
|
|
|
struct pending_node_map_iter pnait;
|
|
|
|
|
|
|
|
/* We don't want them to try to delete from store, so do this
|
|
|
|
* manually. */
|
|
|
|
while ((n = node_map_first(rstate->nodes, &nit)) != NULL) {
|
|
|
|
tal_del_destructor2(n, destroy_node, rstate);
|
|
|
|
if (node_uses_chan_map(n))
|
|
|
|
chan_map_clear(&n->chans.map);
|
|
|
|
node_map_del(rstate->nodes, n);
|
|
|
|
tal_free(n);
|
|
|
|
}
|
2019-06-14 05:30:56 +02:00
|
|
|
|
2019-06-20 04:57:52 +02:00
|
|
|
/* Now free all the channels. */
|
|
|
|
while ((c = uintmap_first(&rstate->chanmap, &index)) != NULL) {
|
|
|
|
uintmap_del(&rstate->chanmap, index);
|
2019-10-17 17:15:55 +02:00
|
|
|
#if DEVELOPER
|
2020-08-05 05:55:30 +02:00
|
|
|
c->sat = amount_sat((unsigned long)c);
|
2019-10-17 17:15:55 +02:00
|
|
|
#endif
|
2019-06-20 04:57:52 +02:00
|
|
|
tal_free(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((uc = uintmap_first(&rstate->unupdated_chanmap, &index)) != NULL)
|
|
|
|
tal_free(uc);
|
2019-06-14 05:30:56 +02:00
|
|
|
|
2019-06-20 04:57:52 +02:00
|
|
|
while ((pca = pending_cannouncement_map_first(&rstate->pending_cannouncements, &pit)) != NULL)
|
|
|
|
tal_free(pca);
|
2019-06-14 05:30:56 +02:00
|
|
|
|
2019-06-20 04:57:52 +02:00
|
|
|
/* Freeing unupdated chanmaps should empty this */
|
|
|
|
assert(pending_node_map_first(rstate->pending_node_map, &pnait) == NULL);
|
2019-06-14 05:30:56 +02:00
|
|
|
}
|