Browse Source

txs: Move commit tx generation out of the signature computation

We need the txs around, so don't throw them away after generating them.
nifty/pset-pre
Christian Decker 5 years ago
committed by Rusty Russell
parent
commit
eb8eabcc3c
  1. 35
      channeld/channeld.c
  2. 23
      channeld/commit_tx.c
  3. 2
      channeld/commit_tx.h
  4. 6
      channeld/full_channel.c
  5. 2
      channeld/full_channel.h
  6. 18
      channeld/test/run-commit_tx.c
  7. 22
      channeld/test/run-full_channel.c
  8. 2
      common/initial_channel.c
  9. 2
      common/initial_channel.h
  10. 19
      common/initial_commit_tx.c
  11. 2
      common/initial_commit_tx.h
  12. 10
      devtools/mkcommit.c
  13. 10
      openingd/openingd.c

35
channeld/channeld.c

@ -820,21 +820,17 @@ static u8 *master_wait_sync_reply(const tal_t *ctx,
/* Returns HTLC sigs, sets commit_sig */ /* Returns HTLC sigs, sets commit_sig */
static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx, static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx,
const struct peer *peer, const struct peer *peer,
struct bitcoin_tx **txs,
const u8 *funding_wscript,
const struct htlc **htlc_map,
u64 commit_index, u64 commit_index,
struct bitcoin_signature *commit_sig) struct bitcoin_signature *commit_sig)
{ {
size_t i; size_t i;
struct bitcoin_tx **txs;
const u8 *funding_wscript;
const struct htlc **htlc_map;
struct pubkey local_htlckey; struct pubkey local_htlckey;
const u8 *msg; const u8 *msg;
secp256k1_ecdsa_signature *htlc_sigs; secp256k1_ecdsa_signature *htlc_sigs;
txs = channel_txs(tmpctx, &htlc_map,
&funding_wscript, peer->channel, &peer->remote_per_commit,
commit_index, REMOTE);
msg = towire_hsm_sign_remote_commitment_tx(NULL, txs[0], msg = towire_hsm_sign_remote_commitment_tx(NULL, txs[0],
&peer->channel->funding_pubkey[REMOTE], &peer->channel->funding_pubkey[REMOTE],
*txs[0]->input_amounts[0], *txs[0]->input_amounts[0],
@ -930,6 +926,10 @@ static void send_commit(struct peer *peer)
const struct htlc **changed_htlcs; const struct htlc **changed_htlcs;
struct bitcoin_signature commit_sig; struct bitcoin_signature commit_sig;
secp256k1_ecdsa_signature *htlc_sigs; secp256k1_ecdsa_signature *htlc_sigs;
struct bitcoin_tx **txs;
const u8 *funding_wscript;
const struct htlc **htlc_map;
struct wally_tx_output *direct_outputs[NUM_SIDES];
#if DEVELOPER #if DEVELOPER
/* Hack to suppress all commit sends if dev_disconnect says to */ /* Hack to suppress all commit sends if dev_disconnect says to */
@ -1020,8 +1020,13 @@ static void send_commit(struct peer *peer)
return; return;
} }
htlc_sigs = calc_commitsigs(tmpctx, peer, peer->next_index[REMOTE], txs = channel_txs(tmpctx, &htlc_map, direct_outputs,
&commit_sig); &funding_wscript, peer->channel, &peer->remote_per_commit,
peer->next_index[REMOTE], REMOTE);
htlc_sigs =
calc_commitsigs(tmpctx, peer, txs, funding_wscript, htlc_map,
peer->next_index[REMOTE], &commit_sig);
status_debug("Telling master we're about to commit..."); status_debug("Telling master we're about to commit...");
/* Tell master to save this next commit to database, then wait. */ /* Tell master to save this next commit to database, then wait. */
@ -1261,7 +1266,7 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
commit_sig.sighash_type = SIGHASH_ALL; commit_sig.sighash_type = SIGHASH_ALL;
txs = txs =
channel_txs(tmpctx, &htlc_map, channel_txs(tmpctx, &htlc_map, NULL,
&funding_wscript, peer->channel, &peer->next_local_per_commit, &funding_wscript, peer->channel, &peer->next_local_per_commit,
peer->next_index[LOCAL], LOCAL); peer->next_index[LOCAL], LOCAL);
@ -2018,6 +2023,10 @@ static void resend_commitment(struct peer *peer, const struct changed_htlc *last
struct bitcoin_signature commit_sig; struct bitcoin_signature commit_sig;
secp256k1_ecdsa_signature *htlc_sigs; secp256k1_ecdsa_signature *htlc_sigs;
u8 *msg; u8 *msg;
struct bitcoin_tx **txs;
const u8 *funding_wscript;
const struct htlc **htlc_map;
struct wally_tx_output *direct_outputs[NUM_SIDES];
status_debug("Retransmitting commitment, feerate LOCAL=%u REMOTE=%u", status_debug("Retransmitting commitment, feerate LOCAL=%u REMOTE=%u",
channel_feerate(peer->channel, LOCAL), channel_feerate(peer->channel, LOCAL),
@ -2101,7 +2110,11 @@ static void resend_commitment(struct peer *peer, const struct changed_htlc *last
} }
/* Re-send the commitment_signed itself. */ /* Re-send the commitment_signed itself. */
htlc_sigs = calc_commitsigs(tmpctx, peer, peer->next_index[REMOTE]-1, txs = channel_txs(tmpctx, &htlc_map, direct_outputs,
&funding_wscript, peer->channel, &peer->remote_per_commit,
peer->next_index[REMOTE]-1, REMOTE);
htlc_sigs = calc_commitsigs(tmpctx, peer, txs, funding_wscript, htlc_map, peer->next_index[REMOTE]-1,
&commit_sig); &commit_sig);
msg = towire_commitment_signed(NULL, &peer->channel_id, msg = towire_commitment_signed(NULL, &peer->channel_id,
&commit_sig.s, htlc_sigs); &commit_sig.s, htlc_sigs);

23
channeld/commit_tx.c

@ -94,6 +94,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
struct amount_msat other_pay, struct amount_msat other_pay,
const struct htlc **htlcs, const struct htlc **htlcs,
const struct htlc ***htlcmap, const struct htlc ***htlcmap,
struct wally_tx_output *direct_outputs[NUM_SIDES],
u64 obscured_commitment_number, u64 obscured_commitment_number,
enum side side) enum side side)
{ {
@ -102,7 +103,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
size_t i, n, untrimmed; size_t i, n, untrimmed;
u32 *cltvs; 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)) if (!amount_msat_add(&total_pay, self_pay, other_pay))
abort(); abort();
assert(!amount_msat_greater_sat(total_pay, funding)); 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); struct amount_sat amount = amount_msat_to_sat_round_down(self_pay);
bitcoin_tx_add_output(tx, p2wsh, amount); 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. /* 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",
@ -248,7 +251,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
*/ */
int pos = bitcoin_tx_add_output(tx, p2wpkh, amount); int pos = bitcoin_tx_add_output(tx, p2wpkh, amount);
assert(pos == n); 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. /* 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-remote amount %s P2WPKH(%s)\n", 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)); u32 sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, funding, NULL); 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); bitcoin_tx_finalize(tx);
assert(bitcoin_tx_check(tx)); assert(bitcoin_tx_check(tx));

2
channeld/commit_tx.h

@ -37,6 +37,7 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
* @htlcs: tal_arr of htlcs committed by transaction (some may be trimmed) * @htlcs: tal_arr of htlcs committed by transaction (some may be trimmed)
* @htlc_map: outputed map of outnum->HTLC (NULL for direct outputs). * @htlc_map: outputed map of outnum->HTLC (NULL for direct outputs).
* @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).
* @side: side to generate commitment transaction for. * @side: side to generate commitment transaction for.
* *
* 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,
@ -56,6 +57,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
struct amount_msat other_pay, struct amount_msat other_pay,
const struct htlc **htlcs, const struct htlc **htlcs,
const struct htlc ***htlcmap, const struct htlc ***htlcmap,
struct wally_tx_output *direct_outputs[NUM_SIDES],
u64 obscured_commitment_number, u64 obscured_commitment_number,
enum side side); enum side side);

6
channeld/full_channel.c

@ -272,6 +272,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,
/* FIXME: We could cache these. */ /* FIXME: We could cache these. */
struct bitcoin_tx **channel_txs(const tal_t *ctx, struct bitcoin_tx **channel_txs(const tal_t *ctx,
const struct htlc ***htlcmap, const struct htlc ***htlcmap,
struct wally_tx_output *direct_outputs[NUM_SIDES],
const u8 **funding_wscript, const u8 **funding_wscript,
const struct channel *channel, const struct channel *channel,
const struct pubkey *per_commitment_point, const struct pubkey *per_commitment_point,
@ -299,8 +300,9 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
channel->config[!side].to_self_delay, &keyset, channel->config[!side].to_self_delay, &keyset,
channel_feerate(channel, side), channel_feerate(channel, side),
channel->config[side].dust_limit, channel->view[side].owed[side], channel->config[side].dust_limit, channel->view[side].owed[side],
channel->view[side].owed[!side], committed, htlcmap, channel->view[side].owed[!side], committed, htlcmap, direct_outputs,
commitment_number ^ channel->commitment_number_obscurer, side); commitment_number ^ channel->commitment_number_obscurer,
side);
/* Generating and saving witness script required to spend /* Generating and saving witness script required to spend
* the funding output */ * the funding output */

2
channeld/full_channel.h

@ -50,6 +50,7 @@ struct channel *new_full_channel(const tal_t *ctx,
* @ctx: tal context to allocate return value from. * @ctx: tal context to allocate return value from.
* @channel: The channel to evaluate * @channel: The channel to evaluate
* @htlc_map: Pointer to htlcs for each tx output (allocated off @ctx). * @htlc_map: Pointer to htlcs for each tx output (allocated off @ctx).
* @direct_outputs: If non-NULL, fill with pointers to the direct (non-HTLC) outputs (or NULL if none).
* @funding_wscript: Pointer to wscript for the funding tx output * @funding_wscript: Pointer to wscript for the funding tx output
* @per_commitment_point: Per-commitment point to determine keys * @per_commitment_point: Per-commitment point to determine keys
* @commitment_number: The index of this commitment. * @commitment_number: The index of this commitment.
@ -61,6 +62,7 @@ struct channel *new_full_channel(const tal_t *ctx,
*/ */
struct bitcoin_tx **channel_txs(const tal_t *ctx, struct bitcoin_tx **channel_txs(const tal_t *ctx,
const struct htlc ***htlcmap, const struct htlc ***htlcmap,
struct wally_tx_output *direct_outputs[NUM_SIDES],
const u8 **funding_wscript, const u8 **funding_wscript,
const struct channel *channel, const struct channel *channel,
const struct pubkey *per_commitment_point, const struct pubkey *per_commitment_point,

18
channeld/test/run-commit_tx.c

@ -732,7 +732,7 @@ int main(void)
dust_limit, dust_limit,
to_local, to_local,
to_remote, to_remote,
NULL, &htlc_map, commitment_number ^ cn_obscurer, NULL, &htlc_map, NULL, commitment_number ^ cn_obscurer,
LOCAL); LOCAL);
print_superverbose = false; print_superverbose = false;
tx2 = commit_tx(tmpctx, tx2 = commit_tx(tmpctx,
@ -744,7 +744,7 @@ int main(void)
dust_limit, dust_limit,
to_local, to_local,
to_remote, to_remote,
NULL, &htlc_map2, commitment_number ^ cn_obscurer, NULL, &htlc_map2, NULL, commitment_number ^ cn_obscurer,
REMOTE); REMOTE);
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,
@ -788,7 +788,7 @@ int main(void)
dust_limit, dust_limit,
to_local, to_local,
to_remote, to_remote,
htlcs, &htlc_map, commitment_number ^ cn_obscurer, htlcs, &htlc_map, NULL, commitment_number ^ cn_obscurer,
LOCAL); LOCAL);
print_superverbose = false; print_superverbose = false;
tx2 = commit_tx(tmpctx, tx2 = commit_tx(tmpctx,
@ -800,7 +800,7 @@ int main(void)
dust_limit, dust_limit,
to_local, to_local,
to_remote, to_remote,
inv_htlcs, &htlc_map2, inv_htlcs, &htlc_map2, NULL,
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
REMOTE); REMOTE);
tx_must_be_eq(tx, tx2); tx_must_be_eq(tx, tx2);
@ -832,7 +832,7 @@ int main(void)
dust_limit, dust_limit,
to_local, to_local,
to_remote, to_remote,
htlcs, &htlc_map, htlcs, &htlc_map, NULL,
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
LOCAL); LOCAL);
/* This is what it would look like for peer generating it! */ /* This is what it would look like for peer generating it! */
@ -845,7 +845,7 @@ int main(void)
dust_limit, dust_limit,
to_local, to_local,
to_remote, to_remote,
inv_htlcs, &htlc_map2, inv_htlcs, &htlc_map2, NULL,
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
REMOTE); REMOTE);
tx_must_be_eq(newtx, tx2); tx_must_be_eq(newtx, tx2);
@ -877,7 +877,7 @@ int main(void)
dust_limit, dust_limit,
to_local, to_local,
to_remote, to_remote,
htlcs, &htlc_map, htlcs, &htlc_map, NULL,
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
LOCAL); LOCAL);
report(tx, wscript, report(tx, wscript,
@ -914,7 +914,7 @@ int main(void)
dust_limit, dust_limit,
to_local, to_local,
to_remote, to_remote,
htlcs, &htlc_map, htlcs, &htlc_map, NULL,
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
LOCAL); LOCAL);
report(newtx, wscript, report(newtx, wscript,
@ -973,7 +973,7 @@ int main(void)
dust_limit, dust_limit,
to_local, to_local,
to_remote, to_remote,
htlcs, &htlc_map, htlcs, &htlc_map, NULL,
commitment_number ^ cn_obscurer, commitment_number ^ cn_obscurer,
LOCAL); LOCAL);
report(tx, wscript, report(tx, wscript,

22
channeld/test/run-full_channel.c

@ -519,10 +519,10 @@ int main(void)
local_config->dust_limit, local_config->dust_limit,
to_local, to_local,
to_remote, to_remote,
NULL, &htlc_map, 0x2bb038521914 ^ 42, LOCAL); NULL, &htlc_map, NULL, 0x2bb038521914 ^ 42, LOCAL);
txs = channel_txs(tmpctx, txs = channel_txs(tmpctx,
&htlc_map, &funding_wscript_alt, &htlc_map, NULL, &funding_wscript_alt,
lchannel, &local_per_commitment_point, 42, LOCAL); lchannel, &local_per_commitment_point, 42, LOCAL);
assert(tal_count(txs) == 1); assert(tal_count(txs) == 1);
assert(tal_count(htlc_map) == 2); assert(tal_count(htlc_map) == 2);
@ -530,7 +530,7 @@ int main(void)
tx_must_be_eq(txs[0], raw_tx); tx_must_be_eq(txs[0], raw_tx);
txs2 = channel_txs(tmpctx, txs2 = channel_txs(tmpctx,
&htlc_map, &funding_wscript, &htlc_map, NULL, &funding_wscript,
rchannel, &local_per_commitment_point, 42, REMOTE); rchannel, &local_per_commitment_point, 42, REMOTE);
txs_must_be_eq(txs, txs2); txs_must_be_eq(txs, txs2);
@ -557,10 +557,10 @@ int main(void)
assert(lchannel->view[REMOTE].owed[REMOTE].millisatoshis assert(lchannel->view[REMOTE].owed[REMOTE].millisatoshis
== rchannel->view[LOCAL].owed[LOCAL].millisatoshis); == rchannel->view[LOCAL].owed[LOCAL].millisatoshis);
txs = channel_txs(tmpctx, &htlc_map, &funding_wscript, txs = channel_txs(tmpctx, &htlc_map, NULL, &funding_wscript,
lchannel, &local_per_commitment_point, 42, LOCAL); lchannel, &local_per_commitment_point, 42, LOCAL);
assert(tal_count(txs) == 1); assert(tal_count(txs) == 1);
txs2 = channel_txs(tmpctx, &htlc_map, &funding_wscript, txs2 = channel_txs(tmpctx, &htlc_map, NULL, &funding_wscript,
rchannel, &local_per_commitment_point, 42, REMOTE); rchannel, &local_per_commitment_point, 42, REMOTE);
txs_must_be_eq(txs, txs2); txs_must_be_eq(txs, txs2);
@ -575,10 +575,10 @@ int main(void)
assert(lchannel->view[REMOTE].owed[REMOTE].millisatoshis assert(lchannel->view[REMOTE].owed[REMOTE].millisatoshis
== rchannel->view[LOCAL].owed[LOCAL].millisatoshis); == rchannel->view[LOCAL].owed[LOCAL].millisatoshis);
txs = channel_txs(tmpctx, &htlc_map, &funding_wscript, txs = channel_txs(tmpctx, &htlc_map, NULL, &funding_wscript,
lchannel, &local_per_commitment_point, 42, LOCAL); lchannel, &local_per_commitment_point, 42, LOCAL);
assert(tal_count(txs) == 6); assert(tal_count(txs) == 6);
txs2 = channel_txs(tmpctx, &htlc_map, &funding_wscript, txs2 = channel_txs(tmpctx, &htlc_map, NULL, &funding_wscript,
rchannel, &local_per_commitment_point, 42, REMOTE); rchannel, &local_per_commitment_point, 42, REMOTE);
txs_must_be_eq(txs, txs2); txs_must_be_eq(txs, txs2);
@ -641,15 +641,15 @@ int main(void)
tmpctx, &funding_txid, funding_output_index, tmpctx, &funding_txid, funding_output_index,
funding_amount, LOCAL, remote_config->to_self_delay, funding_amount, LOCAL, remote_config->to_self_delay,
&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, 0x2bb038521914 ^ 42, to_local, to_remote, htlcs, &htlc_map, NULL,
LOCAL); 0x2bb038521914 ^ 42, LOCAL);
txs = channel_txs(tmpctx, &htlc_map, &funding_wscript, txs = channel_txs(tmpctx, &htlc_map, NULL, &funding_wscript,
lchannel, &local_per_commitment_point, 42, lchannel, &local_per_commitment_point, 42,
LOCAL); LOCAL);
tx_must_be_eq(txs[0], raw_tx); tx_must_be_eq(txs[0], raw_tx);
txs2 = channel_txs(tmpctx, &htlc_map, &funding_wscript, txs2 = channel_txs(tmpctx, &htlc_map, NULL, &funding_wscript,
rchannel, &local_per_commitment_point, rchannel, &local_per_commitment_point,
42, REMOTE); 42, REMOTE);
txs_must_be_eq(txs, txs2); txs_must_be_eq(txs, txs2);

