From ef7a63d8f87224be137a24672a1c10e3dd3e42b2 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 25 Sep 2019 22:38:45 +0200 Subject: [PATCH] elements: Move from a global is_elements to a global chainparams We now have a pointer to chainparams, that fails valgrind if we do anything chain-specific before setting it. Suggested-by: Rusty Russell <@rustyrussell> --- bitcoin/tx.c | 2 +- channeld/channel_wire.csv | 2 +- channeld/channeld.c | 6 ++++-- closingd/closing_wire.csv | 3 +-- closingd/closingd.c | 6 +----- common/utils.c | 2 ++ common/utils.h | 1 + gossipd/gossip_wire.csv | 2 +- gossipd/gossipd.c | 3 ++- hsmd/hsm_wire.csv | 2 +- hsmd/hsmd.c | 7 ++----- lightningd/channel_control.c | 2 +- lightningd/closing_control.c | 2 +- lightningd/gossip_control.c | 3 ++- lightningd/hsm_control.c | 4 +--- lightningd/onchain_control.c | 3 +-- lightningd/opening_control.c | 2 +- lightningd/options.c | 5 ++++- onchaind/onchain_wire.csv | 3 +-- onchaind/onchaind.c | 6 ++---- onchaind/test/run-grind_feerate-bug.c | 2 +- onchaind/test/run-grind_feerate.c | 2 +- openingd/opening_wire.csv | 2 +- openingd/openingd.c | 5 ++--- tools/generate-wire.py | 14 +++++++++++++- wire/fromwire.c | 11 +++++++++++ wire/towire.c | 5 +++++ wire/wire.h | 5 +++++ 28 files changed, 70 insertions(+), 42 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index bdb5fec3b..96b02b417 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -387,7 +387,7 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, /* If we are constructing an elements transaction we need to * explicitly add the fee as an extra output. So allocate one more * than the outputs we need internally. */ - if (is_elements) + if (chainparams->is_elements) output_count += 1; wally_tx_init_alloc(WALLY_TX_VERSION_2, 0, input_count, output_count, diff --git a/channeld/channel_wire.csv b/channeld/channel_wire.csv index ba1f94937..0a56047b9 100644 --- a/channeld/channel_wire.csv +++ b/channeld/channel_wire.csv @@ -5,7 +5,7 @@ # Begin! (passes gossipd-client fd) msgtype,channel_init,1000 -msgdata,channel_init,chain_hash,bitcoin_blkid, +msgdata,channel_init,chainparams,chainparams, msgdata,channel_init,funding_txid,bitcoin_txid, msgdata,channel_init,funding_txout,u16, msgdata,channel_init,funding_satoshi,amount_sat, diff --git a/channeld/channeld.c b/channeld/channeld.c index 83bab15bf..4442934c6 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -2944,7 +2944,7 @@ static void init_channel(struct peer *peer) msg = wire_sync_read(tmpctx, MASTER_FD); if (!fromwire_channel_init(peer, msg, - &peer->chain_hash, + &chainparams, &funding_txid, &funding_txout, &funding, &minimum_depth, @@ -3002,7 +3002,9 @@ static void init_channel(struct peer *peer) /* stdin == requests, 3 == peer, 4 = gossip, 5 = gossip_store, 6 = HSM */ per_peer_state_set_fds(peer->pps, 3, 4, 5); - is_elements = chainparams_by_chainhash(&peer->chain_hash)->is_elements; + is_elements = chainparams->is_elements; + peer->chain_hash = chainparams->genesis_blockhash; + status_debug("init %s: remote_per_commit = %s, old_remote_per_commit = %s" " next_idx_local = %"PRIu64 " next_idx_remote = %"PRIu64 diff --git a/closingd/closing_wire.csv b/closingd/closing_wire.csv index 97d31a761..47e03203d 100644 --- a/closingd/closing_wire.csv +++ b/closingd/closing_wire.csv @@ -3,9 +3,8 @@ #include # Begin! (passes peer fd, gossipd-client fd) msgtype,closing_init,2001 -msgdata,closing_init,chain_hash,chain_hash, +msgdata,closing_init,chainparams,chainparams, msgdata,closing_init,pps,per_peer_state, -msgdata,closing_init,chain_hash,bitcoin_blkid, msgdata,closing_init,funding_txid,bitcoin_txid, msgdata,closing_init,funding_txout,u16, msgdata,closing_init,funding_satoshi,amount_sat, diff --git a/closingd/closingd.c b/closingd/closingd.c index d6f35cd76..0360ee774 100644 --- a/closingd/closingd.c +++ b/closingd/closingd.c @@ -576,8 +576,6 @@ int main(int argc, char *argv[]) enum side whose_turn; u8 *channel_reestablish; struct secret last_remote_per_commit_secret; - struct bitcoin_blkid chain_hash; - const struct chainparams *chainparams; subdaemon_setup(argc, argv); @@ -585,7 +583,7 @@ int main(int argc, char *argv[]) msg = wire_sync_read(tmpctx, REQ_FD); if (!fromwire_closing_init(ctx, msg, - &chain_hash, + &chainparams, &pps, &funding_txid, &funding_txout, &funding, @@ -611,8 +609,6 @@ int main(int argc, char *argv[]) /* stdin == requests, 3 == peer, 4 = gossip, 5 = gossip_store, 6 = hsmd */ per_peer_state_set_fds(pps, 3, 4, 5); - chainparams = chainparams_by_chainhash(&chain_hash); - is_elements = chainparams->is_elements; status_debug("out = %s/%s", type_to_string(tmpctx, struct amount_sat, &out[LOCAL]), diff --git a/common/utils.c b/common/utils.c index 7e40820f1..9139acb00 100644 --- a/common/utils.c +++ b/common/utils.c @@ -8,6 +8,8 @@ secp256k1_context *secp256k1_ctx; const tal_t *tmpctx; bool is_elements = false; +const struct chainparams *chainparams; + char *tal_hexstr(const tal_t *ctx, const void *data, size_t len) { char *str = tal_arr(ctx, char, hex_str_size(len)); diff --git a/common/utils.h b/common/utils.h index c6038f3e4..76cef0d27 100644 --- a/common/utils.h +++ b/common/utils.h @@ -13,6 +13,7 @@ extern secp256k1_context *secp256k1_ctx; /* FIXME: Instead of using this as a global, we might want to pass it as * context whenever we need it. The global var is just lazy... */ extern bool is_elements; +extern const struct chainparams *chainparams; /* Allocate and fill in a hex-encoded string of this data. */ char *tal_hexstr(const tal_t *ctx, const void *data, size_t len); diff --git a/gossipd/gossip_wire.csv b/gossipd/gossip_wire.csv index 191279c5d..a1674d434 100644 --- a/gossipd/gossip_wire.csv +++ b/gossipd/gossip_wire.csv @@ -4,7 +4,7 @@ # Initialize the gossip daemon. msgtype,gossipctl_init,3000 -msgdata,gossipctl_init,chain_hash,bitcoin_blkid, +msgdata,gossipctl_init,chainparams,chainparams, msgdata,gossipctl_init,id,node_id, msgdata,gossipctl_init,gflen,u16, msgdata,gossipctl_init,globalfeatures,u8,gflen diff --git a/gossipd/gossipd.c b/gossipd/gossipd.c index e34a9d19e..9e52641d0 100644 --- a/gossipd/gossipd.c +++ b/gossipd/gossipd.c @@ -930,7 +930,7 @@ static struct io_plan *gossip_init(struct io_conn *conn, bool dev_fast_gossip, dev_fast_gossip_prune; if (!fromwire_gossipctl_init(daemon, msg, - &daemon->chain_hash, + &chainparams, &daemon->id, &daemon->globalfeatures, daemon->rgb, daemon->alias, @@ -941,6 +941,7 @@ static struct io_plan *gossip_init(struct io_conn *conn, master_badmsg(WIRE_GOSSIPCTL_INIT, msg); } + daemon->chain_hash = chainparams->genesis_blockhash; daemon->rstate = new_routing_state(daemon, chainparams_by_chainhash(&daemon->chain_hash), &daemon->id, diff --git a/hsmd/hsm_wire.csv b/hsmd/hsm_wire.csv index bdb542216..d5de505fb 100644 --- a/hsmd/hsm_wire.csv +++ b/hsmd/hsm_wire.csv @@ -9,7 +9,7 @@ msgdata,hsmstatus_client_bad_request,msg,u8,len # Start the HSM. msgtype,hsm_init,11 msgdata,hsm_init,bip32_key_version,bip32_key_version, -msgdata,hsm_init,chain_hash,bitcoin_blkid, +msgdata,hsm_init,chainparams,chainparams, msgdata,hsm_init,dev_force_privkey,?privkey, msgdata,hsm_init,dev_force_bip32_seed,?secret, msgdata,hsm_init,dev_force_channel_secrets,?secrets, diff --git a/hsmd/hsmd.c b/hsmd/hsmd.c index c90f2788c..f5d3bca23 100644 --- a/hsmd/hsmd.c +++ b/hsmd/hsmd.c @@ -580,7 +580,6 @@ static struct io_plan *init_hsm(struct io_conn *conn, struct secret *seed; struct secrets *secrets; struct sha256 *shaseed; - struct bitcoin_blkid chain_hash; /* This must be lightningd. */ assert(is_lightningd(c)); @@ -589,7 +588,7 @@ static struct io_plan *init_hsm(struct io_conn *conn, * definitions in hsm_client_wire.csv. The format of those files is * an extension of the simple comma-separated format output by the * BOLT tools/extract-formats.py tool. */ - if (!fromwire_hsm_init(NULL, msg_in, &bip32_key_version, &chain_hash, + if (!fromwire_hsm_init(NULL, msg_in, &bip32_key_version, &chainparams, &privkey, &seed, &secrets, &shaseed)) return bad_req(conn, c, msg_in); @@ -602,9 +601,7 @@ static struct io_plan *init_hsm(struct io_conn *conn, /* Once we have read the init message we know which params the master * will use */ - c->chainparams = chainparams_by_chainhash(&chain_hash); - is_elements = c->chainparams->is_elements; - + c->chainparams = chainparams; maybe_create_new_hsm(); load_hsm(); diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 3f8df8060..f3f092e57 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -421,7 +421,7 @@ void peer_start_channeld(struct channel *channel, } initmsg = towire_channel_init(tmpctx, - &get_chainparams(ld)->genesis_blockhash, + chainparams, &channel->funding_txid, channel->funding_outnum, channel->funding, diff --git a/lightningd/closing_control.c b/lightningd/closing_control.c index 8cd537190..2c3873ff7 100644 --- a/lightningd/closing_control.c +++ b/lightningd/closing_control.c @@ -273,7 +273,7 @@ void peer_start_closingd(struct channel *channel, return; } initmsg = towire_closing_init(tmpctx, - &get_chainparams(ld)->genesis_blockhash, + chainparams, pps, &channel->funding_txid, channel->funding_outnum, diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index ea7913956..d20c96445 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -208,7 +208,8 @@ void gossip_init(struct lightningd *ld, int connectd_fd) msg = towire_gossipctl_init( tmpctx, - &get_chainparams(ld)->genesis_blockhash, &ld->id, + chainparams, + &ld->id, get_offered_globalfeatures(tmpctx), ld->rgb, ld->alias, diff --git a/lightningd/hsm_control.c b/lightningd/hsm_control.c index e64d6bcba..07a5acc20 100644 --- a/lightningd/hsm_control.c +++ b/lightningd/hsm_control.c @@ -80,8 +80,6 @@ void hsm_init(struct lightningd *ld) { u8 *msg; int fds[2]; - struct bitcoin_blkid chainhash; - chainhash = get_chainparams(ld)->genesis_blockhash; /* We actually send requests synchronously: only status is async. */ if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0) @@ -97,7 +95,7 @@ void hsm_init(struct lightningd *ld) ld->hsm_fd = fds[0]; if (!wire_sync_write(ld->hsm_fd, towire_hsm_init(tmpctx, &ld->topology->bitcoind->chainparams->bip32_key_version, - &chainhash, + chainparams, IFDEV(ld->dev_force_privkey, NULL), IFDEV(ld->dev_force_bip32_seed, NULL), IFDEV(ld->dev_force_channel_secrets, NULL), diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index 57f238f5f..e81dd8c7a 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -513,8 +513,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel, msg = towire_onchain_init(channel, &channel->their_shachain.chain, - chainparams->is_elements, - &chainparams->genesis_blockhash, + chainparams, channel->funding, &channel->channel_info.old_remote_per_commit, &channel->channel_info.remote_per_commit, diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index a70c791bb..d76ca1d73 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -937,7 +937,7 @@ void peer_start_openingd(struct peer *peer, uc->minimum_depth = peer->ld->config.anchor_confirms; msg = towire_opening_init(NULL, - &get_chainparams(peer->ld)->genesis_blockhash, + chainparams, &uc->our_config, max_to_self_delay, min_effective_htlc_capacity, diff --git a/lightningd/options.c b/lightningd/options.c index 699009ad7..0472d9aba 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -241,7 +241,10 @@ static char *opt_set_network(const char *arg, struct lightningd *ld) { assert(arg != NULL); - ld->topology->bitcoind->chainparams = chainparams_for_network(arg); + /* Set the global chainparams instance */ + chainparams = chainparams_for_network(arg); + + ld->topology->bitcoind->chainparams = chainparams; if (!ld->topology->bitcoind->chainparams) return tal_fmt(NULL, "Unknown network name '%s'", arg); is_elements = ld->topology->bitcoind->chainparams->is_elements; diff --git a/onchaind/onchain_wire.csv b/onchaind/onchain_wire.csv index 2af5799c9..56bafa8bf 100644 --- a/onchaind/onchain_wire.csv +++ b/onchaind/onchain_wire.csv @@ -7,8 +7,7 @@ msgtype,onchain_init,5001 msgdata,onchain_init,shachain,shachain, # This needs to be set explicitly since the same message also contains a # transaction that we need to parse correctly. -msgdata,onchain_init,is_elements,bool, -msgdata,onchain_init,chain_hash,bitcoin_blkid, +msgdata,onchain_init,chainparams,chainparams, msgdata,onchain_init,funding_amount_satoshi,amount_sat, # Remote per commit point for committed tx. msgdata,onchain_init,old_remote_per_commitment_point,pubkey, diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index 6acbbd5bf..fc38fe135 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -2628,7 +2628,6 @@ int main(int argc, char *argv[]) bool *tell_if_missing, *tell_immediately; u32 tx_blockheight; struct pubkey *possible_remote_per_commitment_point; - struct bitcoin_blkid chain_hash; subdaemon_setup(argc, argv); @@ -2639,8 +2638,7 @@ int main(int argc, char *argv[]) msg = wire_sync_read(tmpctx, REQ_FD); if (!fromwire_onchain_init(tmpctx, msg, &shachain, - &is_elements, - &chain_hash, + &chainparams, &funding, &old_remote_per_commit_point, &remote_per_commit_point, @@ -2667,7 +2665,7 @@ int main(int argc, char *argv[]) master_badmsg(WIRE_ONCHAIN_INIT, msg); } - tx->chainparams = chainparams_by_chainhash(&chain_hash); + tx->chainparams = chainparams; status_debug("feerate_per_kw = %u", feerate_per_kw); bitcoin_txid(tx, &txid); diff --git a/onchaind/test/run-grind_feerate-bug.c b/onchaind/test/run-grind_feerate-bug.c index b36b0e02b..0f27d6d25 100644 --- a/onchaind/test/run-grind_feerate-bug.c +++ b/onchaind/test/run-grind_feerate-bug.c @@ -43,7 +43,7 @@ bool fromwire_onchain_dev_memleak(const void *p UNNEEDED) bool fromwire_onchain_htlc(const void *p UNNEEDED, struct htlc_stub *htlc UNNEEDED, bool *tell_if_missing UNNEEDED, bool *tell_immediately UNNEEDED) { fprintf(stderr, "fromwire_onchain_htlc called!\n"); abort(); } /* Generated stub for fromwire_onchain_init */ -bool fromwire_onchain_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, bool *is_elements UNNEEDED, struct bitcoin_blkid *chain_hash UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *feerate_per_kw UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *funder UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct bitcoin_tx **tx UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, secp256k1_ecdsa_signature **htlc_signature UNNEEDED, u64 *num_htlcs UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, bool *option_static_remotekey UNNEEDED) +bool fromwire_onchain_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, const struct chainparams **chainparams UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *feerate_per_kw UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *funder UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct bitcoin_tx **tx UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, secp256k1_ecdsa_signature **htlc_signature UNNEEDED, u64 *num_htlcs UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, bool *option_static_remotekey UNNEEDED) { fprintf(stderr, "fromwire_onchain_init called!\n"); abort(); } /* Generated stub for fromwire_onchain_known_preimage */ bool fromwire_onchain_known_preimage(const void *p UNNEEDED, struct preimage *preimage UNNEEDED) diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index f59d54c48..2b08e1e2a 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -47,7 +47,7 @@ bool fromwire_onchain_dev_memleak(const void *p UNNEEDED) bool fromwire_onchain_htlc(const void *p UNNEEDED, struct htlc_stub *htlc UNNEEDED, bool *tell_if_missing UNNEEDED, bool *tell_immediately UNNEEDED) { fprintf(stderr, "fromwire_onchain_htlc called!\n"); abort(); } /* Generated stub for fromwire_onchain_init */ -bool fromwire_onchain_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, bool *is_elements UNNEEDED, struct bitcoin_blkid *chain_hash UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *feerate_per_kw UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *funder UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct bitcoin_tx **tx UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, secp256k1_ecdsa_signature **htlc_signature UNNEEDED, u64 *num_htlcs UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, bool *option_static_remotekey UNNEEDED) +bool fromwire_onchain_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, const struct chainparams **chainparams UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *feerate_per_kw UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *funder UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct bitcoin_tx **tx UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, secp256k1_ecdsa_signature **htlc_signature UNNEEDED, u64 *num_htlcs UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, bool *option_static_remotekey UNNEEDED) { fprintf(stderr, "fromwire_onchain_init called!\n"); abort(); } /* Generated stub for fromwire_onchain_known_preimage */ bool fromwire_onchain_known_preimage(const void *p UNNEEDED, struct preimage *preimage UNNEEDED) diff --git a/openingd/opening_wire.csv b/openingd/opening_wire.csv index f369a77da..f3f969b3a 100644 --- a/openingd/opening_wire.csv +++ b/openingd/opening_wire.csv @@ -5,7 +5,7 @@ msgtype,opening_init,6000 # Which network are we configured for? -msgdata,opening_init,chain_hash,bitcoin_blkid, +msgdata,opening_init,chainparams,chainparams, # Base configuration we'll offer (channel reserve will vary with amount) msgdata,opening_init,our_config,channel_config, # Minimum/maximum configuration values we'll accept diff --git a/openingd/openingd.c b/openingd/openingd.c index e94d5a5c7..adc427dd1 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -1410,7 +1410,6 @@ int main(int argc, char *argv[]) u8 *msg, *inner; struct pollfd pollfd[3]; struct state *state = tal(NULL, struct state); - struct bitcoin_blkid chain_hash; struct secret *none; subdaemon_setup(argc, argv); @@ -1422,7 +1421,7 @@ int main(int argc, char *argv[]) /*~ The very first thing we read from lightningd is our init msg */ msg = wire_sync_read(tmpctx, REQ_FD); if (!fromwire_opening_init(state, msg, - &chain_hash, + &chainparams, &state->localconf, &state->max_to_self_delay, &state->min_effective_htlc_capacity, @@ -1451,7 +1450,7 @@ int main(int argc, char *argv[]) /*~ Even though I only care about bitcoin, there's still testnet and * regtest modes, so we have a general "parameters for this chain" * function. */ - state->chainparams = chainparams_by_chainhash(&chain_hash); + state->chainparams = chainparams; is_elements = state->chainparams->is_elements; /*~ Initially we're not associated with a channel, but diff --git a/tools/generate-wire.py b/tools/generate-wire.py index 1a3488770..01c4a9574 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -115,9 +115,12 @@ class Field(object): return ', const {} *{}'.format(type_name, self.name) def arg_desc_from(self): + type_name = self.type_obj.type_name() + if self.type_obj.is_const_ptr_ptr_type(): + return ', const {} **{}'.format(type_name, self.name) + if self.len_field_of: return '' - type_name = self.type_obj.type_name() if self.is_array(): return ', {} {}[{}]'.format(type_name, self.name, self.count) ptrs = '*' @@ -244,6 +247,12 @@ class Type(FieldSet): # FIXME: omits 'pad' } + # Types that are const pointer-to-pointers, such as chainparams, i.e., + # they set a reference to some const entry. + const_ptr_ptr_types = [ + 'chainparams' + ] + @staticmethod def true_type(type_name, field_name=None, outer_name=None): """ Returns 'true' type of a given type and a flag if @@ -308,6 +317,9 @@ class Type(FieldSet): def is_subtype(self): return bool(self.fields) + def is_const_ptr_ptr_type(self): + return self.name in self.const_ptr_ptr_types + def is_truncated(self): return self.name in self.truncated_typedefs diff --git a/wire/fromwire.c b/wire/fromwire.c index 964351f50..64efec662 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -19,6 +19,8 @@ #define SUPERVERBOSE(...) #endif +extern const struct chainparams *chainparams; + /* Sets *cursor to NULL and returns NULL when extraction fails. */ const void *fromwire_fail(const u8 **cursor, size_t *max) { @@ -389,3 +391,12 @@ struct bitcoin_tx_output *fromwire_bitcoin_tx_output(const tal_t *ctx, fromwire_u8_array(cursor, max, output->script, script_len); return output; } + +void fromwire_chainparams(const u8 **cursor, size_t *max, + const struct chainparams **chainparams) +{ + struct bitcoin_blkid genesis; + fromwire_bitcoin_blkid(cursor, max, &genesis); + *chainparams = chainparams_by_chainhash(&genesis); + is_elements = (*chainparams)->is_elements; +} diff --git a/wire/towire.c b/wire/towire.c index 40ca26cfe..09f689d61 100644 --- a/wire/towire.c +++ b/wire/towire.c @@ -256,3 +256,8 @@ void towire_bitcoin_tx_output(u8 **pptr, const struct bitcoin_tx_output *output) towire_u16(pptr, tal_count(output->script)); towire_u8_array(pptr, output->script, tal_count(output->script)); } + +void towire_chainparams(u8 **cursor, const struct chainparams *chainparams) +{ + towire_bitcoin_blkid(cursor, &chainparams->genesis_blockhash); +} diff --git a/wire/wire.h b/wire/wire.h index ee8051fd0..f9781ab34 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -89,6 +89,7 @@ void towire_siphash_seed(u8 **cursor, const struct siphash_seed *seed); void towire_bip32_key_version(u8 **cursor, const struct bip32_key_version *version); void towire_bitcoin_tx_output(u8 **pptr, const struct bitcoin_tx_output *output); +void towire_chainparams(u8 **cursor, const struct chainparams *chainparams); const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n); u8 fromwire_u8(const u8 **cursor, size_t *max); @@ -141,4 +142,8 @@ void fromwire_bip32_key_version(const u8 **cursor, size_t *max, struct bip32_key_version *version); struct bitcoin_tx_output *fromwire_bitcoin_tx_output(const tal_t *ctx, const u8 **cursor, size_t *max); + +void fromwire_chainparams(const u8 **cursor, size_t *max, + const struct chainparams **chainparams); + #endif /* LIGHTNING_WIRE_WIRE_H */