Skip to content

Commit

Permalink
tx: Add chainparams to struct bitcoin_tx as context
Browse files Browse the repository at this point in the history
The way we build transactions, serialize them, and compute fees depends on the
chain we are working on, so let's add some context to the transactions.

Signed-off-by: Christian Decker <[email protected]>
  • Loading branch information
cdecker authored and rustyrussell committed Jul 31, 2019
1 parent 2537bd5 commit 9288a79
Show file tree
Hide file tree
Showing 32 changed files with 245 additions and 139 deletions.
8 changes: 6 additions & 2 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,20 @@ static void bitcoin_tx_destroy(struct bitcoin_tx *tx)
wally_tx_free(tx->wtx);
}

struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,
varint_t output_count)
struct bitcoin_tx *bitcoin_tx(const tal_t *ctx,
const struct chainparams *chainparams,
varint_t input_count, varint_t output_count)
{
struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx);

wally_tx_init_alloc(WALLY_TX_VERSION_2, 0, input_count, output_count,
&tx->wtx);
tal_add_destructor(tx, bitcoin_tx_destroy);

tx->input_amounts = tal_arrz(tx, struct amount_sat*, input_count);
tx->wtx->locktime = 0;
tx->wtx->version = 2;
tx->chainparams = chainparams;
return tx;
}

Expand All @@ -305,6 +308,7 @@ struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, const u8 **cursor,
/* We don't know the input amounts yet, so set them all to NULL */
tx->input_amounts =
tal_arrz(tx, struct amount_sat *, tx->wtx->inputs_allocation_len);
tx->chainparams = NULL;

*cursor += wsize;
*max -= wsize;
Expand Down
8 changes: 6 additions & 2 deletions bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ struct bitcoin_tx {
* unknown) */
struct amount_sat **input_amounts;
struct wally_tx *wtx;

/* Keep a reference to the ruleset we have to abide by */
const struct chainparams *chainparams;
};

struct bitcoin_tx_output {
Expand Down Expand Up @@ -52,8 +55,9 @@ size_t measure_tx_weight(const struct bitcoin_tx *tx);

/* Allocate a tx: you just need to fill in inputs and outputs (they're
* zeroed with inputs' sequence_number set to FFFFFFFF) */
struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,
varint_t output_count);
struct bitcoin_tx *bitcoin_tx(const tal_t *ctx,
const struct chainparams *chainparams,
varint_t input_count, varint_t output_count);

/* This takes a raw bitcoin tx in hex. */
struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,
Expand Down
14 changes: 7 additions & 7 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -979,10 +979,9 @@ static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx,
const u8 *msg;
secp256k1_ecdsa_signature *htlc_sigs;

txs = channel_txs(tmpctx, &htlc_map, &wscripts, peer->channel,
&peer->remote_per_commit,
commit_index,
REMOTE);
txs = channel_txs(tmpctx, peer->channel->chainparams, &htlc_map,
&wscripts, peer->channel, &peer->remote_per_commit,
commit_index, REMOTE);

msg = towire_hsm_sign_remote_commitment_tx(NULL, txs[0],
&peer->channel->funding_pubkey[REMOTE],
Expand Down Expand Up @@ -1398,9 +1397,10 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
/* SIGHASH_ALL is implied. */
commit_sig.sighash_type = SIGHASH_ALL;

txs = channel_txs(tmpctx, &htlc_map, &wscripts, peer->channel,
&peer->next_local_per_commit,
peer->next_index[LOCAL], LOCAL);
txs =
channel_txs(tmpctx, peer->channel->chainparams, &htlc_map,
&wscripts, peer->channel, &peer->next_local_per_commit,
peer->next_index[LOCAL], LOCAL);

if (!derive_simple_key(&peer->channel->basepoints[REMOTE].htlc,
&peer->next_local_per_commit, &remote_htlckey))
Expand Down
3 changes: 2 additions & 1 deletion channeld/commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
}

