Skip to content

Commit 22b452a

Browse files
committed
lightningd: have onchaind inform us when to make a channel penalty_adj.
bookkeeper used to generate these as channel events, now lightningd does. We also add a "journal" event, which we will need later too. Signed-off-by: Rusty Russell <[email protected]>
1 parent b1fa2ef commit 22b452a

File tree

12 files changed

+66
-57
lines changed

12 files changed

+66
-57
lines changed

common/coin_mvt.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ static const char *mvt_tags[] = {
3333
"stealable",
3434
"channel_proposed",
3535
"splice",
36+
"penalty_adj",
37+
"journal",
3638
};
3739

3840
#define PRIMARY_TAG_BITS ((1ULL << MVT_DEPOSIT) | \
@@ -54,6 +56,8 @@ static const char *mvt_tags[] = {
5456
(1ULL << MVT_STOLEN) | \
5557
(1ULL << MVT_TO_MINER) | \
5658
(1ULL << MVT_LEASE_FEE) | \
59+
(1ULL << MVT_PENALTY_ADJ) | \
60+
(1ULL << MVT_JOURNAL) | \
5761
(1ULL << MVT_CHANNEL_PROPOSED))
5862

5963
const char *mvt_tag_str(enum mvt_tag tag)

common/coin_mvt.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ enum mvt_tag {
3636
MVT_STEALABLE = 21,
3737
MVT_CHANNEL_PROPOSED = 22,
3838
MVT_SPLICE = 23,
39-
#define NUM_MVT_TAGS (MVT_SPLICE + 1)
39+
MVT_PENALTY_ADJ = 24,
40+
MVT_JOURNAL = 25,
41+
#define NUM_MVT_TAGS (MVT_JOURNAL + 1)
4042
};
4143

4244
struct mvt_tags {

common/test/run-coin_mvt.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ static bool mvt_tag_is_primary(enum mvt_tag tag)
177177
return true;
178178
case MVT_SPLICE:
179179
return false;
180+
case MVT_PENALTY_ADJ:
181+
return true;
182+
case MVT_JOURNAL:
183+
return true;
180184
}
181185
abort();
182186
}

lightningd/coin_mvts.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ struct channel_coin_mvt *new_channel_mvt_routed_hout(const tal_t *ctx,
6161
hout->fees);
6262
}
6363

