From 3e53a63cf23abc96fce1d5eaf52dc6d95bc37751 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 3 Sep 2018 10:15:27 +0930 Subject: [PATCH] wallet: do wallet_invoice init during preparation. We have a transaction anyway, and it's simpler. Signed-off-by: Rusty Russell --- lightningd/lightningd.c | 5 ---- lightningd/test/run-find_my_path.c | 3 -- wallet/invoices.c | 46 ++++++++++++------------------ wallet/invoices.h | 8 ------ wallet/test/run-wallet.c | 3 -- wallet/wallet.c | 8 +----- wallet/wallet.h | 14 --------- 7 files changed, 20 insertions(+), 67 deletions(-) diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index ff5c1564d..9430b1c86 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -361,11 +361,6 @@ int main(int argc, char *argv[]) /* Initialize the transaction filter with our pubkeys. */ init_txfilter(ld->wallet, ld->owned_txfilter); - /* Check invoices loaded from the database */ - if (!wallet_invoice_load(ld->wallet)) { - fatal("Could not load invoices from the database"); - } - /* Set up invoice autoclean. */ wallet_invoice_autoclean(ld->wallet, ld->ini_autocleaninvoice_cycle, diff --git a/lightningd/test/run-find_my_path.c b/lightningd/test/run-find_my_path.c index b16ba138a..82fa522f3 100644 --- a/lightningd/test/run-find_my_path.c +++ b/lightningd/test/run-find_my_path.c @@ -145,9 +145,6 @@ void wallet_invoice_autoclean(struct wallet * wallet UNNEEDED, u64 cycle_seconds UNNEEDED, u64 expired_by UNNEEDED) { fprintf(stderr, "wallet_invoice_autoclean called!\n"); abort(); } -/* Generated stub for wallet_invoice_load */ -bool wallet_invoice_load(struct wallet *wallet UNNEEDED) -{ fprintf(stderr, "wallet_invoice_load called!\n"); abort(); } /* Generated stub for wallet_network_check */ bool wallet_network_check(struct wallet *w UNNEEDED, const struct chainparams *chainparams UNNEEDED) diff --git a/wallet/invoices.c b/wallet/invoices.c index 17cf67f8d..ba0f71ab4 100644 --- a/wallet/invoices.c +++ b/wallet/invoices.c @@ -128,6 +128,23 @@ static struct invoice_details *wallet_stmt2invoice_details(const tal_t *ctx, return dtl; } +/* Update expirations. */ +static void update_db_expirations(struct invoices *invoices, u64 now) +{ + sqlite3_stmt *stmt; + stmt = db_prepare(invoices->db, + "UPDATE invoices" + " SET state = ?" + " WHERE state = ?" + " AND expiry_time <= ?;"); + sqlite3_bind_int(stmt, 1, EXPIRED); + sqlite3_bind_int(stmt, 2, UNPAID); + sqlite3_bind_int64(stmt, 3, now); + db_exec_prepared(invoices->db, stmt); +} + +static void install_expiration_timer(struct invoices *invoices); + struct invoices *invoices_new(const tal_t *ctx, struct db *db, struct log *log, @@ -144,30 +161,16 @@ struct invoices *invoices_new(const tal_t *ctx, invs->expiration_timer = NULL; invs->autoclean_timer = NULL; + update_db_expirations(invs, time_now().ts.tv_sec); + install_expiration_timer(invs); return invs; } -/* Update expirations. */ -static void update_db_expirations(struct invoices *invoices, u64 now) -{ - sqlite3_stmt *stmt; - stmt = db_prepare(invoices->db, - "UPDATE invoices" - " SET state = ?" - " WHERE state = ?" - " AND expiry_time <= ?;"); - sqlite3_bind_int(stmt, 1, EXPIRED); - sqlite3_bind_int(stmt, 2, UNPAID); - sqlite3_bind_int64(stmt, 3, now); - db_exec_prepared(invoices->db, stmt); -} - struct invoice_id_node { struct list_node list; u64 id; }; -static void install_expiration_timer(struct invoices *invoices); static void trigger_expiration(struct invoices *invoices) { struct list_head idlist; @@ -254,17 +257,6 @@ static void install_expiration_timer(struct invoices *invoices) invoices); } -bool invoices_load(struct invoices *invoices) -{ - u64 now = time_now().ts.tv_sec; - - update_db_expirations(invoices, now); - - install_expiration_timer(invoices); - - return true; -} - bool invoices_create(struct invoices *invoices, struct invoice *pinvoice, u64 *msatoshi TAKES, diff --git a/wallet/invoices.h b/wallet/invoices.h index 00f8fa874..de825b8ab 100644 --- a/wallet/invoices.h +++ b/wallet/invoices.h @@ -29,14 +29,6 @@ struct invoices *invoices_new(const tal_t *ctx, struct log *log, struct timers *timers); -/** - * invoices_load - Second-stage constructor for invoice handler. - * Must be called before the other functions are called - * - * @invoices - the invoice handler. - */ -bool invoices_load(struct invoices *invoices); - /** * invoices_create - Create a new invoice. * diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 392a1ab32..41dcee370 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -128,9 +128,6 @@ const struct invoice_details *invoices_iterator_deref( const tal_t *ctx UNNEEDED, struct invoices *invoices UNNEEDED, const struct invoice_iterator *it UNNEEDED) { fprintf(stderr, "invoices_iterator_deref called!\n"); abort(); } -/* Generated stub for invoices_load */ -bool invoices_load(struct invoices *invoices UNNEEDED) -{ fprintf(stderr, "invoices_load called!\n"); abort(); } /* Generated stub for invoices_new */ struct invoices *invoices_new(const tal_t *ctx UNNEEDED, struct db *db UNNEEDED, diff --git a/wallet/wallet.c b/wallet/wallet.c index abef40eca..0f6b350f8 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -52,10 +52,10 @@ struct wallet *wallet_new(struct lightningd *ld, wallet->db = db_setup(wallet, log); wallet->log = log; wallet->bip32_base = NULL; - wallet->invoices = invoices_new(wallet, wallet->db, log, timers); list_head_init(&wallet->unstored_payments); db_begin_transaction(wallet->db); + wallet->invoices = invoices_new(wallet, wallet->db, log, timers); outpointfilters_init(wallet); db_commit_transaction(wallet->db); return wallet; @@ -1422,12 +1422,6 @@ bool wallet_htlcs_reconnect(struct wallet *wallet, return true; } -/* Almost all wallet_invoice_* functions delegate to the - * appropriate invoices_* function. */ -bool wallet_invoice_load(struct wallet *wallet) -{ - return invoices_load(wallet->invoices); -} bool wallet_invoice_create(struct wallet *wallet, struct invoice *pinvoice, u64 *msatoshi TAKES, diff --git a/wallet/wallet.h b/wallet/wallet.h index 5c1f56b09..d93795be2 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -565,20 +565,6 @@ struct invoice { #define INVOICE_MAX_LABEL_LEN 128 -/** - * wallet_invoice_load - Load the invoices from the database - * - * @wallet - the wallet whose invoices are to be loaded. - * - * All other wallet_invoice_* functions cannot be called - * until this function is called. - * As a database operation it must be called within - * db_begin_transaction .. db_commit_transaction - * (all other invoice functions also have this requirement). - * Returns true if loaded successfully. - */ -bool wallet_invoice_load(struct wallet *wallet); - /** * wallet_invoice_create - Create a new invoice. *