• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12312390362

13 Dec 2024 08:44AM UTC coverage: 57.458% (+8.5%) from 48.92%
12312390362

Pull #9343

github

ellemouton
fn: rework the ContextGuard and add tests

In this commit, the ContextGuard struct is re-worked such that the
context that its new main WithCtx method provides is cancelled in sync
with a parent context being cancelled or with it's quit channel being
cancelled. Tests are added to assert the behaviour. In order for the
close of the quit channel to be consistent with the cancelling of the
derived context, the quit channel _must_ be contained internal to the
ContextGuard so that callers are only able to close the channel via the
exposed Quit method which will then take care to first cancel any
derived context that depend on the quit channel before returning.
Pull Request #9343: fn: expand the ContextGuard and add tests

101853 of 177264 relevant lines covered (57.46%)

24972.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.99
/input/size.go
1
package input
2

3
import (
4
        "github.com/btcsuite/btcd/blockchain"
5
        "github.com/btcsuite/btcd/txscript"
6
        "github.com/btcsuite/btcd/wire"
7
        "github.com/btcsuite/btcwallet/waddrmgr"
8
        "github.com/lightningnetwork/lnd/lntypes"
9
)
10

11
const (
12
        // witnessScaleFactor determines the level of "discount" witness data
13
        // receives compared to "base" data. A scale factor of 4, denotes that
14
        // witness data is 1/4 as cheap as regular non-witness data. Value copied
15
        // here for convenience.
16
        witnessScaleFactor = blockchain.WitnessScaleFactor
17

18
        // The weight(weight), which is different from the !size! (see BIP-141),
19
        // is calculated as:
20
        // Weight = 4 * BaseSize + WitnessSize (weight).
21
        // BaseSize - size of the transaction without witness data (bytes).
22
        // WitnessSize - witness size (bytes).
23
        // Weight - the metric for determining the weight of the transaction.
24

25
        // P2WPKHSize 22 bytes
26
        //        - OP_0: 1 byte
27
        //        - OP_DATA: 1 byte (PublicKeyHASH160 length)
28
        //        - PublicKeyHASH160: 20 bytes
29
        P2WPKHSize = 1 + 1 + 20
30

31
        // NestedP2WPKHSize 23 bytes
32
        //      - OP_DATA: 1 byte (P2WPKHSize)
33
        //      - P2WPKHWitnessProgram: 22 bytes
34
        NestedP2WPKHSize = 1 + P2WPKHSize
35

36
        // P2WSHSize 34 bytes
37
        //        - OP_0: 1 byte
38
        //        - OP_DATA: 1 byte (WitnessScriptSHA256 length)
39
        //        - WitnessScriptSHA256: 32 bytes
40
        P2WSHSize = 1 + 1 + 32
41

42
        // NestedP2WSHSize 35 bytes
43
        //      - OP_DATA: 1 byte (P2WSHSize)
44
        //      - P2WSHWitnessProgram: 34 bytes
45
        NestedP2WSHSize = 1 + P2WSHSize
46

47
        // UnknownWitnessSize 42 bytes
48
        //      - OP_x: 1 byte
49
        //      - OP_DATA: 1 byte (max-size length)
50
        //      - max-size: 40 bytes
51
        UnknownWitnessSize = 1 + 1 + 40
52

53
        // BaseOutputSize 9 bytes
54
        //     - value: 8 bytes
55
        //     - var_int: 1 byte (pkscript_length)
56
        BaseOutputSize = 8 + 1
57

58
        // P2PKHSize 25 bytes.
59
        P2PKHSize = 25
60

61
        // P2PKHOutputSize 34 bytes
62
        //      - value: 8 bytes
63
        //      - var_int: 1 byte (pkscript_length)
64
        //      - pkscript (p2pkh): 25 bytes
65
        P2PKHOutputSize = BaseOutputSize + P2PKHSize
66

67
        // P2WKHOutputSize 31 bytes
68
        //      - value: 8 bytes
69
        //      - var_int: 1 byte (pkscript_length)
70
        //      - pkscript (p2wpkh): 22 bytes
71
        P2WKHOutputSize = BaseOutputSize + P2WPKHSize
72

73
        // P2WSHOutputSize 43 bytes
74
        //      - value: 8 bytes
75
        //      - var_int: 1 byte (pkscript_length)
76
        //      - pkscript (p2wsh): 34 bytes
77
        P2WSHOutputSize = BaseOutputSize + P2WSHSize
78

79
        // P2SHSize 23 bytes.
80
        P2SHSize = 23
81

82
        // P2SHOutputSize 32 bytes
83
        //      - value: 8 bytes
84
        //      - var_int: 1 byte (pkscript_length)
85
        //      - pkscript (p2sh): 23 bytes
86
        P2SHOutputSize = BaseOutputSize + P2SHSize
87

88
        // P2TRSize 34 bytes
89
        //        - OP_0: 1 byte
90
        //        - OP_DATA: 1 byte (x-only public key length)
91
        //        - x-only public key length: 32 bytes
92
        P2TRSize = 34
93

94
        // P2TROutputSize 43 bytes
95
        //      - value: 8 bytes
96
        //      - var_int: 1 byte (pkscript_length)
97
        //      - pkscript (p2tr): 34 bytes
98
        P2TROutputSize = BaseOutputSize + P2TRSize
99

100
        // P2PKHScriptSigSize 108 bytes
101
        //      - OP_DATA: 1 byte (signature length)
102
        //      - signature
103
        //      - OP_DATA: 1 byte (pubkey length)
104
        //      - pubkey
105
        P2PKHScriptSigSize = 1 + 73 + 1 + 33
106

107
        // P2WKHWitnessSize 109 bytes
108
        //      - number_of_witness_elements: 1 byte
109
        //      - signature_length: 1 byte
110
        //      - signature
111
        //      - pubkey_length: 1 byte
112
        //      - pubkey
113
        P2WKHWitnessSize = 1 + 1 + 73 + 1 + 33
114

115
        // MultiSigSize 71 bytes
116
        //        - OP_2: 1 byte
117
        //        - OP_DATA: 1 byte (pubKeyAlice length)
118
        //        - pubKeyAlice: 33 bytes
119
        //        - OP_DATA: 1 byte (pubKeyBob length)
120
        //        - pubKeyBob: 33 bytes
121
        //        - OP_2: 1 byte
122
        //        - OP_CHECKMULTISIG: 1 byte
123
        MultiSigSize = 1 + 1 + 33 + 1 + 33 + 1 + 1
124

125
        // MultiSigWitnessSize 222 bytes
126
        //        - NumberOfWitnessElements: 1 byte
127
        //        - NilLength: 1 byte
128
        //        - sigAliceLength: 1 byte
129
        //        - sigAlice: 73 bytes
130
        //        - sigBobLength: 1 byte
131
        //        - sigBob: 73 bytes
132
        //        - WitnessScriptLength: 1 byte
133
        //        - WitnessScript (MultiSig)
134
        MultiSigWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + MultiSigSize
135

136
        // InputSize 41 bytes
137
        //        - PreviousOutPoint:
138
        //                - Hash: 32 bytes
139
        //                - Index: 4 bytes
140
        //        - OP_DATA: 1 byte (ScriptSigLength)
141
        //        - ScriptSig: 0 bytes
142
        //        - Witness <----        we use "Witness" instead of "ScriptSig" for
143
        //                         transaction validation, but "Witness" is stored
144
        //                         separately and weight for it size is smaller. So
145
        //                         we separate the calculation of ordinary data
146
        //                         from witness data.
147
        //        - Sequence: 4 bytes
148
        InputSize = 32 + 4 + 1 + 4
149

150
        // FundingInputSize 41 bytes
151
        // FundingInputSize represents the size of an input to a funding
152
        // transaction, and is equivalent to the size of a standard segwit input
153
        // as calculated above.
154
        FundingInputSize = InputSize
155

156
        // CommitmentDelayOutput 43 bytes
157
        //        - Value: 8 bytes
158
        //        - VarInt: 1 byte (PkScript length)
159
        //        - PkScript (P2WSH)
160
        CommitmentDelayOutput = 8 + 1 + P2WSHSize
161

162
        // TaprootCommitmentOutput 43 bytes
163
        //        - Value: 8 bytes
164
        //        - VarInt: 1 byte (PkScript length)
165
        //        - PkScript (P2TR)
166
        TaprootCommitmentOutput = 8 + 1 + P2TRSize
167

168
        // CommitmentKeyHashOutput 31 bytes
169
        //        - Value: 8 bytes
170
        //        - VarInt: 1 byte (PkScript length)
171
        //        - PkScript (P2WPKH)
172
        CommitmentKeyHashOutput = 8 + 1 + P2WPKHSize
173

174
        // CommitmentAnchorOutput 43 bytes
175
        //        - Value: 8 bytes
176
        //        - VarInt: 1 byte (PkScript length)
177
        //        - PkScript (P2WSH)
178
        CommitmentAnchorOutput = 8 + 1 + P2WSHSize
179

180
        // TaprootCommitmentAnchorOutput 43 bytes
181
        //        - Value: 8 bytes
182
        //        - VarInt: 1 byte (PkScript length)
183
        //        - PkScript (P2TR)
184
        TaprootCommitmentAnchorOutput = 8 + 1 + P2TRSize
185

186
        // HTLCSize 43 bytes
187
        //        - Value: 8 bytes
188
        //        - VarInt: 1 byte (PkScript length)
189
        //        - PkScript (PW2SH)
190
        HTLCSize = 8 + 1 + P2WSHSize
191

192
        // WitnessHeaderSize 2 bytes
193
        //        - Flag: 1 byte
194
        //        - Marker: 1 byte
195
        WitnessHeaderSize = 1 + 1
196

197
        // BaseTxSize 8 bytes
198
        //      - Version: 4 bytes
199
        //      - LockTime: 4 bytes
200
        BaseTxSize = 4 + 4
201

202
        // BaseCommitmentTxSize 125 + 43 * num-htlc-outputs bytes
203
        //        - Version: 4 bytes
204
        //        - WitnessHeader <---- part of the witness data
205
        //        - CountTxIn: 1 byte
206
        //        - TxIn: 41 bytes
207
        //                FundingInput
208
        //        - CountTxOut: 1 byte
209
        //        - TxOut: 74 + 43 * num-htlc-outputs bytes
210
        //                OutputPayingToThem,
211
        //                OutputPayingToUs,
212
        //                ....HTLCOutputs...
213
        //        - LockTime: 4 bytes
214
        BaseCommitmentTxSize = 4 + 1 + FundingInputSize + 1 +
215
                CommitmentDelayOutput + CommitmentKeyHashOutput + 4
216

217
        // BaseCommitmentTxWeight 500 weight
218
        BaseCommitmentTxWeight = witnessScaleFactor * BaseCommitmentTxSize
219

220
        // WitnessCommitmentTxWeight 224 weight
221
        WitnessCommitmentTxWeight = WitnessHeaderSize + MultiSigWitnessSize
222

223
        // BaseAnchorCommitmentTxSize 225 + 43 * num-htlc-outputs bytes
224
        //        - Version: 4 bytes
225
        //        - WitnessHeader <---- part of the witness data
226
        //        - CountTxIn: 1 byte
227
        //        - TxIn: 41 bytes
228
        //                FundingInput
229
        //        - CountTxOut: 3 byte
230
        //        - TxOut: 4*43 + 43 * num-htlc-outputs bytes
231
        //                OutputPayingToThem,
232
        //                OutputPayingToUs,
233
        //                AnchorPayingToThem,
234
        //                AnchorPayingToUs,
235
        //                ....HTLCOutputs...
236
        //        - LockTime: 4 bytes
237
        BaseAnchorCommitmentTxSize = 4 + 1 + FundingInputSize + 3 +
238
                2*CommitmentDelayOutput + 2*CommitmentAnchorOutput + 4
239

240
        // BaseAnchorCommitmentTxWeight 900 weight.
241
        BaseAnchorCommitmentTxWeight = witnessScaleFactor * BaseAnchorCommitmentTxSize
242

243
        // BaseTaprootCommitmentTxWeight 225 + 43 * num-htlc-outputs bytes
244
        //        - Version: 4 bytes
245
        //        - WitnessHeader <---- part of the witness data
246
        //        - CountTxIn: 1 byte
247
        //        - TxIn: 41 bytes
248
        //                FundingInput
249
        //        - CountTxOut: 3 byte
250
        //        - TxOut: 172 + 43 * num-htlc-outputs bytes
251
        //                OutputPayingToThem,
252
        //                OutputPayingToUs,
253
        //                ....HTLCOutputs...
254
        //        - LockTime: 4 bytes
255
        BaseTaprootCommitmentTxWeight = (4 + 1 + FundingInputSize + 3 +
256
                2*TaprootCommitmentOutput + 2*TaprootCommitmentAnchorOutput +
257
                4) * witnessScaleFactor
258

259
        // CommitWeight 724 weight.
260
        CommitWeight = BaseCommitmentTxWeight + WitnessCommitmentTxWeight
261

262
        // AnchorCommitWeight 1124 weight.
263
        AnchorCommitWeight = BaseAnchorCommitmentTxWeight + WitnessCommitmentTxWeight
264

265
        // TaprootCommitWeight 968 weight.
266
        TaprootCommitWeight = (BaseTaprootCommitmentTxWeight +
267
                WitnessHeaderSize + TaprootKeyPathWitnessSize)
268

269
        // HTLCWeight 172 weight.
270
        HTLCWeight = witnessScaleFactor * HTLCSize
271

272
        // HtlcTimeoutWeight 663 weight
273
        // HtlcTimeoutWeight is the weight of the HTLC timeout transaction
274
        // which will transition an outgoing HTLC to the delay-and-claim state.
275
        HtlcTimeoutWeight = 663
276

277
        // TaprootHtlcTimeoutWeight is the total weight of the taproot HTLC
278
        // timeout transaction.
279
        TaprootHtlcTimeoutWeight = 645
280

281
        // HtlcSuccessWeight 703 weight
282
        // HtlcSuccessWeight is the weight of the HTLC success transaction
283
        // which will transition an incoming HTLC to the delay-and-claim state.
284
        HtlcSuccessWeight = 703
285

286
        // TaprootHtlcSuccessWeight is the total weight of the taproot HTLC
287
        // success transaction.
288
        TaprootHtlcSuccessWeight = 705
289

290
        // HtlcConfirmedScriptOverhead 3 bytes
291
        // HtlcConfirmedScriptOverhead is the extra length of an HTLC script
292
        // that requires confirmation before it can be spent. These extra bytes
293
        // is a result of the extra CSV check.
294
        HtlcConfirmedScriptOverhead = 3
295

296
        // HtlcTimeoutWeightConfirmed 666 weight
297
        // HtlcTimeoutWeightConfirmed is the weight of the HTLC timeout
298
        // transaction which will transition an outgoing HTLC to the
299
        // delay-and-claim state, for the confirmed HTLC outputs. It is 3 bytes
300
        // larger because of the additional CSV check in the input script.
301
        HtlcTimeoutWeightConfirmed = HtlcTimeoutWeight + HtlcConfirmedScriptOverhead
302

303
        // HtlcSuccessWeightConfirmed 706 weight
304
        // HtlcSuccessWeightConfirmed is the weight of the HTLC success
305
        // transaction which will transition an incoming HTLC to the
306
        // delay-and-claim state, for the confirmed HTLC outputs. It is 3 bytes
307
        // larger because of the cdditional CSV check in the input script.
308
        HtlcSuccessWeightConfirmed = HtlcSuccessWeight + HtlcConfirmedScriptOverhead
309

310
        // MaxHTLCNumber 966
311
        // MaxHTLCNumber is the maximum number HTLCs which can be included in a
312
        // commitment transaction. This limit was chosen such that, in the case
313
        // of a contract breach, the punishment transaction is able to sweep
314
        // all the HTLC's yet still remain below the widely used standard
315
        // weight limits.
316
        MaxHTLCNumber = 966
317

318
        // ToLocalScriptSize 79 bytes
319
        //      - OP_IF: 1 byte
320
        //          - OP_DATA: 1 byte
321
        //          - revoke_key: 33 bytes
322
        //      - OP_ELSE: 1 byte
323
        //          - OP_DATA: 1 byte
324
        //          - csv_delay: 4 bytes
325
        //          - OP_CHECKSEQUENCEVERIFY: 1 byte
326
        //          - OP_DROP: 1 byte
327
        //          - OP_DATA: 1 byte
328
        //          - delay_key: 33 bytes
329
        //      - OP_ENDIF: 1 byte
330
        //      - OP_CHECKSIG: 1 byte
331
        ToLocalScriptSize = 1 + 1 + 33 + 1 + 1 + 4 + 1 + 1 + 1 + 33 + 1 + 1
332

333
        // LeaseWitnessScriptSizeOverhead represents the size overhead in bytes
334
        // of the witness scripts used within script enforced lease commitments.
335
        // This overhead results from the additional CLTV clause required to
336
        // spend.
337
        //
338
        //        - OP_DATA: 1 byte
339
        //         - lease_expiry: 4 bytes
340
        //         - OP_CHECKLOCKTIMEVERIFY: 1 byte
341
        //         - OP_DROP: 1 byte
342
        LeaseWitnessScriptSizeOverhead = 1 + 4 + 1 + 1
343

344
        // ToLocalTimeoutWitnessSize 156 bytes
345
        //      - number_of_witness_elements: 1 byte
346
        //      - local_delay_sig_length: 1 byte
347
        //      - local_delay_sig: 73 bytes
348
        //      - zero_length: 1 byte
349
        //      - witness_script_length: 1 byte
350
        //      - witness_script (to_local_script)
351
        ToLocalTimeoutWitnessSize = 1 + 1 + 73 + 1 + 1 + ToLocalScriptSize
352

353
        // ToLocalPenaltyWitnessSize 157 bytes
354
        //      - number_of_witness_elements: 1 byte
355
        //      - revocation_sig_length: 1 byte
356
        //      - revocation_sig: 73 bytes
357
        //      - OP_TRUE_length: 1 byte
358
        //      - OP_TRUE: 1 byte
359
        //      - witness_script_length: 1 byte
360
        //      - witness_script (to_local_script)
361
        ToLocalPenaltyWitnessSize = 1 + 1 + 73 + 1 + 1 + 1 + ToLocalScriptSize
362

363
        // ToRemoteConfirmedScriptSize 37 bytes
364
        //      - OP_DATA: 1 byte
365
        //      - to_remote_key: 33 bytes
366
        //      - OP_CHECKSIGVERIFY: 1 byte
367
        //      - OP_1: 1 byte
368
        //      - OP_CHECKSEQUENCEVERIFY: 1 byte
369
        ToRemoteConfirmedScriptSize = 1 + 33 + 1 + 1 + 1
370

371
        // ToRemoteConfirmedWitnessSize 113 bytes
372
        //      - number_of_witness_elements: 1 byte
373
        //      - sig_length: 1 byte
374
        //      - sig: 73 bytes
375
        //      - witness_script_length: 1 byte
376
        //      - witness_script (to_remote_delayed_script)
377
        ToRemoteConfirmedWitnessSize = 1 + 1 + 73 + 1 + ToRemoteConfirmedScriptSize
378

379
        // AcceptedHtlcScriptSize 140 bytes
380
        //      - OP_DUP: 1 byte
381
        //      - OP_HASH160: 1 byte
382
        //      - OP_DATA: 1 byte (RIPEMD160(SHA256(revocationkey)) length)
383
        //      - RIPEMD160(SHA256(revocationkey)): 20 bytes
384
        //      - OP_EQUAL: 1 byte
385
        //      - OP_IF: 1 byte
386
        //              - OP_CHECKSIG: 1 byte
387
        //      - OP_ELSE: 1 byte
388
        //              - OP_DATA: 1 byte (remotekey length)
389
        //              - remotekey: 33 bytes
390
        //              - OP_SWAP: 1 byte
391
        //              - OP_SIZE: 1 byte
392
        //              - OP_DATA: 1 byte (32 length)
393
        //              - 32: 1 byte
394
        //              - OP_EQUAL: 1 byte
395
        //              - OP_IF: 1 byte
396
        //                      - OP_HASH160: 1 byte
397
        //                      - OP_DATA: 1 byte (RIPEMD160(payment_hash) length)
398
        //                      - RIPEMD160(payment_hash): 20 bytes
399
        //                      - OP_EQUALVERIFY: 1 byte
400
        //                      - 2: 1 byte
401
        //                      - OP_SWAP: 1 byte
402
        //                      - OP_DATA: 1 byte (localkey length)
403
        //                      - localkey: 33 bytes
404
        //                      - 2: 1 byte
405
        //                      - OP_CHECKMULTISIG: 1 byte
406
        //              - OP_ELSE: 1 byte
407
        //                      - OP_DROP: 1 byte
408
        //                      - OP_DATA: 1 byte (cltv_expiry length)
409
        //                      - cltv_expiry: 4 bytes
410
        //                      - OP_CHECKLOCKTIMEVERIFY: 1 byte
411
        //                      - OP_DROP: 1 byte
412
        //                      - OP_CHECKSIG: 1 byte
413
        //              - OP_ENDIF: 1 byte
414
        //              - OP_1: 1 byte                // These 3 extra bytes are only
415
        //              - OP_CSV: 1 byte        // present for the confirmed
416
        //              - OP_DROP: 1 byte        // HTLC script types.
417
        //      - OP_ENDIF: 1 byte
418
        AcceptedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 8*1 + 20 + 4*1 +
419
                33 + 5*1 + 4 + 5*1
420

421
        // AcceptedHtlcScriptSizeConfirmed 143 bytes.
422
        AcceptedHtlcScriptSizeConfirmed = AcceptedHtlcScriptSize +
423
                HtlcConfirmedScriptOverhead
424

425
        // AcceptedHtlcTimeoutWitnessSize 217 bytes
426
        //      - number_of_witness_elements: 1 byte
427
        //      - sender_sig_length: 1 byte
428
        //      - sender_sig: 73 bytes
429
        //      - nil_length: 1 byte
430
        //      - witness_script_length: 1 byte
431
        //      - witness_script: (accepted_htlc_script)
432
        AcceptedHtlcTimeoutWitnessSize = 1 + 1 + 73 + 1 + 1 + AcceptedHtlcScriptSize
433

434
        // AcceptedHtlcTimeoutWitnessSizeConfirmed 220 bytes.
435
        AcceptedHtlcTimeoutWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 1 +
436
                AcceptedHtlcScriptSizeConfirmed
437

438
        // AcceptedHtlcPenaltyWitnessSize 250 bytes
439
        //      - number_of_witness_elements: 1 byte
440
        //      - revocation_sig_length: 1 byte
441
        //      - revocation_sig: 73 bytes
442
        //      - revocation_key_length: 1 byte
443
        //      - revocation_key: 33 bytes
444
        //      - witness_script_length: 1 byte
445
        //      - witness_script (accepted_htlc_script)
446
        AcceptedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + AcceptedHtlcScriptSize
447

448
        // AcceptedHtlcPenaltyWitnessSizeConfirmed 253 bytes.
449
        AcceptedHtlcPenaltyWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 33 + 1 +
450
                AcceptedHtlcScriptSizeConfirmed
451

452
        // AcceptedHtlcSuccessWitnessSize 324 bytes
453
        //      - number_of_witness_elements: 1 byte
454
        //      - nil_length: 1 byte
455
        //      - sig_alice_length: 1 byte
456
        //      - sig_alice: 73 bytes
457
        //      - sig_bob_length: 1 byte
458
        //      - sig_bob: 73 bytes
459
        //      - preimage_length: 1 byte
460
        //      - preimage: 32 bytes
461
        //      - witness_script_length: 1 byte
462
        //      - witness_script (accepted_htlc_script)
463
        //
464
        // Input to second level success tx, spending non-delayed HTLC output.
465
        AcceptedHtlcSuccessWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 32 + 1 +
466
                AcceptedHtlcScriptSize
467

468
        // AcceptedHtlcSuccessWitnessSizeConfirmed 327 bytes
469
        //
470
        // Input to second level success tx, spending 1 CSV delayed HTLC output.
471
        AcceptedHtlcSuccessWitnessSizeConfirmed = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 32 + 1 +
472
                AcceptedHtlcScriptSizeConfirmed
473

474
        // OfferedHtlcScriptSize 133 bytes
475
        //      - OP_DUP: 1 byte
476
        //      - OP_HASH160: 1 byte
477
        //      - OP_DATA: 1 byte (RIPEMD160(SHA256(revocationkey)) length)
478
        //      - RIPEMD160(SHA256(revocationkey)): 20 bytes
479
        //      - OP_EQUAL: 1 byte
480
        //      - OP_IF: 1 byte
481
        //              - OP_CHECKSIG: 1 byte
482
        //      - OP_ELSE: 1 byte
483
        //              - OP_DATA: 1 byte (remotekey length)
484
        //              - remotekey: 33 bytes
485
        //              - OP_SWAP: 1 byte
486
        //              - OP_SIZE: 1 byte
487
        //              - OP_DATA: 1 byte (32 length)
488
        //              - 32: 1 byte
489
        //              - OP_EQUAL: 1 byte
490
        //              - OP_NOTIF: 1 byte
491
        //                      - OP_DROP: 1 byte
492
        //                      - 2: 1 byte
493
        //                      - OP_SWAP: 1 byte
494
        //                      - OP_DATA: 1 byte (localkey length)
495
        //                      - localkey: 33 bytes
496
        //                      - 2: 1 byte
497
        //                      - OP_CHECKMULTISIG: 1 byte
498
        //              - OP_ELSE: 1 byte
499
        //                      - OP_HASH160: 1 byte
500
        //                      - OP_DATA: 1 byte (RIPEMD160(payment_hash) length)
501
        //                      - RIPEMD160(payment_hash): 20 bytes
502
        //                      - OP_EQUALVERIFY: 1 byte
503
        //                      - OP_CHECKSIG: 1 byte
504
        //              - OP_ENDIF: 1 byte
505
        //              - OP_1: 1 byte                // These 3 extra bytes are only
506
        //              - OP_CSV: 1 byte        // present for the confirmed
507
        //              - OP_DROP: 1 byte        // HTLC script types.
508
        //      - OP_ENDIF: 1 byte
509
        OfferedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 10*1 + 33 + 5*1 + 20 + 4*1
510

511
        // OfferedHtlcScriptSizeConfirmed 136 bytes.
512
        OfferedHtlcScriptSizeConfirmed = OfferedHtlcScriptSize +
513
                HtlcConfirmedScriptOverhead
514

515
        // OfferedHtlcSuccessWitnessSize 242 bytes
516
        //      - number_of_witness_elements: 1 byte
517
        //      - receiver_sig_length: 1 byte
518
        //      - receiver_sig: 73 bytes
519
        //      - payment_preimage_length: 1 byte
520
        //      - payment_preimage: 32 bytes
521
        //      - witness_script_length: 1 byte
522
        //      - witness_script (offered_htlc_script)
523
        OfferedHtlcSuccessWitnessSize = 1 + 1 + 73 + 1 + 32 + 1 + OfferedHtlcScriptSize
524

525
        // OfferedHtlcSuccessWitnessSizeConfirmed 245 bytes.
526
        OfferedHtlcSuccessWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 32 + 1 +
527
                OfferedHtlcScriptSizeConfirmed
528

529
        // OfferedHtlcTimeoutWitnessSize 285 bytes
530
        //      - number_of_witness_elements: 1 byte
531
        //      - nil_length: 1 byte
532
        //      - sig_alice_length: 1 byte
533
        //      - sig_alice: 73 bytes
534
        //      - sig_bob_length: 1 byte
535
        //      - sig_bob: 73 bytes
536
        //      - nil_length: 1 byte
537
        //      - witness_script_length: 1 byte
538
        //      - witness_script (offered_htlc_script)
539
        //
540
        // Input to second level timeout tx, spending non-delayed HTLC output.
541
        OfferedHtlcTimeoutWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 1 +
542
                OfferedHtlcScriptSize
543

544
        // OfferedHtlcTimeoutWitnessSizeConfirmed 288 bytes
545
        //
546
        // Input to second level timeout tx, spending 1 CSV delayed HTLC output.
547
        OfferedHtlcTimeoutWitnessSizeConfirmed = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 1 +
548
                OfferedHtlcScriptSizeConfirmed
549

550
        // OfferedHtlcPenaltyWitnessSize 243 bytes
551
        //      - number_of_witness_elements: 1 byte
552
        //      - revocation_sig_length: 1 byte
553
        //      - revocation_sig: 73 bytes
554
        //      - revocation_key_length: 1 byte
555
        //      - revocation_key: 33 bytes
556
        //      - witness_script_length: 1 byte
557
        //      - witness_script (offered_htlc_script)
558
        OfferedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + OfferedHtlcScriptSize
559

560
        // OfferedHtlcPenaltyWitnessSizeConfirmed 246 bytes.
561
        OfferedHtlcPenaltyWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 33 + 1 +
562
                OfferedHtlcScriptSizeConfirmed
563

564
        // AnchorScriptSize 40 bytes
565
        //      - pubkey_length: 1 byte
566
        //      - pubkey: 33 bytes
567
        //      - OP_CHECKSIG: 1 byte
568
        //      - OP_IFDUP: 1 byte
569
        //      - OP_NOTIF: 1 byte
570
        //              - OP_16: 1 byte
571
        //              - OP_CSV 1 byte
572
        //      - OP_ENDIF: 1 byte
573
        AnchorScriptSize = 1 + 33 + 6*1
574

575
        // AnchorWitnessSize 116 bytes
576
        //      - number_of_witnes_elements: 1 byte
577
        //      - signature_length: 1 byte
578
        //      - signature: 73 bytes
579
        //      - witness_script_length: 1 byte
580
        //      - witness_script (anchor_script)
581
        AnchorWitnessSize = 1 + 1 + 73 + 1 + AnchorScriptSize
582

583
        // TaprootSignatureWitnessSize 65 bytes
584
        //        - sigLength: 1 byte
585
        //        - sig: 64 bytes
586
        TaprootSignatureWitnessSize = 1 + 64
587

588
        // TaprootKeyPathWitnessSize 66 bytes
589
        //        - NumberOfWitnessElements: 1 byte
590
        //        - sigLength: 1 byte
591
        //        - sig: 64 bytes
592
        TaprootKeyPathWitnessSize = 1 + TaprootSignatureWitnessSize
593

594
        // TaprootKeyPathCustomSighashWitnessSize 67 bytes
595
        //        - NumberOfWitnessElements: 1 byte
596
        //        - sigLength: 1 byte
597
        //        - sig: 64 bytes
598
        //      - sighashFlag: 1 byte
599
        TaprootKeyPathCustomSighashWitnessSize = TaprootKeyPathWitnessSize + 1
600

601
        // TaprootBaseControlBlockWitnessSize 33 bytes
602
        //      - leafVersionAndParity: 1 byte
603
        //      - schnorrPubKey: 32 byte
604
        TaprootBaseControlBlockWitnessSize = 33
605

606
        // TaprootToLocalScriptSize
607
        //      - OP_DATA: 1 byte (pub key len)
608
        //      - local_key: 32 bytes
609
        //      - OP_CHECKSIG: 1 byte
610
        //      - OP_DATA: 1 byte (csv delay)
611
        //      - csv_delay: 4 bytes (worst case estimate)
612
        //      - OP_CSV: 1 byte
613
        //      - OP_DROP: 1 byte
614
        TaprootToLocalScriptSize = 41
615

616
        // TaprootToLocalWitnessSize: 175 bytes
617
        //      - number_of_witness_elements: 1 byte
618
        //      - sig_len: 1 byte
619
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
620
        //      - script_len: 1 byte
621
        //      - taproot_to_local_script_size: 41 bytes
622
        //      - ctrl_block_len: 1 byte
623
        //      - base_control_block_size: 33 bytes
624
        //      - sibling_merkle_hash: 32 bytes
625
        TaprootToLocalWitnessSize = 1 + 1 + 65 + 1 + TaprootToLocalScriptSize +
626
                1 + TaprootBaseControlBlockWitnessSize + 32
627

628
        // TaprootToLocalRevokeScriptSize: 68 bytes
629
        //        - OP_DATA: 1 byte
630
        //        - local key: 32 bytes
631
        //         - OP_DROP: 1 byte
632
        //         - OP_DATA: 1 byte
633
        //         - revocation key: 32 bytes
634
        //         - OP_CHECKSIG: 1 byte
635
        TaprootToLocalRevokeScriptSize = 1 + 32 + 1 + 1 + 32 + 1
636

637
        // TaprootToLocalRevokeWitnessSize: 202 bytes
638
        //         - NumberOfWitnessElements: 1 byte
639
        //         - sigLength: 1 byte
640
        //         - sweep sig: 65 bytes
641
        //         - script len: 1 byte
642
        //         - revocation script size: 68 bytes
643
        //         - ctrl block size: 1 byte
644
        //         - base control block: 33 bytes
645
        //        - merkle proof: 32
646
        TaprootToLocalRevokeWitnessSize = (1 + 1 + 65 + 1 +
647
                TaprootToLocalRevokeScriptSize + 1 + 33 + 32)
648

649
        // TaprootToRemoteScriptSize
650
        //         - OP_DATA: 1 byte
651
        //         - remote key: 32 bytes
652
        //         - OP_CHECKSIG: 1 byte
653
        //         - OP_1: 1 byte
654
        //         - OP_CHECKSEQUENCEVERIFY: 1 byte
655
        //         - OP_DROP: 1 byte
656
        TaprootToRemoteScriptSize = 1 + 32 + 1 + 1 + 1 + 1
657

658
        // TaprootToRemoteWitnessSize:
659
        //      - number_of_witness_elements: 1 byte
660
        //      - sig_len: 1 byte
661
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
662
        //      - script_len: 1 byte
663
        //      - taproot_to_local_script_size: 36 bytes
664
        //      - ctrl_block_len: 1 byte
665
        //      - base_control_block_size: 33 bytes
666
        TaprootToRemoteWitnessSize = (1 + 1 + 65 + 1 +
667
                TaprootToRemoteScriptSize + 1 +
668
                TaprootBaseControlBlockWitnessSize)
669

670
        // TaprootAnchorWitnessSize: 67 bytes
671
        //
672
        // In this case, we use the custom sighash size to give the most
673
        // pessemistic estimate.
674
        TaprootAnchorWitnessSize = TaprootKeyPathCustomSighashWitnessSize
675

676
        // TaprootSecondLevelHtlcScriptSize: 41 bytes
677
        //      - OP_DATA: 1 byte (pub key len)
678
        //      - local_key: 32 bytes
679
        //      - OP_CHECKSIG: 1 byte
680
        //      - OP_DATA: 1 byte (csv delay)
681
        //      - csv_delay: 4 bytes (worst case)
682
        //      - OP_CSV: 1 byte
683
        //      - OP_DROP: 1 byte
684
        TaprootSecondLevelHtlcScriptSize = 1 + 32 + 1 + 1 + 4 + 1 + 1
685

686
        // TaprootSecondLevelHtlcWitnessSize:
687
        //      - number_of_witness_elements: 1 byte
688
        //      - sig_len: 1 byte
689
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
690
        //      - script_len: 1 byte
691
        //      - taproot_second_level_htlc_script_size: 40 bytes
692
        //      - ctrl_block_len: 1 byte
693
        //      - base_control_block_size: 33 bytes
694
        TaprootSecondLevelHtlcWitnessSize = 1 + 1 + 65 + 1 +
695
                TaprootSecondLevelHtlcScriptSize + 1 +
696
                TaprootBaseControlBlockWitnessSize
697

698
        // TaprootSecondLevelRevokeWitnessSize
699
        //      - number_of_witness_elements: 1 byte
700
        //      - sig_len: 1 byte
701
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
702
        //nolint:ll
703
        TaprootSecondLevelRevokeWitnessSize = TaprootKeyPathCustomSighashWitnessSize
704

705
        // TaprootAcceptedRevokeWitnessSize:
706
        //      - number_of_witness_elements: 1 byte
707
        //      - sig_len: 1 byte
708
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
709
        //nolint:ll
710
        TaprootAcceptedRevokeWitnessSize = TaprootKeyPathCustomSighashWitnessSize
711

712
        // TaprootOfferedRevokeWitnessSize:
713
        //      - number_of_witness_elements: 1 byte
714
        //      - sig_len: 1 byte
715
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
716
        TaprootOfferedRevokeWitnessSize = TaprootKeyPathCustomSighashWitnessSize
717

718
        // TaprootHtlcOfferedRemoteTimeoutScriptSize: 42 bytes
719
        //        - OP_DATA: 1 byte (pub key len)
720
        //        - local_key: 32 bytes
721
        //        - OP_CHECKSIG: 1 byte
722
        //      - OP_1: 1 byte
723
        //      - OP_DROP: 1 byte
724
        //      - OP_CHECKSEQUENCEVERIFY: 1 byte
725
        //      - OP_DATA: 1 byte (cltv_expiry length)
726
        //      - cltv_expiry: 4 bytes
727
        //      - OP_CHECKLOCKTIMEVERIFY: 1 byte
728
        //      - OP_DROP: 1 byte
729
        TaprootHtlcOfferedRemoteTimeoutScriptSize = (1 + 32 + 1 + 1 + 1 + 1 +
730
                1 + 4 + 1 + 1)
731

732
        // TaprootHtlcOfferedRemoteTimeoutwitSize: 176 bytes
733
        //      - number_of_witness_elements: 1 byte
734
        //      - sig_len: 1 byte
735
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
736
        //      - script_len: 1 byte
737
        //      - taproot_offered_htlc_script_size: 42 bytes
738
        //      - ctrl_block_len: 1 byte
739
        //      - base_control_block_size: 33 bytes
740
        //      - sibilng_merkle_proof: 32 bytes
741
        TaprootHtlcOfferedRemoteTimeoutWitnessSize = 1 + 1 + 65 + 1 +
742
                TaprootHtlcOfferedRemoteTimeoutScriptSize + 1 +
743
                TaprootBaseControlBlockWitnessSize + 32
744

745
        // TaprootHtlcOfferedLocalTmeoutScriptSize:
746
        //        - OP_DATA: 1 byte (pub key len)
747
        //        - local_key: 32 bytes
748
        //        - OP_CHECKSIGVERIFY: 1 byte
749
        //        - OP_DATA: 1 byte (pub key len)
750
        //        - remote_key: 32 bytes
751
        //        - OP_CHECKSIG: 1 byte
752
        TaprootHtlcOfferedLocalTimeoutScriptSize = 1 + 32 + 1 + 1 + 32 + 1
753

754
        // TaprootOfferedLocalTimeoutWitnessSize
755
        //      - number_of_witness_elements: 1 byte
756
        //      - sig_len: 1 byte
757
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
758
        //      - sig_len: 1 byte
759
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
760
        //      - script_len: 1 byte
761
        //      - taproot_offered_htlc_script_timeout_size:
762
        //      - ctrl_block_len: 1 byte
763
        //      - base_control_block_size: 33 bytes
764
        //      - sibilng_merkle_proof: 32 bytes
765
        TaprootOfferedLocalTimeoutWitnessSize = 1 + 1 + 65 + 1 + 65 + 1 +
766
                TaprootHtlcOfferedLocalTimeoutScriptSize + 1 +
767
                TaprootBaseControlBlockWitnessSize + 32
768

769
        // TaprootHtlcAcceptedRemoteSuccessScriptSize:
770
        //      - OP_SIZE: 1 byte
771
        //      - OP_DATA: 1 byte
772
        //      - 32: 1 byte
773
        //      - OP_EQUALVERIFY: 1 byte
774
        //      - OP_HASH160: 1 byte
775
        //      - OP_DATA: 1 byte (RIPEMD160(payment_hash) length)
776
        //      - RIPEMD160(payment_hash): 20 bytes
777
        //      - OP_EQUALVERIFY: 1 byte
778
        //        - OP_DATA: 1 byte (pub key len)
779
        //        - remote_key: 32 bytes
780
        //        - OP_CHECKSIG: 1 byte
781
        //      - OP_1: 1 byte
782
        //      - OP_CSV: 1 byte
783
        //      - OP_DROP: 1 byte
784
        TaprootHtlcAcceptedRemoteSuccessScriptSize = 1 + 1 + 1 + 1 + 1 + 1 +
785
                1 + 20 + 1 + 32 + 1 + 1 + 1 + 1
786

787
        // TaprootHtlcAcceptedRemoteSuccessScriptSize:
788
        //      - number_of_witness_elements: 1 byte
789
        //      - sig_len: 1 byte
790
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
791
        //      - payment_preimage_length: 1 byte
792
        //      - payment_preimage: 32 bytes
793
        //      - script_len: 1 byte
794
        //      - taproot_offered_htlc_script_success_size:
795
        //      - ctrl_block_len: 1 byte
796
        //      - base_control_block_size: 33 bytes
797
        //      - sibilng_merkle_proof: 32 bytes
798
        TaprootHtlcAcceptedRemoteSuccessWitnessSize = 1 + 1 + 65 + 1 + 32 + 1 +
799
                TaprootHtlcAcceptedRemoteSuccessScriptSize + 1 +
800
                TaprootBaseControlBlockWitnessSize + 32
801

802
        // TaprootHtlcAcceptedLocalSuccessScriptSize:
803
        //      - OP_SIZE: 1 byte
804
        //      - OP_DATA: 1 byte
805
        //      - 32: 1 byte
806
        //      - OP_EQUALVERIFY: 1 byte
807
        //      - OP_HASH160: 1 byte
808
        //      - OP_DATA: 1 byte (RIPEMD160(payment_hash) length)
809
        //      - RIPEMD160(payment_hash): 20 bytes
810
        //      - OP_EQUALVERIFY: 1 byte
811
        //        - OP_DATA: 1 byte (pub key len)
812
        //        - local_key: 32 bytes
813
        //        - OP_CHECKSIGVERIFY: 1 byte
814
        //        - OP_DATA: 1 byte (pub key len)
815
        //        - remote_key: 32 bytes
816
        //        - OP_CHECKSIG: 1 byte
817
        TaprootHtlcAcceptedLocalSuccessScriptSize = 1 + 1 + 1 + 1 + 1 + 1 +
818
                20 + 1 + 1 + 32 + 1 + 1 + 32 + 1
819

820
        // TaprootHtlcAcceptedLocalSuccessWitnessSize:
821
        //      - number_of_witness_elements: 1 byte
822
        //      - sig_len: 1 byte
823
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
824
        //      - sig_len: 1 byte
825
        //      - sweep_sig: 65 bytes (worst case w/o sighash default)
826
        //      - payment_preimage_length: 1 byte
827
        //      - payment_preimage: 32 bytes
828
        //      - script_len: 1 byte
829
        //      - taproot_accepted_htlc_script_success_size:
830
        //      - ctrl_block_len: 1 byte
831
        //      - base_control_block_size: 33 bytes
832
        //      - sibilng_merkle_proof: 32 bytes
833
        TaprootHtlcAcceptedLocalSuccessWitnessSize = 1 + 1 + 65 + 1 + 65 + 1 +
834
                32 + 1 + TaprootHtlcAcceptedLocalSuccessScriptSize + 1 +
835
                TaprootBaseControlBlockWitnessSize + 32
836
)
837

838
// EstimateCommitTxWeight estimate commitment transaction weight depending on
839
// the precalculated weight of base transaction, witness data, which is needed
840
// for paying for funding tx, and htlc weight multiplied by their count.
841
func EstimateCommitTxWeight(count int, prediction bool) int64 {
46✔
842
        // Make prediction about the size of commitment transaction with
46✔
843
        // additional HTLC.
46✔
844
        if prediction {
46✔
845
                count++
×
846
        }
×
847

848
        htlcWeight := int64(count * HTLCWeight)
46✔
849
        baseWeight := int64(BaseCommitmentTxWeight)
46✔
850
        witnessWeight := int64(WitnessCommitmentTxWeight)
46✔
851

46✔
852
        // TODO(roasbeef): need taproot modifier? also no anchor so wrong?
46✔
853

46✔
854
        return htlcWeight + baseWeight + witnessWeight
46✔
855
}
856

857
// TxWeightEstimator is able to calculate weight estimates for transactions
858
// based on the input and output types. For purposes of estimation, all
859
// signatures are assumed to be of the maximum possible size, 73 bytes. Each
860
// method of the estimator returns an instance with the estimate applied. This
861
// allows callers to chain each of the methods
862
type TxWeightEstimator struct {
863
        hasWitness       bool
864
        inputCount       uint32
865
        outputCount      uint32
866
        inputSize        lntypes.VByte
867
        inputWitnessSize lntypes.WeightUnit
868
        outputSize       lntypes.VByte
869
}
870

