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>
|
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>
|
2016-06-28 23:19:21 +02:00
|
|
|
#include <ccan/structeq/structeq.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>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/pseudorand.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/status.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>
|
2018-04-21 12:13:33 +02:00
|
|
|
#include <gossipd/gen_gossip_wire.h>
|
2016-06-28 23:19:21 +02:00
|
|
|
#include <inttypes.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <wire/gen_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
|
|
|
|
|
2017-12-18 05:15:40 +01:00
|
|
|
/* For overflow avoidance, we never deal with msatoshi > 40 bits. */
|
|
|
|
#define MAX_MSATOSHI (1ULL << 40)
|
|
|
|
|
|
|
|
/* Proportional fee must be less than 24 bits, so never overflows. */
|
|
|
|
#define MAX_PROPORTIONAL_FEE (1 << 24)
|
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
/* We've unpacked and checked its signatures, now we wait for master to tell
|
|
|
|
* us the txout to check */
|
|
|
|
struct pending_cannouncement {
|
2018-03-02 09:59:13 +01:00
|
|
|
/* Off routing_state->pending_cannouncement */
|
|
|
|
struct list_node list;
|
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
/* Unpacked fields here */
|
|
|
|
struct short_channel_id short_channel_id;
|
|
|
|
struct pubkey node_id_1;
|
|
|
|
struct pubkey node_id_2;
|
|
|
|
struct pubkey bitcoin_key_1;
|
|
|
|
struct pubkey bitcoin_key_2;
|
|
|
|
|
|
|
|
/* The raw bits */
|
|
|
|
const u8 *announce;
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2018-01-12 18:55:29 +01:00
|
|
|
/* Deferred updates, if we received them while waiting for
|
|
|
|
* this (one for each direction) */
|
|
|
|
const u8 *updates[2];
|
2018-02-02 14:55:54 +01:00
|
|
|
|
|
|
|
/* Only ever replace with newer updates */
|
|
|
|
u32 update_timestamps[2];
|
2018-01-04 12:40:58 +01:00
|
|
|
};
|
|
|
|
|
2018-02-02 19:49:12 +01:00
|
|
|
struct pending_node_announce {
|
|
|
|
struct pubkey nodeid;
|
|
|
|
u8 *node_announcement;
|
|
|
|
u32 timestamp;
|
|
|
|
};
|
|
|
|
|
2018-07-04 07:29:56 +02:00
|
|
|
static const struct pubkey *
|
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,
|
2018-07-04 07:29:56 +02:00
|
|
|
const struct pubkey *key)
|
2018-02-02 19:49:12 +01:00
|
|
|
{
|
2018-07-04 07:29:56 +02:00
|
|
|
return structeq(&pna->nodeid, key);
|
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);
|
|
|
|
|
2017-09-01 06:18:55 +02:00
|
|
|
static struct node_map *empty_node_map(const tal_t *ctx)
|
|
|
|
{
|
|
|
|
struct node_map *map = tal(ctx, struct node_map);
|
|
|
|
node_map_init(map);
|
|
|
|
tal_add_destructor(map, node_map_clear);
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:25:01 +02:00
|
|
|
struct routing_state *new_routing_state(const tal_t *ctx,
|
2017-12-18 07:44:10 +01:00
|
|
|
const struct bitcoin_blkid *chain_hash,
|
2018-03-02 09:59:17 +01:00
|
|
|
const struct pubkey *local_id,
|
2018-05-07 06:29:22 +02:00
|
|
|
u32 prune_timeout,
|
|
|
|
bool dev_allow_localhost)
|
2017-01-19 23:46:07 +01:00
|
|
|
{
|
|
|
|
struct routing_state *rstate = tal(ctx, struct routing_state);
|
|
|
|
rstate->nodes = empty_node_map(rstate);
|
2017-01-26 22:47:52 +01:00
|
|
|
rstate->broadcasts = new_broadcast_state(rstate);
|
2017-08-22 07:25:01 +02:00
|
|
|
rstate->chain_hash = *chain_hash;
|
2017-12-02 23:28:19 +01:00
|
|
|
rstate->local_id = *local_id;
|
2018-03-02 09:59:17 +01:00
|
|
|
rstate->prune_timeout = prune_timeout;
|
2018-05-30 19:24:56 +02:00
|
|
|
rstate->store = gossip_store_new(rstate, rstate, rstate->broadcasts);
|
2018-05-07 06:29:22 +02:00
|
|
|
rstate->dev_allow_localhost = dev_allow_localhost;
|
2018-06-04 06:15:25 +02:00
|
|
|
rstate->local_channel_announced = false;
|
2018-01-04 12:40:58 +01:00
|
|
|
list_head_init(&rstate->pending_cannouncement);
|
2018-03-04 03:26:59 +01:00
|
|
|
uintmap_init(&rstate->chanmap);
|
2018-01-30 19:46:07 +01:00
|
|
|
|
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);
|
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
return rstate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-04 07:29:56 +02:00
|
|
|
const struct pubkey *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
|
|
|
}
|
|
|
|
|
2018-07-04 07:29:56 +02:00
|
|
|
size_t node_map_hash_key(const struct pubkey *key)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
2016-11-01 12:03:06 +01:00
|
|
|
return siphash24(siphash_seed(), key, sizeof(*key));
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
2018-07-04 07:29:56 +02:00
|
|
|
bool node_map_node_eq(const struct node *n, const struct pubkey *key)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
2018-07-04 07:29:56 +02:00
|
|
|
return structeq(&n->id, key);
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
2016-09-28 16:52:03 +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
|
|
|
{
|
2018-02-27 20:59:48 +01:00
|
|
|
node_map_del(rstate->nodes, node);
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
/* These remove themselves from the array. */
|
2018-03-04 03:26:59 +01:00
|
|
|
while (tal_count(node->chans))
|
|
|
|
tal_free(node->chans[0]);
|
2016-08-18 06:55:13 +02:00
|
|
|
}
|
|
|
|
|
2018-02-27 21:16:43 +01:00
|
|
|
struct node *get_node(struct routing_state *rstate, const struct pubkey *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,
|
|
|
|
const struct pubkey *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;
|
2018-03-04 03:26:59 +01:00
|
|
|
n->chans = tal_arr(n, struct chan *, 0);
|
2016-10-28 16:40:27 +02:00
|
|
|
n->alias = NULL;
|
2018-05-10 14:22:37 +02:00
|
|
|
n->node_announcement = NULL;
|
2018-06-08 08:31:49 +02:00
|
|
|
n->node_announcement_index = 0;
|
2017-09-01 06:18:54 +02:00
|
|
|
n->last_timestamp = -1;
|
2017-10-23 06:17:38 +02:00
|
|
|
n->addresses = tal_arr(n, struct wireaddr, 0);
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:31:55 +02:00
|
|
|
/* We've received a channel_announce for a channel attached to this node */
|
|
|
|
static bool node_has_public_channels(struct node *node)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(node->chans); i++)
|
|
|
|
if (is_chan_public(node->chans[i]))
|
|
|
|
return true;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(node->chans); i++) {
|
|
|
|
if (!is_chan_public(node->chans[i]))
|
|
|
|
continue;
|
|
|
|
if (is_halfchan_defined(&node->chans[i]->half[0])
|
|
|
|
|| is_halfchan_defined(&node->chans[i]->half[1]))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool remove_channel_from_array(struct chan ***chans, const struct chan *c)
|
2016-08-18 06:55:13 +02:00
|
|
|
{
|
|
|
|
size_t i, n;
|
|
|
|
|
2018-03-02 09:59:16 +01:00
|
|
|
n = tal_count(*chans);
|
2016-08-18 06:55:13 +02:00
|
|
|
for (i = 0; i < n; i++) {
|
2018-03-02 09:59:16 +01:00
|
|
|
if ((*chans)[i] != c)
|
2016-08-18 06:55:13 +02:00
|
|
|
continue;
|
|
|
|
n--;
|
2018-03-02 09:59:16 +01:00
|
|
|
memmove(*chans + i, *chans + i + 1, sizeof(**chans) * (n - i));
|
|
|
|
tal_resize(chans, n);
|
2016-08-18 06:55:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2018-06-08 08:31:55 +02:00
|
|
|
for (size_t i = 0; i < tal_count(node->chans); i++) {
|
|
|
|
if (!is_chan_public(node->chans[i]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (node->chans[i]->channel_announcement_index
|
|
|
|
< node->node_announcement_index)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-08 19:24:41 +02:00
|
|
|
static u64 persistent_broadcast(struct routing_state *rstate, const u8 *msg, u32 timestamp)
|
|
|
|
{
|
|
|
|
u64 index = insert_broadcast(rstate->broadcasts, msg, timestamp);
|
|
|
|
if (index)
|
|
|
|
gossip_store_add(rstate->store, msg);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:31:55 +02:00
|
|
|
static void remove_chan_from_node(struct routing_state *rstate,
|
|
|
|
struct node *node, const struct chan *chan)
|
|
|
|
{
|
|
|
|
if (!remove_channel_from_array(&node->chans, chan))
|
2017-08-28 18:03:01 +02:00
|
|
|
abort();
|
2018-03-02 09:59:16 +01:00
|
|
|
|
2018-06-08 08:31:55 +02:00
|
|
|
/* Last channel? Simply delete node (and associated announce) */
|
|
|
|
if (tal_count(node->chans) == 0) {
|
|
|
|
tal_free(node);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!node->node_announcement_index)
|
|
|
|
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)) {
|
|
|
|
broadcast_del(rstate->broadcasts, node->node_announcement_index,
|
|
|
|
node->node_announcement);
|
|
|
|
node->node_announcement_index = 0;
|
|
|
|
} else if (node_announce_predates_channels(node)) {
|
|
|
|
/* 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 */
|
|
|
|
broadcast_del(rstate->broadcasts, node->node_announcement_index,
|
|
|
|
node->node_announcement);
|
2018-06-08 19:24:41 +02:00
|
|
|
node->node_announcement_index = persistent_broadcast(
|
|
|
|
rstate, node->node_announcement, node->last_timestamp);
|
2018-06-08 08:31:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_chan(struct chan *chan, struct routing_state *rstate)
|
|
|
|
{
|
|
|
|
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);
|
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-03-02 09:59:17 +01:00
|
|
|
int idx)
|
2016-12-12 14:55:46 +01:00
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
struct half_chan *c = &chan->half[idx];
|
2018-03-02 09:59:16 +01:00
|
|
|
|
2018-05-10 14:22:37 +02:00
|
|
|
c->channel_update = NULL;
|
2018-03-02 09:59:16 +01:00
|
|
|
c->unroutable_until = 0;
|
|
|
|
c->flags = idx;
|
2018-03-02 09:59:17 +01:00
|
|
|
/* We haven't seen channel_update: make it halfway to prune time,
|
2018-03-02 09:59:17 +01:00
|
|
|
* which should be older than any update we'd see. */
|
2018-03-02 09:59:17 +01:00
|
|
|
c->last_timestamp = time_now().ts.tv_sec - rstate->prune_timeout/2;
|
2016-12-12 14:55:46 +01:00
|
|
|
}
|
|
|
|
|
2018-05-18 07:49:08 +02:00
|
|
|
static void bad_gossip_order(const u8 *msg, const char *source,
|
|
|
|
const char *details)
|
2018-05-17 07:09:59 +02:00
|
|
|
{
|
2018-05-18 07:49:08 +02:00
|
|
|
status_trace("Bad gossip order from %s: %s before announcement %s",
|
|
|
|
source, wire_type_name(fromwire_peektype(msg)),
|
2018-05-17 07:09:59 +02:00
|
|
|
details);
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *new_chan(struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *scid,
|
|
|
|
const struct pubkey *id1,
|
|
|
|
const struct pubkey *id2)
|
2018-03-02 09:59:17 +01:00
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan = tal(rstate, struct chan);
|
2018-03-02 09:59:17 +01:00
|
|
|
int n1idx = pubkey_idx(id1, id2);
|
2018-03-02 09:59:17 +01:00
|
|
|
size_t n;
|
2018-03-02 09:59:17 +01:00
|
|
|
struct node *n1, *n2;
|
|
|
|
|
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;
|
|
|
|
chan->txout_script = NULL;
|
2018-05-10 14:22:37 +02:00
|
|
|
chan->channel_announce = NULL;
|
2018-06-08 08:31:49 +02:00
|
|
|
chan->channel_announcement_index = 0;
|
2018-03-05 23:32:04 +01:00
|
|
|
chan->satoshis = 0;
|
2018-03-02 09:59:17 +01:00
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
n = tal_count(n2->chans);
|
|
|
|
tal_resize(&n2->chans, n+1);
|
|
|
|
n2->chans[n] = chan;
|
|
|
|
n = tal_count(n1->chans);
|
|
|
|
tal_resize(&n1->chans, n+1);
|
|
|
|
n1->chans[n] = 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
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
uintmap_add(&rstate->chanmap, scid->u64, chan);
|
2018-03-02 09:59:17 +01:00
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
tal_add_destructor2(chan, destroy_chan, rstate);
|
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. */
|
|
|
|
#define INFINITE 0x3FFFFFFFFFFFFFFFULL
|
|
|
|
|
|
|
|
static void clear_bfg(struct node_map *nodes)
|
|
|
|
{
|
|
|
|
struct node *n;
|
|
|
|
struct node_map_iter it;
|
|
|
|
|
|
|
|
for (n = node_map_first(nodes, &it); n; n = node_map_next(nodes, &it)) {
|
|
|
|
size_t i;
|
2016-09-06 09:17:48 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(n->bfg); i++) {
|
2016-06-28 23:19:21 +02:00
|
|
|
n->bfg[i].total = INFINITE;
|
2016-09-06 09:17:48 +02:00
|
|
|
n->bfg[i].risk = 0;
|
|
|
|
}
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
static u64 connection_fee(const struct half_chan *c, u64 msatoshi)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
2017-12-18 05:15:38 +01:00
|
|
|
u64 fee;
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2017-12-18 05:15:40 +01:00
|
|
|
assert(msatoshi < MAX_MSATOSHI);
|
|
|
|
assert(c->proportional_fee < MAX_PROPORTIONAL_FEE);
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
fee = (c->proportional_fee * msatoshi) / 1000000;
|
|
|
|
/* This can't overflow: c->base_fee is a u32 */
|
|
|
|
return c->base_fee + fee;
|
|
|
|
}
|
|
|
|
|
2016-09-06 09:17:48 +02:00
|
|
|
/* Risk of passing through this channel. We insert a tiny constant here
|
|
|
|
* in order to prefer shorter routes, all things equal. */
|
2017-12-18 05:15:38 +01:00
|
|
|
static u64 risk_fee(u64 amount, u32 delay, double riskfactor)
|
2016-09-06 09:17:48 +02:00
|
|
|
{
|
2017-12-18 05:15:41 +01:00
|
|
|
return 1 + amount * delay * riskfactor;
|
2016-09-06 09:17:48 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
/* We track totals, rather than costs. That's because the fee depends
|
|
|
|
* on the current amount passing through. */
|
2018-03-02 09:59:16 +01:00
|
|
|
static void bfg_one_edge(struct node *node,
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan, int idx,
|
2018-03-04 03:23:32 +01:00
|
|
|
double riskfactor,
|
2018-02-23 01:00:00 +01:00
|
|
|
double fuzz, const struct siphash_seed *base_seed)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
|
|
|
size_t h;
|
2018-02-16 04:50:51 +01:00
|
|
|
double fee_scale = 1.0;
|
2018-03-04 03:26:59 +01:00
|
|
|
const struct half_chan *c = &chan->half[idx];
|
2018-02-16 04:50:51 +01:00
|
|
|
|
|
|
|
if (fuzz != 0.0) {
|
2018-03-04 03:23:32 +01:00
|
|
|
u64 h = siphash24(base_seed, &chan->scid, sizeof(chan->scid));
|
2018-02-16 04:50:51 +01:00
|
|
|
|
|
|
|
/* Scale fees for this channel */
|
2018-02-23 01:00:00 +01:00
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
fee_scale = 1.0 + (2.0 * fuzz * h / UINT64_MAX) - fuzz;
|
2018-02-16 04:50:51 +01:00
|
|
|
}
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
for (h = 0; h < ROUTING_MAX_HOPS; h++) {
|
2018-03-04 03:23:32 +01:00
|
|
|
struct node *src;
|
2016-06-28 23:19:21 +02:00
|
|
|
/* FIXME: Bias against smaller channels. */
|
2017-12-18 05:15:38 +01:00
|
|
|
u64 fee;
|
2017-08-31 04:08:12 +02:00
|
|
|
u64 risk;
|
|
|
|
|
|
|
|
if (node->bfg[h].total == INFINITE)
|
|
|
|
continue;
|
|
|
|
|
2018-02-16 04:50:51 +01:00
|
|
|
fee = connection_fee(c, node->bfg[h].total) * fee_scale;
|
2017-08-31 04:08:12 +02:00
|
|
|
risk = node->bfg[h].risk + risk_fee(node->bfg[h].total + fee,
|
|
|
|
c->delay, riskfactor);
|
2017-12-18 05:15:40 +01:00
|
|
|
|
|
|
|
if (node->bfg[h].total + fee + risk >= MAX_MSATOSHI) {
|
|
|
|
SUPERVERBOSE("...extreme %"PRIu64
|
|
|
|
" + fee %"PRIu64
|
|
|
|
" + risk %"PRIu64" ignored",
|
|
|
|
node->bfg[h].total, fee, risk);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:23:32 +01:00
|
|
|
/* nodes[0] is src for connections[0] */
|
|
|
|
src = chan->nodes[idx];
|
2017-12-18 05:15:38 +01:00
|
|
|
if (node->bfg[h].total + fee + risk
|
2018-03-04 03:23:32 +01:00
|
|
|
< src->bfg[h+1].total + src->bfg[h+1].risk) {
|
2017-12-18 05:15:31 +01:00
|
|
|
SUPERVERBOSE("...%s can reach here in hoplen %zu total %"PRIu64,
|
2017-08-31 04:08:12 +02:00
|
|
|
type_to_string(trc, struct pubkey,
|
2018-03-04 03:23:32 +01:00
|
|
|
&src->id),
|
2017-08-31 04:08:12 +02:00
|
|
|
h, node->bfg[h].total + fee);
|
2018-03-04 03:23:32 +01:00
|
|
|
src->bfg[h+1].total = node->bfg[h].total + fee;
|
|
|
|
src->bfg[h+1].risk = risk;
|
|
|
|
src->bfg[h+1].prev = chan;
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
/* Determine if the given half_chan is routable */
|
|
|
|
static bool hc_is_routable(const struct half_chan *hc, time_t now)
|
2018-02-02 02:06:26 +01:00
|
|
|
{
|
2018-05-10 16:00:38 +02:00
|
|
|
return is_halfchan_enabled(hc) && hc->unroutable_until < now;
|
2018-02-02 02:06:26 +01:00
|
|
|
}
|
|
|
|
|
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 **
|
2017-03-15 12:44:01 +01:00
|
|
|
find_route(const tal_t *ctx, struct routing_state *rstate,
|
|
|
|
const struct pubkey *from, const struct pubkey *to, u64 msatoshi,
|
2018-02-16 04:50:51 +01:00
|
|
|
double riskfactor,
|
2018-02-23 01:00:00 +01:00
|
|
|
double fuzz, const struct siphash_seed *base_seed,
|
2018-03-04 03:26:54 +01:00
|
|
|
u64 *fee)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan **route;
|
2016-06-28 23:19:21 +02:00
|
|
|
struct node *n, *src, *dst;
|
|
|
|
struct node_map_iter it;
|
|
|
|
int runs, i, best;
|
2018-02-02 02:42:15 +01:00
|
|
|
/* Call time_now() once at the start, so that our tight loop
|
|
|
|
* does not keep calling into operating system for the
|
|
|
|
* current time */
|
|
|
|
time_t now = time_now().ts.tv_sec;
|
2016-06-28 23:19:21 +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. */
|
2017-01-19 23:46:07 +01:00
|
|
|
dst = get_node(rstate, from);
|
|
|
|
src = get_node(rstate, to);
|
2016-10-23 11:57:50 +02:00
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
if (!src) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_info("find_route: cannot find %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, to));
|
2016-06-28 23:19:21 +02:00
|
|
|
return NULL;
|
2017-08-07 13:10:10 +02:00
|
|
|
} else if (!dst) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_info("find_route: cannot find myself (%s)",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, to));
|
2017-08-07 13:10:10 +02:00
|
|
|
return NULL;
|
2016-10-23 11:57:50 +02:00
|
|
|
} else if (dst == src) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_info("find_route: this is %s, refusing to create empty route",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, to));
|
2016-10-23 11:57:50 +02:00
|
|
|
return NULL;
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 05:15:40 +01:00
|
|
|
if (msatoshi >= MAX_MSATOSHI) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_info("find_route: can't route huge amount %"PRIu64,
|
|
|
|
msatoshi);
|
2017-12-18 05:15:40 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-12-21 12:10:24 +01:00
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
/* Reset all the information. */
|
2017-01-19 23:46:07 +01:00
|
|
|
clear_bfg(rstate->nodes);
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
/* Bellman-Ford-Gibson: like Bellman-Ford, but keep values for
|
|
|
|
* every path length. */
|
|
|
|
src->bfg[0].total = msatoshi;
|
2016-09-06 09:17:48 +02:00
|
|
|
src->bfg[0].risk = 0;
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
for (runs = 0; runs < ROUTING_MAX_HOPS; runs++) {
|
2017-12-18 05:15:31 +01:00
|
|
|
SUPERVERBOSE("Run %i", runs);
|
2016-06-28 23:19:21 +02:00
|
|
|
/* Run through every edge. */
|
2017-01-19 23:46:07 +01:00
|
|
|
for (n = node_map_first(rstate->nodes, &it);
|
2016-06-28 23:19:21 +02:00
|
|
|
n;
|
2017-01-19 23:46:07 +01:00
|
|
|
n = node_map_next(rstate->nodes, &it)) {
|
2018-03-04 03:26:59 +01:00
|
|
|
size_t num_edges = tal_count(n->chans);
|
2016-06-28 23:19:21 +02:00
|
|
|
for (i = 0; i < num_edges; i++) {
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan = n->chans[i];
|
|
|
|
int idx = half_chan_to(n, chan);
|
2018-03-04 03:23:32 +01:00
|
|
|
|
2017-12-18 05:15:31 +01:00
|
|
|
SUPERVERBOSE("Node %s edge %i/%zu",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2017-08-31 04:08:12 +02:00
|
|
|
&n->id),
|
|
|
|
i, num_edges);
|
2018-03-02 09:59:16 +01:00
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
if (!hc_is_routable(&chan->half[idx], now)) {
|
2018-02-02 02:06:26 +01:00
|
|
|
SUPERVERBOSE("...unroutable");
|
2016-12-14 23:42:06 +01:00
|
|
|
continue;
|
2017-08-31 04:08:12 +02:00
|
|
|
}
|
2018-03-04 03:23:32 +01:00
|
|
|
bfg_one_edge(n, chan, idx,
|
2018-03-02 09:59:16 +01:00
|
|
|
riskfactor, fuzz, base_seed);
|
2017-12-18 05:15:31 +01:00
|
|
|
SUPERVERBOSE("...done");
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
best = 0;
|
|
|
|
for (i = 1; i <= ROUTING_MAX_HOPS; i++) {
|
|
|
|
if (dst->bfg[i].total < dst->bfg[best].total)
|
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No route? */
|
|
|
|
if (dst->bfg[best].total >= INFINITE) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("find_route: No route to %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, to));
|
2016-06-28 23:19:21 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:54 +01:00
|
|
|
/* We (dst) don't charge ourselves fees, so skip first hop */
|
|
|
|
n = other_node(dst, dst->bfg[best].prev);
|
|
|
|
*fee = n->bfg[best-1].total - msatoshi;
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2018-03-04 03:26:54 +01:00
|
|
|
/* Lay out route */
|
2018-03-04 03:26:59 +01:00
|
|
|
route = tal_arr(ctx, struct chan *, best);
|
2016-11-04 01:47:04 +01:00
|
|
|
for (i = 0, n = dst;
|
|
|
|
i < best;
|
2018-03-04 03:23:32 +01:00
|
|
|
n = other_node(n, n->bfg[best-i].prev), i++) {
|
2018-03-04 03:26:54 +01:00
|
|
|
route[i] = n->bfg[best-i].prev;
|
2016-11-04 01:47:04 +01:00
|
|
|
}
|
|
|
|
assert(n == src);
|
|
|
|
|
2018-03-04 03:26:54 +01:00
|
|
|
return route;
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
2016-06-28 23:19:21 +02:00
|
|
|
|
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,
|
|
|
|
const struct pubkey *node_key,
|
|
|
|
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;
|
|
|
|
sha256_double(&hash, update + offset, tal_len(update) - offset);
|
|
|
|
|
2018-03-08 05:10:33 +01:00
|
|
|
if (!check_signed_hash(&hash, node_sig, node_key))
|
|
|
|
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,
|
2018-04-20 10:09:50 +02:00
|
|
|
const struct pubkey *node1_key, const struct pubkey *node2_key,
|
|
|
|
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,
|
|
|
|
tal_len(announcement) - offset);
|
|
|
|
|
2018-03-08 05:10:31 +01:00
|
|
|
if (!check_signed_hash(&hash, node1_sig, node1_key)) {
|
|
|
|
return towire_errorfmt(ctx, NULL,
|
|
|
|
"Bad node_signature_1 %s hash %s"
|
|
|
|
" on node_announcement %s",
|
|
|
|
type_to_string(ctx,
|
|
|
|
secp256k1_ecdsa_signature,
|
|
|
|
node1_sig),
|
|
|
|
type_to_string(ctx,
|
|
|
|
struct sha256_double,
|
|
|
|
&hash),
|
|
|
|
tal_hex(ctx, announcement));
|
|
|
|
}
|
|
|
|
if (!check_signed_hash(&hash, node2_sig, node2_key)) {
|
|
|
|
return towire_errorfmt(ctx, NULL,
|
|
|
|
"Bad node_signature_2 %s hash %s"
|
|
|
|
" on node_announcement %s",
|
|
|
|
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"
|
|
|
|
" on node_announcement %s",
|
|
|
|
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"
|
|
|
|
" on node_announcement %s",
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-02 19:49:12 +01:00
|
|
|
static void add_pending_node_announcement(struct routing_state *rstate, struct pubkey *nodeid)
|
|
|
|
{
|
|
|
|
struct pending_node_announce *pna = tal(rstate, struct pending_node_announce);
|
|
|
|
pna->nodeid = *nodeid;
|
|
|
|
pna->node_announcement = NULL;
|
|
|
|
pna->timestamp = 0;
|
|
|
|
pending_node_map_add(rstate->pending_node_map, pna);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void process_pending_node_announcement(struct routing_state *rstate,
|
2018-03-25 18:30:47 +02:00
|
|
|
struct pubkey *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-03-08 05:10:29 +01:00
|
|
|
u8 *err;
|
2018-02-03 17:40:09 +01:00
|
|
|
SUPERVERBOSE(
|
|
|
|
"Processing deferred node_announcement for node %s",
|
|
|
|
type_to_string(pna, struct pubkey, nodeid));
|
2018-03-08 05:10:29 +01:00
|
|
|
|
|
|
|
/* Should not error, since we processed it before */
|
2018-03-25 18:30:47 +02:00
|
|
|
err = handle_node_announcement(rstate, pna->node_announcement);
|
2018-03-08 05:10:29 +01:00
|
|
|
if (err)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"pending node_announcement %s malformed %s?",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, pna->node_announcement),
|
|
|
|
sanitize_error(tmpctx, err, NULL));
|
2018-02-02 19:49:12 +01:00
|
|
|
}
|
|
|
|
pending_node_map_del(rstate->pending_node_map, pna);
|
|
|
|
tal_free(pna);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct pending_cannouncement *i;
|
|
|
|
|
|
|
|
list_for_each(&rstate->pending_cannouncement, i, list) {
|
|
|
|
if (structeq(scid, &i->short_channel_id))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_pending_cannouncement(struct pending_cannouncement *pending,
|
|
|
|
struct routing_state *rstate)
|
|
|
|
{
|
|
|
|
list_del_from(&rstate->pending_cannouncement, &pending->list);
|
|
|
|
}
|
|
|
|
|
2018-06-04 06:15:25 +02:00
|
|
|
static bool is_local_channel(const struct routing_state *rstate,
|
|
|
|
const struct chan *chan)
|
|
|
|
{
|
|
|
|
return pubkey_eq(&chan->nodes[0]->id, &rstate->local_id)
|
|
|
|
|| pubkey_eq(&chan->nodes[1]->id, &rstate->local_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_channel_announce_to_broadcast(struct routing_state *rstate,
|
2018-06-04 06:24:25 +02:00
|
|
|
struct chan *chan,
|
|
|
|
u32 timestamp)
|
2018-06-04 06:15:25 +02:00
|
|
|
{
|
2018-06-08 19:24:41 +02:00
|
|
|
chan->channel_announcement_index =
|
|
|
|
persistent_broadcast(rstate, chan->channel_announce, timestamp);
|
2018-06-04 06:15:25 +02:00
|
|
|
rstate->local_channel_announced |= is_local_channel(rstate, chan);
|
2018-06-04 06:16:25 +02:00
|
|
|
|
|
|
|
/* If we've been waiting for this, now we can announce node */
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(chan->nodes); i++) {
|
|
|
|
struct node *node = chan->nodes[i];
|
|
|
|
if (!node->node_announcement)
|
|
|
|
continue;
|
2018-06-08 08:31:49 +02:00
|
|
|
if (!node->node_announcement_index) {
|
2018-06-08 19:24:41 +02:00
|
|
|
node->node_announcement_index = persistent_broadcast(
|
|
|
|
rstate, node->node_announcement,
|
|
|
|
node->last_timestamp);
|
2018-06-04 06:16:25 +02:00
|
|
|
}
|
|
|
|
}
|
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,
|
2018-03-22 15:11:24 +01:00
|
|
|
const u8 *msg TAKES, u64 satoshis)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
struct pubkey node_id_1;
|
|
|
|
struct pubkey node_id_2;
|
|
|
|
struct pubkey bitcoin_key_1;
|
|
|
|
struct pubkey bitcoin_key_2;
|
2018-04-11 01:03:35 +02:00
|
|
|
|
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,
|
|
|
|
&scid, &node_id_1, &node_id_2, &bitcoin_key_1, &bitcoin_key_2))
|
|
|
|
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);
|
|
|
|
if (!chan)
|
|
|
|
chan = new_chan(rstate, &scid, &node_id_1, &node_id_2);
|
|
|
|
|
|
|
|
chan->satoshis = satoshis;
|
2018-05-10 16:00:38 +02:00
|
|
|
/* Channel is now public. */
|
2018-05-10 14:22:37 +02:00
|
|
|
chan->channel_announce = tal_dup_arr(chan, u8, msg, tal_len(msg), 0);
|
2018-05-10 09:50:03 +02:00
|
|
|
|
2018-05-31 04:34:27 +02:00
|
|
|
/* Clear any private updates: new updates will trigger broadcast of
|
|
|
|
* this channel_announce. */
|
2018-05-31 04:31:27 +02:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(chan->half); i++)
|
|
|
|
chan->half[i].channel_update
|
|
|
|
= tal_free(chan->half[i].channel_update);
|
2018-05-10 09:50:03 +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,
|
2018-03-25 18:30:47 +02:00
|
|
|
const struct short_channel_id **scid)
|
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);
|
2018-01-12 18:55:29 +01:00
|
|
|
pending->updates[0] = NULL;
|
|
|
|
pending->updates[1] = NULL;
|
2018-01-04 12:40:58 +01:00
|
|
|
pending->announce = tal_dup_arr(pending, u8,
|
|
|
|
announce, tal_len(announce), 0);
|
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,
|
|
|
|
&pending->node_id_1,
|
|
|
|
&pending->node_id_2,
|
|
|
|
&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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
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
|
|
|
|
2018-01-12 15:10:21 +01:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if `features` field contains _unknown even bits_:
|
|
|
|
* - MUST NOT parse the remainder of the message.
|
|
|
|
* - MAY discard the message altogether.
|
|
|
|
* - SHOULD NOT connect to the node.
|
|
|
|
* - MAY forward `node_announcement`s that contain an _unknown_
|
|
|
|
* `features` _bit_, regardless of if it has parsed the announcement
|
|
|
|
* or not.
|
2018-01-12 15:10:21 +01:00
|
|
|
*/
|
2018-03-13 16:41:55 +01:00
|
|
|
if (!features_supported(features, NULL)) {
|
2018-01-12 15:10:21 +01:00
|
|
|
status_trace("Ignoring channel announcement, unsupported features %s.",
|
|
|
|
tal_hex(pending, features));
|
2018-03-13 00:06:00 +01:00
|
|
|
goto ignored;
|
2018-01-12 15:10:21 +01:00
|
|
|
}
|
|
|
|
|
2017-08-22 07:25:01 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The final node:
|
|
|
|
*...
|
|
|
|
* - if the specified `chain_hash` is unknown to the receiver:
|
|
|
|
* - MUST ignore the message.
|
2017-08-22 07:25:01 +02:00
|
|
|
*/
|
|
|
|
if (!structeq(&chain_hash, &rstate->chain_hash)) {
|
2017-12-21 10:53:37 +01:00
|
|
|
status_trace(
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-02-01 03:41:19 +01:00
|
|
|
status_trace("Received channel_announcement for channel %s",
|
|
|
|
type_to_string(pending, 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 */
|
|
|
|
add_pending_node_announcement(rstate, &pending->node_id_1);
|
|
|
|
add_pending_node_announcement(rstate, &pending->node_id_2);
|
|
|
|
|
2018-03-02 09:59:13 +01:00
|
|
|
list_add_tail(&rstate->pending_cannouncement, &pending->list);
|
|
|
|
tal_add_destructor2(pending, destroy_pending_cannouncement, rstate);
|
|
|
|
|
2018-03-13 00:06:00 +01:00
|
|
|
/* Success */
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void process_pending_channel_update(struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *scid,
|
2018-03-25 18:30:47 +02:00
|
|
|
const u8 *cupdate)
|
2018-03-13 00:06:00 +01:00
|
|
|
{
|
|
|
|
u8 *err;
|
|
|
|
|
|
|
|
if (!cupdate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* FIXME: We don't remember who sent us updates, so can't error them */
|
2018-05-18 07:49:08 +02:00
|
|
|
err = handle_channel_update(rstate, cupdate, "pending update");
|
2018-03-13 00:06:00 +01:00
|
|
|
if (err) {
|
|
|
|
status_trace("Pending channel_update for %s: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
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
|
|
|
|
2018-06-04 06:15:25 +02:00
|
|
|
void handle_pending_cannouncement(struct routing_state *rstate,
|
2018-01-04 12:40:58 +01:00
|
|
|
const struct short_channel_id *scid,
|
2018-03-05 23:32:04 +01:00
|
|
|
const u64 satoshis,
|
2018-01-04 12:40:58 +01:00
|
|
|
const u8 *outscript)
|
|
|
|
{
|
|
|
|
const u8 *s;
|
|
|
|
struct pending_cannouncement *pending;
|
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)
|
2018-06-04 06:15:25 +02:00
|
|
|
return;
|
2018-01-04 12:40:58 +01:00
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The final node:
|
|
|
|
*...
|
|
|
|
* - if the `short_channel_id`'s output... is spent:
|
|
|
|
* - MUST ignore the message.
|
2018-01-04 12:40:58 +01:00
|
|
|
*/
|
|
|
|
if (tal_len(outscript) == 0) {
|
2018-02-01 03:41:19 +01:00
|
|
|
status_trace("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);
|
2018-06-04 06:15:25 +02:00
|
|
|
return;
|
2018-01-04 12:40:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The final node:
|
|
|
|
*...
|
|
|
|
* - 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)) {
|
|
|
|
status_trace("channel_announcement: txout %s expectes %s, got %s",
|
2018-02-01 03:41:19 +01:00
|
|
|
type_to_string(pending, struct short_channel_id,
|
|
|
|
scid),
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, s), tal_hex(tmpctx, outscript));
|
2018-01-04 12:40:58 +01:00
|
|
|
tal_free(pending);
|
2018-06-04 06:15:25 +02:00
|
|
|
return;
|
2017-04-03 05:58:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-11 01:03:35 +02:00
|
|
|
if (!routing_add_channel_announcement(rstate, pending->announce, satoshis))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Could not add channel_announcement");
|
2018-03-18 15:04:11 +01:00
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
/* Did we have an update waiting? If so, apply now. */
|
2018-03-25 18:30:47 +02:00
|
|
|
process_pending_channel_update(rstate, scid, pending->updates[0]);
|
|
|
|
process_pending_channel_update(rstate, scid, pending->updates[1]);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2018-03-25 18:30:47 +02:00
|
|
|
process_pending_node_announcement(rstate, &pending->node_id_1);
|
|
|
|
process_pending_node_announcement(rstate, &pending->node_id_2);
|
2018-02-02 19:49:12 +01:00
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
tal_free(pending);
|
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,
|
|
|
|
const u8 direction)
|
2018-01-04 12:40:58 +01:00
|
|
|
{
|
2018-02-27 20:58:20 +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]) {
|
|
|
|
status_trace("Replacing existing update");
|
|
|
|
tal_free(pending->updates[direction]);
|
|
|
|
}
|
|
|
|
pending->updates[direction] = tal_dup_arr(pending, u8, update, tal_len(update), 0);
|
|
|
|
pending->update_timestamps[direction] = timestamp;
|
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-05-10 16:00:38 +02:00
|
|
|
u16 flags,
|
2018-05-10 16:00:05 +02:00
|
|
|
u64 timestamp,
|
|
|
|
u32 htlc_minimum_msat)
|
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;
|
|
|
|
c->htlc_minimum_msat = htlc_minimum_msat;
|
|
|
|
c->base_fee = base_fee;
|
|
|
|
c->proportional_fee = proportional_fee;
|
2018-05-10 16:00:38 +02:00
|
|
|
c->flags = flags;
|
2018-03-02 09:59:17 +01:00
|
|
|
c->last_timestamp = timestamp;
|
2018-05-10 16:00:38 +02:00
|
|
|
assert((c->flags & ROUTING_FLAGS_DIRECTION) == idx);
|
2018-03-02 09:59:17 +01:00
|
|
|
|
|
|
|
/* If it was temporarily unroutable, re-enable */
|
|
|
|
c->unroutable_until = 0;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (c->proportional_fee >= MAX_PROPORTIONAL_FEE) {
|
|
|
|
status_trace("Channel %s(%d) massive proportional fee %u:"
|
|
|
|
" disabling.",
|
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),
|
|
|
|
idx,
|
|
|
|
c->proportional_fee);
|
2018-05-10 16:00:38 +02:00
|
|
|
c->flags |= ROUTING_FLAGS_DISABLED;
|
2018-03-02 09:59:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 01:03:35 +02:00
|
|
|
bool routing_add_channel_update(struct routing_state *rstate,
|
2018-03-22 15:11:24 +01:00
|
|
|
const u8 *update TAKES)
|
|
|
|
{
|
|
|
|
secp256k1_ecdsa_signature signature;
|
|
|
|
struct short_channel_id short_channel_id;
|
|
|
|
u32 timestamp;
|
|
|
|
u16 flags;
|
|
|
|
u16 expiry;
|
|
|
|
u64 htlc_minimum_msat;
|
|
|
|
u32 fee_base_msat;
|
|
|
|
u32 fee_proportional_millionths;
|
|
|
|
struct bitcoin_blkid chain_hash;
|
|
|
|
struct chan *chan;
|
|
|
|
u8 direction;
|
2018-05-31 04:34:27 +02:00
|
|
|
bool have_broadcast_announce;
|
2018-03-22 15:11:24 +01:00
|
|
|
|
2018-04-11 01:03:35 +02:00
|
|
|
if (!fromwire_channel_update(update, &signature, &chain_hash,
|
|
|
|
&short_channel_id, ×tamp, &flags,
|
|
|
|
&expiry, &htlc_minimum_msat, &fee_base_msat,
|
|
|
|
&fee_proportional_millionths))
|
|
|
|
return false;
|
2018-03-22 15:11:24 +01:00
|
|
|
chan = get_channel(rstate, &short_channel_id);
|
2018-04-11 01:03:35 +02:00
|
|
|
if (!chan)
|
|
|
|
return false;
|
|
|
|
|
2018-05-31 04:34:27 +02:00
|
|
|
/* We broadcast announce once we have one update */
|
|
|
|
have_broadcast_announce = is_halfchan_defined(&chan->half[0])
|
|
|
|
|| is_halfchan_defined(&chan->half[1]);
|
|
|
|
|
2018-03-22 15:11:24 +01:00
|
|
|
direction = flags & 0x1;
|
|
|
|
set_connection_values(chan, direction, fee_base_msat,
|
|
|
|
fee_proportional_millionths, expiry,
|
2018-05-10 16:00:38 +02:00
|
|
|
flags, timestamp, htlc_minimum_msat);
|
2018-03-22 15:11:24 +01:00
|
|
|
|
2018-05-10 14:22:37 +02:00
|
|
|
/* Replace any old one. */
|
|
|
|
tal_free(chan->half[direction].channel_update);
|
|
|
|
chan->half[direction].channel_update
|
|
|
|
= tal_dup_arr(chan, u8, update, tal_len(update), 0);
|
|
|
|
|
2018-05-10 09:50:03 +02:00
|
|
|
/* For private channels, we get updates without an announce: don't
|
|
|
|
* broadcast them! */
|
2018-05-10 14:22:37 +02:00
|
|
|
if (!chan->channel_announce)
|
2018-05-10 09:50:03 +02:00
|
|
|
return true;
|
|
|
|
|
2018-05-31 04:34:27 +02:00
|
|
|
/* BOLT #7:
|
2018-06-04 06:24:25 +02:00
|
|
|
* - MUST consider the `timestamp` of the `channel_announcement` to be
|
|
|
|
* the `timestamp` of a corresponding `channel_update`.
|
2018-05-31 04:34:27 +02:00
|
|
|
* - MUST consider whether to send the `channel_announcement` after
|
|
|
|
* receiving the first corresponding `channel_update`.
|
|
|
|
*/
|
|
|
|
if (!have_broadcast_announce)
|
2018-06-04 06:24:25 +02:00
|
|
|
add_channel_announce_to_broadcast(rstate, chan, timestamp);
|
2018-05-31 04:34:27 +02:00
|
|
|
|
2018-06-08 19:24:41 +02:00
|
|
|
persistent_broadcast(rstate, chan->half[direction].channel_update,
|
|
|
|
timestamp);
|
2018-04-11 01:03:35 +02:00
|
|
|
return true;
|
2018-03-22 15:11:24 +01:00
|
|
|
}
|
|
|
|
|
2018-05-18 07:49:08 +02:00
|
|
|
u8 *handle_channel_update(struct routing_state *rstate, const u8 *update,
|
|
|
|
const char *source)
|
2017-02-01 15:09:26 +01:00
|
|
|
{
|
|
|
|
u8 *serialized;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct half_chan *c;
|
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;
|
|
|
|
u16 flags;
|
|
|
|
u16 expiry;
|
2017-06-06 01:49:10 +02:00
|
|
|
u64 htlc_minimum_msat;
|
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-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
2018-01-12 18:55:29 +01:00
|
|
|
u8 direction;
|
2017-12-11 04:16:50 +01:00
|
|
|
size_t len = tal_len(update);
|
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,
|
2017-02-01 15:09:26 +01:00
|
|
|
×tamp, &flags, &expiry,
|
|
|
|
&htlc_minimum_msat, &fee_base_msat,
|
|
|
|
&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-01-12 18:55:29 +01:00
|
|
|
direction = flags & 0x1;
|
2017-02-01 15:09:26 +01:00
|
|
|
|
2017-08-22 07:25:01 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The final node:
|
|
|
|
*...
|
|
|
|
* - if the specified `chain_hash` value is unknown (meaning it isn't
|
|
|
|
* active on the specified chain):
|
|
|
|
* - MUST ignore the channel update.
|
|
|
|
*/
|
2017-08-22 07:25:01 +02:00
|
|
|
if (!structeq(&chain_hash, &rstate->chain_hash)) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Received channel_update for unknown chain %s",
|
2017-12-18 07:44:10 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_blkid,
|
2017-08-28 18:03:01 +02:00
|
|
|
&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
|
|
|
|
2018-03-01 10:22:28 +01:00
|
|
|
chan = get_channel(rstate, &short_channel_id);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2018-03-02 09:59:13 +01:00
|
|
|
/* Optimization: only check for pending if not public */
|
2018-05-10 16:00:38 +02:00
|
|
|
if (!chan || !is_chan_public(chan)) {
|
2018-03-02 09:59:13 +01:00
|
|
|
struct pending_cannouncement *pending;
|
|
|
|
|
|
|
|
pending = find_pending_cannouncement(rstate, &short_channel_id);
|
|
|
|
if (pending) {
|
|
|
|
update_pending(pending,
|
|
|
|
timestamp, serialized, direction);
|
2018-03-08 05:10:33 +01:00
|
|
|
return NULL;
|
2018-03-02 09:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!chan) {
|
2018-05-17 07:09:59 +02:00
|
|
|
bad_gossip_order(serialized,
|
2018-05-18 07:49:08 +02:00
|
|
|
source,
|
2018-05-17 07:09:59 +02:00
|
|
|
tal_fmt(tmpctx, "%s(%u)",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
flags));
|
2018-03-08 05:10:33 +01:00
|
|
|
return NULL;
|
2018-03-02 09:59:13 +01:00
|
|
|
}
|
2018-02-27 20:58:20 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 06:25:25 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - if the `timestamp` is unreasonably far in the future:
|
|
|
|
* - MAY discard the `channel_announcement`.
|
|
|
|
*/
|
|
|
|
if (timestamp > time_now().ts.tv_sec + rstate->prune_timeout) {
|
|
|
|
status_debug("Received channel_update for %s with far time %u",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
timestamp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: we can consider old timestamps a case of "instant prune" too */
|
|
|
|
if (timestamp < time_now().ts.tv_sec - rstate->prune_timeout) {
|
|
|
|
status_debug("Received channel_update for %s with old time %u",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
timestamp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
c = &chan->half[direction];
|
2017-02-01 15:09:26 +01:00
|
|
|
|
2018-05-21 06:36:55 +02:00
|
|
|
if (is_halfchan_defined(c) && timestamp <= c->last_timestamp) {
|
|
|
|
/* They're not supposed to do this! */
|
|
|
|
if (timestamp == c->last_timestamp
|
|
|
|
&& !memeq(c->channel_update, tal_len(c->channel_update),
|
|
|
|
serialized, tal_len(serialized))) {
|
|
|
|
status_unusual("Bad gossip repeated timestamp for %s(%u): %s then %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
flags,
|
|
|
|
tal_hex(tmpctx, c->channel_update),
|
|
|
|
tal_hex(tmpctx, serialized));
|
|
|
|
}
|
2018-03-02 09:59:17 +01:00
|
|
|
SUPERVERBOSE("Ignoring outdated update.");
|
2018-03-08 05:10:33 +01:00
|
|
|
return NULL;
|
2018-02-27 20:58:20 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 05:10:33 +01:00
|
|
|
err = check_channel_update(rstate, &chan->nodes[direction]->id,
|
|
|
|
&signature, serialized);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-06-04 06:15:25 +02:00
|
|
|
status_trace("Received channel_update for channel %s(%d) now %s (from %s)",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
2018-02-02 13:55:54 +01:00
|
|
|
&short_channel_id),
|
2018-02-07 01:58:36 +01:00
|
|
|
flags & 0x01,
|
2018-06-04 06:15:25 +02:00
|
|
|
flags & ROUTING_FLAGS_DISABLED ? "DISABLED" : "ACTIVE",
|
|
|
|
source);
|
2018-02-02 13:55:54 +01:00
|
|
|
|
2018-04-11 01:03:35 +02:00
|
|
|
if (!routing_add_channel_update(rstate, serialized))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Failed adding channel_update");
|
2018-05-04 00:13:12 +02:00
|
|
|
|
2018-03-08 05:10:33 +01:00
|
|
|
return NULL;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
2017-10-23 06:17:38 +02:00
|
|
|
static struct wireaddr *read_addresses(const tal_t *ctx, const u8 *ser)
|
2017-05-08 21:26:46 +02:00
|
|
|
{
|
|
|
|
const u8 *cursor = ser;
|
|
|
|
size_t max = tal_len(ser);
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr *wireaddrs = tal_arr(ctx, struct wireaddr, 0);
|
2017-05-08 21:26:46 +02:00
|
|
|
int numaddrs = 0;
|
2017-09-01 06:18:55 +02:00
|
|
|
while (cursor && cursor < ser + max) {
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr wireaddr;
|
2017-09-01 06:18:55 +02:00
|
|
|
|
2017-10-22 15:33:58 +02:00
|
|
|
/* Skip any padding */
|
|
|
|
while (max && cursor[0] == ADDR_TYPE_PADDING)
|
|
|
|
fromwire_u8(&cursor, &max);
|
|
|
|
|
2017-09-01 06:18:55 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The final node:
|
|
|
|
*...
|
|
|
|
* - SHOULD ignore the first `address descriptor` that does
|
|
|
|
* NOT match the types defined above.
|
2017-09-01 06:18:55 +02:00
|
|
|
*/
|
2017-10-23 06:17:38 +02:00
|
|
|
if (!fromwire_wireaddr(&cursor, &max, &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. */
|
|
|
|
break;
|
2017-05-08 21:26:46 +02:00
|
|
|
}
|
2017-09-01 06:18:55 +02:00
|
|
|
|
2017-10-23 06:17:38 +02:00
|
|
|
tal_resize(&wireaddrs, numaddrs+1);
|
|
|
|
wireaddrs[numaddrs] = wireaddr;
|
2017-09-01 06:18:55 +02:00
|
|
|
numaddrs++;
|
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
|
|
|
}
|
|
|
|
|
2018-03-29 13:29:01 +02:00
|
|
|
bool routing_add_node_announcement(struct routing_state *rstate, const u8 *msg TAKES)
|
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;
|
|
|
|
struct pubkey node_id;
|
|
|
|
u8 rgb_color[3];
|
|
|
|
u8 alias[32];
|
|
|
|
u8 *features, *addresses;
|
|
|
|
struct wireaddr *wireaddrs;
|
2018-05-17 07:08:11 +02:00
|
|
|
|
|
|
|
if (!fromwire_node_announcement(tmpctx, msg,
|
|
|
|
&signature, &features, ×tamp,
|
|
|
|
&node_id, rgb_color, alias,
|
|
|
|
&addresses))
|
|
|
|
return false;
|
2018-04-20 10:09:50 +02:00
|
|
|
|
|
|
|
node = get_node(rstate, &node_id);
|
|
|
|
|
|
|
|
/* May happen if we accepted the node_announcement due to a local
|
2018-05-10 14:22:37 +02:00
|
|
|
* channel, for which we didn't have the announcement yet. */
|
2018-04-20 10:09:50 +02:00
|
|
|
if (node == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wireaddrs = read_addresses(tmpctx, addresses);
|
|
|
|
tal_free(node->addresses);
|
|
|
|
node->addresses = tal_steal(node, wireaddrs);
|
|
|
|
|
|
|
|
node->last_timestamp = timestamp;
|
|
|
|
memcpy(node->rgb_color, rgb_color, 3);
|
|
|
|
tal_free(node->alias);
|
|
|
|
node->alias = tal_dup_arr(node, u8, alias, 32, 0);
|
|
|
|
|
2018-05-10 14:22:37 +02:00
|
|
|
tal_free(node->node_announcement);
|
|
|
|
node->node_announcement = tal_dup_arr(node, u8, msg, tal_len(msg), 0);
|
2018-03-22 15:11:24 +01:00
|
|
|
|
2018-06-04 06:16:25 +02:00
|
|
|
/* We might be waiting for channel_announce to be released. */
|
2018-06-08 08:31:49 +02:00
|
|
|
if (node_has_broadcastable_channels(node)) {
|
2018-06-08 19:24:41 +02:00
|
|
|
node->node_announcement_index = persistent_broadcast(
|
|
|
|
rstate, node->node_announcement, timestamp);
|
2018-06-08 08:31:49 +02:00
|
|
|
}
|
2018-06-04 06:16:25 +02:00
|
|
|
return true;
|
2018-04-11 01:03:12 +02:00
|
|
|
}
|
|
|
|
|
2018-03-25 18:30:47 +02:00
|
|
|
u8 *handle_node_announcement(struct routing_state *rstate, const u8 *node_ann)
|
2017-02-01 15:09:26 +01:00
|
|
|
{
|
|
|
|
u8 *serialized;
|
|
|
|
struct sha256_double hash;
|
|
|
|
struct node *node;
|
|
|
|
secp256k1_ecdsa_signature signature;
|
|
|
|
u32 timestamp;
|
|
|
|
struct pubkey node_id;
|
|
|
|
u8 rgb_color[3];
|
|
|
|
u8 alias[32];
|
|
|
|
u8 *features, *addresses;
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr *wireaddrs;
|
2018-02-03 17:40:09 +01:00
|
|
|
struct pending_node_announce *pna;
|
2017-12-11 04:16:50 +01:00
|
|
|
size_t len = tal_len(node_ann);
|
2018-03-29 13:29:01 +02:00
|
|
|
bool applied;
|
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,
|
|
|
|
&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
|
|
|
}
|
|
|
|
|
2018-01-12 15:10:21 +01:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The final node:
|
|
|
|
*...
|
|
|
|
* - if `features` field contains _unknown even bits_:
|
|
|
|
* - MUST NOT parse the remainder of the message.
|
|
|
|
* - MAY discard the message altogether.
|
|
|
|
* - SHOULD NOT connect to the node.
|
2018-01-12 15:10:21 +01:00
|
|
|
*/
|
2018-03-13 16:41:55 +01:00
|
|
|
if (!features_supported(features, NULL)) {
|
2018-02-02 13:55:54 +01:00
|
|
|
status_trace("Ignoring node announcement for node %s, unsupported features %s.",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &node_id),
|
2018-01-12 15:10:21 +01:00
|
|
|
tal_hex(tmpctx, features));
|
2018-03-08 05:10:26 +01:00
|
|
|
return NULL;
|
2018-01-12 15:10:21 +01:00
|
|
|
}
|
|
|
|
|
2017-02-01 15:09:26 +01:00
|
|
|
sha256_double(&hash, serialized + 66, tal_count(serialized) - 66);
|
|
|
|
if (!check_signed_hash(&hash, &signature, &node_id)) {
|
2018-03-08 05:10:26 +01:00
|
|
|
/* 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
|
|
|
|
* `alias`):
|
|
|
|
* - SHOULD fail the connection.
|
|
|
|
* - MUST NOT process the message further.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Beyond this point it's not malformed, so safe if we make it
|
|
|
|
* pending and requeue later. */
|
2017-02-01 15:09:26 +01:00
|
|
|
node = get_node(rstate, &node_id);
|
2018-04-11 01:03:24 +02:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
if (!node || !node_has_public_channels(node)) {
|
|
|
|
/* Check if we are currently verifying the txout for a
|
|
|
|
* matching channel */
|
|
|
|
pna = pending_node_map_get(rstate->pending_node_map,
|
2018-07-04 07:29:56 +02:00
|
|
|
&node_id);
|
2018-04-11 01:03:24 +02:00
|
|
|
if (!pna) {
|
2018-05-18 07:49:08 +02:00
|
|
|
bad_gossip_order(serialized, "node_announcement",
|
2018-05-17 07:09:59 +02:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
&node_id));
|
2018-04-11 01:03:24 +02:00
|
|
|
} else if (pna->timestamp < timestamp) {
|
2018-02-03 17:40:09 +01:00
|
|
|
SUPERVERBOSE(
|
|
|
|
"Deferring node_announcement for node %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &node_id));
|
2018-02-02 19:49:12 +01:00
|
|
|
pna->timestamp = timestamp;
|
|
|
|
tal_free(pna->node_announcement);
|
2018-04-11 01:03:24 +02:00
|
|
|
pna->node_announcement = tal_dup_arr(pna, u8, node_ann,
|
|
|
|
tal_len(node_ann),
|
|
|
|
0);
|
2018-02-02 19:49:12 +01:00
|
|
|
}
|
2018-03-08 05:10:26 +01:00
|
|
|
return NULL;
|
2018-02-02 19:49:12 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 01:03:24 +02:00
|
|
|
if (node->last_timestamp >= timestamp) {
|
2018-02-02 13:55:54 +01:00
|
|
|
SUPERVERBOSE("Ignoring node announcement, it's outdated.");
|
2018-03-08 05:10:26 +01:00
|
|
|
return NULL;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
2018-02-02 13:55:54 +01:00
|
|
|
status_trace("Received node_announcement for node %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &node_id));
|
|
|
|
|
2018-03-29 13:29:01 +02:00
|
|
|
applied = routing_add_node_announcement(rstate, serialized);
|
|
|
|
assert(applied);
|
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
|
|
|
|
2018-03-15 07:10:20 +01:00
|
|
|
struct route_hop *get_route(const tal_t *ctx, struct routing_state *rstate,
|
2017-03-15 12:44:01 +01:00
|
|
|
const struct pubkey *source,
|
|
|
|
const struct pubkey *destination,
|
2018-04-09 15:31:23 +02:00
|
|
|
const u64 msatoshi, double riskfactor,
|
2018-02-16 04:50:51 +01:00
|
|
|
u32 final_cltv,
|
2018-02-23 01:00:00 +01:00
|
|
|
double fuzz, const struct siphash_seed *base_seed)
|
2017-03-15 12:44:01 +01:00
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan **route;
|
2017-03-15 12:44:01 +01:00
|
|
|
u64 total_amount;
|
|
|
|
unsigned int total_delay;
|
2017-12-18 05:15:38 +01:00
|
|
|
u64 fee;
|
2017-03-15 12:44:01 +01:00
|
|
|
struct route_hop *hops;
|
|
|
|
int i;
|
2018-03-04 03:23:32 +01:00
|
|
|
struct node *n;
|
2017-03-15 12:44:01 +01:00
|
|
|
|
2018-03-04 03:26:54 +01:00
|
|
|
route = find_route(ctx, rstate, source, destination, msatoshi,
|
|
|
|
riskfactor / BLOCKS_PER_YEAR / 10000,
|
|
|
|
fuzz, base_seed, &fee);
|
2017-03-15 12:44:01 +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. */
|
2018-03-04 03:26:54 +01:00
|
|
|
hops = tal_arr(ctx, struct route_hop, tal_count(route));
|
2017-03-15 12:44:01 +01:00
|
|
|
total_amount = msatoshi;
|
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);
|
2017-03-15 12:44:01 +01:00
|
|
|
for (i = tal_count(route) - 1; i >= 0; i--) {
|
2018-03-04 03:26:59 +01:00
|
|
|
const struct half_chan *c;
|
|
|
|
int idx = half_chan_to(n, route[i]);
|
|
|
|
c = &route[i]->half[idx];
|
2018-03-04 03:26:54 +01:00
|
|
|
hops[i].channel_id = route[i]->scid;
|
|
|
|
hops[i].nodeid = n->id;
|
|
|
|
hops[i].amount = total_amount;
|
|
|
|
hops[i].delay = total_delay;
|
2018-03-04 03:23:32 +01:00
|
|
|
total_amount += connection_fee(c, total_amount);
|
|
|
|
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
|
|
|
}
|
2018-03-04 03:26:54 +01:00
|
|
|
assert(pubkey_eq(&n->id, source));
|
2017-10-23 06:16:57 +02:00
|
|
|
|
|
|
|
/* FIXME: Shadow route! */
|
2017-03-15 12:44:01 +01:00
|
|
|
return hops;
|
|
|
|
}
|
2018-01-18 00:32:36 +01:00
|
|
|
|
|
|
|
/**
|
2018-03-02 09:59:16 +01:00
|
|
|
* routing_failure_channel_out - Handle routing failure on a specific channel
|
2018-03-02 09:59:17 +01:00
|
|
|
*
|
|
|
|
* If we want to delete the channel, we reparent it to disposal_context.
|
2018-01-18 00:32:36 +01:00
|
|
|
*/
|
2018-03-02 09:59:17 +01:00
|
|
|
static void routing_failure_channel_out(const tal_t *disposal_context,
|
|
|
|
struct node *node,
|
2018-03-02 09:59:16 +01:00
|
|
|
enum onion_type failcode,
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan,
|
2018-03-02 09:59:16 +01:00
|
|
|
time_t now)
|
2018-01-18 00:32:36 +01:00
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
struct half_chan *hc = half_chan_from(node, chan);
|
2018-03-02 09:59:16 +01:00
|
|
|
|
2018-01-18 00:32:36 +01:00
|
|
|
/* BOLT #4:
|
|
|
|
*
|
|
|
|
* - if the PERM bit is NOT set:
|
|
|
|
* - SHOULD restore the channels as it receives new `channel_update`s.
|
|
|
|
*/
|
2018-02-02 08:03:41 +01:00
|
|
|
if (!(failcode & PERM))
|
2018-02-02 02:42:15 +01:00
|
|
|
/* Prevent it for 20 seconds. */
|
2018-03-04 03:26:59 +01:00
|
|
|
hc->unroutable_until = now + 20;
|
2018-01-18 00:32:36 +01:00
|
|
|
else
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Set it up to be pruned. */
|
|
|
|
tal_steal(disposal_context, chan);
|
2018-01-18 00:32:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void routing_failure(struct routing_state *rstate,
|
|
|
|
const struct pubkey *erring_node_pubkey,
|
|
|
|
const struct short_channel_id *scid,
|
2018-01-21 01:36:41 +01:00
|
|
|
enum onion_type failcode,
|
|
|
|
const u8 *channel_update)
|
2018-01-18 00:32:36 +01:00
|
|
|
{
|
|
|
|
struct node *node;
|
2018-02-02 02:42:15 +01:00
|
|
|
time_t now = time_now().ts.tv_sec;
|
2018-01-18 00:32:36 +01:00
|
|
|
|
|
|
|
status_trace("Received routing failure 0x%04x (%s), "
|
|
|
|
"erring node %s, "
|
|
|
|
"channel %s",
|
|
|
|
(int) failcode, onion_type_name(failcode),
|
|
|
|
type_to_string(tmpctx, struct pubkey, erring_node_pubkey),
|
|
|
|
type_to_string(tmpctx, struct short_channel_id, scid));
|
|
|
|
|
|
|
|
node = get_node(rstate, erring_node_pubkey);
|
|
|
|
if (!node) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_unusual("routing_failure: Erring node %s not in map",
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
erring_node_pubkey));
|
2018-01-18 00:32:36 +01:00
|
|
|
/* No node, so no channel, so any channel_update
|
|
|
|
* can also be ignored. */
|
2018-03-15 07:10:20 +01:00
|
|
|
return;
|
2018-01-18 00:32:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #4:
|
|
|
|
*
|
|
|
|
* - if the NODE bit is set:
|
|
|
|
* - SHOULD remove all channels connected with the erring node from
|
|
|
|
* consideration.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if (failcode & NODE) {
|
2018-03-25 21:51:11 +02:00
|
|
|
for (int i = 0; i < tal_count(node->chans); ++i) {
|
2018-03-02 09:59:17 +01:00
|
|
|
routing_failure_channel_out(tmpctx, node, failcode,
|
2018-03-04 03:26:59 +01:00
|
|
|
node->chans[i],
|
2018-03-02 09:59:17 +01:00
|
|
|
now);
|
2018-03-02 09:59:16 +01:00
|
|
|
}
|
2018-01-18 00:32:36 +01:00
|
|
|
} else {
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan = get_channel(rstate, scid);
|
2018-03-02 09:59:16 +01:00
|
|
|
|
|
|
|
if (!chan)
|
|
|
|
status_unusual("routing_failure: "
|
|
|
|
"Channel %s unknown",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct short_channel_id,
|
|
|
|
scid));
|
|
|
|
else if (chan->nodes[0] != node && chan->nodes[1] != node)
|
|
|
|
status_unusual("routing_failure: "
|
|
|
|
"Channel %s does not connect to %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct short_channel_id,
|
|
|
|
scid),
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
erring_node_pubkey));
|
2018-01-18 00:32:36 +01:00
|
|
|
else
|
2018-03-02 09:59:17 +01:00
|
|
|
routing_failure_channel_out(tmpctx,
|
|
|
|
node, failcode, chan, now);
|
2018-01-18 00:32:36 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 01:36:41 +01:00
|
|
|
/* Update the channel if UPDATE failcode. Do
|
|
|
|
* this after deactivating, so that if the
|
|
|
|
* channel_update is newer it will be
|
|
|
|
* reactivated. */
|
|
|
|
if (failcode & UPDATE) {
|
2018-03-08 05:10:33 +01:00
|
|
|
u8 *err;
|
2018-01-21 01:36:41 +01:00
|
|
|
if (tal_len(channel_update) == 0) {
|
2018-02-09 13:50:42 +01:00
|
|
|
/* Suppress UNUSUAL log if local failure */
|
|
|
|
if (structeq(&erring_node_pubkey->pubkey,
|
|
|
|
&rstate->local_id.pubkey))
|
2018-03-15 07:10:20 +01:00
|
|
|
return;
|
2018-02-27 21:01:26 +01:00
|
|
|
status_unusual("routing_failure: "
|
|
|
|
"UPDATE bit set, no channel_update. "
|
|
|
|
"failcode: 0x%04x",
|
|
|
|
(int) failcode);
|
2018-03-15 07:10:20 +01:00
|
|
|
return;
|
2018-01-21 01:36:41 +01:00
|
|
|
}
|
2018-05-18 07:49:08 +02:00
|
|
|
err = handle_channel_update(rstate, channel_update, "error");
|
2018-03-08 05:10:33 +01:00
|
|
|
if (err) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_unusual("routing_failure: "
|
2018-03-08 05:10:33 +01:00
|
|
|
"bad channel_update %s",
|
|
|
|
sanitize_error(err, err, NULL));
|
|
|
|
tal_free(err);
|
2018-03-15 07:10:20 +01:00
|
|
|
return;
|
2018-01-21 01:36:41 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (tal_len(channel_update) != 0)
|
2018-02-27 21:01:26 +01:00
|
|
|
status_unusual("routing_failure: "
|
|
|
|
"UPDATE bit clear, channel_update given. "
|
|
|
|
"failcode: 0x%04x",
|
|
|
|
(int) failcode);
|
2018-01-21 01:36:41 +01:00
|
|
|
}
|
2018-01-18 00:32:36 +01:00
|
|
|
}
|
2018-02-06 16:32:06 +01:00
|
|
|
|
|
|
|
void mark_channel_unroutable(struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *channel)
|
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
2018-02-06 16:32:06 +01:00
|
|
|
time_t now = time_now().ts.tv_sec;
|
|
|
|
const char *scid = type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
channel);
|
|
|
|
|
|
|
|
status_trace("Received mark_channel_unroutable channel %s",
|
|
|
|
scid);
|
|
|
|
|
2018-03-01 10:22:28 +01:00
|
|
|
chan = get_channel(rstate, channel);
|
2018-02-06 16:32:06 +01:00
|
|
|
if (!chan) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_unusual("mark_channel_unroutable: "
|
|
|
|
"channel %s not in routemap",
|
|
|
|
scid);
|
2018-02-06 16:32:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-03-04 03:26:59 +01:00
|
|
|
chan->half[0].unroutable_until = now + 20;
|
|
|
|
chan->half[1].unroutable_until = now + 20;
|
2018-02-06 16:32:06 +01:00
|
|
|
}
|
2018-03-02 09:59:17 +01:00
|
|
|
|
|
|
|
void route_prune(struct routing_state *rstate)
|
|
|
|
{
|
|
|
|
u64 now = time_now().ts.tv_sec;
|
|
|
|
/* Anything below this highwater mark ought to be pruned */
|
|
|
|
const s64 highwater = now - rstate->prune_timeout;
|
2018-03-15 07:10:20 +01:00
|
|
|
const tal_t *pruned = tal(NULL, char);
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
2018-03-02 09:59:17 +01:00
|
|
|
u64 idx;
|
|
|
|
|
|
|
|
/* Now iterate through all channels and see if it is still alive */
|
2018-03-04 03:26:59 +01:00
|
|
|
for (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;
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
if (chan->half[0].last_timestamp < highwater
|
|
|
|
&& chan->half[1].last_timestamp < highwater) {
|
2018-03-02 09:59:17 +01:00
|
|
|
status_trace(
|
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),
|
2018-03-04 03:26:59 +01:00
|
|
|
now - chan->half[0].last_timestamp,
|
|
|
|
now - chan->half[1].last_timestamp);
|
2018-03-02 09:59:17 +01:00
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
/* This may perturb iteration so do outside loop. */
|
|
|
|
tal_steal(pruned, chan);
|
2018-03-02 09:59:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
/* This frees all the chans and maybe even nodes. */
|
2018-03-02 09:59:17 +01:00
|
|
|
tal_free(pruned);
|
|
|
|
}
|
2018-04-21 12:13:33 +02:00
|
|
|
|
2018-05-10 16:00:05 +02:00
|
|
|
void handle_local_add_channel(struct routing_state *rstate, const u8 *msg)
|
2018-04-21 12:13:33 +02:00
|
|
|
{
|
|
|
|
struct short_channel_id scid;
|
|
|
|
struct pubkey remote_node_id;
|
|
|
|
|
2018-05-17 07:09:59 +02:00
|
|
|
if (!fromwire_gossip_local_add_channel(msg, &scid, &remote_node_id)) {
|
2018-04-21 12:13:33 +02:00
|
|
|
status_broken("Unable to parse local_add_channel message: %s", tal_hex(msg, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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)) {
|
2018-04-26 06:51:02 +02:00
|
|
|
status_trace("Attempted to local_add_channel a known channel");
|
2018-04-21 12:13:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-17 07:09:59 +02:00
|
|
|
status_trace("local_add_channel %s",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id, &scid));
|
|
|
|
|
2018-05-17 07:09:59 +02:00
|
|
|
/* Create new (unannounced) channel */
|
|
|
|
new_chan(rstate, &scid, &rstate->local_id, &remote_node_id);
|
2018-04-21 12:13:33 +02:00
|
|
|
}
|