2021-12-04 21:53:56 +10:30
|
|
|
#include "config.h"
|
2021-12-13 16:44:32 -06:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2021-09-16 14:30:42 +09:30
|
|
|
#include <lightningd/channel.h>
|
2020-03-18 18:35:59 -05:00
|
|
|
#include <lightningd/coin_mvts.h>
|
|
|
|
|
#include <lightningd/notification.h>
|
2021-12-13 16:44:32 -06:00
|
|
|
#include <lightningd/peer_control.h>
|
|
|
|
|
|
2020-03-18 18:35:59 -05:00
|
|
|
|
2020-04-14 14:01:18 -05:00
|
|
|
struct channel_coin_mvt *new_channel_mvt_invoice_hin(const tal_t *ctx,
|
2025-08-14 10:57:53 +09:30
|
|
|
const struct htlc_in *hin,
|
|
|
|
|
const struct channel *channel)
|
2020-04-14 14:01:18 -05:00
|
|
|
{
|
2025-08-14 10:57:55 +09:30
|
|
|
return new_channel_coin_mvt(ctx, channel, time_now().ts.tv_sec,
|
2025-08-14 10:57:53 +09:30
|
|
|
&hin->payment_hash, NULL, NULL,
|
2025-08-14 10:57:53 +09:30
|
|
|
COIN_CREDIT, hin->msat,
|
2025-08-14 10:57:54 +09:30
|
|
|
mk_mvt_tags(MVT_INVOICE),
|
2025-08-14 10:57:53 +09:30
|
|
|
AMOUNT_MSAT(0));
|
2020-04-14 14:01:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct channel_coin_mvt *new_channel_mvt_routed_hin(const tal_t *ctx,
|
2025-08-14 10:57:53 +09:30
|
|
|
const struct htlc_in *hin,
|
|
|
|
|
const struct channel *channel)
|
2020-04-14 14:01:18 -05:00
|
|
|
{
|
2021-12-07 10:05:29 -06:00
|
|
|
struct amount_msat fees_collected;
|
|
|
|
|
|
|
|
|
|
if (!hin->payload)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (!amount_msat_sub(&fees_collected, hin->msat,
|
|
|
|
|
hin->payload->amt_to_forward))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2025-08-14 10:57:55 +09:30
|
|
|
return new_channel_coin_mvt(ctx, channel, time_now().ts.tv_sec,
|
2025-08-14 10:57:53 +09:30
|
|
|
&hin->payment_hash, NULL, NULL,
|
2025-08-14 10:57:53 +09:30
|
|
|
COIN_CREDIT, hin->msat,
|
2025-08-14 10:57:54 +09:30
|
|
|
mk_mvt_tags(MVT_ROUTED),
|
2025-08-14 10:57:53 +09:30
|
|
|
fees_collected);
|
2020-04-14 14:01:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct channel_coin_mvt *new_channel_mvt_invoice_hout(const tal_t *ctx,
|
2025-08-14 10:57:53 +09:30
|
|
|
const struct htlc_out *hout,
|
|
|
|
|
const struct channel *channel)
|
2020-04-14 14:01:18 -05:00
|
|
|
{
|
2025-08-14 10:57:55 +09:30
|
|
|
return new_channel_coin_mvt(ctx, channel, time_now().ts.tv_sec,
|
2025-08-14 10:57:53 +09:30
|
|
|
&hout->payment_hash,
|
|
|
|
|
&hout->partid,
|
|
|
|
|
&hout->groupid,
|
2025-08-14 10:57:53 +09:30
|
|
|
COIN_DEBIT, hout->msat,
|
2025-08-14 10:57:54 +09:30
|
|
|
mk_mvt_tags(MVT_INVOICE),
|
2025-08-14 10:57:53 +09:30
|
|
|
hout->fees);
|
2020-04-14 14:01:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct channel_coin_mvt *new_channel_mvt_routed_hout(const tal_t *ctx,
|
2025-08-14 10:57:53 +09:30
|
|
|
const struct htlc_out *hout,
|
|
|
|
|
const struct channel *channel)
|
2020-04-14 14:01:18 -05:00
|
|
|
{
|
2025-08-14 10:57:55 +09:30
|
|
|
return new_channel_coin_mvt(ctx, channel, time_now().ts.tv_sec,
|
2025-08-14 10:57:53 +09:30
|
|
|
&hout->payment_hash, NULL, NULL,
|
2025-08-14 10:57:53 +09:30
|
|
|
COIN_DEBIT, hout->msat,
|
2025-08-14 10:57:54 +09:30
|
|
|
mk_mvt_tags(MVT_ROUTED),
|
2021-12-07 14:09:28 -06:00
|
|
|
hout->fees);
|
2020-04-14 14:01:18 -05:00
|
|
|
}
|
2021-12-13 16:44:32 -06:00
|
|
|
|
2022-09-13 09:53:46 -05:00
|
|
|
static bool report_chan_balance(const struct channel *chan)
|
|
|
|
|
{
|
2023-10-02 09:29:49 +10:30
|
|
|
switch (chan->state) {
|
|
|
|
|
case CHANNELD_AWAITING_LOCKIN:
|
|
|
|
|
case DUALOPEND_OPEN_INIT:
|
2023-10-30 19:38:11 -05:00
|
|
|
case DUALOPEND_OPEN_COMMIT_READY:
|
2023-10-02 09:29:51 +10:30
|
|
|
case DUALOPEND_OPEN_COMMITTED:
|
2023-10-02 09:29:49 +10:30
|
|
|
case DUALOPEND_AWAITING_LOCKIN:
|
|
|
|
|
case CLOSINGD_COMPLETE:
|
|
|
|
|
case AWAITING_UNILATERAL:
|
|
|
|
|
case ONCHAIN:
|
|
|
|
|
case CLOSED:
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
case CHANNELD_NORMAL:
|
|
|
|
|
case CHANNELD_AWAITING_SPLICE:
|
|
|
|
|
case CHANNELD_SHUTTING_DOWN:
|
|
|
|
|
case CLOSINGD_SIGEXCHANGE:
|
|
|
|
|
case FUNDING_SPEND_SEEN:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
abort();
|
2022-09-13 09:53:46 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-15 15:17:49 +09:30
|
|
|
void send_account_balance_snapshot(struct lightningd *ld)
|
2021-12-13 16:44:32 -06:00
|
|
|
{
|
|
|
|
|
struct balance_snapshot *snap = tal(NULL, struct balance_snapshot);
|
|
|
|
|
struct account_balance *bal;
|
|
|
|
|
struct utxo **utxos;
|
|
|
|
|
struct channel *chan;
|
|
|
|
|
struct peer *p;
|
2023-01-18 15:34:32 +10:30
|
|
|
struct peer_node_id_map_iter it;
|
2021-12-13 16:44:32 -06:00
|
|
|
|
2025-05-15 15:17:49 +09:30
|
|
|
snap->blockheight = get_block_height(ld->topology);
|
2021-12-13 16:44:32 -06:00
|
|
|
snap->timestamp = time_now().ts.tv_sec;
|
2024-07-18 10:55:55 +09:30
|
|
|
snap->node_id = &ld->our_nodeid;
|
2021-12-13 16:44:32 -06:00
|
|
|
|
|
|
|
|
/* Add the 'wallet' account balance */
|
|
|
|
|
snap->accts = tal_arr(snap, struct account_balance *, 1);
|
|
|
|
|
bal = tal(snap, struct account_balance);
|
2021-12-10 09:46:42 -06:00
|
|
|
bal->balance = AMOUNT_MSAT(0);
|
2021-12-13 16:44:32 -06:00
|
|
|
bal->acct_id = WALLET;
|
|
|
|
|
bal->bip173_name = chainparams->lightning_hrp;
|
|
|
|
|
|
2023-10-30 15:49:59 +10:30
|
|
|
utxos = wallet_get_unspent_utxos(NULL, ld->wallet);
|
|
|
|
|
for (size_t j = 0; j < tal_count(utxos); j++) {
|
|
|
|
|
/* Don't count unconfirmed utxos! */
|
|
|
|
|
if (!utxos[j]->spendheight && !utxos[j]->blockheight)
|
|
|
|
|
continue;
|
|
|
|
|
if (!amount_msat_add_sat(&bal->balance,
|
|
|
|
|
bal->balance, utxos[j]->amount))
|
|
|
|
|
fatal("Overflow adding node balance");
|
2021-12-13 16:44:32 -06:00
|
|
|
}
|
2023-10-30 15:49:59 +10:30
|
|
|
tal_free(utxos);
|
|
|
|
|
|
2021-12-13 16:44:32 -06:00
|
|
|
snap->accts[0] = bal;
|
|
|
|
|
|
|
|
|
|
/* Add channel balances */
|
2023-01-18 15:34:32 +10:30
|
|
|
for (p = peer_node_id_map_first(ld->peers, &it);
|
|
|
|
|
p;
|
|
|
|
|
p = peer_node_id_map_next(ld->peers, &it)) {
|
2021-12-13 16:44:32 -06:00
|
|
|
list_for_each(&p->channels, chan, list) {
|
2022-09-13 09:53:46 -05:00
|
|
|
if (report_chan_balance(chan)) {
|
2021-12-13 16:44:32 -06:00
|
|
|
bal = tal(snap, struct account_balance);
|
|
|
|
|
bal->bip173_name = chainparams->lightning_hrp;
|
2024-03-20 11:17:52 +10:30
|
|
|
bal->acct_id = fmt_channel_id(bal, &chan->cid);
|
2021-12-13 16:44:32 -06:00
|
|
|
bal->balance = chan->our_msat;
|
|
|
|
|
tal_arr_expand(&snap->accts, bal);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notify_balance_snapshot(ld, snap);
|
|
|
|
|
tal_free(snap);
|
|
|
|
|
}
|