871
// AddP2PKHInput updates the weight estimate to account for an additional input
872
// spending a P2PKH output.
873
func (twe *TxWeightEstimator) AddP2PKHInput() *TxWeightEstimator {
357✔
874
        twe.inputSize += InputSize + P2PKHScriptSigSize
357✔
875
        twe.inputWitnessSize++
357✔
876
        twe.inputCount++
357✔
877

357✔
878
        return twe
357✔
879
}
357✔
880

881
// AddP2WKHInput updates the weight estimate to account for an additional input
882
// spending a native P2PWKH output.
883
func (twe *TxWeightEstimator) AddP2WKHInput() *TxWeightEstimator {
619✔
884
        twe.AddWitnessInput(P2WKHWitnessSize)
619✔
885

619✔
886
        return twe
619✔
887
}
619✔
888

889
// AddWitnessInput updates the weight estimate to account for an additional
890
// input spending a native pay-to-witness output. This accepts the total size
891
// of the witness as a parameter.
892
func (twe *TxWeightEstimator) AddWitnessInput(
893
        witnessSize lntypes.WeightUnit) *TxWeightEstimator {
2,134✔
894

2,134✔
895
        twe.inputSize += InputSize
2,134✔
896
        twe.inputWitnessSize += witnessSize
2,134✔
897
        twe.inputCount++
2,134✔
898
        twe.hasWitness = true
2,134✔
899

2,134✔
900
        return twe
2,134✔
901
}
2,134✔
902

