12
12
#endif
13
13
14
14
static bool trim (const struct htlc * htlc ,
15
- u32 feerate_per_kw , u64 dust_limit_satoshis ,
15
+ u32 feerate_per_kw ,
16
+ struct amount_sat dust_limit ,
16
17
enum side side )
17
18
{
18
- u64 htlc_fee ;
19
+ struct amount_sat htlc_fee , htlc_min ;
19
20
20
21
/* BOLT #3:
21
22
*
@@ -41,17 +42,21 @@ static bool trim(const struct htlc *htlc,
41
42
else
42
43
htlc_fee = htlc_success_fee (feerate_per_kw );
43
44
44
- return htlc -> msatoshi / 1000 < dust_limit_satoshis + htlc_fee ;
45
+ /* If these overflow, it implies htlc must be less. */
46
+ if (!amount_sat_add (& htlc_min , dust_limit , htlc_fee ))
47
+ return true;
48
+ return htlc -> msatoshi / 1000 < htlc_min .satoshis ;
45
49
}
46
50
47
51
size_t commit_tx_num_untrimmed (const struct htlc * * htlcs ,
48
- u32 feerate_per_kw , u64 dust_limit_satoshis ,
52
+ u32 feerate_per_kw ,
53
+ struct amount_sat dust_limit ,
49
54
enum side side )
50
55
{
51
56
size_t i , n ;
52
57
53
58
for (i = n = 0 ; i < tal_count (htlcs ); i ++ )
54
- n += !trim (htlcs [i ], feerate_per_kw , dust_limit_satoshis , side );
59
+ n += !trim (htlcs [i ], feerate_per_kw , dust_limit , side );
55
60
56
61
return n ;
57
62
}
@@ -91,25 +96,28 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
91
96
struct bitcoin_tx * commit_tx (const tal_t * ctx ,
92
97
const struct bitcoin_txid * funding_txid ,
93
98
unsigned int funding_txout ,
94
- u64 funding_satoshis ,
99
+ struct amount_sat funding ,
95
100
enum side funder ,
96
101
u16 to_self_delay ,
97
102
const struct keyset * keyset ,
98
103
u32 feerate_per_kw ,
99
- u64 dust_limit_satoshis ,
100
- u64 self_pay_msat ,
101
- u64 other_pay_msat ,
104
+ struct amount_sat dust_limit ,
105
+ struct amount_msat self_pay ,
106
+ struct amount_msat other_pay ,
102
107
const struct htlc * * htlcs ,
103
108
const struct htlc * * * htlcmap ,
104
109
u64 obscured_commitment_number ,
105
110
enum side side )
106
111
{
107
112
struct amount_sat base_fee ;
113
+ struct amount_msat total_pay ;
108
114
struct bitcoin_tx * tx ;
109
115
size_t i , n , untrimmed ;
110
116
u32 * cltvs ;
111
117
112
- assert (self_pay_msat + other_pay_msat <= funding_satoshis * 1000 );
118
+ if (!amount_msat_add (& total_pay , self_pay , other_pay ))
119
+ abort ();
120
+ assert (!amount_msat_greater_sat (total_pay , funding ));
113
121
114
122
/* BOLT #3:
115
123
*
@@ -118,7 +126,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
118
126
*/
119
127
untrimmed = commit_tx_num_untrimmed (htlcs ,
120
128
feerate_per_kw ,
121
- dust_limit_satoshis , side );
129
+ dust_limit , side );
122
130
123
131
/* BOLT #3:
124
132
*
@@ -135,28 +143,22 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
135
143
* 3. Subtract this base fee from the funder (either `to_local` or
136
144
* `to_remote`), with a floor of 0 (see [Fee Payment](#fee-payment)).
137
145
*/
138
- struct amount_msat self_pay , other_pay ;
139
- self_pay .millisatoshis = self_pay_msat ;
140
- other_pay .millisatoshis = other_pay_msat ;
141
-
142
146
try_subtract_fee (funder , side , base_fee , & self_pay , & other_pay );
143
- self_pay_msat = self_pay .millisatoshis ;
144
- other_pay_msat = other_pay .millisatoshis ;
145
147
146
148
#ifdef PRINT_ACTUAL_FEE
147
149
{
148
150
u64 satoshis_out = 0 ;
149
151
for (i = 0 ; i < tal_count (htlcs ); i ++ ) {
150
- if (!trim (htlcs [i ], feerate_per_kw , dust_limit_satoshis ,
152
+ if (!trim (htlcs [i ], feerate_per_kw , dust_limit ,
151
153
side ))
152
154
satoshis_out += htlcs [i ]-> msatoshi / 1000 ;
153
155
}
154
- if (self_pay_msat / 1000 >= dust_limit_satoshis )
155
- satoshis_out += self_pay_msat / 1000 ;
156
- if (other_pay_msat / 1000 >= dust_limit_satoshis )
157
- satoshis_out += other_pay_msat / 1000 ;
156
+ if (amount_msat_greater_sat ( self_pay , dust_limit ) )
157
+ satoshis_out += self_pay . millisatoshis / 1000 ;
158
+ if (amount_msat_greater_sat ( other_pay , dust_limit ) )
159
+ satoshis_out += other_pay . millisatoshis / 1000 ;
158
160
SUPERVERBOSE ("# actual commitment transaction fee = %" PRIu64 "\n" ,
159
- funding_satoshis - satoshis_out );
161
+ funding . satoshis - satoshis_out );
160
162
}
161
163
#endif
162
164
@@ -182,7 +184,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
182
184
for (i = 0 ; i < tal_count (htlcs ); i ++ ) {
183
185
if (htlc_owner (htlcs [i ]) != side )
184
186
continue ;
185
- if (trim (htlcs [i ], feerate_per_kw , dust_limit_satoshis , side ))
187
+ if (trim (htlcs [i ], feerate_per_kw , dust_limit , side ))
186
188
continue ;
187
189
add_offered_htlc_out (tx , n , htlcs [i ], keyset );
188
190
(* htlcmap )[n ] = htlcs [i ];
@@ -198,7 +200,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
198
200
for (i = 0 ; i < tal_count (htlcs ); i ++ ) {
199
201
if (htlc_owner (htlcs [i ]) == side )
200
202
continue ;
201
- if (trim (htlcs [i ], feerate_per_kw , dust_limit_satoshis , side ))
203
+ if (trim (htlcs [i ], feerate_per_kw , dust_limit , side ))
202
204
continue ;
203
205
add_received_htlc_out (tx , n , htlcs [i ], keyset );
204
206
(* htlcmap )[n ] = htlcs [i ];
@@ -212,9 +214,9 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
212
214
* `dust_limit_satoshis`, add a [`to_local`
213
215
* output](#to_local-output).
214
216
*/
215
- if (self_pay_msat / 1000 >= dust_limit_satoshis ) {
217
+ if (amount_msat_greater_eq_sat ( self_pay , dust_limit ) ) {
216
218
u8 * wscript = to_self_wscript (tmpctx , to_self_delay ,keyset );
217
- tx -> output [n ].amount = self_pay_msat / 1000 ;
219
+ tx -> output [n ].amount = self_pay . millisatoshis / 1000 ;
218
220
tx -> output [n ].script = scriptpubkey_p2wsh (tx , wscript );
219
221
(* htlcmap )[n ] = NULL ;
220
222
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
@@ -231,15 +233,15 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
231
233
* `dust_limit_satoshis`, add a [`to_remote`
232
234
* output](#to_remote-output).
233
235
*/
234
- if (other_pay_msat / 1000 >= dust_limit_satoshis ) {
236
+ if (amount_msat_greater_eq_sat ( other_pay , dust_limit ) ) {
235
237
/* BOLT #3:
236
238
*
237
239
* #### `to_remote` Output
238
240
*
239
241
* This output sends funds to the other peer and thus is a simple
240
242
* P2WPKH to `remotepubkey`.
241
243
*/
242
- tx -> output [n ].amount = other_pay_msat / 1000 ;
244
+ tx -> output [n ].amount = other_pay . millisatoshis / 1000 ;
243
245
tx -> output [n ].script = scriptpubkey_p2wpkh (tx ,
244
246
& keyset -> other_payment_key );
245
247
(* htlcmap )[n ] = NULL ;
@@ -295,7 +297,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
295
297
= (0x80000000 | ((obscured_commitment_number >>24 ) & 0xFFFFFF ));
296
298
297
299
/* Input amount needed for signature code. */
298
- tx -> input [0 ].amount = tal_dup (tx -> input , u64 , & funding_satoshis );
300
+ tx -> input [0 ].amount = tal_dup (tx -> input , u64 , & funding . satoshis );
299
301
300
302
return tx ;
301
303
}
0 commit comments