Ejemplo n.º 1
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)
Ejemplo n.º 2
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]
Ejemplo n.º 3
0
    def test_blockchain_peers_eth_from_file_and_args(self):
        blockchain_ip_1 = "1.2.3.4"
        blockchain_port_1 = 20202
        public_key_1 = "00029fb539bbbebc7bcc986bca2b1d3e262a1133901c3cc699f8dd9cba91df51ede5fed9c2c25b74425d64344a9a9d393904c6f0f8bd95cc0c5e2699b6a19ea1"
        blockchain_peer_1 = BlockchainPeerInfo(blockchain_ip_1, blockchain_port_1, public_key_1)
        blockchain_ip_2 = "5.6.7.8"
        blockchain_port_2 = 30303
        public_key_2 = "873235d21c380e2b7239cfdcb3a02bd1b92232aed81d36cec394749bd578bd47ee73a9c9270a2f196f4b7734b0edeeedc8788abea4abeffd1ae69f423e9f70ea"
        blockchain_peer_2 = BlockchainPeerInfo(blockchain_ip_2, blockchain_port_2, public_key_2)
        blockchain_ip_3 = "172.17.0.1"
        blockchain_port_3 = 30303
        public_key_3 = "X" * 64 * 2
        blockchain_peer_3 = BlockchainPeerInfo(blockchain_ip_3, blockchain_port_3, public_key_3)
        root_dir = os.path.dirname(os.path.abspath(__file__))
        file_path = os.path.join(root_dir, "samples/blockchain_peers_eth_sample_file.txt")

        argv = ["--source-version", "1.2.3",
                "--external-port", "7000",
                "--blockchain-protocol", "ethereum",
                "--blockchain-peers-file", file_path,
                "--blockchain-peers", f"enode://{public_key_3}@{blockchain_ip_3}:{blockchain_port_3}"]
        args = main.get_opts(argv)
        self.assertEqual(3, len(args.blockchain_peers))
        self.assertTrue(blockchain_peer_1 in args.blockchain_peers)
        self.assertTrue(blockchain_peer_2 in args.blockchain_peers)
        self.assertTrue(blockchain_peer_3 in args.blockchain_peers)
    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))
Ejemplo n.º 5
0
    def test_blockchain_peers_btc(self):
        blockchain_ip_1 = "172.17.0.1"
        blockchain_port_1 = 8333
        blockchain_peer_1 = BlockchainPeerInfo(blockchain_ip_1, blockchain_port_1)
        blockchain_ip_2 = "172.17.0.2"
        blockchain_port_2 = 8555
        blockchain_peer_2 = BlockchainPeerInfo(blockchain_ip_2, blockchain_port_2)

        argv = ["--source-version", "1.2.3",
                "--external-port", "7000",
                "--blockchain-protocol", "bitcoin",
                "--blockchain-peers", f"{blockchain_ip_1}:{blockchain_port_1}, {blockchain_ip_2}:{blockchain_port_2}"]
        args = main.get_opts(argv)
        self.assertEqual(2, len(args.blockchain_peers))
        self.assertTrue(blockchain_peer_1 in args.blockchain_peers)
        self.assertTrue(blockchain_peer_2 in args.blockchain_peers)
Ejemplo n.º 6
0
 def mock_add_blockchain_peer(
         self, connection: AbstractGatewayBlockchainConnection) -> None:
     self.blockchain_peers.add(
         BlockchainPeerInfo(connection.peer_ip, connection.peer_port))
     block_queuing_service = self.build_block_queuing_service(connection)
     self.block_queuing_service_manager.add_block_queuing_service(
         connection, block_queuing_service)
Ejemplo n.º 7
0
    def _should_log_closed_connection(self,
                                      connection: AbstractConnection) -> bool:
        if isinstance(connection, EthNodeConnection):
            connection_status = connection.connection_protocol.connection_status

            if (self._should_decrease_version_number(connection.state,
                                                     connection_status)
                    and connection.CONNECTION_TYPE
                    == ConnectionType.BLOCKCHAIN_NODE):
                for peer in self.blockchain_peers:
                    if (peer == BlockchainPeerInfo(connection.peer_ip,
                                                   connection.peer_port)
                            and not peer.connection_established):
                        peer.blockchain_protocol_version = self._get_new_protocol_version(
                            connection, peer.blockchain_protocol_version)
                        return False

        elif isinstance(connection, EthRemoteConnection):
            connection_status = connection.connection_protocol.connection_status

            if (self._should_decrease_version_number(connection.state,
                                                     connection_status)
                    and connection.CONNECTION_TYPE
                    == ConnectionType.REMOTE_BLOCKCHAIN_NODE
                    and not self.remote_blockchain_connection_established):
                self.remote_blockchain_protocol_version = \
                    self._get_new_protocol_version(
                        connection, self.remote_blockchain_protocol_version
                    )
                return False

        return True