903
// AddTapscriptInput updates the weight estimate to account for an additional
904
// input spending a segwit v1 pay-to-taproot output using the script path. This
905
// accepts the total size of the witness for the script leaf that is executed
906
// and adds the size of the control block to the total witness size.
907
//
908
// NOTE: The leaf witness size must be calculated without the byte that accounts
909
// for the number of witness elements, only the total size of all elements on
910
// the stack that are consumed by the revealed script should be counted.
911
func (twe *TxWeightEstimator) AddTapscriptInput(
912
        leafWitnessSize lntypes.WeightUnit,
913
        tapscript *waddrmgr.Tapscript) *TxWeightEstimator {
×
914

×
915
        // We add 1 byte for the total number of witness elements.
×
916
        controlBlockWitnessSize := 1 + TaprootBaseControlBlockWitnessSize +
×
917
                // 1 byte for the length of the element plus the element itself.
×
918
                1 + len(tapscript.RevealedScript) +
×
919
                1 + len(tapscript.ControlBlock.InclusionProof)
×
920

×
921
        twe.inputSize += InputSize
×
922
        twe.inputWitnessSize += leafWitnessSize + lntypes.WeightUnit(
×
923
                controlBlockWitnessSize,
×
924
        )
×
925
        twe.inputCount++
×
926
        twe.hasWitness = true
×
927

×
928
        return twe
×
929
}
×
930

