From 2a3875204a562ebed60af4270f77368d9599d757 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 19 Jul 2022 17:04:35 +0930 Subject: [PATCH] bkpr: parse the 'originating_account' field, save to event It's useful to know which account an 'external' event impacted, so we save this data now --- plugins/bkpr/bookkeeper.c | 8 +++++ plugins/bkpr/chain_event.h | 3 ++ plugins/bkpr/db.c | 1 + plugins/bkpr/recorder.c | 52 ++++++++++++++++++++------------ plugins/bkpr/recorder.h | 3 ++ plugins/bkpr/test/run-recorder.c | 9 ++++++ 6 files changed, 56 insertions(+), 20 deletions(-) diff --git a/plugins/bkpr/bookkeeper.c b/plugins/bkpr/bookkeeper.c index 4ad328854..b0464f3e1 100644 --- a/plugins/bkpr/bookkeeper.c +++ b/plugins/bkpr/bookkeeper.c @@ -784,6 +784,14 @@ parse_and_log_chain_move(struct command *cmd, err = tal_free(err); } + err = json_scan(tmpctx, buf, params, + "{coin_movement:" + "{originating_account:%}}", + JSON_SCAN_TAL(e, json_strdup, &e->origin_acct)); + + if (err) + e->origin_acct = NULL; + e->payment_id = tal_steal(e, payment_hash); e->credit = credit; diff --git a/plugins/bkpr/chain_event.h b/plugins/bkpr/chain_event.h index 9359de8f8..1ebd69523 100644 --- a/plugins/bkpr/chain_event.h +++ b/plugins/bkpr/chain_event.h @@ -19,6 +19,9 @@ struct chain_event { /* Name of the account this belongs to */ char *acct_name; + /* Name of account this originated from */ + char *origin_acct; + /* Tag describing the event */ const char *tag; diff --git a/plugins/bkpr/db.c b/plugins/bkpr/db.c index 6064d3229..8edda43c8 100644 --- a/plugins/bkpr/db.c +++ b/plugins/bkpr/db.c @@ -92,6 +92,7 @@ static struct migration db_migrations[] = { ", PRIMARY KEY (account_id, txid, update_count)" ");"), NULL}, + {SQL("ALTER TABLE chain_events ADD origin TEXT;"), NULL}, }; static bool db_migrate(struct plugin *p, struct db *db) diff --git a/plugins/bkpr/recorder.c b/plugins/bkpr/recorder.c index 1a8b45d44..29f52ea97 100644 --- a/plugins/bkpr/recorder.c +++ b/plugins/bkpr/recorder.c @@ -15,9 +15,6 @@ #include #include -#define EXTERNAL_ACCT "external" -#define WALLET_ACCT WALLET - static struct chain_event *stmt2chain_event(const tal_t *ctx, struct db_stmt *stmt) { struct chain_event *e = tal(ctx, struct chain_event); @@ -25,6 +22,11 @@ static struct chain_event *stmt2chain_event(const tal_t *ctx, struct db_stmt *st e->acct_db_id = db_col_u64(stmt, "e.account_id"); e->acct_name = db_col_strdup(e, stmt, "a.name"); + if (!db_col_is_null(stmt, "e.origin")) + e->origin_acct = db_col_strdup(e, stmt, "e.origin"); + else + e->origin_acct = NULL; + e->tag = db_col_strdup(e, stmt, "e.tag"); db_col_amount_msat(stmt, "e.credit", &e->credit); @@ -105,6 +107,7 @@ struct chain_event **list_chain_events(const tal_t *ctx, struct db *db) " e.id" ", e.account_id" ", a.name" + ", e.origin" ", e.tag" ", e.credit" ", e.debit" @@ -134,6 +137,7 @@ struct chain_event **account_get_chain_events(const tal_t *ctx, " e.id" ", e.account_id" ", a.name" + ", e.origin" ", e.tag" ", e.credit" ", e.debit" @@ -170,6 +174,7 @@ static struct chain_event *find_chain_event(const tal_t *ctx, " e.id" ", e.account_id" ", a.name" + ", e.origin" ", e.tag" ", e.credit" ", e.debit" @@ -195,6 +200,7 @@ static struct chain_event *find_chain_event(const tal_t *ctx, " e.id" ", e.account_id" ", a.name" + ", e.origin" ", e.tag" ", e.credit" ", e.debit" @@ -781,8 +787,9 @@ static struct chain_event **find_chain_events_bytxid(const tal_t *ctx, struct db stmt = db_prepare_v2(db, SQL("SELECT " " e.id" - ", a.name" ", e.account_id" + ", a.name" + ", e.origin" ", e.tag" ", e.credit" ", e.debit" @@ -1069,6 +1076,7 @@ bool log_chain_event(struct db *db, stmt = db_prepare_v2(db, SQL("INSERT INTO chain_events" " (" " account_id" + ", origin" ", tag" ", credit" ", debit" @@ -1082,29 +1090,33 @@ bool log_chain_event(struct db *db, ", spending_txid" ")" " VALUES" - " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")); + " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")); db_bind_u64(stmt, 0, acct->db_id); - db_bind_text(stmt, 1, e->tag); - db_bind_amount_msat(stmt, 2, &e->credit); - db_bind_amount_msat(stmt, 3, &e->debit); - db_bind_amount_msat(stmt, 4, &e->output_value); - db_bind_text(stmt, 5, e->currency); - db_bind_u64(stmt, 6, e->timestamp); - db_bind_int(stmt, 7, e->blockheight); - db_bind_txid(stmt, 8, &e->outpoint.txid); - db_bind_int(stmt, 9, e->outpoint.n); + if (e->origin_acct) + db_bind_text(stmt, 1, e->origin_acct); + else + db_bind_null(stmt, 1); + db_bind_text(stmt, 2, e->tag); + db_bind_amount_msat(stmt, 3, &e->credit); + db_bind_amount_msat(stmt, 4, &e->debit); + db_bind_amount_msat(stmt, 5, &e->output_value); + db_bind_text(stmt, 6, e->currency); + db_bind_u64(stmt, 7, e->timestamp); + db_bind_int(stmt, 8, e->blockheight); + db_bind_txid(stmt, 9, &e->outpoint.txid); + db_bind_int(stmt, 10, e->outpoint.n); if (e->payment_id) - db_bind_sha256(stmt, 10, e->payment_id); - else - db_bind_null(stmt, 10); - - if (e->spending_txid) - db_bind_txid(stmt, 11, e->spending_txid); + db_bind_sha256(stmt, 11, e->payment_id); else db_bind_null(stmt, 11); + if (e->spending_txid) + db_bind_txid(stmt, 12, e->spending_txid); + else + db_bind_null(stmt, 12); + db_exec_prepared_v2(stmt); e->db_id = db_last_insert_id_v2(stmt); e->acct_db_id = acct->db_id; diff --git a/plugins/bkpr/recorder.h b/plugins/bkpr/recorder.h index 2f41e0232..6fe47b59c 100644 --- a/plugins/bkpr/recorder.h +++ b/plugins/bkpr/recorder.h @@ -12,6 +12,9 @@ struct db; enum mvt_tag; struct onchain_fee; +#define EXTERNAL_ACCT "external" +#define WALLET_ACCT WALLET + struct acct_balance { char *currency; struct amount_msat credit; diff --git a/plugins/bkpr/test/run-recorder.c b/plugins/bkpr/test/run-recorder.c index 46dc28896..400a8fc35 100644 --- a/plugins/bkpr/test/run-recorder.c +++ b/plugins/bkpr/test/run-recorder.c @@ -272,6 +272,9 @@ static bool chain_events_eq(struct chain_event *e1, struct chain_event *e2) { CHECK(e1->db_id == e2->db_id); CHECK(e1->acct_db_id == e2->acct_db_id); + CHECK((e1->origin_acct != NULL) == (e2->origin_acct != NULL)); + if (e1->origin_acct) + CHECK(streq(e1->origin_acct, e2->origin_acct)); CHECK(streq(e1->tag, e2->tag)); CHECK(amount_msat_eq(e1->credit, e2->credit)); CHECK(amount_msat_eq(e1->debit, e2->debit)); @@ -289,6 +292,7 @@ static bool chain_events_eq(struct chain_event *e1, struct chain_event *e2) if (e1->payment_id) CHECK(sha256_eq(e1->payment_id, e2->payment_id)); + return true; } @@ -327,6 +331,7 @@ static struct chain_event *make_chain_event(const tal_t *ctx, /* This event spends the second inserted event */ ev->tag = tal_fmt(ctx, "%s", tag); + ev->origin_acct = NULL; ev->credit = credit; ev->debit = debit; ev->output_value = AMOUNT_MSAT(1000); @@ -797,6 +802,7 @@ static bool test_chain_event_crud(const tal_t *ctx, struct plugin *p) /* This event spends the second inserted event */ ev1 = tal(ctx, struct chain_event); ev1->tag = tal_fmt(ev1, "withdrawal"); + ev1->origin_acct = NULL; ev1->credit = AMOUNT_MSAT(100); ev1->debit = AMOUNT_MSAT(102); ev1->output_value = AMOUNT_MSAT(104); @@ -815,6 +821,7 @@ static bool test_chain_event_crud(const tal_t *ctx, struct plugin *p) CHECK_MSG(!db_err, db_err); ev2->tag = tal_fmt(ctx, "deposit"); + ev2->origin_acct = tal_fmt(ctx, "wallet"); ev2->credit = AMOUNT_MSAT(200); ev2->debit = AMOUNT_MSAT(202); ev2->output_value = AMOUNT_MSAT(104); @@ -830,6 +837,7 @@ static bool test_chain_event_crud(const tal_t *ctx, struct plugin *p) /* Dummy event, logged to separate account */ ev3 = tal(ctx, struct chain_event); ev3->tag = tal_fmt(ev3, "deposit"); + ev3->origin_acct = NULL; ev3->credit = AMOUNT_MSAT(300); ev3->debit = AMOUNT_MSAT(302); ev3->output_value = AMOUNT_MSAT(304); @@ -1038,6 +1046,7 @@ static bool test_account_crud(const tal_t *ctx, struct plugin *p) * correctly, given an event and tag list? */ ev1 = tal(ctx, struct chain_event); ev1->tag = tal_fmt(ctx, "withdrawal"); + ev1->origin_acct = NULL; ev1->credit = AMOUNT_MSAT(100); ev1->debit = AMOUNT_MSAT(102); ev1->output_value = AMOUNT_MSAT(104);