diff --git a/gossipd/Makefile b/gossipd/Makefile index 58c7fd8a8..bac6be1cf 100644 --- a/gossipd/Makefile +++ b/gossipd/Makefile @@ -35,6 +35,7 @@ LIGHTNINGD_HEADERS_GEN += $(LIGHTNINGD_GOSSIP_HEADERS) # Common source we use. GOSSIPD_COMMON_OBJS := \ + bitcoin/chainparams.o \ common/base32.o \ common/bech32.o \ common/bech32_util.o \ diff --git a/gossipd/gossipd.c b/gossipd/gossipd.c index abda333ad..df2e41903 100644 --- a/gossipd/gossipd.c +++ b/gossipd/gossipd.c @@ -1,3 +1,4 @@ +#include #include /*~ Welcome to the gossip daemon: keeper of maps! * @@ -96,6 +97,9 @@ struct daemon { /* Routing information */ struct routing_state *rstate; + /* chainhash for checking/making gossip msgs */ + struct bitcoin_blkid chain_hash; + /* Timers: we batch gossip, and also refresh announcements */ struct timers timers; @@ -340,7 +344,7 @@ static void setup_gossip_range(struct peer *peer) * should be much smarter about requesting only what we don't already * have. */ msg = towire_gossip_timestamp_filter(peer, - &peer->daemon->rstate->chain_hash, + &peer->daemon->chain_hash, 0, UINT32_MAX); queue_peer_msg(peer, take(msg)); } @@ -527,7 +531,6 @@ static u8 *handle_channel_update_msg(struct peer *peer, const u8 *msg) * called when there's nothing more important to send. */ static const u8 *handle_query_short_channel_ids(struct peer *peer, const u8 *msg) { - struct routing_state *rstate = peer->daemon->rstate; struct bitcoin_blkid chain; u8 *encoded; struct short_channel_id *scids; @@ -538,7 +541,7 @@ static const u8 *handle_query_short_channel_ids(struct peer *peer, const u8 *msg tal_hex(tmpctx, msg)); } - if (!bitcoin_blkid_eq(&rstate->chain_hash, &chain)) { + if (!bitcoin_blkid_eq(&peer->daemon->chain_hash, &chain)) { status_trace("%s sent query_short_channel_ids chainhash %s", type_to_string(tmpctx, struct pubkey, &peer->id), type_to_string(tmpctx, struct bitcoin_blkid, &chain)); @@ -598,7 +601,7 @@ static u8 *handle_gossip_timestamp_filter(struct peer *peer, const u8 *msg) tal_hex(tmpctx, msg)); } - if (!bitcoin_blkid_eq(&peer->daemon->rstate->chain_hash, &chain_hash)) { + if (!bitcoin_blkid_eq(&peer->daemon->chain_hash, &chain_hash)) { status_trace("%s sent gossip_timestamp_filter chainhash %s", type_to_string(tmpctx, struct pubkey, &peer->id), type_to_string(tmpctx, struct bitcoin_blkid, @@ -645,7 +648,7 @@ static void reply_channel_range(struct peer *peer, * - SHOULD set `complete` to 1. */ u8 *msg = towire_reply_channel_range(NULL, - &peer->daemon->rstate->chain_hash, + &peer->daemon->chain_hash, first_blocknum, number_of_blocks, 1, encoded); @@ -739,7 +742,7 @@ static u8 *handle_query_channel_range(struct peer *peer, const u8 *msg) /* FIXME: if they ask for the wrong chain, we should not ignore it, * but give an empty response with the `complete` flag unset? */ - if (!bitcoin_blkid_eq(&peer->daemon->rstate->chain_hash, &chain_hash)) { + if (!bitcoin_blkid_eq(&peer->daemon->chain_hash, &chain_hash)) { status_trace("%s sent query_channel_range chainhash %s", type_to_string(tmpctx, struct pubkey, &peer->id), type_to_string(tmpctx, struct bitcoin_blkid, @@ -778,7 +781,7 @@ static const u8 *handle_reply_channel_range(struct peer *peer, const u8 *msg) tal_hex(tmpctx, msg)); } - if (!bitcoin_blkid_eq(&peer->daemon->rstate->chain_hash, &chain)) { + if (!bitcoin_blkid_eq(&peer->daemon->chain_hash, &chain)) { return towire_errorfmt(peer, NULL, "reply_channel_range for bad chain: %s", tal_hex(tmpctx, msg)); @@ -924,7 +927,7 @@ static u8 *handle_reply_short_channel_ids_end(struct peer *peer, const u8 *msg) tal_hex(tmpctx, msg)); } - if (!bitcoin_blkid_eq(&peer->daemon->rstate->chain_hash, &chain)) { + if (!bitcoin_blkid_eq(&peer->daemon->chain_hash, &chain)) { return towire_errorfmt(peer, NULL, "reply_short_channel_ids_end for bad chain: %s", tal_hex(tmpctx, msg)); @@ -1069,7 +1072,7 @@ static void maybe_create_next_scid_reply(struct peer *peer) */ /* FIXME: We consider ourselves to have complete knowledge. */ u8 *end = towire_reply_short_channel_ids_end(peer, - &rstate->chain_hash, + &peer->daemon->chain_hash, true); queue_peer_msg(peer, take(end)); @@ -1205,7 +1208,7 @@ static void update_local_channel(struct daemon *daemon, /* We create an update with a dummy signature, and hand to hsmd to get * it signed. */ update = towire_channel_update_option_channel_htlc_max(tmpctx, &dummy_sig, - &daemon->rstate->chain_hash, + &daemon->chain_hash, &chan->scid, timestamp, message_flags, channel_flags, @@ -1815,14 +1818,13 @@ static struct io_plan *gossip_init(struct io_conn *conn, struct daemon *daemon, const u8 *msg) { - struct bitcoin_blkid chain_hash; u32 update_channel_interval; if (!fromwire_gossipctl_init(daemon, msg, /* 60,000 ms * (unless --dev-broadcast-interval) */ &daemon->broadcast_interval_msec, - &chain_hash, + &daemon->chain_hash, &daemon->id, &daemon->globalfeatures, daemon->rgb, daemon->alias, @@ -1834,7 +1836,9 @@ static struct io_plan *gossip_init(struct io_conn *conn, } /* Prune time (usually 2 weeks) is twice update time */ - daemon->rstate = new_routing_state(daemon, &chain_hash, &daemon->id, + daemon->rstate = new_routing_state(daemon, + chainparams_by_chainhash(&daemon->chain_hash), + &daemon->id, update_channel_interval * 2); /* Load stored gossip messages */ @@ -2197,7 +2201,7 @@ static struct io_plan *query_scids_req(struct io_conn *conn, goto fail; } - msg = towire_query_short_channel_ids(NULL, &daemon->rstate->chain_hash, + msg = towire_query_short_channel_ids(NULL, &daemon->chain_hash, encoded); queue_peer_msg(peer, take(msg)); peer->num_scid_queries_outstanding++; @@ -2248,7 +2252,7 @@ static struct io_plan *send_timestamp_filter(struct io_conn *conn, goto out; } - msg = towire_gossip_timestamp_filter(NULL, &daemon->rstate->chain_hash, + msg = towire_gossip_timestamp_filter(NULL, &daemon->chain_hash, first, range); queue_peer_msg(peer, take(msg)); out: @@ -2289,7 +2293,7 @@ static struct io_plan *query_channel_range(struct io_conn *conn, status_debug("sending query_channel_range for blocks %u+%u", first_blocknum, number_of_blocks); - msg = towire_query_channel_range(NULL, &daemon->rstate->chain_hash, + msg = towire_query_channel_range(NULL, &daemon->chain_hash, first_blocknum, number_of_blocks); queue_peer_msg(peer, take(msg)); peer->range_first_blocknum = first_blocknum; diff --git a/gossipd/routing.c b/gossipd/routing.c index 101bbc9d4..8ee451ebe 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -85,14 +85,14 @@ static struct node_map *empty_node_map(const tal_t *ctx) } struct routing_state *new_routing_state(const tal_t *ctx, - const struct bitcoin_blkid *chain_hash, + const struct chainparams *chainparams, const struct pubkey *local_id, u32 prune_timeout) { struct routing_state *rstate = tal(ctx, struct routing_state); rstate->nodes = empty_node_map(rstate); rstate->broadcasts = new_broadcast_state(rstate); - rstate->chain_hash = *chain_hash; + rstate->chainparams = chainparams; rstate->local_id = *local_id; rstate->prune_timeout = prune_timeout; rstate->store = gossip_store_new(rstate, rstate, rstate->broadcasts); @@ -849,7 +849,8 @@ u8 *handle_channel_announcement(struct routing_state *rstate, * - if the specified `chain_hash` is unknown to the receiver: * - MUST ignore the message. */ - if (!bitcoin_blkid_eq(&chain_hash, &rstate->chain_hash)) { + if (!bitcoin_blkid_eq(&chain_hash, + &rstate->chainparams->genesis_blockhash)) { status_trace( "Received channel_announcement %s for unknown chain %s", type_to_string(pending, struct short_channel_id, @@ -1167,7 +1168,8 @@ u8 *handle_channel_update(struct routing_state *rstate, const u8 *update TAKES, * active on the specified chain): * - MUST ignore the channel update. */ - if (!bitcoin_blkid_eq(&chain_hash, &rstate->chain_hash)) { + if (!bitcoin_blkid_eq(&chain_hash, + &rstate->chainparams->genesis_blockhash)) { status_trace("Received channel_update for unknown chain %s", type_to_string(tmpctx, struct bitcoin_blkid, &chain_hash)); diff --git a/gossipd/routing.h b/gossipd/routing.h index cd44591b1..82220ae29 100644 --- a/gossipd/routing.h +++ b/gossipd/routing.h @@ -172,6 +172,9 @@ static inline int half_chan_to(const struct node *n, const struct chan *chan) } struct routing_state { + /* Which chain we're on */ + const struct chainparams *chainparams; + /* All known nodes. */ struct node_map *nodes; @@ -184,8 +187,6 @@ struct routing_state { struct broadcast_state *broadcasts; - struct bitcoin_blkid chain_hash; - /* Our own ID so we can identify local channels */ struct pubkey local_id; @@ -218,7 +219,7 @@ struct route_hop { }; struct routing_state *new_routing_state(const tal_t *ctx, - const struct bitcoin_blkid *chain_hash, + const struct chainparams *chainparams, const struct pubkey *local_id, u32 prune_timeout); diff --git a/gossipd/test/run-bench-find_route.c b/gossipd/test/run-bench-find_route.c index eb2ea5f6f..37872f385 100644 --- a/gossipd/test/run-bench-find_route.c +++ b/gossipd/test/run-bench-find_route.c @@ -215,7 +215,6 @@ int main(int argc, char *argv[]) { setup_locale(); - static const struct bitcoin_blkid zerohash; struct routing_state *rstate; size_t num_nodes = 100, num_runs = 1; struct timemono start, end; @@ -229,7 +228,7 @@ int main(int argc, char *argv[]) | SECP256K1_CONTEXT_SIGN); setup_tmpctx(); - rstate = new_routing_state(tmpctx, &zerohash, &me, 0); + rstate = new_routing_state(tmpctx, NULL, &me, 0); opt_register_noarg("--perfme", opt_set_bool, &perfme, "Run perfme-start and perfme-stop around benchmark"); diff --git a/gossipd/test/run-find_route-specific.c b/gossipd/test/run-find_route-specific.c index 10b68e1f0..27b365e7c 100644 --- a/gossipd/test/run-find_route-specific.c +++ b/gossipd/test/run-find_route-specific.c @@ -149,7 +149,6 @@ int main(void) { setup_locale(); - static const struct bitcoin_blkid zerohash; struct half_chan *nc; struct routing_state *rstate; struct pubkey a, b, c, d; @@ -174,7 +173,7 @@ int main(void) strlen("02cca6c5c966fcf61d121e3a70e03a1cd9eeeea024b26ea666ce974d43b242e636"), &d); - rstate = new_routing_state(tmpctx, &zerohash, &a, 0); + rstate = new_routing_state(tmpctx, NULL, &a, 0); /* [{'active': True, 'short_id': '6990:2:1/1', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 1, 'destination': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'source': '02ea622d5c8d6143f15ed3ce1d501dd0d3d09d3b1c83a44d0034949f8a9ab60f06', 'last_update': 1504064344}, */ diff --git a/gossipd/test/run-find_route.c b/gossipd/test/run-find_route.c index 1c178ee82..aa0c1ac8a 100644 --- a/gossipd/test/run-find_route.c +++ b/gossipd/test/run-find_route.c @@ -189,7 +189,6 @@ int main(void) { setup_locale(); - static const struct bitcoin_blkid zerohash; struct routing_state *rstate; struct pubkey a, b, c, d; struct privkey tmp; @@ -203,7 +202,7 @@ int main(void) memset(&tmp, 'a', sizeof(tmp)); pubkey_from_privkey(&tmp, &a); - rstate = new_routing_state(tmpctx, &zerohash, &a, 0); + rstate = new_routing_state(tmpctx, NULL, &a, 0); new_node(rstate, &a);