2
common/initial_channel.c

@ -71,6 +71,7 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
const struct channel *channel, const struct channel *channel,
const struct pubkey *per_commitment_point, const struct pubkey *per_commitment_point,
enum side side, enum side side,
struct wally_tx_output *direct_outputs[NUM_SIDES],
char** err_reason) char** err_reason)
{ {
struct keyset keyset; struct keyset keyset;
@ -105,6 +106,7 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
channel->view[side].owed[!side], channel->view[side].owed[!side],
channel->config[!side].channel_reserve, channel->config[!side].channel_reserve,
0 ^ channel->commitment_number_obscurer, 0 ^ channel->commitment_number_obscurer,
direct_outputs,
side, side,
err_reason); err_reason);
} }

2
common/initial_channel.h

@ -106,6 +106,7 @@ struct channel *new_initial_channel(const tal_t *ctx,
* @channel: The channel to evaluate * @channel: The channel to evaluate
* @per_commitment_point: Per-commitment point to determine keys * @per_commitment_point: Per-commitment point to determine keys
* @side: which side to get the commitment transaction for * @side: which side to get the commitment transaction for
* @direct_outputs: If non-NULL, fill with pointers to the direct (non-HTLC) outputs (or NULL if none).
* @err_reason: When NULL is returned, this will point to a human readable reason. * @err_reason: When NULL is returned, this will point to a human readable reason.
* *
* Returns the unsigned initial commitment transaction for @side, or NULL * Returns the unsigned initial commitment transaction for @side, or NULL
@ -116,6 +117,7 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
const struct channel *channel, const struct channel *channel,
const struct pubkey *per_commitment_point, const struct pubkey *per_commitment_point,
enum side side, enum side side,
struct wally_tx_output *direct_outputs[NUM_SIDES],
char** err_reason); char** err_reason);
/** /**

19
common/initial_commit_tx.c

@ -71,6 +71,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
struct amount_msat other_pay, struct amount_msat other_pay,
struct amount_sat self_reserve, struct amount_sat self_reserve,
u64 obscured_commitment_number, u64 obscured_commitment_number,
struct wally_tx_output *direct_outputs[NUM_SIDES],
enum side side, enum side side,
char** err_reason) char** err_reason)
{ {
@ -80,6 +81,8 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
struct amount_msat total_pay; struct amount_msat total_pay;
struct amount_sat amount; struct amount_sat amount;
u32 sequence; u32 sequence;
void *dummy_local = (void *)LOCAL, *dummy_remote = (void *)REMOTE;
const void *output_order[NUM_SIDES];
if (!amount_msat_add(&total_pay, self_pay, other_pay)) if (!amount_msat_add(&total_pay, self_pay, other_pay))
abort(); abort();
@ -180,6 +183,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
tx->output_witscripts[n]->ptr = tx->output_witscripts[n]->ptr =
tal_dup_arr(tx->output_witscripts[n], u8, tal_dup_arr(tx->output_witscripts[n], u8,
wscript, tal_count(wscript), 0); wscript, tal_count(wscript), 0);
output_order[n] = dummy_local;
n++; n++;
} }
@ -202,6 +206,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
tx, scriptpubkey_p2wpkh(tx, &keyset->other_payment_key), tx, scriptpubkey_p2wpkh(tx, &keyset->other_payment_key),
amount); amount);
assert(pos == n); assert(pos == n);
output_order[n] = dummy_remote;
n++; n++;
} }
@ -212,7 +217,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
* 7. Sort the outputs into [BIP 69+CLTV * 7. Sort the outputs into [BIP 69+CLTV
* order](#transaction-input-and-output-ordering) * order](#transaction-input-and-output-ordering)
*/ */
permute_outputs(tx, NULL, NULL); permute_outputs(tx, NULL, output_order);
/* BOLT #3: /* BOLT #3:
* *
@ -241,7 +246,19 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF)); sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, funding, NULL); bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, funding, NULL);
if (direct_outputs != NULL) {
direct_outputs[LOCAL] = direct_outputs[REMOTE] = NULL;
for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
if (output_order[i] == dummy_local)
direct_outputs[LOCAL] = &tx->wtx->outputs[i];
else if (output_order[i] == dummy_remote)
direct_outputs[REMOTE] = &tx->wtx->outputs[i];
}
}
/* This doesn't reorder outputs, so we can do this after mapping outputs. */
bitcoin_tx_finalize(tx); bitcoin_tx_finalize(tx);
assert(bitcoin_tx_check(tx)); assert(bitcoin_tx_check(tx));
return tx; return tx;

