mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 21:35:11 +01:00
routing: Added channel_id and directions to connections
The gossip protocol spec refers to channels by their `channel_id` and a direction. Furthermore, inbetween the `channel_announcement` and the `channel_update` for either direction, the channel direction is in an undefined state and cannot be used, so added the `half_add_connection` function and an `active` flag to differentiate usable connections from unusable ones.
This commit is contained in:
parent
9f846925b3
commit
57d5ae96a4
@ -66,6 +66,7 @@ struct node *new_node(struct lightningd_state *dstate,
|
||||
n->out = tal_arr(n, struct node_connection *, 0);
|
||||
n->port = 0;
|
||||
n->alias = NULL;
|
||||
n->hostname = NULL;
|
||||
node_map_add(dstate->nodes, n);
|
||||
tal_add_destructor(n, destroy_node);
|
||||
|
||||
@ -116,6 +117,48 @@ static void destroy_connection(struct node_connection *nc)
|
||||
fatal("Connection not found in array?!");
|
||||
}
|
||||
|
||||
struct node_connection * get_connection(struct lightningd_state *dstate,
|
||||
const struct pubkey *from_id,
|
||||
const struct pubkey *to_id)
|
||||
{
|
||||
int i, n;
|
||||
struct node *from, *to;
|
||||
from = get_node(dstate, from_id);
|
||||
to = get_node(dstate, to_id);
|
||||
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;
|
||||
}
|
||||
|
||||
struct node_connection *get_connection_by_cid(const struct lightningd_state *dstate,
|
||||
const struct channel_id *chanid,
|
||||
const u8 direction)
|
||||
{
|
||||
struct node *n;
|
||||
int i, num_conn;
|
||||
struct node_map *nodes = dstate->nodes;
|
||||
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];
|
||||
if (memcmp(&c->channel_id, chanid, sizeof(*chanid)) == 0 &&
|
||||
(c->flags&0x1) == direction)
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct node_connection *
|
||||
get_or_make_connection(struct lightningd_state *dstate,
|
||||
const struct pubkey *from_id,
|
||||
@ -151,6 +194,7 @@ get_or_make_connection(struct lightningd_state *dstate,
|
||||
nc = tal(dstate, struct node_connection);
|
||||
nc->src = from;
|
||||
nc->dst = to;
|
||||
memset(&nc->channel_id, 0, sizeof(nc->channel_id));
|
||||
log_add(dstate->base_log, " = %p (%p->%p)", nc, from, to);
|
||||
|
||||
/* Hook it into in/out arrays. */
|
||||
@ -165,6 +209,28 @@ get_or_make_connection(struct lightningd_state *dstate,
|
||||
return nc;
|
||||
}
|
||||
|
||||
struct node_connection *half_add_connection(struct lightningd_state *dstate,
|
||||
const struct pubkey *from,
|
||||
const struct pubkey *to,
|
||||
const struct channel_id *chanid,
|
||||
const u16 flags
|
||||
)
|
||||
{
|
||||
struct node_connection *nc;
|
||||
nc = get_or_make_connection(dstate, from, to);
|
||||
memcpy(&nc->channel_id, chanid, sizeof(nc->channel_id));
|
||||
nc->active = false;
|
||||
nc->last_timestamp = 0;
|
||||
nc->flags = flags;
|
||||
nc->min_blocks = 0;
|
||||
nc->proportional_fee = 0;
|
||||
nc->base_fee = 0;
|
||||
nc->delay = 0;
|
||||
return nc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Updates existing route if required. */
|
||||
struct node_connection *add_connection(struct lightningd_state *dstate,
|
||||
const struct pubkey *from,
|
||||
@ -177,6 +243,10 @@ struct node_connection *add_connection(struct lightningd_state *dstate,
|
||||
c->proportional_fee = proportional_fee;
|
||||
c->delay = delay;
|
||||
c->min_blocks = min_blocks;
|
||||
c->active = true;
|
||||
c->last_timestamp = 0;
|
||||
memset(&c->channel_id, 0, sizeof(c->channel_id));
|
||||
c->flags = pubkey_cmp(from, to) > 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
@ -507,6 +577,8 @@ static void json_getchannels(struct command *cmd,
|
||||
json_add_pubkey(response, "to", &c->dst->id);
|
||||
json_add_num(response, "base_fee", c->base_fee);
|
||||
json_add_num(response, "proportional_fee", c->proportional_fee);
|
||||
json_add_num(response, "expiry", c->delay);
|
||||
json_add_bool(response, "active", c->active);
|
||||
json_object_end(response);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define LIGHTNING_DAEMON_ROUTING_H
|
||||
#include "config.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "wire/wire.h"
|
||||
|
||||
#define ROUTING_MAX_HOPS 20
|
||||
|
||||
@ -16,6 +17,21 @@ struct node_connection {
|
||||
u32 delay;
|
||||
/* Minimum allowable HTLC expiry in blocks. */
|
||||
u32 min_blocks;
|
||||
|
||||
/* Is this connection active? */
|
||||
bool active;
|
||||
|
||||
u32 last_timestamp;
|
||||
|
||||
/* Minimum number of msatoshi in an HTLC */
|
||||
u32 htlc_minimum_msat;
|
||||
|
||||
/* The channel ID, as determined by the anchor transaction */
|
||||
struct channel_id channel_id;
|
||||
|
||||
/* Flags as specified by the `channel_update`s, among other
|
||||
* things indicated direction wrt the `channel_id` */
|
||||
u16 flags;
|
||||
};
|
||||
|
||||
struct node {
|
||||
@ -25,6 +41,8 @@ struct node {
|
||||
char *hostname;
|
||||
int port;
|
||||
|
||||
u32 last_timestamp;
|
||||
|
||||
/* Routes connecting to us, from us. */
|
||||
struct node_connection **in, **out;
|
||||
|
||||
@ -40,6 +58,9 @@ struct node {
|
||||
|
||||
/* UTF-8 encoded alias as tal_arr, not zero terminated */
|
||||
u8 *alias;
|
||||
|
||||
/* Color to be used when displaying the name */
|
||||
u8 rgb_color[3];
|
||||
};
|
||||
|
||||
struct lightningd_state;
|
||||
@ -67,6 +88,28 @@ struct node_connection *add_connection(struct lightningd_state *dstate,
|
||||
u32 base_fee, s32 proportional_fee,
|
||||
u32 delay, u32 min_blocks);
|
||||
|
||||
/* 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. */
|
||||
|
||||
struct node_connection *half_add_connection(struct lightningd_state *dstate,
|
||||
const struct pubkey *from,
|
||||
const struct pubkey *to,
|
||||
const struct channel_id *chanid,
|
||||
const u16 flags);
|
||||
|
||||
/* Get an existing connection between `from` and `to`, NULL if no such
|
||||
* connection exists. */
|
||||
struct node_connection *get_connection(struct lightningd_state *dstate,
|
||||
const struct pubkey *from,
|
||||
const struct pubkey *to);
|
||||
|
||||
/* Given a channel_id, retrieve the matching connection, or NULL if it is
|
||||
* unknown. */
|
||||
struct node_connection *get_connection_by_cid(const struct lightningd_state *dstate,
|
||||
const struct channel_id *chanid,
|
||||
const u8 direction);
|
||||
|
||||
void remove_connection(struct lightningd_state *dstate,
|
||||
const struct pubkey *src, const struct pubkey *dst);
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
/* Routines to handle private keys. */
|
||||
#include "config.h"
|
||||
#include <ccan/short_types/short_types.h>
|
||||
#include <ccan/tal/tal.h>
|
||||
|
||||
struct peer;
|
||||
struct lightningd_state;
|
||||
|
Loading…
Reference in New Issue
Block a user