2016-06-28 23:19:21 +02:00
|
|
|
#ifndef LIGHTNING_DAEMON_ROUTING_H
|
|
|
|
#define LIGHTNING_DAEMON_ROUTING_H
|
|
|
|
#include "config.h"
|
|
|
|
#include "bitcoin/pubkey.h"
|
2017-01-26 22:47:52 +01:00
|
|
|
#include "daemon/broadcast.h"
|
2016-12-12 14:55:46 +01:00
|
|
|
#include "wire/wire.h"
|
2017-01-21 15:13:10 +01:00
|
|
|
#include <ccan/htable/htable_type.h>
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
#define ROUTING_MAX_HOPS 20
|
|
|
|
|
|
|
|
struct node_connection {
|
|
|
|
struct node *src, *dst;
|
|
|
|
/* millisatoshi. */
|
|
|
|
u32 base_fee;
|
|
|
|
/* millionths */
|
|
|
|
s32 proportional_fee;
|
|
|
|
|
|
|
|
/* Delay for HTLC in blocks.*/
|
|
|
|
u32 delay;
|
|
|
|
/* Minimum allowable HTLC expiry in blocks. */
|
|
|
|
u32 min_blocks;
|
2016-12-12 14:55:46 +01:00
|
|
|
|
|
|
|
/* Is this connection active? */
|
|
|
|
bool active;
|
|
|
|
|
|
|
|
u32 last_timestamp;
|
|
|
|
|
|
|
|
/* 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
|
|
|
/* The channel ID, as determined by the anchor transaction */
|
2017-03-02 13:21:49 +01:00
|
|
|
struct short_channel_id short_channel_id;
|
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
|
|
|
|
|
|
|
/* Cached `channel_announcement` and `channel_update` we might forward to new peers*/
|
|
|
|
u8 *channel_announcement;
|
|
|
|
u8 *channel_update;
|
2016-06-28 23:19:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct node {
|
|
|
|
struct pubkey id;
|
2016-09-28 16:52:03 +02:00
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
/* IP/Hostname and port of this node (may be NULL) */
|
2016-09-28 16:52:03 +02:00
|
|
|
char *hostname;
|
|
|
|
int port;
|
|
|
|
|
2016-12-12 14:55:46 +01:00
|
|
|
u32 last_timestamp;
|
|
|
|
|
2016-08-18 06:55:13 +02:00
|
|
|
/* Routes connecting to us, from us. */
|
|
|
|
struct node_connection **in, **out;
|
2016-06-28 23:19:21 +02:00
|
|
|
|
|
|
|
/* Temporary data for routefinding. */
|
|
|
|
struct {
|
|
|
|
/* Total to get to here from target. */
|
|
|
|
s64 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. */
|
|
|
|
struct node_connection *prev;
|
|
|
|
} 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
|
|
|
|
|
|
|
/* Cached `node_announcement` we might forward to new peers. */
|
|
|
|
u8 *node_announcement;
|
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);
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
struct lightningd_state;
|
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
struct routing_state {
|
|
|
|
/* All known nodes. */
|
|
|
|
struct node_map *nodes;
|
|
|
|
|
|
|
|
struct log *base_log;
|
2017-01-26 22:47:52 +01:00
|
|
|
|
|
|
|
struct broadcast_state *broadcasts;
|
2017-01-19 23:46:07 +01:00
|
|
|
};
|
|
|
|
|
2017-03-15 12:44:01 +01:00
|
|
|
struct route_hop {
|
|
|
|
struct pubkey nodeid;
|
|
|
|
u32 amount;
|
|
|
|
u32 delay;
|
|
|
|
};
|
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
//FIXME(cdecker) The log will have to be replaced for the new subdaemon, keeping for now to keep changes small.
|
|
|
|
struct routing_state *new_routing_state(const tal_t *ctx, struct log *base_log);
|
|
|
|
|
|
|
|
struct node *new_node(struct routing_state *rstate,
|
2016-06-28 23:19:21 +02:00
|
|
|
const struct pubkey *id);
|
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
struct node *get_node(struct routing_state *rstate,
|
2016-06-28 23:19:21 +02:00
|
|
|
const struct pubkey *id);
|
|
|
|
|
|
|
|
/* msatoshi must be possible (< 21 million BTC), ie < 2^60.
|
2016-09-06 09:17:49 +02:00
|
|
|
* If it returns more than msatoshi, it overflowed. */
|
2016-06-28 23:19:21 +02:00
|
|
|
s64 connection_fee(const struct node_connection *c, u64 msatoshi);
|
|
|
|
|
2016-09-28 16:52:03 +02:00
|
|
|
/* Updates existing node, or creates a new one as required. */
|
2017-01-19 23:46:07 +01:00
|
|
|
struct node *add_node(struct routing_state *rstate,
|
2016-09-28 16:52:03 +02:00
|
|
|
const struct pubkey *pk,
|
|
|
|
char *hostname,
|
|
|
|
int port);
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
/* Updates existing connection, or creates new one as required. */
|
2017-01-19 23:46:07 +01:00
|
|
|
struct node_connection *add_connection(struct routing_state *rstate,
|
2016-08-18 06:55:13 +02:00
|
|
|
const struct pubkey *from,
|
|
|
|
const struct pubkey *to,
|
2016-06-28 23:19:21 +02:00
|
|
|
u32 base_fee, s32 proportional_fee,
|
|
|
|
u32 delay, u32 min_blocks);
|
|
|
|
|
2016-12-12 14:55:46 +01:00
|
|
|
/* Add a connection to the routing table, but do not mark it as usable
|
|
|
|
* yet. Used by channel_announcements before the channel_update comes
|
|
|
|
* in. */
|
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
struct node_connection *half_add_connection(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);
|
|
|
|
|
|
|
|
/* Get an existing connection between `from` and `to`, NULL if no such
|
|
|
|
* connection exists. */
|
2017-01-19 23:46:07 +01:00
|
|
|
struct node_connection *get_connection(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
|
|
|
/* Given a short_channel_id, retrieve the matching connection, or NULL if it is
|
2016-12-12 14:55:46 +01:00
|
|
|
* unknown. */
|
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);
|
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
void remove_connection(struct routing_state *rstate,
|
2016-08-31 08:40:17 +02:00
|
|
|
const struct pubkey *src, const struct pubkey *dst);
|
|
|
|
|
2017-03-15 12:44:01 +01:00
|
|
|
struct node_connection *
|
|
|
|
find_route(const tal_t *ctx, struct routing_state *rstate,
|
|
|
|
const struct pubkey *from, const struct pubkey *to, u64 msatoshi,
|
|
|
|
double riskfactor, s64 *fee, struct node_connection ***route);
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2017-01-19 23:46:07 +01:00
|
|
|
struct node_map *empty_node_map(const tal_t *ctx);
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
char *opt_add_route(const char *arg, struct lightningd_state *dstate);
|
|
|
|
|
2017-01-22 16:16:51 +01:00
|
|
|
bool add_channel_direction(struct routing_state *rstate,
|
|
|
|
const struct pubkey *from,
|
|
|
|
const struct pubkey *to,
|
|
|
|
const int direction,
|
2017-03-02 13:21:49 +01:00
|
|
|
const struct short_channel_id *short_channel_id,
|
2017-01-22 16:16:51 +01:00
|
|
|
const u8 *announcement);
|
|
|
|
|
2017-01-23 13:55:55 +01:00
|
|
|
bool read_ip(const tal_t *ctx, const u8 *addresses, char **hostname, int *port);
|
|
|
|
u8 *write_ip(const tal_t *ctx, const char *srcip, int port);
|
|
|
|
|
2017-02-01 15:09:26 +01:00
|
|
|
/* Handlers for incoming messages */
|
|
|
|
void handle_channel_announcement(struct routing_state *rstate, const u8 *announce, size_t len);
|
|
|
|
void handle_channel_update(struct routing_state *rstate, const u8 *update, size_t len);
|
|
|
|
void handle_node_announcement(struct routing_state *rstate, const u8 *node, size_t len);
|
|
|
|
|
2017-03-15 12:44:01 +01:00
|
|
|
/* Compute a route to a destination, for a given amount and riskfactor. */
|
|
|
|
struct route_hop *get_route(tal_t *ctx, struct routing_state *rstate,
|
|
|
|
const struct pubkey *source,
|
|
|
|
const struct pubkey *destination,
|
|
|
|
const u32 msatoshi, double riskfactor);
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
#endif /* LIGHTNING_DAEMON_ROUTING_H */
|