def _test_get_outbound_peer_addresses(self, initiate_handshake,
                                          expected_node_con_protocol):
        node = self._set_up_test_node(initiate_handshake,
                                      generate_pub_key=True)
        assert isinstance(node, EthGatewayNode)

        peer_connections = node.get_outbound_peer_info()
        self.assertTrue(peer_connections)

        self.assertEqual(len(self.servers) + 1, len(peer_connections))

        for server in self.servers:
            peer_connection = [
                peer for peer in peer_connections
                if peer.endpoint.ip_address == server.ip
                and peer.endpoint.port == server.port
            ][0]

            self.assertEqual(IpEndpoint(server.ip, server.port),
                             peer_connection.endpoint)

        blockchain_connection = peer_connections[len(self.servers)]

        self.assertEqual(IpEndpoint(self.blockchain_ip, self.blockchain_port),
                         blockchain_connection.endpoint)
        self.assertEqual(expected_node_con_protocol,
                         blockchain_connection.transport_protocol)
Example #2
0
    def get_outbound_peer_info(self) -> List[ConnectionPeerInfo]:
        peers = []

        for peer in self.opts.outbound_peers:
            peers.append(
                ConnectionPeerInfo(
                    IpEndpoint(peer.ip, peer.port),
                    convert.peer_node_to_connection_type(
                        self.NODE_TYPE, peer.node_type)))

        local_protocol = TransportLayerProtocol.UDP if self._is_in_local_discovery(
        ) else TransportLayerProtocol.TCP
        for blockchain_peer in self.blockchain_peers:
            peers.append(
                ConnectionPeerInfo(
                    IpEndpoint(blockchain_peer.ip, blockchain_peer.port),
                    ConnectionType.BLOCKCHAIN_NODE, local_protocol))

        if self.remote_blockchain_ip is not None and self.remote_blockchain_port is not None:
            remote_protocol = TransportLayerProtocol.UDP if self._is_in_remote_discovery() else \
                TransportLayerProtocol.TCP
            peers.append(
                ConnectionPeerInfo(
                    # pyre-fixme[6]: Expected `str` for 1st param but got `Optional[str]`.
                    IpEndpoint(self.remote_blockchain_ip,
                               self.remote_blockchain_port),
                    ConnectionType.REMOTE_BLOCKCHAIN_NODE,
                    remote_protocol))

        return peers
Example #3
0
    def test_enqueue_connection(self):

        self.assertNotIn(
            ConnectionPeerInfo(IpEndpoint(self.ip, self.port), ConnectionType.RELAY_BLOCK),
            self.node.pending_connection_requests
        )
        self.node.enqueue_connection(self.ip, self.port, ConnectionType.GATEWAY)
        self.assertIn(
            ConnectionPeerInfo(IpEndpoint(self.ip, self.port), ConnectionType.GATEWAY),
            self.node.pending_connection_requests
        )
 def test_dequeue_connection_requests(self):
     self.assertIsNone(self.node.dequeue_connection_requests())
     self.node.pending_connection_requests.add(
         ConnectionPeerInfo(IpEndpoint(self.ip, self.port),
                            ConnectionType.RELAY_BLOCK))
     self.node.pending_connection_requests.add(
         ConnectionPeerInfo(IpEndpoint(self.ip, self.port),
                            ConnectionType.RELAY_TRANSACTION))
     pending_connection_requests = self.node.dequeue_connection_requests()
     self.assertEqual(1, len(pending_connection_requests))
     peer_info = next(iter(pending_connection_requests))
     self.assertEqual(IpEndpoint(self.ip, self.port), peer_info.endpoint)
     self.assertEqual(ConnectionType.RELAY_BLOCK, peer_info.connection_type)
     self.assertIsNone(self.node.dequeue_connection_requests())
Example #5
0
def _blockchain_connections_state_info(
        connections: List[BlockchainConnection]) -> Dict[str, ConnectionState]:
    connection_states = {}
    for conn in connections:
        connection_states[str(IpEndpoint(conn.ip_address, int(conn.port)))] = \
            ConnectionState.ESTABLISHED if _check_connections_established([conn]) else ConnectionState.DISCONNECTED
    return connection_states
