Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions common/coin_mvt.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static const char *mvt_tags[] = {
"leased",
"stealable",
"channel_proposed",
"splice",
};

const char *mvt_tag_str(enum mvt_tag tag)
Expand Down Expand Up @@ -177,19 +178,31 @@ struct chain_coin_mvt *new_onchaind_deposit(const tal_t *ctx,
}

struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx,
const struct channel_id *chan_id,
const struct bitcoin_txid *txid,
const struct bitcoin_outpoint *out,
u32 blockheight,
const struct amount_msat amount,
const struct amount_sat output_val,
u32 output_count)
u32 output_count,
bool is_splice)
{
return new_chain_coin_mvt(ctx, NULL, txid,
struct chain_coin_mvt *mvt;
enum mvt_tag *tags = new_tag_arr(NULL, CHANNEL_CLOSE);

if (is_splice)
tal_arr_expand(&tags, SPLICE);

mvt = new_chain_coin_mvt(ctx, NULL, txid,
out, NULL, blockheight,
take(new_tag_arr(NULL, CHANNEL_CLOSE)),
take(tags),
amount, false,
output_val,
output_count);
if (chan_id)
mvt->account_name = fmt_channel_id(mvt, chan_id);

return mvt;
}

struct chain_coin_mvt *new_coin_channel_open_proposed(const tal_t *ctx,
Expand Down
9 changes: 6 additions & 3 deletions common/coin_mvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum mvt_type {
CHANNEL_MVT = 1,
};

#define NUM_MVT_TAGS (CHANNEL_PROPOSED + 1)
#define NUM_MVT_TAGS (SPLICE + 1)
enum mvt_tag {
DEPOSIT = 0,
WITHDRAWAL = 1,
Expand All @@ -40,6 +40,7 @@ enum mvt_tag {
LEASED = 21,
STEALABLE = 22,
CHANNEL_PROPOSED = 23,
SPLICE = 24,
};

struct channel_coin_mvt {
Expand Down Expand Up @@ -181,13 +182,15 @@ struct chain_coin_mvt *new_onchaind_deposit(const tal_t *ctx,
NON_NULL_ARGS(2);

struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx,
const struct channel_id *chan_id,
const struct bitcoin_txid *txid,
const struct bitcoin_outpoint *out,
u32 blockheight,
const struct amount_msat amount,
const struct amount_sat output_val,
u32 output_count)
NON_NULL_ARGS(2, 3);
u32 output_count,
bool is_splice)
NON_NULL_ARGS(3, 4);

struct chain_coin_mvt *new_coin_channel_open_proposed(const tal_t *ctx,
const struct channel_id *chan_id,
Expand Down
49 changes: 44 additions & 5 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,27 @@ static void handle_update_inflight(struct lightningd *ld,
wallet_inflight_save(ld->wallet, inflight);
}

static void channel_record_splice(struct channel *channel,
struct amount_msat orig_our_msats,
struct amount_sat orig_funding_sats,
struct bitcoin_outpoint *funding,
u32 blockheight, struct bitcoin_txid *txid, const struct channel_inflight *inflight)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newlines

{
struct chain_coin_mvt *mvt;
u32 output_count;

output_count = inflight->funding_psbt->num_outputs;
mvt = new_coin_channel_close(tmpctx, &channel->cid,
txid,
funding,
blockheight,
orig_our_msats,
orig_funding_sats,
output_count,
/* is_splice = */true);
notify_chain_mvt(channel->peer->ld, mvt);
}

void channel_record_open(struct channel *channel, u32 blockheight, bool record_push)
{
struct chain_coin_mvt *mvt;
Expand Down Expand Up @@ -1039,7 +1060,9 @@ bool channel_on_channel_ready(struct channel *channel,

static void handle_peer_splice_locked(struct channel *channel, const u8 *msg)
{
struct amount_sat funding_sats;
struct amount_sat funding_sats, prev_funding_sats;
struct amount_msat prev_our_msats;
struct bitcoin_outpoint prev_funding_out;
s64 splice_amnt;
struct channel_inflight *inflight;
struct bitcoin_txid locked_txid;
Expand All @@ -1054,16 +1077,22 @@ static void handle_peer_splice_locked(struct channel *channel, const u8 *msg)
return;
}

channel->our_msat.millisatoshis += splice_amnt * 1000; /* Raw: splicing */
channel->msat_to_us_min.millisatoshis += splice_amnt * 1000; /* Raw: splicing */
channel->msat_to_us_max.millisatoshis += splice_amnt * 1000; /* Raw: splicing */

inflight = channel_inflight_find(channel, &locked_txid);
if(!inflight)
channel_internal_error(channel, "Unable to load inflight for"
" locked_txid %s",
fmt_bitcoin_txid(tmpctx, &locked_txid));

/* Stash prev funding data so we can log it after scid is updated
* (to get the blockheight) */
prev_our_msats = channel->our_msat;
prev_funding_sats = channel->funding_sats;
prev_funding_out = channel->funding;

channel->our_msat.millisatoshis += splice_amnt * 1000; /* Raw: splicing */
channel->msat_to_us_min.millisatoshis += splice_amnt * 1000; /* Raw: splicing */
channel->msat_to_us_max.millisatoshis += splice_amnt * 1000; /* Raw: splicing */

wallet_htlcsigs_confirm_inflight(channel->peer->ld->wallet, channel,
&inflight->funding->outpoint);

Expand All @@ -1085,6 +1114,16 @@ static void handle_peer_splice_locked(struct channel *channel, const u8 *msg)
/* That freed watchers in inflights: now watch funding tx */
channel_watch_funding(channel->peer->ld, channel);

/* Log that funding output has been spent */
channel_record_splice(channel,
prev_our_msats,
prev_funding_sats,
&prev_funding_out,
channel->scid ?
short_channel_id_blocknum(*channel->scid) : 0,
&locked_txid,
inflight);

/* Put the successful inflight back in as a memory-only object.
* peer_control's funding_spent function will pick this up and clean up
* our inflight.
Expand Down
5 changes: 3 additions & 2 deletions onchaind/onchaind.c
Original file line number Diff line number Diff line change
Expand Up @@ -3463,14 +3463,15 @@ int main(int argc, char *argv[])
FUNDING_OUTPUT, NULL, NULL, NULL);

/* Record funding output spent */
send_coin_mvt(take(new_coin_channel_close(NULL, &tx->txid,
send_coin_mvt(take(new_coin_channel_close(NULL, NULL, &tx->txid,
&funding, tx_blockheight,
our_msat,
funding_sats,
is_elements(chainparams) ?
/* Minus 1, fee output */
tal_count(tx->outputs) - 1 :
tal_count(tx->outputs))));
tal_count(tx->outputs),
/* is_splice? */ false)));

status_debug("Remote per-commit point: %s",
fmt_pubkey(tmpctx, &remote_per_commit_point));
Expand Down
4 changes: 3 additions & 1 deletion onchaind/test/run-grind_feerate-bug.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ void memleak_status_broken(void *unused UNNEEDED, const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "memleak_status_broken called!\n"); abort(); }
/* Generated stub for new_coin_channel_close */
struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx UNNEEDED,
const struct channel_id *chan_id UNNEEDED,
const struct bitcoin_txid *txid UNNEEDED,
const struct bitcoin_outpoint *out UNNEEDED,
u32 blockheight UNNEEDED,
const struct amount_msat amount UNNEEDED,
const struct amount_sat output_val UNNEEDED,
u32 output_count)
u32 output_count UNNEEDED,
bool is_splice)

{ fprintf(stderr, "new_coin_channel_close called!\n"); abort(); }
/* Generated stub for new_coin_external_deposit */
Expand Down
4 changes: 3 additions & 1 deletion onchaind/test/run-grind_feerate.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,14 @@ void memleak_status_broken(void *unused UNNEEDED, const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "memleak_status_broken called!\n"); abort(); }
/* Generated stub for new_coin_channel_close */
struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx UNNEEDED,
const struct channel_id *chan_id UNNEEDED,
const struct bitcoin_txid *txid UNNEEDED,
const struct bitcoin_outpoint *out UNNEEDED,
u32 blockheight UNNEEDED,
const struct amount_msat amount UNNEEDED,
const struct amount_sat output_val UNNEEDED,
u32 output_count)
u32 output_count UNNEEDED,
bool is_splice)

