Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2467,3 +2467,12 @@ def test_old_htlcs_cleanup(node_factory, bitcoind):
# Now they're not
assert l1.db_query('SELECT COUNT(*) as c FROM channel_htlcs')[0]['c'] == 0
assert l1.rpc.listhtlcs() == {'htlcs': []}


@unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "Makes use of the sqlite3 db")
@unittest.skipIf(TEST_NETWORK != 'regtest', "sqlite3 snapshot is regtest")
def test_pending_payments_cleanup(node_factory, bitcoind):
bitcoind.generate_block(1)
l1 = node_factory.get_node(dbfile='l1-pending-sendpays-with-no-htlc.sqlite3.xz', options={'database-upgrade': True})
assert [p['status'] for p in l1.rpc.listsendpays()['payments']] == ['failed', 'pending']
assert [p['status'] for p in l1.rpc.listpays()['pays']] == ['pending']
27 changes: 26 additions & 1 deletion wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ static void migrate_convert_old_channel_keyidx(struct lightningd *ld,
struct db *db);
static void migrate_initialize_channel_htlcs_wait_indexes_and_fixup_forwards(struct lightningd *ld,
struct db *db);
static void migrate_fail_pending_payments_without_htlcs(struct lightningd *ld,
struct db *db);

/* Do not reorder or remove elements from this array, it is used to
* migrate existing databases from a previous state, based on the
Expand Down Expand Up @@ -1087,7 +1089,8 @@ static struct migration dbmigrations[] = {
{SQL("CREATE INDEX chain_moves_utxo_idx ON chain_moves (utxo)"), NULL},
{NULL, migrate_from_account_db},
/* We accidentally allowed duplicate entries */
{NULL, migrate_remove_chain_moves_duplicates}
{NULL, migrate_remove_chain_moves_duplicates},
{NULL, migrate_fail_pending_payments_without_htlcs},
};

/**
Expand Down Expand Up @@ -2117,3 +2120,25 @@ static void migrate_convert_old_channel_keyidx(struct lightningd *ld,
db_bind_int(stmt, channel_state_in_db(CLOSED));
db_exec_prepared_v2(take(stmt));
}

static void migrate_fail_pending_payments_without_htlcs(struct lightningd *ld,
struct db *db)
{
/* If channeld died or was offline at the right moment, we
* could register a payment as pending, but then not create an
* HTLC. Clean those up. */
struct db_stmt *stmt;

stmt = db_prepare_v2(db, SQL("UPDATE payments AS p"
" SET status = ?"
" WHERE p.status = ?"
" AND NOT EXISTS ("
" SELECT 1"
" FROM channel_htlcs AS h"
" WHERE h.payment_hash = p.payment_hash"
" AND h.groupid = p.groupid"
" AND h.partid = p.partid);"));
db_bind_int(stmt, payment_status_in_db(PAYMENT_FAILED));
db_bind_int(stmt, payment_status_in_db(PAYMENT_PENDING));
db_exec_prepared_v2(take(stmt));
}
Loading