2017-08-28 18:02:01 +02:00
|
|
|
/* Commit tx without HTLC support; needed for openingd. */
|
|
|
|
#ifndef LIGHTNING_COMMON_INITIAL_COMMIT_TX_H
|
|
|
|
#define LIGHTNING_COMMON_INITIAL_COMMIT_TX_H
|
|
|
|
#include "config.h"
|
2019-07-30 16:14:43 +02:00
|
|
|
#include <bitcoin/chainparams.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <bitcoin/pubkey.h>
|
2019-02-21 04:45:55 +01:00
|
|
|
#include <common/amount.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/htlc.h>
|
2019-07-02 16:23:07 +02:00
|
|
|
#include <common/utils.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
|
2019-07-16 23:46:14 +02:00
|
|
|
struct bitcoin_txid;
|
2017-08-28 18:02:01 +02:00
|
|
|
struct keyset;
|
2020-09-09 09:20:53 +02:00
|
|
|
struct wally_tx_output;
|
2017-08-28 18:02:01 +02:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* This obscures the number of commitments made on the channel in the
|
|
|
|
* case of unilateral close, yet still provides a useful index for
|
|
|
|
* both nodes (who know the `payment_basepoint`s) to quickly find a
|
|
|
|
* revoked commitment transaction.
|
|
|
|
*/
|
|
|
|
u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint,
|
|
|
|
const struct pubkey *accepter_payment_basepoint);
|
|
|
|
|
2020-08-13 19:49:02 +02:00
|
|
|
|
|
|
|
/* The base weight of a commitment tx */
|
|
|
|
static inline size_t commit_tx_base_weight(size_t num_untrimmed_htlcs,
|
|
|
|
bool option_anchor_outputs)
|
2017-08-28 18:02:01 +02:00
|
|
|
{
|
2020-08-13 19:49:02 +02:00
|
|
|
size_t weight;
|
2017-08-28 18:02:01 +02:00
|
|
|
|
2020-08-20 08:49:47 +02:00
|
|
|
/* BOLT #3:
|
2017-08-28 18:02:01 +02:00
|
|
|
*
|
2018-06-17 12:11:53 +02:00
|
|
|
* The base fee for a commitment transaction:
|
|
|
|
* - MUST be calculated to match:
|
2020-08-13 19:44:02 +02:00
|
|
|
* 1. Start with `weight` = 724 (1124 if `option_anchor_outputs` applies).
|
2017-08-28 18:02:01 +02:00
|
|
|
*/
|
2020-08-13 19:44:02 +02:00
|
|
|
if (option_anchor_outputs)
|
|
|
|
weight = 1124;
|
|
|
|
else
|
|
|
|
weight = 724;
|
2017-08-28 18:02:01 +02:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2018-06-17 12:11:53 +02:00
|
|
|
* 2. For each committed HTLC, if that output is not trimmed as
|
|
|
|
* specified in [Trimmed Outputs](#trimmed-outputs), add 172
|
|
|
|
* to `weight`.
|
2017-08-28 18:02:01 +02:00
|
|
|
*/
|
|
|
|
weight += 172 * num_untrimmed_htlcs;
|
|
|
|
|
2019-09-26 00:42:26 +02:00
|
|
|
if (chainparams->is_elements) {
|
2019-07-02 16:23:07 +02:00
|
|
|
/* Each transaction has surjection and rangeproof (both empty
|
|
|
|
* for us as long as we use unblinded L-BTC transactions). */
|
|
|
|
weight += 2 * 4;
|
|
|
|
|
|
|
|
/* Inputs have 6 bytes of blank proofs attached. This TX only
|
|
|
|
* has a single input. */
|
|
|
|
weight += 6;
|
|
|
|
|
|
|
|
/* Each direct output has a bit more weight to it */
|
|
|
|
weight += (32 + 1 + 1 + 1) * 4 * 2; /* Elements added fields */
|
|
|
|
|
|
|
|
/* Each HTLC output also carries a bit more weight */
|
|
|
|
weight += (32 + 1 + 1 + 1) * 4 * num_untrimmed_htlcs;
|
|
|
|
|
|
|
|
/* For elements we also need to add the fee output and the
|
|
|
|
* overhead for rangeproofs into the mix. */
|
|
|
|
weight += (8 + 1) * 4; /* Bitcoin style output */
|
|
|
|
weight += (32 + 1 + 1 + 1) * 4; /* Elements added fields */
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:49:02 +02:00
|
|
|
return weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper to calculate the base fee if we have this many htlc outputs */
|
|
|
|
static inline struct amount_sat commit_tx_base_fee(u32 feerate_per_kw,
|
|
|
|
size_t num_untrimmed_htlcs,
|
|
|
|
bool option_anchor_outputs)
|
|
|
|
{
|
|
|
|
return amount_tx_fee(feerate_per_kw,
|
|
|
|
commit_tx_base_weight(num_untrimmed_htlcs,
|
|
|
|
option_anchor_outputs));
|
2017-08-28 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* initial_commit_tx: create (unsigned) commitment tx to spend the funding tx output
|
|
|
|
* @ctx: context to allocate transaction and @htlc_map from.
|
2019-02-21 04:45:55 +01:00
|
|
|
* @funding_txid, @funding_out, @funding: funding outpoint.
|
2020-05-21 21:46:19 +02:00
|
|
|
* @funding_wscript: scriptPubkey of the funding output
|
2020-08-13 19:46:02 +02:00
|
|
|
* @funding_keys: funding bitcoin keys
|
2019-09-09 18:11:24 +02:00
|
|
|
* @opener: is the LOCAL or REMOTE paying the fee?
|
2017-08-28 18:02:01 +02:00
|
|
|
* @keyset: keys derived for this commit tx.
|
|
|
|
* @feerate_per_kw: feerate to use
|
2019-02-21 04:45:55 +01:00
|
|
|
* @dust_limit: dust limit below which to trim outputs.
|
|
|
|
* @self_pay: amount to pay directly to self
|
|
|
|
* @other_pay: amount to pay directly to the other side
|
|
|
|
* @self_reserve: reserve the other side insisted we have
|
2017-08-28 18:02:01 +02:00
|
|
|
* @obscured_commitment_number: number to encode in commitment transaction
|
2020-05-07 02:43:34 +02:00
|
|
|
* @direct_outputs: If non-NULL, fill with pointers to the direct (non-HTLC) outputs (or NULL if none).
|
2017-08-28 18:02:01 +02:00
|
|
|
* @side: side to generate commitment transaction for.
|
2020-08-13 19:44:02 +02:00
|
|
|
* @option_anchor_outputs: does option_anchor_outputs apply to this channel?
|
2019-04-03 10:20:36 +02:00
|
|
|
* @err_reason: When NULL is returned, this will point to a human readable reason.
|
2017-08-28 18:02:01 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* transaction, so we carefully use the terms "self" and "other" here.
|
|
|
|
*/
|
|
|
|
struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
2017-12-18 07:41:52 +01:00
|
|
|
const struct bitcoin_txid *funding_txid,
|
2017-08-28 18:02:01 +02:00
|
|
|
unsigned int funding_txout,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat funding,
|
2020-08-13 19:46:02 +02:00
|
|
|
const struct pubkey funding_key[NUM_SIDES],
|
2019-09-09 18:11:24 +02:00
|
|
|
enum side opener,
|
2017-08-28 18:02:01 +02:00
|
|
|
u16 to_self_delay,
|
|
|
|
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,
|
|
|
|
struct amount_sat self_reserve,
|
2017-08-28 18:02:01 +02:00
|
|
|
u64 obscured_commitment_number,
|
2020-05-07 02:43:34 +02:00
|
|
|
struct wally_tx_output *direct_outputs[NUM_SIDES],
|
2019-04-03 10:20:36 +02:00
|
|
|
enum side side,
|
2021-06-16 19:44:51 +02:00
|
|
|
u32 csv_lock,
|
2020-08-13 19:44:02 +02:00
|
|
|
bool option_anchor_outputs,
|
2019-04-03 10:20:36 +02:00
|
|
|
char** err_reason);
|
2017-08-28 18:02:01 +02:00
|
|
|
|
2019-09-09 18:11:24 +02:00
|
|
|
/* try_subtract_fee - take away this fee from the opener (and return true), or all if insufficient (and return false). */
|
|
|
|
bool try_subtract_fee(enum side opener, enum side side,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat base_fee,
|
|
|
|
struct amount_msat *self,
|
|
|
|
struct amount_msat *other);
|
2017-08-28 18:02:01 +02:00
|
|
|
|
|
|
|
/* Generate the witness script for the to-self output:
|
|
|
|
* scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */
|
|
|
|
u8 *to_self_wscript(const tal_t *ctx,
|
|
|
|
u16 to_self_delay,
|
2021-06-16 19:44:51 +02:00
|
|
|
u32 csv,
|
2017-08-28 18:02:01 +02:00
|
|
|
const struct keyset *keyset);
|
|
|
|
|
|
|
|
/* To-other is simply: scriptpubkey_p2wpkh(tx, keyset->other_payment_key) */
|
|
|
|
|
2020-08-13 19:47:02 +02:00
|
|
|
/* If we determine we need one, append this anchor output */
|
|
|
|
void tx_add_anchor_output(struct bitcoin_tx *tx,
|
|
|
|
const struct pubkey *funding_key);
|
|
|
|
|
2017-08-28 18:02:01 +02:00
|
|
|
#endif /* LIGHTNING_COMMON_INITIAL_COMMIT_TX_H */
|