async def test_connection_properties(): Protocol_A = ProtocolFactory() Protocol_B = ProtocolFactory() alice_handshakers = (NoopHandshaker(Protocol_A), ) bob_handshakers = (NoopHandshaker(Protocol_A), NoopHandshaker(Protocol_B)) bob_capabilities = (Protocol_A.as_capability(), Protocol_B.as_capability()) bob_remote = NodeFactory() bob_private_key = PrivateKeyFactory() pair_factory = ConnectionPairFactory( alice_handshakers=alice_handshakers, bob_handshakers=bob_handshakers, alice_p2p_version=DEVP2P_V4, bob_client_version='bob-client', bob_p2p_version=DEVP2P_V5, bob_private_key=bob_private_key, bob_remote=bob_remote, ) async with pair_factory as (connection, _): assert type(connection.get_base_protocol()) is P2PProtocolV4 assert connection.remote_capabilities == bob_capabilities assert connection.remote_p2p_version == DEVP2P_V5 assert connection.negotiated_p2p_version == DEVP2P_V4 assert connection.remote_public_key == bob_private_key.public_key assert connection.client_version_string == 'bob-client' assert connection.safe_client_version_string == 'bob-client'
def test_node_constructor(): privkey = PrivateKeyFactory() address = AddressFactory() enr = ENRFactory(private_key=privkey.to_bytes(), address=address) node = Node(enr) assert node.id == keccak(privkey.public_key.to_bytes()) assert node.address == address
def test_node_from_enr_uri(): privkey = PrivateKeyFactory() address = AddressFactory() enr = ENRFactory(private_key=privkey.to_bytes(), address=address) node = Node.from_uri(repr(enr)) assert node.id == keccak(privkey.public_key.to_bytes()) assert node.address == address
def test_discover_v4_message_pack(): sender, recipient = AddressFactory.create_batch(2) version = rlp.sedes.big_endian_int.serialize(PROTO_VERSION) payload = (version, sender.to_endpoint(), recipient.to_endpoint()) privkey = PrivateKeyFactory() message = _pack_v4(CMD_PING.id, payload, privkey) pubkey, cmd_id, payload, _ = _unpack_v4(message) assert pubkey == privkey.public_key assert cmd_id == CMD_PING.id
async def test_lookup_and_maybe_update_enr_existing_node(): discovery = MockDiscoveryService([]) privkey = PrivateKeyFactory() address = AddressFactory() # When we have an ENR for the given pubkey, and its address matches the given one, the # existing ENR is returned. enr = ENRFactory(private_key=privkey.to_bytes(), address=address) discovery.enr_db.set_enr(enr) lookedup_enr = discovery.lookup_and_maybe_update_enr(privkey.public_key, address) assert lookedup_enr == enr
async def test_lookup_and_maybe_update_enr_new_node(): discovery = MockDiscoveryService([]) privkey = PrivateKeyFactory() address = AddressFactory() # When looking up the ENR for a node we haven't heard about before, we'll create a stub ENR # and add that into our DB. enr = discovery.lookup_and_maybe_update_enr(privkey.public_key, address) assert enr.sequence_number == 0 node = Node(enr) assert node.pubkey == privkey.public_key assert node.address == address db_enr = discovery.enr_db.get_enr(node.id) assert db_enr == enr
async def test_bootstrap_nodes(): private_key = PrivateKeyFactory().to_bytes() bootnode1 = ENRFactory(private_key=private_key) bootnode2 = ENRFactory() discovery = MockDiscoveryService([Node(bootnode1), Node(bootnode2)]) assert discovery.enr_db.get_enr(bootnode1.node_id) == bootnode1 assert discovery.enr_db.get_enr(bootnode2.node_id) == bootnode2 assert [node.enr for node in discovery.bootstrap_nodes] == [bootnode1, bootnode2] # If our DB gets updated with a newer ENR of one of our bootnodes, the @bootstrap_nodes # property will reflect that. new_bootnode1 = ENRFactory( private_key=private_key, sequence_number=bootnode1.sequence_number + 1) discovery.enr_db.set_enr(new_bootnode1) assert [node.enr for node in discovery.bootstrap_nodes] == [new_bootnode1, bootnode2]
async def test_lookup_and_maybe_update_enr_existing_node_different_address(): discovery = MockDiscoveryService([]) privkey = PrivateKeyFactory() address = AddressFactory() # If the address given is different than the one we have in our DB, though, a stub ENR would # be created and stored in our DB, replacing the existing one. enr = ENRFactory(private_key=privkey.to_bytes(), address=address) discovery.enr_db.set_enr(enr) new_address = AddressFactory() lookedup_enr = discovery.lookup_and_maybe_update_enr(privkey.public_key, new_address) assert lookedup_enr != enr assert lookedup_enr.public_key == enr.public_key assert lookedup_enr.sequence_number == 0 assert Node(lookedup_enr).address == new_address assert lookedup_enr == discovery.enr_db.get_enr(enr.node_id)
def test_node_constructor(): privkey = PrivateKeyFactory() ip = socket.inet_aton(IPAddressFactory.generate()) udp_port = tcp_port = 30303 enr = ENRFactory(private_key=privkey.to_bytes(), custom_kv_pairs={ IP_V4_ADDRESS_ENR_KEY: ip, UDP_PORT_ENR_KEY: udp_port }) node = Node(enr) assert node.id == keccak(privkey.public_key.to_bytes()) assert node.address.ip_packed == ip assert node.address.tcp_port == tcp_port assert node.address.udp_port == udp_port
def _create(cls, model_class: Type[BaseTransactionFields], *args: Any, **kwargs: Any) -> BaseTransactionFields: if 'vrs' in kwargs: v, r, s = kwargs.pop('vrs') else: if 'private_key' in kwargs: private_key = kwargs.pop('private_key') else: private_key = PrivateKeyFactory() tx_for_signing = FrontierUnsignedTransaction(**kwargs) signed_tx = tx_for_signing.as_signed_transaction(private_key) v = signed_tx.v r = signed_tx.r s = signed_tx.s return model_class(**kwargs, v=v, r=r, s=s)
async def get_peer_and_receive_server( request, event_loop, event_bus ) -> Tuple[BCCPeer, BCCRequestServer, BCCReceiveServer, asyncio.Queue]: alice_chain = await get_fake_chain() bob_chain = await get_fake_chain() alice_private_key = PrivateKeyFactory() bob_private_key = PrivateKeyFactory() alice_context = BeaconContextFactory(chain_db=alice_chain.chaindb) bob_context = BeaconContextFactory(chain_db=bob_chain.chaindb) peer_pair = BCCPeerPairFactory( alice_peer_context=alice_context, alice_private_key=alice_private_key, bob_peer_context=bob_context, bob_private_key=bob_private_key, event_bus=event_bus, ) async with peer_pair as (alice, bob): alice_pool_ctx = BCCPeerPoolFactory.run_for_peer( alice, privkey=alice_private_key, context=alice_context, event_bus=event_bus, ) bob_pool_ctx = BCCPeerPoolFactory.run_for_peer( bob, privkey=bob_private_key, context=bob_context, event_bus=event_bus, ) async with alice_pool_ctx as alice_peer_pool, bob_pool_ctx as bob_peer_pool: msg_queue = asyncio.Queue() orig_handle_msg = BCCReceiveServer._handle_msg # Inject a queue to each `BCCReceiveServer`, which puts the message # passed to `_handle_msg` to the queue, right after every `_handle_msg` # finishes. This is crucial to make the test be able to wait until # `_handle_msg` finishes. async def _handle_msg(self, base_peer, cmd, msg): task = asyncio.ensure_future( orig_handle_msg(self, base_peer, cmd, msg)) def enqueue_msg(future, msg): msg_queue.put_nowait(msg) task.add_done_callback(functools.partial(enqueue_msg, msg=msg)) await task BCCReceiveServer._handle_msg = _handle_msg async with run_peer_pool_event_server( event_bus, alice_peer_pool, BCCPeerPoolEventServer), run_request_server( event_bus, alice_chain.chaindb, server_type=BCCRequestServer) as alice_req_server: bob_recv_server = BCCReceiveServer(chain=bob_chain, peer_pool=bob_peer_pool) asyncio.ensure_future(bob_recv_server.run()) await bob_recv_server.events.started.wait() def finalizer(): event_loop.run_until_complete(bob_recv_server.cancel()) request.addfinalizer(finalizer) yield alice, alice_req_server, bob_recv_server, msg_queue