@@ -86,6 +86,8 @@ static void migrate_initialize_alias_local(struct lightningd *ld,
8686 struct db * db );
8787static void insert_addrtype_to_addresses (struct lightningd * ld ,
8888 struct db * db );
89+ static void migrate_convert_old_channel_keyidx (struct lightningd * ld ,
90+ struct db * db );
8991
9092/* Do not reorder or remove elements from this array, it is used to
9193 * migrate existing databases from a previous state, based on the
@@ -1030,6 +1032,7 @@ static struct migration dbmigrations[] = {
10301032 {SQL ("ALTER TABLE channel_funding_inflights ADD remote_funding BLOB DEFAULT NULL;" ), NULL },
10311033 {SQL ("ALTER TABLE peers ADD last_known_address BLOB DEFAULT NULL;" ), NULL },
10321034 {SQL ("ALTER TABLE channels ADD close_attempt_height INTEGER DEFAULT 0;" ), NULL },
1035+ {NULL , migrate_convert_old_channel_keyidx },
10331036};
10341037
10351038/**
@@ -2025,3 +2028,28 @@ static void insert_addrtype_to_addresses(struct lightningd *ld,
20252028 tal_free (stmt );
20262029 }
20272030}
2031+
2032+ /* If we said a channel final key was taproot-only, but actually the peer
2033+ * didn't support `option_shutdown_anysegwit`, we used the p2wpkh instead. We
2034+ * don't have access to the peers' features in the db, so instead convert all
2035+ * the keys to ADDR_ALL. Users with closed channels may still need to
2036+ * rescan! */
2037+ static void migrate_convert_old_channel_keyidx (struct lightningd * ld ,
2038+ struct db * db )
2039+ {
2040+ struct db_stmt * stmt ;
2041+
2042+ stmt = db_prepare_v2 (db , SQL ("UPDATE addresses"
2043+ " SET addrtype = ?"
2044+ " FROM channels "
2045+ " WHERE addresses.keyidx = channels.shutdown_keyidx_local"
2046+ " AND channels.state != ?"
2047+ " AND channels.state != ?"
2048+ " AND channels.state != ?" ));
2049+ db_bind_int (stmt , wallet_addrtype_in_db (ADDR_ALL ));
2050+ /* If we might have already seen onchain funds, we need to rescan */
2051+ db_bind_int (stmt , channel_state_in_db (FUNDING_SPEND_SEEN ));
2052+ db_bind_int (stmt , channel_state_in_db (ONCHAIN ));
2053+ db_bind_int (stmt , channel_state_in_db (CLOSED ));
2054+ db_exec_prepared_v2 (take (stmt ));
2055+ }
0 commit comments