2018-03-22 11:36:25 +01:00
|
|
|
#ifndef LIGHTNING_GOSSIPD_ROUTING_H
|
|
|
|
#define LIGHTNING_GOSSIPD_ROUTING_H
|
2016-06-28 23:19:21 +02:00
|
|
|
#include "config.h"
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <bitcoin/pubkey.h>
|
2018-02-23 01:00:00 +01:00
|
|
|
#include <ccan/crypto/siphash24/siphash24.h>
|
2017-01-21 15:13:10 +01:00
|
|
|
#include <ccan/htable/htable_type.h>
|
2018-02-05 20:41:10 +01:00
|
|
|
#include <ccan/time/time.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <gossipd/broadcast.h>
|
2018-03-18 15:01:22 +01:00
|
|
|
#include <gossipd/gossip_store.h>
|
2018-01-18 00:32:36 +01:00
|
|
|
#include <wire/gen_onion_wire.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <wire/wire.h>
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
#define ROUTING_MAX_HOPS 20
|
2017-03-21 21:21:17 +01:00
|
|
|
#define ROUTING_FLAGS_DISABLED 2
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
struct half_chan {
|
2016-06-28 23:19:21 +02:00
|
|
|
/* millisatoshi. */
|
|
|
|
u32 base_fee;
|
|
|
|
/* millionths */
|
2017-12-18 05:15:38 +01:00
|
|
|
u32 proportional_fee;
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
/* Delay for HTLC in blocks.*/
|
|
|
|
u32 delay;
|
2016-12-12 14:55:46 +01:00
|
|
|
|
|
|
|
/* Is this connection active? */
|
|
|
|
bool active;
|
|
|
|
|
2017-09-01 06:18:54 +02:00
|
|
|
s64 last_timestamp;
|
2016-12-12 14:55:46 +01:00
|
|
|
|
|
|
|
/* Minimum number of msatoshi in an HTLC */
|
|
|
|
u32 htlc_minimum_msat;
|
2017-01-04 04:39:20 +01:00
|
|
|
|
2016-12-12 14:55:46 +01:00
|
|
|
/* Flags as specified by the `channel_update`s, among other
|
|
|
|
* things indicated direction wrt the `channel_id` */
|
|
|
|
u16 flags;
|
2016-12-16 22:07:57 +01:00
|
|
|
|
2018-03-13 16:40:55 +01:00
|
|
|
/* Cached `channel_update` we might forward to new peers (or 0) */
|
2018-03-04 03:26:58 +01:00
|
|
|
u64 channel_update_msgidx;
|
2018-02-02 02:42:15 +01:00
|
|
|
|
|
|
|
/* If greater than current time, this connection should not
|
|
|
|
* be used for routing. */
|
|
|
|
time_t unroutable_until;
|
2016-06-28 23:19:21 +02:00
|
|
|
};
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan {
|
|
|
|
struct short_channel_id scid;
|
|
|
|
u8 *txout_script;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* half[0]->src == nodes[0] half[0]->dst == nodes[1]
|
|
|
|
* half[1]->src == nodes[1] half[1]->dst == nodes[0]
|
|
|
|
*/
|
|
|
|
struct half_chan half[2];
|
|
|
|
/* node[0].id < node[1].id */
|
|
|
|
struct node *nodes[2];
|
|
|
|
|
2018-03-13 16:40:55 +01:00
|
|
|
/* Cached `channel_announcement` we might forward to new peers (or 0) */
|
2018-03-04 03:26:59 +01:00
|
|
|
u64 channel_announce_msgidx;
|
|
|
|
|
|
|
|
/* Is this a public channel, or was it only added locally? */
|
|
|
|
bool public;
|
2018-03-05 23:32:04 +01:00
|
|
|
|
|
|
|
u64 satoshis;
|
2018-03-04 03:26:59 +01:00
|
|
|
};
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
struct node {
|
|
|
|
struct pubkey id;
|
2016-09-28 16:52:03 +02:00
|
|
|
|
2017-09-01 06:18:54 +02:00
|
|
|
/* -1 means never; other fields undefined */
|
|
|
|
s64 last_timestamp;
|
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
/* IP/Hostname and port of this node (may be NULL) */
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr *addresses;
|
2016-09-28 16:52:03 +02:00
|
|
|
|
2018-03-02 09:59:16 +01:00
|
|
|
/* Channels connecting us to other nodes */
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan **chans;
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
/* Temporary data for routefinding. */
|
|
|
|
struct {
|
|
|
|
/* Total to get to here from target. */
|
2017-12-18 05:15:38 +01:00
|
|
|
u64 total;
|
2016-09-06 09:17:48 +02:00
|
|
|
/* Total risk premium of this route. */
|
|
|
|
u64 risk;
|
2016-06-28 23:19:21 +02:00
|
|
|
/* Where that came from. */
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *prev;
|
2016-06-28 23:19:21 +02:00
|
|
|
} bfg[ROUTING_MAX_HOPS+1];
|
2016-10-28 16:40:27 +02:00
|
|
|
|
|
|
|
/* UTF-8 encoded alias as tal_arr, not zero terminated */
|
|
|
|
u8 *alias;
|
2016-12-12 14:55:46 +01:00
|
|
|
|
|
|
|
/* Color to be used when displaying the name */
|
|
|
|
u8 rgb_color[3];
|
2016-12-16 22:07:57 +01:00
|
|
|
|
2018-03-13 16:40:55 +01:00
|
|
|
/* Cached `node_announcement` we might forward to new peers (or 0). */
|
|
|
|
u64 node_announce_msgidx;
|
2016-06-28 23:19:21 +02:00
|
|
|
};
|
|
|
|
|
2017-01-21 15:13:10 +01:00
|
|
|
const secp256k1_pubkey *node_map_keyof_node(const struct node *n);
|
|
|
|
size_t node_map_hash_key(const secp256k1_pubkey *key);
|
|
|
|
bool node_map_node_eq(const struct node *n, const secp256k1_pubkey *key);
|
|
|
|
HTABLE_DEFINE_TYPE(struct node, node_map_keyof_node, node_map_hash_key, node_map_node_eq, node_map);
|
|
|
|
|
2018-02-02 19:49:12 +01:00
|
|
|
struct pending_node_map;
|
2018-02-03 16:51:15 +01:00
|
|
|
struct pending_cannouncement;
|
2018-02-02 19:49:12 +01:00
|
|
|
|
2018-03-02 09:59:16 +01:00
|
|
|
/* If the two nodes[] are id1 and id2, which index would id1 be? */
|
|
|
|
static inline int pubkey_idx(const struct pubkey *id1, const struct pubkey *id2)
|
|
|
|
{
|
|
|
|
return pubkey_cmp(id1, id2) > 0;
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:58 +01:00
|
|
|
/* Fast versions: if you know n is one end of the channel */
|
2018-03-04 03:26:59 +01:00
|
|
|
static inline struct node *other_node(const struct node *n, struct chan *chan)
|
2018-03-04 03:23:32 +01:00
|
|
|
{
|
|
|
|
int idx = (chan->nodes[1] == n);
|
|
|
|
|
2018-03-04 03:26:58 +01:00
|
|
|
assert(chan->nodes[0] == n || chan->nodes[1] == n);
|
2018-03-04 03:23:32 +01:00
|
|
|
return chan->nodes[!idx];
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:58 +01:00
|
|
|
/* If you know n is one end of the channel, get connection src == n */
|
2018-03-04 03:26:59 +01:00
|
|
|
static inline struct half_chan *half_chan_from(const struct node *n,
|
|
|
|
struct chan *chan)
|
2018-03-02 09:59:16 +01:00
|
|
|
{
|
|
|
|
int idx = (chan->nodes[1] == n);
|
|
|
|
|
2018-03-04 03:26:58 +01:00
|
|
|
assert(chan->nodes[0] == n || chan->nodes[1] == n);
|
2018-03-04 03:26:59 +01:00
|
|
|
return &chan->half[idx];
|
2018-03-02 09:59:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:58 +01:00
|
|
|
/* If you know n is one end of the channel, get index dst == n */
|
2018-03-04 03:26:59 +01:00
|
|
|
static inline int half_chan_to(const struct node *n, struct chan *chan)
|
2018-03-02 09:59:16 +01:00
|
|
|
{
|
|
|
|
int idx = (chan->nodes[1] == n);
|
|
|
|
|
2018-03-04 03:26:58 +01:00
|
|
|
assert(chan->nodes[0] == n || chan->nodes[1] == n);
|
2018-03-04 03:23:32 +01:00
|
|
|
return !idx;
|
2018-03-02 09:59:16 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
struct routing_state {
|
|
|
|
/* All known nodes. */
|
|
|
|
struct node_map *nodes;
|
|
|
|
|
2018-03-02 09:59:13 +01:00
|
|
|
/* node_announcements which are waiting on pending_cannouncement */
|
2018-02-02 19:49:12 +01:00
|
|
|
struct pending_node_map *pending_node_map;
|
|
|
|
|
2018-03-02 09:59:13 +01:00
|
|
|
/* FIXME: Make this a htable! */
|
2018-01-04 12:40:58 +01:00
|
|
|
/* channel_announcement which are pending short_channel_id lookup */
|
|
|
|
struct list_head pending_cannouncement;
|
|
|
|
|
2017-01-26 22:47:52 +01:00
|
|
|
struct broadcast_state *broadcasts;
|
2017-08-22 07:25:01 +02:00
|
|
|
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid chain_hash;
|
2017-11-24 15:47:14 +01:00
|
|
|
|
|
|
|
/* Our own ID so we can identify local channels */
|
|
|
|
struct pubkey local_id;
|
2018-01-30 19:46:07 +01:00
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
/* How old does a channel have to be before we prune it? */
|
|
|
|
u32 prune_timeout;
|
|
|
|
|
2018-03-18 15:01:22 +01:00
|
|
|
/* Store for processed messages that we might want to remember across
|
|
|
|
* restarts */
|
|
|
|
struct gossip_store *store;
|
|
|
|
|
2018-01-30 19:46:07 +01:00
|
|
|
/* A map of channels indexed by short_channel_ids */
|
2018-03-04 03:26:59 +01:00
|
|
|
UINTMAP(struct chan *) chanmap;
|
2017-01-19 23:46:07 +01:00
|
|
|
};
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
static inline struct chan *
|
2018-03-01 10:22:28 +01:00
|
|
|
get_channel(const struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *scid)
|
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
return uintmap_get(&rstate->chanmap, scid->u64);
|
2018-03-01 10:22:28 +01:00
|
|
|
}
|
|
|
|
|
2017-03-15 12:44:01 +01:00
|
|
|
struct route_hop {
|
2017-04-29 10:52:40 +02:00
|
|
|
struct short_channel_id channel_id;
|
2017-03-15 12:44:01 +01:00
|
|
|
struct pubkey nodeid;
|
|
|
|
u32 amount;
|
|
|
|
u32 delay;
|
|
|
|
};
|
|
|
|
|
2017-08-28 18:03: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,
|
|
|
|
u32 prune_timeout);
|
2017-01-19 23:46:07 +01:00
|
|
|
|
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);
|
2017-12-20 07:22:03 +01:00
|
|
|
|
2017-02-01 15:09:26 +01:00
|
|
|
/* Handlers for incoming messages */
|
2017-11-24 15:47:14 +01:00
|
|
|
|
|
|
|
/**
|
2018-01-04 12:40:58 +01:00
|
|
|
* handle_channel_announcement -- Check channel announcement is valid
|
|
|
|
*
|
2018-03-08 05:10:31 +01:00
|
|
|
* Returns error message if we should fail channel. Make *scid non-NULL
|
|
|
|
* (for checking) if we extracted a short_channel_id, otherwise ignore.
|
2018-01-04 12:40:58 +01:00
|
|
|
*/
|
2018-03-08 05:10:31 +01:00
|
|
|
u8 *handle_channel_announcement(struct routing_state *rstate,
|
|
|
|
const u8 *announce TAKES,
|
2018-03-20 16:55:52 +01:00
|
|
|
const struct short_channel_id **scid,
|
|
|
|
bool store);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* handle_pending_cannouncement -- handle channel_announce once we've
|
|
|
|
* completed short_channel_id lookup.
|
2017-11-24 15:47:14 +01:00
|
|
|
*
|
2018-01-04 12:40:58 +01:00
|
|
|
* Returns true if the channel was new and is local. This means that
|
|
|
|
* if we haven't sent a node_announcement just yet, now would be a
|
|
|
|
* good time.
|
2017-11-24 15:47:14 +01:00
|
|
|
*/
|
2018-01-04 12:40:58 +01:00
|
|
|
bool handle_pending_cannouncement(struct routing_state *rstate,
|
|
|
|
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 *txscript);
|
2018-03-08 05:10:33 +01:00
|
|
|
|
|
|
|
/* Returns NULL if all OK, otherwise an error for the peer which sent. */
|
2018-03-20 16:55:52 +01:00
|
|
|
u8 *handle_channel_update(struct routing_state *rstate, const u8 *update,
|
|
|
|
bool store);
|
2018-03-08 05:10:26 +01:00
|
|
|
|
|
|
|
/* Returns NULL if all OK, otherwise an error for the peer which sent. */
|
2018-03-20 16:55:52 +01:00
|
|
|
u8 *handle_node_announcement(struct routing_state *rstate, const u8 *node,
|
|
|
|
bool store);
|
2017-02-01 15:09:26 +01:00
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Set values on the struct node_connection */
|
2018-03-04 03:26:59 +01:00
|
|
|
void set_connection_values(struct chan *chan,
|
2018-03-02 09:59:17 +01:00
|
|
|
int idx,
|
|
|
|
u32 base_fee,
|
|
|
|
u32 proportional_fee,
|
|
|
|
u32 delay,
|
|
|
|
bool active,
|
|
|
|
u64 timestamp,
|
|
|
|
u32 htlc_minimum_msat);
|
|
|
|
|
2018-02-27 21:16:43 +01:00
|
|
|
/* Get a node: use this instead of node_map_get() */
|
|
|
|
struct node *get_node(struct routing_state *rstate, const struct pubkey *id);
|
|
|
|
|
2017-03-15 12:44:01 +01:00
|
|
|
/* Compute a route to a destination, for a given amount and riskfactor. */
|
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,
|
2017-10-23 06:16:57 +02:00
|
|
|
const u32 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);
|
2018-01-18 00:32:36 +01:00
|
|
|
/* Disable channel(s) based on the given routing failure. */
|
|
|
|
void routing_failure(struct routing_state *rstate,
|
|
|
|
const struct pubkey *erring_node,
|
|
|
|
const struct short_channel_id *erring_channel,
|
2018-01-21 01:36:41 +01:00
|
|
|
enum onion_type failcode,
|
|
|
|
const u8 *channel_update);
|
2018-02-06 16:32:06 +01:00
|
|
|
/* Disable specific channel from routing. */
|
|
|
|
void mark_channel_unroutable(struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *channel);
|
2017-03-15 12:44:01 +01:00
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
void route_prune(struct routing_state *rstate);
|
2018-03-02 09:59:16 +01:00
|
|
|
|
2017-03-22 16:46:48 +01:00
|
|
|
/* Utility function that, given a source and a destination, gives us
|
|
|
|
* the direction bit the matching channel should get */
|
|
|
|
#define get_channel_direction(from, to) (pubkey_cmp(from, to) > 0)
|
|
|
|
|
2018-03-22 15:11:24 +01:00
|
|
|
/**
|
|
|
|
* Add a channel_announcement to the network view without checking it
|
|
|
|
*
|
|
|
|
* Directly add the channel to the local network, without checking it first. Use
|
|
|
|
* this only for messages from trusted sources. Untrusted sources should use the
|
|
|
|
* @see{handle_channel_announcement} entrypoint to check before adding.
|
|
|
|
*/
|
|
|
|
void routing_add_channel_announcement(struct routing_state *rstate,
|
|
|
|
const u8 *msg TAKES, u64 satoshis);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a channel_update without checking for errors
|
|
|
|
*
|
|
|
|
* Used to actually insert the information in the channel update into the local
|
|
|
|
* network view. Only use this for messages that are known to be good. For
|
|
|
|
* untrusted source, requiring verification please use
|
|
|
|
* @see{handle_channel_update}
|
|
|
|
*/
|
|
|
|
void routing_add_channel_update(struct routing_state *rstate,
|
|
|
|
const u8 *update TAKES);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a node_announcement to the network view without checking it
|
|
|
|
*
|
|
|
|
* Directly add the node being announced to the network view, without verifying
|
|
|
|
* it. This must be from a trusted source, e.g., gossip_store. For untrusted
|
|
|
|
* sources (peers) please use @see{handle_node_announcement}.
|
|
|
|
*/
|
|
|
|
void routing_add_node_announcement(struct routing_state *rstate,
|
|
|
|
const u8 *msg TAKES);
|
|
|
|
|
2018-03-22 11:36:25 +01:00
|
|
|
#endif /* LIGHTNING_GOSSIPD_ROUTING_H */
|