From 1c846461cac8ee40a1286310894621e9d4a99c69 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 12 Jul 2023 05:09:17 +0930 Subject: [PATCH] invoices: keep wallet pointer. We need this to access ->ld later. Signed-off-by: Rusty Russell --- wallet/invoices.c | 40 ++++++++++++++++++++-------------------- wallet/invoices.h | 5 +++-- wallet/test/run-wallet.c | 2 +- wallet/wallet.c | 2 +- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/wallet/invoices.c b/wallet/invoices.c index 51ef3828c..bf597b7c2 100644 --- a/wallet/invoices.c +++ b/wallet/invoices.c @@ -25,7 +25,7 @@ struct invoice_waiter { struct invoices { /* The database connection to use. */ - struct db *db; + struct wallet *wallet; /* The timers object to use for expirations. */ struct timers *timers; /* Waiters waiting for invoices to be paid, expired, or deleted. */ @@ -118,7 +118,7 @@ static struct invoice_details *wallet_stmt2invoice_details(const tal_t *ctx, static void update_db_expirations(struct invoices *invoices, u64 now) { struct db_stmt *stmt; - stmt = db_prepare_v2(invoices->db, SQL("UPDATE invoices" + stmt = db_prepare_v2(invoices->wallet->db, SQL("UPDATE invoices" " SET state = ?" " WHERE state = ?" " AND expiry_time <= ?;")); @@ -131,12 +131,12 @@ static void update_db_expirations(struct invoices *invoices, u64 now) static void install_expiration_timer(struct invoices *invoices); struct invoices *invoices_new(const tal_t *ctx, - struct db *db, + struct wallet *wallet, struct timers *timers) { struct invoices *invs = tal(ctx, struct invoices); - invs->db = db; + invs->wallet = wallet; invs->timers = timers; list_head_init(&invs->waiters); @@ -166,7 +166,7 @@ static void trigger_expiration(struct invoices *invoices) /* Acquire all expired invoices and save them in a list */ list_head_init(&idlist); - stmt = db_prepare_v2(invoices->db, SQL("SELECT id" + stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT id" " FROM invoices" " WHERE state = ?" " AND expiry_time <= ?")); @@ -205,7 +205,7 @@ static void install_expiration_timer(struct invoices *invoices) assert(!invoices->expiration_timer); /* Find unpaid invoice with nearest expiry time */ - stmt = db_prepare_v2(invoices->db, SQL("SELECT MIN(expiry_time)" + stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT MIN(expiry_time)" " FROM invoices" " WHERE state = ?;")); db_bind_int(stmt, 0, UNPAID); @@ -277,7 +277,7 @@ bool invoices_create(struct invoices *invoices, /* Save to database. */ stmt = db_prepare_v2( - invoices->db, + invoices->wallet->db, SQL("INSERT INTO invoices" " ( payment_hash, payment_key, state" " , msatoshi, label, expiry_time" @@ -333,7 +333,7 @@ bool invoices_find_by_label(struct invoices *invoices, const struct json_escape *label) { struct db_stmt *stmt; - stmt = db_prepare_v2(invoices->db, SQL("SELECT id" + stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT id" " FROM invoices" " WHERE label = ?;")); db_bind_json_escape(stmt, 0, label); @@ -355,7 +355,7 @@ bool invoices_find_by_rhash(struct invoices *invoices, { struct db_stmt *stmt; - stmt = db_prepare_v2(invoices->db, SQL("SELECT id" + stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT id" " FROM invoices" " WHERE payment_hash = ?;")); db_bind_sha256(stmt, 0, rhash); @@ -376,7 +376,7 @@ bool invoices_find_unpaid(struct invoices *invoices, const struct sha256 *rhash) { struct db_stmt *stmt; - stmt = db_prepare_v2(invoices->db, SQL("SELECT id" + stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT id" " FROM invoices" " WHERE payment_hash = ?" " AND state = ?;")); @@ -399,7 +399,7 @@ bool invoices_delete(struct invoices *invoices, struct invoice invoice) struct db_stmt *stmt; int changes; /* Delete from database. */ - stmt = db_prepare_v2(invoices->db, + stmt = db_prepare_v2(invoices->wallet->db, SQL("DELETE FROM invoices WHERE id=?;")); db_bind_u64(stmt, 0, invoice.id); db_exec_prepared_v2(stmt); @@ -420,7 +420,7 @@ bool invoices_delete_description(struct invoices *invoices, struct invoice invoi struct db_stmt *stmt; int changes; - stmt = db_prepare_v2(invoices->db, SQL("UPDATE invoices" + stmt = db_prepare_v2(invoices->wallet->db, SQL("UPDATE invoices" " SET description = NULL" " WHERE ID = ?;")); db_bind_u64(stmt, 0, invoice.id); @@ -436,7 +436,7 @@ void invoices_delete_expired(struct invoices *invoices, u64 max_expiry_time) { struct db_stmt *stmt; - stmt = db_prepare_v2(invoices->db, SQL( + stmt = db_prepare_v2(invoices->wallet->db, SQL( "DELETE FROM invoices" " WHERE state = ?" " AND expiry_time <= ?;")); @@ -451,7 +451,7 @@ bool invoices_iterate(struct invoices *invoices, struct db_stmt *stmt; if (!it->p) { - stmt = db_prepare_v2(invoices->db, SQL("SELECT" + stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT" " state" ", payment_key" ", payment_hash" @@ -509,7 +509,7 @@ static enum invoice_status invoice_get_status(struct invoices *invoices, struct bool res; stmt = db_prepare_v2( - invoices->db, SQL("SELECT state FROM invoices WHERE id = ?;")); + invoices->wallet->db, SQL("SELECT state FROM invoices WHERE id = ?;")); db_bind_u64(stmt, 0, invoice.id); db_query_prepared(stmt); @@ -555,11 +555,11 @@ bool invoices_resolve(struct invoices *invoices, return false; /* Assign a pay-index. */ - pay_index = get_next_pay_index(invoices->db); + pay_index = get_next_pay_index(invoices->wallet->db); paid_timestamp = time_now().ts.tv_sec; /* Update database. */ - stmt = db_prepare_v2(invoices->db, SQL("UPDATE invoices" + stmt = db_prepare_v2(invoices->wallet->db, SQL("UPDATE invoices" " SET state=?" " , pay_index=?" " , msatoshi_received=?" @@ -572,7 +572,7 @@ bool invoices_resolve(struct invoices *invoices, db_bind_u64(stmt, 4, invoice.id); db_exec_prepared_v2(take(stmt)); - maybe_mark_offer_used(invoices->db, invoice); + maybe_mark_offer_used(invoices->wallet->db, invoice); /* Tell all the waiters about the paid invoice. */ trigger_invoice_waiter_resolve(invoices, invoice.id, &invoice); @@ -617,7 +617,7 @@ void invoices_waitany(const tal_t *ctx, struct invoice invoice; /* Look for an already-paid invoice. */ - stmt = db_prepare_v2(invoices->db, + stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT id" " FROM invoices" " WHERE pay_index IS NOT NULL" @@ -667,7 +667,7 @@ struct invoice_details *invoices_get_details(const tal_t *ctx, bool res; struct invoice_details *details; - stmt = db_prepare_v2(invoices->db, SQL("SELECT" + stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT" " state" ", payment_key" ", payment_hash" diff --git a/wallet/invoices.h b/wallet/invoices.h index 7aa584452..e57ab54c6 100644 --- a/wallet/invoices.h +++ b/wallet/invoices.h @@ -13,16 +13,17 @@ struct invoice_iterator; struct invoices; struct sha256; struct timers; +struct wallet; /** * invoices_new - Constructor for a new invoice handler * * @ctx - the owner of the invoice handler. - * @db - the database connection to use for saving invoice. + * @wallet - the wallet * @timers - the timers object to use for expirations. */ struct invoices *invoices_new(const tal_t *ctx, - struct db *db, + struct wallet *wallet, struct timers *timers); /** diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 5b4e64612..461ace8b5 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -294,7 +294,7 @@ const struct invoice_details *invoices_iterator_deref( { fprintf(stderr, "invoices_iterator_deref called!\n"); abort(); } /* Generated stub for invoices_new */ struct invoices *invoices_new(const tal_t *ctx UNNEEDED, - struct db *db UNNEEDED, + struct wallet *wallet UNNEEDED, struct timers *timers UNNEEDED) { fprintf(stderr, "invoices_new called!\n"); abort(); } /* Generated stub for invoices_resolve */ diff --git a/wallet/wallet.c b/wallet/wallet.c index 05fa25430..40dbd1be2 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -112,7 +112,7 @@ struct wallet *wallet_new(struct lightningd *ld, struct timers *timers) wallet->db = db_setup(wallet, ld, ld->bip32_base); db_begin_transaction(wallet->db); - wallet->invoices = invoices_new(wallet, wallet->db, timers); + wallet->invoices = invoices_new(wallet, wallet, timers); outpointfilters_init(wallet); db_commit_transaction(wallet->db); return wallet;