async def test_get_peers(event_bus, response, expected_count): do_mock = mock_request_response(GetConnectedPeersRequest, response, event_bus) async with do_mock: proxy_peer_pool = ETHProxyPeerPool(event_bus, TO_NETWORKING_BROADCAST_CONFIG) async with run_service(proxy_peer_pool): peers = await proxy_peer_pool.get_peers() assert len(peers) == expected_count
async def test_network_id_ipc_request(jsonrpc_ipc_pipe_path, event_loop, event_bus, ipc_server): do_mock = mock_request_response(NetworkIdRequest, NetworkIdResponse(1337), event_bus) async with do_mock: request_msg = build_request('net_version') expected = {'result': '1337', 'id': 3, 'jsonrpc': '2.0'} result = await get_ipc_response(jsonrpc_ipc_pipe_path, request_msg, event_loop, event_bus) assert result == expected
async def test_eth_over_ipc(jsonrpc_ipc_pipe_path, request_msg, event_bus_response, event_bus, expected, event_loop, ipc_server): do_mock = mock_request_response(SyncingRequest, event_bus_response, event_bus) async with do_mock: result = await get_ipc_response( jsonrpc_ipc_pipe_path, request_msg, event_loop, event_bus, ) assert result == expected
async def test_adds_new_peers(event_bus): do_mock = mock_request_response( GetConnectedPeersRequest, GetConnectedPeersResponseFactory.from_sessions((TEST_NODES[0],)), event_bus, ) async with do_mock: proxy_peer_pool = ETHProxyPeerPool(event_bus, TO_NETWORKING_BROADCAST_CONFIG) async with run_service(proxy_peer_pool): assert len(await proxy_peer_pool.get_peers()) == 1 await event_bus.broadcast(PeerJoinedEvent(TEST_NODES[1])) # Give the peer a moment to pickup the peer await asyncio.sleep(0.01) assert len(await proxy_peer_pool.get_peers()) == 2
async def test_does_not_propagate_invalid_tx(event_bus, funded_address_private_key, chain_with_block_validation, tx_validator): chain = chain_with_block_validation initial_two_peers = TEST_NODES[:2] node_one = initial_two_peers[0] node_two = initial_two_peers[1] async with AsyncExitStack() as stack: await stack.enter_async_context( mock_request_response( GetConnectedPeersRequest, GetConnectedPeersResponse(initial_two_peers), event_bus, )) peer_pool = ETHProxyPeerPool(event_bus, TO_NETWORKING_BROADCAST_CONFIG) await stack.enter_async_context(run_service(peer_pool)) tx_pool = TxPool(event_bus, peer_pool, tx_validator) await stack.enter_async_context(background_asyncio_service(tx_pool)) await asyncio.sleep(0.01) txs_broadcasted_by_peer1 = [ create_random_tx(chain, funded_address_private_key, is_valid=False), create_random_tx(chain, funded_address_private_key) ] outgoing_tx, got_txns = observe_outgoing_transactions(event_bus) # Peer1 sends some txs await event_bus.broadcast( TransactionsEvent(session=node_one, command=Transactions(txs_broadcasted_by_peer1))) await asyncio.wait_for(got_txns.wait(), timeout=0.1) # Check that Peer2 received only the second tx which is valid assert outgoing_tx == [ (node_two, (txs_broadcasted_by_peer1[1], )), ]
async def test_removes_peers(event_bus): do_mock = mock_request_response( GetConnectedPeersRequest, GetConnectedPeersResponseFactory.from_sessions(TEST_NODES[:2]), event_bus, ) async with do_mock: proxy_peer_pool = ETHProxyPeerPool(event_bus, TO_NETWORKING_BROADCAST_CONFIG) async with run_service(proxy_peer_pool): assert len(await proxy_peer_pool.get_peers()) == 2 await event_bus.broadcast(PeerLeftEvent(TEST_NODES[0])) # Give the peer a moment to remove the peer await asyncio.sleep(0.01) peers = await proxy_peer_pool.get_peers() assert len(peers) == 1 assert peers[0].session == TEST_NODES[1]
async def test_tx_propagation(event_bus, funded_address_private_key, chain_with_block_validation, tx_validator): initial_two_peers = TEST_NODES[:2] node_one = initial_two_peers[0] node_two = initial_two_peers[1] async with AsyncExitStack() as stack: await stack.enter_async_context(mock_request_response( GetConnectedPeersRequest, GetConnectedPeersResponseFactory.from_sessions(initial_two_peers), event_bus, )) peer_pool = ETHProxyPeerPool(event_bus, TO_NETWORKING_BROADCAST_CONFIG) await stack.enter_async_context(run_service(peer_pool)) tx_pool = TxPool(event_bus, peer_pool, tx_validator) await stack.enter_async_context(background_asyncio_service(tx_pool)) await asyncio.sleep(0.01) txs_broadcasted_by_peer1 = [ create_random_tx(chain_with_block_validation, funded_address_private_key) ] # this needs to go here to ensure that the subscription is *after* # the one installed by the transaction pool so that the got_txns # event will get set after the other handlers have been called. outgoing_tx, got_txns = observe_outgoing_transactions(event_bus) # Peer1 sends some txs await event_bus.broadcast( TransactionsEvent(session=node_one, command=Transactions(txs_broadcasted_by_peer1)) ) await asyncio.wait_for(got_txns.wait(), timeout=0.1) assert outgoing_tx == [ (node_two, tuple(txs_broadcasted_by_peer1)), ] # Clear the recording, we asserted all we want and would like to have a fresh start outgoing_tx.clear() # Peer1 sends same txs again await event_bus.broadcast( TransactionsEvent(session=node_one, command=Transactions(txs_broadcasted_by_peer1)) ) await asyncio.wait_for(got_txns.wait(), timeout=0.1) # Check that Peer2 doesn't receive them again assert len(outgoing_tx) == 0 # Peer2 sends exact same txs back await event_bus.broadcast( TransactionsEvent(session=node_two, command=Transactions(txs_broadcasted_by_peer1)) ) await asyncio.wait_for(got_txns.wait(), timeout=0.1) # Check that Peer1 won't get them as that is where they originally came from assert len(outgoing_tx) == 0 txs_broadcasted_by_peer2 = [ create_random_tx(chain_with_block_validation, funded_address_private_key), txs_broadcasted_by_peer1[0] ] # Peer2 sends old + new tx await event_bus.broadcast( TransactionsEvent(session=node_two, command=Transactions(txs_broadcasted_by_peer2)) ) await asyncio.wait_for(got_txns.wait(), timeout=0.1) # Not sure why this sleep is needed.... await asyncio.sleep(0.01) # Check that Peer1 receives only the one tx that it didn't know about assert outgoing_tx == [ (node_one, (txs_broadcasted_by_peer2[0],)), ]