wally: Migrate commit_tx

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker
2019-03-15 21:50:08 +01:00
committed by Rusty Russell
parent 41eea5f40c
commit 0c962fdc29

View File

@@ -66,16 +66,15 @@ static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
const struct keyset *keyset) const struct keyset *keyset)
{ {
struct ripemd160 ripemd; struct ripemd160 ripemd;
u8 *wscript; u8 *wscript, *p2wsh;
struct amount_sat amount = amount_msat_to_sat_round_down(htlc->amount);
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8)); ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
wscript = htlc_offered_wscript(tx->output, &ripemd, keyset); wscript = htlc_offered_wscript(tx, &ripemd, keyset);
tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount); p2wsh = scriptpubkey_p2wsh(tx, wscript);
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript); bitcoin_tx_add_output(tx, p2wsh, &amount);
SUPERVERBOSE("# HTLC %"PRIu64" offered %s wscript %s\n", SUPERVERBOSE("# HTLC %" PRIu64 " offered %s wscript %s\n", htlc->id,
htlc->id, type_to_string(tmpctx, struct amount_sat, &amount),
type_to_string(tmpctx, struct amount_sat,
&tx->output[n].amount),
tal_hex(wscript, wscript)); tal_hex(wscript, wscript));
tal_free(wscript); tal_free(wscript);
} }
@@ -85,16 +84,20 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
const struct keyset *keyset) const struct keyset *keyset)
{ {
struct ripemd160 ripemd; struct ripemd160 ripemd;
u8 *wscript; u8 *wscript, *p2wsh;
struct amount_sat amount;
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8)); ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset); wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset);
tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount); p2wsh = scriptpubkey_p2wsh(tx, wscript);
tx->output[n].script = scriptpubkey_p2wsh(tx->output, wscript); amount = amount_msat_to_sat_round_down(htlc->amount);
bitcoin_tx_add_output(tx, p2wsh, &amount);
SUPERVERBOSE("# HTLC %"PRIu64" received %s wscript %s\n", SUPERVERBOSE("# HTLC %"PRIu64" received %s wscript %s\n",
htlc->id, htlc->id,
type_to_string(tmpctx, struct amount_sat, type_to_string(tmpctx, struct amount_sat,
&tx->output[n].amount), &amount),
tal_hex(wscript, wscript)); tal_hex(wscript, wscript));
tal_free(wscript); tal_free(wscript);
} }
@@ -173,11 +176,11 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
tx = bitcoin_tx(ctx, 1, untrimmed + 2); tx = bitcoin_tx(ctx, 1, untrimmed + 2);
/* We keep track of which outputs have which HTLCs */ /* We keep track of which outputs have which HTLCs */
*htlcmap = tal_arr(tx, const struct htlc *, tal_count(tx->output)); *htlcmap = tal_arr(tx, const struct htlc *, tx->wtx->outputs_allocation_len);
/* We keep cltvs for tie-breaking HTLC outputs; we use the same order /* We keep cltvs for tie-breaking HTLC outputs; we use the same order
* for sending the htlc txs, so it may matter. */ * for sending the htlc txs, so it may matter. */
cltvs = tal_arr(tmpctx, u32, tal_count(tx->output)); cltvs = tal_arr(tmpctx, u32, tx->wtx->outputs_allocation_len);
/* This could be done in a single loop, but we follow the BOLT /* This could be done in a single loop, but we follow the BOLT
* literally to make comments in test vectors clearer. */ * literally to make comments in test vectors clearer. */
@@ -223,14 +226,15 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
*/ */
if (amount_msat_greater_eq_sat(self_pay, dust_limit)) { if (amount_msat_greater_eq_sat(self_pay, dust_limit)) {
u8 *wscript = to_self_wscript(tmpctx, to_self_delay,keyset); u8 *wscript = to_self_wscript(tmpctx, to_self_delay,keyset);
tx->output[n].amount = amount_msat_to_sat_round_down(self_pay); u8 *p2wsh = scriptpubkey_p2wsh(tx, wscript);
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript); struct amount_sat amount = amount_msat_to_sat_round_down(self_pay);
bitcoin_tx_add_output(tx, p2wsh, &amount);
(*htlcmap)[n] = NULL; (*htlcmap)[n] = NULL;
/* We don't assign cltvs[n]: if we use it, order doesn't matter. /* We don't assign cltvs[n]: if we use it, order doesn't matter.
* However, valgrind will warn us something wierd is happening */ * However, valgrind will warn us something wierd is happening */
SUPERVERBOSE("# to-local amount %s wscript %s\n", SUPERVERBOSE("# to-local amount %s wscript %s\n",
type_to_string(tmpctx, struct amount_sat, type_to_string(tmpctx, struct amount_sat, &amount),
&tx->output[n].amount),
tal_hex(tmpctx, wscript)); tal_hex(tmpctx, wscript));
n++; n++;
} }
@@ -242,6 +246,9 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* output](#to_remote-output). * output](#to_remote-output).
*/ */
if (amount_msat_greater_eq_sat(other_pay, dust_limit)) { if (amount_msat_greater_eq_sat(other_pay, dust_limit)) {
struct amount_sat amount = amount_msat_to_sat_round_down(other_pay);
u8 *p2wpkh =
scriptpubkey_p2wpkh(tx, &keyset->other_payment_key);
/* BOLT #3: /* BOLT #3:
* *
* #### `to_remote` Output * #### `to_remote` Output
@@ -249,9 +256,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* This output sends funds to the other peer and thus is a simple * This output sends funds to the other peer and thus is a simple
* P2WPKH to `remotepubkey`. * P2WPKH to `remotepubkey`.
*/ */
tx->output[n].amount = amount_msat_to_sat_round_down(other_pay); int pos = bitcoin_tx_add_output(tx, p2wpkh, &amount);
tx->output[n].script = scriptpubkey_p2wpkh(tx, assert(pos == n);
&keyset->other_payment_key);
(*htlcmap)[n] = NULL; (*htlcmap)[n] = NULL;
/* We don't assign cltvs[n]: if we use it, order doesn't matter. /* We don't assign cltvs[n]: if we use it, order doesn't matter.
* However, valgrind will warn us something wierd is happening */ * However, valgrind will warn us something wierd is happening */
@@ -263,7 +269,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
n++; n++;
} }
assert(n <= tal_count(tx->output)); assert(n <= tx->wtx->outputs_allocation_len);
tal_resize(&tx->output, n); tal_resize(&tx->output, n);
tal_resize(htlcmap, n); tal_resize(htlcmap, n);
@@ -295,18 +301,12 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* * `txin[0]` outpoint: `txid` and `output_index` from * * `txin[0]` outpoint: `txid` and `output_index` from
* `funding_created` message * `funding_created` message
*/ */
tx->input[0].txid = *funding_txid;
tx->input[0].index = funding_txout;
/* BOLT #3: /* BOLT #3:
* *
* * `txin[0]` sequence: upper 8 bits are 0x80, lower 24 bits are upper 24 bits of the obscured commitment number * * `txin[0]` sequence: upper 8 bits are 0x80, lower 24 bits are upper 24 bits of the obscured commitment number
*/ */
tx->input[0].sequence_number u32 sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
= (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF)); bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, &funding, NULL);
/* Input amount needed for signature code. */
tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &funding);
return tx; return tx;
} }