Example #1
0
        total_difficulty = await wait(headerdb.coro_get_score(head.hash))
        genesis_hash = await wait(
            headerdb.coro_get_canonical_block_hash(
                BlockNumber(GENESIS_BLOCK_NUMBER)))

        handshake_params = ETHHandshakeParams(
            head_hash=head.hash,
            total_difficulty=total_difficulty,
            genesis_hash=genesis_hash,
            network_id=self.context.network_id,
            version=ETHProtocol.version,
        )
        return (ETHHandshaker(handshake_params), )


async_fire_and_forget = async_suppress_exceptions(
    PeerConnectionLost, asyncio.TimeoutError)  # type: ignore  # noqa: E501


class ETHPeerPoolEventServer(PeerPoolEventServer[ETHPeer]):
    """
    ETH protocol specific ``PeerPoolEventServer``. See ``PeerPoolEventServer`` for more info.
    """

    subscription_msg_types = frozenset({
        GetBlockHeaders,
        GetBlockBodies,
        GetReceipts,
        GetNodeData,
        Transactions,
        NewBlockHashes,
        NewBlock,
Example #2
0
    GetConnectedPeersResponse,
    GetProtocolCapabilitiesRequest,
    PeerCountRequest,
    PeerCountResponse,
    PeerJoinedEvent,
    PeerLeftEvent,
    ProtocolCapabilitiesResponse,
    PeerPoolMessageEvent,
)
from .peer import BaseProxyPeer

TPeer = TypeVar('TPeer', bound=BasePeer)
TEvent = TypeVar('TEvent', bound=BaseEvent)
TRequest = TypeVar('TRequest', bound=BaseRequestResponseEvent[Any])

async_fire_and_forget = async_suppress_exceptions(PeerConnectionLost,
                                                  asyncio.TimeoutError)


class PeerPoolEventServer(Service, PeerSubscriber, Generic[TPeer]):
    """
    Base class to create a bridge between the ``PeerPool`` and the event bus so that peer
    messages become available to external processes (e.g. isolated components). In the opposite
    direction, other processes can also retrieve information or execute actions on the peer pool by
    sending specific events through the event bus that the ``PeerPoolEventServer`` answers.

    This class bridges all common APIs but protocol specific communication can be enabled through
    subclasses that add more handlers.
    """
    logger = get_logger('trinity.protocol.common.PeerPoolEventServer')

    msg_queue_maxsize: int = 2000