simplify setup

This commit is contained in:
Yostra 2020-04-03 16:09:30 -07:00
parent 9cb8afce28
commit 8ebf55bff7
4 changed files with 50 additions and 99 deletions

View File

@ -8,9 +8,7 @@ from src.simulator.simulator_protocol import FarmNewBlockProtocol, ReorgProtocol
from src.types.peer_info import PeerInfo from src.types.peer_info import PeerInfo
from src.util.ints import uint16, uint32 from src.util.ints import uint16, uint32
from tests.setup_nodes import ( from tests.setup_nodes import (
setup_node_simulator_and_two_wallets, setup_simulators_and_wallets
setup_node_simulator_and_wallet,
setup_three_simulators_and_two_wallets,
) )
from src.consensus.block_rewards import calculate_base_fee, calculate_block_reward from src.consensus.block_rewards import calculate_base_fee, calculate_block_reward
@ -24,19 +22,19 @@ def event_loop():
class TestTransactions: class TestTransactions:
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
async def wallet_node(self): async def wallet_node(self):
async for _ in setup_node_simulator_and_wallet(): async for _ in setup_simulators_and_wallets(1, 1, {}):
yield _ yield _
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
async def two_wallet_nodes(self): async def two_wallet_nodes(self):
async for _ in setup_node_simulator_and_two_wallets( async for _ in setup_simulators_and_wallets(1, 2,
{"COINBASE_FREEZE_PERIOD": 0} {"COINBASE_FREEZE_PERIOD": 0}
): ):
yield _ yield _
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
async def three_nodes_two_wallets(self): async def three_nodes_two_wallets(self):
async for _ in setup_three_simulators_and_two_wallets( async for _ in setup_simulators_and_wallets(3, 2,
{"COINBASE_FREEZE_PERIOD": 0} {"COINBASE_FREEZE_PERIOD": 0}
): ):
yield _ yield _
@ -44,7 +42,9 @@ class TestTransactions:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_wallet_coinbase(self, wallet_node): async def test_wallet_coinbase(self, wallet_node):
num_blocks = 10 num_blocks = 10
full_node_1, wallet_node, server_1, server_2 = wallet_node full_nodes, wallets = wallet_node
full_node_1, server_1 = full_nodes[0]
wallet_node, server_2 = wallets[0]
wallet = wallet_node.main_wallet wallet = wallet_node.main_wallet
ph = await wallet.get_new_puzzlehash() ph = await wallet.get_new_puzzlehash()

View File

