Skip to content

Bookkeeper refactors and cleanups in preparation for migration #8410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
464 changes: 228 additions & 236 deletions common/coin_mvt.c

Large diffs are not rendered by default.

264 changes: 106 additions & 158 deletions common/coin_mvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,84 @@
#define COIN_MVT_VERSION 2
#define WALLET "wallet"

enum mvt_type {
CHAIN_MVT = 0,
CHANNEL_MVT = 1,
};

#define NUM_MVT_TAGS (SPLICE + 1)
enum mvt_tag {
DEPOSIT = 0,
WITHDRAWAL = 1,
PENALTY = 2,
INVOICE = 3,
ROUTED = 4,
PUSHED = 5,
CHANNEL_OPEN = 6,
CHANNEL_CLOSE = 7,
CHANNEL_TO_US = 8,
HTLC_TIMEOUT = 9,
HTLC_FULFILL = 10,
HTLC_TX = 11,
TO_WALLET = 12,
IGNORED = 13,
ANCHOR = 14,
TO_THEM = 15,
PENALIZED = 16,
STOLEN = 17,
TO_MINER = 18,
OPENER = 19,
LEASE_FEE = 20,
LEASED = 21,
STEALABLE = 22,
CHANNEL_PROPOSED = 23,
SPLICE = 24,
MVT_DEPOSIT = 0,
MVT_WITHDRAWAL = 1,
MVT_PENALTY = 2,
MVT_INVOICE = 3,
MVT_ROUTED = 4,
MVT_PUSHED = 5,
MVT_CHANNEL_OPEN = 6,
MVT_CHANNEL_CLOSE = 7,
MVT_CHANNEL_TO_US = 8,
MVT_HTLC_TIMEOUT = 9,
MVT_HTLC_FULFILL = 10,
MVT_HTLC_TX = 11,
MVT_TO_WALLET = 12,
MVT_IGNORED = 13,
MVT_ANCHOR = 14,
MVT_TO_THEM = 15,
MVT_PENALIZED = 16,
MVT_STOLEN = 17,
MVT_TO_MINER = 18,
MVT_OPENER = 19,
MVT_LEASE_FEE = 20,
MVT_LEASED = 21,
MVT_STEALABLE = 22,
MVT_CHANNEL_PROPOSED = 23,
MVT_SPLICE = 24,
#define NUM_MVT_TAGS (MVT_SPLICE + 1)
};

