Skip to content

Commit 3412c5d

Browse files
committed
commit_tx & htlc_tx: use amount_sat/amount_msat.
Signed-off-by: Rusty Russell <[email protected]>
1 parent bb00dee commit 3412c5d

9 files changed

+162
-145
lines changed

channeld/commit_tx.c

+32-30
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
#endif
1313

1414
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,
1617
enum side side)
1718
{
18-
u64 htlc_fee;
19+
struct amount_sat htlc_fee, htlc_min;
1920

2021
/* BOLT #3:
2122
*
@@ -41,17 +42,21 @@ static bool trim(const struct htlc *htlc,
4142
else
4243
htlc_fee = htlc_success_fee(feerate_per_kw);
4344

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;
4549
}
4650

4751
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,
4954
enum side side)
5055
{
5156
size_t i, n;
5257

5358
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);
5560

5661
return n;
5762
}
@@ -91,25 +96,28 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
9196
struct bitcoin_tx *commit_tx(const tal_t *ctx,
9297
const struct bitcoin_txid *funding_txid,
9398
unsigned int funding_txout,
94-
u64 funding_satoshis,
99+
struct amount_sat funding,
95100
enum side funder,
96101
u16 to_self_delay,
97102
const struct keyset *keyset,
98103
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,
102107
const struct htlc **htlcs,
103108
const struct htlc ***htlcmap,
104109
u64 obscured_commitment_number,
105110
enum side side)
106111
{
107112
struct amount_sat base_fee;
113+
struct amount_msat total_pay;
108114
struct bitcoin_tx *tx;
109115
size_t i, n, untrimmed;
110116
u32 *cltvs;
111117

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));
113121

114122
/* BOLT #3:
115123
*
@@ -118,7 +126,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
118126
*/
119127
untrimmed = commit_tx_num_untrimmed(htlcs,
120128
feerate_per_kw,
121-
dust_limit_satoshis, side);
129+
dust_limit, side);
122130

123131
/* BOLT #3:
124132
*
@@ -135,28 +143,22 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
135143
* 3. Subtract this base fee from the funder (either `to_local` or
136144
* `to_remote`), with a floor of 0 (see [Fee Payment](#fee-payment)).
137145
*/
138-
struct amount_msat self_pay, other_pay;
139-
self_pay.millisatoshis = self_pay_msat;
140-
other_pay.millisatoshis = other_pay_msat;
141-
142146
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;
145147

146148
#ifdef PRINT_ACTUAL_FEE
147149
{
148150
u64 satoshis_out = 0;
149151
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,
151153
side))
152154
satoshis_out += htlcs[i]->msatoshi / 1000;
153155
}
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;
158160
SUPERVERBOSE("# actual commitment transaction fee = %"PRIu64"\n",
159-
funding_satoshis - satoshis_out);
161+
funding.satoshis - satoshis_out);
160162
}
161163
#endif
162164