931
// AddTaprootKeySpendInput updates the weight estimate to account for an
932
// additional input spending a segwit v1 pay-to-taproot output using the key
933
// spend path. This accepts the sighash type being used since that has an
934
// influence on the total size of the signature.
935
func (twe *TxWeightEstimator) AddTaprootKeySpendInput(
936
        hashType txscript.SigHashType) *TxWeightEstimator {
23✔
937

23✔
938
        twe.inputSize += InputSize
23✔
939

23✔
940
        if hashType == txscript.SigHashDefault {
45✔
941
                twe.inputWitnessSize += TaprootKeyPathWitnessSize
22✔
942
        } else {
23✔
943
                twe.inputWitnessSize += TaprootKeyPathCustomSighashWitnessSize
1✔
944
        }
1✔
945

946
        twe.inputCount++
23✔
947
        twe.hasWitness = true
23✔
948

23✔
949
        return twe
23✔
950
}
951

952
// AddNestedP2WKHInput updates the weight estimate to account for an additional
953
// input spending a P2SH output with a nested P2WKH redeem script.
954
func (twe *TxWeightEstimator) AddNestedP2WKHInput() *TxWeightEstimator {
359✔
955
        twe.inputSize += InputSize + NestedP2WPKHSize
359✔
956
        twe.inputWitnessSize += P2WKHWitnessSize
359✔
957
        twe.inputCount++
359✔
958
        twe.hasWitness = true
359✔
959

359✔
960
        return twe
359✔
961
}
359✔
962

