Skip to content

Commit 24a1c91

Browse files
niftyneirustyrussell
authored andcommittedMar 5, 2022
coin_mvt: report the number of outputs on a channel close tx
The bookkeeper needs to know how many outputs to expect before we can consider a channel resolved onchain.
1 parent 12cbf61 commit 24a1c91

File tree

7 files changed

+37
-10
lines changed

7 files changed

+37
-10
lines changed
 

‎common/coin_mvt.c

+15-6
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ static struct chain_coin_mvt *new_chain_coin_mvt(const tal_t *ctx,
9191
enum mvt_tag *tags TAKES,
9292
struct amount_msat amount,
9393
bool is_credit,
94-
struct amount_sat output_val)
94+
struct amount_sat output_val,
95+
u32 out_count)
9596
{
9697
struct chain_coin_mvt *mvt = tal(ctx, struct chain_coin_mvt);
9798

@@ -118,7 +119,9 @@ static struct chain_coin_mvt *new_chain_coin_mvt(const tal_t *ctx,
118119
mvt->debit = amount;
119120
mvt->credit = AMOUNT_MSAT(0);
120121
}
122+
121123
mvt->output_val = output_val;
124+
mvt->output_count = out_count;
122125

123126
return mvt;
124127
}
@@ -143,7 +146,7 @@ static struct chain_coin_mvt *new_chain_coin_mvt_sat(const tal_t *ctx,
143146
blockheight, tags, amt_msat, is_credit,
144147
/* All amounts that are sat are
145148
* on-chain output values */
146-
amt_sat);
149+
amt_sat, 0);
147150
}
148151

149152
struct chain_coin_mvt *new_onchaind_withdraw(const tal_t *ctx,
@@ -178,13 +181,15 @@ struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx,
178181
const struct bitcoin_outpoint *out,
179182
u32 blockheight,
180183
const struct amount_msat amount,
181-
const struct amount_sat output_val)
184+
const struct amount_sat output_val,
185+
u32 output_count)
182186
{
183187
return new_chain_coin_mvt(ctx, NULL, txid,
184188
out, NULL, blockheight,
185189
take(new_tag_arr(NULL, CHANNEL_CLOSE)),
186190
amount, false,
187-
output_val);
191+
output_val,
192+
output_count);
188193
}
189194

190195
struct chain_coin_mvt *new_coin_channel_open(const tal_t *ctx,
@@ -200,7 +205,7 @@ struct chain_coin_mvt *new_coin_channel_open(const tal_t *ctx,
200205

201206
mvt = new_chain_coin_mvt(ctx, NULL, NULL, out, NULL, blockheight,
202207
take(new_tag_arr(NULL, CHANNEL_OPEN)), amount,
203-
true, output_val);
208+
true, output_val, 0);
204209
mvt->account_name = type_to_string(mvt, struct channel_id, chan_id);
205210

206211
/* If we're the opener, add to the tag list */
@@ -252,7 +257,7 @@ struct chain_coin_mvt *new_coin_external_spend(const tal_t *ctx,
252257
return new_chain_coin_mvt(ctx, EXTERNAL, txid,
253258
outpoint, NULL, blockheight,
254259
take(new_tag_arr(NULL, tag)),
255-
AMOUNT_MSAT(0), true, amount);
260+
AMOUNT_MSAT(0), true, amount, 0);
256261
}
257262

