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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2023-10-26 13:03:28 +10:30
parent 9b3965c805
commit 01d31e7dde
5 changed files with 44 additions and 29 deletions

View File

@@ -128,7 +128,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
u64 obscured_commitment_number, u64 obscured_commitment_number,
bool option_anchor_outputs, bool option_anchor_outputs,
bool option_anchors_zero_fee_htlc_tx, bool option_anchors_zero_fee_htlc_tx,
enum side side) enum side side,
int *anchor_outnum)
{ {
struct amount_sat base_fee; struct amount_sat base_fee;
struct amount_msat total_pay; struct amount_msat total_pay;
@@ -139,7 +140,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
u32 *cltvs; u32 *cltvs;
bool to_local, to_remote; bool to_local, to_remote;
struct htlc *dummy_to_local = (struct htlc *)0x01, 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, const u8 *funding_wscript = bitcoin_redeem_2of2(tmpctx,
local_funding_key, local_funding_key,
remote_funding_key); remote_funding_key);
@@ -379,9 +381,11 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
n++; n++;
} }
/* With anchors, the caller really wants to know what
* is the LOCAL anchor for the REMOTE side. */
if (to_remote || untrimmed != 0) { if (to_remote || untrimmed != 0) {
tx_add_anchor_output(tx, remote_funding_key); tx_add_anchor_output(tx, remote_funding_key);
(*htlcmap)[n] = NULL; (*htlcmap)[n] = dummy_other_anchor;
n++; n++;
} }
} }
@@ -433,17 +437,21 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
bitcoin_tx_add_input(tx, funding, bitcoin_tx_add_input(tx, funding,
sequence, NULL, funding_sats, NULL, funding_wscript); sequence, NULL, funding_sats, NULL, funding_wscript);
/* Identify the direct outputs (to_us, to_them). */ /* Identify the direct outputs (to_us, to_them), and the local anchor */
if (direct_outputs != NULL) { if (direct_outputs != NULL)
direct_outputs[LOCAL] = direct_outputs[REMOTE] = 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) { *anchor_outnum = -1;
(*htlcmap)[i] = NULL; for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
direct_outputs[LOCAL] = tx->wtx->outputs + i; if ((*htlcmap)[i] == dummy_to_local) {
} else if ((*htlcmap)[i] == dummy_to_remote) { (*htlcmap)[i] = NULL;
(*htlcmap)[i] = NULL; direct_outputs[LOCAL] = tx->wtx->outputs + i;
direct_outputs[REMOTE] = 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;
} }
} }

View File

@@ -63,8 +63,9 @@ bool commit_tx_amount_trimmed(const struct htlc **htlcs,
* @obscured_commitment_number: number to encode in commitment transaction * @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). * @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_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. * @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, * 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 * 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, u64 obscured_commitment_number,
bool option_anchor_outputs, bool option_anchor_outputs,
bool option_anchors_zero_fee_htlc_tx, bool option_anchors_zero_fee_htlc_tx,
enum side side); enum side side,
int *anchor_outnum);
#endif /* LIGHTNING_CHANNELD_COMMIT_TX_H */ #endif /* LIGHTNING_CHANNELD_COMMIT_TX_H */

View File

