2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2021-12-13 23:44:32 +01:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <lightningd/channel.h>
|
2020-03-19 00:35:59 +01:00
|
|
|
#include <lightningd/coin_mvts.h>
|
|
|
|
#include <lightningd/notification.h>
|
2021-12-13 23:44:32 +01:00
|
|
|
#include <lightningd/peer_control.h>
|
|
|
|
|
2020-03-19 00:35:59 +01:00
|
|
|
|
|
|
|
void notify_channel_mvt(struct lightningd *ld, const struct channel_coin_mvt *mvt)
|
|
|
|
{
|
|
|
|
const struct coin_mvt *cm;
|
|
|
|
u32 timestamp;
|
|
|
|
|
|
|
|
timestamp = time_now().ts.tv_sec;
|
2021-11-29 08:10:06 +01:00
|
|
|
cm = finalize_channel_mvt(mvt, mvt, chainparams->lightning_hrp,
|
2024-07-18 03:25:55 +02:00
|
|
|
timestamp, &ld->our_nodeid);
|
2021-12-01 17:34:58 +01:00
|
|
|
|
2020-03-19 00:35:59 +01:00
|
|
|
notify_coin_mvt(ld, cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void notify_chain_mvt(struct lightningd *ld, const struct chain_coin_mvt *mvt)
|
|
|
|
{
|
|
|
|
const struct coin_mvt *cm;
|
|
|
|
u32 timestamp;
|
|
|
|
|
|
|
|
timestamp = time_now().ts.tv_sec;
|
2023-11-21 21:12:56 +01:00
|
|
|
cm = finalize_chain_mvt(mvt, mvt, chainparams->lightning_hrp,
|
2024-07-18 03:25:55 +02:00
|
|
|
timestamp, &ld->our_nodeid);
|
2020-03-19 00:35:59 +01:00
|
|
|
notify_coin_mvt(ld, cm);
|
|
|
|
}
|
2020-04-04 01:58:04 +02:00
|
|
|
|
2020-04-14 21:01:18 +02:00
|
|
|
struct channel_coin_mvt *new_channel_mvt_invoice_hin(const tal_t *ctx,
|
|
|
|
struct htlc_in *hin,
|
|
|
|
struct channel *channel)
|
|
|
|
{
|
2020-09-09 09:20:53 +02:00
|
|
|
return new_channel_coin_mvt(ctx, &channel->cid,
|
2021-12-29 01:09:56 +01:00
|
|
|
&hin->payment_hash, NULL,
|
2021-12-06 19:24:20 +01:00
|
|
|
hin->msat, new_tag_arr(ctx, INVOICE),
|
2021-12-07 17:05:29 +01:00
|
|
|
true, AMOUNT_MSAT(0));
|
2020-04-14 21:01:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct channel_coin_mvt *new_channel_mvt_routed_hin(const tal_t *ctx,
|
|
|
|
struct htlc_in *hin,
|
|
|
|
struct channel *channel)
|
|
|
|
{
|
2021-12-07 17:05:29 +01: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;
|
|
|
|
|
2020-09-09 09:20:53 +02:00
|
|
|
return new_channel_coin_mvt(ctx, &channel->cid,
|
2021-12-29 01:09:56 +01:00
|
|
|
&hin->payment_hash, NULL,
|
2021-12-06 19:24:20 +01:00
|
|
|
hin->msat, new_tag_arr(ctx, ROUTED),
|
2021-12-07 17:05:29 +01:00
|
|
|
true, fees_collected);
|
2020-04-14 21:01:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct channel_coin_mvt *new_channel_mvt_invoice_hout(const tal_t *ctx,
|
|
|
|
struct htlc_out *hout,
|
|
|
|
struct channel *channel)
|
|
|
|
{
|
2020-09-09 09:20:53 +02:00
|
|
|
return new_channel_coin_mvt(ctx, &channel->cid,
|
2021-12-29 01:09:56 +01:00
|
|
|
&hout->payment_hash, &hout->partid,
|
2021-12-06 19:24:20 +01:00
|
|
|
hout->msat, new_tag_arr(ctx, INVOICE),
|
2021-12-07 21:09:28 +01:00
|
|
|
false, hout->fees);
|
2020-04-14 21:01:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct channel_coin_mvt *new_channel_mvt_routed_hout(const tal_t *ctx,
|
|
|
|
struct htlc_out *hout,
|
|
|
|
struct channel *channel)
|
|
|
|
{
|
2020-09-09 09:20:53 +02:00
|
|
|
return new_channel_coin_mvt(ctx, &channel->cid,
|
2021-12-29 01:09:56 +01:00
|
|
|
&hout->payment_hash, NULL,
|
2021-12-06 19:24:20 +01:00
|
|
|
hout->msat, new_tag_arr(ctx, ROUTED),
|
2021-12-07 17:05:29 +01:00
|
|
|
false,
|
2021-12-07 21:09:28 +01:00
|
|
|
hout->fees);
|
2020-04-14 21:01:18 +02:00
|
|
|
}
|
2021-12-13 23:44:32 +01:00
|
|
|
|
2022-09-13 16:53:46 +02:00
|
|
|
static bool report_chan_balance(const struct channel *chan)
|
|
|
|
{
|
2023-10-02 00:59:49 +02:00
|
|
|
switch (chan->state) {
|
|
|
|
case CHANNELD_AWAITING_LOCKIN:
|
|
|
|
case DUALOPEND_OPEN_INIT:
|
2023-10-31 01:38:11 +01:00
|
|
|
case DUALOPEND_OPEN_COMMIT_READY:
|
2023-10-02 00:59:51 +02:00
|
|
|
case DUALOPEND_OPEN_COMMITTED:
|
2023-10-02 00:59:49 +02:00
|
|
|
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 16:53:46 +02:00
|
|
|
}
|
|
|
|
|
2021-12-13 23:44:32 +01:00
|
|
|
void send_account_balance_snapshot(struct lightningd *ld, u32 blockheight)
|
|
|
|
{
|
|
|
|
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 06:04:32 +01:00
|
|
|
struct peer_node_id_map_iter it;
|
2021-12-13 23:44:32 +01:00
|
|
|
|
|
|
|
snap->blockheight = blockheight;
|
|
|
|
snap->timestamp = time_now().ts.tv_sec;
|
2024-07-18 03:25:55 +02:00
|
|
|
snap->node_id = &ld->our_nodeid;
|
2021-12-13 23:44:32 +01:00
|
|
|
|
|
|
|
/* Add the 'wallet' account balance */
|
|
|
|
snap->accts = tal_arr(snap, struct account_balance *, 1);
|
|
|
|
bal = tal(snap, struct account_balance);
|
2021-12-10 16:46:42 +01:00
|
|
|
bal->balance = AMOUNT_MSAT(0);
|
2021-12-13 23:44:32 +01:00
|
|
|
bal->acct_id = WALLET;
|
|
|
|
bal->bip173_name = chainparams->lightning_hrp;
|
|
|
|
|
2023-10-30 06:19:59 +01:00
|
|
|
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 23:44:32 +01:00
|
|
|
}
|
2023-10-30 06:19:59 +01:00
|
|
|
tal_free(utxos);
|
|
|
|
|
2021-12-13 23:44:32 +01:00
|
|
|
snap->accts[0] = bal;
|
|
|
|
|
|
|
|
/* Add channel balances */
|
2023-01-18 06:04:32 +01:00
|
|
|
for (p = peer_node_id_map_first(ld->peers, &it);
|
|
|
|
p;
|
|
|
|
p = peer_node_id_map_next(ld->peers, &it)) {
|
2021-12-13 23:44:32 +01:00
|
|
|
list_for_each(&p->channels, chan, list) {
|
2022-09-13 16:53:46 +02:00
|
|
|
if (report_chan_balance(chan)) {
|
2021-12-13 23:44:32 +01:00
|
|
|
bal = tal(snap, struct account_balance);
|
|
|
|
bal->bip173_name = chainparams->lightning_hrp;
|
2024-03-20 01:47:52 +01:00
|
|
|
bal->acct_id = fmt_channel_id(bal, &chan->cid);
|
2021-12-13 23:44:32 +01:00
|
|
|
bal->balance = chan->our_msat;
|
|
|
|
tal_arr_expand(&snap->accts, bal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notify_balance_snapshot(ld, snap);
|
|
|
|
tal_free(snap);
|
|
|
|
}
|