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

wallet: Implement WalletNFTStore.delete_wallet #15128

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions chia/wallet/wallet_nft_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,8 @@ async def rollback_to_block(self, height: int) -> bool:
if result.rowcount > 0:
return True
return False

async def delete_wallet(self, wallet_id: uint32) -> None:
xdustinface marked this conversation as resolved.
Show resolved Hide resolved
async with self.db_wrapper.writer_maybe_transaction() as conn:
cursor = await conn.execute("DELETE FROM users_nfts WHERE wallet_id=?", (wallet_id,))
await cursor.close()
46 changes: 46 additions & 0 deletions tests/wallet/test_nft_store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from __future__ import annotations

from dataclasses import dataclass, field
from secrets import token_bytes
from typing import Dict, List

import pytest

from chia.types.blockchain_format.coin import Coin
Expand All @@ -12,6 +16,26 @@
from tests.util.db_connection import DBConnection


def get_dummy_nft() -> NFTCoinInfo:
return NFTCoinInfo(
bytes32(token_bytes(32)),
Coin(bytes32(token_bytes(32)), bytes32(token_bytes(32)), uint64(1)),
LineageProof(bytes32(token_bytes(32)), bytes32(token_bytes(32)), uint64(1)),
Program.to(["A Test puzzle"]),
uint32(1),
)


@dataclass
class DummyNFTs:
nfts_per_wallet: Dict[uint32, List[NFTCoinInfo]] = field(default_factory=dict)

def generate(self, wallet_id: int, count: int) -> None:
nfts = self.nfts_per_wallet.setdefault(uint32(wallet_id), [])
for _ in range(count):
nfts.append(get_dummy_nft())


class TestNftStore:
@pytest.mark.asyncio
async def test_nft_insert(self) -> None:
Expand Down Expand Up @@ -144,3 +168,25 @@ async def test_nft_reorg(self) -> None:
await db.rollback_to_block(-1)
assert await db.count(wallet_id=uint32(1)) == 0
assert await db.is_empty(wallet_id=uint32(1))


@pytest.mark.asyncio
async def test_delete_wallet() -> None:
dummy_nfts = DummyNFTs()
for i in range(5):
dummy_nfts.generate(i, i * 5)
async with DBConnection(1) as wrapper:
db = await WalletNftStore.create(wrapper)
# Add the nfts per wallet and verify them
for wallet_id, nfts in dummy_nfts.nfts_per_wallet.items():
for nft in nfts:
await db.save_nft(wallet_id, None, nft)
assert await db.count(wallet_id) == len(nfts)
# Remove one wallet after the other and verify before and after each
for wallet_id, nfts in dummy_nfts.nfts_per_wallet.items():
# Assert the length again here to make sure the previous removals did not affect other wallet_ids
assert await db.count(wallet_id) == len(nfts)
await db.delete_wallet(wallet_id)
assert await db.count(wallet_id) == 0

assert await db.is_empty()