963
// AddNestedP2WSHInput updates the weight estimate to account for an additional
964
// input spending a P2SH output with a nested P2WSH redeem script.
965
func (twe *TxWeightEstimator) AddNestedP2WSHInput(
966
        witnessSize lntypes.WeightUnit) *TxWeightEstimator {
358✔
967

358✔
968
        twe.inputSize += InputSize + NestedP2WSHSize
358✔
969
        twe.inputWitnessSize += witnessSize
358✔
970
        twe.inputCount++
358✔
971
        twe.hasWitness = true
358✔
972

358✔
973
        return twe
358✔
974
}
358✔
975

976
// AddTxOutput adds a known TxOut to the weight estimator.
977
func (twe *TxWeightEstimator) AddTxOutput(txOut *wire.TxOut) *TxWeightEstimator {
33✔
978
        twe.outputSize += lntypes.VByte(txOut.SerializeSize())
33✔
979
        twe.outputCount++
33✔
980

33✔
981
        return twe
33✔
982
}
33✔
983

984
// AddP2PKHOutput updates the weight estimate to account for an additional P2PKH
985
// output.
986
func (twe *TxWeightEstimator) AddP2PKHOutput() *TxWeightEstimator {
383✔
987
        twe.outputSize += P2PKHOutputSize
383✔
988
        twe.outputCount++
383✔
989

383✔
990
        return twe
383✔
991
}
383✔
992

