From 417884a791685ddd6e5701ed0631e1b9c474a654 Mon Sep 17 00:00:00 2001 From: jMyles Date: Tue, 17 Jul 2018 19:46:34 -0700 Subject: [PATCH] Some cleanup for make_ursulas. Lots more to do. #347 --- tests/fixtures.py | 2 -- tests/utilities.py | 48 +++++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/tests/fixtures.py b/tests/fixtures.py index c606b9b9a..fb6218bfa 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -168,7 +168,6 @@ def ursulas(three_agents): token_agent, miner_agent, policy_agent = three_agents ether_addresses = [to_checksum_address(os.urandom(20)) for _ in range(constants.NUMBER_OF_URSULAS_IN_NETWORK)] _ursulas = make_ursulas(ether_addresses=ether_addresses, - ursula_starting_port=int(constants.URSULA_PORT_SEED), miner_agent=miner_agent ) try: @@ -190,7 +189,6 @@ def mining_ursulas(three_agents): ursula_addresses = all_yall[:int(constants.NUMBER_OF_URSULAS_IN_NETWORK)] _ursulas = make_ursulas(ether_addresses=ursula_addresses, - ursula_starting_port=int(starting_point), miner_agent=miner_agent, miners=True) try: diff --git a/tests/utilities.py b/tests/utilities.py index 54db5da6f..c551a9119 100644 --- a/tests/utilities.py +++ b/tests/utilities.py @@ -6,12 +6,14 @@ from typing import List, Set import maya from apistar.test import TestClient from constant_sorrow import constants +from eth_utils import to_checksum_address from nucypher.blockchain.eth.agents import MinerAgent from nucypher.characters import Ursula # # Setup # +from nucypher.crypto.api import secure_random from nucypher.network.middleware import RestMiddleware from nucypher.policy.models import Arrangement, Policy @@ -24,8 +26,12 @@ constants.NUMBER_OF_URSULAS_IN_NETWORK(10) _ALL_URSULAS = {} -def make_ursulas(ether_addresses: list, ursula_starting_port: int, - miner_agent=None, miners=False, bare=False) -> Set[Ursula]: +def make_ursulas(ether_addresses: list, + miner_agent=None, + miners=False, + bare=False, + know_each_other=True, + **ursula_kwargs) -> Set[Ursula]: """ :param ether_addresses: Ethereum addresses to create ursulas with. :param ursula_starting_port: The port of the first created Ursula; subsequent Ursulas will increment the port number by 1. @@ -39,10 +45,18 @@ def make_ursulas(ether_addresses: list, ursula_starting_port: int, :return: A list of created Ursulas """ + if isinstance(ether_addresses, int): + ether_addresses = [to_checksum_address(secure_random(20)) for _ in range(ether_addresses)] + + event_loop = asyncio.get_event_loop() + if not _ALL_URSULAS: + starting_port = constants.URSULA_PORT_SEED + else: + starting_port = max(_ALL_URSULAS.keys()) + 1 ursulas = set() - for port, ether_address in enumerate(ether_addresses, start=ursula_starting_port): + for port, ether_address in enumerate(ether_addresses, start=starting_port): if bare: ursula = Ursula(is_me=False, # do not attach dht server @@ -51,7 +65,8 @@ def make_ursulas(ether_addresses: list, ursula_starting_port: int, checksum_address=ether_address, always_be_learning=False, miner_agent=miner_agent, - abort_on_learning_error=True) + abort_on_learning_error=True, + **ursula_kwargs) ursula.is_me = True # Patch to allow execution of transacting methods in tests @@ -68,7 +83,8 @@ def make_ursulas(ether_addresses: list, ursula_starting_port: int, rest_port=port+100, always_be_learning=False, miner_agent=miner_agent, - federated_only=federated_only) + federated_only=federated_only, + **ursula_kwargs) ursula.attach_rest_server() @@ -79,16 +95,6 @@ def make_ursulas(ether_addresses: list, ursula_starting_port: int, ursula.datastore_threadpool = MockDatastoreThreadPool() ursula.dht_listen() - for ursula_to_teach in ursulas: - # Add other Ursulas as known nodes. - for ursula_to_learn_about in ursulas: - ursula_to_teach.remember_node(ursula_to_learn_about) - - event_loop.run_until_complete( - ursula.dht_server.bootstrap( - [("127.0.0.1", ursula_starting_port + _c) for _c in range(len(ursulas))])) - ursula.publish_dht_information() - if miners is True: # TODO: 309 # stake a random amount @@ -106,6 +112,18 @@ def make_ursulas(ether_addresses: list, ursula_starting_port: int, ursulas.add(ursula) _ALL_URSULAS[ursula.rest_interface.port] = ursula + if know_each_other: + + for ursula_to_teach in ursulas: + # Add other Ursulas as known nodes. + for ursula_to_learn_about in ursulas: + ursula_to_teach.remember_node(ursula_to_learn_about) + + event_loop.run_until_complete( + ursula.dht_server.bootstrap( + [("127.0.0.1", starting_port + _c) for _c in range(len(ursulas))])) + ursula.publish_dht_information() + return ursulas