struct bitcoin_tx *commit_tx(const tal_t *ctx,
const struct chainparams *chainparams,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
struct amount_sat funding,
Expand Down Expand Up @@ -146,7 +147,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
#endif

/* Worst-case sizing: both to-local and to-remote outputs. */
tx = bitcoin_tx(ctx, 1, untrimmed + 2);
tx = bitcoin_tx(ctx, chainparams, 1, untrimmed + 2);

/* We keep track of which outputs have which HTLCs */
*htlcmap = tal_arr(tx, const struct htlc *, tx->wtx->outputs_allocation_len);
Expand Down
2 changes: 2 additions & 0 deletions channeld/commit_tx.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef LIGHTNING_CHANNELD_COMMIT_TX_H
#define LIGHTNING_CHANNELD_COMMIT_TX_H
#include "config.h"
#include <bitcoin/chainparams.h>
#include <bitcoin/pubkey.h>
#include <channeld/channeld_htlc.h>
#include <common/htlc.h>
Expand Down Expand Up @@ -43,6 +44,7 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
* transaction, so we carefully use the terms "self" and "other" here.
*/
struct bitcoin_tx *commit_tx(const tal_t *ctx,
const struct chainparams *chainparams,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
struct amount_sat funding,
Expand Down
32 changes: 14 additions & 18 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ static bool sum_offered_msatoshis(struct amount_msat *total,
return true;
}

static void add_htlcs(struct bitcoin_tx ***txs,
static void add_htlcs(const struct chainparams *chainparams,
struct bitcoin_tx ***txs,
const u8 ***wscripts,
const struct htlc **htlcmap,
const struct channel *channel,
Expand All @@ -210,7 +211,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,
continue;

if (htlc_owner(htlc) == side) {
tx = htlc_timeout_tx(*txs, &txid, i,
tx = htlc_timeout_tx(*txs, chainparams, &txid, i,
htlc->amount,
htlc->expiry.locktime,
channel->config[!side].to_self_delay,
Expand All @@ -222,7 +223,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,
&htlc->rhash,
&keyset->self_revocation_key);
} else {
tx = htlc_success_tx(*txs, &txid, i,
tx = htlc_success_tx(*txs, chainparams, &txid, i,
htlc->amount,
channel->config[!side].to_self_delay,
feerate_per_kw,
Expand All @@ -245,6 +246,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,

/* FIXME: We could cache these. */
struct bitcoin_tx **channel_txs(const tal_t *ctx,
const struct chainparams *chainparams,
const struct htlc ***htlcmap,
const u8 ***wscripts,
const struct channel *channel,
Expand All @@ -266,27 +268,21 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
gather_htlcs(ctx, channel, side, &committed, NULL, NULL);

txs = tal_arr(ctx, struct bitcoin_tx *, 1);
txs[0] = commit_tx(ctx, &channel->funding_txid,
channel->funding_txout,
channel->funding,
channel->funder,
channel->config[!side].to_self_delay,
&keyset,
channel->view[side].feerate_per_kw,
channel->config[side].dust_limit,
channel->view[side].owed[side],
channel->view[side].owed[!side],
committed,
htlcmap,
commitment_number ^ channel->commitment_number_obscurer,
side);
txs[0] = commit_tx(
ctx, chainparams, &channel->funding_txid, channel->funding_txout,
channel->funding, channel->funder,
channel->config[!side].to_self_delay, &keyset,
channel->view[side].feerate_per_kw,
channel->config[side].dust_limit, channel->view[side].owed[side],
channel->view[side].owed[!side], committed, htlcmap,
commitment_number ^ channel->commitment_number_obscurer, side);

*wscripts = tal_arr(ctx, const u8 *, 1);
(*wscripts)[0] = bitcoin_redeem_2of2(*wscripts,
&channel->funding_pubkey[side],
&channel->funding_pubkey[!side]);

add_htlcs(&txs, wscripts, *htlcmap, channel, &keyset, side);
add_htlcs(chainparams, &txs, wscripts, *htlcmap, channel, &keyset, side);

tal_free(committed);
return txs;
Expand Down
2 changes: 2 additions & 0 deletions channeld/full_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct channel *new_full_channel(const tal_t *ctx,
/**
* channel_txs: Get the current commitment and htlc txs for the channel.
* @ctx: tal context to allocate return value from.
* @chainparams: Parameters for the generated transactions.
* @channel: The channel to evaluate
* @htlc_map: Pointer to htlcs for each tx output (allocated off @ctx).
* @wscripts: Pointer to array of wscript for each tx returned (alloced off @ctx)
Expand All @@ -59,6 +60,7 @@ struct channel *new_full_channel(const tal_t *ctx,
* fills in @htlc_map, or NULL on key derivation failure.
*/
struct bitcoin_tx **channel_txs(const tal_t *ctx,
const struct chainparams *chainparams,
const struct htlc ***htlcmap,
const u8 ***wscripts,
const struct channel *channel,
Expand Down
34 changes: 23 additions & 11 deletions channeld/test/run-commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ static void report_htlcs(const struct bitcoin_tx *tx,
continue;

if (htlc_owner(htlc) == LOCAL) {
htlc_tx[i] = htlc_timeout_tx(htlc_tx, &txid, i,
htlc_tx[i] = htlc_timeout_tx(htlc_tx, tx->chainparams,
&txid, i,
htlc->amount,
htlc->expiry.locktime,
to_self_delay,
Expand All @@ -234,7 +235,8 @@ static void report_htlcs(const struct bitcoin_tx *tx,
&htlc->rhash,
remote_revocation_key);
} else {
htlc_tx[i] = htlc_success_tx(htlc_tx, &txid, i,
htlc_tx[i] = htlc_success_tx(htlc_tx, tx->chainparams,
&txid, i,
htlc->amount,
to_self_delay,
feerate_per_kw,
Expand Down Expand Up @@ -457,6 +459,7 @@ 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);
Expand Down Expand Up @@ -712,7 +715,8 @@ int main(void)
keyset.other_htlc_key = remote_htlckey;

print_superverbose = true;
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
tx = commit_tx(tmpctx, chainparams,
&funding_txid, funding_output_index,
funding_amount,
LOCAL, to_self_delay,
&keyset,
Expand All @@ -723,7 +727,8 @@ int main(void)
NULL, &htlc_map, commitment_number ^ cn_obscurer,
LOCAL);
print_superverbose = false;
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
tx2 = commit_tx(tmpctx, chainparams,
&funding_txid, funding_output_index,
funding_amount,
REMOTE, to_self_delay,
&keyset,
Expand Down Expand Up @@ -766,7 +771,8 @@ int main(void)
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw);

print_superverbose = true;
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
tx = commit_tx(tmpctx, chainparams,
&funding_txid, funding_output_index,
funding_amount,
LOCAL, to_self_delay,
&keyset,
Expand All @@ -777,7 +783,8 @@ int main(void)
htlcs, &htlc_map, commitment_number ^ cn_obscurer,
LOCAL);
print_superverbose = false;
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
tx2 = commit_tx(tmpctx, chainparams,
&funding_txid, funding_output_index,
funding_amount,
REMOTE, to_self_delay,
&keyset,
Expand Down Expand Up @@ -808,7 +815,8 @@ int main(void)

feerate_per_kw = increase(feerate_per_kw);
print_superverbose = false;
newtx = commit_tx(tmpctx, &funding_txid, funding_output_index,
newtx = commit_tx(tmpctx, chainparams,
&funding_txid, funding_output_index,
funding_amount,
LOCAL, to_self_delay,
&keyset,
Expand All @@ -820,7 +828,8 @@ int main(void)
commitment_number ^ cn_obscurer,
LOCAL);
/* This is what it would look like for peer generating it! */
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
tx2 = commit_tx(tmpctx, chainparams,
&funding_txid, funding_output_index,
funding_amount,
REMOTE, to_self_delay,
&keyset,
Expand Down Expand Up @@ -851,7 +860,8 @@ int main(void)
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw-1);
/* Recalc with verbosity on */
print_superverbose = true;
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
tx = commit_tx(tmpctx, chainparams,
&funding_txid, funding_output_index,
funding_amount,
LOCAL, to_self_delay,
&keyset,
Expand Down Expand Up @@ -887,7 +897,8 @@ int main(void)
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw);
/* Recalc with verbosity on */
print_superverbose = true;
newtx = commit_tx(tmpctx, &funding_txid, funding_output_index,
newtx = commit_tx(tmpctx, chainparams,
&funding_txid, funding_output_index,
funding_amount,
LOCAL, to_self_delay,
&keyset,
Expand Down Expand Up @@ -945,7 +956,8 @@ int main(void)
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw);
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
tx = commit_tx(tmpctx, chainparams,
&funding_txid, funding_output_index,
funding_amount,
LOCAL, to_self_delay,
&keyset,
Expand Down
Loading

0 comments on commit 9288a79

Please sign in to comment.