diff --git a/bitcoin/block.c b/bitcoin/block.c index b7abf0b55..2d48fd56b 100644 --- a/bitcoin/block.c +++ b/bitcoin/block.c @@ -31,7 +31,7 @@ bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams, pull(&p, &len, &b->hdr.merkle_hash, sizeof(b->hdr.merkle_hash)); b->hdr.timestamp = pull_le32(&p, &len); - if (is_elements) { + if (is_elements(chainparams)) { b->elements_hdr = tal(b, struct elements_block_hdr); b->elements_hdr->block_height = pull_le32(&p, &len); @@ -76,7 +76,7 @@ void bitcoin_block_blkid(const struct bitcoin_block *b, sha256_update(&shactx, &b->hdr.merkle_hash, sizeof(b->hdr.merkle_hash)); sha256_le32(&shactx, b->hdr.timestamp); - if (is_elements) { + if (is_elements(chainparams)) { size_t clen = tal_bytelen(b->elements_hdr->proof.challenge); sha256_le32(&shactx, b->elements_hdr->block_height); diff --git a/bitcoin/signature.c b/bitcoin/signature.c index 6bd7bcd90..30a50dad1 100644 --- a/bitcoin/signature.c +++ b/bitcoin/signature.c @@ -98,7 +98,7 @@ static void bitcoin_tx_hash_for_sig(const struct bitcoin_tx *tx, unsigned int in u64 satoshis = tx->input_amounts[in]->satoshis /* Raw: sig-helper */; int flags = WALLY_TX_FLAG_USE_WITNESS; - if (is_elements) { + if (is_elements(chainparams)) { ret = wally_tx_confidential_value_from_satoshi(satoshis, value, sizeof(value)); assert(ret == WALLY_OK); ret = wally_tx_get_elements_signature_hash( diff --git a/bitcoin/test/run-bitcoin_block_from_hex.c b/bitcoin/test/run-bitcoin_block_from_hex.c index 5246024a0..892183028 100644 --- a/bitcoin/test/run-bitcoin_block_from_hex.c +++ b/bitcoin/test/run-bitcoin_block_from_hex.c @@ -69,7 +69,8 @@ int main(void) struct bitcoin_block *b; setup_locale(); - b = bitcoin_block_from_hex(NULL, chainparams_for_network("bitcoin"), + chainparams = chainparams_for_network("bitcoin"); + b = bitcoin_block_from_hex(NULL, chainparams, block, strlen(block)); assert(b); diff --git a/bitcoin/test/run-tx-encode.c b/bitcoin/test/run-tx-encode.c index 3aba907f1..0b8ef963d 100644 --- a/bitcoin/test/run-tx-encode.c +++ b/bitcoin/test/run-tx-encode.c @@ -34,6 +34,7 @@ static void hexeq(const void *p, size_t len, const char *hex) int main(void) { setup_locale(); + chainparams = chainparams_for_network("bitcoin"); struct bitcoin_tx *tx; diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 96b02b417..dd8df7c24 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -27,7 +27,7 @@ int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script, assert(tx->wtx != NULL); assert(chainparams); - if (is_elements) { + if (chainparams->is_elements) { u8 value[9]; ret = wally_tx_confidential_value_from_satoshi(satoshis, value, sizeof(value)); @@ -65,7 +65,8 @@ int bitcoin_tx_add_multi_outputs(struct bitcoin_tx *tx, static bool elements_tx_output_is_fee(const struct bitcoin_tx *tx, int outnum) { assert(outnum < tx->wtx->num_outputs); - return is_elements && tx->wtx->outputs[outnum].script_len == 0; + return chainparams->is_elements && + tx->wtx->outputs[outnum].script_len == 0; } /** @@ -82,7 +83,7 @@ static u64 bitcoin_tx_compute_fee(const struct bitcoin_tx *tx) if (elements_tx_output_is_fee(tx, i)) continue; - if (!is_elements) { + if (!chainparams->is_elements) { fee -= tx->wtx->outputs[i].satoshi; /* Raw: low-level helper */ } else { beint64_t tmp; @@ -103,7 +104,7 @@ int elements_tx_add_fee_output(struct bitcoin_tx *tx) fee.satoshis = rawsats; /* Raw: need amounts later */ /* If we aren't using elements, we don't add explicit fee outputs */ - if (!is_elements || rawsats == 0) + if (!chainparams->is_elements || rawsats == 0) return -1; /* Try to find any existing fee output */ @@ -135,7 +136,7 @@ int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid, sizeof(struct bitcoin_txid), outnum, sequence, script, tal_bytelen(script), NULL /* Empty witness stack */, &input); - input->features = is_elements ? WALLY_TX_IS_ELEMENTS : 0; + input->features = chainparams->is_elements ? WALLY_TX_IS_ELEMENTS : 0; wally_tx_add_input(tx->wtx, input); wally_tx_input_free(input); @@ -155,7 +156,7 @@ bool bitcoin_tx_check(const struct bitcoin_tx *tx) if (wally_tx_get_length(tx->wtx, flags, &written) != WALLY_OK) return false; - if (is_elements) { + if (chainparams->is_elements) { flags |= WALLY_TX_FLAG_USE_ELEMENTS; } @@ -176,9 +177,9 @@ void bitcoin_tx_output_set_amount(struct bitcoin_tx *tx, int outnum, u64 satoshis = amount.satoshis; /* Raw: low-level helper */ struct wally_tx_output *output = &tx->wtx->outputs[outnum]; assert(outnum < tx->wtx->num_outputs); - if (is_elements) { - int ret = wally_tx_confidential_value_from_satoshi(satoshis, output->value, - output->value_len); + if (chainparams->is_elements) { + int ret = wally_tx_confidential_value_from_satoshi( + satoshis, output->value, output->value_len); assert(ret == WALLY_OK); } else { output->satoshi = satoshis; @@ -318,7 +319,7 @@ static void push_tx(const struct bitcoin_tx *tx, if (bip144 && uses_witness(tx)) flag |= WALLY_TX_FLAG_USE_WITNESS; - if (is_elements) + if (chainparams->is_elements) flag |= WALLY_TX_FLAG_USE_ELEMENTS; res = wally_tx_get_length(tx->wtx, flag & WALLY_TX_FLAG_USE_WITNESS, &len); @@ -408,7 +409,7 @@ struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, const u8 **cursor, int flags = WALLY_TX_FLAG_USE_WITNESS; struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx); - if (is_elements) + if (chainparams->is_elements) flags |= WALLY_TX_FLAG_USE_ELEMENTS; if (wally_tx_from_bytes(*cursor, *max, flags, &tx->wtx) != WALLY_OK) { diff --git a/channeld/channeld.c b/channeld/channeld.c index 4442934c6..c0504a0bf 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -3001,8 +3001,6 @@ 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->is_elements; peer->chain_hash = chainparams->genesis_blockhash; status_debug("init %s: remote_per_commit = %s, old_remote_per_commit = %s" diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index f220d8d34..94169ee58 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -465,11 +465,11 @@ int main(void) u64 commitment_number, cn_obscurer; struct amount_msat to_local, to_remote; const struct htlc **htlcs, **htlc_map, **htlc_map2, **inv_htlcs; - const struct chainparams *chainparams = chainparams_for_network("bitcoin"); secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); setup_tmpctx(); + chainparams = chainparams_for_network("bitcoin"); htlcs = setup_htlcs(tmpctx); inv_htlcs = invert_htlcs(htlcs); diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 2fdd75ed0..e07536634 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -354,11 +354,11 @@ int main(void) const struct htlc **htlc_map, **htlcs; const u8 *funding_wscript, **wscripts; size_t i; - const struct chainparams *chainparams = chainparams_for_network("bitcoin"); wally_init(0); secp256k1_ctx = wally_get_secp_context(); setup_tmpctx(); + chainparams = chainparams_for_network("bitcoin"); feerate_per_kw = tal_arr(tmpctx, u32, NUM_SIDES); unknown = tal(tmpctx, struct pubkey); diff --git a/common/htlc_tx.h b/common/htlc_tx.h index 4664b5044..9c643d080 100644 --- a/common/htlc_tx.h +++ b/common/htlc_tx.h @@ -22,7 +22,7 @@ struct ripemd160; static inline size_t elements_add_overhead(size_t weight, size_t incount, size_t outcount) { - if (is_elements) { + if (chainparams->is_elements) { /* Each transaction has surjection and rangeproof (both empty * for us as long as we use unblinded L-BTC transactions). */ weight += 2 * 4; diff --git a/common/initial_commit_tx.h b/common/initial_commit_tx.h index 2b75e4d2a..81f05c803 100644 --- a/common/initial_commit_tx.h +++ b/common/initial_commit_tx.h @@ -43,7 +43,7 @@ static inline struct amount_sat commit_tx_base_fee(u32 feerate_per_kw, */ weight += 172 * num_untrimmed_htlcs; - if (is_elements) { + if (chainparams->is_elements) { /* Each transaction has surjection and rangeproof (both empty * for us as long as we use unblinded L-BTC transactions). */ weight += 2 * 4; diff --git a/common/test/run-funding_tx.c b/common/test/run-funding_tx.c index af91a2922..510eefbfd 100644 --- a/common/test/run-funding_tx.c +++ b/common/test/run-funding_tx.c @@ -110,11 +110,11 @@ int main(void) struct bitcoin_signature sig; struct bitcoin_address addr; struct amount_sat tmpamt; - const struct chainparams *chainparams = chainparams_for_network("bitcoin"); secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); setup_tmpctx(); + chainparams = chainparams_for_network("bitcoin"); /* BOLT #3: * diff --git a/common/utils.c b/common/utils.c index 9139acb00..959f535c6 100644 --- a/common/utils.c +++ b/common/utils.c @@ -1,4 +1,5 @@ #include "utils.h" +#include #include #include #include @@ -6,10 +7,14 @@ secp256k1_context *secp256k1_ctx; const tal_t *tmpctx; -bool is_elements = false; const struct chainparams *chainparams; +bool is_elements(const struct chainparams *chainparams) +{ + return chainparams->is_elements; +} + 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 76cef0d27..85689e8a2 100644 --- a/common/utils.h +++ b/common/utils.h @@ -10,11 +10,12 @@ 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; +/* Simple accessor function for our own dependencies to use, in order to avoid + * circular dependencies (should only be used in `bitcoin/y`). */ +bool is_elements(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/lightningd/closing_control.c b/lightningd/closing_control.c index 2c3873ff7..94f576b09 100644 --- a/lightningd/closing_control.c +++ b/lightningd/closing_control.c @@ -31,7 +31,7 @@ static struct amount_sat calc_tx_fee(struct amount_sat sat_in, scriptlen = tal_bytelen(oscript); tal_free(oscript); - if (is_elements && scriptlen == 0) + if (chainparams->is_elements && scriptlen == 0) continue; if (!amount_sat_sub(&fee, fee, amt)) diff --git a/lightningd/options.c b/lightningd/options.c index 0472d9aba..9ce520527 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -247,7 +247,6 @@ static char *opt_set_network(const char *arg, struct lightningd *ld) 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; return NULL; } diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index fc38fe135..9ff92e594 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -727,7 +727,8 @@ static bool is_mutual_close(const struct bitcoin_tx *tx, for (i = 0; i < tx->wtx->num_outputs; i++) { const u8 *script = bitcoin_tx_output_get_script(tmpctx, tx, i); /* To be paranoid, we only let each one match once. */ - if (is_elements && (script == NULL || tal_bytelen(script) == 0)) { + if (chainparams->is_elements && + (script == NULL || tal_bytelen(script) == 0)) { /* This is a fee output, ignore please */ continue; } else if (scripteq(script, local_scriptpubkey) @@ -1797,7 +1798,8 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, const u8 *oscript = bitcoin_tx_output_get_script(tmpctx, tx, i); struct amount_sat amt = bitcoin_tx_output_get_amount(tx, i); - if (is_elements && (oscript == NULL || tal_bytelen(oscript) == 0)) { + if (chainparams->is_elements && + (oscript == NULL || tal_bytelen(oscript) == 0)) { status_debug("OUTPUT %zu is a fee output", i); /* An empty script simply means that that this is a * fee output. */ @@ -2157,7 +2159,8 @@ static void handle_their_cheat(const struct bitcoin_tx *tx, const u8 *oscript = bitcoin_tx_output_get_script(tmpctx, tx, i); struct amount_sat amt = bitcoin_tx_output_get_amount(tx, i); - if (is_elements && (oscript == NULL || tal_bytelen(oscript) == 0)) { + if (chainparams->is_elements && + (oscript == NULL || tal_bytelen(oscript) == 0)) { /* An empty script simply means that that this is a * fee output. */ out = new_tracked_output(tx->chainparams, @@ -2390,7 +2393,8 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, const u8 *oscript = bitcoin_tx_output_get_script(tmpctx, tx, i); struct amount_sat amt = bitcoin_tx_output_get_amount(tx, i); - if (is_elements && (oscript == NULL || tal_bytelen(oscript) == 0)) { + if (chainparams->is_elements && + (oscript == NULL || tal_bytelen(oscript) == 0)) { /* An empty script simply means that that this is a * fee output. */ out = new_tracked_output(tx->chainparams, diff --git a/onchaind/test/run-grind_feerate-bug.c b/onchaind/test/run-grind_feerate-bug.c index 0f27d6d25..aeb0e95cd 100644 --- a/onchaind/test/run-grind_feerate-bug.c +++ b/onchaind/test/run-grind_feerate-bug.c @@ -244,6 +244,8 @@ int main(void) secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); setup_tmpctx(); + chainparams = chainparams_for_network("bitcoin"); + htlcs[0].cltv_expiry = 585998; htlcs[1].cltv_expiry = 585998; htlcs[2].cltv_expiry = 586034; diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index 2b08e1e2a..486199594 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -201,6 +201,7 @@ int main(int argc, char *argv[]) secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); setup_tmpctx(); + chainparams = chainparams_for_network("bitcoin"); tx = bitcoin_tx_from_hex(tmpctx, "0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000", strlen("0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000")); tx->chainparams = chainparams_for_network("regtest"); diff --git a/openingd/openingd.c b/openingd/openingd.c index adc427dd1..40b6386a1 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -1451,7 +1451,6 @@ int main(int argc, char *argv[]) * regtest modes, so we have a general "parameters for this chain" * function. */ state->chainparams = chainparams; - is_elements = state->chainparams->is_elements; /*~ Initially we're not associated with a channel, but * handle_peer_gossip_or_error compares this. */ diff --git a/plugins/libplugin.c b/plugins/libplugin.c index c66e8be89..7461f2a7d 100644 --- a/plugins/libplugin.c +++ b/plugins/libplugin.c @@ -543,7 +543,6 @@ static struct command_result *handle_init(struct command *init_cmd, nettok = json_delve(buf, configtok, ".network"); network = json_strdup(tmpctx, buf, nettok); chainparams = chainparams_for_network(network); - is_elements = chainparams->is_elements; rpctok = json_delve(buf, configtok, ".rpc-file"); rpc_conn.fd = socket(AF_UNIX, SOCK_STREAM, 0); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 05b3c8b9d..6400f37a6 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -1298,6 +1298,7 @@ static bool test_wallet_payment_status_enum(void) int main(void) { setup_locale(); + chainparams = chainparams_for_network("bitcoin"); bool ok = true; struct lightningd *ld; diff --git a/wallet/wallet.c b/wallet/wallet.c index fb4321349..ea4fcb467 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -372,7 +372,7 @@ static const struct utxo **wallet_select(const tal_t *ctx, struct wallet *w, weight += (8 + 1 + BITCOIN_SCRIPTPUBKEY_P2WPKH_LEN) * 4; /* A couple of things need to change for elements: */ - if (is_elements) { + if (chainparams->is_elements) { /* Each transaction has surjection and rangeproof (both empty * for us as long as we use unblinded L-BTC transactions). */ weight += 2 * 4; @@ -425,7 +425,7 @@ static const struct utxo **wallet_select(const tal_t *ctx, struct wallet *w, input_weight += 1 + (1 + 73 + 1 + 33); /* Elements inputs have 6 bytes of blank proofs attached. */ - if (is_elements) + if (chainparams->is_elements) input_weight += 6; weight += input_weight; diff --git a/wire/fromwire.c b/wire/fromwire.c index 64efec662..9fadeeb6b 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -398,5 +398,4 @@ void fromwire_chainparams(const u8 **cursor, size_t *max, struct bitcoin_blkid genesis; fromwire_bitcoin_blkid(cursor, max, &genesis); *chainparams = chainparams_by_chainhash(&genesis); - is_elements = (*chainparams)->is_elements; }