Skip to content

Commit

Permalink
Use node_id everywhere for nodes.
Browse files Browse the repository at this point in the history
I tried to just do gossipd, but it was uncontainable, so this ended up being
a complete sweep.

We didn't get much space saving in gossipd, even though we should save
24 bytes per node.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and niftynei committed Apr 9, 2019
1 parent b4455d5 commit a2fa699
Show file tree
Hide file tree
Showing 63 changed files with 685 additions and 578 deletions.
1 change: 1 addition & 0 deletions channeld/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ CHANNELD_COMMON_OBJS := \
common/key_derive.o \
common/memleak.o \
common/msg_queue.o \
common/node_id.o \
common/peer_billboard.o \
common/peer_failed.o \
common/permute_tx.o \
Expand Down
4 changes: 2 additions & 2 deletions channeld/channel_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ channel_init,,fee_proportional,u32
channel_init,,local_msatoshi,struct amount_msat
channel_init,,our_basepoints,struct basepoints
channel_init,,our_funding_pubkey,struct pubkey
channel_init,,local_node_id,struct pubkey
channel_init,,remote_node_id,struct pubkey
channel_init,,local_node_id,struct node_id
channel_init,,remote_node_id,struct node_id
channel_init,,commit_msec,u32
channel_init,,cltv_delta,u16
channel_init,,last_was_revoke,bool
Expand Down
25 changes: 19 additions & 6 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <common/key_derive.h>
#include <common/memleak.h>
#include <common/msg_queue.h>
#include <common/node_id.h>
#include <common/peer_billboard.h>
#include <common/peer_failed.h>
#include <common/ping.h>
Expand Down Expand Up @@ -117,7 +118,7 @@ struct peer {
u32 desired_feerate;

/* Announcement related information */
struct pubkey node_ids[NUM_SIDES];
struct node_id node_ids[NUM_SIDES];
struct short_channel_id short_channel_ids[NUM_SIDES];
secp256k1_ecdsa_signature announcement_node_sigs[NUM_SIDES];
secp256k1_ecdsa_signature announcement_bitcoin_sigs[NUM_SIDES];
Expand Down Expand Up @@ -343,6 +344,7 @@ static void send_announcement_signatures(struct peer *peer)
size_t offset = 258;
struct sha256_double hash;
const u8 *msg, *ca, *req;
struct pubkey mykey;

status_trace("Exchanging announcement signatures.");
ca = create_channel_announcement(tmpctx, peer);
Expand All @@ -358,8 +360,13 @@ static void send_announcement_signatures(struct peer *peer)

/* Double-check that HSM gave valid signatures. */
sha256_double(&hash, ca + offset, tal_count(ca) - offset);
if (!pubkey_from_node_id(&mykey, &peer->node_ids[LOCAL]))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Could not convert my id '%s' to pubkey",
type_to_string(tmpctx, struct node_id,
&peer->node_ids[LOCAL]));
if (!check_signed_hash(&hash, &peer->announcement_node_sigs[LOCAL],
&peer->node_ids[LOCAL])) {
&mykey)) {
/* It's ok to fail here, the channel announcement is
* unique, unlike the channel update which may have
* been replaced in the meantime. */
Expand Down Expand Up @@ -399,15 +406,21 @@ static u8 *create_channel_announcement(const tal_t *ctx, struct peer *peer)
second = LOCAL;
}

/* FIXME */
struct pubkey pk1, pk2;
if (!pubkey_from_node_id(&pk1, &peer->node_ids[first])
|| !pubkey_from_node_id(&pk2, &peer->node_ids[second]))
abort();

cannounce = towire_channel_announcement(
ctx, &peer->announcement_node_sigs[first],
&peer->announcement_node_sigs[second],
&peer->announcement_bitcoin_sigs[first],
&peer->announcement_bitcoin_sigs[second],
features,
&peer->chain_hash,
&peer->short_channel_ids[LOCAL], &peer->node_ids[first],
&peer->node_ids[second], &peer->channel->funding_pubkey[first],
&peer->short_channel_ids[LOCAL], &pk1,
&pk2, &peer->channel->funding_pubkey[first],
&peer->channel->funding_pubkey[second]);
tal_free(features);
return cannounce;
Expand Down Expand Up @@ -2883,8 +2896,8 @@ static void init_channel(struct peer *peer)
tal_free(failed);
tal_free(failed_sides);

