mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 09:54:16 +01:00
invoices: keep wallet pointer.
We need this to access ->ld later. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
5132fea44e
commit
1c846461ca
@ -25,7 +25,7 @@ struct invoice_waiter {
|
|||||||
|
|
||||||
struct invoices {
|
struct invoices {
|
||||||
/* The database connection to use. */
|
/* The database connection to use. */
|
||||||
struct db *db;
|
struct wallet *wallet;
|
||||||
/* The timers object to use for expirations. */
|
/* The timers object to use for expirations. */
|
||||||
struct timers *timers;
|
struct timers *timers;
|
||||||
/* Waiters waiting for invoices to be paid, expired, or deleted. */
|
/* 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)
|
static void update_db_expirations(struct invoices *invoices, u64 now)
|
||||||
{
|
{
|
||||||
struct db_stmt *stmt;
|
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 = ?"
|
" SET state = ?"
|
||||||
" WHERE state = ?"
|
" WHERE state = ?"
|
||||||
" AND expiry_time <= ?;"));
|
" 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);
|
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 wallet *wallet,
|
||||||
struct timers *timers)
|
struct timers *timers)
|
||||||
{
|
{
|
||||||
struct invoices *invs = tal(ctx, struct invoices);
|
struct invoices *invs = tal(ctx, struct invoices);
|
||||||
|
|
||||||
invs->db = db;
|
invs->wallet = wallet;
|
||||||
invs->timers = timers;
|
invs->timers = timers;
|
||||||
|
|
||||||
list_head_init(&invs->waiters);
|
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 */
|
/* Acquire all expired invoices and save them in a list */
|
||||||
list_head_init(&idlist);
|
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"
|
" FROM invoices"
|
||||||
" WHERE state = ?"
|
" WHERE state = ?"
|
||||||
" AND expiry_time <= ?"));
|
" AND expiry_time <= ?"));
|
||||||
@ -205,7 +205,7 @@ static void install_expiration_timer(struct invoices *invoices)
|
|||||||
assert(!invoices->expiration_timer);
|
assert(!invoices->expiration_timer);
|
||||||
|
|
||||||
/* Find unpaid invoice with nearest expiry time */
|
/* 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"
|
" FROM invoices"
|
||||||
" WHERE state = ?;"));
|
" WHERE state = ?;"));
|
||||||
db_bind_int(stmt, 0, UNPAID);
|
db_bind_int(stmt, 0, UNPAID);
|
||||||
@ -277,7 +277,7 @@ bool invoices_create(struct invoices *invoices,
|
|||||||
|
|
||||||
/* Save to database. */
|
/* Save to database. */
|
||||||
stmt = db_prepare_v2(
|
stmt = db_prepare_v2(
|
||||||
invoices->db,
|
invoices->wallet->db,
|
||||||
SQL("INSERT INTO invoices"
|
SQL("INSERT INTO invoices"
|
||||||
" ( payment_hash, payment_key, state"
|
" ( payment_hash, payment_key, state"
|
||||||
" , msatoshi, label, expiry_time"
|
" , msatoshi, label, expiry_time"
|
||||||
@ -333,7 +333,7 @@ bool invoices_find_by_label(struct invoices *invoices,
|
|||||||
const struct json_escape *label)
|
const struct json_escape *label)
|
||||||
{
|
{
|
||||||
struct db_stmt *stmt;
|
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"
|
" FROM invoices"
|
||||||
" WHERE label = ?;"));
|
" WHERE label = ?;"));
|
||||||
db_bind_json_escape(stmt, 0, label);
|
db_bind_json_escape(stmt, 0, label);
|
||||||
@ -355,7 +355,7 @@ bool invoices_find_by_rhash(struct invoices *invoices,
|
|||||||
{
|
{
|
||||||
struct db_stmt *stmt;
|
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"
|
" FROM invoices"
|
||||||
" WHERE payment_hash = ?;"));
|
" WHERE payment_hash = ?;"));
|
||||||
db_bind_sha256(stmt, 0, rhash);
|
db_bind_sha256(stmt, 0, rhash);
|
||||||
@ -376,7 +376,7 @@ bool invoices_find_unpaid(struct invoices *invoices,
|
|||||||
const struct sha256 *rhash)
|
const struct sha256 *rhash)
|
||||||
{
|
{
|
||||||
struct db_stmt *stmt;
|
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"
|
" FROM invoices"
|
||||||
" WHERE payment_hash = ?"
|
" WHERE payment_hash = ?"
|
||||||
" AND state = ?;"));
|
" AND state = ?;"));
|
||||||
@ -399,7 +399,7 @@ bool invoices_delete(struct invoices *invoices, struct invoice invoice)
|
|||||||
struct db_stmt *stmt;
|
struct db_stmt *stmt;
|
||||||
int changes;
|
int changes;
|
||||||
/* Delete from database. */
|
/* Delete from database. */
|
||||||
stmt = db_prepare_v2(invoices->db,
|
stmt = db_prepare_v2(invoices->wallet->db,
|
||||||
SQL("DELETE FROM invoices WHERE id=?;"));
|
SQL("DELETE FROM invoices WHERE id=?;"));
|
||||||
db_bind_u64(stmt, 0, invoice.id);
|
db_bind_u64(stmt, 0, invoice.id);
|
||||||
db_exec_prepared_v2(stmt);
|
db_exec_prepared_v2(stmt);
|
||||||
@ -420,7 +420,7 @@ bool invoices_delete_description(struct invoices *invoices, struct invoice invoi
|
|||||||
struct db_stmt *stmt;
|
struct db_stmt *stmt;
|
||||||
int changes;
|
int changes;
|
||||||
|
|
||||||
stmt = db_prepare_v2(invoices->db, SQL("UPDATE invoices"
|
stmt = db_prepare_v2(invoices->wallet->db, SQL("UPDATE invoices"
|
||||||
" SET description = NULL"
|
" SET description = NULL"
|
||||||
" WHERE ID = ?;"));
|
" WHERE ID = ?;"));
|
||||||
db_bind_u64(stmt, 0, invoice.id);
|
db_bind_u64(stmt, 0, invoice.id);
|
||||||
@ -436,7 +436,7 @@ void invoices_delete_expired(struct invoices *invoices,
|
|||||||
u64 max_expiry_time)
|
u64 max_expiry_time)
|
||||||
{
|
{
|
||||||
struct db_stmt *stmt;
|
struct db_stmt *stmt;
|
||||||
stmt = db_prepare_v2(invoices->db, SQL(
|
stmt = db_prepare_v2(invoices->wallet->db, SQL(
|
||||||
"DELETE FROM invoices"
|
"DELETE FROM invoices"
|
||||||
" WHERE state = ?"
|
" WHERE state = ?"
|
||||||
" AND expiry_time <= ?;"));
|
" AND expiry_time <= ?;"));
|
||||||
@ -451,7 +451,7 @@ bool invoices_iterate(struct invoices *invoices,
|
|||||||
struct db_stmt *stmt;
|
struct db_stmt *stmt;
|
||||||
|
|
||||||
if (!it->p) {
|
if (!it->p) {
|
||||||
stmt = db_prepare_v2(invoices->db, SQL("SELECT"
|
stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT"
|
||||||
" state"
|
" state"
|
||||||
", payment_key"
|
", payment_key"
|
||||||
", payment_hash"
|
", payment_hash"
|
||||||
@ -509,7 +509,7 @@ static enum invoice_status invoice_get_status(struct invoices *invoices, struct
|
|||||||
bool res;
|
bool res;
|
||||||
|
|
||||||
stmt = db_prepare_v2(
|
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_bind_u64(stmt, 0, invoice.id);
|
||||||
db_query_prepared(stmt);
|
db_query_prepared(stmt);
|
||||||
|
|
||||||
@ -555,11 +555,11 @@ bool invoices_resolve(struct invoices *invoices,
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* Assign a pay-index. */
|
/* 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;
|
paid_timestamp = time_now().ts.tv_sec;
|
||||||
|
|
||||||
/* Update database. */
|
/* Update database. */
|
||||||
stmt = db_prepare_v2(invoices->db, SQL("UPDATE invoices"
|
stmt = db_prepare_v2(invoices->wallet->db, SQL("UPDATE invoices"
|
||||||
" SET state=?"
|
" SET state=?"
|
||||||
" , pay_index=?"
|
" , pay_index=?"
|
||||||
" , msatoshi_received=?"
|
" , msatoshi_received=?"
|
||||||
@ -572,7 +572,7 @@ bool invoices_resolve(struct invoices *invoices,
|
|||||||
db_bind_u64(stmt, 4, invoice.id);
|
db_bind_u64(stmt, 4, invoice.id);
|
||||||
db_exec_prepared_v2(take(stmt));
|
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. */
|
/* Tell all the waiters about the paid invoice. */
|
||||||
trigger_invoice_waiter_resolve(invoices, invoice.id, &invoice);
|
trigger_invoice_waiter_resolve(invoices, invoice.id, &invoice);
|
||||||
@ -617,7 +617,7 @@ void invoices_waitany(const tal_t *ctx,
|
|||||||
struct invoice invoice;
|
struct invoice invoice;
|
||||||
|
|
||||||
/* Look for an already-paid invoice. */
|
/* Look for an already-paid invoice. */
|
||||||
stmt = db_prepare_v2(invoices->db,
|
stmt = db_prepare_v2(invoices->wallet->db,
|
||||||
SQL("SELECT id"
|
SQL("SELECT id"
|
||||||
" FROM invoices"
|
" FROM invoices"
|
||||||
" WHERE pay_index IS NOT NULL"
|
" WHERE pay_index IS NOT NULL"
|
||||||
@ -667,7 +667,7 @@ struct invoice_details *invoices_get_details(const tal_t *ctx,
|
|||||||
bool res;
|
bool res;
|
||||||
struct invoice_details *details;
|
struct invoice_details *details;
|
||||||
|
|
||||||
stmt = db_prepare_v2(invoices->db, SQL("SELECT"
|
stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT"
|
||||||
" state"
|
" state"
|
||||||
", payment_key"
|
", payment_key"
|
||||||
", payment_hash"
|
", payment_hash"
|
||||||
|
@ -13,16 +13,17 @@ struct invoice_iterator;
|
|||||||
struct invoices;
|
struct invoices;
|
||||||
struct sha256;
|
struct sha256;
|
||||||
struct timers;
|
struct timers;
|
||||||
|
struct wallet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* invoices_new - Constructor for a new invoice handler
|
* invoices_new - Constructor for a new invoice handler
|
||||||
*
|
*
|
||||||
* @ctx - the owner of the 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.
|
* @timers - the timers object to use for expirations.
|
||||||
*/
|
*/
|
||||||
struct invoices *invoices_new(const tal_t *ctx,
|
struct invoices *invoices_new(const tal_t *ctx,
|
||||||
struct db *db,
|
struct wallet *wallet,
|
||||||
struct timers *timers);
|
struct timers *timers);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -294,7 +294,7 @@ const struct invoice_details *invoices_iterator_deref(
|
|||||||
{ fprintf(stderr, "invoices_iterator_deref called!\n"); abort(); }
|
{ fprintf(stderr, "invoices_iterator_deref 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 wallet *wallet UNNEEDED,
|
||||||
struct timers *timers UNNEEDED)
|
struct timers *timers UNNEEDED)
|
||||||
{ fprintf(stderr, "invoices_new called!\n"); abort(); }
|
{ fprintf(stderr, "invoices_new called!\n"); abort(); }
|
||||||
/* Generated stub for invoices_resolve */
|
/* Generated stub for invoices_resolve */
|
||||||
|
@ -112,7 +112,7 @@ struct wallet *wallet_new(struct lightningd *ld, struct timers *timers)
|
|||||||
wallet->db = db_setup(wallet, ld, ld->bip32_base);
|
wallet->db = db_setup(wallet, ld, ld->bip32_base);
|
||||||
|
|
||||||
db_begin_transaction(wallet->db);
|
db_begin_transaction(wallet->db);
|
||||||
wallet->invoices = invoices_new(wallet, wallet->db, timers);
|
wallet->invoices = invoices_new(wallet, wallet, timers);
|
||||||
outpointfilters_init(wallet);
|
outpointfilters_init(wallet);
|
||||||
db_commit_transaction(wallet->db);
|
db_commit_transaction(wallet->db);
|
||||||
return wallet;
|
return wallet;
|
||||||
|
Loading…
Reference in New Issue
Block a user