def setUp(self):
        self.node = MockGatewayNode(gateway_helpers.get_gateway_opts(8000))
        self.node_conn = cast(
            AbstractGatewayBlockchainConnection,
            MockConnection(
                MockSocketConnection(1,
                                     self.node,
                                     ip_address=LOCALHOST,
                                     port=8002), self.node))
        self.node.blockchain_peers.add(
            BlockchainPeerInfo(self.node_conn.peer_ip,
                               self.node_conn.peer_port))

        self.node_conn_2 = cast(
            AbstractGatewayBlockchainConnection,
            MockConnection(
                MockSocketConnection(2,
                                     self.node,
                                     ip_address=LOCALHOST,
                                     port=8003), self.node))
        self.node.blockchain_peers.add(
            BlockchainPeerInfo(self.node_conn_2.peer_ip,
                               self.node_conn_2.peer_port))

        self.node_conn_3 = cast(
            AbstractGatewayBlockchainConnection,
            MockConnection(
                MockSocketConnection(3,
                                     self.node,
                                     ip_address=LOCALHOST,
                                     port=8004), self.node))
        self.node.blockchain_peers.add(
            BlockchainPeerInfo(self.node_conn_3.peer_ip,
                               self.node_conn_3.peer_port))
Example #2
0
    def test_delete_removes_multiple_types(self):
        class TestConnectionType(SerializableFlag):
            A = auto()
            B = auto()
            AB = A | B

        conn = MockConnection(
            MockSocketConnection(1,
                                 node=self.node1,
                                 ip_address=LOCALHOST,
                                 port=8000), self.node1)
        conn.CONNECTION_TYPE = TestConnectionType.AB
        self.conn_pool1.add(self.fileno1, LOCALHOST, 8000, conn)
        self.assertIn(
            conn,
            self.conn_pool1.get_by_connection_types([TestConnectionType.A]))
        self.assertIn(
            conn,
            self.conn_pool1.get_by_connection_types([TestConnectionType.B]))

        self.conn_pool1.delete(conn)
        self.assertNotIn(
            conn,
            self.conn_pool1.get_by_connection_types([TestConnectionType.A]))
        self.assertNotIn(
            conn,
            self.conn_pool1.get_by_connection_types([TestConnectionType.B]))
Example #3
0
 async def test_remove_blockchain_peer(self):
     conn = MockConnection(
         MockSocketConnection(9,
                              self.gateway_node,
                              ip_address="127.0.0.1",
                              port=30302), self.gateway_node)
     conn.mark_for_close = MagicMock()
     self.gateway_node.connection_pool.add(9, "127.0.0.1", 30302, conn)
     self.gateway_node.blockchain_peers.add(
         BlockchainPeerInfo(
             "127.0.0.1", 30302,
             "d76d7d11a822fab02836f8b0ea462205916253eb630935d15191fb6f9d218cd94a768fc5b3d5516b9ed5010a4765f95aea7124a39d0ab8aaf6fa3d57e21ef396"
         ))
     result = await self.request(
         BxJsonRpcRequest(
             "9", RpcRequestType.REMOVE_BLOCKCHAIN_PEER, {
                 "peer":
                 "enode://d76d7d11a822fab02836f8b0ea462205916253eb630935d15191fb6f9d218cd94a768fc5b3d5516b9ed5010a4765f95aea7124a39d0ab8aaf6fa3d57e21ef396@127.0.0.1:30302"
             }))
     self.assertNotIn(BlockchainPeerInfo("127.0.0.1", 30302),
                      self.gateway_node.blockchain_peers)
     conn.mark_for_close.assert_called_once_with(False)
     self.assertEqual("9", result.id)
     self.assertIsNone(result.error)
     self.assertEqual({"removed_peer": "127.0.0.1:30302"}, result.result)
    def setUp(self):
        self.conn_pool1 = ConnectionPool()

        self.fileno1 = 1
        self.ip1 = "123.123.123.123"
        self.port1 = 1000
        self.node1 = MockNode(
            helpers.get_common_opts(1001, external_ip="128.128.128.128"))
        self.node_id1 = str(uuid.uuid1())
        self.conn1 = MockConnection(
            MockSocketConnection(self.fileno1,
                                 ip_address=self.ip1,
                                 port=self.port1), self.node1)

        self.fileno2 = 5
        self.ip2 = "234.234.234.234"
        self.port2 = 2000
        self.node2 = MockNode(
            helpers.get_common_opts(1003, external_ip="321.321.321.321"))
        self.node_id2 = str(uuid.uuid1())
        self.conn2 = MockConnection(
            MockSocketConnection(self.fileno2,
                                 ip_address=self.ip2,
                                 port=self.port2), self.node2)

        self.fileno3 = 6
        self.ip3 = "234.234.234.234"
        self.port3 = 3000
        self.node3 = MockNode(
            helpers.get_common_opts(1003, external_ip="213.213.213.213."))
        self.node_id3 = str(uuid.uuid1())
        self.conn3 = MockConnection(
            MockSocketConnection(self.fileno3,
                                 ip_address=self.ip3,
                                 port=self.port3), self.node3)