peer->channel_direction = get_channel_direction(
&peer->node_ids[LOCAL], &peer->node_ids[REMOTE]);
peer->channel_direction = node_id_idx(&peer->node_ids[LOCAL],
&peer->node_ids[REMOTE]);

/* Default desired feerate is the feerate we set for them last. */
if (peer->channel->funder == LOCAL)
Expand Down
34 changes: 19 additions & 15 deletions common/bolt11.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,6 @@ static char *decode_n(struct bolt11 *b11,
u5 **data, size_t *data_len,
size_t data_length, bool *have_n)
{
u8 der[PUBKEY_CMPR_LEN];

if (*have_n)
return unknown_field(b11, hu5, data, data_len, 'n',
data_length);
Expand All @@ -294,10 +292,12 @@ static char *decode_n(struct bolt11 *b11,
return unknown_field(b11, hu5, data, data_len, 'n',
data_length);

pull_bits_certain(hu5, data, data_len, der, data_length * 5, false);
if (!pubkey_from_der(der, sizeof(der), &b11->receiver_id))
return tal_fmt(b11, "n: invalid pubkey %.*s",
(int)sizeof(der), der);
pull_bits_certain(hu5, data, data_len, &b11->receiver_id.k,
data_length * 5, false);
if (!node_id_valid(&b11->receiver_id))
return tal_fmt(b11, "n: invalid pubkey %s",
type_to_string(tmpctx, struct node_id,
&b11->receiver_id));

*have_n = true;
return NULL;
Expand Down Expand Up @@ -377,7 +377,7 @@ static char *decode_f(struct bolt11 *b11,
static bool fromwire_route_info(const u8 **cursor, size_t *max,
struct route_info *route_info)
{
fromwire_pubkey(cursor, max, &route_info->pubkey);
fromwire_node_id(cursor, max, &route_info->pubkey);
fromwire_short_channel_id(cursor, max, &route_info->short_channel_id);
route_info->fee_base_msat = fromwire_u32(cursor, max);
route_info->fee_proportional_millionths = fromwire_u32(cursor, max);
Expand All @@ -387,7 +387,7 @@ static bool fromwire_route_info(const u8 **cursor, size_t *max,

static void towire_route_info(u8 **pptr, const struct route_info *route_info)
{
towire_pubkey(pptr, &route_info->pubkey);
towire_node_id(pptr, &route_info->pubkey);
towire_short_channel_id(pptr, &route_info->short_channel_id);
towire_u32(pptr, route_info->fee_base_msat);
towire_u32(pptr, route_info->fee_proportional_millionths);
Expand Down Expand Up @@ -697,16 +697,22 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str,
* performing signature recovery.
*/
if (!have_n) {
struct pubkey k;
if (!secp256k1_ecdsa_recover(secp256k1_ctx,
&b11->receiver_id.pubkey,
&k.pubkey,
&sig,
(const u8 *)&hash))
return decode_fail(b11, fail,
"signature recovery failed");
node_id_from_pubkey(&b11->receiver_id, &k);
} else {
struct pubkey k;
/* n parsing checked this! */
if (!pubkey_from_node_id(&k, &b11->receiver_id))
abort();
if (!secp256k1_ecdsa_verify(secp256k1_ctx, &b11->sig,
(const u8 *)&hash,
&b11->receiver_id.pubkey))
&k.pubkey))
return decode_fail(b11, fail, "invalid signature");
}

Expand Down Expand Up @@ -785,12 +791,10 @@ static void encode_h(u5 **data, const struct sha256 *hash)
push_field(data, 'h', hash, 256);
}

static void encode_n(u5 **data, const struct pubkey *id)
static void encode_n(u5 **data, const struct node_id *id)
{
u8 der[PUBKEY_CMPR_LEN];

pubkey_to_der(der, id);
push_field(data, 'n', der, sizeof(der) * CHAR_BIT);
assert(node_id_valid(id));
push_field(data, 'n', id->k, sizeof(id->k) * CHAR_BIT);
}

static void encode_x(u5 **data, u64 expiry)
Expand Down
8 changes: 5 additions & 3 deletions common/bolt11.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <ccan/short_types/short_types.h>
#include <ccan/take/take.h>
#include <common/hash_u5.h>
#include <common/node_id.h>
#include <secp256k1_recovery.h>

/* We only have 10 bits for the field length, meaning < 640 bytes */
Expand All @@ -29,10 +30,11 @@ struct bolt11_field {
*/

struct route_info {
struct pubkey pubkey;
/* This is 33 bytes, so we pack cltv_expiry_delta next to it */
struct node_id pubkey;
u16 cltv_expiry_delta;
struct short_channel_id short_channel_id;
u32 fee_base_msat, fee_proportional_millionths;
u16 cltv_expiry_delta;
};

struct bolt11 {
Expand All @@ -41,7 +43,7 @@ struct bolt11 {
struct amount_msat *msat; /* NULL if not specified. */

struct sha256 payment_hash;
struct pubkey receiver_id;
struct node_id receiver_id;

/* description_hash valid if and only if description is NULL. */
const char *description;
Expand Down
21 changes: 13 additions & 8 deletions common/test/run-bolt11.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "../bech32.c"
#include "../bech32_util.c"
#include "../bolt11.c"
#include "../node_id.c"
#include "../hash_u5.c"
#include <ccan/err/err.h>
#include <ccan/mem/mem.h>
Expand All @@ -10,9 +11,9 @@
#include <wally_core.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for fromwire_pubkey */
void fromwire_pubkey(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct pubkey *pubkey UNNEEDED)
{ fprintf(stderr, "fromwire_pubkey called!\n"); abort(); }
/* Generated stub for fromwire_node_id */
void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED)
{ fprintf(stderr, "fromwire_node_id called!\n"); abort(); }
/* Generated stub for fromwire_short_channel_id */
void fromwire_short_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct short_channel_id *short_channel_id UNNEEDED)
Expand All @@ -23,9 +24,9 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for fromwire_u32 */
u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u32 called!\n"); abort(); }
/* Generated stub for towire_pubkey */
void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED)
{ fprintf(stderr, "towire_pubkey called!\n"); abort(); }
/* Generated stub for towire_node_id */
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "towire_node_id called!\n"); abort(); }
/* Generated stub for towire_short_channel_id */
void towire_short_channel_id(u8 **pptr UNNEEDED,
const struct short_channel_id *short_channel_id UNNEEDED)
Expand All @@ -36,6 +37,10 @@ void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED)
/* Generated stub for towire_u32 */
void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED)
{ fprintf(stderr, "towire_u32 called!\n"); abort(); }
/* Generated stub for type_to_string_ */
const char *type_to_string_(const tal_t *ctx UNNEEDED, const char *typename UNNEEDED,
union printable_types u UNNEEDED)
{ fprintf(stderr, "type_to_string_ called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

static struct privkey privkey;
Expand Down Expand Up @@ -120,7 +125,7 @@ int main(void)
setup_locale();

struct bolt11 *b11;
struct pubkey node;
struct node_id node;
struct amount_msat msatoshi;
const char *badstr;

Expand All @@ -144,7 +149,7 @@ int main(void)
* > ### Please make a donation of any amount using payment_hash 0001020304050607080900010203040506070809000102030405060708090102 to me @03e7156ae33b0a208d0744199163177e909e80176e55d97a2f221ede0f934dd9ad
* > lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kgetjypeh2ursdae8g6twvus8g6rfwvs8qun0dfjkxaq8rkx3yf5tcsyz3d73gafnh3cax9rn449d9p5uxz9ezhhypd0elx87sjle52x86fux2ypatgddc6k63n7erqz25le42c4u4ecky03ylcqca784w
*/
if (!pubkey_from_hexstr("03e7156ae33b0a208d0744199163177e909e80176e55d97a2f221ede0f934dd9ad", strlen("03e7156ae33b0a208d0744199163177e909e80176e55d97a2f221ede0f934dd9ad"), &node))
if (!node_id_from_hexstr("03e7156ae33b0a208d0744199163177e909e80176e55d97a2f221ede0f934dd9ad", strlen("03e7156ae33b0a208d0744199163177e909e80176e55d97a2f221ede0f934dd9ad"), &node))
abort();

/* BOLT #11:
Expand Down
6 changes: 6 additions & 0 deletions common/test/run-funding_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ void fromwire_bitcoin_txid(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
/* Generated stub for fromwire_bool */
bool fromwire_bool(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_bool called!\n"); abort(); }
/* Generated stub for fromwire_node_id */
void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED)
{ fprintf(stderr, "fromwire_node_id called!\n"); abort(); }
/* Generated stub for fromwire_pubkey */
void fromwire_pubkey(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct pubkey *pubkey UNNEEDED)
{ fprintf(stderr, "fromwire_pubkey called!\n"); abort(); }
Expand All @@ -46,6 +49,9 @@ void towire_bitcoin_txid(u8 **pptr UNNEEDED, const struct bitcoin_txid *txid UNN
/* Generated stub for towire_bool */
void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED)
{ fprintf(stderr, "towire_bool called!\n"); abort(); }
/* Generated stub for towire_node_id */
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "towire_node_id called!\n"); abort(); }
/* Generated stub for towire_pubkey */
void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED)
{ fprintf(stderr, "towire_pubkey called!\n"); abort(); }
Expand Down
4 changes: 2 additions & 2 deletions common/utxo.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void towire_utxo(u8 **pptr, const struct utxo *utxo)
towire_bool(pptr, is_unilateral_close);
if (is_unilateral_close) {
towire_u64(pptr, utxo->close_info->channel_id);
towire_pubkey(pptr, &utxo->close_info->peer_id);
towire_node_id(pptr, &utxo->close_info->peer_id);
towire_pubkey(pptr, &utxo->close_info->commitment_point);
}
}
Expand All @@ -39,7 +39,7 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max)
if (fromwire_bool(ptr, max)) {
utxo->close_info = tal(utxo, struct unilateral_close_info);
utxo->close_info->channel_id = fromwire_u64(ptr, max);
fromwire_pubkey(ptr, max, &utxo->close_info->peer_id);
fromwire_node_id(ptr, max, &utxo->close_info->peer_id);
fromwire_pubkey(ptr, max, &utxo->close_info->commitment_point);
} else {
utxo->close_info = NULL;
Expand Down
3 changes: 2 additions & 1 deletion common/utxo.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <common/amount.h>
#include <common/node_id.h>
#include <stdbool.h>

struct ext_key;

/* Information needed for their_unilateral/to-us outputs */
struct unilateral_close_info {
u64 channel_id;
struct pubkey peer_id;
struct node_id peer_id;
struct pubkey commitment_point;
};

Expand Down
1 change: 1 addition & 0 deletions connectd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ CONNECTD_COMMON_OBJS := \
common/key_derive.o \
common/memleak.o \
common/msg_queue.o \
common/node_id.o \
common/pseudorand.o \
common/status.o \
common/status_wire.o \
Expand Down
4 changes: 2 additions & 2 deletions connectd/connect_gossip_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Communication between gossipd and connectd.
gossip_new_peer,4000
gossip_new_peer,,id,struct pubkey
gossip_new_peer,,id,struct node_id
# Did we negotiate LOCAL_GOSSIP_QUERIES?
gossip_new_peer,,gossip_queries_feature,bool
# Did they offer LOCAL_INITIAL_ROUTING_SYNC?
Expand All @@ -14,7 +14,7 @@ gossip_new_peer_reply,,success,bool

# Connectd asks gossipd for any known addresses for that node.
gossip_get_addrs,4001
gossip_get_addrs,,id,struct pubkey
gossip_get_addrs,,id,struct node_id

gossip_get_addrs_reply,4101
gossip_get_addrs_reply,,num,u16
Expand Down
Loading

0 comments on commit a2fa699

Please sign in to comment.