{ fprintf(stderr, "new_coin_channel_close called!\n"); abort(); }
/* Generated stub for new_coin_external_deposit */
Expand Down
3 changes: 3 additions & 0 deletions plugins/bkpr/bookkeeper.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ static bool new_missed_channel_account(struct command *cmd,
chain_ev->payment_id = NULL;
chain_ev->ignored = false;
chain_ev->stealable = false;
chain_ev->splice_close = false;
chain_ev->desc = NULL;

/* Update the account info too */
Expand Down Expand Up @@ -1458,9 +1459,11 @@ parse_and_log_chain_move(struct command *cmd,

e->ignored = false;
e->stealable = false;
e->splice_close = false;
for (size_t i = 0; i < tal_count(tags); i++) {
e->ignored |= tags[i] == IGNORED;
e->stealable |= tags[i] == STEALABLE;
e->splice_close |= tags[i] == SPLICE;
}

db_begin_transaction(db);
Expand Down
4 changes: 4 additions & 0 deletions plugins/bkpr/chain_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ struct chain_event {
* we'll need to watch it for longer */
bool stealable;

/* Is this chain event because of a splice
* confirmation? */
bool splice_close;

/* Is this a rebalance event? */
bool rebalance;

Expand Down
1 change: 1 addition & 0 deletions plugins/bkpr/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ static struct migration db_migrations[] = {
{SQL("ALTER TABLE chain_events ADD ev_desc TEXT DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE channel_events ADD ev_desc TEXT DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE channel_events ADD rebalance_id BIGINT DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE chain_events ADD spliced INTEGER DEFAULT 0;"), NULL},
{NULL, migration_remove_dupe_lease_fees}
};

Expand Down
23 changes: 20 additions & 3 deletions plugins/bkpr/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ static struct chain_event *stmt2chain_event(const tal_t *ctx, struct db_stmt *st
else
e->desc = NULL;

e->splice_close = db_col_int(stmt, "e.spliced") == 1;

return e;
}

Expand Down Expand Up @@ -162,6 +164,7 @@ struct chain_event **list_chain_events_timebox(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -204,6 +207,7 @@ struct chain_event **account_get_chain_events(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -239,6 +243,7 @@ static struct chain_event **find_txos_for_tx(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -548,7 +553,9 @@ struct account *find_close_account(const tal_t *ctx,
" ON e.account_id = a.id"
" WHERE "
" e.tag = ?"
" AND e.spending_txid = ?"));
" AND e.spending_txid = ?"
/* ignore splicing 'close' events */
" AND e.spliced = 0 "));

db_bind_text(stmt, mvt_tag_str(CHANNEL_CLOSE));
db_bind_txid(stmt, txid);
Expand Down Expand Up @@ -678,6 +685,7 @@ struct chain_event *find_chain_event_by_id(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -726,6 +734,7 @@ static struct chain_event *find_chain_event(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -755,6 +764,7 @@ static struct chain_event *find_chain_event(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -1263,6 +1273,9 @@ void maybe_update_account(struct db *db,
*acct->open_event_db_id = e->db_id;
break;
case CHANNEL_CLOSE:
/* Splices dont count as closes */
if (e->splice_close)
break;
updated = true;
acct->closed_event_db_id = tal(acct, u64);
*acct->closed_event_db_id = e->db_id;
Expand Down Expand Up @@ -1294,6 +1307,7 @@ void maybe_update_account(struct db *db,
case TO_MINER:
case LEASE_FEE:
case STEALABLE:
case SPLICE:
/* Ignored */
break;
}
Expand All @@ -1304,7 +1318,7 @@ void maybe_update_account(struct db *db,
acct->peer_id = tal_dup(acct, struct node_id, peer_id);
}

if (closed_count > 0) {
if (!e->splice_close && closed_count > 0) {
updated = true;
acct->closed_count = closed_count;
}
Expand Down Expand Up @@ -1423,6 +1437,7 @@ static struct chain_event **find_chain_events_bytxid(const tal_t *ctx, struct db
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON a.id = e.account_id"
Expand Down Expand Up @@ -2002,9 +2017,10 @@ bool log_chain_event(struct db *db,
", ignored"
", stealable"
", ev_desc"
", spliced"
")"
" VALUES "
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"));
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"));

db_bind_u64(stmt, acct->db_id);
if (e->origin_acct)
Expand Down Expand Up @@ -2037,6 +2053,7 @@ bool log_chain_event(struct db *db,
db_bind_text(stmt, e->desc);
else
db_bind_null(stmt);
db_bind_int(stmt, e->splice_close ? 1 : 0);
db_exec_prepared_v2(stmt);
e->db_id = db_last_insert_id_v2(stmt);
e->acct_db_id = acct->db_id;
Expand Down
Loading