mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-01 17:47:30 +01:00
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>
This commit is contained in:
parent
2af94f1817
commit
3e53a63cf2
7 changed files with 20 additions and 67 deletions
|
@ -361,11 +361,6 @@ int main(int argc, char *argv[])
|
||||||
/* Initialize the transaction filter with our pubkeys. */
|
/* Initialize the transaction filter with our pubkeys. */
|
||||||
init_txfilter(ld->wallet, ld->owned_txfilter);
|
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. */
|
/* Set up invoice autoclean. */
|
||||||
wallet_invoice_autoclean(ld->wallet,
|
wallet_invoice_autoclean(ld->wallet,
|
||||||
ld->ini_autocleaninvoice_cycle,
|
ld->ini_autocleaninvoice_cycle,
|
||||||
|
|
|
@ -145,9 +145,6 @@ void wallet_invoice_autoclean(struct wallet * wallet UNNEEDED,
|
||||||
u64 cycle_seconds UNNEEDED,
|
u64 cycle_seconds UNNEEDED,
|
||||||
u64 expired_by UNNEEDED)
|
u64 expired_by UNNEEDED)
|
||||||
{ fprintf(stderr, "wallet_invoice_autoclean called!\n"); abort(); }
|
{ 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 */
|
/* Generated stub for wallet_network_check */
|
||||||
bool wallet_network_check(struct wallet *w UNNEEDED,
|
bool wallet_network_check(struct wallet *w UNNEEDED,
|
||||||
const struct chainparams *chainparams UNNEEDED)
|
const struct chainparams *chainparams UNNEEDED)
|
||||||
|
|
|
@ -128,6 +128,23 @@ static struct invoice_details *wallet_stmt2invoice_details(const tal_t *ctx,
|
||||||
return dtl;
|
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 invoices *invoices_new(const tal_t *ctx,
|
||||||
struct db *db,
|
struct db *db,
|
||||||
struct log *log,
|
struct log *log,
|
||||||
|
@ -144,30 +161,16 @@ struct invoices *invoices_new(const tal_t *ctx,
|
||||||
invs->expiration_timer = NULL;
|
invs->expiration_timer = NULL;
|
||||||
invs->autoclean_timer = NULL;
|
invs->autoclean_timer = NULL;
|
||||||
|
|
||||||
|
update_db_expirations(invs, time_now().ts.tv_sec);
|
||||||
|
install_expiration_timer(invs);
|
||||||
return 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 invoice_id_node {
|
||||||
struct list_node list;
|
struct list_node list;
|
||||||
u64 id;
|
u64 id;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void install_expiration_timer(struct invoices *invoices);
|
|
||||||
static void trigger_expiration(struct invoices *invoices)
|
static void trigger_expiration(struct invoices *invoices)
|
||||||
{
|
{
|
||||||
struct list_head idlist;
|
struct list_head idlist;
|
||||||
|
@ -254,17 +257,6 @@ static void install_expiration_timer(struct invoices *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,
|
bool invoices_create(struct invoices *invoices,
|
||||||
struct invoice *pinvoice,
|
struct invoice *pinvoice,
|
||||||
u64 *msatoshi TAKES,
|
u64 *msatoshi TAKES,
|
||||||
|
|
|
@ -29,14 +29,6 @@ struct invoices *invoices_new(const tal_t *ctx,
|
||||||
struct log *log,
|
struct log *log,
|
||||||
struct timers *timers);
|
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.
|
* invoices_create - Create a new invoice.
|
||||||
*
|
*
|
||||||
|
|
|
@ -128,9 +128,6 @@ const struct invoice_details *invoices_iterator_deref(
|
||||||
const tal_t *ctx UNNEEDED, struct invoices *invoices UNNEEDED,
|
const tal_t *ctx UNNEEDED, struct invoices *invoices UNNEEDED,
|
||||||
const struct invoice_iterator *it UNNEEDED)
|
const struct invoice_iterator *it UNNEEDED)
|
||||||
{ fprintf(stderr, "invoices_iterator_deref called!\n"); abort(); }
|
{ 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 */
|
/* Generated stub for invoices_new */
|
||||||
struct invoices *invoices_new(const tal_t *ctx UNNEEDED,
|
struct invoices *invoices_new(const tal_t *ctx UNNEEDED,
|
||||||
struct db *db UNNEEDED,
|
struct db *db UNNEEDED,
|
||||||
|
|
|
@ -52,10 +52,10 @@ struct wallet *wallet_new(struct lightningd *ld,
|
||||||
wallet->db = db_setup(wallet, log);
|
wallet->db = db_setup(wallet, log);
|
||||||
wallet->log = log;
|
wallet->log = log;
|
||||||
wallet->bip32_base = NULL;
|
wallet->bip32_base = NULL;
|
||||||
wallet->invoices = invoices_new(wallet, wallet->db, log, timers);
|
|
||||||
list_head_init(&wallet->unstored_payments);
|
list_head_init(&wallet->unstored_payments);
|
||||||
|
|
||||||
db_begin_transaction(wallet->db);
|
db_begin_transaction(wallet->db);
|
||||||
|
wallet->invoices = invoices_new(wallet, wallet->db, log, timers);
|
||||||
outpointfilters_init(wallet);
|
outpointfilters_init(wallet);
|
||||||
db_commit_transaction(wallet->db);
|
db_commit_transaction(wallet->db);
|
||||||
return wallet;
|
return wallet;
|
||||||
|
@ -1422,12 +1422,6 @@ bool wallet_htlcs_reconnect(struct wallet *wallet,
|
||||||
return true;
|
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,
|
bool wallet_invoice_create(struct wallet *wallet,
|
||||||
struct invoice *pinvoice,
|
struct invoice *pinvoice,
|
||||||
u64 *msatoshi TAKES,
|
u64 *msatoshi TAKES,
|
||||||
|
|
|
@ -565,20 +565,6 @@ struct invoice {
|
||||||
|
|
||||||
#define INVOICE_MAX_LABEL_LEN 128
|
#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.
|
* wallet_invoice_create - Create a new invoice.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue