def test_ping(): """ Testing of ping HTTP endpoint """ api = common.external_api(node) top = api.get_top() gen_hash = common.genesis_hash(api) ping_obj = Ping(source="localhost", genesis_hash=gen_hash, best_hash=top.hash, difficulty=top.difficulty, share=32, peers=[]) ping = api.ping(ping_obj)
def test_node_discovery(): """ Test node discovery Assuming Carol's node only knows about Bob upon startup and that Bob's node knows Alice, Carol's node should be able to discover Alice and sync with her node. """ test_settings = settings["test_node_discovery"] alice_node = test_settings["nodes"]["alice"] bob_node = test_settings["nodes"]["bob"] carol_node = test_settings["nodes"]["carol"] alice_peer_url = node_peer_url(alice_node) bob_peer_url = node_peer_url(bob_node) carol_peer_url = node_peer_url(carol_node) # prepare a dir to hold the configs root_dir = tempfile.mkdtemp() # Alice's config: no peers alice_sys_config = make_peers_config(root_dir, "alice.config", alice_peer_url, [], mining=True) print("\nAlice has address " + alice_peer_url + " and no peers") # Bob's config: only peer is Alice bob_sys_config = make_peers_config(root_dir, "bob.config", bob_peer_url, [alice_peer_url], mining=False) print("Bob has address " + bob_peer_url + " and peers [" + alice_peer_url + "]") # Carol's config: only peer is Bob carol_sys_config = make_peers_config(root_dir, "carol.config", carol_peer_url, [bob_peer_url], mining=False) print("Carol has address " + carol_peer_url + " and peers [" + bob_peer_url + "]") # start Alice's node common.start_node(alice_node, alice_sys_config) alice_api = common.external_api(alice_node) # Insert some blocks in Alice's chain blocks_to_mine = test_settings["blocks_to_mine"] common.wait_until_height(alice_api, blocks_to_mine) alice_top = alice_api.get_top() assert_equals(alice_top.height >= blocks_to_mine, True) # Now Alice has at least blocks_to_mine blocks # start the other nodes common.start_node(bob_node, bob_sys_config) common.start_node(carol_node, carol_sys_config) time.sleep(1) # give some time for the data to propagate carol_api = common.external_api(carol_node) carol_top = carol_api.get_top() gen_hash = common.genesis_hash(carol_api) ping_obj = Ping(source="localhost", genesis_hash=gen_hash, best_hash=carol_top.hash, difficulty=1, share=32, peers=[]) ping = carol_api.ping(ping_obj) carol_peers = ping.peers synced = len(list(filter(lambda peer: peer == alice_peer_url, carol_peers))) == 1 print("Carol now has peers " + str(carol_peers)) assert_equals(synced, True) # for larger peer lists this might be too fragile assert_equals(carol_top.height >= blocks_to_mine, True) if carol_top.height > alice_top.height: # Alice had mined some more blocks alice_block = alice_api.get_block_by_hash( carol_top.hash) # this block is presnet assert_equals(alice_block.height, carol_top.height) else: assert_equals(alice_top.height, carol_top.height) # cleanup common.stop_node(alice_node) common.stop_node(bob_node) common.stop_node(carol_node) shutil.rmtree(root_dir)
def test_node_discovery(): """ Test node discovery Assuming Carol's node only knows about Bob upon startup and that Bob's node knows Alice, Carol's node should be able to discover Alice and sync with her node. """ test_settings = settings["test_node_discovery"] alice_node = test_settings["nodes"]["alice"] bob_node = test_settings["nodes"]["bob"] carol_node = test_settings["nodes"]["carol"] alice_peer_url = node_peer_url(alice_node) bob_peer_url = node_peer_url(bob_node) carol_peer_url = node_peer_url(carol_node) # prepare a dir to hold the configs root_dir = tempfile.mkdtemp() # Alice's config: no peers alice_sys_config = make_peers_config(root_dir, "alice.config", alice_peer_url, [], mining=True) print("\nAlice has address " + alice_peer_url + " and no peers") # Bob's config: only peer is Alice bob_sys_config = make_peers_config(root_dir, "bob.config", bob_peer_url, [alice_peer_url], mining=False) print("Bob has address " + bob_peer_url + " and peers [" + alice_peer_url + "]") # Carol's config: only peer is Bob carol_sys_config = make_peers_config(root_dir, "carol.config", carol_peer_url, [bob_peer_url], mining=False) print("Carol has address " + carol_peer_url + " and peers [" + bob_peer_url + "]") # start Alice's node common.start_node(alice_node, alice_sys_config) alice_api = common.external_api(alice_node) # Insert some blocks in Alice's chain blocks_to_mine = test_settings["blocks_to_mine"] common.wait_until_height(alice_api, blocks_to_mine) alice_top = alice_api.get_top() assert_true(alice_top.height >= blocks_to_mine) # Now Alice has at least blocks_to_mine blocks # start the other nodes common.start_node(bob_node, bob_sys_config) common.start_node(carol_node, carol_sys_config) # Check that Carol syncs with Alice's chain carol_api = common.external_api(carol_node) common.wait_until_height(carol_api, alice_top.height) assert_equals( carol_api.get_block_by_hash(alice_top.hash).height, alice_top.height) # Check that Carol discovers Alice as a peer gen_hash = common.genesis_hash(carol_api) ping_obj = Ping( source="http://localhost:1234", genesis_hash=gen_hash, best_hash=gen_hash, difficulty=1, share=32, # expected peer list is small peers=[]) def carol_peers(): ps = [p.encode('utf-8') for p in carol_api.ping(ping_obj).peers] print("Carol now has peers " + str(ps)) return ps wait(lambda: common.is_among_peers(alice_peer_url, carol_peers()), timeout_seconds=120, sleep_seconds=0.25) # cleanup common.stop_node(alice_node) common.stop_node(bob_node) common.stop_node(carol_node) shutil.rmtree(root_dir)