2
common/initial_commit_tx.h

@ -84,6 +84,7 @@ static inline struct amount_sat commit_tx_base_fee(u32 feerate_per_kw,
* @other_pay: amount to pay directly to the other side * @other_pay: amount to pay directly to the other side
* @self_reserve: reserve the other side insisted we have * @self_reserve: reserve the other side insisted we have
* @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).
* @side: side to generate commitment transaction for. * @side: side to generate commitment transaction for.
* @err_reason: When NULL is returned, this will point to a human readable reason. * @err_reason: When NULL is returned, this will point to a human readable reason.
* *
@ -104,6 +105,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
struct amount_msat other_pay, struct amount_msat other_pay,
struct amount_sat self_reserve, struct amount_sat self_reserve,
u64 obscured_commitment_number, u64 obscured_commitment_number,
struct wally_tx_output *direct_outputs[NUM_SIDES],
enum side side, enum side side,
char** err_reason); char** err_reason);

10
devtools/mkcommit.c

@ -397,8 +397,9 @@ int main(int argc, char *argv[])
if (!per_commit_point(&localseed, &local_per_commit_point, commitnum)) if (!per_commit_point(&localseed, &local_per_commit_point, commitnum))
errx(1, "Bad deriving local per-commitment-point"); errx(1, "Bad deriving local per-commitment-point");
local_txs = channel_txs(NULL, &htlcmap, &funding_wscript, channel, local_txs = channel_txs(NULL, &htlcmap, NULL, &funding_wscript, channel,
&local_per_commit_point, commitnum, LOCAL); &local_per_commit_point, commitnum,
LOCAL);
printf("## local_commitment\n" printf("## local_commitment\n"
"# input amount %s, funding_wscript %s, pubkey %s\n", "# input amount %s, funding_wscript %s, pubkey %s\n",
@ -511,8 +512,9 @@ int main(int argc, char *argv[])
/* Create the remote commitment tx */ /* Create the remote commitment tx */
if (!per_commit_point(&remoteseed, &remote_per_commit_point, commitnum)) if (!per_commit_point(&remoteseed, &remote_per_commit_point, commitnum))
errx(1, "Bad deriving remote per-commitment-point"); errx(1, "Bad deriving remote per-commitment-point");
remote_txs = channel_txs(NULL, &htlcmap, &funding_wscript, channel, remote_txs = channel_txs(NULL, &htlcmap, NULL, &funding_wscript, channel,
&remote_per_commit_point, commitnum, REMOTE); &remote_per_commit_point, commitnum,
REMOTE);
remote_txs[0]->input_amounts[0] remote_txs[0]->input_amounts[0]
= tal_dup(remote_txs[0], struct amount_sat, &funding_amount); = tal_dup(remote_txs[0], struct amount_sat, &funding_amount);

10
openingd/openingd.c

@ -665,6 +665,7 @@ static bool funder_finalize_channel_setup(struct state *state,
struct channel_id id_in; struct channel_id id_in;
const u8 *wscript; const u8 *wscript;
char *err_reason; char *err_reason;
struct wally_tx_output *direct_outputs[NUM_SIDES];
/*~ Now we can initialize the `struct channel`. This represents /*~ Now we can initialize the `struct channel`. This represents
* the current channel state and is how we can generate the current * the current channel state and is how we can generate the current
@ -710,7 +711,7 @@ static bool funder_finalize_channel_setup(struct state *state,
/* This gives us their first commitment transaction. */ /* This gives us their first commitment transaction. */
*tx = initial_channel_tx(state, &wscript, state->channel, *tx = initial_channel_tx(state, &wscript, state->channel,
&state->first_per_commitment_point[REMOTE], &state->first_per_commitment_point[REMOTE],
REMOTE, &err_reason); REMOTE, direct_outputs, &err_reason);
if (!*tx) { if (!*tx) {
/* This should not happen: we should never create channels we /* This should not happen: we should never create channels we
* can't afford the fees for after reserve. */ * can't afford the fees for after reserve. */
@ -820,7 +821,7 @@ static bool funder_finalize_channel_setup(struct state *state,
* signature they sent against that. */ * signature they sent against that. */
*tx = initial_channel_tx(state, &wscript, state->channel, *tx = initial_channel_tx(state, &wscript, state->channel,
&state->first_per_commitment_point[LOCAL], &state->first_per_commitment_point[LOCAL],
LOCAL, &err_reason); LOCAL, direct_outputs, &err_reason);
if (!*tx) { if (!*tx) {
negotiation_failed(state, true, negotiation_failed(state, true,
"Could not meet our fees and reserve: %s", err_reason); "Could not meet our fees and reserve: %s", err_reason);
@ -903,6 +904,7 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
const u8 *wscript; const u8 *wscript;
u8 channel_flags; u8 channel_flags;
char* err_reason; char* err_reason;
struct wally_tx_output *direct_outputs[NUM_SIDES];
/* BOLT #2: /* BOLT #2:
* *
@ -1185,7 +1187,7 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
*/ */
local_commit = initial_channel_tx(state, &wscript, state->channel, local_commit = initial_channel_tx(state, &wscript, state->channel,
&state->first_per_commitment_point[LOCAL], &state->first_per_commitment_point[LOCAL],
LOCAL, &err_reason); LOCAL, NULL, &err_reason);
/* This shouldn't happen either, AFAICT. */ /* This shouldn't happen either, AFAICT. */
if (!local_commit) { if (!local_commit) {
negotiation_failed(state, false, negotiation_failed(state, false,
@ -1245,7 +1247,7 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
*/ */
remote_commit = initial_channel_tx(state, &wscript, state->channel, remote_commit = initial_channel_tx(state, &wscript, state->channel,
&state->first_per_commitment_point[REMOTE], &state->first_per_commitment_point[REMOTE],
REMOTE, &err_reason); REMOTE, direct_outputs, &err_reason);
if (!remote_commit) { if (!remote_commit) {
negotiation_failed(state, false, negotiation_failed(state, false,
"Could not meet their fees and reserve: %s", err_reason); "Could not meet their fees and reserve: %s", err_reason);

Loading…
Cancel
Save