diff --git a/tests/data/last_tx_closed.sqlite3.xz b/tests/data/last_tx_closed.sqlite3.xz new file mode 100644 index 000000000..16a0a1890 Binary files /dev/null and b/tests/data/last_tx_closed.sqlite3.xz differ diff --git a/tests/test_db.py b/tests/test_db.py index e4e3a6199..8ac2ced78 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -165,6 +165,20 @@ def test_last_tx_psbt_upgrade(node_factory, bitcoind): assert funding_input['witness_utxo']['scriptPubKey']['type'] == 'witness_v0_scripthash' assert funding_input['witness_script']['type'] == 'multisig' + l1.stop() + # Test again, but this time with a database with a closed channel + forgotten peer + # We need to get to block #232 from block #113 + bitcoind.generate_block(232 - 113) + # We need to give it a chance to update + time.sleep(2) + + l2 = node_factory.get_node(dbfile='last_tx_closed.sqlite3.xz') + last_txs = [x['last_tx'] for x in l2.db_query('SELECT last_tx FROM channels ORDER BY id;')] + + # The first tx should be psbt, the second should still be hex + bitcoind.rpc.decodepsbt(base64.b64encode(last_txs[0]).decode('utf-8')) + bitcoind.rpc.decoderawtransaction(last_txs[1].hex()) + @unittest.skipIf(VALGRIND and not DEVELOPER, "Without developer valgrind will complain about debug symbols missing") def test_optimistic_locking(node_factory, bitcoind): diff --git a/wallet/db.c b/wallet/db.c index b5eb29c5e..2a93167da 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -1150,6 +1150,11 @@ void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db) last_tx = db_column_tx(stmt, stmt, 2); assert(last_tx != NULL); + /* If we've forgotten about the peer_id + * because we closed / forgot the channel, + * we can skip this. */ + if (db_column_is_null(stmt, 1)) + continue; db_column_node_id(stmt, 1, &peer_id); db_column_amount_sat(stmt, 3, &funding_sat); db_column_pubkey(stmt, 4, &remote_funding_pubkey);