993
// AddP2WKHOutput updates the weight estimate to account for an additional
994
// native P2WKH output.
995
func (twe *TxWeightEstimator) AddP2WKHOutput() *TxWeightEstimator {
450✔
996
        twe.outputSize += P2WKHOutputSize
450✔
997
        twe.outputCount++
450✔
998

450✔
999
        return twe
450✔
1000
}
450✔
1001

1002
// AddP2WSHOutput updates the weight estimate to account for an additional
1003
// native P2WSH output.
1004
func (twe *TxWeightEstimator) AddP2WSHOutput() *TxWeightEstimator {
567✔
1005
        twe.outputSize += P2WSHOutputSize
567✔
1006
        twe.outputCount++
567✔
1007

567✔
1008
        return twe
567✔
1009
}
567✔
1010

1011
// AddP2TROutput updates the weight estimate to account for an additional native
1012
// SegWit v1 P2TR output.
1013
func (twe *TxWeightEstimator) AddP2TROutput() *TxWeightEstimator {
733✔
1014
        twe.outputSize += P2TROutputSize
733✔
1015
        twe.outputCount++
733✔
1016

733✔
1017
        return twe
733✔
1018
}
733✔
1019

1020
// AddP2SHOutput updates the weight estimate to account for an additional P2SH
1021
// output.
1022
func (twe *TxWeightEstimator) AddP2SHOutput() *TxWeightEstimator {
384✔
1023
        twe.outputSize += P2SHOutputSize
384✔
1024
        twe.outputCount++
384✔
1025

384✔
1026
        return twe
384✔
1027
}
384✔
1028

