From 54d939c81a70df9db265385b937d46129279cb05 Mon Sep 17 00:00:00 2001 From: Kieran Prasch Date: Sun, 24 Jun 2018 23:24:51 -0700 Subject: [PATCH] Allow creation of 'bare' ursula in utils. --- tests/utilities.py | 69 ++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/tests/utilities.py b/tests/utilities.py index 2f1741df5..c235f370b 100644 --- a/tests/utilities.py +++ b/tests/utilities.py @@ -24,34 +24,59 @@ constants.NUMBER_OF_URSULAS_IN_NETWORK(10) _ALL_URSULAS = {} -def make_ursulas(ether_addresses: list, ursula_starting_port: int, miner_agent=None, miners=False) -> List[Ursula]: +def make_ursulas(ether_addresses: list, ursula_starting_port: int, + miner_agent=None, miners=False, bare=False) -> Set[Ursula]: """ + :param ether_addresses: Ethereum addresses to create ursulas with. :param how_many_ursulas: How many Ursulas to create. :param ursula_starting_port: The port of the first created Ursula; subsequent Ursulas will increment the port number by 1. :return: A list of created Ursulas """ + event_loop = asyncio.get_event_loop() ursulas = set() for port, ether_address in enumerate(ether_addresses, start=ursula_starting_port): - ursula = Ursula(is_me=True, - ether_address=ether_address, - dht_host="127.0.0.1", - dht_port=port, - db_name="test-{}".format(port), - rest_host="127.0.0.1", - rest_port=port+100, - always_be_learning=False, - miner_agent=miner_agent, - ) - ursula.attach_rest_server() - class MockDatastoreThreadPool(object): - def callInThread(self, f, *args, **kwargs): - return f(*args, **kwargs) + if bare: + ursula = Ursula(is_me=False, # do not attach dht server + rest_host="127.0.0.1", + rest_port=port + 100, + ether_address=ether_address, + always_be_learning=False, + miner_agent=miner_agent) - ursula.datastore_threadpool = MockDatastoreThreadPool() - ursula.dht_listen() + ursula.is_me = True # Patch to allow execution of transacting methods in tests + + else: + ursula = Ursula(is_me=True, + ether_address=ether_address, + dht_host="127.0.0.1", + dht_port=port, + db_name="test-{}".format(port), + rest_host="127.0.0.1", + rest_port=port+100, + always_be_learning=False, + miner_agent=miner_agent) + + ursula.attach_rest_server() + + class MockDatastoreThreadPool(object): + def callInThread(self, f, *args, **kwargs): + return f(*args, **kwargs) + + 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 @@ -64,20 +89,10 @@ def make_ursulas(ether_addresses: list, ursula_starting_port: int, miner_agent=N periods = random.randint(min_locktime, max_locktime) ursula.stake(amount=amount, lock_periods=periods) - ursula.miner_agent.blockchain.time_travel(periods=1) ursulas.add(ursula) _ALL_URSULAS[ursula.rest_interface.port] = ursula - 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() - return ursulas