@@ -84,6 +84,8 @@ static void migrate_convert_old_channel_keyidx(struct lightningd *ld,
8484 struct db * db );
8585static 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