Skip to content

Commit

Permalink
elements: Remove global is_elements variable in favor of chainparams
Browse files Browse the repository at this point in the history
No need to keep duplicate globals.
  • Loading branch information
cdecker authored and rustyrussell committed Oct 3, 2019
1 parent ef7a63d commit d5f0c08
Show file tree
Hide file tree
Showing 23 changed files with 48 additions and 37 deletions.
4 changes: 2 additions & 2 deletions bitcoin/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion bitcoin/signature.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion bitcoin/test/run-bitcoin_block_from_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions bitcoin/test/run-tx-encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
23 changes: 12 additions & 11 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion channeld/test/run-commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion common/htlc_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion common/initial_commit_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion common/test/run-funding_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand Down
7 changes: 6 additions & 1 deletion common/utils.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#include "utils.h"
#include <bitcoin/chainparams.h>
#include <ccan/list/list.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <locale.h>

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));
Expand Down
7 changes: 4 additions & 3 deletions common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion lightningd/closing_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 0 additions & 1 deletion lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
12 changes: 8 additions & 4 deletions onchaind/onchaind.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions onchaind/test/run-grind_feerate-bug.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions onchaind/test/run-grind_feerate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 0 additions & 1 deletion openingd/openingd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
1 change: 0 additions & 1 deletion plugins/libplugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion wire/fromwire.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit d5f0c08

Please sign in to comment.