2017-02-07 02:44:22 +01:00
|
|
|
#include <bitcoin/script.h>
|
|
|
|
#include <bitcoin/tx.h>
|
|
|
|
#include <ccan/endian/endian.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <channeld/commit_tx.h>
|
2019-05-31 09:30:32 +02:00
|
|
|
#include <common/htlc_trim.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/htlc_tx.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/keyset.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/permute_tx.h>
|
|
|
|
#include <common/utils.h>
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
#ifndef SUPERVERBOSE
|
|
|
|
#define SUPERVERBOSE(...)
|
|
|
|
#endif
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
static bool trim(const struct htlc *htlc,
|
2019-02-21 04:45:55 +01:00
|
|
|
u32 feerate_per_kw,
|
|
|
|
struct amount_sat dust_limit,
|
2017-02-21 05:45:28 +01:00
|
|
|
enum side side)
|
2017-02-07 02:44:22 +01:00
|
|
|
{
|
2019-05-31 09:30:32 +02:00
|
|
|
return htlc_is_trimmed(htlc_owner(htlc), htlc->amount,
|
|
|
|
feerate_per_kw, dust_limit, side);
|
2017-02-21 05:45:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
|
2019-02-21 04:45:55 +01:00
|
|
|
u32 feerate_per_kw,
|
|
|
|
struct amount_sat dust_limit,
|
2017-02-21 05:45:28 +01:00
|
|
|
enum side side)
|
|
|
|
{
|
|
|
|
size_t i, n;
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
for (i = n = 0; i < tal_count(htlcs); i++)
|
2019-02-21 04:45:55 +01:00
|
|
|
n += !trim(htlcs[i], feerate_per_kw, dust_limit, side);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
return n;
|
2017-02-07 02:44:22 +01:00
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
|
|
|
|
const struct htlc *htlc,
|
2017-08-18 06:43:53 +02:00
|
|
|
const struct keyset *keyset)
|
2017-02-21 05:45:28 +01:00
|
|
|
{
|
2017-08-18 06:43:53 +02:00
|
|
|
struct ripemd160 ripemd;
|
2019-03-15 21:50:08 +01:00
|
|
|
u8 *wscript, *p2wsh;
|
|
|
|
struct amount_sat amount = amount_msat_to_sat_round_down(htlc->amount);
|
2017-08-18 06:43:53 +02:00
|
|
|
|
|
|
|
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
|
2019-03-15 21:50:08 +01:00
|
|
|
wscript = htlc_offered_wscript(tx, &ripemd, keyset);
|
|
|
|
p2wsh = scriptpubkey_p2wsh(tx, wscript);
|
2019-08-21 05:52:44 +02:00
|
|
|
bitcoin_tx_add_output(tx, p2wsh, amount);
|
2019-03-15 21:50:08 +01:00
|
|
|
SUPERVERBOSE("# HTLC %" PRIu64 " offered %s wscript %s\n", htlc->id,
|
|
|
|
type_to_string(tmpctx, struct amount_sat, &amount),
|
2019-02-21 04:45:55 +01:00
|
|
|
tal_hex(wscript, wscript));
|
2017-02-21 05:45:28 +01:00
|
|
|
tal_free(wscript);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
|
|
|
|
const struct htlc *htlc,
|
2017-08-18 06:43:53 +02:00
|
|
|
const struct keyset *keyset)
|
2017-02-21 05:45:28 +01:00
|
|
|
{
|
2017-08-18 06:43:53 +02:00
|
|
|
struct ripemd160 ripemd;
|
2019-03-15 21:50:08 +01:00
|
|
|
u8 *wscript, *p2wsh;
|
|
|
|
struct amount_sat amount;
|
2017-08-18 06:43:53 +02:00
|
|
|
|
|
|
|
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
|
|
|
|
wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset);
|
2019-03-15 21:50:08 +01:00
|
|
|
p2wsh = scriptpubkey_p2wsh(tx, wscript);
|
|
|
|
amount = amount_msat_to_sat_round_down(htlc->amount);
|
|
|
|
|
2019-08-21 05:52:44 +02:00
|
|
|
bitcoin_tx_add_output(tx, p2wsh, amount);
|
2019-03-15 21:50:08 +01:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
SUPERVERBOSE("# HTLC %"PRIu64" received %s wscript %s\n",
|
|
|
|
htlc->id,
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
2019-03-15 21:50:08 +01:00
|
|
|
&amount),
|
2019-02-21 04:45:55 +01:00
|
|
|
tal_hex(wscript, wscript));
|
2017-02-21 05:45:28 +01:00
|
|
|
tal_free(wscript);
|
|
|
|
}
|
|
|
|
|
2017-02-07 02:44:22 +01:00
|
|
|
struct bitcoin_tx *commit_tx(const tal_t *ctx,
|
2019-07-30 16:14:43 +02:00
|
|
|
const struct chainparams *chainparams,
|
2017-12-18 07:41:52 +01:00
|
|
|
const struct bitcoin_txid *funding_txid,
|
2017-02-07 02:44:22 +01:00
|
|
|
unsigned int funding_txout,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat funding,
|
2017-02-07 02:44:22 +01:00
|
|
|
enum side funder,
|
|
|
|
u16 to_self_delay,
|
2017-08-18 06:43:53 +02:00
|
|
|
const struct keyset *keyset,
|
2017-11-21 04:33:22 +01:00
|
|
|
u32 feerate_per_kw,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat dust_limit,
|
|
|
|
struct amount_msat self_pay,
|
|
|
|
struct amount_msat other_pay,
|
2017-02-07 02:44:22 +01:00
|
|
|
const struct htlc **htlcs,
|
2017-02-07 02:44:22 +01:00
|
|
|
const struct htlc ***htlcmap,
|
2017-02-21 05:45:28 +01:00
|
|
|
u64 obscured_commitment_number,
|
|
|
|
enum side side)
|
2017-02-07 02:44:22 +01:00
|
|
|
{
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat base_fee;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat total_pay;
|
2017-02-07 02:44:22 +01:00
|
|
|
struct bitcoin_tx *tx;
|
2017-02-21 05:45:28 +01:00
|
|
|
size_t i, n, untrimmed;
|
2018-10-22 06:21:05 +02:00
|
|
|
u32 *cltvs;
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_msat_add(&total_pay, self_pay, other_pay))
|
|
|
|
abort();
|
|
|
|
assert(!amount_msat_greater_sat(total_pay, funding));
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* 1. Calculate which committed HTLCs need to be trimmed (see
|
|
|
|
* [Trimmed Outputs](#trimmed-outputs)).
|
|
|
|
*/
|
2017-02-21 05:45:28 +01:00
|
|
|
untrimmed = commit_tx_num_untrimmed(htlcs,
|
|
|
|
feerate_per_kw,
|
2019-02-21 04:45:55 +01:00
|
|
|
dust_limit, side);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* 2. Calculate the base [commitment transaction
|
|
|
|
* fee](#fee-calculation).
|
|
|
|
*/
|
2019-02-21 04:45:55 +01:00
|
|
|
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
SUPERVERBOSE("# base commitment transaction fee = %s\n",
|
|
|
|
type_to_string(tmpctx, struct amount_sat, &base_fee));
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* 3. Subtract this base fee from the funder (either `to_local` or
|
2018-06-17 12:13:44 +02:00
|
|
|
* `to_remote`), with a floor of 0 (see [Fee Payment](#fee-payment)).
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-02-21 04:45:55 +01:00
|
|
|
try_subtract_fee(funder, side, base_fee, &self_pay, &other_pay);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2017-03-07 06:49:11 +01:00
|
|
|
#ifdef PRINT_ACTUAL_FEE
|
|
|
|
{
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat out = AMOUNT_SAT(0);
|
|
|
|
bool ok = true;
|
2018-07-31 10:52:58 +02:00
|
|
|
for (i = 0; i < tal_count(htlcs); i++) {
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!trim(htlcs[i], feerate_per_kw, dust_limit, side))
|
|
|
|
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(htlcs[i]->amount));
|
2017-03-07 06:49:11 +01:00
|
|
|
}
|
2019-02-21 04:45:55 +01:00
|
|
|
if (amount_msat_greater_sat(self_pay, dust_limit))
|
2019-02-21 04:45:55 +01:00
|
|
|
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(self_pay));
|
2019-02-21 04:45:55 +01:00
|
|
|
if (amount_msat_greater_sat(other_pay, dust_limit))
|
2019-02-21 04:45:55 +01:00
|
|
|
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(other_pay));
|
|
|
|
assert(ok);
|
2017-03-07 06:49:11 +01:00
|
|
|
SUPERVERBOSE("# actual commitment transaction fee = %"PRIu64"\n",
|
2019-02-21 04:45:56 +01:00
|
|
|
funding.satoshis - out.satoshis); /* Raw: test output */
|
2017-03-07 06:49:11 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-02-07 02:44:22 +01:00
|
|
|
/* Worst-case sizing: both to-local and to-remote outputs. */
|
2019-07-30 16:14:43 +02:00
|
|
|
tx = bitcoin_tx(ctx, chainparams, 1, untrimmed + 2);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2017-02-07 02:44:22 +01:00
|
|
|
/* We keep track of which outputs have which HTLCs */
|
2019-03-15 21:50:08 +01:00
|
|
|
*htlcmap = tal_arr(tx, const struct htlc *, tx->wtx->outputs_allocation_len);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2018-10-22 06:21:05 +02:00
|
|
|
/* We keep cltvs for tie-breaking HTLC outputs; we use the same order
|
|
|
|
* for sending the htlc txs, so it may matter. */
|
2019-03-15 21:50:08 +01:00
|
|
|
cltvs = tal_arr(tmpctx, u32, tx->wtx->outputs_allocation_len);
|
2018-10-22 06:21:05 +02:00
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
/* This could be done in a single loop, but we follow the BOLT
|
|
|
|
* literally to make comments in test vectors clearer. */
|
|
|
|
|
|
|
|
n = 0;
|
2017-02-07 02:44:22 +01:00
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2017-02-21 05:45:28 +01:00
|
|
|
* 3. For every offered HTLC, if it is not trimmed, add an
|
|
|
|
* [offered HTLC output](#offered-htlc-outputs).
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2017-02-21 05:45:28 +01:00
|
|
|
for (i = 0; i < tal_count(htlcs); i++) {
|
|
|
|
if (htlc_owner(htlcs[i]) != side)
|
|
|
|
continue;
|
2019-02-21 04:45:55 +01:00
|
|
|
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
|
2017-02-21 05:45:28 +01:00
|
|
|
continue;
|
2017-08-18 06:43:53 +02:00
|
|
|
add_offered_htlc_out(tx, n, htlcs[i], keyset);
|
2018-10-22 06:17:24 +02:00
|
|
|
(*htlcmap)[n] = htlcs[i];
|
2018-10-22 06:21:05 +02:00
|
|
|
cltvs[n] = abs_locktime_to_blocks(&htlcs[i]->expiry);
|
2018-10-22 06:17:24 +02:00
|
|
|
n++;
|
2017-02-07 02:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2017-02-21 05:45:28 +01:00
|
|
|
* 4. For every received HTLC, if it is not trimmed, add an
|
|
|
|
* [received HTLC output](#received-htlc-outputs).
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2017-02-21 05:45:28 +01:00
|
|
|
for (i = 0; i < tal_count(htlcs); i++) {
|
|
|
|
if (htlc_owner(htlcs[i]) == side)
|
|
|
|
continue;
|
2019-02-21 04:45:55 +01:00
|
|
|
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
|
2017-02-21 05:45:28 +01:00
|
|
|
continue;
|
2017-08-18 06:43:53 +02:00
|
|
|
add_received_htlc_out(tx, n, htlcs[i], keyset);
|
2018-10-22 06:17:24 +02:00
|
|
|
(*htlcmap)[n] = htlcs[i];
|
2018-10-22 06:21:05 +02:00
|
|
|
cltvs[n] = abs_locktime_to_blocks(&htlcs[i]->expiry);
|
2018-10-22 06:17:24 +02:00
|
|
|
n++;
|
2017-02-07 02:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* 5. If the `to_local` amount is greater or equal to
|
|
|
|
* `dust_limit_satoshis`, add a [`to_local`
|
2018-09-20 22:19:38 +02:00
|
|
|
* output](#to_local-output).
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-02-21 04:45:55 +01:00
|
|
|
if (amount_msat_greater_eq_sat(self_pay, dust_limit)) {
|
2017-08-18 06:43:53 +02:00
|
|
|
u8 *wscript = to_self_wscript(tmpctx, to_self_delay,keyset);
|
2019-03-15 21:50:08 +01:00
|
|
|
u8 *p2wsh = scriptpubkey_p2wsh(tx, wscript);
|
|
|
|
struct amount_sat amount = amount_msat_to_sat_round_down(self_pay);
|
|
|
|
|
2019-08-21 05:52:44 +02:00
|
|
|
bitcoin_tx_add_output(tx, p2wsh, amount);
|
2018-10-22 06:17:24 +02:00
|
|
|
(*htlcmap)[n] = NULL;
|
2018-10-22 06:21:05 +02:00
|
|
|
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
|
|
|
|
* However, valgrind will warn us something wierd is happening */
|
2019-02-21 04:45:55 +01:00
|
|
|
SUPERVERBOSE("# to-local amount %s wscript %s\n",
|
2019-03-15 21:50:08 +01:00
|
|
|
type_to_string(tmpctx, struct amount_sat, &amount),
|
2017-02-07 02:44:22 +01:00
|
|
|
tal_hex(tmpctx, wscript));
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* 6. If the `to_remote` amount is greater or equal to
|
|
|
|
* `dust_limit_satoshis`, add a [`to_remote`
|
2018-09-20 22:19:38 +02:00
|
|
|
* output](#to_remote-output).
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-02-21 04:45:55 +01:00
|
|
|
if (amount_msat_greater_eq_sat(other_pay, dust_limit)) {
|
2019-03-15 21:50:08 +01:00
|
|
|
struct amount_sat amount = amount_msat_to_sat_round_down(other_pay);
|
|
|
|
u8 *p2wpkh =
|
|
|
|
scriptpubkey_p2wpkh(tx, &keyset->other_payment_key);
|
2017-02-07 02:44:22 +01:00
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* #### `to_remote` Output
|
2017-02-07 02:44:22 +01:00
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* This output sends funds to the other peer and thus is a simple
|
|
|
|
* P2WPKH to `remotepubkey`.
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-08-21 05:52:44 +02:00
|
|
|
int pos = bitcoin_tx_add_output(tx, p2wpkh, amount);
|
2019-03-15 21:50:08 +01:00
|
|
|
assert(pos == n);
|
2018-10-22 06:17:24 +02:00
|
|
|
(*htlcmap)[n] = NULL;
|
2018-10-22 06:21:05 +02:00
|
|
|
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
|
|
|
|
* However, valgrind will warn us something wierd is happening */
|
2019-02-21 04:45:55 +01:00
|
|
|
SUPERVERBOSE("# to-remote amount %s P2WPKH(%s)\n",
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
2019-03-25 13:37:11 +01:00
|
|
|
&amount),
|
2017-08-18 06:43:53 +02:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
&keyset->other_payment_key));
|
2017-02-07 02:44:22 +01:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2019-06-27 02:25:17 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* - MUST set `channel_reserve_satoshis` greater than or equal to
|
|
|
|
* `dust_limit_satoshis`.
|
|
|
|
*/
|
|
|
|
/* This means there must be at least one output. */
|
|
|
|
assert(n > 0);
|
|
|
|
|
2019-03-15 21:50:08 +01:00
|
|
|
assert(n <= tx->wtx->outputs_allocation_len);
|
2018-10-22 06:17:24 +02:00
|
|
|
tal_resize(htlcmap, n);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2019-04-01 05:25:45 +02:00
|
|
|
* 7. Sort the outputs into [BIP 69+CLTV
|
2017-02-07 02:44:22 +01:00
|
|
|
* order](#transaction-input-and-output-ordering)
|
|
|
|
*/
|
2019-03-14 17:47:13 +01:00
|
|
|
permute_outputs(tx, cltvs, (const void **)*htlcmap);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* ## Commitment Transaction
|
|
|
|
*
|
|
|
|
* * version: 2
|
|
|
|
*/
|
2019-03-05 14:28:29 +01:00
|
|
|
assert(tx->wtx->version == 2);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2019-01-14 03:22:05 +01:00
|
|
|
* * locktime: upper 8 bits are 0x20, lower 24 bits are the lower 24 bits of the obscured commitment number
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-03-05 14:28:29 +01:00
|
|
|
tx->wtx->locktime
|
2017-02-07 02:44:22 +01:00
|
|
|
= (0x20000000 | (obscured_commitment_number & 0xFFFFFF));
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* * txin count: 1
|
|
|
|
* * `txin[0]` outpoint: `txid` and `output_index` from
|
|
|
|
* `funding_created` message
|
|
|
|
*/
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2019-01-14 03:22:05 +01:00
|
|
|
* * `txin[0]` sequence: upper 8 bits are 0x80, lower 24 bits are upper 24 bits of the obscured commitment number
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-03-15 21:50:08 +01:00
|
|
|
u32 sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
|
2019-08-21 05:52:44 +02:00
|
|
|
bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, funding, NULL);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
return tx;
|
|
|
|
}
|