@ -1,5 +1,5 @@
import signal import signal
from typing import Any, Dict, Optional from typing import Any, Dict, Optional, Tuple, List
from pathlib import Path from pathlib import Path
import asyncio import asyncio
@ -28,8 +28,7 @@ from src.farmer import Farmer
from src.introducer import Introducer from src.introducer import Introducer
from src.timelord import Timelord from src.timelord import Timelord
from src.server.connection import PeerInfo from src.server.connection import PeerInfo
from src.util.ints import uint16 from src.util.ints import uint16, uint32
bt = BlockTools() bt = BlockTools()
@ -316,24 +315,6 @@ async def setup_node_and_wallet(dic={}):
pass pass
async def setup_node_simulator_and_wallet(dic={}):
node_iters = [
setup_full_node_simulator("blockchain_test.db", 21234, dic=dic),
setup_wallet_node(21235, dic=dic),
]
full_node, s1 = await node_iters[0].__anext__()
wallet, s2 = await node_iters[1].__anext__()
yield (full_node, wallet, s1, s2)
for node_iter in node_iters:
try:
await node_iter.__anext__()
except StopAsyncIteration:
pass
async def setup_node_and_two_wallets(dic={}): async def setup_node_and_two_wallets(dic={}):
node_iters = [ node_iters = [
setup_full_node("blockchain_test.db", 21234, dic=dic), setup_full_node("blockchain_test.db", 21234, dic=dic),
@ -354,45 +335,26 @@ async def setup_node_and_two_wallets(dic={}):
pass pass
async def setup_node_simulator_and_two_wallets(dic={}): async def setup_simulators_and_wallets(simulator_count: int, wallet_count: int, dic: Dict):
node_iters = [ simulators: List[Tuple[FullNode, ChiaServer]] = []
setup_full_node_simulator("blockchain_test.db", 21234, dic=dic), wallets = []
setup_wallet_node(21235, key_seed=b"Test node 1", dic=dic), node_iters = []
setup_wallet_node(21236, key_seed=b"Test node 2", dic=dic),
]
full_node, s1 = await node_iters[0].__anext__() for index in range(0, simulator_count):
wallet, s2 = await node_iters[1].__anext__() db_name = f"blockchain_test{index}.db"
wallet_2, s3 = await node_iters[2].__anext__() port = 50000 + index
sim = setup_full_node_simulator(db_name, port, dic=dic)
simulators.append(await sim.__anext__())
node_iters.append(sim)
yield (full_node, wallet, wallet_2, s1, s2, s3) for index in range(0, wallet_count):
seed = bytes(uint32(index))
port = 55000 + index
wlt = setup_wallet_node(port, key_seed=seed, dic=dic)
wallets.append(await wlt.__anext__())
node_iters.append(wlt)
for node_iter in node_iters: yield (simulators, wallets)
try:
await node_iter.__anext__()
except StopAsyncIteration:
pass
async def setup_three_simulators_and_two_wallets(dic={}):
node_iters = [
setup_full_node_simulator("blockchain_test0.db", 21234, dic=dic),
setup_full_node_simulator("blockchain_test1.db", 21235, dic=dic),
setup_full_node_simulator("blockchain_test2.db", 21236, dic=dic),
setup_wallet_node(21237, key_seed=b"Test node 1", dic=dic),
setup_wallet_node(21238, key_seed=b"Test node 2", dic=dic),
]
full_node0, s0 = await node_iters[0].__anext__()
full_node1, s1 = await node_iters[1].__anext__()
full_node2, s2 = await node_iters[2].__anext__()
wallet_0, s3 = await node_iters[3].__anext__()
wallet_1, s4 = await node_iters[4].__anext__()
full_nodes = [(full_node0, s0), (full_node1, s1), (full_node2, s2)]
wallets = [(wallet_0, s3), (wallet_1, s4)]
yield (full_nodes, wallets)
for node_iter in node_iters: for node_iter in node_iters:
try: try:

View File

@ -5,13 +5,10 @@ import pytest
from blspy import ExtendedPrivateKey from blspy import ExtendedPrivateKey
from chiabip158 import PyBIP158 from chiabip158 import PyBIP158
from src.wallet.wallet_node import WalletNode
from tests.setup_nodes import ( from tests.setup_nodes import (
setup_two_nodes,
test_constants, test_constants,
bt, bt,
setup_node_simulator_and_wallet, setup_simulators_and_wallets)
)
from src.util.config import load_config from src.util.config import load_config
@ -24,15 +21,14 @@ def event_loop():
class TestFilter: class TestFilter:
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
async def wallet_and_node(self): async def wallet_and_node(self):
async for _ in setup_node_simulator_and_wallet(): async for _ in setup_simulators_and_wallets(1, 1, {}):
yield _ yield _
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_basic_filter_test(self, wallet_and_node): async def test_basic_filter_test(self, wallet_and_node):
sk = bytes(ExtendedPrivateKey.from_seed(b"")).hex() full_nodes, wallets = wallet_and_node
config = load_config("config.yaml", "wallet") full_node_1, server_1 = full_nodes[0]
key_config = {"wallet_sk": sk} wallet_node, server_2 = wallets[0]
full_node_1, wallet_node, server_1, server_2 = wallet_and_node
wallet = wallet_node.main_wallet wallet = wallet_node.main_wallet
num_blocks = 2 num_blocks = 2

View File

@ -8,10 +8,7 @@ from src.simulator.simulator_protocol import FarmNewBlockProtocol, ReorgProtocol
from src.types.peer_info import PeerInfo from src.types.peer_info import PeerInfo
from src.util.ints import uint16, uint32 from src.util.ints import uint16, uint32
from tests.setup_nodes import ( from tests.setup_nodes import (
setup_node_simulator_and_two_wallets, setup_simulators_and_wallets)
setup_node_simulator_and_wallet,
setup_three_simulators_and_two_wallets,
)
from src.consensus.block_rewards import calculate_base_fee, calculate_block_reward from src.consensus.block_rewards import calculate_base_fee, calculate_block_reward
@ -24,26 +21,26 @@ def event_loop():
class TestWalletSimulator: class TestWalletSimulator:
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
async def wallet_node(self): async def wallet_node(self):
async for _ in setup_node_simulator_and_wallet(): async for _ in setup_simulators_and_wallets(1, 1, {}):
yield _ yield _
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
async def two_wallet_nodes(self): async def two_wallet_nodes(self):
async for _ in setup_node_simulator_and_two_wallets( async for _ in setup_simulators_and_wallets(1, 2,
{"COINBASE_FREEZE_PERIOD": 0} {"COINBASE_FREEZE_PERIOD": 0}
): ):
yield _ yield _
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
async def two_wallet_nodes_five_freeze(self): async def two_wallet_nodes_five_freeze(self):
async for _ in setup_node_simulator_and_two_wallets( async for _ in setup_simulators_and_wallets(1, 2,
{"COINBASE_FREEZE_PERIOD": 5} {"COINBASE_FREEZE_PERIOD": 5}
): ):
yield _ yield _
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
async def three_sim_two_wallets(self): async def three_sim_two_wallets(self):
async for _ in setup_three_simulators_and_two_wallets( async for _ in setup_simulators_and_wallets(3, 2,
{"COINBASE_FREEZE_PERIOD": 0} {"COINBASE_FREEZE_PERIOD": 0}
): ):
yield _ yield _
@ -51,7 +48,9 @@ class TestWalletSimulator:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_wallet_coinbase(self, wallet_node): async def test_wallet_coinbase(self, wallet_node):
num_blocks = 10 num_blocks = 10
full_node_1, wallet_node, server_1, server_2 = wallet_node full_nodes, wallets = wallet_node
full_node_1, server_1 = full_nodes[0]
wallet_node, server_2 = wallets[0]
wallet = wallet_node.main_wallet wallet = wallet_node.main_wallet
ph = await wallet.get_new_puzzlehash() ph = await wallet.get_new_puzzlehash()
@ -73,14 +72,10 @@ class TestWalletSimulator:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_wallet_make_transaction(self, two_wallet_nodes): async def test_wallet_make_transaction(self, two_wallet_nodes):
num_blocks = 10 num_blocks = 10
( full_nodes, wallets = two_wallet_nodes
full_node_1, full_node_1, server_1 = full_nodes[0]
wallet_node, wallet_node, server_2 = wallets[0]
wallet_node_2, wallet_node_2, server_3 = wallets[1]
server_1,
server_2,
server_3,
) = two_wallet_nodes
wallet = wallet_node.main_wallet wallet = wallet_node.main_wallet
ph = await wallet.get_new_puzzlehash() ph = await wallet.get_new_puzzlehash()
@ -136,7 +131,9 @@ class TestWalletSimulator:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_wallet_coinbase_reorg(self, wallet_node): async def test_wallet_coinbase_reorg(self, wallet_node):
num_blocks = 10 num_blocks = 10
full_node_1, wallet_node, server_1, server_2 = wallet_node full_nodes, wallets = wallet_node
full_node_1, server_1 = full_nodes[0]
wallet_node, server_2 = wallets[0]
wallet = wallet_node.main_wallet wallet = wallet_node.main_wallet
ph = await wallet.get_new_puzzlehash() ph = await wallet.get_new_puzzlehash()
@ -241,14 +238,10 @@ class TestWalletSimulator:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_wallet_make_transaction_hop(self, two_wallet_nodes_five_freeze): async def test_wallet_make_transaction_hop(self, two_wallet_nodes_five_freeze):
num_blocks = 10 num_blocks = 10
( full_nodes, wallets = two_wallet_nodes_five_freeze
full_node_0, full_node_0, full_node_server = full_nodes[0]
wallet_node_0, wallet_node_0, wallet_0_server = wallets[0]
wallet_node_1, wallet_node_1, wallet_1_server = wallets[1]
full_node_server,
wallet_0_server,
wallet_1_server,
) = two_wallet_nodes_five_freeze
wallet_0 = wallet_node_0.main_wallet wallet_0 = wallet_node_0.main_wallet
wallet_1 = wallet_node_1.main_wallet wallet_1 = wallet_node_1.main_wallet
ph = await wallet_0.get_new_puzzlehash() ph = await wallet_0.get_new_puzzlehash()