From 462fa20c17dbf6e2b4db2a0f911ad061bd16eb10 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 19 Jul 2022 17:04:37 +0930 Subject: [PATCH] bkpr: move json_to functions to respective type files --- plugins/bkpr/Makefile | 2 ++ plugins/bkpr/bookkeeper.c | 51 ------------------------------------ plugins/bkpr/chain_event.c | 26 ++++++++++++++++++ plugins/bkpr/chain_event.h | 5 ++++ plugins/bkpr/channel_event.c | 17 ++++++++++++ plugins/bkpr/channel_event.h | 4 +++ plugins/bkpr/onchain_fee.c | 20 ++++++++++++++ plugins/bkpr/onchain_fee.h | 4 +++ 8 files changed, 78 insertions(+), 51 deletions(-) create mode 100644 plugins/bkpr/chain_event.c create mode 100644 plugins/bkpr/onchain_fee.c diff --git a/plugins/bkpr/Makefile b/plugins/bkpr/Makefile index 34b54ae1d..a9b123613 100644 --- a/plugins/bkpr/Makefile +++ b/plugins/bkpr/Makefile @@ -4,8 +4,10 @@ BOOKKEEPER_PLUGIN_SRC := \ plugins/bkpr/account.c \ plugins/bkpr/account_entry.c \ plugins/bkpr/bookkeeper.c \ + plugins/bkpr/chain_event.c \ plugins/bkpr/channel_event.c \ plugins/bkpr/db.c \ + plugins/bkpr/onchain_fee.c \ plugins/bkpr/recorder.c BOOKKEEPER_DB_QUERIES := \ diff --git a/plugins/bkpr/bookkeeper.c b/plugins/bkpr/bookkeeper.c index afe7b4717..69f248cf3 100644 --- a/plugins/bkpr/bookkeeper.c +++ b/plugins/bkpr/bookkeeper.c @@ -27,57 +27,6 @@ static struct db *db ; // FIXME: make relative to directory we're loaded into static char *db_dsn = "sqlite3://accounts.sqlite3"; -static void json_add_channel_event(struct json_stream *out, - struct channel_event *ev) -{ - json_object_start(out, NULL); - json_add_string(out, "account", ev->acct_name); - json_add_string(out, "type", "channel"); - json_add_string(out, "tag", ev->tag); - json_add_amount_msat_only(out, "credit", ev->credit); - json_add_amount_msat_only(out, "debit", ev->debit); - json_add_string(out, "currency", ev->currency); - if (ev->payment_id) - json_add_sha256(out, "payment_id", ev->payment_id); - json_add_u64(out, "timestamp", ev->timestamp); - json_object_end(out); -} - -static void json_add_chain_event(struct json_stream *out, - struct chain_event *ev) -{ - json_object_start(out, NULL); - json_add_string(out, "account", ev->acct_name); - json_add_string(out, "type", "chain"); - json_add_string(out, "tag", ev->tag); - json_add_amount_msat_only(out, "credit", ev->credit); - json_add_amount_msat_only(out, "debit", ev->debit); - json_add_string(out, "currency", ev->currency); - json_add_outpoint(out, "outpoint", &ev->outpoint); - if (ev->spending_txid) - json_add_txid(out, "txid", ev->spending_txid); - if (ev->payment_id) - json_add_sha256(out, "payment_id", ev->payment_id); - json_add_u64(out, "timestamp", ev->timestamp); - json_add_u32(out, "blockheight", ev->blockheight); - json_object_end(out); -} - -static void json_add_onchain_fee(struct json_stream *out, - struct onchain_fee *fee) -{ - json_object_start(out, NULL); - json_add_string(out, "account", fee->acct_name); - json_add_string(out, "type", "onchain_fee"); - json_add_string(out, "tag", "onchain_fee"); - json_add_amount_msat_only(out, "credit", fee->credit); - json_add_amount_msat_only(out, "debit", fee->debit); - json_add_string(out, "currency", fee->currency); - json_add_u64(out, "timestamp", fee->timestamp); - json_add_txid(out, "txid", &fee->txid); - json_object_end(out); -} - static struct fee_sum *find_sum_for_txid(struct fee_sum **sums, struct bitcoin_txid *txid) { diff --git a/plugins/bkpr/chain_event.c b/plugins/bkpr/chain_event.c new file mode 100644 index 000000000..9b055782e --- /dev/null +++ b/plugins/bkpr/chain_event.c @@ -0,0 +1,26 @@ +#include "config.h" + +#include +#include + +void json_add_chain_event(struct json_stream *out, struct chain_event *ev) +{ + json_object_start(out, NULL); + json_add_string(out, "account", ev->acct_name); + if (ev->origin_acct) + json_add_string(out, "origin", ev->origin_acct); + json_add_string(out, "type", "chain"); + json_add_string(out, "tag", ev->tag); + json_add_amount_msat_only(out, "credit", ev->credit); + json_add_amount_msat_only(out, "debit", ev->debit); + json_add_string(out, "currency", ev->currency); + json_add_outpoint(out, "outpoint", &ev->outpoint); + + if (ev->spending_txid) + json_add_txid(out, "txid", ev->spending_txid); + if (ev->payment_id) + json_add_sha256(out, "payment_id", ev->payment_id); + json_add_u64(out, "timestamp", ev->timestamp); + json_add_u32(out, "blockheight", ev->blockheight); + json_object_end(out); +} diff --git a/plugins/bkpr/chain_event.h b/plugins/bkpr/chain_event.h index 1ebd69523..d9d12c27a 100644 --- a/plugins/bkpr/chain_event.h +++ b/plugins/bkpr/chain_event.h @@ -2,11 +2,13 @@ #define LIGHTNING_PLUGINS_BKPR_CHAIN_EVENT_H #include "config.h" +#include #include struct amount_msat; struct bitcoin_outpoint; struct bitcoin_txid; +struct json_stream; struct chain_event { @@ -53,4 +55,7 @@ struct chain_event { struct sha256 *payment_id; }; +void json_add_chain_event(struct json_stream *out, + struct chain_event *ev); + #endif /* LIGHTNING_PLUGINS_BKPR_CHAIN_EVENT_H */ diff --git a/plugins/bkpr/channel_event.c b/plugins/bkpr/channel_event.c index 55ae4686e..782aacf6b 100644 --- a/plugins/bkpr/channel_event.c +++ b/plugins/bkpr/channel_event.c @@ -3,6 +3,7 @@ #include #include #include +#include #include struct channel_event *new_channel_event(const tal_t *ctx, @@ -28,3 +29,19 @@ struct channel_event *new_channel_event(const tal_t *ctx, return ev; } + +void json_add_channel_event(struct json_stream *out, + struct channel_event *ev) +{ + json_object_start(out, NULL); + json_add_string(out, "account", ev->acct_name); + json_add_string(out, "type", "channel"); + json_add_string(out, "tag", ev->tag); + json_add_amount_msat_only(out, "credit", ev->credit); + json_add_amount_msat_only(out, "debit", ev->debit); + json_add_string(out, "currency", ev->currency); + if (ev->payment_id) + json_add_sha256(out, "payment_id", ev->payment_id); + json_add_u64(out, "timestamp", ev->timestamp); + json_object_end(out); +} diff --git a/plugins/bkpr/channel_event.h b/plugins/bkpr/channel_event.h index 8b0066ab9..28a078702 100644 --- a/plugins/bkpr/channel_event.h +++ b/plugins/bkpr/channel_event.h @@ -6,6 +6,7 @@ #include struct amount_msat; +struct json_stream; struct sha256; struct channel_event { @@ -54,4 +55,7 @@ struct channel_event *new_channel_event(const tal_t *ctx, u32 part_id, u64 timestamp); +void json_add_channel_event(struct json_stream *out, + struct channel_event *ev); + #endif /* LIGHTNING_PLUGINS_BKPR_CHANNEL_EVENT_H */ diff --git a/plugins/bkpr/onchain_fee.c b/plugins/bkpr/onchain_fee.c new file mode 100644 index 000000000..57c1002fb --- /dev/null +++ b/plugins/bkpr/onchain_fee.c @@ -0,0 +1,20 @@ +#include "config.h" +#include +#include + + +void json_add_onchain_fee(struct json_stream *out, + struct onchain_fee *fee) +{ + json_object_start(out, NULL); + json_add_string(out, "account", fee->acct_name); + json_add_string(out, "type", "onchain_fee"); + json_add_string(out, "tag", "onchain_fee"); + json_add_amount_msat_only(out, "credit", fee->credit); + json_add_amount_msat_only(out, "debit", fee->debit); + json_add_string(out, "currency", fee->currency); + json_add_u64(out, "timestamp", fee->timestamp); + json_add_txid(out, "txid", &fee->txid); + json_object_end(out); +} + diff --git a/plugins/bkpr/onchain_fee.h b/plugins/bkpr/onchain_fee.h index b57c9f18a..34f95407d 100644 --- a/plugins/bkpr/onchain_fee.h +++ b/plugins/bkpr/onchain_fee.h @@ -2,6 +2,7 @@ #define LIGHTNING_PLUGINS_BKPR_ONCHAIN_FEE_H #include "config.h" +#include #include struct amount_msat; @@ -32,4 +33,7 @@ struct onchain_fee { u32 update_count; }; +void json_add_onchain_fee(struct json_stream *out, + struct onchain_fee *fee); + #endif /* LIGHTNING_PLUGINS_BKPR_ONCHAIN_FEE_H */