From 167681c7090ba45d9864f078c5e5d1bb99ac1081 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 22 May 2021 14:30:22 +0930 Subject: [PATCH] gossmap: fix gossmap_chan_get_announce() and gossmap_node_get_announce(). These were never used, nor tested. Signed-off-by: Rusty Russell --- common/gossmap.c | 18 +++- common/test/Makefile | 4 + common/test/run-gossmap_local.c | 154 +++++++++++++++++++------------- 3 files changed, 109 insertions(+), 67 deletions(-) diff --git a/common/gossmap.c b/common/gossmap.c index 7b3ac6416..4d6502448 100644 --- a/common/gossmap.c +++ b/common/gossmap.c @@ -1087,9 +1087,18 @@ u8 *gossmap_chan_get_announce(const tal_t *ctx, const struct gossmap *map, const struct gossmap_chan *c) { - u16 len = map_be16(map, c->cann_off); - u8 *msg = tal_arr(ctx, u8, len); + u32 len; + u8 *msg; + u32 pre_off; + /* We need to go back to struct gossip_hdr to get len */ + if (c->private) + pre_off = 2 + 8 + 2 + sizeof(struct gossip_hdr); + else + pre_off = sizeof(struct gossip_hdr); + len = (map_be32(map, c->cann_off - pre_off) & GOSSIP_STORE_LEN_MASK); + + msg = tal_arr(ctx, u8, len); map_copy(map, c->cann_off, msg, len); return msg; } @@ -1099,13 +1108,14 @@ u8 *gossmap_node_get_announce(const tal_t *ctx, const struct gossmap *map, const struct gossmap_node *n) { - u16 len; + u32 len; u8 *msg; if (n->nann_off == 0) return NULL; - len = map_be16(map, n->nann_off); + len = (map_be32(map, n->nann_off - sizeof(struct gossip_hdr)) + & GOSSIP_STORE_LEN_MASK); msg = tal_arr(ctx, u8, len); map_copy(map, n->nann_off, msg, len); diff --git a/common/test/Makefile b/common/test/Makefile index 3065d9f55..596801d82 100644 --- a/common/test/Makefile +++ b/common/test/Makefile @@ -45,5 +45,9 @@ common/test/run-route common/test/run-route-specific: \ wire/peer$(EXP)_wiregen.o \ wire/towire.o +common/test/run-gossmap_local: \ + wire/fromwire.o \ + wire/peer$(EXP)_wiregen.o \ + wire/towire.o check-units: $(COMMON_TEST_PROGRAMS:%=unittest/%) diff --git a/common/test/run-gossmap_local.c b/common/test/run-gossmap_local.c index 11499bbc7..2e23651df 100644 --- a/common/test/run-gossmap_local.c +++ b/common/test/run-gossmap_local.c @@ -11,69 +11,32 @@ #include /* AUTOGENERATED MOCKS START */ -/* Generated stub for fromwire */ -const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED) -{ fprintf(stderr, "fromwire called!\n"); abort(); } -/* 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_fail */ -void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) -{ fprintf(stderr, "fromwire_fail called!\n"); abort(); } -/* Generated stub for fromwire_secp256k1_ecdsa_signature */ -void fromwire_secp256k1_ecdsa_signature(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, - secp256k1_ecdsa_signature *signature UNNEEDED) -{ fprintf(stderr, "fromwire_secp256k1_ecdsa_signature called!\n"); abort(); } -/* Generated stub for fromwire_sha256 */ -void fromwire_sha256(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct sha256 *sha256 UNNEEDED) -{ fprintf(stderr, "fromwire_sha256 called!\n"); abort(); } -/* Generated stub for fromwire_tal_arrn */ -u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED, - const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED) -{ fprintf(stderr, "fromwire_tal_arrn called!\n"); abort(); } -/* Generated stub for fromwire_u16 */ -u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) -{ fprintf(stderr, "fromwire_u16 called!\n"); abort(); } -/* 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 fromwire_u64 */ -u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) -{ fprintf(stderr, "fromwire_u64 called!\n"); abort(); } -/* Generated stub for fromwire_u8 */ -u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) -{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); } -/* Generated stub for fromwire_u8_array */ -void fromwire_u8_array(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, u8 *arr UNNEEDED, size_t num UNNEEDED) -{ fprintf(stderr, "fromwire_u8_array called!\n"); abort(); } -/* Generated stub for towire */ -void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED) -{ fprintf(stderr, "towire called!\n"); abort(); } -/* 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_secp256k1_ecdsa_signature */ -void towire_secp256k1_ecdsa_signature(u8 **pptr UNNEEDED, - const secp256k1_ecdsa_signature *signature UNNEEDED) -{ fprintf(stderr, "towire_secp256k1_ecdsa_signature called!\n"); abort(); } -/* Generated stub for towire_sha256 */ -void towire_sha256(u8 **pptr UNNEEDED, const struct sha256 *sha256 UNNEEDED) -{ fprintf(stderr, "towire_sha256 called!\n"); abort(); } -/* Generated stub for towire_u16 */ -void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED) -{ fprintf(stderr, "towire_u16 called!\n"); abort(); } -/* Generated stub for towire_u32 */ -void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED) -{ fprintf(stderr, "towire_u32 called!\n"); abort(); } -/* Generated stub for towire_u64 */ -void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED) -{ fprintf(stderr, "towire_u64 called!\n"); abort(); } -/* Generated stub for towire_u8 */ -void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED) -{ fprintf(stderr, "towire_u8 called!\n"); abort(); } -/* Generated stub for towire_u8_array */ -void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED) -{ fprintf(stderr, "towire_u8_array called!\n"); abort(); } +/* Generated stub for fromwire_bigsize */ +bigsize_t fromwire_bigsize(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_bigsize called!\n"); abort(); } +/* Generated stub for fromwire_channel_id */ +void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, + struct channel_id *channel_id UNNEEDED) +{ fprintf(stderr, "fromwire_channel_id called!\n"); abort(); } +/* Generated stub for fromwire_tlv */ +bool fromwire_tlv(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, + const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED, + void *record UNNEEDED, struct tlv_field **fields UNNEEDED) +{ fprintf(stderr, "fromwire_tlv called!\n"); abort(); } +/* Generated stub for tlv_fields_valid */ +bool tlv_fields_valid(const struct tlv_field *fields UNNEEDED, size_t *err_index UNNEEDED) +{ fprintf(stderr, "tlv_fields_valid called!\n"); abort(); } +/* Generated stub for towire_bigsize */ +void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED) +{ fprintf(stderr, "towire_bigsize called!\n"); abort(); } +/* Generated stub for towire_channel_id */ +void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id UNNEEDED) +{ fprintf(stderr, "towire_channel_id called!\n"); abort(); } +/* Generated stub for towire_tlv */ +void towire_tlv(u8 **pptr UNNEEDED, + const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED, + const void *record UNNEEDED) +{ fprintf(stderr, "towire_tlv 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) @@ -290,6 +253,53 @@ static u8 canned_map[] = { , 0x00, 0x03, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x02, 0x33, 0x80 }; +static void check_cannounce(const u8 *cannounce, + const struct short_channel_id *scid, + const struct node_id *n1, + const struct node_id *n2) +{ + secp256k1_ecdsa_signature sig; + u8 *features; + struct bitcoin_blkid chain_hash; + struct short_channel_id actual_scid; + struct node_id actual_n1, actual_n2; + struct pubkey k; + + assert(fromwire_channel_announcement(cannounce, cannounce, + &sig, &sig, &sig, &sig, + &features, &chain_hash, + &actual_scid, + &actual_n1, + &actual_n2, + &k, &k)); + assert(short_channel_id_eq(&actual_scid, scid)); + if (node_id_cmp(n1, n2) < 0) { + assert(node_id_eq(&actual_n1, n1)); + assert(node_id_eq(&actual_n2, n2)); + } else { + assert(node_id_eq(&actual_n1, n2)); + assert(node_id_eq(&actual_n2, n1)); + } +} + +static void check_nannounce(const u8 *nannounce, + const struct node_id *n) +{ + secp256k1_ecdsa_signature sig; + u8 *features, *addresses; + u32 timestamp; + u8 rgb_color[3], alias[32]; + struct node_id node_id; + assert(fromwire_node_announcement(nannounce, nannounce, + &sig, &features, + ×tamp, + &node_id, + rgb_color, + alias, + &addresses)); + assert(node_id_eq(&node_id, n)); +} + int main(int argc, char *argv[]) { int fd; @@ -303,6 +313,7 @@ int main(int argc, char *argv[]) u32 timestamp, fee_base_msat, fee_proportional_millionths; u8 message_flags, channel_flags; struct amount_msat htlc_minimum_msat, htlc_maximum_msat; + u8 *cann, *nann; common_setup(argv[0]); @@ -407,6 +418,23 @@ int main(int argc, char *argv[]) assert(tal_bytelen(gossmap_chan_get_features(tmpctx, map, gossmap_find_chan(map, &scid23))) == 0); + cann = gossmap_chan_get_announce(tmpctx, map, + gossmap_find_chan(map, &scid12)); + check_cannounce(cann, &scid12, &l1, &l2); + cann = gossmap_chan_get_announce(tmpctx, map, + gossmap_find_chan(map, &scid23)); + check_cannounce(cann, &scid23, &l2, &l3); + + nann = gossmap_node_get_announce(tmpctx, map, + gossmap_find_node(map, &l1)); + assert(!nann); + nann = gossmap_node_get_announce(tmpctx, map, + gossmap_find_node(map, &l2)); + check_nannounce(nann, &l2); + nann = gossmap_node_get_announce(tmpctx, map, + gossmap_find_node(map, &l3)); + check_nannounce(nann, &l3); + /* Now, let's add a new channel l1 -> l4. */ mods = gossmap_localmods_new(tmpctx); assert(node_id_from_hexstr("0382ce59ebf18be7d84677c2e35f23294b9992ceca95491fcf8a56c6cb2d9de199", 66, &l4));