@ -2,6 +2,7 @@
# include "wallet.h"
# include <bitcoin/script.h>
# include <ccan/structeq/structeq.h>
# include <ccan/tal/str/str.h>
# include <inttypes.h>
# include <lightningd/invoice.h>
@ -22,6 +23,7 @@ struct wallet *wallet_new(const tal_t *ctx, struct log *log)
wallet - > log = log ;
wallet - > bip32_base = NULL ;
wallet - > invoices = invoices_new ( wallet , wallet - > db , log ) ;
list_head_init ( & wallet - > unstored_payments ) ;
return wallet ;
}
@ -1056,8 +1058,6 @@ static bool wallet_stmt2htlc_out(const struct wallet_channel *channel,
* htlcs , will wire using origin_htlc_id */
out - > in = NULL ;
out - > payment = NULL ;
return ok ;
}
@ -1243,10 +1243,38 @@ struct htlc_stub *wallet_htlc_stubs(const tal_t *ctx, struct wallet *wallet,
return stubs ;
}
bool wallet_payment_add ( struct wallet * wallet ,
struct wallet_payment * payment )
static struct wallet_payment *
find_unstored_payment ( struct wallet * wallet , const struct sha256 * payment_hash )
{
struct wallet_payment * i ;
list_for_each ( & wallet - > unstored_payments , i , list ) {
if ( structeq ( payment_hash , & i - > payment_hash ) )
return i ;
}
return NULL ;
}
static void remove_unstored_payment ( struct wallet_payment * payment )
{
list_del ( & payment - > list ) ;
}
void wallet_payment_setup ( struct wallet * wallet , struct wallet_payment * payment )
{
assert ( ! find_unstored_payment ( wallet , & payment - > payment_hash ) ) ;
list_add_tail ( & wallet - > unstored_payments , & payment - > list ) ;
tal_add_destructor ( payment , remove_unstored_payment ) ;
}
void wallet_payment_store ( struct wallet * wallet ,
const struct sha256 * payment_hash )
{
sqlite3_stmt * stmt ;
struct wallet_payment * payment ;
payment = find_unstored_payment ( wallet , payment_hash ) ;
/* Don't attempt to add the same payment twice */
assert ( ! payment - > id ) ;
@ -1268,14 +1296,21 @@ bool wallet_payment_add(struct wallet *wallet,
sqlite3_bind_int ( stmt , 5 , payment - > timestamp ) ;
db_exec_prepared ( wallet - > db , stmt ) ;
payment - > id = sqlite3_last_insert_rowid ( wallet - > db - > sql ) ;
return true ;
tal_free ( payment ) ;
}
void wallet_payment_delete ( struct wallet * wallet ,
const struct sha256 * payment_hash )
{
sqlite3_stmt * stmt ;
struct wallet_payment * payment ;
payment = find_unstored_payment ( wallet , payment_hash ) ;
if ( payment ) {
tal_free ( payment ) ;
return ;
}
stmt = db_prepare (
wallet - > db ,
@ -1314,7 +1349,12 @@ wallet_payment_by_hash(const tal_t *ctx, struct wallet *wallet,
const struct sha256 * payment_hash )
{
sqlite3_stmt * stmt ;
struct wallet_payment * payment = NULL ;
struct wallet_payment * payment ;
/* Present the illusion that it's in the db... */
payment = find_unstored_payment ( wallet , payment_hash ) ;
if ( payment )
return payment ;
stmt = db_prepare ( wallet - > db ,
" SELECT id, status, destination, "
@ -1358,6 +1398,9 @@ void wallet_payment_set_status(struct wallet *wallet,
{
sqlite3_stmt * stmt ;
/* We should never try this on an unstored payment! */
assert ( ! find_unstored_payment ( wallet , payment_hash ) ) ;
stmt = db_prepare ( wallet - > db ,
" UPDATE payments SET status=? "
" WHERE payment_hash=? " ) ;
@ -1382,6 +1425,8 @@ const struct wallet_payment **wallet_payment_list(const tal_t *ctx,
{
const struct wallet_payment * * payments ;
sqlite3_stmt * stmt ;
struct wallet_payment * p ;
size_t i ;
payments = tal_arr ( ctx , const struct wallet_payment * , 0 ) ;
stmt = db_prepare (
@ -1391,12 +1436,18 @@ const struct wallet_payment **wallet_payment_list(const tal_t *ctx,
" path_secrets "
" FROM payments; " ) ;
for ( int i = 0 ; sqlite3_step ( stmt ) = = SQLITE_ROW ; i + + ) {
for ( i = 0 ; sqlite3_step ( stmt ) = = SQLITE_ROW ; i + + ) {
tal_resize ( & payments , i + 1 ) ;
payments [ i ] = wallet_stmt2payment ( payments , stmt ) ;
}
sqlite3_finalize ( stmt ) ;
/* Now attach payments not yet in db. */
list_for_each ( & wallet - > unstored_payments , p , list ) {
tal_resize ( & payments , i + 1 ) ;
payments [ i + + ] = p ;
}
return payments ;
}