wait for transactions to enter mempool

This commit is contained in:
Kyle Altendorf 2021-11-29 16:17:39 -05:00
parent 2057d388e4
commit 4864c87573
No known key found for this signature in database
GPG Key ID: 5715D880FF005192
5 changed files with 61 additions and 29 deletions

View File

@ -71,9 +71,10 @@ class DataLayer:
main_wallet = self.wallet_node.wallet_state_manager.main_wallet
amount = uint64(1) # todo what should amount be ?
async with self.wallet_node.wallet_state_manager.lock:
self.wallet = await self.wallet.create_new_dl_wallet(
creation_record = await self.wallet.create_new_dl_wallet(
self.wallet_node.wallet_state_manager, main_wallet, amount, None
)
self.wallet = creation_record.wallet
self.initialized = True
return True

View File

@ -3,7 +3,7 @@ import os
import json
import time
from dataclasses import dataclass
from typing import Any, Optional, Tuple, Set, List, Dict, Type, TypeVar
from typing import Any, Generic, Optional, Tuple, Set, List, Dict, Type, TypeVar
from blspy import G2Element, AugSchemeMPL
@ -19,7 +19,7 @@ from chia.types.blockchain_format.coin import Coin
from chia.types.blockchain_format.program import Program, SerializedProgram
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.coin_spend import CoinSpend
from chia.types.spend_bundle import SpendBundle
from chia.types.spend_bundle import ItemAndSpendBundleNames, SpendBundle
from chia.util.ints import uint8, uint32, uint64, uint128
from secrets import token_bytes
from chia.util.streamable import Streamable, streamable
@ -80,7 +80,7 @@ class DataLayerWallet:
root_hash: bytes32,
fee: uint64 = uint64(0),
name: Optional[str] = None,
) -> _T_DataLayerWallet:
) -> ItemAndSpendBundleNames[_T_DataLayerWallet]:
"""
This must be called under the wallet state manager lock
"""
@ -167,7 +167,7 @@ class DataLayerWallet:
await self.standard_wallet.push_transaction(regular_record)
await self.standard_wallet.push_transaction(dl_record)
await self.wallet_state_manager.update_wallet_puzzle_hashes(self.wallet_info.id)
return self
return ItemAndSpendBundleNames(item=self, spend_bundle_names={spend_bundle.name()})
async def generate_launcher_spend(
self,

View File

@ -1,7 +1,7 @@
import asyncio
import itertools
import sys
from typing import List, Optional
from typing import Iterable, List, Optional
from chia.consensus.block_record import BlockRecord
from chia.consensus.block_rewards import calculate_pool_reward, calculate_base_farmer_reward
@ -220,3 +220,20 @@ class FullNodeSimulator(FullNodeAPI):
return rewards
raise Exception("internal error")
async def wait_spend_bundle_entered_mempool(self, spend_bundle_names: Iterable[bytes32]) -> None:
ids_to_check = set(spend_bundle_names)
# TODO: add a timeout
# TODO: can we avoid polling
while True:
found = set()
for spend_bundle_name in ids_to_check:
tx = self.full_node.mempool_manager.get_spendbundle(spend_bundle_name)
if tx is not None:
found.add(spend_bundle_name)
ids_to_check = ids_to_check.difference(found)
if len(ids_to_check) == 0:
return
await asyncio.sleep(0.050)

View File

@ -2,7 +2,7 @@ import dataclasses
import warnings
from dataclasses import dataclass
from typing import List
from typing import Generic, List, Set, TypeVar
from blspy import AugSchemeMPL, G2Element
@ -15,6 +15,15 @@ from chia.wallet.util.debug_spend_bundle import debug_spend_bundle
from .coin_spend import CoinSpend
T = TypeVar("T")
@dataclass
class ItemAndSpendBundleNames(Generic[T]):
item: T
spend_bundle_names: Set[bytes32]
@dataclass(frozen=True)
@streamable
class SpendBundle(Streamable):

View File

@ -10,7 +10,7 @@ from chia.wallet.dlo_wallet.dlo_wallet import DLOWallet
from chia.wallet.db_wallet.db_wallet_puzzles import create_host_fullpuz
from chia.types.blockchain_format.program import Program
from chia.types.announcement import Announcement
from chia.types.spend_bundle import SpendBundle
from chia.types.spend_bundle import ItemAndSpendBundleNames, SpendBundle
from tests.time_out_assert import time_out_assert
from chia.wallet.util.merkle_tree import MerkleTree
from chia.wallet.transaction_record import TransactionRecord
@ -79,11 +79,13 @@ class TestDLWallet:
# Wallet1 sets up DLWallet1 without any backup set
async with wallet_node_0.wallet_state_manager.lock:
dl_wallet_0: DataLayerWallet = await DataLayerWallet.create_new_dl_wallet(
creation_record = await DataLayerWallet.create_new_dl_wallet(
wallet_node_0.wallet_state_manager, wallet_0, uint64(101), current_root
)
await dl_wallet_0.wallet_state_manager.tx_store.wait_all_sent()
dl_wallet_0: DataLayerWallet = creation_record.item
await full_node_api.wait_spend_bundle_entered_mempool(spend_bundle_names=creation_record.spend_bundle_names)
await full_node_api.process_blocks(count=1)
@ -94,8 +96,8 @@ class TestDLWallet:
nodes.append(Program.to("beep").get_tree_hash())
new_merkle_tree = MerkleTree(nodes)
await dl_wallet_0.create_update_state_spend(new_merkle_tree.calculate_root())
await dl_wallet_0.wallet_state_manager.tx_store.wait_all_sent()
spend_bundle = await dl_wallet_0.create_update_state_spend(new_merkle_tree.calculate_root())
await full_node_api.wait_spend_bundle_entered_mempool(spend_bundle_names=[spend_bundle.name()])
await full_node_api.process_blocks(count=2)
@ -145,11 +147,13 @@ class TestDLWallet:
# Wallet1 sets up DLWallet1 without any backup set
async with wallet_node_0.wallet_state_manager.lock:
dl_wallet_0: DataLayerWallet = await DataLayerWallet.create_new_dl_wallet(
creation_record = await DataLayerWallet.create_new_dl_wallet(
wallet_node_0.wallet_state_manager, wallet_0, uint64(101), current_root
)
await dl_wallet_0.wallet_state_manager.tx_store.wait_all_sent()
dl_wallet_0: DataLayerWallet = creation_record.item
await full_node_api.wait_spend_bundle_entered_mempool(spend_bundle_names=creation_record.spend_bundle_names)
await full_node_api.farm_blocks(count=1, farm_to=ph1)
@ -178,7 +182,7 @@ class TestDLWallet:
name=sb.name(),
)
await wallet_1.push_transaction(tr)
await wallet_1.wallet_state_manager.tx_store.wait_all_sent()
await full_node_api.wait_spend_bundle_entered_mempool(spend_bundle_names=[sb.name()])
await full_node_api.process_blocks(count=2)
@ -216,11 +220,13 @@ class TestDLWallet:
# Wallet1 sets up DLWallet1
async with wallet_node_0.wallet_state_manager.lock:
dl_wallet_0: DataLayerWallet = await DataLayerWallet.create_new_dl_wallet(
creation_record = await DataLayerWallet.create_new_dl_wallet(
wallet_node_0.wallet_state_manager, wallet_0, uint64(101), current_root
)
await dl_wallet_0.wallet_state_manager.tx_store.wait_all_sent()
dl_wallet_0: DataLayerWallet = creation_record.item
await full_node_api.wait_spend_bundle_entered_mempool(spend_bundle_names=creation_record.spend_bundle_names)
await full_node_api.process_blocks(count=1)
@ -234,8 +240,6 @@ class TestDLWallet:
wallet_1,
)
await dlo_wallet_1.wallet_state_manager.tx_store.wait_all_sent()
await full_node_api.farm_blocks(count=2, farm_to=ph1)
await time_out_assert(15, dlo_wallet_1.get_confirmed_balance, 0)
@ -254,7 +258,7 @@ class TestDLWallet:
10,
)
await wallet_1.push_transaction(tr)
await wallet_1.wallet_state_manager.tx_store.wait_all_sent()
await full_node_api.wait_spend_bundle_entered_mempool(spend_bundle_names=[tr.spend_bundle.name()])
# TODO: ugh
if sys.platform == "darwin":
@ -272,8 +276,6 @@ class TestDLWallet:
wallet_2,
)
await dlo_wallet_2.wallet_state_manager.tx_store.wait_all_sent()
offer_coin = await dlo_wallet_1.get_coin()
offer_full_puzzle = dlo_wallet_1.puzzle_for_pk(0x00)
db_puzzle, db_innerpuz, current_root = await dl_wallet_0.get_info_for_offer_claim()
@ -308,7 +310,7 @@ class TestDLWallet:
name=sb.name(),
)
await wallet_2.push_transaction(tr)
await wallet_2.wallet_state_manager.tx_store.wait_all_sent()
await full_node_api.wait_spend_bundle_entered_mempool(spend_bundle_names=[sb.name()])
await full_node_api.process_blocks(count=2)
@ -345,11 +347,13 @@ class TestDLWallet:
# Wallet1 sets up DLWallet1
async with wallet_node_0.wallet_state_manager.lock:
dl_wallet_0: DataLayerWallet = await DataLayerWallet.create_new_dl_wallet(
creation_record = await DataLayerWallet.create_new_dl_wallet(
wallet_node_0.wallet_state_manager, wallet_0, uint64(101), current_root
)
await dl_wallet_0.wallet_state_manager.tx_store.wait_all_sent()
dl_wallet_0: DataLayerWallet = creation_record.item
await full_node_api.wait_spend_bundle_entered_mempool(spend_bundle_names=creation_record.spend_bundle_names)
await full_node_api.process_blocks(count=1)
@ -363,8 +367,6 @@ class TestDLWallet:
wallet_1,
)
await dlo_wallet_1.wallet_state_manager.tx_store.wait_all_sent()
wallet_1_funds = await full_node_api.farm_blocks(count=1, farm_to=ph1)
offer_amount = 201
@ -384,7 +386,7 @@ class TestDLWallet:
10,
)
await wallet_1.push_transaction(tr)
await wallet_1.wallet_state_manager.tx_store.wait_all_sent()
await full_node_api.wait_spend_bundle_entered_mempool(spend_bundle_names=[tr.spend_bundle.name()])
await full_node_api.process_blocks(count=2)
@ -395,7 +397,10 @@ class TestDLWallet:
await time_out_assert(15, wallet_1.get_confirmed_balance, wallet_1_funds)
await time_out_assert(15, wallet_1.get_unconfirmed_balance, wallet_1_funds)
await dlo_wallet_1.create_recover_dl_offer_spend()
transaction_record = await dlo_wallet_1.create_recover_dl_offer_spend()
await full_node_api.wait_spend_bundle_entered_mempool(
spend_bundle_names=[transaction_record.spend_bundle.name()]
)
await dlo_wallet_1.wallet_state_manager.tx_store.wait_all_sent()
await full_node_api.process_blocks(count=2)