Ejemplo n.º 8
0
 def _add_connections(self):
     self.conn_pool.add(self.fileno1, self.ip1, self.port1, self.conn1)
     self.conn_pool.add(self.fileno2, self.ip2, self.port2, self.conn2)
     self.conn_pool.add(self.fileno3, self.ip3, self.port3, self.conn3)
     self.conn_pool.add(self.fileno4, self.ip4, self.port4, self.conn4)
     self.conn_pool.add(self.fileno5, self.ip5, self.port5, self.conn5)
     self.blockchain_peers.add(BlockchainPeerInfo(self.ip5, self.port5))
Ejemplo n.º 9
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,
            }
        )
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
def add_blockchain_peer(node: AbstractGatewayNode,
                        connection: AbstractConnection):
    node_conn = cast(AbstractGatewayBlockchainConnection, connection)
    node.blockchain_peers.add(
        BlockchainPeerInfo(node_conn.peer_ip, node_conn.peer_port))
    block_queuing_service = node.build_block_queuing_service(node_conn)
    node.block_queuing_service_manager.add_block_queuing_service(
        node_conn, block_queuing_service)
Ejemplo n.º 12
0
    def test_blockchain_peers_btc_from_file_only(self):
        blockchain_ip_1 = "1.2.3.4"
        blockchain_port_1 = 8200
        blockchain_peer_1 = BlockchainPeerInfo(blockchain_ip_1, blockchain_port_1)
        blockchain_ip_2 = "5.6.7.8"
        blockchain_port_2 = 8300
        blockchain_peer_2 = BlockchainPeerInfo(blockchain_ip_2, blockchain_port_2)
        root_dir = os.path.dirname(os.path.abspath(__file__))
        file_path = os.path.join(root_dir, "samples/blockchain_peers_btc_sample_file.txt")

        argv = ["--source-version", "1.2.3",
                "--external-port", "7000",
                "--blockchain-protocol", "bitcoin",
                "--blockchain-peers-file", file_path]
        args = main.get_opts(argv)
        self.assertEqual(2, len(args.blockchain_peers))
        self.assertTrue(blockchain_peer_1 in args.blockchain_peers)
        self.assertTrue(blockchain_peer_2 in args.blockchain_peers)
Ejemplo n.º 13
0
 async def test_remove_blockchain_peer_not_in_pool(self):
     self.gateway_node.alarm_queue.register_alarm = MagicMock()
     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)
     self.gateway_node.alarm_queue.register_alarm.assert_called()
     self.assertEqual("9", result.id)
     self.assertIsNone(result.error)
     self.assertEqual({"removed_peer": "127.0.0.1:30302"}, result.result)
Ejemplo n.º 14
0
    def test_blockchain_peers_eth_from_args(self):
        blockchain_ip_1 = "172.17.0.1"
        blockchain_port_1 = 30303
        public_key_1 = "X" * 64 * 2
        blockchain_peer_1 = BlockchainPeerInfo(blockchain_ip_1, blockchain_port_1, public_key_1)
        blockchain_ip_2 = "172.17.0.2"
        blockchain_port_2 = 30304
        public_key_2 = "Y" * 64 * 2
        blockchain_peer_2 = BlockchainPeerInfo(blockchain_ip_2, blockchain_port_2, public_key_2)

        argv = ["--source-version", "1.2.3",
                "--external-port", "7000",
                "--blockchain-protocol", "ethereum",
                "--blockchain-peers", f"enode://{public_key_1}@{blockchain_ip_1}:{blockchain_port_1}, "
                                      f"enode://{public_key_2}@{blockchain_ip_2}:{blockchain_port_2}"]
        args = main.get_opts(argv)
        self.assertEqual(2, len(args.blockchain_peers))
        self.assertTrue(blockchain_peer_1 in args.blockchain_peers)
        self.assertTrue(blockchain_peer_2 in args.blockchain_peers)
