forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitial_commit_tx.h
145 lines (126 loc) · 5.15 KB
/
initial_commit_tx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* 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"
#include <bitcoin/chainparams.h>
#include <bitcoin/pubkey.h>
#include <common/amount.h>
#include <common/htlc.h>
#include <common/utils.h>
struct bitcoin_txid;
struct keyset;
struct wally_tx_output;
/* 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);
/* The base weight of a commitment tx */
static inline size_t commit_tx_base_weight(size_t num_untrimmed_htlcs,
bool option_anchor_outputs)
{
size_t weight;
/* BOLT #3:
*
* The base fee for a commitment transaction:
* - MUST be calculated to match:
* 1. Start with `weight` = 724 (1124 if `option_anchor_outputs` applies).
*/
if (option_anchor_outputs)
weight = 1124;
else
weight = 724;
/* BOLT #3:
*
* 2. For each committed HTLC, if that output is not trimmed as
* specified in [Trimmed Outputs](#trimmed-outputs), add 172
* to `weight`.
*/
weight += 172 * num_untrimmed_htlcs;
if (chainparams->is_elements) {
/* 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 */
}
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));
}
/**
* initial_commit_tx: create (unsigned) commitment tx to spend the funding tx output
* @ctx: context to allocate transaction and @htlc_map from.
* @funding_txid, @funding_out, @funding: funding outpoint.
* @funding_wscript: scriptPubkey of the funding output
* @funding_keys: funding bitcoin keys
* @opener: is the LOCAL or REMOTE paying the fee?
* @keyset: keys derived for this commit tx.
* @feerate_per_kw: feerate to use
* @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
* @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.
* @option_anchor_outputs: does option_anchor_outputs apply to this channel?
* @err_reason: When NULL is returned, this will point to a human readable reason.
*
* 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,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
struct amount_sat funding,
const struct pubkey funding_key[NUM_SIDES],
enum side opener,
u16 to_self_delay,
const struct keyset *keyset,
u32 feerate_per_kw,
struct amount_sat dust_limit,
struct amount_msat self_pay,
struct amount_msat other_pay,
struct amount_sat self_reserve,
u64 obscured_commitment_number,
struct wally_tx_output *direct_outputs[NUM_SIDES],
enum side side,
bool option_anchor_outputs,
char** err_reason);
/* 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,
struct amount_sat base_fee,
struct amount_msat *self,
struct amount_msat *other);
/* 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,
const struct keyset *keyset);
/* To-other is simply: scriptpubkey_p2wpkh(tx, keyset->other_payment_key) */
/* If we determine we need one, append this anchor output */
void tx_add_anchor_output(struct bitcoin_tx *tx,
const struct pubkey *funding_key);
#endif /* LIGHTNING_COMMON_INITIAL_COMMIT_TX_H */