Ejemplo n.º 1
0
async def test_does_not_propagate_invalid_tx(event_bus,
                                             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 run_proxy_peer_pool(event_bus) as peer_pool:
        outgoing_tx = observe_outgoing_transactions(event_bus)
        tx_pool = TxPool(event_bus, peer_pool, tx_validator)
        asyncio.ensure_future(tx_pool.run())

        run_mock_request_response(GetConnectedPeersRequest,
                                  GetConnectedPeersResponse(initial_two_peers),
                                  event_bus)

        await asyncio.sleep(0.01)

        txs_broadcasted_by_peer1 = [
            create_random_tx(chain_with_block_validation, is_valid=False),
            create_random_tx(chain_with_block_validation)
        ]

        # Peer1 sends some txs
        await event_bus.broadcast(
            TransactionsEvent(remote=node_one,
                              msg=txs_broadcasted_by_peer1,
                              cmd=Transactions))
        await asyncio.sleep(0.01)

        # Check that Peer2 received only the second tx which is valid
        assert outgoing_tx == [
            (node_two, [txs_broadcasted_by_peer1[1]]),
        ]
Ejemplo n.º 2
0
async def test_does_not_propagate_invalid_tx(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 run_proxy_peer_pool(event_bus) as peer_pool:
        tx_pool = TxPool(event_bus, peer_pool, tx_validator)
        async with run_service(tx_pool):
            run_mock_request_response(
                GetConnectedPeersRequest, GetConnectedPeersResponse(initial_two_peers), event_bus)

            await asyncio.sleep(0.01)

            txs_broadcasted_by_peer1 = [
                create_random_tx(chain_with_block_validation, funded_address_private_key, is_valid=False),  # noqa: E501
                create_random_tx(chain_with_block_validation, 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],)),
            ]
Ejemplo n.º 3
0
 def from_sessions(cls, sessions: Sequence[SessionAPI], *args: Any,
                   **kwargs: Any) -> GetConnectedPeersResponse:
     return GetConnectedPeersResponse(
         tuple(
             PeerInfo(session=session,
                      capabilities=(),
                      client_version_string='unknown',
                      inbound=False) for session in sessions))
Ejemplo n.º 4
0
async def test_adds_new_peers(event_bus):

    async with run_proxy_peer_pool(event_bus) as proxy_peer_pool:
        run_mock_request_response(GetConnectedPeersRequest,
                                  GetConnectedPeersResponse((TEST_NODES[0], )),
                                  event_bus)

        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
Ejemplo n.º 5
0
async def test_removes_peers(event_bus):

    async with run_proxy_peer_pool(event_bus) as proxy_peer_pool:
        run_mock_request_response(GetConnectedPeersRequest,
                                  GetConnectedPeersResponse(TEST_NODES[:2]),
                                  event_bus)

        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].remote is TEST_NODES[1]
Ejemplo n.º 6
0
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_adds_new_peers(event_bus):

    do_mock = mock_request_response(
        GetConnectedPeersRequest,
        GetConnectedPeersResponse((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_removes_peers(event_bus):
    do_mock = mock_request_response(
        GetConnectedPeersRequest,
        GetConnectedPeersResponse(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]
Ejemplo n.º 9
0
async def test_tx_propagation(event_bus, 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 run_proxy_peer_pool(event_bus) as peer_pool:
        outgoing_tx = observe_outgoing_transactions(event_bus)
        tx_pool = TxPool(event_bus, peer_pool, tx_validator)
        asyncio.ensure_future(tx_pool.run())

        run_mock_request_response(GetConnectedPeersRequest,
                                  GetConnectedPeersResponse(initial_two_peers),
                                  event_bus)

        await asyncio.sleep(0.01)

        txs_broadcasted_by_peer1 = [
            create_random_tx(chain_with_block_validation)
        ]

        # Peer1 sends some txs
        await event_bus.broadcast(
            TransactionsEvent(remote=node_one,
                              msg=txs_broadcasted_by_peer1,
                              cmd=Transactions))

        await asyncio.sleep(0.01)
        assert outgoing_tx == [
            (node_two, 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(remote=node_one,
                              msg=txs_broadcasted_by_peer1,
                              cmd=Transactions))
        await asyncio.sleep(0.01)
        # Check that Peer2 doesn't receive them again
        assert len(outgoing_tx) == 0

        # Peer2 sends exact same txs back
        await event_bus.broadcast(
            TransactionsEvent(remote=node_two,
                              msg=txs_broadcasted_by_peer1,
                              cmd=Transactions))
        await asyncio.sleep(0.01)

        # 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),
            txs_broadcasted_by_peer1[0]
        ]

        # Peer2 sends old + new tx
        await event_bus.broadcast(
            TransactionsEvent(remote=node_two,
                              msg=txs_broadcasted_by_peer2,
                              cmd=Transactions))
        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]]),
        ]
Ejemplo n.º 10
0
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 run_proxy_peer_pool(event_bus) as peer_pool:
        tx_pool = TxPool(event_bus, peer_pool, tx_validator)
        async with run_service(tx_pool):

            run_mock_request_response(
                GetConnectedPeersRequest, GetConnectedPeersResponse(initial_two_peers), event_bus)

            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],)),
            ]
Ejemplo n.º 11
0
    run_mock_request_response,
)

TEST_NODES = tuple(NodeFactory() for i in range(4))


@pytest.mark.asyncio
async def test_can_instantiate_proxy_pool(event_bus):
    async with run_proxy_peer_pool(event_bus) as proxy_peer_pool:
        assert proxy_peer_pool is not None


@pytest.mark.parametrize(
    "response, expected_count",
    (
        (GetConnectedPeersResponse(tuple()), 0),
        (GetConnectedPeersResponse(TEST_NODES), 4),
    ),
)
@pytest.mark.asyncio
async def test_fetch_initial_peers(event_bus, response, expected_count):

    run_mock_request_response(GetConnectedPeersRequest, response, event_bus)

    async with run_proxy_peer_pool(event_bus) as proxy_peer_pool:
        peers = await proxy_peer_pool.fetch_initial_peers()
        assert len(peers) == expected_count


@pytest.mark.parametrize(
    "response, expected_count",