test: coverage for migration failure when last sync is beyond prune height

Github-Pull: bitcoin/bitcoin#34156
Rebased-From: b7c34d08dd9549a95cffc6ec1ffa4bb4f81e35eb
This commit is contained in:
furszy 2026-01-04 12:25:21 -05:00 committed by Ava Chow
parent 86eaf71e60
commit 77622e000b

View File

@ -1088,6 +1088,29 @@ class WalletMigrationTest(BitcoinTestFramework):
wallet.unloadwallet()
def unsynced_wallet_on_pruned_node_fails(self):
self.log.info("Test migration of an unsynced wallet on a pruned node fails gracefully")
shutil.rmtree(self.nodes[0].wallets_path / "database", ignore_errors=True)
wallet = self.create_legacy_wallet("", load_on_startup=False)
last_wallet_synced_block = wallet.getwalletinfo()['lastprocessedblock']['height']
# Generate blocks just so the wallet best block is pruned
self.restart_node(0, ["-fastprune", "-prune=1", "-nowallet"])
self.generate(self.nodes[0], 450, sync_fun=self.no_op)
self.nodes[0].pruneblockchain(250)
# Ensure next block to sync is unavailable
assert_raises_rpc_error(-1, "Block not available (pruned data)", self.nodes[0].getblock, self.nodes[0].getblockhash(last_wallet_synced_block + 1))
# Check migration failure
mocked_time = int(time.time())
self.nodes[0].setmocktime(mocked_time)
assert_raises_rpc_error(-4, "last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)\nUnable to restore backup of wallet.", self.nodes[0].migratewallet, wallet_name="")
self.nodes[0].setmocktime(0)
# The wallet will not have been restored, and the backup will be in the wallets directory.
backup_path = self.nodes[0].wallets_path / f"default_wallet_{mocked_time}.legacy.bak"
assert backup_path.exists()
def run_test(self):
self.generate(self.nodes[0], 101)
@ -1113,5 +1136,8 @@ class WalletMigrationTest(BitcoinTestFramework):
self.test_preserve_tx_extra_info()
self.test_blank()
# Note: After this test the first 250 blocks of 'nodes[0]' are pruned
self.unsynced_wallet_on_pruned_node_fails()
if __name__ == '__main__':
WalletMigrationTest(__file__).main()