Example #5
0
    def setUp(self):
        self.node = MockGatewayNode(
            gateway_helpers.get_gateway_opts(
                8000,
                max_block_interval_s=gateway_constants.MAX_INTERVAL_BETWEEN_BLOCKS_S,
                blockchain_message_ttl=TTL
            )
        )
        self.node_conn = MockConnection(
            MockSocketConnection(1, self.node, ip_address=LOCALHOST, port=8002), self.node
            )
        self.node.blockchain_peers.add(BlockchainPeerInfo(self.node_conn.peer_ip, self.node_conn.peer_port))
        self.block_queuing_service = TestPushBlockQueuingService(self.node, self.node_conn)
        self.node.block_queuing_service_manager.add_block_queuing_service(self.node_conn, self.block_queuing_service)
        self.node_conn.enqueue_msg = MagicMock()

        self.node_conn_2 = MockConnection(
            MockSocketConnection(2, self.node, ip_address=LOCALHOST, port=8003), self.node
            )
        self.node.blockchain_peers.add(BlockchainPeerInfo(self.node_conn_2.peer_ip, self.node_conn_2.peer_port))
        self.block_queuing_service_2 = TestPushBlockQueuingService(self.node, self.node_conn_2)
        self.node.block_queuing_service_manager.add_block_queuing_service(self.node_conn_2, self.block_queuing_service_2)
        self.node_conn_2.enqueue_msg = MagicMock()

        self.blockchain_connections = [self.node_conn, self.node_conn_2]
        self.block_queuing_services = [self.block_queuing_service, self.block_queuing_service_2]
