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

Clawback resync #15496

Merged
merged 24 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 6 additions & 4 deletions chia/wallet/wallet_state_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,11 +1149,14 @@ async def handle_clawback(
if is_recipient is not None:
spend_bundle = SpendBundle([coin_spend], G2Element())
memos = compute_memos(spend_bundle)
spent_height: uint32 = uint32(0)
if coin_state.spent_height is not None: # pragma: no cover
ytx1991 marked this conversation as resolved.
Show resolved Hide resolved
spent_height = uint32(coin_state.spent_height)
coin_record = WalletCoinRecord(
coin_state.coin,
uint32(coin_state.created_height),
uint32(0),
False,
spent_height,
False if spent_height == 0 else True,
xdustinface marked this conversation as resolved.
Show resolved Hide resolved
False,
WalletType.STANDARD_WALLET,
1,
Expand All @@ -1164,14 +1167,13 @@ async def handle_clawback(
await self.coin_store.add_coin_record(coin_record)
# Add tx record
created_timestamp = await self.wallet_node.get_timestamp_for_height(uint32(coin_state.created_height))
spend_bundle = SpendBundle([coin_spend], G2Element())
xdustinface marked this conversation as resolved.
Show resolved Hide resolved
tx_record = TransactionRecord(
confirmed_at_height=uint32(coin_state.created_height),
created_at_time=uint64(created_timestamp),
to_puzzle_hash=metadata.recipient_puzzle_hash,
amount=uint64(coin_state.coin.amount),
fee_amount=uint64(0),
confirmed=False,
confirmed=False if spent_height == 0 else True,
ytx1991 marked this conversation as resolved.
Show resolved Hide resolved
sent=uint32(0),
spend_bundle=None,
additions=[coin_state.coin],
Expand Down
34 changes: 29 additions & 5 deletions tests/wallet/rpc/test_wallet_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,8 @@ async def test_set_wallet_resync_on_startup(wallet_rpc_environment: WalletRpcTes
env: WalletRpcTestEnvironment = wallet_rpc_environment
full_node_api: FullNodeSimulator = env.full_node.api
client: WalletRpcClient = env.wallet_1.rpc_client
wallet_node: WalletNode = env.wallet_1.node
wallet_node_2: WalletNode = env.wallet_2.node
xdustinface marked this conversation as resolved.
Show resolved Hide resolved
await generate_funds(full_node_api, env.wallet_1)
wc = env.wallet_1.rpc_client
await wc.create_new_did_wallet(1, 0)
Expand All @@ -1927,17 +1929,35 @@ async def test_set_wallet_resync_on_startup(wallet_rpc_environment: WalletRpcTes
await time_out_assert(5, check_mempool_spend_count, True, full_node_api, 1)
await farm_transaction_block(full_node_api, env.wallet_1.node)
await time_out_assert(20, wc.get_synced)

wallet_node: WalletNode = env.wallet_1.node
wallet_node_2: WalletNode = env.wallet_2.node
# Test Clawback resync
tx = await wc.send_transaction(
wallet_id=1,
amount=uint64(500),
address=address,
fee=uint64(0),
puzzle_decorator_override=[{"decorator": "CLAWBACK", "clawback_timelock": 5}],
)
clawback_coin_id = tx.additions[0].name()
assert tx.spend_bundle is not None
await farm_transaction(full_node_api, wallet_node, tx.spend_bundle)
await time_out_assert(20, wc.get_synced)
await asyncio.sleep(10)
resp = await wc.spend_clawback_coins([clawback_coin_id], 0)
assert resp["success"]
assert len(resp["transaction_ids"]) == 1
await time_out_assert_not_none(
10, full_node_api.full_node.mempool_manager.get_spendbundle, bytes32.from_hexstr(resp["transaction_ids"][0])
)
await farm_transaction_block(full_node_api, wallet_node)
await time_out_assert(20, wc.get_synced)
wallet_node_2._close()
await wallet_node_2._await_closed()
# set flag to reset wallet sync data on start
await client.set_wallet_resync_on_startup()
fingerprint = wallet_node.logged_in_fingerprint
assert wallet_node._wallet_state_manager
# 2 reward coins, 1 DID, 1 NFT
assert len(await wallet_node._wallet_state_manager.coin_store.get_all_unspent_coins()) == 4
# 2 reward coins, 1 DID, 1 NFT, 1 clawbacked coin
assert len(await wallet_node._wallet_state_manager.coin_store.get_all_unspent_coins()) == 5
assert await wallet_node._wallet_state_manager.nft_store.count() == 1
# standard wallet, did wallet, nft wallet, did nft wallet
assert len(await wallet_node.wallet_state_manager.user_store.get_all_wallet_info_entries()) == 4
Expand All @@ -1958,6 +1978,10 @@ async def test_set_wallet_resync_on_startup(wallet_rpc_environment: WalletRpcTes
after_txs = await wallet_node_2.wallet_state_manager.tx_store.get_all_transactions()
# transactions should be the same
assert after_txs == before_txs
# Check clawback
clawback_tx = await wallet_node_2.wallet_state_manager.tx_store.get_transaction_record(clawback_coin_id)
assert clawback_tx is not None
assert clawback_tx.confirmed
# only coin_store was populated in this case, but now should be empty
assert len(await wallet_node_2._wallet_state_manager.coin_store.get_all_unspent_coins()) == 0
assert await wallet_node_2._wallet_state_manager.nft_store.count() == 0
Expand Down
Loading