Example #6
0
    def setUp(self):
        opts = gateway_helpers.get_gateway_opts(
            8000,
            include_default_eth_args=True,
            track_detailed_sent_messages=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()

        self.node = MockGatewayNode(opts)
        self.node.block_processing_service = MagicMock()

        self.connection = MagicMock()
        gateway_helpers.add_blockchain_peer(self.node, self.connection)
        self.connection.node = self.node
        self.connection.peer_ip = LOCALHOST
        self.connection.peer_port = 8001
        self.connection.network_num = 2
        self.connection.endpoint = IpEndpoint(self.connection.peer_ip,
                                              self.connection.peer_port)
        self.node.blockchain_peers.add(
            BlockchainPeerInfo(self.connection.peer_ip,
                               self.connection.peer_port))
        gateway_bdn_performance_stats_service.set_node(self.node)

        dummy_private_key = crypto_utils.make_private_key(
            helpers.generate_bytearray(111))
        dummy_public_key = crypto_utils.private_to_public_key(
            dummy_private_key)
        self.sut = EthNodeConnectionProtocol(self.connection, True,
                                             dummy_private_key,
                                             dummy_public_key)
Example #7
0
def add_stats_to_node_stats(
    node_stats: Dict[IpEndpoint, BdnPerformanceStatsData],
    ip: str,
    port: int,
    new_blocks_from_node: int,
    new_blocks_from_bdn: int,
    new_tx_from_node: int,
    new_tx_from_bdn: int,
    new_blocks_seen: int,
    new_block_messages_from_node: int,
    new_block_announcements_from_node: int,
    tx_sent_to_node: int,
    duplicate_tx_from_node: int
) -> None:
    new_node_stats = BdnPerformanceStatsData()
    new_node_stats.new_blocks_received_from_blockchain_node = new_blocks_from_node
    new_node_stats.new_blocks_received_from_bdn = new_blocks_from_bdn
    new_node_stats.new_blocks_seen = new_blocks_seen
    new_node_stats.new_block_messages_from_blockchain_node = new_block_messages_from_node
    new_node_stats.new_block_announcements_from_blockchain_node = new_block_announcements_from_node
    new_node_stats.new_tx_received_from_blockchain_node = new_tx_from_node
    new_node_stats.new_tx_received_from_bdn = new_tx_from_bdn
    new_node_stats.tx_sent_to_node = tx_sent_to_node
    new_node_stats.duplicate_tx_from_node = duplicate_tx_from_node
    node_stats[IpEndpoint(ip, port)] = new_node_stats
Example #8
0
    def __init__(
        self,
        file_no,
        node,
        default_socket_opts=None,
        send_bytes=False,
        ip_address: str = "127.0.0.1",
        port: int = 8000,
        is_ssl: bool = False,
        authenticated_peer_info: AuthenticatedPeerInfo = AuthenticatedPeerInfo(
            ConnectionType.EXTERNAL_GATEWAY, "", ""),
    ):
        super(MockSocketConnection,
              self).__init__(node, IpEndpoint(ip_address, port), is_ssl=is_ssl)
        self.transport = MagicMock()
        if default_socket_opts is None:
            default_socket_opts = {(socket.SOL_SOCKET, socket.SO_SNDBUF): 4096}

        self.file_no = file_no
        # TODO: temporary fix for some situations where, see https://bloxroute.atlassian.net/browse/BX-1153
        self._send_bytes = send_bytes

        self.bytes_sent = []
        self.socket_opts: Dict[Tuple[int, int], Any] = default_socket_opts

        self.transport.write = self.socket_instance_send
        self.transport.get_write_buffer_size = MagicMock(return_value=0)

        self.authenticated_peer_info = authenticated_peer_info
        self.initialized = True
Example #9
0
 def get_outbound_peer_info(self) -> List[ConnectionPeerInfo]:
     return [
         ConnectionPeerInfo(
             IpEndpoint(peer.ip, peer.port),
             convert.peer_node_to_connection_type(self.NODE_TYPE,
                                                  peer.node_type))
         for peer in self.outbound_peers
     ]
Example #10
0
    def test_retry_init_client_socket(self):
        self.node._retry_init_client_socket(self.ip, self.port, ConnectionType.RELAY_ALL)
        self.assertIn(
            ConnectionPeerInfo(IpEndpoint(self.ip, self.port), ConnectionType.RELAY_ALL),
            self.node.pending_connection_requests
        )
        self.assertEqual(1, self.node.num_retries_by_ip[(self.ip, self.port)])

        self.node._retry_init_client_socket(self.ip, self.port, ConnectionType.RELAY_ALL)
        self.assertEqual(2, self.node.num_retries_by_ip[(self.ip, self.port)])
Example #11
0
    def test_bdn_performance_stats_message_one_node(self):
        start_time = datetime.utcnow()
        memory_utilization_mb = 700
        node_1_bdn_stats = BdnPerformanceStatsData()
        node_1_bdn_stats.new_blocks_received_from_blockchain_node = 100
        node_1_bdn_stats.new_blocks_received_from_bdn = 200
        node_1_bdn_stats.new_tx_received_from_blockchain_node = 300
        node_1_bdn_stats.new_tx_received_from_bdn = constants.UNSIGNED_SHORT_MAX_VALUE + 1  # unsigned short max (0xffff) + 1
        node_1_bdn_stats.new_blocks_seen = 800
        node_1_bdn_stats.new_block_messages_from_blockchain_node = 900
        node_1_bdn_stats.new_block_announcements_from_blockchain_node = 1000
        node_1_bdn_stats.tx_sent_to_node = 1100
        node_1_bdn_stats.duplicate_tx_from_node = 600
        end_time = datetime.utcnow()

        node_stats = {}
        node_1_ip = "127.0.0.1"
        node_1_port = 8001
        node_stats[IpEndpoint(node_1_ip, node_1_port)] = node_1_bdn_stats

        bdn_stats_msg = self.create_message_successfully(
            BdnPerformanceStatsMessage(start_time, end_time,
                                       memory_utilization_mb, node_stats),
            BdnPerformanceStatsMessage)

        ip_endpoint, stats = bdn_stats_msg.node_stats().popitem()
        self.assertEqual(start_time, bdn_stats_msg.interval_start_time())
        self.assertEqual(end_time, bdn_stats_msg.interval_end_time())
        self.assertEqual(
            node_1_bdn_stats.new_blocks_received_from_blockchain_node,
            stats.new_blocks_received_from_blockchain_node)
        self.assertEqual(node_1_bdn_stats.new_blocks_received_from_bdn,
                         stats.new_blocks_received_from_bdn)
        self.assertEqual(node_1_bdn_stats.new_tx_received_from_blockchain_node,
                         stats.new_tx_received_from_blockchain_node)
        self.assertEqual(node_1_bdn_stats.new_tx_received_from_bdn,
                         stats.new_tx_received_from_bdn)
        self.assertEqual(memory_utilization_mb,
                         bdn_stats_msg.memory_utilization())
        self.assertEqual(node_1_bdn_stats.new_blocks_seen,
                         stats.new_blocks_seen)
        self.assertEqual(
            node_1_bdn_stats.new_block_messages_from_blockchain_node,
            stats.new_block_messages_from_blockchain_node)
        self.assertEqual(
            node_1_bdn_stats.new_block_announcements_from_blockchain_node,
            stats.new_block_announcements_from_blockchain_node)
        self.assertEqual(node_1_bdn_stats.tx_sent_to_node,
                         stats.tx_sent_to_node)
        self.assertEqual(node_1_bdn_stats.duplicate_tx_from_node,
                         stats.duplicate_tx_from_node)
        self.assertEqual(node_1_ip, ip_endpoint.ip_address)
        self.assertEqual(node_1_port, ip_endpoint.port)
Example #12
0
 def _iter_outbound_peers(
         self) -> Generator[ConnectionPeerInfo, None, None]:
     sdn_address = self._node.get_sdn_address()
     if sdn_address:
         yield ConnectionPeerInfo(IpEndpoint(*sdn_address),
                                  ConnectionType.SDN)
     else:
         logger.debug(
             "SDN address not provided, skipping connection. This is expected for gateways."
         )
     for peer_info in self._node.get_outbound_peer_info():
         yield peer_info
Example #13
0
 def enqueue_connection(self, ip: str, port: int,
                        connection_type: ConnectionType):
     """
     Queues a connection up for the event loop to open a socket for.
     """
     peer_info = ConnectionPeerInfo(IpEndpoint(ip, port), connection_type)
     if peer_info in self.pending_connection_attempts:
         logger.debug(
             "Not adding {}, waiting until connection attempt to complete",
             peer_info)
     else:
         logger.trace("Enqueuing connection: {}.", peer_info)
         self.pending_connection_requests.add(peer_info)
Example #14
0
def update(conn_pool: ConnectionPool, use_ext: bool, src_ver: str,
           ip_address: str, continent: str, country: str,
           update_required: bool, blockchain_peers: Set[BlockchainPeerInfo],
           account_id: Optional[str],
           quota_level: Optional[int]) -> Diagnostics:
    path = config.get_data_file(STATUS_FILE_NAME)
    if not os.path.exists(path):
        initialize(use_ext, src_ver, ip_address, continent, country,
                   update_required, account_id, quota_level)
    diagnostics = _load_status_from_file(use_ext, src_ver, ip_address,
                                         continent, country, update_required,
                                         account_id, quota_level)
    analysis = diagnostics.analysis
    network = analysis.network

    for network_type, individual_network in network.iter_network_type_pairs():
        for conn in individual_network:
            if conn.get_connection_state() == ConnectionState.DISCONNECTED or \
                    not conn_pool.has_connection(conn.ip_address, int(conn.port)):
                network.remove_connection(conn, network_type)

    for conn_type in CONN_TYPES:
        for conn in conn_pool.get_by_connection_types([conn_type]):
            network.add_connection(conn.CONNECTION_TYPE, conn.peer_desc,
                                   str(conn.file_no), conn.peer_id)

    for blockchain_peer in blockchain_peers:
        blockchain_ip_endpoint = IpEndpoint(blockchain_peer.ip,
                                            blockchain_peer.port)
        blockchain_conn = None
        if conn_pool.has_connection(blockchain_peer.ip, blockchain_peer.port):
            blockchain_conn = conn_pool.get_by_ipport(blockchain_peer.ip,
                                                      blockchain_peer.port)
        if blockchain_conn:
            network.add_connection(ConnectionType.BLOCKCHAIN_NODE,
                                   str(blockchain_ip_endpoint),
                                   str(blockchain_conn.file_no),
                                   blockchain_conn.peer_id)
        else:
            network.add_connection(ConnectionType.BLOCKCHAIN_NODE,
                                   str(blockchain_ip_endpoint), None, None)

    summary = network.get_summary(ip_address, continent, country,
                                  update_required, account_id, quota_level)
    assert summary.gateway_status is not None
    # pyre-fixme[16]: `Optional` has no attribute `value`.
    gateway_status.state(summary.gateway_status.value)
    diagnostics = Diagnostics(summary, analysis)

    _save_status_to_file(diagnostics)
    return diagnostics
Example #15
0
 async def setUp(self) -> None:
     self.gateway_node = MockGatewayNode(self.get_gateway_opts())
     self.gateway_node.requester = ThreadedRequestService(
         "mock_thread_service", self.gateway_node.alarm_queue,
         constants.THREADED_HTTP_POOL_SLEEP_INTERVAL_S)
     self.gateway_node.requester.start()
     self.gateway_node.account_id = ACCOUNT_ID
     self.node_endpoint_1 = IpEndpoint("127.0.0.1", 7000)
     self.blockchain_connection_1 = EthBaseConnection(
         MockSocketConnection(node=self.gateway_node,
                              ip_address="127.0.0.1",
                              port=7000),
         cast(EthGatewayNode, self.gateway_node))
     self.blockchain_connection_1.state = ConnectionState.ESTABLISHED
    def _unpack(self) -> None:
        buf = self.buf
        assert buf is not None
        node_stats = {}

        off = AbstractBloxrouteMessage.HEADER_LENGTH
        self._interval_start_time, = struct.unpack_from("<d", buf, off)
        off += constants.DOUBLE_SIZE_IN_BYTES
        self._interval_end_time, = struct.unpack_from("<d", buf, off)
        off += constants.DOUBLE_SIZE_IN_BYTES
        self._memory_utilization_mb, = struct.unpack_from("<H", buf, off)
        off += constants.UL_SHORT_SIZE_IN_BYTES

        num_peers, = struct.unpack_from("<H", buf, off)
        off += constants.UL_SHORT_SIZE_IN_BYTES
        for _ in range(num_peers):
            ip, port = message_utils.unpack_ip_port(
                self._memoryview[off:].tobytes())
            off += constants.IP_ADDR_SIZE_IN_BYTES + constants.UL_SHORT_SIZE_IN_BYTES
            single_node_stats = BdnPerformanceStatsData()
            single_node_stats.new_blocks_received_from_blockchain_node, = struct.unpack_from(
                "<H", buf, off)
            off += constants.UL_SHORT_SIZE_IN_BYTES
            single_node_stats.new_blocks_received_from_bdn, = struct.unpack_from(
                "<H", buf, off)
            off += constants.UL_SHORT_SIZE_IN_BYTES
            single_node_stats.new_tx_received_from_blockchain_node, = struct.unpack_from(
                "<I", buf, off)
            off += constants.UL_INT_SIZE_IN_BYTES
            single_node_stats.new_tx_received_from_bdn, = struct.unpack_from(
                "<I", buf, off)
            off += constants.UL_INT_SIZE_IN_BYTES
            single_node_stats.new_blocks_seen, = struct.unpack_from(
                "<I", buf, off)
            off += constants.UL_INT_SIZE_IN_BYTES
            single_node_stats.new_block_messages_from_blockchain_node, = struct.unpack_from(
                "<I", buf, off)
            off += constants.UL_INT_SIZE_IN_BYTES
            single_node_stats.new_block_announcements_from_blockchain_node, = struct.unpack_from(
                "<I", buf, off)
            off += constants.UL_INT_SIZE_IN_BYTES
            single_node_stats.tx_sent_to_node, = struct.unpack_from(
                "<I", buf, off)
            off += constants.UL_INT_SIZE_IN_BYTES
            single_node_stats.duplicate_tx_from_node, = struct.unpack_from(
                "<I", buf, off)
            off += constants.UL_INT_SIZE_IN_BYTES
            node_stats[IpEndpoint(ip, port)] = single_node_stats
        self._node_stats = node_stats
Example #17
0
    async def test_bdn_performance_multi_node(self):
        self.gateway_node.connection_pool.add(20,
                                              self.node_endpoint_1.ip_address,
                                              self.node_endpoint_1.port,
                                              self.blockchain_connection_1)

        blockchain_connection_2 = EthNodeConnection(
            MockSocketConnection(1,
                                 node=self.gateway_node,
                                 ip_address="127.0.0.1",
                                 port=333), self.gateway_node)
        blockchain_connection_2.on_connection_established()
        self.gateway_node.mock_add_blockchain_peer(blockchain_connection_2)
        node_endpoint_2 = IpEndpoint("127.0.0.1", 333)
        self.gateway_node.connection_pool.add(21, node_endpoint_2.ip_address,
                                              node_endpoint_2.port,
                                              blockchain_connection_2)

        gateway_bdn_performance_stats_service.set_node(self.gateway_node)
        gateway_bdn_performance_stats_service.log_block_from_bdn()
        gateway_bdn_performance_stats_service.log_block_from_bdn()
        gateway_bdn_performance_stats_service.log_block_from_blockchain_node(
            self.node_endpoint_1)
        gateway_bdn_performance_stats_service.log_tx_from_bdn()
        gateway_bdn_performance_stats_service.log_tx_from_blockchain_node(
            self.node_endpoint_1)
        gateway_bdn_performance_stats_service.log_tx_from_blockchain_node(
            self.node_endpoint_1)
        gateway_bdn_performance_stats_service.close_interval_data()

        result = await self.request(
            BxJsonRpcRequest("6", RpcRequestType.BDN_PERFORMANCE, None))
        self.assertEqual("6", result.id)
        self.assertIsNone(result.error)
        node_stats = result.result[str(self.node_endpoint_1)]
        self.assertEqual("66.67%", node_stats["blocks_from_bdn_percentage"])
        self.assertEqual("33.33%",
                         node_stats["transactions_from_bdn_percentage"])
        self.assertEqual(3, node_stats["total_blocks_seen"])

        node_stats_2 = result.result[str(node_endpoint_2)]
        self.assertEqual("100.00%", node_stats_2["blocks_from_bdn_percentage"])
        self.assertEqual("100.00%",
                         node_stats_2["transactions_from_bdn_percentage"])
        self.assertEqual(3, node_stats_2["total_blocks_seen"])
    def connection_made(self, transport: BaseTransport) -> None:
        self.transport = typing.cast(Transport, transport)

        sock = transport.get_extra_info("socket")
        self.file_no = sock.fileno()
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

        if self._node.opts.enable_tcp_quickack:
            self.enable_tcp_quickack()

        if self.direction == NetworkDirection.INBOUND:
            self.endpoint = IpEndpoint(*transport.get_extra_info("peername"))
            logger.debug("[{}] - accepted connection.", self)
        self._node.on_connection_added(self)
        self.state = SocketConnectionStates.INITIALIZED
        self.can_send = True
        self.send()
        logger.debug("[{}] - connection established successfully.", self)
    def setUp(self):
        opts = gateway_helpers.get_gateway_opts(8000,
                                                include_default_btc_args=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        self.node = MockGatewayNode(opts)
        self.node.block_processing_service = MagicMock()

        self.connection = MagicMock()
        gateway_helpers.add_blockchain_peer(self.node, self.connection)
        self.connection.node = self.node
        self.connection.peer_ip = LOCALHOST
        self.connection.peer_port = 8001
        self.connection.network_num = 2
        self.connection.endpoint = IpEndpoint(self.connection.peer_ip,
                                              self.connection.peer_port)
        self.node.blockchain_peers.add(
            BlockchainPeerInfo(self.connection.peer_ip,
                               self.connection.peer_port))
        gateway_bdn_performance_stats_service.set_node(self.node)

        self.sut = BtcBaseConnectionProtocol(self.connection)
Example #20
0
    def connection_made(self, transport: BaseTransport) -> None:
        transport = typing.cast(Transport, transport)

        if self._endpoint is None:
            self._endpoint = IpEndpoint(*transport.get_extra_info("peername"))

        endpoint = self._endpoint
        assert endpoint is not None

        if self._is_server:
            try:
                cert = self.get_peer_certificate(transport)
                key = extensions_factory.get_node_id(cert)
                if key is None:
                    key = endpoint.ip_address
            except TypeError:
                key = endpoint.ip_address
            attempts = self._node.report_connection_attempt(key)
            if attempts >= constants.MAX_HIGH_RECONNECT_ATTEMPTS_ALLOWED:
                logger.debug(
                    "Rejecting connection attempt from {} / {}. Too many attempts: {}",
                    self._endpoint, key, attempts)
                # transport.abort()
                # return

        if sys.version.startswith("3.6."):
            protocol_cls = SocketConnectionProtocolPy36
        else:
            from bxcommon.network.socket_connection_protocol import SocketConnectionProtocol
            protocol_cls = SocketConnectionProtocol

        if self._is_server:
            endpoint = None
        delegate_protocol = protocol_cls(self._node, endpoint, self.is_ssl)
        delegate_protocol.connection_made(transport)
        self._delegate_protocol = delegate_protocol
Example #21
0
    def __init__(self,
                 opts: CommonOpts,
                 node_ssl_service: NodeSSLService,
                 connection_pool: Optional[ConnectionPool] = None):
        self.node_ssl_service = node_ssl_service
        logger.debug("Initializing node of type: {}", self.NODE_TYPE)
        self.server_endpoints = [
            IpEndpoint(constants.LISTEN_ON_IP_ADDRESS, opts.external_port),
            # TODO: remove this after v1 is no longer supported
            IpEndpoint(constants.LISTEN_ON_IP_ADDRESS, opts.non_ssl_port)
        ]

        self.set_node_config_opts_from_sdn(opts)
        self.opts: CommonOpts = opts
        self.pending_connection_requests: Set[ConnectionPeerInfo] = set()
        self.pending_connection_attempts: Set[ConnectionPeerInfo] = set()
        self.outbound_peers: Set[OutboundPeerModel] = opts.outbound_peers.copy(
        )

        if connection_pool is not None:
            self.connection_pool = connection_pool
        else:
            self.connection_pool = ConnectionPool()

        self.should_force_exit = False
        self.should_restart_on_high_memory = False

        self.num_retries_by_ip: Dict[Tuple[str, int], int] = defaultdict(int)

        # Event handling queue for delayed events
        self.alarm_queue = AlarmQueue()

        self.init_node_status_logging()
        self.init_throughput_logging()
        self.init_node_info_logging()
        self.init_memory_stats_logging()
        self.init_block_stats_logging()
        self.init_tx_stats_logging()

        # TODO: clean this up alongside outputbuffer holding time
        # this is Nagle's algorithm and we need to implement it properly
        # flush buffers regularly because of output buffer holding time
        self.alarm_queue.register_approx_alarm(
            self.FLUSH_SEND_BUFFERS_INTERVAL,
            constants.OUTPUT_BUFFER_BATCH_MAX_HOLD_TIME,
            self.flush_all_send_buffers)

        self.network_num = opts.blockchain_network_num
        self.broadcast_service = self.get_broadcast_service()

        # converting setting in MB to bytes
        self.next_report_mem_usage_bytes = self.opts.dump_detailed_report_at_memory_usage * 1024 * 1024

        if opts.dump_removed_short_ids:
            os.makedirs(opts.dump_removed_short_ids_path, exist_ok=True)

        # each time a network has an update regarding txs, blocks, etc. register in a dict,
        # this way can verify if node lost connection to requested relay.

        self.last_sync_message_received_by_network: Dict[int, float] = {}

        self.start_sync_time: Optional[float] = None
        self.sync_metrics: Dict[int, Counter] = defaultdict(Counter)
        self.sync_short_id_buckets: Dict[
            int,
            TransactionShortIdBuckets] = defaultdict(TransactionShortIdBuckets)

        opts.has_fully_updated_tx_service = False

        self.check_sync_relay_connections_alarm_id: Optional[AlarmId] = None
        self.transaction_sync_timeout_alarm_id: Optional[AlarmId] = None

        self.requester = ThreadedRequestService(
            # pyre-fixme[16]: `Optional` has no attribute `name`.
            self.NODE_TYPE.name.lower(),
            self.alarm_queue,
            constants.THREADED_HTTP_POOL_SLEEP_INTERVAL_S)

        self._last_responsiveness_check_log_time = time.time()
        self._last_responsiveness_check_details = {}
        self.gc_logging_enabled = False
        self.serialized_message_cache = SerializedMessageCache(
            self.alarm_queue)

        self.alarm_queue.register_alarm(
            constants.RESPONSIVENESS_CHECK_INTERVAL_S,
            self._responsiveness_check_log)
Example #22
0
    def setUp(self):
        self.node = MockGatewayNode(gateway_helpers.get_gateway_opts(
            8000, include_default_eth_args=True, use_extensions=True),
                                    block_queueing_cls=MagicMock())
        self.node.message_converter = converter_factory.create_eth_message_converter(
            self.node.opts)
        self.node.block_processing_service = BlockProcessingService(self.node)

        is_handshake_initiator = True
        dummy_private_key = crypto_utils.make_private_key(
            helpers.generate_bytearray(111))
        dummy_public_key = crypto_utils.private_to_public_key(
            dummy_private_key)
        rlpx_cipher = RLPxCipher(is_handshake_initiator, dummy_private_key,
                                 dummy_public_key)

        node_ssl_service = MockNodeSSLService(EthGatewayNode.NODE_TYPE,
                                              MagicMock())
        local_ip = "127.0.0.1"
        eth_port = 30303
        eth_opts = gateway_helpers.get_gateway_opts(
            1234,
            include_default_eth_args=True,
            blockchain_address=(local_ip, eth_port),
            pub_key=convert.bytes_to_hex(dummy_public_key))
        self.eth_node = EthGatewayNode(eth_opts, node_ssl_service)
        self.blockchain_connection = EthNodeConnection(
            MockSocketConnection(1,
                                 node=self.node,
                                 ip_address=local_ip,
                                 port=30303), self.node)
        self.blockchain_connection.on_connection_established()
        self.node.connection_pool.add(19, local_ip, eth_port,
                                      self.blockchain_connection)

        self.blockchain_connection_1 = EthNodeConnection(
            MockSocketConnection(1,
                                 node=self.node,
                                 ip_address=local_ip,
                                 port=333), self.node)
        self.blockchain_connection_1.on_connection_established()
        self.node.mock_add_blockchain_peer(self.blockchain_connection_1)
        self.node_1_endpoint = IpEndpoint(local_ip, 333)
        self.node.connection_pool.add(20, self.node_1_endpoint.ip_address,
                                      self.node_1_endpoint.port,
                                      self.blockchain_connection_1)

        self.blockchain_connection_2 = EthNodeConnection(
            MockSocketConnection(1,
                                 node=self.node,
                                 ip_address=local_ip,
                                 port=444), self.node)
        self.blockchain_connection_2.on_connection_established()
        self.node.mock_add_blockchain_peer(self.blockchain_connection_2)
        self.node_2_endpoint = IpEndpoint(local_ip, 444)
        self.node.connection_pool.add(21, self.node_2_endpoint.ip_address,
                                      self.node_2_endpoint.port,
                                      self.blockchain_connection_2)

        self.blockchain_connection_1.network_num = 0

        self.tx_blockchain_connection_protocol = EthNodeConnectionProtocol(
            self.blockchain_connection_1, is_handshake_initiator, rlpx_cipher)
        self.block_blockchain_connection_protocol = EthNodeConnectionProtocol(
            self.blockchain_connection_1, is_handshake_initiator, rlpx_cipher)
        self.tx_blockchain_connection_protocol.publish_transaction = MagicMock(
        )
        self.block_blockchain_connection_protocol.publish_transaction = MagicMock(
        )

        self.tx_blockchain_connection_protocol_2 = EthNodeConnectionProtocol(
            self.blockchain_connection_2, is_handshake_initiator, rlpx_cipher)
        self.block_blockchain_connection_protocol_2 = EthNodeConnectionProtocol(
            self.blockchain_connection_2, is_handshake_initiator, rlpx_cipher)
        self.tx_blockchain_connection_protocol_2.publish_transaction = MagicMock(
        )
        self.block_blockchain_connection_protocol_2.publish_transaction = MagicMock(
        )

        self.relay_connection = AbstractRelayConnection(
            MockSocketConnection(1,
                                 node=self.node,
                                 ip_address=local_ip,
                                 port=12345), self.node)
        self.relay_connection.state = ConnectionState.INITIALIZED

        gateway_bdn_performance_stats_service.set_node(self.node)
        self.node.account_id = "12345"