2022-07-19 14:39:26 +09:30
|
|
|
#ifndef LIGHTNING_PLUGINS_BKPR_RECORDER_H
|
|
|
|
|
#define LIGHTNING_PLUGINS_BKPR_RECORDER_H
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#include <ccan/tal/tal.h>
|
|
|
|
|
|
|
|
|
|
struct account;
|
2022-07-19 15:04:40 +09:30
|
|
|
struct bitcoin_txid;
|
2022-07-19 14:39:26 +09:30
|
|
|
struct chain_event;
|
|
|
|
|
struct channel_event;
|
2025-08-14 10:57:56 +09:30
|
|
|
struct plugin;
|
2022-07-19 14:39:26 +09:30
|
|
|
enum mvt_tag;
|
|
|
|
|
struct onchain_fee;
|
|
|
|
|
|
2022-07-19 17:04:37 +09:30
|
|
|
#define SQLITE_MAX_UINT 0x7FFFFFFFFFFFFFFF
|
2022-07-19 17:04:35 +09:30
|
|
|
|
2022-07-19 17:04:36 +09:30
|
|
|
struct fee_sum {
|
2025-08-19 10:30:47 +09:30
|
|
|
const char *acct_name;
|
2022-07-19 17:04:36 +09:30
|
|
|
struct bitcoin_txid *txid;
|
|
|
|
|
struct amount_msat fees_paid;
|
2025-10-21 14:58:57 +10:30
|
|
|
u64 last_timestamp;
|
2022-07-19 17:04:36 +09:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct txo_pair {
|
|
|
|
|
struct chain_event *txo;
|
|
|
|
|
struct chain_event *spend;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct txo_set {
|
|
|
|
|
struct bitcoin_txid *txid;
|
|
|
|
|
struct txo_pair **pairs;
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-19 14:39:26 +09:30
|
|
|
/* Get all channel events for this account */
|
|
|
|
|
struct channel_event **account_get_channel_events(const tal_t *ctx,
|
2025-08-19 10:30:52 +09:30
|
|
|
const struct bkpr *bkpr,
|
|
|
|
|
struct command *cmd,
|
2022-07-19 14:39:26 +09:30
|
|
|
struct account *acct);
|
|
|
|
|
|
2024-08-06 14:56:27 +02:00
|
|
|
/* Get all channel events for a payment id, order by timestamp */
|
|
|
|
|
struct channel_event **get_channel_events_by_id(const tal_t *ctx,
|
2025-08-19 10:30:52 +09:30
|
|
|
const struct bkpr *bkpr,
|
|
|
|
|
struct command *cmd,
|
|
|
|
|
const struct sha256 *id);
|
2024-08-06 14:56:27 +02:00
|
|
|
|
2022-07-19 17:04:35 +09:30
|
|
|
/* Get all channel events, ordered by timestamp */
|
2025-08-19 10:30:52 +09:30
|
|
|
struct channel_event **list_channel_events(const tal_t *ctx,
|
|
|
|
|
const struct bkpr *bkpr,
|
|
|
|
|
struct command *cmd);
|
2022-07-19 17:04:35 +09:30
|
|
|
|
2022-07-19 17:04:37 +09:30
|
|
|
/* Get all channel events, order by timestamp.
|
|
|
|
|
*
|
|
|
|
|
* @ctx - context to allocate from
|
|
|
|
|
* @start_time - UNIX timestamp to query after (exclusive)
|
|
|
|
|
* @end_time - UNIX timestamp to query until (inclusive)
|
|
|
|
|
*/
|
|
|
|
|
struct channel_event **list_channel_events_timebox(const tal_t *ctx,
|
2025-08-19 10:30:52 +09:30
|
|
|
const struct bkpr *bkpr,
|
|
|
|
|
struct command *cmd,
|
2022-07-19 17:04:37 +09:30
|
|
|
u64 start_time,
|
|
|
|
|
u64 end_time);
|
|
|
|
|
|
2022-07-19 14:39:26 +09:30
|
|
|
/* Get all chain events for this account */
|
|
|
|
|
struct chain_event **account_get_chain_events(const tal_t *ctx,
|
2025-08-19 10:30:50 +09:30
|
|
|
const struct bkpr *bkpr,
|
2025-08-19 10:30:52 +09:30
|
|
|
struct command *cmd,
|
2022-07-19 14:39:26 +09:30
|
|
|
struct account *acct);
|
2022-07-19 17:04:35 +09:30
|
|
|
|
2024-08-06 14:56:27 +02:00
|
|
|
/* Get all chain events for a transaction id, order by timestamp */
|
2025-08-19 10:30:50 +09:30
|
|
|
struct chain_event **find_chain_events_bytxid(const tal_t *ctx,
|
|
|
|
|
const struct bkpr *bkpr,
|
2025-08-19 10:30:52 +09:30
|
|
|
struct command *cmd,
|
2025-08-19 10:30:50 +09:30
|
|
|
const struct bitcoin_txid *txid);
|
2024-08-06 14:56:27 +02:00
|
|
|
|
2022-07-19 17:04:37 +09:30
|
|
|
/* Get all chain events, order by timestamp. */
|
2025-08-19 10:30:52 +09:30
|
|
|
struct chain_event **list_chain_events(const tal_t *ctx,
|
|
|
|
|
const struct bkpr *bkpr,
|
|
|
|
|
struct command *cmd);
|
2022-07-19 17:04:35 +09:30
|
|
|
|
2022-07-19 17:04:37 +09:30
|
|
|
/* Get all chain events, order by timestamp.
|
|
|
|
|
*
|
|
|
|
|
* @ctx - context to allocate from
|
|
|
|
|
* @start_time - UNIX timestamp to query after (exclusive)
|
|
|
|
|
* @end_time - UNIX timestamp to query until (inclusive)
|
|
|
|
|
*/
|
|
|
|
|
struct chain_event **list_chain_events_timebox(const tal_t *ctx,
|
2025-08-19 10:30:50 +09:30
|
|
|
const struct bkpr *bkpr,
|
2025-08-19 10:30:52 +09:30
|
|
|
struct command *cmd,
|
2022-07-19 17:04:37 +09:30
|
|
|
u64 start_time,
|
|
|
|
|
u64 end_time);
|
|
|
|
|
|
2024-08-22 17:16:48 -05:00
|
|
|
/* Get all chain events for a payment hash */
|
|
|
|
|
struct chain_event **get_chain_events_by_id(const tal_t *ctx,
|
2025-08-19 10:30:52 +09:30
|
|
|
const struct bkpr *bkpr,
|
|
|
|
|
struct command *cmd,
|
|
|
|
|
const struct sha256 *id);
|
2024-08-22 17:16:48 -05:00
|
|
|
|
2024-08-22 18:00:22 -05:00
|
|
|
/* Get all chain events for a utxo */
|
|
|
|
|
struct chain_event **get_chain_events_by_outpoint(const tal_t *ctx,
|
2025-08-19 10:30:50 +09:30
|
|
|
const struct bkpr *bkpr,
|
2025-08-19 10:30:52 +09:30
|
|
|
struct command *cmd,
|
|
|
|
|
const struct bitcoin_outpoint *outpoint);
|
2024-08-22 18:00:22 -05:00
|
|
|
|
2025-08-14 10:57:56 +09:30
|
|
|
/* Get total credits and debits for this account: returns false if no entries at all
|
|
|
|
|
* (in which case, credit and debit will both be AMOUNT_MSAT(0)). */
|
2025-08-19 10:30:52 +09:30
|
|
|
bool account_get_credit_debit(const struct bkpr *bkpr,
|
|
|
|
|
struct command *cmd,
|
2025-08-14 10:57:56 +09:30
|
|
|
const char *acct_name,
|
|
|
|
|
struct amount_msat *credit,
|
|
|
|
|
struct amount_msat *debit);
|
|
|
|
|
|
2022-07-19 17:04:35 +09:30
|
|
|
|
2022-07-19 17:04:36 +09:30
|
|
|
/* Find a chain event by its database id */
|
|
|
|
|
struct chain_event *find_chain_event_by_id(const tal_t *ctx,
|
2025-08-19 10:30:50 +09:30
|
|
|
const struct bkpr *bkpr,
|
2025-08-19 10:30:52 +09:30
|
|
|
struct command *cmd,
|
2022-07-19 17:04:36 +09:30
|
|
|
u64 event_db_id);
|
|
|
|
|
|
2022-07-19 17:04:36 +09:30
|
|
|
/* Find the utxos for this account.
|
|
|
|
|
*
|
|
|
|
|
* Returns true if chain is complete:
|
|
|
|
|
* (all outputs terminate either to wallet or external)
|
|
|
|
|
*/
|
|
|
|
|
bool find_txo_chain(const tal_t *ctx,
|
2025-08-19 10:30:50 +09:30
|
|
|
const struct bkpr *bkpr,
|
2025-08-19 10:30:52 +09:30
|
|
|
struct command *cmd,
|
2025-08-19 10:30:47 +09:30
|
|
|
const struct account *acct,
|
2022-07-19 17:04:36 +09:30
|
|
|
struct txo_set ***sets);
|
|
|
|
|
|
2022-07-19 17:04:36 +09:30
|
|
|
/* Find the account that was closed by this txid.
|
|
|
|
|
* Returns NULL if none */
|
2025-08-19 10:30:47 +09:30
|
|
|
const char *find_close_account_name(const tal_t *ctx,
|
2025-08-19 10:30:52 +09:30
|
|
|
const struct bkpr *bkpr,
|
|
|
|
|
struct command *cmd,
|
2025-08-19 10:30:47 +09:30
|
|
|
const struct bitcoin_txid *txid);
|
2022-07-19 14:39:26 +09:30
|
|
|
|
2022-07-19 17:04:36 +09:30
|
|
|
/* Have all the outputs for this account's close tx
|
2025-08-19 10:30:47 +09:30
|
|
|
* been resolved onchain? If so, return the
|
2022-07-19 17:04:36 +09:30
|
|
|
* highest blockheight that has a resolving tx in it.
|
|
|
|
|
*
|
|
|
|
|
* The point of this is to allow us to prune data, eventually */
|
2025-08-19 10:30:52 +09:30
|
|
|
u64 account_onchain_closeheight(const struct bkpr *bkpr,
|
|
|
|
|
struct command *cmd,
|
|
|
|
|
const struct account *acct);
|
2022-07-19 17:04:36 +09:30
|
|
|
|
2022-07-19 17:04:37 +09:30
|
|
|
/* When we make external deposits from the wallet, we don't
|
|
|
|
|
* count them until any output that was spent *into* them is
|
|
|
|
|
* confirmed onchain.
|
|
|
|
|
*
|
2025-08-19 10:30:50 +09:30
|
|
|
* This method updates bkpr->blockheights to show the
|
2022-07-19 17:04:37 +09:30
|
|
|
* height an input was spent into */
|
2025-08-19 10:30:50 +09:30
|
|
|
void maybe_closeout_external_deposits(struct command *cmd,
|
|
|
|
|
struct bkpr *bkpr,
|
2024-07-01 13:35:01 -05:00
|
|
|
const struct bitcoin_txid *txid,
|
|
|
|
|
u32 blockheight);
|
2022-07-19 17:04:37 +09:30
|
|
|
|
2025-08-19 10:30:49 +09:30
|
|
|
/* Keep track of rebalancing payments (payments paid to/from ourselves. */
|
|
|
|
|
void maybe_record_rebalance(struct command *cmd,
|
|
|
|
|
struct bkpr *bkpr,
|
|
|
|
|
const struct channel_event *out);
|
2022-07-19 14:39:26 +09:30
|
|
|
#endif /* LIGHTNING_PLUGINS_BKPR_RECORDER_H */
|