struct channel_coin_mvt {
/* account_id */
struct channel_id chan_id;
struct mvt_tags {
u64 bits;
};

/* identifier */
struct sha256 *payment_hash;
enum coin_mvt_dir {
COIN_CREDIT = 1,
COIN_DEBIT = 2,
};

/* mutli-part payments may share a payment hash,
* so we should also record a 'part-id' for them */
u64 *part_id;
struct channel_coin_mvt_id {
/* multi-part payments may share a payment hash,
* so we should also record part-id and group-id for them */
u64 part_id;
u64 group_id;
};

/* label / tag array */
enum mvt_tag *tags;
/* Only one of these is set. */
struct mvt_account_id {
const struct channel *channel;
const char *alt_account;
};

struct channel_coin_mvt {
/* Common fields */
struct mvt_account_id account;
struct mvt_tags tags;
/* only one or the other */
struct amount_msat credit;
struct amount_msat debit;

/* identifier */
const struct sha256 *payment_hash;

/* multi-part payments may share a payment hash,
* so we should also record part-id and group-id for them */
const struct channel_coin_mvt_id *part_and_group;

/* Fees collected (or paid) on this mvt */
struct amount_msat fees;
};

struct chain_coin_mvt {
/* account_id */
const char *account_name;
struct mvt_account_id account;
struct mvt_tags tags;
/* only one or the other */
struct amount_msat credit;
struct amount_msat debit;

const struct bitcoin_txid *tx_txid;
const struct bitcoin_outpoint *outpoint;

Expand All @@ -76,93 +95,53 @@ struct chain_coin_mvt {
const struct node_id *peer_id;

/* some on-chain movements have a payment hash */
struct sha256 *payment_hash;

/* label / tag array */
enum mvt_tag *tags;
const struct sha256 *payment_hash;

/* block this transaction is confirmed in */
u32 blockheight;

/* only one or the other */
struct amount_msat credit;
struct amount_msat debit;

/* total value of output (useful for tracking external outs) */
struct amount_sat output_val;

/* When we pay to external accounts, it's useful
* to track which internal account it originated from */
const char *originating_acct;
const struct mvt_account_id *originating_acct;

/* Number of outputs in spending tx; used by the
* `channel_close` event */
u32 output_count;
};

/* differs depending on type!? */
struct mvt_id {
struct sha256 *payment_hash;
u64 *part_id;
const struct bitcoin_txid *tx_txid;
const struct bitcoin_outpoint *outpoint;
};

struct coin_mvt {
/* name of 'account': wallet, external, <channel_id> */
const char *account_id;

/* Peer that this event occurred with */
const struct node_id *peer_id;

/* if account_id is external, the account this 'impacted' */
const char *originating_acct;

/* Chain name: BIP 173, except signet lightning-style: tbs not tb */
const char *hrp_name;

/* type of movement: channel or chain */
enum mvt_type type;

/* identifier */
struct mvt_id id;

/* label / tag array */
enum mvt_tag *tags;
/* Convenience macro for creating tag bitmaps */
#define mk_mvt_tags(...) mk_mvt_tags_(__VA_ARGS__, 999)
struct mvt_tags mk_mvt_tags_(enum mvt_tag tag, ...);

/* only one or the other */
struct amount_msat credit;
struct amount_msat debit;
static inline struct mvt_tags tag_to_mvt_tags(enum mvt_tag tag)
{
struct mvt_tags tags;
tags.bits = ((u64)1) << tag;
return tags;
}

/* Value of the output. May be different than
* our credit/debit amount, eg channel opens */
struct amount_sat *output_val;
/* Really only needed for channel closes */
size_t output_count;
/* Useful constructor for mvt_account_id: exactly one of channel/account_name must be NULL */
void set_mvt_account_id(struct mvt_account_id *acct_id,
const struct channel *channel,
const char *account_name TAKES);

/* Amount of fees collected/paid by channel mvt */
struct amount_msat *fees;

u32 timestamp;
u32 blockheight;

/* version is a counter of the format of the data payload that
* makes up a coin movement */
u8 version;

/* node originating this movement */
struct node_id *node_id;
};

enum mvt_tag *new_tag_arr(const tal_t *ctx, enum mvt_tag tag);
/* Allocating version */
struct mvt_account_id *new_mvt_account_id(const tal_t *ctx,
const struct channel *channel,
const char *account_name TAKES);

/* Either part_id and group_id both NULL, or neither are */
struct channel_coin_mvt *new_channel_coin_mvt(const tal_t *ctx,
const struct channel_id *cid,
const struct channel *channel,
const struct sha256 *payment_hash TAKES,
u64 *part_id TAKES,
const u64 *part_id,
const u64 *group_id,
enum coin_mvt_dir direction,
struct amount_msat amount,
const enum mvt_tag *tags TAKES,
bool is_credit,
struct mvt_tags tags,
struct amount_msat fees)
NON_NULL_ARGS(2);

Expand All @@ -171,47 +150,48 @@ struct chain_coin_mvt *new_onchaind_withdraw(const tal_t *ctx,
const struct bitcoin_txid *spend_txid,
u32 blockheight,
struct amount_sat amount,
enum mvt_tag tag)
struct mvt_tags tags)
NON_NULL_ARGS(2, 3);

struct chain_coin_mvt *new_onchaind_deposit(const tal_t *ctx,
const struct bitcoin_outpoint *outpoint,
u32 blockheight,
struct amount_sat amount,
enum mvt_tag tag)
struct mvt_tags tags)
NON_NULL_ARGS(2);

struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx,
const struct channel_id *chan_id,
const struct channel *channel,
const char *alt_account,
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,
bool is_splice)
NON_NULL_ARGS(3, 4);
NON_NULL_ARGS(4, 5);

struct chain_coin_mvt *new_coin_channel_open_proposed(const tal_t *ctx,
const struct channel_id *chan_id,
const struct channel *channel,
const struct bitcoin_outpoint *out,
const struct node_id *peer_id,
const struct amount_msat amount,
const struct amount_sat output_val,
bool is_opener,
bool is_leased)
NON_NULL_ARGS(2, 3);
NON_NULL_ARGS(2, 3, 4);

