txs: Move commit tx generation out of the signature computation

We need the txs around, so don't throw them away after generating them.
This commit is contained in:
Christian Decker
2020-05-07 10:13:34 +09:30
committed by Rusty Russell
parent 667a763659
commit eb8eabcc3c
13 changed files with 108 additions and 45 deletions

View File

@@ -94,6 +94,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
struct amount_msat other_pay,
const struct htlc **htlcs,
const struct htlc ***htlcmap,
struct wally_tx_output *direct_outputs[NUM_SIDES],
u64 obscured_commitment_number,
enum side side)
{
@@ -102,7 +103,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
struct bitcoin_tx *tx;
size_t i, n, untrimmed;
u32 *cltvs;
struct htlc *dummy_to_local = (struct htlc *)0x01,
*dummy_to_remote = (struct htlc *)0x02;
if (!amount_msat_add(&total_pay, self_pay, other_pay))
abort();
assert(!amount_msat_greater_sat(total_pay, funding));
@@ -215,7 +217,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
struct amount_sat amount = amount_msat_to_sat_round_down(self_pay);
bitcoin_tx_add_output(tx, p2wsh, amount);
(*htlcmap)[n] = NULL;
/* Add a dummy entry to the htlcmap so we can recognize it later */
(*htlcmap)[n] = direct_outputs ? dummy_to_local : 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",
@@ -248,7 +251,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
*/
int pos = bitcoin_tx_add_output(tx, p2wpkh, amount);
assert(pos == n);
(*htlcmap)[n] = NULL;
(*htlcmap)[n] = direct_outputs ? dummy_to_remote : 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-remote amount %s P2WPKH(%s)\n",
@@ -305,6 +308,20 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
u32 sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, funding, NULL);
/* Identify the direct outputs (to_us, to_them). */
if (direct_outputs != NULL) {
direct_outputs[LOCAL] = direct_outputs[REMOTE] = NULL;
for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
if ((*htlcmap)[i] == dummy_to_local) {
(*htlcmap)[i] = NULL;
direct_outputs[LOCAL] = tx->wtx->outputs + i;
} else if ((*htlcmap)[i] == dummy_to_remote) {
(*htlcmap)[i] = NULL;
direct_outputs[REMOTE] = tx->wtx->outputs + i;
}
}
}
bitcoin_tx_finalize(tx);
assert(bitcoin_tx_check(tx));