diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 22f1262c6..19402db13 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include struct invoice_waiter { @@ -87,9 +89,6 @@ static void tell_waiter(struct command *cmd, const struct invoice *paid) command_success(cmd, response); } -/* UNIFICATION FIXME */ -bool db_remove_invoice(struct lightningd *ld, const char *label); - void resolve_invoice(struct lightningd *ld, struct invoice *invoice) { struct invoice_waiter *w; @@ -265,7 +264,10 @@ static void json_delinvoice(struct command *cmd, command_fail(cmd, "Unknown invoice"); return; } - if (!db_remove_invoice(cmd->ld, i->label)) { + + if (!wallet_invoice_remove(cmd->ld->wallet, i)) { + log_broken(cmd->ld->log, "Error attempting to remove invoice %"PRIu64": %s", + i->id, cmd->ld->wallet->db->err); command_fail(cmd, "Database error"); return; } diff --git a/wallet/wallet.c b/wallet/wallet.c index 33deec9db..df1ab4c6f 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1192,3 +1192,10 @@ bool wallet_invoices_load(struct wallet *wallet, struct invoices *invs) log_debug(wallet->log, "Loaded %d invoices from DB", count); return true; } + +bool wallet_invoice_remove(struct wallet *wallet, struct invoice *inv) +{ + sqlite3_stmt *stmt = db_prepare(wallet->db, "DELETE FROM invoices WHERE id=?"); + sqlite3_bind_int64(stmt, 1, inv->id); + return db_exec_prepared(wallet->db, stmt) && sqlite3_changes(wallet->db->sql) == 1; +} diff --git a/wallet/wallet.h b/wallet/wallet.h index dbd0768e6..9220b90a3 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -323,4 +323,17 @@ bool wallet_invoice_save(struct wallet *wallet, struct invoice *inv); */ bool wallet_invoices_load(struct wallet *wallet, struct invoices *invs); +/** + * wallet_invoice_remove -- Remove the specified invoice from the wallet + * + * Remove the invoice from the underlying database. The invoice is + * identified by `inv->id` so if the caller does not have the full + * invoice, it may just instantiate a new one and set the `id` to + * match the desired invoice. + * + * @wallet: Wallet to remove from + * @inv: Invoice to remove. + */ +bool wallet_invoice_remove(struct wallet *wallet, struct invoice *inv); + #endif /* WALLET_WALLET_H */