diff --git a/channeld/commit_tx.c b/channeld/commit_tx.c index c4a5d860d..27e64caca 100644 --- a/channeld/commit_tx.c +++ b/channeld/commit_tx.c @@ -66,16 +66,15 @@ static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n, const struct keyset *keyset) { 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)); - wscript = htlc_offered_wscript(tx->output, &ripemd, keyset); - tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount); - tx->output[n].script = scriptpubkey_p2wsh(tx, wscript); - SUPERVERBOSE("# HTLC %"PRIu64" offered %s wscript %s\n", - htlc->id, - type_to_string(tmpctx, struct amount_sat, - &tx->output[n].amount), + wscript = htlc_offered_wscript(tx, &ripemd, keyset); + p2wsh = scriptpubkey_p2wsh(tx, wscript); + bitcoin_tx_add_output(tx, p2wsh, &amount); + SUPERVERBOSE("# HTLC %" PRIu64 " offered %s wscript %s\n", htlc->id, + type_to_string(tmpctx, struct amount_sat, &amount), tal_hex(wscript, 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) { struct ripemd160 ripemd; - u8 *wscript; + u8 *wscript, *p2wsh; + struct amount_sat amount; ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8)); wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset); - tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount); - tx->output[n].script = scriptpubkey_p2wsh(tx->output, wscript); + p2wsh = scriptpubkey_p2wsh(tx, 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", htlc->id, type_to_string(tmpctx, struct amount_sat, - &tx->output[n].amount), + &amount), tal_hex(wscript, wscript)); tal_free(wscript); } @@ -173,11 +176,11 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, tx = bitcoin_tx(ctx, 1, untrimmed + 2); /* 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 * 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 * 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)) { u8 *wscript = to_self_wscript(tmpctx, to_self_delay,keyset); - tx->output[n].amount = amount_msat_to_sat_round_down(self_pay); - tx->output[n].script = scriptpubkey_p2wsh(tx, wscript); + u8 *p2wsh = 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; /* We don't assign cltvs[n]: if we use it, order doesn't matter. * However, valgrind will warn us something wierd is happening */ SUPERVERBOSE("# to-local amount %s wscript %s\n", - type_to_string(tmpctx, struct amount_sat, - &tx->output[n].amount), + type_to_string(tmpctx, struct amount_sat, &amount), tal_hex(tmpctx, wscript)); n++; } @@ -242,6 +246,9 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, * output](#to_remote-output). */ 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: * * #### `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 * P2WPKH to `remotepubkey`. */ - tx->output[n].amount = amount_msat_to_sat_round_down(other_pay); - tx->output[n].script = scriptpubkey_p2wpkh(tx, - &keyset->other_payment_key); + int pos = bitcoin_tx_add_output(tx, p2wpkh, &amount); + assert(pos == n); (*htlcmap)[n] = NULL; /* We don't assign cltvs[n]: if we use it, order doesn't matter. * However, valgrind will warn us something wierd is happening */ @@ -263,7 +269,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, n++; } - assert(n <= tal_count(tx->output)); + assert(n <= tx->wtx->outputs_allocation_len); tal_resize(&tx->output, 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 * `funding_created` message */ - tx->input[0].txid = *funding_txid; - tx->input[0].index = funding_txout; - /* BOLT #3: * * * `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 - = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF)); - - /* Input amount needed for signature code. */ - tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &funding); + u32 sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF)); + bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, &funding, NULL); return tx; }