Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHIA-683] Drop unknown tables when resetting wallet sync DB #18222

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading