Browse Source

elements: Remove global is_elements variable in favor of chainparams

No need to keep duplicate globals.
travis-debug
Christian Decker 5 years ago
committed by Rusty Russell
parent
commit
d5f0c08a88
  1. 4
      bitcoin/block.c
  2. 2
      bitcoin/signature.c
  3. 3
      bitcoin/test/run-bitcoin_block_from_hex.c
  4. 1
      bitcoin/test/run-tx-encode.c
  5. 23
      bitcoin/tx.c
  6. 2
      channeld/channeld.c
  7. 2
      channeld/test/run-commit_tx.c
  8. 2
      channeld/test/run-full_channel.c
  9. 2
      common/htlc_tx.h
  10. 2
      common/initial_commit_tx.h
  11. 2
      common/test/run-funding_tx.c
  12. 7
      common/utils.c
  13. 7
      common/utils.h
  14. 2
      lightningd/closing_control.c
  15. 1
      lightningd/options.c
  16. 12
      onchaind/onchaind.c
  17. 2
      onchaind/test/run-grind_feerate-bug.c
  18. 1
      onchaind/test/run-grind_feerate.c
  19. 1
      openingd/openingd.c
  20. 1
      plugins/libplugin.c
  21. 1
      wallet/test/run-wallet.c
  22. 4
      wallet/wallet.c
  23. 1
      wire/fromwire.c

4
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);

2
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(

3
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);

1
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;

23
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) {

2
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"

2
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);

2
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);

2
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;

2
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;

2
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:
*

7
common/utils.c

@ -1,4 +1,5 @@
#include "utils.h"
#include <bitcoin/chainparams.h>
#include <ccan/list/list.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
@ -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));

7
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);

2
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))

1
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;
}

12
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,

2
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;

1
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");

1
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. */

1
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);

1
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;

4
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;

1
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;
}

Loading…
Cancel
Save