Example #6
0
    def setUp(self):
        opts = gateway_helpers.get_gateway_opts(8000, include_default_btc_args=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism(opts.thread_pool_parallelism_degree)
        node_ssl_service = MockNodeSSLService(BtcGatewayNode.NODE_TYPE, MagicMock())
        self.gateway_node = BtcGatewayNode(opts, node_ssl_service)
        self.gateway_node.opts.has_fully_updated_tx_service = True
        self.sut = BtcRelayConnection(
            MockSocketConnection(1, node=self.gateway_node, ip_address=LOCALHOST, port=8001), self.gateway_node
            )
        self.gateway_node.message_converter = converter_factory.create_btc_message_converter(
            12345, self.gateway_node.opts
        )

        self.node_conn = MockConnection(
            MockSocketConnection(1, self.gateway_node, ip_address=LOCALHOST, port=8002), self.gateway_node
            )
        self.gateway_node.connection_pool.add(1, LOCALHOST, 8002, self.node_conn)
        gateway_helpers.add_blockchain_peer(self.gateway_node, self.node_conn)

        self.node_conn_2 = MockConnection(
            MockSocketConnection(1, self.gateway_node, ip_address=LOCALHOST, port=8003), self.gateway_node
            )
        self.gateway_node.connection_pool.add(1, LOCALHOST, 8003, self.node_conn_2)
        gateway_helpers.add_blockchain_peer(self.gateway_node, self.node_conn_2)

        self.blockchain_connections = [self.node_conn, self.node_conn_2]

        self.node_conn.enqueue_msg = MagicMock()
        self.node_conn_2.enqueue_msg = MagicMock()
        self.gateway_node.broadcast = MagicMock()
        self.sut.enqueue_msg = MagicMock()
        gateway_transaction_stats_service.set_node(self.gateway_node)

        self.gateway_node.has_active_blockchain_peer = MagicMock(return_value=True)
    def test_make_sync_request(self):
        other_connections = []
        for i in range(2, 5):
            fileno = i
            port = 8000 + i
            connection = MockConnection(fileno, (LOCALHOST, port), self.node)
            connection.state |= ConnectionState.ESTABLISHED
            connection.enqueue_msg = MagicMock()
            self.node.connection_pool.add(fileno, LOCALHOST, port, connection)
            other_connections.append(connection)

        preferred_connection = other_connections[0]
        self.node.peer_gateways.add(OutboundPeerModel(LOCALHOST, 8002))

        request = GetHeadersBtcMessage(self.MAGIC, self.VERSION, self.HASHES,
                                       self.HASH2)
        self.blockchain_sync_service.make_sync_request(request.command(),
                                                       request)

        request_message = BlockchainSyncRequestMessage(request.command(),
                                                       request.rawbytes())
        preferred_connection.enqueue_msg.assert_called_once_with(
            request_message, prepend=True)

        time.time = MagicMock(
            return_value=time.time() +
            gateway_constants.BLOCKCHAIN_SYNC_BROADCAST_DELAY_S)
        self.node.alarm_queue.fire_alarms()
        self.assertIn(request_message, self.node.broadcast_messages)
Example #8
0
    def _add_connection(
            self,
            fileno: int,
            port: int,
            network_num: int,
            connection_type=MockConnection.CONNECTION_TYPE) -> MockConnection:
        conn = MockConnection(MockSocketConnection(fileno), (LOCALHOST, port),
                              self.node)
        conn.network_num = network_num
        conn.state = ConnectionState.ESTABLISHED
        conn.CONNECTION_TYPE = connection_type

        self.connection_pool.add(fileno, LOCALHOST, port, conn)
        return conn
Example #9
0
    def test_delete_removes_multiple_types(self):
        class TestConnectionType(Flag):
            A = auto()
            B = auto()
            AB = A | B

        conn = MockConnection(MockSocketConnection(), (LOCALHOST, 8000), self.node1)
        conn.CONNECTION_TYPE = TestConnectionType.AB
        self.conn_pool1.add(self.fileno1, LOCALHOST, 8000, conn)
        self.assertIn(conn, self.conn_pool1.get_by_connection_type(TestConnectionType.A))
        self.assertIn(conn, self.conn_pool1.get_by_connection_type(TestConnectionType.B))

        self.conn_pool1.delete(conn)
        self.assertNotIn(conn, self.conn_pool1.get_by_connection_type(TestConnectionType.A))
        self.assertNotIn(conn, self.conn_pool1.get_by_connection_type(TestConnectionType.B))
Example #10
0
    def test_propagate_block_to_network_unencrypted_block(self):
        self.node.opts.encrypt_blocks = False

        block_message = helpers.generate_bytearray(50)
        block_info = BlockInfo(
            Sha256Hash(helpers.generate_bytearray(crypto.SHA256_HASH_LEN)), [],
            datetime.datetime.utcnow(), datetime.datetime.utcnow(), 0, 1,
            helpers.generate_bytearray(crypto.SHA256_HASH_LEN),
            helpers.generate_bytearray(crypto.SHA256_HASH_LEN), 0, 0, 0, [])

        connection = MockConnection(
            MockSocketConnection(1, self.node, ip_address=LOCALHOST,
                                 port=9000), self.node)
        self.neutrality_service.propagate_block_to_network(
            block_message, connection, block_info)

        self.assertEqual(1, len(self.node.broadcast_messages))
        broadcast_message, connection_types = self.node.broadcast_messages[0]
        # self.assertTrue(any(ConnectionType.RELAY_BLOCK & connection_type for connection_type in connection_types))
        self.assertTrue(
            all(ConnectionType.RELAY_BLOCK in connection_type
                for connection_type in connection_types))
        self.assertEqual(block_info.block_hash, broadcast_message.block_hash())

        self.assertNotIn(block_info.block_hash,
                         self.node.in_progress_blocks._cache)
        self.assertNotIn(broadcast_message.block_hash(),
                         self.neutrality_service._receipt_tracker)
Example #11
0
    def test_add_special_objects(self):
        node1 = MockNode(get_common_opts(1001, external_ip="128.128.128.128"))
        conn1 = MockConnection(
            MockSocketConnection(1,
                                 node1,
                                 ip_address="123.123.123.123",
                                 port=1000), node1)
        conn1.inputbuf.add_bytes(bytearray(b"0000" * 10))
        conn1.outputbuf.prepend_msgbytes(bytearray(b"1111" * 100))
        total_special_size, ids = memory_utils.get_special_size(conn1)

        self.assertTrue(ids)
        self.assertTrue(id(conn1.inputbuf.input_list) in ids)
        self.assertTrue(id(conn1.outputbuf.output_msgs) in ids)

        expected_special_size = memory_utils.get_special_size(
            conn1.inputbuf.input_list).size
        expected_special_size += memory_utils.get_special_size(
            conn1.outputbuf.output_msgs).size
        self.assertEqual(total_special_size, expected_special_size)

        s, s_id = memory_utils.get_special_size(conn1.outputbuf.output_msgs)
        self.assertNotEqual(s, 0)
        s, ids = memory_utils.get_special_size(conn1.outputbuf.output_msgs,
                                               ids)
        self.assertEqual(s, 0)
    def setUp(self):
        opts = gateway_helpers.get_gateway_opts(8000,
                                                include_default_ont_args=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism(
                opts.thread_pool_parallelism_degree)
        node_ssl_service = MockNodeSSLService(OntGatewayNode.NODE_TYPE,
                                              MagicMock())
        self.gateway_node = OntGatewayNode(opts, node_ssl_service)
        self.gateway_node.opts.has_fully_updated_tx_service = True
        self.gateway_node.opts.is_consensus = False
        self.sut = OntRelayConnection(
            MockSocketConnection(node=self.gateway_node,
                                 ip_address=LOCALHOST,
                                 port=8001), self.gateway_node)
        self.gateway_node.node_conn = MockConnection(
            MockSocketConnection(1,
                                 self.gateway_node,
                                 ip_address=LOCALHOST,
                                 port=8002), self.gateway_node)
        self.gateway_node_sut = OntNodeConnectionProtocol(
            self.gateway_node.node_conn)
        self.gateway_node.message_converter = converter_factory.create_ont_message_converter(
            12345, self.gateway_node.opts)
        self.gateway_node.node_conn.state = ConnectionState.ESTABLISHED

        self.gateway_node.send_msg_to_node = MagicMock()
        self.sut.enqueue_msg = MagicMock()
        gateway_transaction_stats_service.set_node(self.gateway_node)
Example #13
0
    def test_remove_blockchain_connection_and_add_new(self):
        self._add_connections()

        new_desc6 = "1.1.1.1 80"
        new_ip6 = new_desc6.split()[0]
        new_port6 = new_desc6.split()[1]
        new_fileno6 = "10"
        new_node6 = MockNode(helpers.get_common_opts(1004, external_ip="214.215.216.217"))
        new_conn6 = MockConnection(
            MockSocketConnection(new_fileno6, new_node6, ip_address=new_ip6, port=int(new_port6)), new_node6
        )
        self.conn_pool.delete(self.conn3)
        self.blockchain_peers.add(BlockchainPeerInfo(new_ip6, int(new_port6)))
        self.conn_pool.add(int(new_fileno6), new_ip6, int(new_port6), new_conn6)

        update(self.conn_pool, True, self.source_version, self.ip_address, self.continent, self.country, False,
               self.blockchain_peers, self.account_id, self.quota_level)
        summary_loaded, _, _, network_loaded = self._load_status_file()
        self.assertEqual(summary_loaded,
                         network_loaded.get_summary(self.ip_address, self.continent, self.country,
                                                    False, self.account_id, self.quota_level))
        self.assertEqual(summary_loaded.gateway_status, GatewayStatus.WITH_ERRORS)
        self.assertEqual(summary_loaded.account_info, summary.gateway_status_get_account_info(None))
        self.assertEqual(summary_loaded.quota_level, summary.gateway_status_get_quota_level(self.quota_level))
        self.assertEqual(
            summary_loaded.blockchain_node_connection_states,
            {
                f"{new_ip6} {new_port6}": ConnectionState.ESTABLISHED,
                f"{self.ip3} {self.port3}": ConnectionState.DISCONNECTED,
                f"{self.ip5} {self.port5}": ConnectionState.ESTABLISHED,
            }
        )
 def setUp(self):
     self.node = MockGatewayNode(gateway_helpers.get_gateway_opts(8000))
     self.node.connection_pool = ConnectionPool()
     self.blockchain_sync_service = BlockchainSyncService(
         self.node, {BtcMessageType.GET_HEADERS: BtcMessageType.HEADERS})
     self.blockchain_connection = MockConnection(1, (LOCALHOST, 8001),
                                                 self.node)
     self.blockchain_connection.state |= ConnectionState.ESTABLISHED
     self.node.node_conn = self.blockchain_connection
     self.alarms = []
Example #15
0
    def setUp(self):
        self.node = MockGatewayNode(
            gateway_helpers.get_gateway_opts(
                8000, max_block_interval_s=BLOCK_INTERVAL))
        self.node.block_parts_storage = ExpiringDict(
            self.node.alarm_queue,
            gateway_constants.MAX_BLOCK_CACHE_TIME_S,
            "eth_block_queue_parts",
        )
        self.node.alarm_queue = AlarmQueue()
        self.node.set_known_total_difficulty = MagicMock()
        self.block_processing_service = EthBlockProcessingService(self.node)
        self.node.block_processing_service = self.block_processing_service

        self.node_connection = MockConnection(
            MockSocketConnection(1, self.node, ip_address=LOCALHOST,
                                 port=8002), self.node)
        self.node_connection.is_active = MagicMock(return_value=True)
        self.block_queuing_service = EthBlockQueuingService(
            self.node, self.node_connection)
        self.node.block_queuing_service_manager.add_block_queuing_service(
            self.node_connection, self.block_queuing_service)
        self.node_connection.enqueue_msg = MagicMock()

        self.node_connection_2 = MockConnection(
            MockSocketConnection(1, self.node, ip_address=LOCALHOST,
                                 port=8003), self.node)
        self.node_connection_2.is_active = MagicMock(return_value=True)
        self.block_queuing_service_2 = EthBlockQueuingService(
            self.node, self.node_connection_2)
        self.node.block_queuing_service_manager.add_block_queuing_service(
            self.node_connection_2, self.block_queuing_service_2)
        self.node_connection_2.enqueue_msg = MagicMock()

        self.blockchain_connections = [
            self.node_connection, self.node_connection_2
        ]
        self.block_queuing_services = [
            self.block_queuing_service, self.block_queuing_service_2
        ]

        time.time = MagicMock(return_value=time.time())
    def test_remote_sync_request(self):
        self.node.send_msg_to_node = MagicMock()
        remote_caller_connection1 = MockConnection(1, (LOCALHOST, 8001),
                                                   self.node)
        remote_caller_connection1.enqueue_msg = MagicMock()
        remote_caller_connection2 = MockConnection(2, (LOCALHOST, 8002),
                                                   self.node)
        remote_caller_connection2.enqueue_msg = MagicMock()

        remote_blockchain_message = GetHeadersBtcMessage(
            self.MAGIC, self.VERSION, self.HASHES, self.HASH2)
        self.blockchain_sync_service.process_remote_sync_request(
            remote_blockchain_message.command(), remote_blockchain_message,
            remote_caller_connection1)
        self.blockchain_sync_service.process_remote_sync_request(
            remote_blockchain_message.command(), remote_blockchain_message,
            remote_caller_connection2)
        self.node.send_msg_to_node.assert_called_once_with(
            remote_blockchain_message)

        local_blockchain_message = HeadersBtcMessage(self.MAGIC, headers=[])
        self.blockchain_sync_service.process_local_sync_response(
            local_blockchain_message.command(), local_blockchain_message)

        local_response = BlockchainSyncResponseMessage(
            local_blockchain_message.command(),
            local_blockchain_message.rawbytes())

        remote_caller_connection1.enqueue_msg.assert_called_once_with(
            local_response)
    def setUp(self):
        self.node = MockGatewayNode(gateway_helpers.get_gateway_opts(8000))
        self.sut = BlockProcessingService(self.node)

        self.node.block_processing_service = self.sut
        self.node.neutrality_service = MagicMock(spec=NeutralityService)
        self.node.block_recovery_service = MagicMock(spec=BlockRecoveryService)
        self.node.block_queuing_service = MagicMock(
            spec=PushBlockQueuingService)

        self.dummy_connection = MockConnection(
            MockSocketConnection(0, self.node, ip_address=LOCALHOST,
                                 port=9000), self.node)
Example #18
0
    def _add_connection(self, fileno: int, port: int, network_num: int,
                        connection_type=MockConnection.CONNECTION_TYPE) -> MockConnection:
        conn = MockConnection(MockSocketConnection(fileno, self.node, ip_address=LOCALHOST, port=port), self.node)
        conn.network_num = network_num
        conn.on_connection_established()
        conn.CONNECTION_TYPE = connection_type

        self.connection_pool.add(fileno, LOCALHOST, port, conn)
        return conn
Example #19
0
    def broadcast(self,
                  msg,
                  broadcasting_conn=None,
                  prepend_to_queue=False,
                  connection_types=None):
        if connection_types is None:
            connection_types = [ConnectionType.RELAY_ALL]

        if len(connection_types) == 1 and connection_types[
                0] == ConnectionType.BLOCKCHAIN_NODE:
            self.broadcast_to_nodes_messages.append(msg)
        else:
            self.broadcast_messages.append((msg, connection_types))
        return [
            MockConnection(
                MockSocketConnection(1,
                                     ip_address="123.123.123.123",
                                     port=1000), self)
        ]
Example #20
0
    def test_propagate_block_to_network_encrypted_block(self):
        self.node.opts.encrypt_blocks = True

        block_message = helpers.generate_bytearray(50)
        connection = MockConnection(
            MockSocketConnection(1, self.node, ip_address=LOCALHOST,
                                 port=9000), self.node)
        self.neutrality_service.propagate_block_to_network(
            block_message, connection)

        self.assertEqual(1, len(self.node.broadcast_messages))
        broadcast_message, connection_types = self.node.broadcast_messages[0]
        self.assertTrue(ConnectionType.RELAY_BLOCK in connection_types[0])

        raw_block_hash = bytes(broadcast_message.block_hash().binary)
        cache_item = self.node.in_progress_blocks._cache.get(raw_block_hash)
        self.assertEqual(
            cache_item.payload,
            crypto.symmetric_decrypt(cache_item.key,
                                     broadcast_message.blob().tobytes()))
        self.assertIn(broadcast_message.block_hash(),
                      self.neutrality_service._receipt_tracker)
Example #21
0
    def setUp(self) -> None:

        crypto_utils.recover_public_key = MagicMock(return_value=bytes(32))

        pub_key = "a04f30a45aae413d0ca0f219b4dcb7049857bc3f91a6351288cce603a2c9646294a02b987bf6586b370b2c22d74662355677007a14238bb037aedf41c2d08866"
        opts = gateway_helpers.get_gateway_opts(8000,
                                                include_default_eth_args=True,
                                                pub_key=pub_key)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        node_ssl_service = MockNodeSSLService(EthGatewayNode.NODE_TYPE,
                                              MagicMock())
        self.node = EthGatewayNode(opts, node_ssl_service)
        self.node_conn = MockConnection(
            MockSocketConnection(1, self.node, ip_address=LOCALHOST,
                                 port=8002), self.node)
        self.node.connection_pool.add(1, LOCALHOST, 8002, self.node_conn)
        gateway_helpers.add_blockchain_peer(self.node, self.node_conn)
        self.block_queuing_service = self.node.block_queuing_service_manager.get_designated_block_queuing_service(
        )

        self.sut = EthNewBlockFeed(self.node)
    def test_get_by_connection_types_performance(self):
        log_config.set_level([
            "bxcommon.connections.abstract_node",
            "bxcommon.services.transaction_service"
        ], LogLevel.INFO)
        conn_pool = ConnectionPool()
        self.conn1.CONNECTION_TYPE = ConnectionType.EXTERNAL_GATEWAY
        self.conn2.CONNECTION_TYPE = ConnectionType.RELAY_BLOCK
        self.conn3.CONNECTION_TYPE = ConnectionType.RELAY_ALL
        number_of_iteration = 100
        for i in range(40):
            ip = f"{i}.{i}.{i}.{i}"
            node = MockNode(helpers.get_common_opts(i, external_ip=ip))
            conn = MockConnection(
                MockSocketConnection(i, ip_address=ip, port=i), node)
            if i % 7 == 0:
                conn.CONNECTION_TYPE = ConnectionType.RELAY_BLOCK
            elif i % 5 == 0:
                conn.CONNECTION_TYPE = ConnectionType.RELAY_TRANSACTION
            elif i % 3 == 0:
                conn.CONNECTION_TYPE = ConnectionType.INTERNAL_GATEWAY
            else:
                conn.CONNECTION_TYPE = ConnectionType.EXTERNAL_GATEWAY
            conn_pool.add(i, ip, i, conn)

        timeit_get_by_connections_types_one_type = timeit.timeit(
            lambda: conn_pool.get_by_connection_types([ConnectionType.GATEWAY]
                                                      ),
            number=number_of_iteration)
        timeit_get_by_connections_types_two_types = timeit.timeit(
            lambda: conn_pool.get_by_connection_types(
                [ConnectionType.GATEWAY, ConnectionType.RELAY_TRANSACTION]),
            number=number_of_iteration)
        print(
            f"\ntimeit_get_by_connections_types_one_type # 2:  {timeit_get_by_connections_types_one_type * 1000 / number_of_iteration:.4f}ms, "
            f"#connections: {len(list(conn_pool.get_by_connection_types([ConnectionType.GATEWAY])))}"
            f"\ntimeit_get_by_connections_types_two_types # 2: {timeit_get_by_connections_types_two_types * 1000 / number_of_iteration:.4f}ms, "
            f"#connections: {len(list(conn_pool.get_by_connection_types([ConnectionType.GATEWAY, ConnectionType.RELAY_TRANSACTION])))}"
        )

        print("*****")
        for c in conn_pool.get_by_connection_types(
            [ConnectionType.GATEWAY, ConnectionType.RELAY_TRANSACTION]):
            print(f"connection: {c}, connection type: {c.CONNECTION_TYPE}")
Example #23
0
    def _test_mark_blocks_and_request_cleanup(self):
        node_conn = MockConnection(
            MockSocketConnection(0, self.node, ip_address=LOCALHOST,
                                 port=9000), self.node)
        self.node.get_any_active_blockchain_connection = MagicMock(
            return_value=node_conn)

        marked_block = BtcObjectHash(
            binary=helpers.generate_bytearray(btc_constants.BTC_SHA_HASH_LEN))
        prev_block = BtcObjectHash(
            binary=helpers.generate_bytearray(btc_constants.BTC_SHA_HASH_LEN))
        tracked_blocks = []
        self.cleanup_service.on_new_block_received(marked_block, prev_block)
        self.transaction_service.track_seen_short_ids(marked_block, [])
        for _ in range(self.block_confirmations_count - 1):
            tracked_block = BtcObjectHash(binary=helpers.generate_bytearray(
                btc_constants.BTC_SHA_HASH_LEN))
            self.transaction_service.track_seen_short_ids(tracked_block, [])
            tracked_blocks.append(tracked_block)
        unmarked_block = BtcObjectHash(
            binary=helpers.generate_bytearray(btc_constants.BTC_SHA_HASH_LEN))
        self.assertIsNone(self.cleanup_service.last_confirmed_block)
        self.cleanup_service.mark_blocks_and_request_cleanup(
            [marked_block, *tracked_blocks])
        self.assertEqual(marked_block,
                         self.cleanup_service.last_confirmed_block)
        self.assertTrue(
            self.cleanup_service.is_marked_for_cleanup(marked_block))
        self.assertFalse(
            self.cleanup_service.is_marked_for_cleanup(unmarked_block))
        self.assertEqual(marked_block,
                         self.cleanup_service.last_confirmed_block)
        msg = node_conn.enqueued_messages[0]
        self.assertEqual(1, msg.count())
        self.assertEqual((InventoryType.MSG_BLOCK, marked_block),
                         next(iter(msg)))
Example #24
0
    def setUp(self):
        self.conn_pool = ConnectionPool()
        self.source_version = "v1.0.0"
        self.ip_address = "0.0.0.0"
        self.continent = "NA"
        self.country = "United States"
        self.account_id = None

        self.fileno1 = 1
        self.ip1 = "123.123.123.123"
        self.port1 = 1000
        self.node1 = MockNode(
            helpers.get_common_opts(1001, external_ip="128.128.128.128"))
        self.node_id1 = str(uuid.uuid1())
        self.conn1 = MockConnection(
            MockSocketConnection(self.fileno1,
                                 self.node1,
                                 ip_address=self.ip1,
                                 port=self.port1), self.node1)
        self.conn1.CONNECTION_TYPE = ConnectionType.RELAY_BLOCK

        self.fileno2 = 5
        self.ip2 = "234.234.234.234"
        self.port2 = 2000
        self.node2 = MockNode(
            helpers.get_common_opts(1003, external_ip="321.321.321.321"))
        self.node_id2 = str(uuid.uuid1())
        self.conn2 = MockConnection(
            MockSocketConnection(self.fileno2,
                                 self.node2,
                                 ip_address=self.ip2,
                                 port=self.port2), self.node2)
        self.conn2.CONNECTION_TYPE = ConnectionType.RELAY_TRANSACTION

        self.fileno3 = 6
        self.ip3 = "234.234.234.234"
        self.port3 = 3000
        self.node3 = MockNode(
            helpers.get_common_opts(1003, external_ip="213.213.213.213"))
        self.node_id3 = str(uuid.uuid1())
        self.conn3 = MockConnection(
            MockSocketConnection(self.fileno3,
                                 self.node3,
                                 ip_address=self.ip3,
                                 port=self.port3), self.node3)
        self.conn3.CONNECTION_TYPE = ConnectionType.BLOCKCHAIN_NODE

        self.fileno4 = 8
        self.ip4 = "111.222.111.222"
        self.port4 = 3000
        self.node4 = MockNode(
            helpers.get_common_opts(1003, external_ip="101.101.101.101"))
        self.node_id4 = str(uuid.uuid1())
        self.conn4 = MockConnection(
            MockSocketConnection(self.fileno4,
                                 self.node4,
                                 ip_address=self.ip4,
                                 port=self.port4), self.node4)
        self.conn4.CONNECTION_TYPE = ConnectionType.REMOTE_BLOCKCHAIN_NODE
        self.quota_level = 0
        initialize(False, self.source_version, self.ip_address, self.continent,
                   self.country, False, self.account_id, self.quota_level)

        path = config.get_data_file(STATUS_FILE_NAME)
        self.addCleanup(os.remove, path)
Example #25
0
    def broadcast(self, msg, broadcasting_conn=None, prepend_to_queue=False, connection_types=None):
        if connection_types is None:
            connection_types = [ConnectionType.RELAY_ALL]

        self.broadcast_messages.append((msg, connection_types))
        return [MockConnection(MockSocketConnection(1, ip_address="123.123.123.123", port=1000), self)]
 def build_connection(self, socket_connection):
     return MockConnection(socket_connection, self)
 def setUp(self) -> None:
     self.node = MockNode(
         helpers.get_common_opts(1001, external_ip="128.128.128.128"))
     self.tracker = MessageTracker(
         MockConnection(MockSocketConnection(), self.node))
     self.output_buffer = OutputBuffer(enable_buffering=True)
Example #28
0
 def build_connection(self, socket_connection, ip, port, from_me=False):
     return MockConnection(socket_connection, (ip, port), self, from_me)