1029
// AddOutput estimates the weight of an output based on the pkScript.
1030
func (twe *TxWeightEstimator) AddOutput(pkScript []byte) *TxWeightEstimator {
×
1031
        twe.outputSize += BaseOutputSize + lntypes.VByte(len(pkScript))
×
1032
        twe.outputCount++
×
1033

×
1034
        return twe
×
1035
}
×
1036

1037
// Weight gets the estimated weight of the transaction.
1038
func (twe *TxWeightEstimator) Weight() lntypes.WeightUnit {
1,011✔
1039
        inputCount := wire.VarIntSerializeSize(uint64(twe.inputCount))
1,011✔
1040
        outputCount := wire.VarIntSerializeSize(uint64(twe.outputCount))
1,011✔
1041
        txSizeStripped := BaseTxSize + lntypes.VByte(inputCount) +
1,011✔
1042
                twe.inputSize + lntypes.VByte(outputCount) + twe.outputSize
1,011✔
1043
        weight := lntypes.WeightUnit(txSizeStripped * witnessScaleFactor)
1,011✔
1044

1,011✔
1045
        if twe.hasWitness {
1,978✔
1046
                weight += WitnessHeaderSize + twe.inputWitnessSize
967✔
1047
        }
967✔
1048
        return weight
1,011✔
1049
}
1050

1051
// VSize gets the estimated virtual size of the transactions, in vbytes.
1052
func (twe *TxWeightEstimator) VSize() int {
×
1053
        // A tx's vsize is 1/4 of the weight, rounded up.
×
1054
        return int(twe.Weight().ToVB())
×
1055
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc