From d943e5e85cabae7f2611bd754f91fb0855cf78c3 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 19 Jul 2022 17:04:35 +0930 Subject: [PATCH] bkpr: use pointer for payment_id for channel events sometimes these are null! --- plugins/bkpr/bookkeeper.c | 7 ++++--- plugins/bkpr/channel_event.h | 2 +- plugins/bkpr/recorder.c | 17 ++++++++++++++--- plugins/bkpr/test/run-recorder.c | 27 +++++++++++++++++++-------- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/plugins/bkpr/bookkeeper.c b/plugins/bkpr/bookkeeper.c index dccc75d43..160b06c30 100644 --- a/plugins/bkpr/bookkeeper.c +++ b/plugins/bkpr/bookkeeper.c @@ -204,7 +204,7 @@ static struct command_result *json_balance_snapshot(struct command *cmd, ev.fees = AMOUNT_MSAT(0); ev.currency = s.coin_type; ev.part_id = 0; - memset(&ev.payment_id, 0, sizeof(struct sha256)); + ev.payment_id = NULL; /* Use current time for this */ ev.timestamp = time_now().ts.tv_sec; @@ -323,11 +323,12 @@ static const char *parse_and_log_channel_move(struct command *cmd, struct account *acct; const char *err; + e->payment_id = tal(e, struct sha256); err = json_scan(tmpctx, buf, params, "{coin_movement:{payment_hash:%}}", - JSON_SCAN(json_to_sha256, &e->payment_id)); + JSON_SCAN(json_to_sha256, e->payment_id)); if (err) - memset(&e->payment_id, 0, sizeof(struct sha256)); + e->payment_id = tal_free(e->payment_id); err = json_scan(tmpctx, buf, params, "{coin_movement:{part_id:%}}", diff --git a/plugins/bkpr/channel_event.h b/plugins/bkpr/channel_event.h index d6a9533fc..c64c21d4b 100644 --- a/plugins/bkpr/channel_event.h +++ b/plugins/bkpr/channel_event.h @@ -31,7 +31,7 @@ struct channel_event { const char *currency; /* Payment identifier (typically the preimage hash) */ - struct sha256 payment_id; + struct sha256 *payment_id; /* Some payments share a payment_id, and are differentiable via id */ u32 part_id; diff --git a/plugins/bkpr/recorder.c b/plugins/bkpr/recorder.c index 51c7332ef..57e38e388 100644 --- a/plugins/bkpr/recorder.c +++ b/plugins/bkpr/recorder.c @@ -84,7 +84,11 @@ static struct channel_event *stmt2channel_event(const tal_t *ctx, struct db_stmt db_col_amount_msat(stmt, "fees", &e->fees); e->currency = db_col_strdup(e, stmt, "currency"); - db_col_sha256(stmt, "payment_id", &e->payment_id); + if (!db_col_is_null(stmt, "payment_id")) { + e->payment_id = tal(e, struct sha256); + db_col_sha256(stmt, "payment_id", e->payment_id); + } else + e->payment_id = NULL; e->part_id = db_col_int(stmt, "part_id"); e->timestamp = db_col_u64(stmt, "timestamp"); @@ -629,7 +633,10 @@ void log_channel_event(struct db *db, db_bind_amount_msat(stmt, 3, &e->debit); db_bind_amount_msat(stmt, 4, &e->fees); db_bind_text(stmt, 5, e->currency); - db_bind_sha256(stmt, 6, &e->payment_id); + if (e->payment_id) + db_bind_sha256(stmt, 6, e->payment_id); + else + db_bind_null(stmt, 6); db_bind_int(stmt, 7, e->part_id); db_bind_u64(stmt, 8, e->timestamp); @@ -956,7 +963,11 @@ void log_chain_event(struct db *db, db_bind_int(stmt, 7, e->blockheight); db_bind_txid(stmt, 8, &e->outpoint.txid); db_bind_int(stmt, 9, e->outpoint.n); - db_bind_sha256(stmt, 10, e->payment_id); + + 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); diff --git a/plugins/bkpr/test/run-recorder.c b/plugins/bkpr/test/run-recorder.c index f7ed501ba..06140746d 100644 --- a/plugins/bkpr/test/run-recorder.c +++ b/plugins/bkpr/test/run-recorder.c @@ -259,7 +259,9 @@ static bool channel_events_eq(struct channel_event *e1, struct channel_event *e2 CHECK(amount_msat_eq(e1->debit, e2->debit)); CHECK(amount_msat_eq(e1->fees, e2->fees)); CHECK(streq(e1->currency, e2->currency)); - CHECK(sha256_eq(&e1->payment_id, &e2->payment_id)); + CHECK((e1->payment_id != NULL) == (e2->payment_id != NULL)); + if (e1->payment_id) + CHECK(sha256_eq(e1->payment_id, e2->payment_id)); CHECK(e1->part_id == e2->part_id); CHECK(e1->timestamp == e2->timestamp); @@ -298,7 +300,8 @@ static struct channel_event *make_channel_event(const tal_t *ctx, { struct channel_event *ev = tal(ctx, struct channel_event); - memset(&ev->payment_id, payment_char, sizeof(struct sha256)); + ev->payment_id = tal(ev, struct sha256); + memset(ev->payment_id, payment_char, sizeof(struct sha256)); ev->credit = credit; ev->debit = debit; ev->fees = AMOUNT_MSAT(104); @@ -707,32 +710,35 @@ static bool test_channel_event_crud(const tal_t *ctx, struct plugin *p) db_commit_transaction(db); CHECK_MSG(!db_err, db_err); - memset(&ev1.payment_id, 'B', sizeof(struct sha256)); + ev1.payment_id = tal(ctx, struct sha256); + memset(ev1.payment_id, 'B', sizeof(struct sha256)); ev1.credit = AMOUNT_MSAT(100); ev1.debit = AMOUNT_MSAT(102); ev1.fees = AMOUNT_MSAT(104); ev1.currency = "btc"; - ev1.timestamp = 1919191; + ev1.timestamp = 11111; ev1.part_id = 19; /* Passing unknown tags in should be ok */ ev1.tag = "hello"; - memset(&ev2.payment_id, 'C', sizeof(struct sha256)); + ev2.payment_id = tal(ctx, struct sha256); + memset(ev2.payment_id, 'C', sizeof(struct sha256)); ev2.credit = AMOUNT_MSAT(200); ev2.debit = AMOUNT_MSAT(202); ev2.fees = AMOUNT_MSAT(204); ev2.currency = "brct"; - ev2.timestamp = 1818181; + ev2.timestamp = 22222; ev2.part_id = 0; ev2.tag = tal_fmt(ctx, "deposit"); - memset(&ev3.payment_id, 'D', sizeof(struct sha256)); + ev3.payment_id = tal(ctx, struct sha256); + memset(ev3.payment_id, 'D', sizeof(struct sha256)); ev3.credit = AMOUNT_MSAT(300); ev3.debit = AMOUNT_MSAT(302); ev3.fees = AMOUNT_MSAT(304); ev3.currency = "brct"; - ev3.timestamp = 1717171; + ev3.timestamp = 33333; ev3.part_id = 5; ev3.tag = tal_fmt(ctx, "routed"); @@ -742,6 +748,11 @@ static bool test_channel_event_crud(const tal_t *ctx, struct plugin *p) /* log a channel event to a different acct */ log_channel_event(db, acct2, &ev3); + + /* log a channel event without a payment id */ + ev3.payment_id = NULL; + log_channel_event(db, acct2, &ev3); + db_commit_transaction(db); CHECK_MSG(!db_err, db_err);