From 01d31e7dde67485f74a0afbe2e4e033343f2a69d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 26 Oct 2023 13:03:28 +1030 Subject: [PATCH] channeld: have commit_tx tell is what output was the other's anchor. We're going to want this, soon. Signed-off-by: Rusty Russell --- channeld/commit_tx.c | 34 ++++++++++++++++++++------------ channeld/commit_tx.h | 6 ++++-- channeld/full_channel.c | 3 ++- channeld/test/run-commit_tx.c | 23 ++++++++++----------- channeld/test/run-full_channel.c | 7 +++++-- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/channeld/commit_tx.c b/channeld/commit_tx.c index 48ce7427e..443841537 100644 --- a/channeld/commit_tx.c +++ b/channeld/commit_tx.c @@ -128,7 +128,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, u64 obscured_commitment_number, bool option_anchor_outputs, bool option_anchors_zero_fee_htlc_tx, - enum side side) + enum side side, + int *anchor_outnum) { struct amount_sat base_fee; struct amount_msat total_pay; @@ -139,7 +140,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, u32 *cltvs; bool to_local, to_remote; struct htlc *dummy_to_local = (struct htlc *)0x01, - *dummy_to_remote = (struct htlc *)0x02; + *dummy_to_remote = (struct htlc *)0x02, + *dummy_other_anchor = (struct htlc *)0x03; const u8 *funding_wscript = bitcoin_redeem_2of2(tmpctx, local_funding_key, remote_funding_key); @@ -379,9 +381,11 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, n++; } + /* With anchors, the caller really wants to know what + * is the LOCAL anchor for the REMOTE side. */ if (to_remote || untrimmed != 0) { tx_add_anchor_output(tx, remote_funding_key); - (*htlcmap)[n] = NULL; + (*htlcmap)[n] = dummy_other_anchor; n++; } } @@ -433,17 +437,21 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, bitcoin_tx_add_input(tx, funding, sequence, NULL, funding_sats, NULL, funding_wscript); - /* Identify the direct outputs (to_us, to_them). */ - if (direct_outputs != NULL) { + /* Identify the direct outputs (to_us, to_them), and the local anchor */ + 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; - } + + *anchor_outnum = -1; + 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; + } else if ((*htlcmap)[i] == dummy_other_anchor) { + (*htlcmap)[i] = NULL; + *anchor_outnum = i; } } diff --git a/channeld/commit_tx.h b/channeld/commit_tx.h index 23eacdf54..a2f6d51de 100644 --- a/channeld/commit_tx.h +++ b/channeld/commit_tx.h @@ -63,8 +63,9 @@ bool commit_tx_amount_trimmed(const struct htlc **htlcs, * @obscured_commitment_number: number to encode in commitment transaction * @direct_outputs: If non-NULL, fill with pointers to the direct (non-HTLC) outputs (or NULL if none). * @option_anchor_outputs: does option_anchor_outputs apply to this channel? + * @option_anchors_zero_fee_htlc_tx: does option_anchors_zero_fee_htlc_tx apply to this channel? * @side: side to generate commitment transaction for. - * @option_anchor_outputs: does option_anchor_outputs apply to this channel? + * @anchor_outnum: set to index of local anchor, or -1 if none. * * We need to be able to generate the remote side's tx to create signatures, * but the BOLT is expressed in terms of generating our local commitment @@ -90,6 +91,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, u64 obscured_commitment_number, bool option_anchor_outputs, bool option_anchors_zero_fee_htlc_tx, - enum side side); + enum side side, + int *anchor_outnum); #endif /* LIGHTNING_CHANNELD_COMMIT_TX_H */ diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 197a4639c..e0aaeb39b 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -315,6 +315,7 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx, const struct htlc **committed; struct keyset keyset; struct amount_msat side_pay, other_side_pay; + int local_anchor; if (!derive_keyset(per_commitment_point, &channel->basepoints[side], @@ -363,7 +364,7 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx, commitment_number ^ channel->commitment_number_obscurer, channel_has(channel, OPT_ANCHOR_OUTPUTS), channel_has(channel, OPT_ANCHORS_ZERO_FEE_HTLC_TX), - side); + side, &local_anchor); /* Set the remote/local pubkeys on the commitment tx psbt */ psbt_input_add_pubkey(txs[0]->psbt, 0, diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index 5b6040cb1..236540c15 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -542,6 +542,7 @@ int main(int argc, const char *argv[]) bool option_anchor_outputs = false; bool option_anchors_zero_fee_htlc_tx = false; bool option_static_remotekey = false; + int local_anchor; /* Allow us to check static-remotekey BOLT 3 vectors, too */ if (argv[1] && streq(argv[1], "--static-remotekey")) @@ -822,7 +823,7 @@ int main(int argc, const char *argv[]) NULL, &htlc_map, NULL, commitment_number ^ cn_obscurer, option_anchor_outputs, option_anchors_zero_fee_htlc_tx, - LOCAL); + LOCAL, &local_anchor); print_superverbose = false; tx2 = commit_tx(tmpctx, &funding, @@ -839,7 +840,7 @@ int main(int argc, const char *argv[]) NULL, &htlc_map2, NULL, commitment_number ^ cn_obscurer, option_anchor_outputs, option_anchors_zero_fee_htlc_tx, - REMOTE); + REMOTE, &local_anchor); tx_must_be_eq(tx, tx2); report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey, &local_funding_privkey, &local_funding_pubkey, @@ -891,7 +892,7 @@ int main(int argc, const char *argv[]) htlcs, &htlc_map, NULL, commitment_number ^ cn_obscurer, option_anchor_outputs, option_anchors_zero_fee_htlc_tx, - LOCAL); + LOCAL, &local_anchor); print_superverbose = false; tx2 = commit_tx(tmpctx, &funding, @@ -909,7 +910,7 @@ int main(int argc, const char *argv[]) commitment_number ^ cn_obscurer, option_anchor_outputs, option_anchors_zero_fee_htlc_tx, - REMOTE); + REMOTE, &local_anchor); tx_must_be_eq(tx, tx2); report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey, &local_funding_privkey, &local_funding_pubkey, @@ -949,7 +950,7 @@ int main(int argc, const char *argv[]) commitment_number ^ cn_obscurer, option_anchor_outputs, option_anchors_zero_fee_htlc_tx, - LOCAL); + LOCAL, &local_anchor); /* This is what it would look like for peer generating it! */ tx2 = commit_tx(tmpctx, &funding, @@ -967,7 +968,7 @@ int main(int argc, const char *argv[]) commitment_number ^ cn_obscurer, option_anchor_outputs, option_anchors_zero_fee_htlc_tx, - REMOTE); + REMOTE, &local_anchor); tx_must_be_eq(newtx, tx2); #ifdef DEBUG if (feerate_per_kw % 100000 == 0) @@ -1011,7 +1012,7 @@ int main(int argc, const char *argv[]) commitment_number ^ cn_obscurer, option_anchor_outputs, option_anchors_zero_fee_htlc_tx, - LOCAL); + LOCAL, &local_anchor); report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey, &local_funding_privkey, &local_funding_pubkey, @@ -1063,7 +1064,7 @@ int main(int argc, const char *argv[]) commitment_number ^ cn_obscurer, option_anchor_outputs, option_anchors_zero_fee_htlc_tx, - LOCAL); + LOCAL, &local_anchor); report(newtx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey, &local_funding_privkey, &local_funding_pubkey, @@ -1142,7 +1143,7 @@ int main(int argc, const char *argv[]) commitment_number ^ cn_obscurer, option_anchor_outputs, option_anchors_zero_fee_htlc_tx, - LOCAL); + LOCAL, &local_anchor); report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey, &local_funding_privkey, &local_funding_pubkey, @@ -1199,7 +1200,7 @@ int main(int argc, const char *argv[]) htlcs, &htlc_map, NULL, commitment_number ^ cn_obscurer, option_anchor_outputs, option_anchors_zero_fee_htlc_tx, - LOCAL); + LOCAL, &local_anchor); print_superverbose = false; tx2 = commit_tx(tmpctx, &funding, @@ -1217,7 +1218,7 @@ int main(int argc, const char *argv[]) commitment_number ^ cn_obscurer, option_anchor_outputs, option_anchors_zero_fee_htlc_tx, - REMOTE); + REMOTE, &local_anchor); tx_must_be_eq(tx, tx2); report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey, &local_funding_privkey, &local_funding_pubkey, diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 6d123c129..c9efc7876 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -364,6 +364,7 @@ int main(int argc, const char *argv[]) bool option_anchor_outputs = false; bool option_anchors_zero_fee_htlc_tx = false; u32 blockheight = 0; + int local_anchor; size_t i; chainparams = chainparams_for_network("bitcoin"); @@ -535,7 +536,8 @@ int main(int argc, const char *argv[]) to_local, to_remote, NULL, &htlc_map, NULL, 0x2bb038521914 ^ 42, - option_anchor_outputs, option_anchors_zero_fee_htlc_tx, LOCAL); + option_anchor_outputs, option_anchors_zero_fee_htlc_tx, + LOCAL, &local_anchor); txs = channel_txs(tmpctx, &funding, funding_amount, &htlc_map, NULL, &funding_wscript_alt, @@ -667,7 +669,8 @@ int main(int argc, const char *argv[]) &keyset, feerate_per_kw[LOCAL], local_config->dust_limit, to_local, to_remote, htlcs, &htlc_map, NULL, 0x2bb038521914 ^ 42, - option_anchor_outputs, option_anchors_zero_fee_htlc_tx, LOCAL); + option_anchor_outputs, option_anchors_zero_fee_htlc_tx, + LOCAL, &local_anchor); txs = channel_txs(tmpctx, &funding, funding_amount, &htlc_map, NULL, &funding_wscript,