Skip to content

Commit

Permalink
Update to new spec: differentiate channel_id and short_channel_id.
Browse files Browse the repository at this point in the history
The spec 4af8e18 uses a 32-byte 'channel-id'
field, not to be confused with the 8-byte short ID used by gossip.  Rename
appropriately, and update to the new handshake protocol.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Mar 2, 2017
1 parent 2ce6a4b commit 7419fde
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 192 deletions.
28 changes: 14 additions & 14 deletions daemon/p2p_announce.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ static void broadcast_channel_update(struct lightningd_state *dstate, struct pee
struct txlocator *loc;
u8 *serialized;
secp256k1_ecdsa_signature signature;
struct channel_id channel_id;
struct short_channel_id short_channel_id;
u32 timestamp = time_now().ts.tv_sec;
const tal_t *tmpctx = tal_tmpctx(dstate);

loc = locate_tx(tmpctx, dstate->topology, &peer->anchor.txid);
channel_id.blocknum = loc->blkheight;
channel_id.txnum = loc->index;
channel_id.outnum = peer->anchor.index;
short_channel_id.blocknum = loc->blkheight;
short_channel_id.txnum = loc->index;
short_channel_id.outnum = peer->anchor.index;

/* Avoid triggering memcheck */
memset(&signature, 0, sizeof(signature));

serialized = towire_channel_update(tmpctx, &signature, &channel_id,
serialized = towire_channel_update(tmpctx, &signature, &short_channel_id,
timestamp,
pubkey_cmp(&dstate->id, peer->id) > 0,
dstate->config.min_htlc_expiry,
Expand All @@ -41,15 +41,15 @@ static void broadcast_channel_update(struct lightningd_state *dstate, struct pee
dstate->config.fee_per_satoshi);
privkey_sign(dstate, serialized + 66, tal_count(serialized) - 66,
&signature);
serialized = towire_channel_update(tmpctx, &signature, &channel_id,
serialized = towire_channel_update(tmpctx, &signature, &short_channel_id,
timestamp,
pubkey_cmp(&dstate->id, peer->id) > 0,
dstate->config.min_htlc_expiry,
1,
dstate->config.fee_base,
dstate->config.fee_per_satoshi);
u8 *tag = tal_arr(tmpctx, u8, 0);
towire_channel_id(&tag, &channel_id);
towire_short_channel_id(&tag, &short_channel_id);
queue_broadcast(dstate->rstate->broadcasts, WIRE_CHANNEL_UPDATE, tag, serialized);
tal_free(tmpctx);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ static void broadcast_node_announcement(struct lightningd_state *dstate)
static void broadcast_channel_announcement(struct lightningd_state *dstate, struct peer *peer)
{
struct txlocator *loc;
struct channel_id channel_id;
struct short_channel_id short_channel_id;
secp256k1_ecdsa_signature node_signature[2];
secp256k1_ecdsa_signature bitcoin_signature[2];
const struct pubkey *node_id[2];
Expand All @@ -108,9 +108,9 @@ static void broadcast_channel_announcement(struct lightningd_state *dstate, stru

loc = locate_tx(tmpctx, dstate->topology, &peer->anchor.txid);

channel_id.blocknum = loc->blkheight;
channel_id.txnum = loc->index;
channel_id.outnum = peer->anchor.index;
short_channel_id.blocknum = loc->blkheight;
short_channel_id.txnum = loc->index;
short_channel_id.outnum = peer->anchor.index;

/* Set all sigs to zero */
memset(node_signature, 0, sizeof(node_signature));
Expand Down Expand Up @@ -147,7 +147,7 @@ static void broadcast_channel_announcement(struct lightningd_state *dstate, stru
&node_signature[1],
&bitcoin_signature[0],
&bitcoin_signature[1],
&channel_id,
&short_channel_id,
node_id[0],
node_id[1],
bitcoin_key[0],
Expand All @@ -159,14 +159,14 @@ static void broadcast_channel_announcement(struct lightningd_state *dstate, stru
&node_signature[1],
&bitcoin_signature[0],
&bitcoin_signature[1],
&channel_id,
&short_channel_id,
node_id[0],
node_id[1],
bitcoin_key[0],
bitcoin_key[1],
NULL);
u8 *tag = tal_arr(tmpctx, u8, 0);
towire_channel_id(&tag, &channel_id);
towire_short_channel_id(&tag, &short_channel_id);
queue_broadcast(dstate->rstate->broadcasts, WIRE_CHANNEL_ANNOUNCEMENT,
tag, serialized);
tal_free(tmpctx);
Expand Down
63 changes: 32 additions & 31 deletions daemon/routing.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ struct node_connection * get_connection(struct routing_state *rstate,
return NULL;
}

struct node_connection *get_connection_by_cid(const struct routing_state *rstate,
const struct channel_id *chanid,
struct node_connection *get_connection_by_scid(const struct routing_state *rstate,
const struct short_channel_id *schanid,
const u8 direction)
{
struct node *n;
Expand All @@ -162,7 +162,7 @@ struct node_connection *get_connection_by_cid(const struct routing_state *rstate
num_conn = tal_count(n->out);
for (i = 0; i < num_conn; i++){
c = n->out[i];
if (structeq(&c->channel_id, chanid) &&
if (structeq(&c->short_channel_id, schanid) &&
(c->flags&0x1) == direction)
return c;
}
Expand Down Expand Up @@ -224,13 +224,13 @@ get_or_make_connection(struct routing_state *rstate,
struct node_connection *half_add_connection(struct routing_state *rstate,
const struct pubkey *from,
const struct pubkey *to,
const struct channel_id *chanid,
const struct short_channel_id *schanid,
const u16 flags
)
{
struct node_connection *nc;
nc = get_or_make_connection(rstate, from, to);
memcpy(&nc->channel_id, chanid, sizeof(nc->channel_id));
nc->short_channel_id = *schanid;
nc->active = false;
nc->last_timestamp = 0;
nc->flags = flags;
Expand All @@ -257,7 +257,7 @@ struct node_connection *add_connection(struct routing_state *rstate,
c->min_blocks = min_blocks;
c->active = true;
c->last_timestamp = 0;
memset(&c->channel_id, 0, sizeof(c->channel_id));
memset(&c->short_channel_id, 0, sizeof(c->short_channel_id));
c->flags = pubkey_cmp(from, to) > 0;
return c;
}
Expand Down Expand Up @@ -514,20 +514,21 @@ bool add_channel_direction(struct routing_state *rstate,
const struct pubkey *from,
const struct pubkey *to,
const int direction,
const struct channel_id *channel_id,
const struct short_channel_id *short_channel_id,
const u8 *announcement)
{
struct node_connection *c = get_connection(rstate, from, to);
if (c){
/* Do not clobber connections added otherwise */
memcpy(&c->channel_id, channel_id, sizeof(c->channel_id));
memcpy(&c->short_channel_id, short_channel_id,
sizeof(c->short_channel_id));
c->flags = direction;
return false;
}else if(get_connection_by_cid(rstate, channel_id, direction)) {
}else if(get_connection_by_scid(rstate, short_channel_id, direction)) {
return false;
}

c = half_add_connection(rstate, from, to, channel_id, direction);
c = half_add_connection(rstate, from, to, short_channel_id, direction);

/* Remember the announcement so we can forward it to new peers */
tal_free(c->channel_announcement);
Expand Down Expand Up @@ -647,7 +648,7 @@ void handle_channel_announcement(
bool forward = false;
secp256k1_ecdsa_signature node_signature_1;
secp256k1_ecdsa_signature node_signature_2;
struct channel_id channel_id;
struct short_channel_id short_channel_id;
secp256k1_ecdsa_signature bitcoin_signature_1;
secp256k1_ecdsa_signature bitcoin_signature_2;
struct pubkey node_id_1;
Expand All @@ -662,7 +663,7 @@ void handle_channel_announcement(
&node_signature_1, &node_signature_2,
&bitcoin_signature_1,
&bitcoin_signature_2,
&channel_id,
&short_channel_id,
&node_id_1, &node_id_2,
&bitcoin_key_1, &bitcoin_key_2,
&features)) {
Expand All @@ -676,16 +677,16 @@ void handle_channel_announcement(

log_debug(rstate->base_log,
"Received channel_announcement for channel %d:%d:%d",
channel_id.blocknum,
channel_id.txnum,
channel_id.outnum
short_channel_id.blocknum,
short_channel_id.txnum,
short_channel_id.outnum
);

forward |= add_channel_direction(rstate, &node_id_1,
&node_id_2, 0, &channel_id,
&node_id_2, 0, &short_channel_id,
serialized);
forward |= add_channel_direction(rstate, &node_id_2,
&node_id_1, 1, &channel_id,
&node_id_1, 1, &short_channel_id,
serialized);
if (!forward){
log_debug(rstate->base_log, "Not forwarding channel_announcement");
Expand All @@ -694,7 +695,7 @@ void handle_channel_announcement(
}

u8 *tag = tal_arr(tmpctx, u8, 0);
towire_channel_id(&tag, &channel_id);
towire_short_channel_id(&tag, &short_channel_id);
queue_broadcast(rstate->broadcasts, WIRE_CHANNEL_ANNOUNCEMENT,
tag, serialized);

Expand All @@ -706,7 +707,7 @@ void handle_channel_update(struct routing_state *rstate, const u8 *update, size_
u8 *serialized;
struct node_connection *c;
secp256k1_ecdsa_signature signature;
struct channel_id channel_id;
struct short_channel_id short_channel_id;
u32 timestamp;
u16 flags;
u16 expiry;
Expand All @@ -716,7 +717,7 @@ void handle_channel_update(struct routing_state *rstate, const u8 *update, size_
const tal_t *tmpctx = tal_tmpctx(rstate);

serialized = tal_dup_arr(tmpctx, u8, update, len, 0);
if (!fromwire_channel_update(serialized, NULL, &signature, &channel_id,
if (!fromwire_channel_update(serialized, NULL, &signature, &short_channel_id,
&timestamp, &flags, &expiry,
&htlc_minimum_msat, &fee_base_msat,
&fee_proportional_millionths)) {
Expand All @@ -726,19 +727,19 @@ void handle_channel_update(struct routing_state *rstate, const u8 *update, size_


log_debug(rstate->base_log, "Received channel_update for channel %d:%d:%d(%d)",
channel_id.blocknum,
channel_id.txnum,
channel_id.outnum,
short_channel_id.blocknum,
short_channel_id.txnum,
short_channel_id.outnum,
flags & 0x01
);

c = get_connection_by_cid(rstate, &channel_id, flags & 0x1);
c = get_connection_by_scid(rstate, &short_channel_id, flags & 0x1);

if (!c) {
log_debug(rstate->base_log, "Ignoring update for unknown channel %d:%d:%d",
channel_id.blocknum,
channel_id.txnum,
channel_id.outnum
short_channel_id.blocknum,
short_channel_id.txnum,
short_channel_id.outnum
);
tal_free(tmpctx);
return;
Expand All @@ -756,14 +757,14 @@ void handle_channel_update(struct routing_state *rstate, const u8 *update, size_
c->proportional_fee = fee_proportional_millionths;
c->active = true;
log_debug(rstate->base_log, "Channel %d:%d:%d(%d) was updated.",
channel_id.blocknum,
channel_id.txnum,
channel_id.outnum,
short_channel_id.blocknum,
short_channel_id.txnum,
short_channel_id.outnum,
flags
);

u8 *tag = tal_arr(tmpctx, u8, 0);
towire_channel_id(&tag, &channel_id);
towire_short_channel_id(&tag, &short_channel_id);
queue_broadcast(rstate->broadcasts,
WIRE_CHANNEL_UPDATE,
tag,
Expand Down
12 changes: 6 additions & 6 deletions daemon/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct node_connection {
u32 htlc_minimum_msat;

/* The channel ID, as determined by the anchor transaction */
struct channel_id channel_id;
struct short_channel_id short_channel_id;

/* Flags as specified by the `channel_update`s, among other
* things indicated direction wrt the `channel_id` */
Expand Down Expand Up @@ -121,7 +121,7 @@ struct node_connection *add_connection(struct routing_state *rstate,
struct node_connection *half_add_connection(struct routing_state *rstate,
const struct pubkey *from,
const struct pubkey *to,
const struct channel_id *chanid,
const struct short_channel_id *schanid,
const u16 flags);

/* Get an existing connection between `from` and `to`, NULL if no such
Expand All @@ -130,10 +130,10 @@ struct node_connection *get_connection(struct routing_state *rstate,
const struct pubkey *from,
const struct pubkey *to);

/* Given a channel_id, retrieve the matching connection, or NULL if it is
/* Given a short_channel_id, retrieve the matching connection, or NULL if it is
* unknown. */
struct node_connection *get_connection_by_cid(const struct routing_state *rstate,
const struct channel_id *chanid,
struct node_connection *get_connection_by_scid(const struct routing_state *rstate,
const struct short_channel_id *schanid,
const u8 direction);

void remove_connection(struct routing_state *rstate,
Expand All @@ -156,7 +156,7 @@ bool add_channel_direction(struct routing_state *rstate,
const struct pubkey *from,
const struct pubkey *to,
const int direction,
const struct channel_id *channel_id,
const struct short_channel_id *short_channel_id,
const u8 *announcement);

bool read_ip(const tal_t *ctx, const u8 *addresses, char **hostname, int *port);
Expand Down
2 changes: 1 addition & 1 deletion lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
*
* 1. type: 128 (`update_add_htlc`)
* 2. data:
* * [8:channel-id]
* * [32:channel-id]
* * [8:id]
* * [4:amount-msat]
* * [4:cltv-expiry]
Expand Down
4 changes: 2 additions & 2 deletions lightningd/channel_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* 1. type: 32 (`open_channel`)
* 2. data:
* * [8:temporary-channel-id]
* * [32:temporary-channel-id]
* * [8:funding-satoshis]
* * [8:push-msat]
* * [8:dust-limit-satoshis]
Expand All @@ -21,7 +21,7 @@
*...
* 1. type: 33 (`accept_channel`)
* 2. data:
* * [8:temporary-channel-id]
* * [32:temporary-channel-id]
* * [8:dust-limit-satoshis]
* * [8:max-htlc-value-in-flight-msat]
* * [8:channel-reserve-satoshis]
Expand Down
2 changes: 1 addition & 1 deletion lightningd/cryptomsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void hkdf_two_keys(struct sha256 *out1, struct sha256 *out2,
{
/* BOLT #8:
*
* * `HKDF(salt,ikm)`: a function is defined in [5](#reference-5),
* * `HKDF(salt,ikm)`: a function is defined in [3](#reference-3),
* evaluated with a zero-length `info` field.
* * All invocations of the `HKDF` implicitly return `64-bytes`
* of cryptographic randomness using the extract-and-expand
Expand Down
2 changes: 1 addition & 1 deletion lightningd/handshake/handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static void hkdf_two_keys(struct secret *out1, struct secret *out2,
{
/* BOLT #8:
*
* * `HKDF(salt,ikm)`: a function is defined in [5](#reference-5),
* * `HKDF(salt,ikm)`: a function is defined in [3](#reference-3),
* evaluated with a zero-length `info` field.
* * All invocations of the `HKDF` implicitly return `64-bytes`
* of cryptographic randomness using the extract-and-expand
Expand Down
Loading

0 comments on commit 7419fde

Please sign in to comment.