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>
|
2016-06-28 23:19:21 +02:00
|
|
|
#include <ccan/array_size/array_size.h>
|
|
|
|
#include <ccan/crypto/siphash24/siphash24.h>
|
2017-01-23 13:55:55 +01:00
|
|
|
#include <ccan/endian/endian.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>
|
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>
|
2017-10-23 06:17:38 +02:00
|
|
|
#include <common/wireaddr.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)
|
|
|
|
|
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,
|
2017-12-02 23:28:19 +01:00
|
|
|
const struct pubkey *local_id)
|
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;
|
2017-01-19 23:46:07 +01:00
|
|
|
return rstate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-21 15:13:10 +01:00
|
|
|
const secp256k1_pubkey *node_map_keyof_node(const struct node *n)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
|
|
|
return &n->id.pubkey;
|
|
|
|
}
|
|
|
|
|
2017-01-21 15:13:10 +01:00
|
|
|
size_t node_map_hash_key(const secp256k1_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
|
|
|
}
|
|
|
|
|
2017-01-21 15:13:10 +01:00
|
|
|
bool node_map_node_eq(const struct node *n, const secp256k1_pubkey *key)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
|
|
|
return structeq(&n->id.pubkey, key);
|
|
|
|
}
|
2016-09-28 16:52:03 +02:00
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
static void destroy_node(struct node *node)
|
|
|
|
{
|
|
|
|
/* These remove themselves from the array. */
|
|
|
|
while (tal_count(node->in))
|
|
|
|
tal_free(node->in[0]);
|
|
|
|
while (tal_count(node->out))
|
|
|
|
tal_free(node->out[0]);
|
|
|
|
}
|
|
|
|
|
2017-09-01 06:18:55 +02:00
|
|
|
static struct node *get_node(struct routing_state *rstate,
|
2016-06-28 23:19:21 +02:00
|
|
|
const struct pubkey *id)
|
2017-09-01 06:18:55 +02:00
|
|
|
{
|
|
|
|
return node_map_get(rstate->nodes, &id->pubkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-08-18 06:55:13 +02:00
|
|
|
n->in = tal_arr(n, struct node_connection *, 0);
|
|
|
|
n->out = tal_arr(n, struct node_connection *, 0);
|
2016-10-28 16:40:27 +02:00
|
|
|
n->alias = NULL;
|
2016-12-16 22:07:57 +01:00
|
|
|
n->node_announcement = NULL;
|
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);
|
2016-08-18 06:55:13 +02:00
|
|
|
tal_add_destructor(n, destroy_node);
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
static bool remove_conn_from_array(struct node_connection ***conns,
|
|
|
|
struct node_connection *nc)
|
|
|
|
{
|
|
|
|
size_t i, n;
|
|
|
|
|
|
|
|
n = tal_count(*conns);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if ((*conns)[i] != nc)
|
|
|
|
continue;
|
|
|
|
n--;
|
|
|
|
memmove(*conns + i, *conns + i + 1, sizeof(**conns) * (n - i));
|
|
|
|
tal_resize(conns, n);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_connection(struct node_connection *nc)
|
|
|
|
{
|
|
|
|
if (!remove_conn_from_array(&nc->dst->in, nc)
|
|
|
|
|| !remove_conn_from_array(&nc->src->out, nc))
|
2017-08-28 18:03:01 +02:00
|
|
|
/* FIXME! */
|
|
|
|
abort();
|
2016-08-18 06:55:13 +02:00
|
|
|
}
|
|
|
|
|
2017-09-01 06:18:55 +02:00
|
|
|
static struct node_connection * get_connection(struct routing_state *rstate,
|
|
|
|
const struct pubkey *from_id,
|
|
|
|
const struct pubkey *to_id)
|
2016-12-12 14:55:46 +01:00
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
struct node *from, *to;
|
2017-01-19 23:46:07 +01:00
|
|
|
from = get_node(rstate, from_id);
|
|
|
|
to = get_node(rstate, to_id);
|
2016-12-12 14:55:46 +01:00
|
|
|
if (!from || ! to)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
n = tal_count(to->in);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (to->in[i]->src == from)
|
|
|
|
return to->in[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
struct node_connection *get_connection_by_scid(const struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *schanid,
|
2016-12-12 14:55:46 +01:00
|
|
|
const u8 direction)
|
|
|
|
{
|
|
|
|
struct node *n;
|
|
|
|
int i, num_conn;
|
2017-01-19 23:46:07 +01:00
|
|
|
struct node_map *nodes = rstate->nodes;
|
2016-12-12 14:55:46 +01:00
|
|
|
struct node_connection *c;
|
|
|
|
struct node_map_iter it;
|
|
|
|
|
|
|
|
//FIXME(cdecker) We probably want to speed this up by indexing by chanid.
|
|
|
|
for (n = node_map_first(nodes, &it); n; n = node_map_next(nodes, &it)) {
|
|
|
|
num_conn = tal_count(n->out);
|
|
|
|
for (i = 0; i < num_conn; i++){
|
|
|
|
c = n->out[i];
|
2017-05-19 14:28:39 +02:00
|
|
|
if (short_channel_id_eq(&c->short_channel_id, schanid) &&
|
2016-12-12 14:55:46 +01:00
|
|
|
(c->flags&0x1) == direction)
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
static struct node_connection *
|
2017-01-19 23:46:07 +01:00
|
|
|
get_or_make_connection(struct routing_state *rstate,
|
2016-08-18 06:55:13 +02:00
|
|
|
const struct pubkey *from_id,
|
|
|
|
const struct pubkey *to_id)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
2016-08-18 06:55:13 +02:00
|
|
|
size_t i, n;
|
|
|
|
struct node *from, *to;
|
|
|
|
struct node_connection *nc;
|
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
from = get_node(rstate, from_id);
|
2016-08-18 06:55:13 +02:00
|
|
|
if (!from)
|
2017-01-19 23:46:07 +01:00
|
|
|
from = new_node(rstate, from_id);
|
|
|
|
to = get_node(rstate, to_id);
|
2016-08-18 06:55:13 +02:00
|
|
|
if (!to)
|
2017-01-19 23:46:07 +01:00
|
|
|
to = new_node(rstate, to_id);
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
n = tal_count(to->in);
|
2016-06-28 23:19:21 +02:00
|
|
|
for (i = 0; i < n; i++) {
|
2016-08-18 06:55:13 +02:00
|
|
|
if (to->in[i]->src == from) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Updating existing route from %s to %s",
|
|
|
|
type_to_string(trc, struct pubkey,
|
|
|
|
&from->id),
|
|
|
|
type_to_string(trc, struct pubkey,
|
|
|
|
&to->id));
|
2016-08-18 06:55:13 +02:00
|
|
|
return to->in[i];
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Creating new route from %s to %s",
|
|
|
|
type_to_string(trc, struct pubkey, &from->id),
|
|
|
|
type_to_string(trc, struct pubkey, &to->id));
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
nc = tal(rstate, struct node_connection);
|
2016-08-18 06:55:13 +02:00
|
|
|
nc->src = from;
|
|
|
|
nc->dst = to;
|
2016-12-16 22:07:57 +01:00
|
|
|
nc->channel_announcement = NULL;
|
|
|
|
nc->channel_update = NULL;
|
2016-08-18 06:55:13 +02:00
|
|
|
|
|
|
|
/* Hook it into in/out arrays. */
|
|
|
|
i = tal_count(to->in);
|
|
|
|
tal_resize(&to->in, i+1);
|
|
|
|
to->in[i] = nc;
|
|
|
|
i = tal_count(from->out);
|
|
|
|
tal_resize(&from->out, i+1);
|
|
|
|
from->out[i] = nc;
|
|
|
|
|
|
|
|
tal_add_destructor(nc, destroy_connection);
|
|
|
|
return nc;
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
2017-12-20 07:22:03 +01:00
|
|
|
struct node_connection *half_add_connection(
|
2017-12-18 05:13:31 +01:00
|
|
|
struct routing_state *rstate,
|
2016-12-12 14:55:46 +01:00
|
|
|
const struct pubkey *from,
|
|
|
|
const struct pubkey *to,
|
2017-03-02 13:21:49 +01:00
|
|
|
const struct short_channel_id *schanid,
|
2016-12-12 14:55:46 +01:00
|
|
|
const u16 flags
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct node_connection *nc;
|
2017-01-19 23:46:07 +01:00
|
|
|
nc = get_or_make_connection(rstate, from, to);
|
2017-03-02 13:21:49 +01:00
|
|
|
nc->short_channel_id = *schanid;
|
2016-12-12 14:55:46 +01:00
|
|
|
nc->active = false;
|
|
|
|
nc->flags = flags;
|
2017-09-01 06:18:54 +02:00
|
|
|
nc->last_timestamp = -1;
|
2016-12-12 14:55:46 +01:00
|
|
|
return nc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 05:15:38 +01:00
|
|
|
static u64 connection_fee(const struct node_connection *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. */
|
2016-09-06 09:17:48 +02:00
|
|
|
static void bfg_one_edge(struct node *node, size_t edgenum, double riskfactor)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
2016-08-18 06:55:13 +02:00
|
|
|
struct node_connection *c = node->in[edgenum];
|
2016-06-28 23:19:21 +02:00
|
|
|
size_t h;
|
|
|
|
|
|
|
|
assert(c->dst == node);
|
|
|
|
for (h = 0; h < ROUTING_MAX_HOPS; h++) {
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
fee = connection_fee(c, node->bfg[h].total);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-18 05:15:38 +01:00
|
|
|
if (node->bfg[h].total + fee + risk
|
|
|
|
< c->src->bfg[h+1].total + c->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,
|
|
|
|
&c->src->id),
|
|
|
|
h, node->bfg[h].total + fee);
|
2016-06-28 23:19:21 +02:00
|
|
|
c->src->bfg[h+1].total = node->bfg[h].total + fee;
|
2016-09-06 09:17:48 +02:00
|
|
|
c->src->bfg[h+1].risk = risk;
|
2016-06-28 23:19:21 +02:00
|
|
|
c->src->bfg[h+1].prev = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 05:15:41 +01:00
|
|
|
/* riskfactor is already scaled to per-block amount */
|
2017-09-01 06:18:55 +02:00
|
|
|
static struct node_connection *
|
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,
|
2017-12-18 05:15:38 +01:00
|
|
|
double riskfactor, u64 *fee, struct node_connection ***route)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
|
|
|
struct node *n, *src, *dst;
|
|
|
|
struct node_map_iter it;
|
2017-03-15 12:44:01 +01:00
|
|
|
struct node_connection *first_conn;
|
2016-06-28 23:19:21 +02:00
|
|
|
int runs, i, best;
|
|
|
|
|
|
|
|
/* 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) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("find_route: cannot find %s",
|
|
|
|
type_to_string(trc, struct pubkey, to));
|
2016-06-28 23:19:21 +02:00
|
|
|
return NULL;
|
2017-08-07 13:10:10 +02:00
|
|
|
} else if (!dst) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("find_route: cannot find myself (%s)",
|
|
|
|
type_to_string(trc, 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) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("find_route: this is %s, refusing to create empty route",
|
|
|
|
type_to_string(trc, 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) {
|
|
|
|
status_trace("find_route: can't route huge amount %"PRIu64,
|
|
|
|
msatoshi);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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)) {
|
2016-08-18 06:55:13 +02:00
|
|
|
size_t num_edges = tal_count(n->in);
|
2016-06-28 23:19:21 +02:00
|
|
|
for (i = 0; i < num_edges; i++) {
|
2017-12-18 05:15:31 +01:00
|
|
|
SUPERVERBOSE("Node %s edge %i/%zu",
|
2017-08-31 04:08:12 +02:00
|
|
|
type_to_string(trc, struct pubkey,
|
|
|
|
&n->id),
|
|
|
|
i, num_edges);
|
|
|
|
if (!n->in[i]->active) {
|
2017-12-18 05:15:31 +01:00
|
|
|
SUPERVERBOSE("...inactive");
|
2016-12-14 23:42:06 +01:00
|
|
|
continue;
|
2017-08-31 04:08:12 +02:00
|
|
|
}
|
2016-09-06 09:17:48 +02:00
|
|
|
bfg_one_edge(n, i, riskfactor);
|
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",
|
|
|
|
type_to_string(trc, struct pubkey, to));
|
2016-06-28 23:19:21 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save route from *next* hop (we return first hop as peer).
|
|
|
|
* Note that we take our own fees into account for routing, even
|
|
|
|
* though we don't pay them: it presumably effects preference. */
|
2017-03-15 12:44:01 +01:00
|
|
|
first_conn = dst->bfg[best].prev;
|
2016-06-28 23:19:21 +02:00
|
|
|
dst = dst->bfg[best].prev->dst;
|
|
|
|
best--;
|
|
|
|
|
2016-11-04 01:47:04 +01:00
|
|
|
*fee = dst->bfg[best].total - msatoshi;
|
|
|
|
*route = tal_arr(ctx, struct node_connection *, best);
|
|
|
|
for (i = 0, n = dst;
|
|
|
|
i < best;
|
|
|
|
n = n->bfg[best-i].prev->dst, i++) {
|
|
|
|
(*route)[i] = n->bfg[best-i].prev;
|
|
|
|
}
|
|
|
|
assert(n == src);
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
msatoshi += *fee;
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("find_route: via %s",
|
|
|
|
type_to_string(trc, struct pubkey, &first_conn->dst->id));
|
2017-12-07 23:59:39 +01:00
|
|
|
/* If there are intermediaries, dump them, and total fees. */
|
2016-09-06 06:17:33 +02:00
|
|
|
if (best != 0) {
|
|
|
|
for (i = 0; i < best; i++) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace(" %s (%i+%i=%"PRIu64")",
|
|
|
|
type_to_string(trc, struct pubkey,
|
|
|
|
&(*route)[i]->dst->id),
|
|
|
|
(*route)[i]->base_fee,
|
|
|
|
(*route)[i]->proportional_fee,
|
|
|
|
connection_fee((*route)[i], msatoshi));
|
2016-09-06 06:17:33 +02:00
|
|
|
msatoshi -= connection_fee((*route)[i], msatoshi);
|
|
|
|
}
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace(" =%"PRIi64"(%+"PRIi64")",
|
|
|
|
(*route)[best-1]->dst->bfg[best-1].total, *fee);
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
2017-03-15 12:44:01 +01:00
|
|
|
return first_conn;
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2017-09-01 06:18:55 +02:00
|
|
|
static bool add_channel_direction(struct routing_state *rstate,
|
|
|
|
const struct pubkey *from,
|
|
|
|
const struct pubkey *to,
|
|
|
|
const struct short_channel_id *short_channel_id,
|
|
|
|
const u8 *announcement)
|
2017-01-22 16:16:51 +01:00
|
|
|
{
|
|
|
|
struct node_connection *c = get_connection(rstate, from, to);
|
2017-03-22 16:46:48 +01:00
|
|
|
u16 direction = get_channel_direction(from, to);
|
2017-01-22 16:16:51 +01:00
|
|
|
if (c){
|
|
|
|
/* Do not clobber connections added otherwise */
|
2017-03-02 13:21:49 +01:00
|
|
|
memcpy(&c->short_channel_id, short_channel_id,
|
|
|
|
sizeof(c->short_channel_id));
|
2017-01-22 16:16:51 +01:00
|
|
|
c->flags = direction;
|
|
|
|
return false;
|
2017-03-02 13:21:49 +01:00
|
|
|
}else if(get_connection_by_scid(rstate, short_channel_id, direction)) {
|
2017-01-22 16:16:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
c = half_add_connection(rstate, from, to, short_channel_id, direction);
|
2017-01-22 16:16:51 +01:00
|
|
|
|
|
|
|
/* Remember the announcement so we can forward it to new peers */
|
2017-12-04 17:46:50 +01:00
|
|
|
if (announcement) {
|
|
|
|
tal_free(c->channel_announcement);
|
|
|
|
c->channel_announcement = tal_dup_arr(c, u8, announcement,
|
|
|
|
tal_count(announcement), 0);
|
|
|
|
}
|
2017-01-22 16:16:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-01-23 13:55:55 +01:00
|
|
|
|
2017-04-03 05:59:03 +02:00
|
|
|
/* Verify the signature of a channel_update message */
|
|
|
|
static bool check_channel_update(const struct pubkey *node_key,
|
|
|
|
const secp256k1_ecdsa_signature *node_sig,
|
|
|
|
const u8 *update)
|
|
|
|
{
|
|
|
|
/* 2 byte msg type + 64 byte signatures */
|
|
|
|
int offset = 66;
|
|
|
|
struct sha256_double hash;
|
|
|
|
sha256_double(&hash, update + offset, tal_len(update) - offset);
|
|
|
|
|
|
|
|
return check_signed_hash(&hash, node_sig, node_key);
|
|
|
|
}
|
|
|
|
|
2017-04-03 05:58:03 +02:00
|
|
|
static bool check_channel_announcement(
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* 2 byte msg type + 256 byte signatures */
|
|
|
|
int offset = 258;
|
|
|
|
struct sha256_double hash;
|
|
|
|
sha256_double(&hash, announcement + offset,
|
|
|
|
tal_len(announcement) - offset);
|
|
|
|
|
|
|
|
return check_signed_hash(&hash, node1_sig, node1_key) &&
|
|
|
|
check_signed_hash(&hash, node2_sig, node2_key) &&
|
|
|
|
check_signed_hash(&hash, bitcoin1_sig, bitcoin1_key) &&
|
|
|
|
check_signed_hash(&hash, bitcoin2_sig, bitcoin2_key);
|
|
|
|
}
|
|
|
|
|
2017-11-24 15:47:14 +01:00
|
|
|
bool handle_channel_announcement(
|
2017-02-01 15:09:26 +01:00
|
|
|
struct routing_state *rstate,
|
2017-12-11 04:16:50 +01:00
|
|
|
const u8 *announce)
|
2017-02-01 15:09:26 +01:00
|
|
|
{
|
|
|
|
u8 *serialized;
|
2017-11-24 15:47:14 +01:00
|
|
|
bool forward = false, local, sigfail;
|
2017-02-01 15:09:26 +01:00
|
|
|
secp256k1_ecdsa_signature node_signature_1;
|
|
|
|
secp256k1_ecdsa_signature node_signature_2;
|
2017-03-02 13:21:49 +01:00
|
|
|
struct short_channel_id short_channel_id;
|
2017-02-01 15:09:26 +01:00
|
|
|
secp256k1_ecdsa_signature bitcoin_signature_1;
|
|
|
|
secp256k1_ecdsa_signature bitcoin_signature_2;
|
|
|
|
struct pubkey node_id_1;
|
|
|
|
struct pubkey node_id_2;
|
|
|
|
struct pubkey bitcoin_key_1;
|
|
|
|
struct pubkey bitcoin_key_2;
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid chain_hash;
|
2017-12-04 13:40:45 +01:00
|
|
|
struct node_connection *c0, *c1;
|
2017-02-01 15:09:26 +01:00
|
|
|
const tal_t *tmpctx = tal_tmpctx(rstate);
|
|
|
|
u8 *features;
|
2017-12-11 04:16:50 +01:00
|
|
|
size_t len = tal_len(announce);
|
2017-02-01 15:09:26 +01:00
|
|
|
|
|
|
|
serialized = tal_dup_arr(tmpctx, u8, announce, len, 0);
|
|
|
|
if (!fromwire_channel_announcement(tmpctx, serialized, NULL,
|
|
|
|
&node_signature_1, &node_signature_2,
|
|
|
|
&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,
|
2017-03-02 13:21:49 +01:00
|
|
|
&short_channel_id,
|
2017-02-01 15:09:26 +01:00
|
|
|
&node_id_1, &node_id_2,
|
2017-08-03 03:45:04 +02:00
|
|
|
&bitcoin_key_1, &bitcoin_key_2)) {
|
2017-02-01 15:09:26 +01:00
|
|
|
tal_free(tmpctx);
|
2017-11-24 15:47:14 +01:00
|
|
|
return false;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
2017-08-22 07:25:01 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* The receiving node MUST ignore the message if the specified
|
|
|
|
* `chain_hash` is unknown to the receiver.
|
|
|
|
*/
|
|
|
|
if (!structeq(&chain_hash, &rstate->chain_hash)) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Received channel_announcement 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));
|
2017-08-22 07:25:01 +02:00
|
|
|
tal_free(tmpctx);
|
2017-11-24 15:47:14 +01:00
|
|
|
return false;
|
2017-08-22 07:25:01 +02:00
|
|
|
}
|
|
|
|
|
2017-02-01 15:09:26 +01:00
|
|
|
// FIXME: Check features!
|
|
|
|
//FIXME(cdecker) Check chain topology for the anchor TX
|
|
|
|
|
|
|
|
|
2017-11-24 15:47:14 +01:00
|
|
|
local = pubkey_eq(&node_id_1, &rstate->local_id) ||
|
|
|
|
pubkey_eq(&node_id_2, &rstate->local_id);
|
|
|
|
sigfail = !check_channel_announcement(
|
|
|
|
&node_id_1, &node_id_2, &bitcoin_key_1, &bitcoin_key_2,
|
|
|
|
&node_signature_1, &node_signature_2, &bitcoin_signature_1,
|
|
|
|
&bitcoin_signature_2, serialized);
|
|
|
|
|
2017-12-02 23:38:43 +01:00
|
|
|
status_trace("Received channel_announcement for channel %s, local=%d, sigfail=%d",
|
|
|
|
type_to_string(trc, struct short_channel_id,
|
|
|
|
&short_channel_id), local, sigfail);
|
|
|
|
|
|
|
|
|
2017-11-24 15:47:14 +01:00
|
|
|
if (sigfail && !local) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace(
|
2017-12-04 13:40:45 +01:00
|
|
|
"Signature verification of non-local channel announcement failed");
|
2017-04-03 05:58:03 +02:00
|
|
|
tal_free(tmpctx);
|
2017-11-24 15:47:14 +01:00
|
|
|
return false;
|
2017-04-03 05:58:03 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:40:45 +01:00
|
|
|
/* Is this a new connection? */
|
|
|
|
c0 = get_connection_by_scid(rstate, &short_channel_id, 0);
|
|
|
|
c1 = get_connection_by_scid(rstate, &short_channel_id, 1);
|
|
|
|
forward = !c0 || !c1 || !c0->channel_announcement || !c1->channel_announcement;
|
2017-11-24 15:47:14 +01:00
|
|
|
|
2017-12-21 03:43:24 +01:00
|
|
|
/* FIXME: What should we do if this channel_announce is completely
|
|
|
|
* different from previous? eg. different nodes? We would have to
|
|
|
|
* clear out the old announce and all updates... */
|
2017-12-04 13:40:45 +01:00
|
|
|
add_channel_direction(rstate, &node_id_1, &node_id_2, &short_channel_id,
|
|
|
|
sigfail ? NULL : serialized);
|
|
|
|
add_channel_direction(rstate, &node_id_2, &node_id_1, &short_channel_id,
|
|
|
|
sigfail ? NULL : serialized);
|
|
|
|
|
|
|
|
if (!forward || sigfail) {
|
|
|
|
status_trace("Not forwarding channel_announcement, forward=%d, sigfail=%d", forward, sigfail);
|
2017-02-01 15:09:26 +01:00
|
|
|
tal_free(tmpctx);
|
2017-11-24 15:47:14 +01:00
|
|
|
/* This will not be forwarded so we do not want to
|
|
|
|
* announce the node either, others might drop it. */
|
|
|
|
return false;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u8 *tag = tal_arr(tmpctx, u8, 0);
|
2017-03-02 13:21:49 +01:00
|
|
|
towire_short_channel_id(&tag, &short_channel_id);
|
2017-02-01 15:09:26 +01:00
|
|
|
queue_broadcast(rstate->broadcasts, WIRE_CHANNEL_ANNOUNCEMENT,
|
2017-12-21 03:43:24 +01:00
|
|
|
tag, serialized, true);
|
2017-02-01 15:09:26 +01:00
|
|
|
|
|
|
|
tal_free(tmpctx);
|
2017-11-24 15:47:14 +01:00
|
|
|
return local;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 04:16:50 +01:00
|
|
|
void handle_channel_update(struct routing_state *rstate, const u8 *update)
|
2017-02-01 15:09:26 +01:00
|
|
|
{
|
|
|
|
u8 *serialized;
|
|
|
|
struct node_connection *c;
|
|
|
|
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;
|
|
|
|
const tal_t *tmpctx = tal_tmpctx(rstate);
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid chain_hash;
|
2017-12-11 04:16:50 +01:00
|
|
|
size_t len = tal_len(update);
|
2017-02-01 15:09:26 +01:00
|
|
|
|
|
|
|
serialized = tal_dup_arr(tmpctx, u8, update, len, 0);
|
2017-08-22 07:25:01 +02:00
|
|
|
if (!fromwire_channel_update(serialized, NULL, &signature,
|
|
|
|
&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)) {
|
|
|
|
tal_free(tmpctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:25:01 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* The receiving node MUST ignore the channel update if the specified
|
|
|
|
* `chain_hash` value is unknown, meaning it isn't active on the
|
|
|
|
* specified chain. */
|
|
|
|
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));
|
2017-08-22 07:25:01 +02:00
|
|
|
tal_free(tmpctx);
|
|
|
|
return;
|
|
|
|
}
|
2017-02-01 15:09:26 +01:00
|
|
|
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Received channel_update for channel %s(%d)",
|
|
|
|
type_to_string(trc, struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
flags & 0x01);
|
2017-02-01 15:09:26 +01:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
c = get_connection_by_scid(rstate, &short_channel_id, flags & 0x1);
|
2017-02-01 15:09:26 +01:00
|
|
|
|
|
|
|
if (!c) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Ignoring update for unknown channel %s",
|
|
|
|
type_to_string(trc, struct short_channel_id,
|
|
|
|
&short_channel_id));
|
2017-02-01 15:09:26 +01:00
|
|
|
tal_free(tmpctx);
|
|
|
|
return;
|
2017-09-01 06:18:54 +02:00
|
|
|
} else if (c->last_timestamp >= timestamp) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Ignoring outdated update.");
|
2017-02-01 15:09:26 +01:00
|
|
|
tal_free(tmpctx);
|
|
|
|
return;
|
2017-04-03 05:59:03 +02:00
|
|
|
} else if (!check_channel_update(&c->src->id, &signature, serialized)) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Signature verification failed.");
|
2017-04-03 05:59:03 +02:00
|
|
|
tal_free(tmpctx);
|
|
|
|
return;
|
2017-02-01 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME(cdecker) Check signatures
|
|
|
|
c->last_timestamp = timestamp;
|
|
|
|
c->delay = expiry;
|
|
|
|
c->htlc_minimum_msat = htlc_minimum_msat;
|
|
|
|
c->base_fee = fee_base_msat;
|
|
|
|
c->proportional_fee = fee_proportional_millionths;
|
2017-03-21 21:21:17 +01:00
|
|
|
c->active = (flags & ROUTING_FLAGS_DISABLED) == 0;
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Channel %s(%d) was updated.",
|
|
|
|
type_to_string(trc, struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
flags);
|
2017-02-01 15:09:26 +01:00
|
|
|
|
2017-12-18 05:15:40 +01:00
|
|
|
if (c->proportional_fee >= MAX_PROPORTIONAL_FEE) {
|
|
|
|
status_trace("Channel %s(%d) massive proportional fee %u:"
|
|
|
|
" disabling.",
|
|
|
|
type_to_string(trc, struct short_channel_id,
|
|
|
|
&short_channel_id),
|
|
|
|
flags,
|
|
|
|
fee_proportional_millionths);
|
|
|
|
c->active = false;
|
|
|
|
}
|
|
|
|
|
2017-02-01 15:09:26 +01:00
|
|
|
u8 *tag = tal_arr(tmpctx, u8, 0);
|
2017-03-02 13:21:49 +01:00
|
|
|
towire_short_channel_id(&tag, &short_channel_id);
|
2017-03-29 12:51:15 +02:00
|
|
|
towire_u16(&tag, flags & 0x1);
|
2017-02-01 15:09:26 +01:00
|
|
|
queue_broadcast(rstate->broadcasts,
|
|
|
|
WIRE_CHANNEL_UPDATE,
|
|
|
|
tag,
|
2017-12-21 03:43:24 +01:00
|
|
|
serialized, false);
|
2017-02-01 15:09:26 +01:00
|
|
|
|
|
|
|
tal_free(c->channel_update);
|
|
|
|
c->channel_update = tal_steal(c, serialized);
|
|
|
|
tal_free(tmpctx);
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
*
|
|
|
|
* The receiving node SHOULD ignore the first `address
|
|
|
|
* descriptor` which does not match the types defined
|
|
|
|
* above.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2017-02-01 15:09:26 +01:00
|
|
|
void handle_node_announcement(
|
2017-12-11 04:16:50 +01:00
|
|
|
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;
|
|
|
|
const tal_t *tmpctx = tal_tmpctx(rstate);
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr *wireaddrs;
|
2017-12-11 04:16:50 +01:00
|
|
|
size_t len = tal_len(node_ann);
|
2017-02-01 15:09:26 +01:00
|
|
|
|
|
|
|
serialized = tal_dup_arr(tmpctx, u8, node_ann, len, 0);
|
|
|
|
if (!fromwire_node_announcement(tmpctx, serialized, NULL,
|
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)) {
|
|
|
|
tal_free(tmpctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Check features!
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Received node_announcement for node %s",
|
|
|
|
type_to_string(trc, struct pubkey, &node_id));
|
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)) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Ignoring node announcement, signature verification failed.");
|
2017-02-01 15:09:26 +01:00
|
|
|
tal_free(tmpctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
node = get_node(rstate, &node_id);
|
|
|
|
|
|
|
|
if (!node) {
|
2017-12-07 23:59:39 +01:00
|
|
|
status_trace("Node not found, was the node_announcement preceded by at least channel_announcement?");
|
2017-02-01 15:09:26 +01:00
|
|
|
tal_free(tmpctx);
|
|
|
|
return;
|
2017-09-01 06:18:54 +02:00
|
|
|
} else if (node->last_timestamp >= timestamp) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Ignoring node announcement, it's outdated.");
|
2017-02-01 15:09:26 +01:00
|
|
|
tal_free(tmpctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-23 06:17:38 +02:00
|
|
|
wireaddrs = read_addresses(tmpctx, addresses);
|
|
|
|
if (!wireaddrs) {
|
2017-08-28 18:03:01 +02:00
|
|
|
status_trace("Unable to parse addresses.");
|
2017-05-08 21:26:46 +02:00
|
|
|
tal_free(serialized);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tal_free(node->addresses);
|
2017-10-23 06:17:38 +02:00
|
|
|
node->addresses = tal_steal(node, wireaddrs);
|
2017-05-08 21:26:46 +02:00
|
|
|
|
2017-02-01 15:09:26 +01:00
|
|
|
node->last_timestamp = timestamp;
|
2017-05-08 21:26:46 +02:00
|
|
|
|
2017-02-01 15:09:26 +01:00
|
|
|
memcpy(node->rgb_color, rgb_color, 3);
|
|
|
|
|
|
|
|
u8 *tag = tal_arr(tmpctx, u8, 0);
|
|
|
|
towire_pubkey(&tag, &node_id);
|
|
|
|
queue_broadcast(rstate->broadcasts,
|
|
|
|
WIRE_NODE_ANNOUNCEMENT,
|
|
|
|
tag,
|
2017-12-21 03:43:24 +01:00
|
|
|
serialized,
|
|
|
|
false);
|
2017-02-01 15:09:26 +01:00
|
|
|
tal_free(node->node_announcement);
|
|
|
|
node->node_announcement = tal_steal(node, serialized);
|
|
|
|
tal_free(tmpctx);
|
|
|
|
}
|
2017-03-15 12:44:01 +01:00
|
|
|
|
|
|
|
struct route_hop *get_route(tal_t *ctx, struct routing_state *rstate,
|
|
|
|
const struct pubkey *source,
|
|
|
|
const struct pubkey *destination,
|
2017-10-23 06:16:57 +02:00
|
|
|
const u32 msatoshi, double riskfactor,
|
|
|
|
u32 final_cltv)
|
2017-03-15 12:44:01 +01:00
|
|
|
{
|
|
|
|
struct node_connection **route;
|
|
|
|
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;
|
|
|
|
struct node_connection *first_conn;
|
|
|
|
|
|
|
|
first_conn = find_route(ctx, rstate, source, destination, msatoshi,
|
2017-12-18 05:15:41 +01:00
|
|
|
riskfactor / BLOCKS_PER_YEAR / 10000,
|
|
|
|
&fee, &route);
|
2017-03-15 12:44:01 +01:00
|
|
|
|
|
|
|
if (!first_conn) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fees, delays need to be calculated backwards along route. */
|
|
|
|
hops = tal_arr(ctx, struct route_hop, tal_count(route) + 1);
|
|
|
|
total_amount = msatoshi;
|
2017-10-23 06:16:57 +02:00
|
|
|
total_delay = final_cltv;
|
2017-03-15 12:44:01 +01:00
|
|
|
|
|
|
|
for (i = tal_count(route) - 1; i >= 0; i--) {
|
2017-04-29 10:52:40 +02:00
|
|
|
hops[i + 1].channel_id = route[i]->short_channel_id;
|
2017-03-15 12:44:01 +01:00
|
|
|
hops[i + 1].nodeid = route[i]->dst->id;
|
|
|
|
hops[i + 1].amount = total_amount;
|
|
|
|
total_amount += connection_fee(route[i], total_amount);
|
|
|
|
|
|
|
|
hops[i + 1].delay = total_delay;
|
2017-10-23 06:16:57 +02:00
|
|
|
total_delay += route[i]->delay;
|
2017-03-15 12:44:01 +01:00
|
|
|
}
|
|
|
|
/* Backfill the first hop manually */
|
2017-04-29 10:52:40 +02:00
|
|
|
hops[0].channel_id = first_conn->short_channel_id;
|
2017-03-15 12:44:01 +01:00
|
|
|
hops[0].nodeid = first_conn->dst->id;
|
2017-10-23 06:16:57 +02:00
|
|
|
/* We don't charge ourselves any fees, nor require delay */
|
2017-03-15 12:44:01 +01:00
|
|
|
hops[0].amount = total_amount;
|
|
|
|
hops[0].delay = total_delay;
|
2017-10-23 06:16:57 +02:00
|
|
|
|
|
|
|
/* FIXME: Shadow route! */
|
2017-03-15 12:44:01 +01:00
|
|
|
return hops;
|
|
|
|
}
|