64+
struct channel_coin_mvt *new_channel_mvt_penalty_adj(const tal_t *ctx,
65+
const struct channel *channel,
66+
struct amount_msat amount,
67+
enum coin_mvt_dir direction)
68+
{
69+
return new_channel_coin_mvt(ctx, channel, time_now().ts.tv_sec,
70+
NULL, NULL, NULL,
71+
direction, amount,
72+
mk_mvt_tags(MVT_PENALTY_ADJ),
73+
AMOUNT_MSAT(0));
74+
}
75+
6476
static bool report_chan_balance(const struct channel *chan)
6577
{
6678
switch (chan->state) {

lightningd/coin_mvts.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ struct channel_coin_mvt *new_channel_mvt_invoice_hout(const tal_t *ctx,
3232
struct channel_coin_mvt *new_channel_mvt_routed_hout(const tal_t *ctx,
3333
const struct htlc_out *hout,
3434
const struct channel *channel);
35+
struct channel_coin_mvt *new_channel_mvt_penalty_adj(const tal_t *ctx,
36+
const struct channel *channel,
37+
struct amount_msat amount,
38+
enum coin_mvt_dir direction);
3539

3640
void send_account_balance_snapshot(struct lightningd *ld);
3741

lightningd/onchain_control.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,22 @@ static void handle_onchain_log_coin_move(struct channel *channel, const u8 *msg)
358358
tal_free(mvt);
359359
}
360360

361+
static void handle_onchain_log_penalty_adj(struct channel *channel, const u8 *msg)
362+
{
363+
struct amount_msat msat;
364+
struct channel_coin_mvt *mvt;
365+
366+
if (!fromwire_onchaind_notify_penalty_adj(msg, &msat)) {
367+
channel_internal_error(channel, "Invalid onchain notify_penalty_adj");
368+
return;
369+
}
370+
371+
mvt = new_channel_mvt_penalty_adj(tmpctx, channel, msat, COIN_CREDIT);
372+
notify_channel_mvt(channel->peer->ld, mvt);
373+
mvt = new_channel_mvt_penalty_adj(tmpctx, channel, msat, COIN_DEBIT);
374+
notify_channel_mvt(channel->peer->ld, mvt);
375+
}
376+
361377
static void replay_watch_tx(struct channel *channel,
362378
u32 blockheight,
363379
const struct bitcoin_tx *tx TAKES)
@@ -1651,6 +1667,10 @@ static unsigned int onchain_msg(struct subd *sd, const u8 *msg, const int *fds U
16511667
handle_onchain_log_coin_move(sd->channel, msg);
16521668
break;
16531669

1670+
case WIRE_ONCHAIND_NOTIFY_PENALTY_ADJ:
1671+
handle_onchain_log_penalty_adj(sd->channel, msg);
1672+
break;
1673+
16541674
case WIRE_ONCHAIND_SPEND_TO_US:
16551675
handle_onchaind_spend_to_us(sd->channel, msg);
16561676
break;

onchaind/onchaind.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,15 @@ static void record_coin_movements(struct tracked_output *out,
378378
else
379379
record_channel_withdrawal(txid, out, blockheight, mk_mvt_tags(MVT_TO_WALLET));
380380
}
381+
382+
/* Tell lightningd to create penalty_adj on channel balance */
383+
if (out->resolved->tx_type == OUR_PENALTY_TX) {
384+
struct amount_msat msat;
385+
if (!amount_sat_to_msat(&msat, out->sat))
386+
abort();
387+
wire_sync_write(REQ_FD,
388+
take(towire_onchaind_notify_penalty_adj(NULL, msat)));
389+
}
381390
}
382391

383392
/* We vary feerate until signature they offered matches. */
@@ -1660,6 +1669,7 @@ static void wait_for_resolved(struct tracked_output **outs)
16601669
case WIRE_ONCHAIND_ANNOTATE_TXOUT:
16611670
case WIRE_ONCHAIND_ANNOTATE_TXIN:
16621671
case WIRE_ONCHAIND_NOTIFY_COIN_MVT:
1672+
case WIRE_ONCHAIND_NOTIFY_PENALTY_ADJ:
16631673
case WIRE_ONCHAIND_SPEND_TO_US:
16641674
case WIRE_ONCHAIND_SPEND_PENALTY:
16651675
case WIRE_ONCHAIND_SPEND_HTLC_SUCCESS:

onchaind/onchaind_wire.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ msgdata,onchaind_annotate_txin,type,enum wallet_tx_type,
125125
msgtype,onchaind_notify_coin_mvt,5037
126126
msgdata,onchaind_notify_coin_mvt,mvt,chain_coin_mvt,
127127

128+
msgtype,onchaind_notify_penalty_adj,5038
129+
msgdata,onchaind_notify_penalty_adj,amount,amount_msat,
130+
128131
# We tell lightningd to create, sign and broadcast this tx:
129132
msgtype,onchaind_spend_to_us,5040
130133
msgdata,onchaind_spend_to_us,outpoint,bitcoin_outpoint,

onchaind/test/run-grind_feerate.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ u8 *towire_onchaind_missing_htlc_output(const tal_t *ctx UNNEEDED, const struct
292292
/* Generated stub for towire_onchaind_notify_coin_mvt */
293293
u8 *towire_onchaind_notify_coin_mvt(const tal_t *ctx UNNEEDED, const struct chain_coin_mvt *mvt UNNEEDED)
294294
{ fprintf(stderr, "towire_onchaind_notify_coin_mvt called!\n"); abort(); }
295+
/* Generated stub for towire_onchaind_notify_penalty_adj */
296+
u8 *towire_onchaind_notify_penalty_adj(const tal_t *ctx UNNEEDED, struct amount_msat amount UNNEEDED)
297+
{ fprintf(stderr, "towire_onchaind_notify_penalty_adj called!\n"); abort(); }
295298
/* Generated stub for towire_onchaind_spend_fulfill */
296299
u8 *towire_onchaind_spend_fulfill(const tal_t *ctx UNNEEDED, const struct bitcoin_outpoint *outpoint UNNEEDED, struct amount_sat outpoint_amount UNNEEDED, u64 htlc_id UNNEEDED, const struct pubkey *remote_per_commitment_point UNNEEDED, const struct preimage *preimage UNNEEDED, const u8 *wscript UNNEEDED)
297300
{ fprintf(stderr, "towire_onchaind_spend_fulfill called!\n"); abort(); }

plugins/bkpr/recorder.c

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,8 @@ void maybe_update_account(struct db *db,
14071407
case MVT_LEASE_FEE:
14081408
case MVT_STEALABLE:
14091409
case MVT_SPLICE:
1410+
case MVT_PENALTY_ADJ:
1411+
case MVT_JOURNAL:
14101412
/* Ignored */
14111413
break;
14121414
}
@@ -1686,36 +1688,7 @@ char *update_channel_onchain_fees(const tal_t *ctx,
16861688
ev->tag);
16871689
}
16881690

1689-
/* Was this an 'old state' tx, where we ended up
1690-
* with more sats than we had on record? */
1691-
if (amount_msat_greater(onchain_amt, close_ev->debit)) {
1692-
struct channel_event *ev;
1693-
struct amount_msat diff;
1694-
1695-
if (!amount_msat_sub(&diff, onchain_amt,
1696-
close_ev->debit))
1697-
return tal_fmt(ctx, "Unable to sub"
1698-
"close debit from onchain_amt");
1699-
/* Add in/out journal entries for it */
1700-
ev = new_channel_event(ctx,
1701-
tal_fmt(tmpctx, "%s",
1702-
account_entry_tag_str(PENALTY_ADJ)),
1703-
diff,
1704-
AMOUNT_MSAT(0),
1705-
AMOUNT_MSAT(0),
1706-
NULL, 0,
1707-
close_ev->timestamp);
1708-
log_channel_event(db, acct, ev);
1709-
ev = new_channel_event(ctx,
1710-
tal_fmt(tmpctx, "%s",
1711-
account_entry_tag_str(PENALTY_ADJ)),
1712-
AMOUNT_MSAT(0),
1713-
diff,
1714-
AMOUNT_MSAT(0),
1715-
NULL, 0,
1716-
close_ev->timestamp);
1717-
log_channel_event(db, acct, ev);
1718-
} else {
1691+
if (amount_msat_less_eq(onchain_amt, close_ev->debit)) {
17191692
struct amount_msat fees;
17201693
if (!amount_msat_sub(&fees, close_ev->debit,
17211694
onchain_amt))

0 commit comments

Comments
 (0)