@@ -182,7 +184,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
182184
for (i = 0; i < tal_count(htlcs); i++) {
183185
if (htlc_owner(htlcs[i]) != side)
184186
continue;
185-
if (trim(htlcs[i], feerate_per_kw, dust_limit_satoshis, side))
187+
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
186188
continue;
187189
add_offered_htlc_out(tx, n, htlcs[i], keyset);
188190
(*htlcmap)[n] = htlcs[i];
@@ -198,7 +200,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
198200
for (i = 0; i < tal_count(htlcs); i++) {
199201
if (htlc_owner(htlcs[i]) == side)
200202
continue;
201-
if (trim(htlcs[i], feerate_per_kw, dust_limit_satoshis, side))
203+
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
202204
continue;
203205
add_received_htlc_out(tx, n, htlcs[i], keyset);
204206
(*htlcmap)[n] = htlcs[i];
@@ -212,9 +214,9 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
212214
* `dust_limit_satoshis`, add a [`to_local`
213215
* output](#to_local-output).
214216
*/
215-
if (self_pay_msat / 1000 >= dust_limit_satoshis) {
217+
if (amount_msat_greater_eq_sat(self_pay, dust_limit)) {
216218
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;
218220
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
219221
(*htlcmap)[n] = NULL;
220222
/* 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,
231233
* `dust_limit_satoshis`, add a [`to_remote`
232234
* output](#to_remote-output).
233235
*/
234-
if (other_pay_msat / 1000 >= dust_limit_satoshis) {
236+
if (amount_msat_greater_eq_sat(other_pay, dust_limit)) {
235237
/* BOLT #3:
236238
*
237239
* #### `to_remote` Output
238240
*
239241
* This output sends funds to the other peer and thus is a simple
240242
* P2WPKH to `remotepubkey`.
241243
*/
242-
tx->output[n].amount = other_pay_msat / 1000;
244+
tx->output[n].amount = other_pay.millisatoshis / 1000;
243245
tx->output[n].script = scriptpubkey_p2wpkh(tx,
244246
&keyset->other_payment_key);
245247
(*htlcmap)[n] = NULL;
@@ -295,7 +297,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
295297
= (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
296298

297299
/* 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);
299301

300302
return tx;
301303
}

channeld/commit_tx.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@ struct keyset;
1212
* commit_tx_num_untrimmed: how many of these htlc outputs will commit tx have?
1313
* @htlcs: tal_arr of HTLCs
1414
* @feerate_per_kw: feerate to use
15-
* @dust_limit_satoshis: dust limit below which to trim outputs.
15+
* @dust_limit: dust limit below which to trim outputs.
1616
* @side: from which side's point of view
1717
*
1818
* We need @side because HTLC fees are different for offered and
1919
* received HTLCs.
2020
*/
2121
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
22-
u32 feerate_per_kw, u64 dust_limit_satoshis,
22+
u32 feerate_per_kw,
23+
struct amount_sat dust_limit,
2324
enum side side);
2425

2526
/**
2627
* commit_tx: create (unsigned) commitment tx to spend the funding tx output
2728
* @ctx: context to allocate transaction and @htlc_map from.
28-
* @funding_txid, @funding_out, @funding_satoshis: funding outpoint.
29+
* @funding_txid, @funding_out, @funding: funding outpoint.
2930
* @funder: is the LOCAL or REMOTE paying the fee?
3031
* @keyset: keys derived for this commit tx.
3132
* @feerate_per_kw: feerate to use
32-
* @dust_limit_satoshis: dust limit below which to trim outputs.
33-
* @self_pay_msat: amount to pay directly to self
34-
* @other_pay_msat: amount to pay directly to the other side
33+
* @dust_limit: dust limit below which to trim outputs.
34+
* @self_pay: amount to pay directly to self
35+
* @other_pay: amount to pay directly to the other side
3536
* @htlcs: tal_arr of htlcs committed by transaction (some may be trimmed)
3637
* @htlc_map: outputed map of outnum->HTLC (NULL for direct outputs).
3738
* @obscured_commitment_number: number to encode in commitment transaction
@@ -44,14 +45,14 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
4445
struct bitcoin_tx *commit_tx(const tal_t *ctx,
4546
const struct bitcoin_txid *funding_txid,
4647
unsigned int funding_txout,
47-
u64 funding_satoshis,
48+
struct amount_sat funding,
4849
enum side funder,
4950
u16 to_self_delay,
5051
const struct keyset *keyset,
5152
u32 feerate_per_kw,
52-
u64 dust_limit_satoshis,
53-
u64 self_pay_msat,
54-
u64 other_pay_msat,
53+
struct amount_sat dust_limit,
54+
struct amount_msat self_pay,
55+
struct amount_msat other_pay,
5556
const struct htlc **htlcs,
5657
const struct htlc ***htlcmap,
5758
u64 obscured_commitment_number,

channeld/full_channel.c

+16-14
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,15 @@ static void add_htlcs(struct bitcoin_tx ***txs,
211211
const struct htlc *htlc = htlcmap[i];
212212
struct bitcoin_tx *tx;
213213
u8 *wscript;
214+
struct amount_msat htlc_amount;
214215

215216
if (!htlc)
216217
continue;
217218

219+
htlc_amount.millisatoshis = htlc->msatoshi;
218220
if (htlc_owner(htlc) == side) {
219221
tx = htlc_timeout_tx(*txs, &txid, i,
220-
htlc->msatoshi,
222+
htlc_amount,
221223
htlc->expiry.locktime,
222224
channel->config[!side].to_self_delay,
223225
feerate_per_kw,
@@ -229,7 +231,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,
229231
&keyset->self_revocation_key);
230232
} else {
231233
tx = htlc_success_tx(*txs, &txid, i,
232-
htlc->msatoshi,
234+
htlc_amount,
233235
channel->config[!side].to_self_delay,
234236
feerate_per_kw,
235237
keyset);
@@ -274,14 +276,14 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
274276
txs = tal_arr(ctx, struct bitcoin_tx *, 1);
275277
txs[0] = commit_tx(ctx, &channel->funding_txid,
276278
channel->funding_txout,
277-
channel->funding.satoshis,
279+
channel->funding,
278280
channel->funder,
279281
channel->config[!side].to_self_delay,
280282
&keyset,
281283
channel->view[side].feerate_per_kw,
282-
channel->config[side].dust_limit.satoshis,
283-
channel->view[side].owed[side].millisatoshis,
284-
channel->view[side].owed[!side].millisatoshis,
284+
channel->config[side].dust_limit,
285+
channel->view[side].owed[side],
286+
channel->view[side].owed[!side],
285287
committed,
286288
htlcmap,
287289
commitment_number ^ channel->commitment_number_obscurer,
@@ -438,14 +440,14 @@ static enum channel_add_err add_htlc(struct channel *channel,
438440
*/
439441
if (channel->funder == htlc_owner(htlc)) {
440442
u32 feerate = view->feerate_per_kw;
441-
u64 dust = channel->config[recipient].dust_limit.satoshis;
443+
struct amount_sat dust_limit = channel->config[recipient].dust_limit;
442444
size_t untrimmed;
443445

444-
untrimmed = commit_tx_num_untrimmed(committed, feerate, dust,
446+
untrimmed = commit_tx_num_untrimmed(committed, feerate, dust_limit,
445447
recipient)
446-
+ commit_tx_num_untrimmed(adding, feerate, dust,
448+
+ commit_tx_num_untrimmed(adding, feerate, dust_limit,
447449
recipient)
448-
- commit_tx_num_untrimmed(removing, feerate, dust,
450+
- commit_tx_num_untrimmed(removing, feerate, dust_limit,
449451
recipient);
450452

451453
fee = commit_tx_base_fee(feerate, untrimmed);
@@ -793,17 +795,17 @@ u32 approx_max_feerate(const struct channel *channel)
793795
bool can_funder_afford_feerate(const struct channel *channel, u32 feerate_per_kw)
794796
{
795797
struct amount_sat needed, fee;
796-
u64 dust = channel->config[!channel->funder].dust_limit.satoshis;
798+
struct amount_sat dust_limit = channel->config[!channel->funder].dust_limit;
797799
size_t untrimmed;
798800
const struct htlc **committed, **adding, **removing;
799801
gather_htlcs(tmpctx, channel, !channel->funder,
800802
&committed, &removing, &adding);
801803

802-
untrimmed = commit_tx_num_untrimmed(committed, feerate_per_kw, dust,
804+
untrimmed = commit_tx_num_untrimmed(committed, feerate_per_kw, dust_limit,
803805
!channel->funder)
804-
+ commit_tx_num_untrimmed(adding, feerate_per_kw, dust,
806+
+ commit_tx_num_untrimmed(adding, feerate_per_kw, dust_limit,
805807
!channel->funder)
806-
- commit_tx_num_untrimmed(removing, feerate_per_kw, dust,
808+
- commit_tx_num_untrimmed(removing, feerate_per_kw, dust_limit,
807809
!channel->funder);
808810

809811
fee = commit_tx_base_fee(feerate_per_kw, untrimmed);

0 commit comments

Comments
 (0)