Skip to content

Commit 65db5d5

Browse files
committed
lightningd: db migration to clean up any pending payments where theres no htlc.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 8fb0e79 commit 65db5d5

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

tests/test_wallet.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2469,7 +2469,6 @@ def test_old_htlcs_cleanup(node_factory, bitcoind):
24692469
assert l1.rpc.listhtlcs() == {'htlcs': []}
24702470

24712471

2472-
@pytest.mark.xfail(strict=True)
24732472
@unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "Makes use of the sqlite3 db")
24742473
@unittest.skipIf(TEST_NETWORK != 'regtest', "sqlite3 snapshot is regtest")
24752474
def test_pending_payments_cleanup(node_factory, bitcoind):

wallet/db.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ static void migrate_convert_old_channel_keyidx(struct lightningd *ld,
8484
struct db *db);
8585
static void migrate_initialize_channel_htlcs_wait_indexes_and_fixup_forwards(struct lightningd *ld,
8686
struct db *db);
87+
static void migrate_fail_pending_payments_without_htlcs(struct lightningd *ld,
88+
struct db *db);
8789

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

10931096
/**
@@ -2117,3 +2120,25 @@ static void migrate_convert_old_channel_keyidx(struct lightningd *ld,
21172120
db_bind_int(stmt, channel_state_in_db(CLOSED));
21182121
db_exec_prepared_v2(take(stmt));
21192122
}
2123+
2124+
static void migrate_fail_pending_payments_without_htlcs(struct lightningd *ld,
2125+
struct db *db)
2126+
{
2127+
/* If channeld died or was offline at the right moment, we
2128+
* could register a payment as pending, but then not create an
2129+
* HTLC. Clean those up. */
2130+
struct db_stmt *stmt;
2131+
2132+
stmt = db_prepare_v2(db, SQL("UPDATE payments AS p"
2133+
" SET status = ?"
2134+
" WHERE p.status = ?"
2135+
" AND NOT EXISTS ("
2136+
" SELECT 1"
2137+
" FROM channel_htlcs AS h"
2138+
" WHERE h.payment_hash = p.payment_hash"
2139+
" AND h.groupid = p.groupid"
2140+
" AND h.partid = p.partid);"));
2141+
db_bind_int(stmt, payment_status_in_db(PAYMENT_FAILED));
2142+
db_bind_int(stmt, payment_status_in_db(PAYMENT_PENDING));
2143+
db_exec_prepared_v2(take(stmt));
2144+
}

0 commit comments

Comments
 (0)