258263
struct chain_coin_mvt *new_coin_external_deposit(const tal_t *ctx,
@@ -334,6 +339,7 @@ struct coin_mvt *finalize_chain_mvt(const tal_t *ctx,
334339

335340
mvt->output_val = tal(mvt, struct amount_sat);
336341
*mvt->output_val = chain_mvt->output_val;
342+
mvt->output_count = chain_mvt->output_count;
337343
mvt->fees = NULL;
338344

339345
mvt->timestamp = timestamp;
@@ -366,6 +372,7 @@ struct coin_mvt *finalize_channel_mvt(const tal_t *ctx,
366372
mvt->credit = chan_mvt->credit;
367373
mvt->debit = chan_mvt->debit;
368374
mvt->output_val = NULL;
375+
mvt->output_count = 0;
369376
mvt->fees = tal(mvt, struct amount_msat);
370377
*mvt->fees = chan_mvt->fees;
371378
mvt->timestamp = timestamp;
@@ -413,6 +420,7 @@ void towire_chain_coin_mvt(u8 **pptr, const struct chain_coin_mvt *mvt)
413420
towire_amount_msat(pptr, mvt->credit);
414421
towire_amount_msat(pptr, mvt->debit);
415422
towire_amount_sat(pptr, mvt->output_val);
423+
towire_u32(pptr, mvt->output_count);
416424
}
417425

418426
void fromwire_chain_coin_mvt(const u8 **cursor, size_t *max, struct chain_coin_mvt *mvt)
@@ -455,4 +463,5 @@ void fromwire_chain_coin_mvt(const u8 **cursor, size_t *max, struct chain_coin_m
455463
mvt->credit = fromwire_amount_msat(cursor, max);
456464
mvt->debit = fromwire_amount_msat(cursor, max);
457465
mvt->output_val = fromwire_amount_sat(cursor, max);
466+
mvt->output_count = fromwire_u32(cursor, max);
458467
}

‎common/coin_mvt.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ struct chain_coin_mvt {
8787
/* When we pay to external accounts, it's useful
8888
* to track which internal account it originated from */
8989
const char *originating_acct;
90+
91+
/* Number of outputs in spending tx; used by the
92+
* `channel_close` event */
93+
u32 output_count;
9094
};
9195

9296
/* differs depending on type!? */
@@ -123,6 +127,8 @@ struct coin_mvt {
123127
/* Value of the output. May be different than
124128
* our credit/debit amount, eg channel opens */
125129
struct amount_sat *output_val;
130+
/* Really only needed for channel closes */
131+
size_t output_count;
126132

127133
/* Amount of fees collected/paid by channel mvt */
128134
struct amount_msat *fees;
@@ -170,7 +176,8 @@ struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx,
170176
const struct bitcoin_outpoint *out,
171177
u32 blockheight,
172178
const struct amount_msat amount,
173-
const struct amount_sat output_val)
179+
const struct amount_sat output_val,
180+
u32 output_count)
174181
NON_NULL_ARGS(2, 3);
175182

176183
struct chain_coin_mvt *new_coin_channel_open(const tal_t *ctx,

‎doc/PLUGINS.md

+4
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ i.e. only definitively resolved HTLCs or confirmed bitcoin transactions.
710710
"credit":"2000000000msat",
711711
"debit":"0msat",
712712
"output_value": "2000000000msat", // ('chain_mvt' only)
713+
"output_count": 2, // ('chain_mvt' only, typically only channel closes)
713714
"fees": "382msat", // ('channel_mvt' only)
714715
"tags": ["deposit"],
715716
"blockheight":102, // (May be null)
@@ -757,6 +758,9 @@ multiple times. `channel_mvt` only
757758
channel opens/closes the total output value will not necessarily correspond
758759
to the amount that's credited/debited.
759760

761+
`output_count` is the total outputs to expect for a channel close. Useful
762+
for figuring out when every onchain output for a close has been resolved.
763+
760764
`fees` is an HTLC annotation for the amount of fees either paid or
761765
earned. For "invoice" tagged events, the fees are the total fees
762766
paid to send that payment. The end amount can be found by subtracting

‎lightningd/notification.c

+4
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,10 @@ static void coin_movement_notification_serialize(struct json_stream *stream,
480480
if (mvt->output_val)
481481
json_add_amount_sat_only(stream, "output_value",
482482
*mvt->output_val);
483+
if (mvt->output_count > 0)
484+
json_add_num(stream, "output_count",
485+
mvt->output_count);
486+
483487
if (mvt->fees)
484488
json_add_amount_msat_only(stream, "fees",
485489
*mvt->fees);

‎onchaind/onchaind.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -3860,7 +3860,8 @@ int main(int argc, char *argv[])
38603860
send_coin_mvt(take(new_coin_channel_close(NULL, &tx->txid,
38613861
&funding, tx_blockheight,
38623862
our_msat,
3863-
funding_sats)));
3863+
funding_sats,
3864+
tal_count(tx->outputs))));
38643865

38653866
status_debug("Remote per-commit point: %s",
38663867
type_to_string(tmpctx, struct pubkey,

‎onchaind/test/run-grind_feerate-bug.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx UNNEEDED,
119119
const struct bitcoin_outpoint *out UNNEEDED,
120120
u32 blockheight UNNEEDED,
121121
const struct amount_msat amount UNNEEDED,
122-
const struct amount_sat output_val)
122+
const struct amount_sat output_val UNNEEDED,
123+
u32 output_count)
123124

124125
{ fprintf(stderr, "new_coin_channel_close called!\n"); abort(); }
125126
/* Generated stub for new_coin_external_deposit */

‎onchaind/test/run-grind_feerate.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx UNNEEDED,
142142
const struct bitcoin_outpoint *out UNNEEDED,
143143
u32 blockheight UNNEEDED,
144144
const struct amount_msat amount UNNEEDED,
145-
const struct amount_sat output_val)
145+
const struct amount_sat output_val UNNEEDED,
146+
u32 output_count)
146147

147148
{ fprintf(stderr, "new_coin_channel_close called!\n"); abort(); }
148149
/* Generated stub for new_coin_external_deposit */

0 commit comments

Comments
 (0)