common/coin_mvt: add struct mvt_account_id to separate channels from others.

This means we can keep a pointer to the channel directly, *or* a string.
This avoids gratuitous formatting (on creation) and lookups (later).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2025-08-14 10:57:54 +09:30
parent e372c2ec0b
commit 6fc0b1f3d1
13 changed files with 127 additions and 95 deletions

View File

@@ -960,13 +960,15 @@ 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)
u32 blockheight,
const struct bitcoin_txid *txid,
const struct channel_inflight *inflight)
{
struct chain_coin_mvt *mvt;
u32 output_count;
output_count = inflight->funding_psbt->num_outputs;
mvt = new_coin_channel_close(tmpctx, &channel->cid,
mvt = new_coin_channel_close(tmpctx, channel, NULL,
txid,
funding,
blockheight,
@@ -1002,7 +1004,7 @@ void channel_record_open(struct channel *channel, u32 blockheight, bool record_p
/* If it's not in a block yet, send a proposal */
if (blockheight > 0)
mvt = new_coin_channel_open(tmpctx,
&channel->cid,
channel,
&channel->funding,
&channel->peer->id,
blockheight,
@@ -1012,7 +1014,7 @@ void channel_record_open(struct channel *channel, u32 blockheight, bool record_p
is_leased);
else
mvt = new_coin_channel_open_proposed(tmpctx,
&channel->cid,
channel,
&channel->funding,
&channel->peer->id,
start_balance,
@@ -1025,7 +1027,7 @@ void channel_record_open(struct channel *channel, u32 blockheight, bool record_p
/* If we pushed sats, *now* record them */
if (is_pushed && record_push)
notify_channel_mvt(channel->peer->ld,
new_coin_channel_push(tmpctx, &channel->cid,
new_coin_channel_push(tmpctx, channel,
channel->opener == REMOTE ? COIN_CREDIT : COIN_DEBIT,
channel->push,
is_leased ? LEASE_FEE : PUSHED));

View File

@@ -10,7 +10,7 @@ struct channel_coin_mvt *new_channel_mvt_invoice_hin(const tal_t *ctx,
const struct htlc_in *hin,
const struct channel *channel)
{
return new_channel_coin_mvt(ctx, &channel->cid,
return new_channel_coin_mvt(ctx, channel,
&hin->payment_hash, NULL, NULL,
COIN_CREDIT, hin->msat,
new_tag_arr(ctx, INVOICE),
@@ -30,7 +30,7 @@ struct channel_coin_mvt *new_channel_mvt_routed_hin(const tal_t *ctx,
hin->payload->amt_to_forward))
return NULL;
return new_channel_coin_mvt(ctx, &channel->cid,
return new_channel_coin_mvt(ctx, channel,
&hin->payment_hash, NULL, NULL,
COIN_CREDIT, hin->msat,
new_tag_arr(ctx, ROUTED),
@@ -41,7 +41,7 @@ struct channel_coin_mvt *new_channel_mvt_invoice_hout(const tal_t *ctx,
const struct htlc_out *hout,
const struct channel *channel)
{
return new_channel_coin_mvt(ctx, &channel->cid,
return new_channel_coin_mvt(ctx, channel,
&hout->payment_hash,
&hout->partid,
&hout->groupid,
@@ -54,7 +54,7 @@ struct channel_coin_mvt *new_channel_mvt_routed_hout(const tal_t *ctx,
const struct htlc_out *hout,
const struct channel *channel)
{
return new_channel_coin_mvt(ctx, &channel->cid,
return new_channel_coin_mvt(ctx, channel,
&hout->payment_hash, NULL, NULL,
COIN_DEBIT, hout->msat,
new_tag_arr(ctx, ROUTED),

View File

@@ -445,6 +445,16 @@ void notify_sendpay_failure(struct lightningd *ld,
notify_send(ld, n);
}
static void json_add_mvt_account_id(struct json_stream *stream,
const char *fieldname,
const struct mvt_account_id *account_id)
{
if (account_id->channel)
json_add_channel_id(stream, fieldname, &account_id->channel->cid);
else
json_add_string(stream, fieldname, account_id->alt_account);
}
static void chain_movement_notification_serialize(struct json_stream *stream,
struct lightningd *ld,
const struct chain_coin_mvt *chain_mvt)
@@ -454,10 +464,11 @@ static void chain_movement_notification_serialize(struct json_stream *stream,
json_add_node_id(stream, "node_id", &ld->our_nodeid);
if (chain_mvt->peer_id)
json_add_node_id(stream, "peer_id", chain_mvt->peer_id);
json_add_string(stream, "account_id", chain_mvt->account_name);
json_add_mvt_account_id(stream, "account_id", &chain_mvt->account);
if (chain_mvt->originating_acct)
json_add_string(stream, "originating_account",
chain_mvt->originating_acct);
json_add_mvt_account_id(stream, "originating_account", chain_mvt->originating_acct);
/* some 'journal entries' don't have a txid */
if (chain_mvt->tx_txid)
json_add_string(stream, "txid",
@@ -501,7 +512,7 @@ static void channel_movement_notification_serialize(struct json_stream *stream,
json_add_num(stream, "version", COIN_MVT_VERSION);
json_add_string(stream, "type", "channel_mvt");
json_add_node_id(stream, "node_id", &ld->our_nodeid);
json_add_channel_id(stream, "account_id", &chan_mvt->chan_id);
json_add_mvt_account_id(stream, "account_id", &chan_mvt->account);
/* push funding / leases don't have a payment_hash */
if (chan_mvt->payment_hash)
json_add_sha256(stream, "payment_hash", chan_mvt->payment_hash);

View File

@@ -346,13 +346,14 @@ static void handle_onchain_log_coin_move(struct channel *channel, const u8 *msg)
return;
}
/* Any 'ignored' payments get registered to the wallet */
if (!mvt->account_name)
mvt->account_name = fmt_channel_id(mvt,
&channel->cid);
else
mvt->originating_acct = fmt_channel_id(mvt,
&channel->cid);
/* onchaind uses an empty string to mean "this channel" */
if (streq(mvt->account.alt_account, "")) {
tal_free(mvt->account.alt_account);
set_mvt_account_id(&mvt->account, channel, NULL);
} else {
mvt->originating_acct = new_mvt_account_id(mvt, channel, NULL);
}
notify_chain_mvt(channel->peer->ld, mvt);
tal_free(mvt);
}
@@ -578,8 +579,7 @@ static void onchain_add_utxo(struct channel *channel, const u8 *msg)
mvt = new_coin_wallet_deposit(msg, &outpoint, blockheight,
amount, DEPOSIT);
mvt->originating_acct = fmt_channel_id(mvt,
&channel->cid);
mvt->originating_acct = new_mvt_account_id(mvt, channel, NULL);
notify_chain_mvt(channel->peer->ld, mvt);
}