diff --git a/daemon/p2p_announce.c b/daemon/p2p_announce.c index 4b8dcca02..801726d74 100644 --- a/daemon/p2p_announce.c +++ b/daemon/p2p_announce.c @@ -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, @@ -41,7 +41,7 @@ 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, @@ -49,7 +49,7 @@ static void broadcast_channel_update(struct lightningd_state *dstate, struct pee 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); } @@ -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]; @@ -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)); @@ -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], @@ -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); diff --git a/daemon/routing.c b/daemon/routing.c index 7a6bcef33..fefdd8fab 100644 --- a/daemon/routing.c +++ b/daemon/routing.c @@ -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; @@ -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; } @@ -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; @@ -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; } @@ -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); @@ -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; @@ -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)) { @@ -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"); @@ -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); @@ -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; @@ -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, ×tamp, &flags, &expiry, &htlc_minimum_msat, &fee_base_msat, &fee_proportional_millionths)) { @@ -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; @@ -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, diff --git a/daemon/routing.h b/daemon/routing.h index 735170706..1e192bb04 100644 --- a/daemon/routing.h +++ b/daemon/routing.h @@ -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` */ @@ -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 @@ -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, @@ -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); diff --git a/lightningd/channel.c b/lightningd/channel.c index 32b27475a..68d452d4d 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -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] diff --git a/lightningd/channel_config.h b/lightningd/channel_config.h index f9f6a2865..f3dcff877 100644 --- a/lightningd/channel_config.h +++ b/lightningd/channel_config.h @@ -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] @@ -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] diff --git a/lightningd/cryptomsg.c b/lightningd/cryptomsg.c index 0e6e3199b..e28547360 100644 --- a/lightningd/cryptomsg.c +++ b/lightningd/cryptomsg.c @@ -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 diff --git a/lightningd/handshake/handshake.c b/lightningd/handshake/handshake.c index 3d320ea8b..93c89e4db 100644 --- a/lightningd/handshake/handshake.c +++ b/lightningd/handshake/handshake.c @@ -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 diff --git a/lightningd/opening/opening.c b/lightningd/opening/opening.c index f7fc11317..fe93bbd38 100644 --- a/lightningd/opening/opening.c +++ b/lightningd/opening/opening.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -53,7 +54,7 @@ struct state { u64 funding_satoshis, push_msat; u32 feerate_per_kw; struct sha256_double funding_txid; - u8 funding_txout; + u16 funding_txout; /* Secret keys and basepoint secrets. */ struct secrets our_secrets; @@ -257,10 +258,39 @@ static void set_reserve(u64 *reserve, u64 funding) *reserve = (funding + 99) / 100; } +/* BOLT #2: + * + * This message introduces the `channel-id` which identifies , which is + * derived from the funding transaction by combining the `funding-txid` and + * the `funding-output-index` using big-endian exclusive-OR + * (ie. `funding-output-index` alters the last two bytes). + */ +static void derive_channel_id(struct channel_id *channel_id, + struct sha256_double *txid, u16 txout) +{ + BUILD_ASSERT(sizeof(*channel_id) == sizeof(*txid)); + memcpy(channel_id, txid, sizeof(*channel_id)); + channel_id->id[sizeof(*channel_id)-2] ^= txout >> 8; + channel_id->id[sizeof(*channel_id)-1] ^= txout; +} + +/* BOLT #2: + * + * A sending node MUST ensure `temporary-channel-id` is unique from any other + * channel id with the same peer. + */ +static void temporary_channel_id(struct channel_id *channel_id) +{ + size_t i; + + for (i = 0; i < sizeof(*channel_id); i++) + channel_id->id[i] = pseudorand(256); +} + static u8 *open_channel(struct state *state, const struct points *ours, u32 max_minimum_depth) { - struct channel_id tmpid, tmpid2; + struct channel_id channel_id, id_in; u8 *msg; struct bitcoin_tx *tx; struct points theirs; @@ -269,14 +299,7 @@ static u8 *open_channel(struct state *state, const struct points *ours, set_reserve(&state->localconf.channel_reserve_satoshis, state->funding_satoshis); - /* BOLT #2: - * - * A sending node MUST set the most significant bit in - * `temporary-channel-id`, and MUST ensure it is unique from any other - * channel id with the same peer. - */ - /* We don't support more than one channel, so this is easy. */ - memset(&tmpid, 0xFF, sizeof(tmpid)); + temporary_channel_id(&channel_id); /* BOLT #2: * @@ -295,7 +318,7 @@ static u8 *open_channel(struct state *state, const struct points *ours, "push-msat must be < %"PRIu64, 1000 * state->funding_satoshis); - msg = towire_open_channel(state, &tmpid, + msg = towire_open_channel(state, &channel_id, state->funding_satoshis, state->push_msat, state->localconf.dust_limit_satoshis, state->localconf.max_htlc_value_in_flight_msat, @@ -327,7 +350,7 @@ static u8 *open_channel(struct state *state, const struct points *ours, * `delayed-payment-basepoint` are not valid DER-encoded compressed * secp256k1 pubkeys. */ - if (!fromwire_accept_channel(msg, NULL, &tmpid2, + if (!fromwire_accept_channel(msg, NULL, &id_in, &state->remoteconf->dust_limit_satoshis, &state->remoteconf ->max_htlc_value_in_flight_msat, @@ -349,11 +372,11 @@ static u8 *open_channel(struct state *state, const struct points *ours, * * The `temporary-channel-id` MUST be the same as the * `temporary-channel-id` in the `open_channel` message. */ - if (!structeq(&tmpid, &tmpid2)) + if (!structeq(&id_in, &channel_id)) peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_READ_FAILED, "accept_channel ids don't match: sent %s got %s", - type_to_string(msg, struct channel_id, &tmpid), - type_to_string(msg, struct channel_id, &tmpid2)); + type_to_string(msg, struct channel_id, &id_in), + type_to_string(msg, struct channel_id, &channel_id)); /* BOLT #2: * @@ -421,7 +444,7 @@ static u8 *open_channel(struct state *state, const struct points *ours, type_to_string(trc, struct bitcoin_tx, tx), type_to_string(trc, struct pubkey, &ours->funding_pubkey)); - msg = towire_funding_created(state, &tmpid, + msg = towire_funding_created(state, &channel_id, &state->funding_txid.sha, state->funding_txout, &sig); @@ -442,15 +465,27 @@ static u8 *open_channel(struct state *state, const struct points *ours, peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_READ_FAILED, "Reading funding_signed"); - if (!fromwire_funding_signed(msg, NULL, &tmpid2, &sig)) + if (!fromwire_funding_signed(msg, NULL, &id_in, &sig)) peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_READ_FAILED, "Parsing funding_signed (%s)", wire_type_name(fromwire_peektype(msg))); - if (!structeq(&tmpid, &tmpid2)) + + /* BOLT #2: + * + * This message introduces the `channel-id` which identifies , which + * is derived from the funding transaction by combining the + * `funding-txid` and the `funding-output-index` using big-endian + * exclusive-OR (ie. `funding-output-index` alters the last two + * bytes). + */ + derive_channel_id(&channel_id, + &state->funding_txid, state->funding_txout); + + if (!structeq(&id_in, &channel_id)) peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_READ_FAILED, - "funding_signed ids don't match: sent %s got %s", - type_to_string(msg, struct channel_id, &tmpid), - type_to_string(msg, struct channel_id, &tmpid2)); + "funding_signed ids don't match: expceted %s got %s", + type_to_string(msg, struct channel_id, &channel_id), + type_to_string(msg, struct channel_id, &id_in)); /* BOLT #2: * @@ -489,7 +524,7 @@ static u8 *open_channel(struct state *state, const struct points *ours, static u8 *recv_channel(struct state *state, const struct points *ours, u32 min_feerate, u32 max_feerate, const u8 *peer_msg) { - struct channel_id tmpid, tmpid2; + struct channel_id id_in, channel_id; struct points theirs; secp256k1_ecdsa_signature theirsig, sig; struct bitcoin_tx *tx; @@ -504,7 +539,7 @@ static u8 *recv_channel(struct state *state, const struct points *ours, * `delayed-payment-basepoint` are not valid DER-encoded compressed * secp256k1 pubkeys. */ - if (!fromwire_open_channel(peer_msg, NULL, &tmpid, + if (!fromwire_open_channel(peer_msg, NULL, &channel_id, &state->funding_satoshis, &state->push_msat, &state->remoteconf->dust_limit_satoshis, &state->remoteconf->max_htlc_value_in_flight_msat, @@ -561,7 +596,7 @@ static u8 *recv_channel(struct state *state, const struct points *ours, state->funding_satoshis); check_config_bounds(state, state->remoteconf); - msg = towire_accept_channel(state, &tmpid, + msg = towire_accept_channel(state, &channel_id, state->localconf.dust_limit_satoshis, state->localconf .max_htlc_value_in_flight_msat, @@ -585,7 +620,7 @@ static u8 *recv_channel(struct state *state, const struct points *ours, peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_READ_FAILED, "Reading funding_created"); - if (!fromwire_funding_created(msg, NULL, &tmpid2, + if (!fromwire_funding_created(msg, NULL, &id_in, &state->funding_txid.sha, &state->funding_txout, &theirsig)) @@ -596,11 +631,11 @@ static u8 *recv_channel(struct state *state, const struct points *ours, * * The sender MUST set `temporary-channel-id` the same as the * `temporary-channel-id` in the `open_channel` message. */ - if (!structeq(&tmpid, &tmpid2)) + if (!structeq(&id_in, &channel_id)) peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_READ_FAILED, - "funding_created ids don't match: sent %s got %s", - type_to_string(msg, struct channel_id, &tmpid), - type_to_string(msg, struct channel_id, &tmpid2)); + "funding_created ids don't match: sent %s got %s", + type_to_string(msg, struct channel_id, &channel_id), + type_to_string(msg, struct channel_id, &id_in)); state->channel = new_channel(state, &state->funding_txid, @@ -638,6 +673,17 @@ static u8 *recv_channel(struct state *state, const struct points *ours, type_to_string(trc, struct pubkey, &theirs.funding_pubkey)); + /* BOLT #2: + * + * This message introduces the `channel-id` which identifies , which + * is derived from the funding transaction by combining the + * `funding-txid` and the `funding-output-index` using big-endian + * exclusive-OR (ie. `funding-output-index` alters the last two + * bytes). + */ + derive_channel_id(&channel_id, + &state->funding_txid, state->funding_txout); + /* BOLT #2: * * ### The `funding_signed` message @@ -652,7 +698,7 @@ static u8 *recv_channel(struct state *state, const struct points *ours, &ours->funding_pubkey, &theirs.funding_pubkey, tx); - msg = towire_funding_signed(state, &tmpid, &sig); + msg = towire_funding_signed(state, &channel_id, &sig); if (!sync_crypto_write(&state->cs, PEER_FD, msg)) peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_WRITE_FAILED, "Writing funding_signed"); diff --git a/lightningd/opening/opening_control_wire.csv b/lightningd/opening/opening_control_wire.csv index 6be108b64..aad98e990 100644 --- a/lightningd/opening/opening_control_wire.csv +++ b/lightningd/opening/opening_control_wire.csv @@ -25,7 +25,7 @@ opening_open_resp,0,remote_fundingkey,33 # Now we give the funding txid and outnum. opening_open_funding,2 opening_open_funding,0,txid,32,struct sha256_double -opening_open_funding,32,txout,1,u8 +opening_open_funding,32,txout,2,u16 # This gives their sig, means we can broadcast tx: we're done. opening_open_funding_resp,102 @@ -47,7 +47,7 @@ opening_accept,10,msg,len,u8 # This gives the txid of their funding tx: we're done. opening_accept_resp,103 opening_accept_resp,0,funding_txid,32,struct sha256_double -opening_accept_resp,32,funding_txout,1,u8 +opening_accept_resp,32,funding_txout,2,u16 opening_accept_resp,33,their_config,36,struct channel_config opening_accept_resp,69,first_commit_sig,64,secp256k1_ecdsa_signature opening_accept_resp,133,crypto_state,144,struct crypto_state diff --git a/lightningd/peer_failed.c b/lightningd/peer_failed.c index 6d622bae7..a3a60ce0d 100644 --- a/lightningd/peer_failed.c +++ b/lightningd/peer_failed.c @@ -19,12 +19,11 @@ void peer_failed(int peer_fd, struct crypto_state *cs, /* BOLT #1: * - * A node sending `error` MUST fail the channel referred to by the - * `channel-id`, or if `channel-id` is `0xFFFFFFFFFFFFFFFF` it MUST - * fail all channels and MUST close the connection. + * The channel is referred to by `channel-id` unless `channel-id` is + * zero (ie. all bytes zero), in which case it refers to all channels. */ if (!channel_id) { - memset(&all_channels, 0xFF, sizeof(all_channels)); + memset(&all_channels, 0, sizeof(all_channels)); channel_id = &all_channels; } diff --git a/tools/generate-wire.py b/tools/generate-wire.py index c3b27c377..09d183a36 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -21,8 +21,10 @@ class FieldType(object): def _typesize(typename): if typename == 'pad': return 1 - elif typename == 'struct channel_id': + elif typename == 'struct short_channel_id': return 8 + elif typename == 'struct channel_id': + return 32 elif typename == 'struct ipv6': return 16 elif typename == 'secp256k1_ecdsa_signature': @@ -104,6 +106,9 @@ class Field(object): if fieldname.startswith('pad'): return FieldType('pad') + if fieldname.endswith('short_channel_id'): + return FieldType('struct short_channel_id') + if fieldname.endswith('channel_id'): return FieldType('struct channel_id') diff --git a/type_to_string.h b/type_to_string.h index a96657544..0169776d3 100644 --- a/type_to_string.h +++ b/type_to_string.h @@ -20,6 +20,7 @@ union printable_types { const struct netaddr *netaddr; const secp256k1_pubkey *secp256k1_pubkey; const struct channel_id *channel_id; + const struct short_channel_id *short_channel_id; const struct privkey *privkey; const secp256k1_ecdsa_signature *secp256k1_ecdsa_signature; const struct channel *channel; diff --git a/wire/fromwire.c b/wire/fromwire.c index 9c51e51a3..ab49e4a2e 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -121,16 +121,22 @@ void fromwire_secp256k1_ecdsa_signature(const u8 **cursor, void fromwire_channel_id(const u8 **cursor, size_t *max, struct channel_id *channel_id) +{ + fromwire(cursor, max, channel_id, sizeof(*channel_id)); +} + +void fromwire_short_channel_id(const u8 **cursor, size_t *max, + struct short_channel_id *short_channel_id) { be32 txnum = 0; u8 outnum; - channel_id->blocknum = fromwire_u32(cursor, max); + short_channel_id->blocknum = fromwire_u32(cursor, max); /* Pulling 3 bytes off wire is tricky; they're big-endian. */ fromwire(cursor, max, (char *)&txnum + 1, 3); - channel_id->txnum = be32_to_cpu(txnum); + short_channel_id->txnum = be32_to_cpu(txnum); fromwire(cursor, max, &outnum, 1); - channel_id->outnum = outnum; + short_channel_id->outnum = outnum; } void fromwire_sha256(const u8 **cursor, size_t *max, struct sha256 *sha256) @@ -200,8 +206,11 @@ void fromwire_sha256_double_array(const u8 **cursor, size_t *max, for (i = 0; i < num; i++) fromwire_sha256_double(cursor, max, arr + i); } -static char *fmt_channel_id(const tal_t *ctx, const struct channel_id *id) + +static char *fmt_short_channel_id(const tal_t *ctx, + const struct short_channel_id *id) { return tal_fmt(ctx, "%u/%u/%u", id->blocknum, id->txnum, id->outnum); } -REGISTER_TYPE_TO_STRING(channel_id, fmt_channel_id); +REGISTER_TYPE_TO_STRING(short_channel_id, fmt_short_channel_id); +REGISTER_TYPE_TO_HEXSTR(channel_id); diff --git a/wire/gen_peer_wire_csv b/wire/gen_peer_wire_csv index 057577822..59ae8b6e3 100644 --- a/wire/gen_peer_wire_csv +++ b/wire/gen_peer_wire_csv @@ -4,105 +4,105 @@ init,2,globalfeatures,gflen init,2+gflen,lflen,2 init,4+gflen,localfeatures,lflen error,17 -error,0,channel-id,8 -error,8,len,2 -error,10,data,len +error,0,channel-id,32 +error,32,len,2 +error,34,data,len open_channel,32 -open_channel,0,temporary-channel-id,8 -open_channel,8,funding-satoshis,8 -open_channel,16,push-msat,8 -open_channel,24,dust-limit-satoshis,8 -open_channel,32,max-htlc-value-in-flight-msat,8 -open_channel,40,channel-reserve-satoshis,8 -open_channel,48,htlc-minimum-msat,4 -open_channel,52,feerate-per-kw,4 -open_channel,56,to-self-delay,2 -open_channel,58,max-accepted-htlcs,2 -open_channel,60,funding-pubkey,33 -open_channel,93,revocation-basepoint,33 -open_channel,126,payment-basepoint,33 -open_channel,159,delayed-payment-basepoint,33 -open_channel,192,first-per-commitment-point,33 +open_channel,0,temporary-channel-id,32 +open_channel,32,funding-satoshis,8 +open_channel,40,push-msat,8 +open_channel,48,dust-limit-satoshis,8 +open_channel,56,max-htlc-value-in-flight-msat,8 +open_channel,64,channel-reserve-satoshis,8 +open_channel,72,htlc-minimum-msat,4 +open_channel,76,feerate-per-kw,4 +open_channel,80,to-self-delay,2 +open_channel,82,max-accepted-htlcs,2 +open_channel,84,funding-pubkey,33 +open_channel,117,revocation-basepoint,33 +open_channel,150,payment-basepoint,33 +open_channel,183,delayed-payment-basepoint,33 +open_channel,216,first-per-commitment-point,33 accept_channel,33 -accept_channel,0,temporary-channel-id,8 -accept_channel,8,dust-limit-satoshis,8 -accept_channel,16,max-htlc-value-in-flight-msat,8 -accept_channel,24,channel-reserve-satoshis,8 -accept_channel,32,minimum-depth,4 -accept_channel,36,htlc-minimum-msat,4 -accept_channel,40,to-self-delay,2 -accept_channel,42,max-accepted-htlcs,2 -accept_channel,44,funding-pubkey,33 -accept_channel,77,revocation-basepoint,33 -accept_channel,110,payment-basepoint,33 -accept_channel,143,delayed-payment-basepoint,33 -accept_channel,176,first-per-commitment-point,33 +accept_channel,0,temporary-channel-id,32 +accept_channel,32,dust-limit-satoshis,8 +accept_channel,40,max-htlc-value-in-flight-msat,8 +accept_channel,48,channel-reserve-satoshis,8 +accept_channel,56,minimum-depth,4 +accept_channel,60,htlc-minimum-msat,4 +accept_channel,64,to-self-delay,2 +accept_channel,66,max-accepted-htlcs,2 +accept_channel,68,funding-pubkey,33 +accept_channel,101,revocation-basepoint,33 +accept_channel,134,payment-basepoint,33 +accept_channel,167,delayed-payment-basepoint,33 +accept_channel,200,first-per-commitment-point,33 funding_created,34 -funding_created,0,temporary-channel-id,8 -funding_created,8,txid,32 -funding_created,40,output-index,1 -funding_created,41,signature,64 +funding_created,0,temporary-channel-id,32 +funding_created,32,funding-txid,32 +funding_created,64,funding-output-index,2 +funding_created,66,signature,64 funding_signed,35 -funding_signed,0,temporary-channel-id,8 -funding_signed,8,signature,64 +funding_signed,0,channel-id,32 +funding_signed,32,signature,64 funding_locked,36 -funding_locked,0,temporary-channel-id,8 -funding_locked,8,channel-id,8 -funding_locked,16,next-per-commitment-point,33 +funding_locked,0,channel-id,32 +funding_locked,32,next-per-commitment-point,33 shutdown,38 -shutdown,0,channel-id,8 -shutdown,8,len,2 -shutdown,10,scriptpubkey,len +shutdown,0,channel-id,32 +shutdown,32,len,2 +shutdown,34,scriptpubkey,len closing_signed,39 -closing_signed,0,channel-id,8 -closing_signed,8,fee-satoshis,8 -closing_signed,16,signature,64 +closing_signed,0,channel-id,32 +closing_signed,32,fee-satoshis,8 +closing_signed,40,signature,64 update_add_htlc,128 -update_add_htlc,0,channel-id,8 -update_add_htlc,8,id,8 -update_add_htlc,16,amount-msat,4 -update_add_htlc,20,cltv-expiry,4 -update_add_htlc,24,payment-hash,32 -update_add_htlc,56,onion-routing-packet,1254 +update_add_htlc,0,channel-id,32 +update_add_htlc,32,id,8 +update_add_htlc,40,amount-msat,4 +update_add_htlc,44,cltv-expiry,4 +update_add_htlc,48,payment-hash,32 +update_add_htlc,80,onion-routing-packet,1254 update_fulfill_htlc,130 -update_fulfill_htlc,0,channel-id,8 -update_fulfill_htlc,8,id,8 -update_fulfill_htlc,16,payment-preimage,32 +update_fulfill_htlc,0,channel-id,32 +update_fulfill_htlc,32,id,8 +update_fulfill_htlc,40,payment-preimage,32 update_fail_htlc,131 -update_fail_htlc,0,channel-id,8 -update_fail_htlc,8,id,8 -update_fail_htlc,16,len,2 -update_fail_htlc,18,reason,len +update_fail_htlc,0,channel-id,32 +update_fail_htlc,32,id,8 +update_fail_htlc,40,len,2 +update_fail_htlc,42,reason,len update_fail_malformed_htlc,135 -update_fail_malformed_htlc,0,channel-id,8 -update_fail_malformed_htlc,8,id,8 -update_fail_malformed_htlc,16,sha256-of-onion,32 -update_fail_malformed_htlc,48,failure-code,2 +update_fail_malformed_htlc,0,channel-id,32 +update_fail_malformed_htlc,32,id,8 +update_fail_malformed_htlc,40,sha256-of-onion,32 +update_fail_malformed_htlc,72,failure-code,2 commit_sig,132 -commit_sig,0,channel-id,8 -commit_sig,8,signature,64 -commit_sig,72,num-htlcs,2 -commit_sig,74,htlc-signature,num-htlcs*64 +commit_sig,0,channel-id,32 +commit_sig,32,signature,64 +commit_sig,96,num-htlcs,2 +commit_sig,98,htlc-signature,num-htlcs*64 revoke_and_ack,133 -revoke_and_ack,0,channel-id,8 -revoke_and_ack,8,per-commitment-secret,32 -revoke_and_ack,40,next-per-commitment-point,33 -revoke_and_ack,73,padding,1 -revoke_and_ack,74,num-htlc-timeouts,2 -revoke_and_ack,76,htlc-timeout-signature,num-htlc-timeouts*64 +revoke_and_ack,0,channel-id,32 +revoke_and_ack,32,per-commitment-secret,32 +revoke_and_ack,64,next-per-commitment-point,33 +revoke_and_ack,97,padding,1 +revoke_and_ack,98,num-htlc-timeouts,2 +revoke_and_ack,100,htlc-timeout-signature,num-htlc-timeouts*64 update_fee,134 -update_fee,0,channel-id,8 -update_fee,8,feerate-per-kw,4 +update_fee,0,channel-id,32 +update_fee,32,feerate-per-kw,4 announcement_signatures,259 -announcement_signatures,0,channel-id,8 -announcement_signatures,8,node-signature,64 -announcement_signatures,72,bitcoin-signature,64 +announcement_signatures,0,channel-id,32 +announcement_signatures,32,short-channel-id,8 +announcement_signatures,40,node-signature,64 +announcement_signatures,104,bitcoin-signature,64 channel_announcement,256 channel_announcement,0,node-signature-1,64 channel_announcement,64,node-signature-2,64 channel_announcement,128,bitcoin-signature-1,64 channel_announcement,192,bitcoin-signature-2,64 -channel_announcement,256,channel-id,8 +channel_announcement,256,short-channel-id,8 channel_announcement,264,node-id-1,33 channel_announcement,297,node-id-2,33 channel_announcement,330,bitcoin-key-1,33 @@ -121,7 +121,7 @@ node_announcement,138+flen,addrlen,2 node_announcement,140+flen,addresses,addrlen channel_update,258 channel_update,0,signature,64 -channel_update,64,channel-id,8 +channel_update,64,short-channel-id,8 channel_update,72,timestamp,4 channel_update,76,flags,2 channel_update,78,cltv-expiry-delta,2 diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c index 9af8456e7..47e4dae5f 100644 --- a/wire/test/run-peer-wire.c +++ b/wire/test/run-peer-wire.c @@ -91,7 +91,7 @@ struct msg_closing_signed { struct msg_funding_created { struct channel_id temporary_channel_id; struct sha256 txid; - u8 output_index; + u16 output_index; secp256k1_ecdsa_signature signature; }; struct msg_accept_channel { @@ -131,7 +131,7 @@ struct msg_revoke_and_ack { }; struct msg_channel_update { secp256k1_ecdsa_signature signature; - struct channel_id channel_id; + struct short_channel_id short_channel_id; u32 timestamp; u16 flags; u16 expiry; @@ -140,12 +140,12 @@ struct msg_channel_update { u32 fee_proportional_millionths; }; struct msg_funding_locked { - struct channel_id temporary_channel_id; struct channel_id channel_id; struct pubkey next_per_commitment_point; }; struct msg_announcement_signatures { struct channel_id channel_id; + struct short_channel_id short_channel_id; secp256k1_ecdsa_signature announcement_node_signature; secp256k1_ecdsa_signature announcement_bitcoin_signature; }; @@ -190,7 +190,7 @@ struct msg_channel_announcement { secp256k1_ecdsa_signature node_signature_2; secp256k1_ecdsa_signature bitcoin_signature_1; secp256k1_ecdsa_signature bitcoin_signature_2; - struct channel_id channel_id; + struct short_channel_id short_channel_id; struct pubkey node_id_1; struct pubkey node_id_2; struct pubkey bitcoin_key_1; @@ -222,7 +222,7 @@ static void *towire_struct_channel_announcement(const tal_t *ctx, &s->node_signature_2, &s->bitcoin_signature_1, &s->bitcoin_signature_2, - &s->channel_id, + &s->short_channel_id, &s->node_id_1, &s->node_id_2, &s->bitcoin_key_1, @@ -238,7 +238,7 @@ static struct msg_channel_announcement *fromwire_struct_channel_announcement(con &s->node_signature_2, &s->bitcoin_signature_1, &s->bitcoin_signature_2, - &s->channel_id, + &s->short_channel_id, &s->node_id_1, &s->node_id_2, &s->bitcoin_key_1, @@ -366,7 +366,7 @@ static void *towire_struct_channel_update(const tal_t *ctx, { return towire_channel_update(ctx, &s->signature, - &s->channel_id, + &s->short_channel_id, s->timestamp, s->flags, s->expiry, @@ -381,7 +381,7 @@ static struct msg_channel_update *fromwire_struct_channel_update(const tal_t *ct if (fromwire_channel_update(p, plen, &s->signature, - &s->channel_id, + &s->short_channel_id, &s->timestamp, &s->flags, &s->expiry, @@ -396,7 +396,6 @@ static void *towire_struct_funding_locked(const tal_t *ctx, const struct msg_funding_locked *s) { return towire_funding_locked(ctx, - &s->temporary_channel_id, &s->channel_id, &s->next_per_commitment_point); } @@ -406,7 +405,6 @@ static struct msg_funding_locked *fromwire_struct_funding_locked(const tal_t *ct struct msg_funding_locked *s = tal(ctx, struct msg_funding_locked); if (fromwire_funding_locked(p, plen, - &s->temporary_channel_id, &s->channel_id, &s->next_per_commitment_point)) return s; @@ -418,6 +416,7 @@ static void *towire_struct_announcement_signatures(const tal_t *ctx, { return towire_announcement_signatures(ctx, &s->channel_id, + &s->short_channel_id, &s->announcement_node_signature, &s->announcement_bitcoin_signature); } @@ -428,6 +427,7 @@ static struct msg_announcement_signatures *fromwire_struct_announcement_signatur if (fromwire_announcement_signatures(p, plen, &s->channel_id, + &s->short_channel_id, &s->announcement_node_signature, &s->announcement_bitcoin_signature)) return s; diff --git a/wire/towire.c b/wire/towire.c index a78a8c177..db999e75e 100644 --- a/wire/towire.c +++ b/wire/towire.c @@ -70,10 +70,16 @@ void towire_secp256k1_ecdsa_signature(u8 **pptr, void towire_channel_id(u8 **pptr, const struct channel_id *channel_id) { - be32 txnum = cpu_to_be32(channel_id->txnum); - u8 outnum = channel_id->outnum; + towire(pptr, channel_id, sizeof(*channel_id)); +} - towire_u32(pptr, channel_id->blocknum); +void towire_short_channel_id(u8 **pptr, + const struct short_channel_id *short_channel_id) +{ + be32 txnum = cpu_to_be32(short_channel_id->txnum); + u8 outnum = short_channel_id->outnum; + + towire_u32(pptr, short_channel_id->blocknum); towire(pptr, (char *)&txnum + 1, 3); towire(pptr, &outnum, 1); } diff --git a/wire/wire.h b/wire/wire.h index 023d46db4..bd974b086 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -9,11 +9,14 @@ #include #include -struct channel_id { +struct short_channel_id { u32 blocknum; u32 txnum : 24; u8 outnum : 8; }; +struct channel_id { + u8 id[32]; +}; struct ipv6 { u8 addr[16]; }; @@ -27,6 +30,8 @@ void towire_privkey(u8 **pptr, const struct privkey *privkey); void towire_secp256k1_ecdsa_signature(u8 **pptr, const secp256k1_ecdsa_signature *signature); void towire_channel_id(u8 **pptr, const struct channel_id *channel_id); +void towire_short_channel_id(u8 **pptr, + const struct short_channel_id *short_channel_id); void towire_sha256(u8 **pptr, const struct sha256 *sha256); void towire_sha256_double(u8 **pptr, const struct sha256_double *sha256d); void towire_ipv6(u8 **pptr, const struct ipv6 *ipv6); @@ -59,6 +64,8 @@ void fromwire_secp256k1_ecdsa_signature(const u8 **cursor, size_t *max, secp256k1_ecdsa_signature *signature); void fromwire_channel_id(const u8 **cursor, size_t *max, struct channel_id *channel_id); +void fromwire_short_channel_id(const u8 **cursor, size_t *max, + struct short_channel_id *short_channel_id); void fromwire_sha256(const u8 **cursor, size_t *max, struct sha256 *sha256); void fromwire_sha256_double(const u8 **cursor, size_t *max, struct sha256_double *sha256d);