Skip to content

Commit

Permalink
Drop unknown tables when resetting wallet sync DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Quexington committed Jun 20, 2024
1 parent ff30678 commit b78fdf0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
17 changes: 6 additions & 11 deletions chia/_tests/wallet/rpc/test_wallet_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,7 @@ async def test_set_wallet_resync_on_startup_disable(wallet_rpc_environment: Wall


@pytest.mark.anyio
@pytest.mark.limit_consensus_modes(reason="irrelevant")
async def test_set_wallet_resync_schema(wallet_rpc_environment: WalletRpcTestEnvironment):
env: WalletRpcTestEnvironment = wallet_rpc_environment
full_node_api: FullNodeSimulator = env.full_node.api
Expand All @@ -2454,17 +2455,11 @@ async def test_set_wallet_resync_schema(wallet_rpc_environment: WalletRpcTestEnv
dbw: DBWrapper2 = wallet_node.wallet_state_manager.db_wrapper
conn: aiosqlite.Connection
async with dbw.writer() as conn:
await conn.execute("ALTER TABLE coin_record RENAME TO coin_record_temp")
assert not await wallet_node.reset_sync_db(db_path, fingerprint)
async with dbw.writer() as conn:
await conn.execute("ALTER TABLE coin_record_temp RENAME TO coin_record")
assert await wallet_node.reset_sync_db(db_path, fingerprint)
async with dbw.writer() as conn:
await conn.execute("CREATE TABLE testing_schema (a int, b bool)")
assert not await wallet_node.reset_sync_db(db_path, fingerprint)
async with dbw.writer() as conn:
await conn.execute("DROP TABLE testing_schema")
assert await wallet_node.reset_sync_db(db_path, fingerprint)
await conn.execute("CREATE TABLE blah(temp int)")
await wallet_node.reset_sync_db(db_path, fingerprint)
assert (
len(list(await conn.execute_fetchall("SELECT name FROM sqlite_master WHERE type='table' AND name='blah'"))) == 0
)


@pytest.mark.anyio
Expand Down
13 changes: 6 additions & 7 deletions chia/wallet/wallet_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ async def reset_sync_db(self, db_path: Union[Path, str], fingerprint: int) -> bo
conn: aiosqlite.Connection
# are not part of core wallet tables, but might appear later
ignore_tables = {"lineage_proofs_", "sqlite_", "MIGRATED_VALID_TIMES_TXS", "MIGRATED_VALID_TIMES_TRADES"}
required_tables = [
known_tables = [
"coin_record",
"transaction_record",
"derivation_paths",
Expand Down Expand Up @@ -354,22 +354,21 @@ async def reset_sync_db(self, db_path: Union[Path, str], fingerprint: int) -> bo
self.log.info("Resetting wallet sync data...")
rows = list(await conn.execute_fetchall("SELECT name FROM sqlite_master WHERE type='table'"))
names = {x[0] for x in rows}
names = names - set(required_tables)
names = names - set(known_tables)
tables_to_drop = []
for name in names:
for ignore_name in ignore_tables:
if name.startswith(ignore_name):
break
else:
self.log.error(
f"Mismatch in expected schema to reset, found unexpected table: {name}. "
"Please check if you've run all migration scripts."
)
return False
tables_to_drop.append(name)

await conn.execute("BEGIN")
commit = True
tables = [row[0] for row in rows]
try:
for table in tables_to_drop:
await conn.execute(f"DROP TABLE {table}")
if "coin_record" in tables:
await conn.execute("DELETE FROM coin_record")
if "interested_coins" in tables:
Expand Down

0 comments on commit b78fdf0

Please sign in to comment.