Browse Source

wallet: do wallet_invoice init during preparation.

We have a transaction anyway, and it's simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 6 years ago
parent
commit
3e53a63cf2
  1. 5
      lightningd/lightningd.c
  2. 3
      lightningd/test/run-find_my_path.c
  3. 46
      wallet/invoices.c
  4. 8
      wallet/invoices.h
  5. 3
      wallet/test/run-wallet.c
  6. 8
      wallet/wallet.c
  7. 14
      wallet/wallet.h

5
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,

3
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)

46
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,

8
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.
*

3
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,

8
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,

14
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.
*

Loading…
Cancel
Save