Skip to content

Commit 3e53a63

Browse files
committed
wallet: do wallet_invoice init during preparation.
We have a transaction anyway, and it's simpler. Signed-off-by: Rusty Russell <[email protected]>
1 parent 2af94f1 commit 3e53a63

File tree

7 files changed

+20
-67
lines changed

7 files changed

+20
-67
lines changed

lightningd/lightningd.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,6 @@ int main(int argc, char *argv[])
361361
/* Initialize the transaction filter with our pubkeys. */
362362
init_txfilter(ld->wallet, ld->owned_txfilter);
363363

364-
/* Check invoices loaded from the database */
365-
if (!wallet_invoice_load(ld->wallet)) {
366-
fatal("Could not load invoices from the database");
367-
}
368-
369364
/* Set up invoice autoclean. */
370365
wallet_invoice_autoclean(ld->wallet,
371366
ld->ini_autocleaninvoice_cycle,

lightningd/test/run-find_my_path.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,6 @@ void wallet_invoice_autoclean(struct wallet * wallet UNNEEDED,
145145
u64 cycle_seconds UNNEEDED,
146146
u64 expired_by UNNEEDED)
147147
{ fprintf(stderr, "wallet_invoice_autoclean called!\n"); abort(); }
148-
/* Generated stub for wallet_invoice_load */
149-
bool wallet_invoice_load(struct wallet *wallet UNNEEDED)
150-
{ fprintf(stderr, "wallet_invoice_load called!\n"); abort(); }
151148
/* Generated stub for wallet_network_check */
152149
bool wallet_network_check(struct wallet *w UNNEEDED,
153150
const struct chainparams *chainparams UNNEEDED)

wallet/invoices.c

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,23 @@ static struct invoice_details *wallet_stmt2invoice_details(const tal_t *ctx,
128128
return dtl;
129129
}
130130

131+
/* Update expirations. */
132+
static void update_db_expirations(struct invoices *invoices, u64 now)
133+
{
134+
sqlite3_stmt *stmt;
135+
stmt = db_prepare(invoices->db,
136+
"UPDATE invoices"
137+
" SET state = ?"
138+
" WHERE state = ?"
139+
" AND expiry_time <= ?;");
140+
sqlite3_bind_int(stmt, 1, EXPIRED);
141+
sqlite3_bind_int(stmt, 2, UNPAID);
142+
sqlite3_bind_int64(stmt, 3, now);
143+
db_exec_prepared(invoices->db, stmt);
144+
}
145+
146+
static void install_expiration_timer(struct invoices *invoices);
147+
131148
struct invoices *invoices_new(const tal_t *ctx,
132149
struct db *db,
133150
struct log *log,
@@ -144,30 +161,16 @@ struct invoices *invoices_new(const tal_t *ctx,
144161
invs->expiration_timer = NULL;
145162
invs->autoclean_timer = NULL;
146163

164+
update_db_expirations(invs, time_now().ts.tv_sec);
165+
install_expiration_timer(invs);
147166
return invs;
148167
}
149168

150-
/* Update expirations. */
151-
static void update_db_expirations(struct invoices *invoices, u64 now)
152-
{
153-
sqlite3_stmt *stmt;
154-
stmt = db_prepare(invoices->db,
155-
"UPDATE invoices"
156-
" SET state = ?"
157-
" WHERE state = ?"
158-
" AND expiry_time <= ?;");
159-
sqlite3_bind_int(stmt, 1, EXPIRED);
160-
sqlite3_bind_int(stmt, 2, UNPAID);
161-
sqlite3_bind_int64(stmt, 3, now);
162-
db_exec_prepared(invoices->db, stmt);
163-
}
164-
165169
struct invoice_id_node {
166170
struct list_node list;
167171
u64 id;
168172
};
169173

170-
static void install_expiration_timer(struct invoices *invoices);
171174
static void trigger_expiration(struct invoices *invoices)
172175
{
173176
struct list_head idlist;
@@ -254,17 +257,6 @@ static void install_expiration_timer(struct invoices *invoices)
254257
invoices);
255258
}
256259

257-
bool invoices_load(struct invoices *invoices)
258-
{
259-
u64 now = time_now().ts.tv_sec;
260-
261-
update_db_expirations(invoices, now);
262-
263-
install_expiration_timer(invoices);
264-
265-
return true;
266-
}
267-
268260
bool invoices_create(struct invoices *invoices,
269261
struct invoice *pinvoice,
270262
u64 *msatoshi TAKES,

wallet/invoices.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ struct invoices *invoices_new(const tal_t *ctx,
2929
struct log *log,
3030
struct timers *timers);
3131

32-
/**
33-
* invoices_load - Second-stage constructor for invoice handler.
34-
* Must be called before the other functions are called
35-
*
36-
* @invoices - the invoice handler.
37-
*/
38-
bool invoices_load(struct invoices *invoices);
39-
4032
/**
4133
* invoices_create - Create a new invoice.
4234
*

wallet/test/run-wallet.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@ const struct invoice_details *invoices_iterator_deref(
128128
const tal_t *ctx UNNEEDED, struct invoices *invoices UNNEEDED,
129129
const struct invoice_iterator *it UNNEEDED)
130130
{ fprintf(stderr, "invoices_iterator_deref called!\n"); abort(); }
131-
/* Generated stub for invoices_load */
132-
bool invoices_load(struct invoices *invoices UNNEEDED)
133-
{ fprintf(stderr, "invoices_load called!\n"); abort(); }
134131
/* Generated stub for invoices_new */
135132
struct invoices *invoices_new(const tal_t *ctx UNNEEDED,
136133
struct db *db UNNEEDED,

wallet/wallet.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ struct wallet *wallet_new(struct lightningd *ld,
5252
wallet->db = db_setup(wallet, log);
5353
wallet->log = log;
5454
wallet->bip32_base = NULL;
55-
wallet->invoices = invoices_new(wallet, wallet->db, log, timers);
5655
list_head_init(&wallet->unstored_payments);
5756

5857
db_begin_transaction(wallet->db);
58+
wallet->invoices = invoices_new(wallet, wallet->db, log, timers);
5959
outpointfilters_init(wallet);
6060
db_commit_transaction(wallet->db);
6161
return wallet;
@@ -1422,12 +1422,6 @@ bool wallet_htlcs_reconnect(struct wallet *wallet,
14221422
return true;
14231423
}
14241424

1425-
/* Almost all wallet_invoice_* functions delegate to the
1426-
* appropriate invoices_* function. */
1427-
bool wallet_invoice_load(struct wallet *wallet)
1428-
{
1429-
return invoices_load(wallet->invoices);
1430-
}
14311425
bool wallet_invoice_create(struct wallet *wallet,
14321426
struct invoice *pinvoice,
14331427
u64 *msatoshi TAKES,

wallet/wallet.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -565,20 +565,6 @@ struct invoice {
565565

566566
#define INVOICE_MAX_LABEL_LEN 128
567567

568-
/**
569-
* wallet_invoice_load - Load the invoices from the database
570-
*
571-
* @wallet - the wallet whose invoices are to be loaded.
572-
*
573-
* All other wallet_invoice_* functions cannot be called
574-
* until this function is called.
575-
* As a database operation it must be called within
576-
* db_begin_transaction .. db_commit_transaction
577-
* (all other invoice functions also have this requirement).
578-
* Returns true if loaded successfully.
579-
*/
580-
bool wallet_invoice_load(struct wallet *wallet);
581-
582568
/**
583569
* wallet_invoice_create - Create a new invoice.
584570
*

0 commit comments

Comments
 (0)