From 967dd6c776e139a51f98c7ad84c8c382e8c53a80 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 2 Nov 2017 21:09:57 +0100 Subject: [PATCH] wallet: Add functions to store and manipulate payments Signed-off-by: Christian Decker --- wallet/wallet.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ wallet/wallet.h | 29 ++++++++++++++- 2 files changed, 123 insertions(+), 1 deletion(-) diff --git a/wallet/wallet.c b/wallet/wallet.c index ae609e55a..f9b6fc958 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1157,3 +1157,98 @@ struct htlc_stub *wallet_htlc_stubs(tal_t *ctx, struct wallet *wallet, sqlite3_finalize(stmt); return stubs; } + +bool wallet_payment_add(struct wallet *wallet, + struct wallet_payment *payment) +{ + sqlite3_stmt *stmt; + + /* Don't attempt to add the same payment twice */ + assert(!payment->id); + + stmt = db_prepare( + wallet->db, + "INSERT INTO payments (" + " status," + " payment_hash," + " direction," + " destination," + " msatoshi," + " timestamp" + ") VALUES (?, ?, ?, ?, ?, ?);"); + + sqlite3_bind_int(stmt, 1, payment->status); + sqlite3_bind_sha256(stmt, 2, &payment->payment_hash); + sqlite3_bind_int(stmt, 3, payment->incoming?DIRECTION_INCOMING:DIRECTION_OUTGOING); + + if (payment->destination) + sqlite3_bind_pubkey(stmt, 4, payment->destination); + else + sqlite3_bind_null(stmt, 4); + + sqlite3_bind_int64(stmt, 5, payment->msatoshi); + + sqlite3_bind_int(stmt, 6, payment->timestamp); + + db_exec_prepared(wallet->db, stmt); + payment->id = sqlite3_last_insert_rowid(wallet->db->sql); + return true; +} + +static struct wallet_payment *wallet_stmt2payment(const tal_t *ctx, + sqlite3_stmt *stmt) +{ + struct wallet_payment *payment = tal(ctx, struct wallet_payment); + payment->id = sqlite3_column_int64(stmt, 0); + payment->status = sqlite3_column_int(stmt, 1); + payment->incoming = sqlite3_column_int(stmt, 2) == DIRECTION_INCOMING; + + if (sqlite3_column_type(stmt, 3) != SQLITE_NULL) { + payment->destination = tal(payment, struct pubkey); + sqlite3_column_pubkey(stmt, 3, payment->destination); + } else { + payment->destination = NULL; + } + + payment->msatoshi = sqlite3_column_int64(stmt, 4); + sqlite3_column_sha256(stmt, 5, &payment->payment_hash); + + payment->timestamp = sqlite3_column_int(stmt, 6); + return payment; +} + +struct wallet_payment * +wallet_payment_by_hash(const tal_t *ctx, struct wallet *wallet, + const struct sha256 *payment_hash) +{ + sqlite3_stmt *stmt; + struct wallet_payment *payment = NULL; + + stmt = db_prepare(wallet->db, + "SELECT id, status, direction, destination," + "msatoshi , payment_hash, timestamp " + "FROM payments " + "WHERE payment_hash = ?"); + + sqlite3_bind_sha256(stmt, 1, payment_hash); + if (sqlite3_step(stmt) == SQLITE_ROW) { + payment = wallet_stmt2payment(ctx, stmt); + } + sqlite3_finalize(stmt); + return payment; +} + +void wallet_payment_set_status(struct wallet *wallet, + const struct sha256 *payment_hash, + const enum wallet_payment_status newstatus) +{ + sqlite3_stmt *stmt; + + stmt = db_prepare(wallet->db, + "UPDATE payments SET status=? " + "WHERE payment_hash=?"); + + sqlite3_bind_int(stmt, 1, newstatus); + sqlite3_bind_sha256(stmt, 2, payment_hash); + db_exec_prepared(wallet->db, stmt); +} diff --git a/wallet/wallet.h b/wallet/wallet.h index 2f46dda90..72c6b5941 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -86,7 +86,7 @@ struct wallet_payment { u32 timestamp; bool incoming; struct sha256 payment_hash; - enum wallet_transfer_status status; + enum wallet_payment_status status; struct pubkey *destination; u64 msatoshi; }; @@ -380,4 +380,31 @@ bool wallet_invoice_remove(struct wallet *wallet, struct invoice *inv); struct htlc_stub *wallet_htlc_stubs(tal_t *ctx, struct wallet *wallet, struct wallet_channel *chan); +/** + * wallet_payment_add - Record a new incoming/outgoing payment + * + * Stores the payment in the database. + */ +bool wallet_payment_add(struct wallet *wallet, + struct wallet_payment *payment); + +/** + * wallet_payment_by_hash - Retrieve a specific payment + * + * Given the `payment_hash` retrieve the matching payment. + */ +struct wallet_payment * +wallet_payment_by_hash(const tal_t *ctx, struct wallet *wallet, + const struct sha256 *payment_hash); + +/** + * wallet_payment_set_status - Update the status of the payment + * + * Search for the payment with the given `payment_hash` and update + * its state. + */ +void wallet_payment_set_status(struct wallet *wallet, + const struct sha256 *payment_hash, + const enum wallet_payment_status newstatus); + #endif /* WALLET_WALLET_H */