struct chain_coin_mvt *new_coin_channel_open(const tal_t *ctx,
const struct channel_id *chan_id,
const struct channel *channel,
const struct bitcoin_outpoint *out,
const struct node_id *peer_id,
u32 blockheight,
const struct amount_msat amount,
const struct amount_sat output_val,
bool is_opener,
bool is_leased)
NON_NULL_ARGS(2, 3);
NON_NULL_ARGS(2, 3, 4);

struct chain_coin_mvt *new_onchain_htlc_deposit(const tal_t *ctx,
const struct bitcoin_outpoint *outpoint,
Expand All @@ -231,80 +211,48 @@ struct chain_coin_mvt *new_coin_wallet_deposit(const tal_t *ctx,
const struct bitcoin_outpoint *outpoint,
u32 blockheight,
struct amount_sat amount,
enum mvt_tag tag)
struct mvt_tags tags)
NON_NULL_ARGS(2);

struct chain_coin_mvt *new_coin_wallet_deposit_tagged(const tal_t *ctx,
const struct bitcoin_outpoint *outpoint,
u32 blockheight,
struct amount_sat amount,
enum mvt_tag *tags TAKES)
NON_NULL_ARGS(2);

struct chain_coin_mvt *new_coin_wallet_withdraw(const tal_t *ctx,
const struct bitcoin_txid *spend_txid,
const struct bitcoin_outpoint *outpoint,
u32 blockheight,
struct amount_sat amount,
enum mvt_tag tag)
NON_NULL_ARGS(2, 3);

struct chain_coin_mvt *new_coin_external_spend_tags(const tal_t *ctx,
const struct bitcoin_outpoint *outpoint,
const struct bitcoin_txid *txid,
u32 blockheight,
struct amount_sat amount,
enum mvt_tag *tags)
struct mvt_tags tags)
NON_NULL_ARGS(2, 3);

struct chain_coin_mvt *new_coin_external_spend(const tal_t *ctx,
const struct bitcoin_outpoint *outpoint,
const struct bitcoin_txid *txid,
u32 blockheight,
struct amount_sat amount,
enum mvt_tag tag)
struct mvt_tags tags)
NON_NULL_ARGS(2, 3);

struct chain_coin_mvt *new_coin_external_deposit_tags(const tal_t *ctx,
const struct bitcoin_outpoint *outpoint,
u32 blockheight,
struct amount_sat amount,
enum mvt_tag *tags)
NON_NULL_ARGS(2, 5);

struct chain_coin_mvt *new_coin_external_deposit(const tal_t *ctx,
const struct bitcoin_outpoint *outpoint,
u32 blockheight,
struct amount_sat amount,
enum mvt_tag tag)
struct mvt_tags tags)
NON_NULL_ARGS(2);

struct channel_coin_mvt *new_coin_channel_push(const tal_t *ctx,
const struct channel_id *cid,
const struct channel *channel,
enum coin_mvt_dir direction,
struct amount_msat amount,
enum mvt_tag tag,
bool is_credit)
struct mvt_tags tags)
NON_NULL_ARGS(2);

struct coin_mvt *finalize_chain_mvt(const tal_t *ctx,
const struct chain_coin_mvt *chain_mvt,
const char *hrp_name,
u32 timestamp,
struct node_id *node_id)
NON_NULL_ARGS(2, 3);

struct coin_mvt *finalize_channel_mvt(const tal_t *ctx,
const struct channel_coin_mvt *chan_mvt,
const char *hrp_name,
u32 timestamp,
const struct node_id *node_id)
NON_NULL_ARGS(2, 3, 5);

/* Is this an xternal account? */
bool chain_mvt_is_external(const struct chain_coin_mvt *mvt);

const char *mvt_type_str(enum mvt_type type);
const char *mvt_tag_str(enum mvt_tag tag);
const char **mvt_tag_strs(const tal_t *ctx, struct mvt_tags tags);

/* Parse a single mvt tag. Returns false or populates *tag */
bool mvt_tag_parse(const char *buf, size_t len, enum mvt_tag *tag);

void towire_chain_coin_mvt(u8 **pptr, const struct chain_coin_mvt *mvt);
void fromwire_chain_coin_mvt(const u8 **cursor, size_t *max, struct chain_coin_mvt *mvt);
Expand Down
Loading
Loading