Ejemplo n.º 15
0
 async def test_add_blockchain_peer(self):
     self.gateway_node.enqueue_connection = MagicMock()
     result = await self.request(
         BxJsonRpcRequest(
             "8", RpcRequestType.ADD_BLOCKCHAIN_PEER, {
                 "peer":
                 "enode://d76d7d11a822fab02836f8b0ea462205916253eb630935d15191fb6f9d218cd94a768fc5b3d5516b9ed5010a4765f95aea7124a39d0ab8aaf6fa3d57e21ef396@127.0.0.1:30302"
             }))
     self.gateway_node.enqueue_connection.assert_called_once_with(
         "127.0.0.1", 30302, ConnectionType.BLOCKCHAIN_NODE)
     self.assertIn(BlockchainPeerInfo("127.0.0.1", 30302),
                   self.gateway_node.blockchain_peers)
     self.assertEqual("8", result.id)
     self.assertIsNone(result.error)
     self.assertEqual({"new_peer": "127.0.0.1:30302"}, result.result)
Ejemplo n.º 16
0
 def parse_enode(self, enode: str) -> BlockchainPeerInfo:
     # Make sure enode is at least as long as the public key
     if not argument_parsers.enode_is_valid_length(enode):
         raise RpcInvalidParams(
             self.request_id,
             f"Invalid enode: {enode}, with length: {len(enode)}. "
             f"Expected format: enode://<eth node public key>@<eth node ip>:<port>"
         )
     try:
         pub_key, ip, port = argument_parsers.get_enode_parts(enode)
         if not port.isnumeric():
             raise RpcInvalidParams(self.request_id,
                                    f"Invalid port: {port}")
     except ValueError:
         raise RpcInvalidParams(
             self.request_id, f"Invalid enode: {enode}. "
             f"Expected format: enode://<eth node public key>@<eth node ip>:<port>"
         )
     else:
         return BlockchainPeerInfo(ip, int(port), pub_key)
Ejemplo n.º 17
0
def parse_enode(enode: str) -> BlockchainPeerInfo:
    if not enode_is_valid_length(enode):
        logger.fatal(log_messages.ETH_PARSER_INVALID_ENODE_LENGTH,
                     enode,
                     len(enode),
                     exc_info=False)
        sys.exit(1)
    try:
        pub_key, ip, port = get_enode_parts(enode)
        if not port.isnumeric():
            logger.fatal(log_messages.PARSER_INVALID_PORT,
                         port,
                         exc_info=False)
            sys.exit(1)
    except ValueError:
        logger.fatal(log_messages.ETH_PARSER_INVALID_ENODE,
                     enode,
                     exc_info=False)
        sys.exit(1)
    else:
        return BlockchainPeerInfo(ip, int(port), pub_key)
    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)
Ejemplo n.º 19
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.blockchain_peers: Set[BlockchainPeerInfo] = set()

        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.blockchain_peers.add(BlockchainPeerInfo(self.ip3, self.port3))
        self.blockchain_conn3 = BlockchainConnection(self.ip3, str(self.port3), str(self.fileno3), None)

        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.fileno5 = 9
        self.ip5 = "123.456.456.456"
        self.port5 = 3000
        self.node5 = MockNode(helpers.get_common_opts(1003, external_ip="456.456.456.456"))
        self.node_id5 = str(uuid.uuid1())
        self.conn5 = MockConnection(
            MockSocketConnection(self.fileno5, self.node5, ip_address=self.ip5, port=self.port5), self.node5
        )
        self.conn5.CONNECTION_TYPE = ConnectionType.BLOCKCHAIN_NODE
        self.blockchain_conn5 = BlockchainConnection(self.ip5, str(self.port5), str(self.fileno5), None)

        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)
Ejemplo n.º 20
0
def parse_ip_port(ip_port_string: str) -> BlockchainPeerInfo:
    ip, port = get_ip_port_string_parts(ip_port_string)
    if not port.isnumeric():
        logger.fatal(log_messages.PARSER_INVALID_PORT, port, exc_info=False)
        sys.exit(1)
    return BlockchainPeerInfo(ip, int(port))
Ejemplo n.º 21
0
 def parse_ip_port(self, ip_port_string: str) -> BlockchainPeerInfo:
     ip, port = argument_parsers.get_ip_port_string_parts(ip_port_string)
     if not port.isnumeric():
         raise RpcInvalidParams(self.request_id, f"Invalid port: {port}")
     return BlockchainPeerInfo(ip, int(port))