@@ -315,6 +315,7 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
const struct htlc **committed; const struct htlc **committed;
struct keyset keyset; struct keyset keyset;
struct amount_msat side_pay, other_side_pay; struct amount_msat side_pay, other_side_pay;
int local_anchor;
if (!derive_keyset(per_commitment_point, if (!derive_keyset(per_commitment_point,
&channel->basepoints[side], &channel->basepoints[side],
@@ -363,7 +364,7 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
commitment_number ^ channel->commitment_number_obscurer, commitment_number ^ channel->commitment_number_obscurer,
channel_has(channel, OPT_ANCHOR_OUTPUTS), channel_has(channel, OPT_ANCHOR_OUTPUTS),
channel_has(channel, OPT_ANCHORS_ZERO_FEE_HTLC_TX), channel_has(channel, OPT_ANCHORS_ZERO_FEE_HTLC_TX),
side); side, &local_anchor);
/* Set the remote/local pubkeys on the commitment tx psbt */ /* Set the remote/local pubkeys on the commitment tx psbt */
psbt_input_add_pubkey(txs[0]->psbt, 0, psbt_input_add_pubkey(txs[0]->psbt, 0,

View File

@@ -542,6 +542,7 @@ int main(int argc, const char *argv[])
bool option_anchor_outputs = false; bool option_anchor_outputs = false;
bool option_anchors_zero_fee_htlc_tx = false; bool option_anchors_zero_fee_htlc_tx = false;
bool option_static_remotekey = false; bool option_static_remotekey = false;
int local_anchor;
/* Allow us to check static-remotekey BOLT 3 vectors, too */ /* Allow us to check static-remotekey BOLT 3 vectors, too */
if (argv[1] && streq(argv[1], "--static-remotekey")) 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, NULL, &htlc_map, NULL, commitment_number ^ cn_obscurer,
option_anchor_outputs, option_anchor_outputs,
option_anchors_zero_fee_htlc_tx, option_anchors_zero_fee_htlc_tx,
LOCAL); LOCAL, &local_anchor);
print_superverbose = false; print_superverbose = false;
tx2 = commit_tx(tmpctx, tx2 = commit_tx(tmpctx,
&funding, &funding,
@@ -839,7 +840,7 @@ int main(int argc, const char *argv[])
NULL, &htlc_map2, NULL, commitment_number ^ cn_obscurer, NULL, &htlc_map2, NULL, commitment_number ^ cn_obscurer,
option_anchor_outputs, option_anchor_outputs,
option_anchors_zero_fee_htlc_tx, option_anchors_zero_fee_htlc_tx,
REMOTE); REMOTE, &local_anchor);
tx_must_be_eq(tx, tx2); tx_must_be_eq(tx, tx2);
report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey, report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey,
&local_funding_privkey, &local_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, htlcs, &htlc_map, NULL, commitment_number ^ cn_obscurer,
option_anchor_outputs, option_anchor_outputs,
option_anchors_zero_fee_htlc_tx, option_anchors_zero_fee_htlc_tx,
LOCAL); LOCAL, &local_anchor);
print_superverbose = false; print_superverbose = false;
tx2 = commit_tx(tmpctx, tx2 = commit_tx(tmpctx,
&funding, &funding,
@@ -909,7 +910,7 @@ int main(int argc, const char *argv[])
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
option_anchor_outputs, option_anchor_outputs,
option_anchors_zero_fee_htlc_tx, option_anchors_zero_fee_htlc_tx,
REMOTE); REMOTE, &local_anchor);
tx_must_be_eq(tx, tx2); tx_must_be_eq(tx, tx2);
report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey, report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey,
&local_funding_privkey, &local_funding_pubkey, &local_funding_privkey, &local_funding_pubkey,
@@ -949,7 +950,7 @@ int main(int argc, const char *argv[])
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
option_anchor_outputs, option_anchor_outputs,
option_anchors_zero_fee_htlc_tx, option_anchors_zero_fee_htlc_tx,
LOCAL); LOCAL, &local_anchor);
/* This is what it would look like for peer generating it! */ /* This is what it would look like for peer generating it! */
tx2 = commit_tx(tmpctx, tx2 = commit_tx(tmpctx,
&funding, &funding,
@@ -967,7 +968,7 @@ int main(int argc, const char *argv[])
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
option_anchor_outputs, option_anchor_outputs,
option_anchors_zero_fee_htlc_tx, option_anchors_zero_fee_htlc_tx,
REMOTE); REMOTE, &local_anchor);
tx_must_be_eq(newtx, tx2); tx_must_be_eq(newtx, tx2);
#ifdef DEBUG #ifdef DEBUG
if (feerate_per_kw % 100000 == 0) if (feerate_per_kw % 100000 == 0)
@@ -1011,7 +1012,7 @@ int main(int argc, const char *argv[])
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
option_anchor_outputs, option_anchor_outputs,
option_anchors_zero_fee_htlc_tx, option_anchors_zero_fee_htlc_tx,
LOCAL); LOCAL, &local_anchor);
report(tx, wscript, report(tx, wscript,
&x_remote_funding_privkey, &remote_funding_pubkey, &x_remote_funding_privkey, &remote_funding_pubkey,
&local_funding_privkey, &local_funding_pubkey, &local_funding_privkey, &local_funding_pubkey,
@@ -1063,7 +1064,7 @@ int main(int argc, const char *argv[])
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
option_anchor_outputs, option_anchor_outputs,
option_anchors_zero_fee_htlc_tx, option_anchors_zero_fee_htlc_tx,
LOCAL); LOCAL, &local_anchor);
report(newtx, wscript, report(newtx, wscript,
&x_remote_funding_privkey, &remote_funding_pubkey, &x_remote_funding_privkey, &remote_funding_pubkey,
&local_funding_privkey, &local_funding_pubkey, &local_funding_privkey, &local_funding_pubkey,
@@ -1142,7 +1143,7 @@ int main(int argc, const char *argv[])
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
option_anchor_outputs, option_anchor_outputs,
option_anchors_zero_fee_htlc_tx, option_anchors_zero_fee_htlc_tx,
LOCAL); LOCAL, &local_anchor);
report(tx, wscript, report(tx, wscript,
&x_remote_funding_privkey, &remote_funding_pubkey, &x_remote_funding_privkey, &remote_funding_pubkey,
&local_funding_privkey, &local_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, htlcs, &htlc_map, NULL, commitment_number ^ cn_obscurer,
option_anchor_outputs, option_anchor_outputs,
option_anchors_zero_fee_htlc_tx, option_anchors_zero_fee_htlc_tx,
LOCAL); LOCAL, &local_anchor);
print_superverbose = false; print_superverbose = false;
tx2 = commit_tx(tmpctx, tx2 = commit_tx(tmpctx,
&funding, &funding,
@@ -1217,7 +1218,7 @@ int main(int argc, const char *argv[])
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
option_anchor_outputs, option_anchor_outputs,
option_anchors_zero_fee_htlc_tx, option_anchors_zero_fee_htlc_tx,
REMOTE); REMOTE, &local_anchor);
tx_must_be_eq(tx, tx2); tx_must_be_eq(tx, tx2);
report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey, report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey,
&local_funding_privkey, &local_funding_pubkey, &local_funding_privkey, &local_funding_pubkey,

View File

@@ -364,6 +364,7 @@ int main(int argc, const char *argv[])
bool option_anchor_outputs = false; bool option_anchor_outputs = false;
bool option_anchors_zero_fee_htlc_tx = false; bool option_anchors_zero_fee_htlc_tx = false;
u32 blockheight = 0; u32 blockheight = 0;
int local_anchor;
size_t i; size_t i;
chainparams = chainparams_for_network("bitcoin"); chainparams = chainparams_for_network("bitcoin");
@@ -535,7 +536,8 @@ int main(int argc, const char *argv[])
to_local, to_local,
to_remote, to_remote,
NULL, &htlc_map, NULL, 0x2bb038521914 ^ 42, 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, txs = channel_txs(tmpctx, &funding, funding_amount,
&htlc_map, NULL, &funding_wscript_alt, &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, &keyset, feerate_per_kw[LOCAL], local_config->dust_limit,
to_local, to_remote, htlcs, &htlc_map, NULL, to_local, to_remote, htlcs, &htlc_map, NULL,
0x2bb038521914 ^ 42, 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, txs = channel_txs(tmpctx, &funding, funding_amount,
&htlc_map, NULL, &funding_wscript, &htlc_map, NULL, &funding_wscript,