Esempio n. 1
0
 def __init__(self,
              opts: Namespace,
              node_ssl_service: Optional[NodeSSLService] = None):
     if node_ssl_service is None:
         node_ssl_service = MockNodeSSLService(self.NODE_TYPE, MagicMock())
     helpers.set_extensions_parallelism()
     super().__init__(opts, node_ssl_service)
    def setUp(self) -> None:
        if constants.USE_EXTENSION_MODULES:
            helpers.set_extensions_parallelism()

        self.gateway_info_1 = full_gateway_node.of_type(
            self.gateway_class(), self.gateway_opts_1())
        self.gateway_info_2 = full_gateway_node.of_type(
            self.gateway_class(), self.gateway_opts_2())

        (
            self.gateway_1,
            self.blockchain_fileno_1,
            self.relay_fileno_1,
            self.gateway_fileno_1,
            _,
            _,
            _,
            self.blockchain_connection_1,
            self.gateway_connection_1,
            self.relay_connection_1,
        ) = self.gateway_info_1

        (
            self.gateway_2,
            self.blockchain_fileno_2,
            self.relay_fileno_2,
            self.gateway_fileno_2,
            _,
            _,
            _,
            self.blockchain_connection_2,
            self.gateway_connection_2,
            self.relay_connection_2,
        ) = self.gateway_info_2
Esempio n. 3
0
    def setUp(self):
        opts = gateway_helpers.get_gateway_opts(8000,
                                                include_default_ont_args=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        self.node = MockOntGatewayNode(opts)
        self.node.neutrality_service = MagicMock()

        self.connection = OntNodeConnection(
            MockSocketConnection(1,
                                 node=self.node,
                                 ip_address=LOCALHOST,
                                 port=123), self.node)
        gateway_helpers.add_blockchain_peer(self.node, self.connection)

        self.tx_hash = OntObjectHash(buf=helpers.generate_bytearray(32),
                                     length=ONT_HASH_LEN)
        self.block_hash = OntObjectHash(buf=helpers.generate_bytearray(32),
                                        length=ONT_HASH_LEN)

        self.sut = OntNodeConnectionProtocol(self.connection)

        while self.connection.outputbuf.length > 0:
            initial_bytes = self.connection.get_bytes_to_send()
            self.connection.advance_sent_bytes(len(initial_bytes))
Esempio n. 4
0
    def test_split_relay_connection(self):
        relay_connections = [
            OutboundPeerModel(LOCALHOST, 8001, node_type=NodeType.RELAY_BLOCK)
        ]
        network_latency.get_best_relays_by_ping_latency_one_per_country = MagicMock(
            return_value=[relay_connections[0]])
        opts = gateway_helpers.get_gateway_opts(8000,
                                                split_relays=True,
                                                include_default_btc_args=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        node = GatewayNode(opts)
        node.enqueue_connection = MagicMock()

        node._register_potential_relay_peers(
            node._find_best_relay_peers(
                network_latency.
                get_best_relays_by_ping_latency_one_per_country()))
        self.assertEqual(1, len(node.peer_relays))
        self.assertEqual(1, len(node.peer_transaction_relays))
        self.assertEqual(8002, next(iter(node.peer_transaction_relays)).port)

        node.enqueue_connection.assert_has_calls([
            call(LOCALHOST, 8001, ConnectionType.RELAY_BLOCK),
            call(LOCALHOST, 8002, ConnectionType.RELAY_TRANSACTION),
        ],
                                                 any_order=True)

        node.on_connection_added(
            MockSocketConnection(1, node, ip_address=LOCALHOST, port=8001))
        self._check_connection_pool(node, 1, 1, 0, 1)

        node.on_connection_added(
            MockSocketConnection(2, node, ip_address=LOCALHOST, port=8002))
        self._check_connection_pool(node, 2, 1, 1, 2)
    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)
Esempio n. 6
0
    def test_gateway_peer_never_destroy_cli_peer(self):
        peer_gateways = [OutboundPeerModel(LOCALHOST, 8001)]
        opts = gateway_helpers.get_gateway_opts(8000,
                                                peer_gateways=peer_gateways)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        node = GatewayNode(opts)
        node.enqueue_connection = MagicMock()
        node.alarm_queue.register_alarm = MagicMock()
        node.alarm_queue.register_approx_alarm = MagicMock()

        mock_socket = MockSocketConnection(7,
                                           node,
                                           ip_address=LOCALHOST,
                                           port=8001)
        node.on_connection_added(mock_socket)
        cli_peer_conn = node.connection_pool.get_by_ipport(LOCALHOST, 8001)
        node.num_retries_by_ip[(LOCALHOST, 8001)] = MAX_CONNECT_RETRIES
        cli_peer_conn.state = ConnectionState.CONNECTING
        node._connection_timeout(cli_peer_conn)
        self.assertFalse(mock_socket.alive)

        node.on_connection_closed(mock_socket.fileno())
        # timeout is fib(3) == 3
        node.alarm_queue.register_alarm.assert_has_calls([
            call(3, node._retry_init_client_socket, LOCALHOST, 8001,
                 ConnectionType.EXTERNAL_GATEWAY)
        ])
        node._retry_init_client_socket(LOCALHOST, 8001,
                                       ConnectionType.EXTERNAL_GATEWAY)
        self.assertEqual(MAX_CONNECT_RETRIES + 1,
                         node.num_retries_by_ip[(LOCALHOST, 8001)])
Esempio n. 7
0
    async def setUp(self) -> None:
        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.feed_manager = FeedManager(self.node)
        self.node.feed_manager = self.feed_manager
        self.rpc = SubscriptionRpcHandler(self.node, self.feed_manager,
                                          Case.SNAKE)
        self.feed_name = NewTransactionFeed.NAME
        self.node.init_live_feeds()

        self.feed_service_model = FeedServiceModelBase(
            allow_filtering=True, available_fields=["all"])
        self.base_feed_service_model = BdnFeedServiceModelConfigBase(
            expire_date="2999-01-01", feed=self.feed_service_model)
        self.node.account_model = BdnAccountModelBase(
            account_id="account_id",
            certificate="",
            logical_account_name="test",
            new_transaction_streaming=self.base_feed_service_model,
        )
Esempio n. 8
0
def initialize_split_relay_node():
    relay_connections = [
        OutboundPeerModel(LOCALHOST, 8001, node_type=NodeType.RELAY_BLOCK)
    ]
    network_latency.get_best_relays_by_ping_latency_one_per_country = MagicMock(
        return_value=[relay_connections[0]])
    opts = gateway_helpers.get_gateway_opts(8000,
                                            split_relays=True,
                                            include_default_btc_args=True)
    if opts.use_extensions:
        helpers.set_extensions_parallelism()
    node = GatewayNode(opts)
    node.enqueue_connection = MagicMock()

    node._register_potential_relay_peers(
        node._find_best_relay_peers(
            network_latency.get_best_relays_by_ping_latency_one_per_country()))
    node.enqueue_connection.assert_has_calls([
        call(LOCALHOST, 8001, ConnectionType.RELAY_BLOCK),
        call(LOCALHOST, 8002, ConnectionType.RELAY_TRANSACTION),
    ],
                                             any_order=True)
    node.on_connection_added(
        MockSocketConnection(1, node, ip_address=LOCALHOST, port=8001))
    node.on_connection_added(
        MockSocketConnection(2, node, ip_address=LOCALHOST, port=8002))

    node.alarm_queue = AlarmQueue()
    node.enqueue_connection.reset_mock()
    return node
Esempio n. 9
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)
Esempio n. 10
0
    def __init__(self,
                 opts,
                 node_ssl_service: Optional[NodeSSLService] = None,
                 block_queueing_cls=BtcBlockQueuingService):
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        if node_ssl_service is None:
            node_ssl_service = MockNodeSSLService(self.NODE_TYPE, MagicMock())
        super(MockGatewayNode, self).__init__(opts, node_ssl_service)

        self.broadcast_messages = []
        self.broadcast_to_nodes_messages = []
        self._tx_service = GatewayTransactionService(self, 0)
        self.block_cleanup_service = self._get_cleanup_service()
        self.block_queuing_service = block_queueing_cls(self)
        self.message_converter = MockMessageConverter()
        if opts.use_extensions:
            from bxgateway.services.extension_gateway_transaction_service import ExtensionGatewayTransactionService
            self._tx_service = ExtensionGatewayTransactionService(
                self, self.network_num)
        else:
            self._tx_service = GatewayTransactionService(
                self, self.network_num)
        self.opts.has_fully_updated_tx_service = True
        self.requester = MagicMock()
        self.has_active_blockchain_peer = MagicMock(return_value=True)
        self.min_tx_from_node_gas_price = MagicMock()
Esempio n. 11
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)
Esempio n. 12
0
    def setUpClass(cls):
        arg_parser = argparse.ArgumentParser(add_help=False)
        cli.add_argument_parser_logging(arg_parser,
                                        default_log_level=LogLevel.DEBUG)
        opts = arg_parser.parse_args(args=[])
        log_config.setup_logging(
            opts.log_format,
            opts.log_level,
            default_logger_names=[
                "bxcommon", "bxgateway", "bxrelay", "bxgateway_internal",
                "bxapi"
            ],
            log_level_overrides=opts.log_level_overrides,
            enable_fluent_logger=opts.log_fluentd_enable,
            fluentd_host=opts.log_fluentd_host,
            fluentd_queue_size=opts.log_fluentd_queue_size,
            third_party_loggers=node_runner.THIRD_PARTY_LOGGERS,
            fluent_log_level=opts.log_level_fluentd,
            stdout_log_level=opts.log_level_stdout,
        )
        log_config.set_level([LogRecordType.Config.value], LogLevel.WARNING)

        http_service.get_json = MagicMock()
        http_service.post_json = MagicMock()
        http_service.patch_json = MagicMock()
        http_service.delete_json = MagicMock()
        memory_statistics.start_recording = MagicMock()

        REGISTRY.register = MagicMock()
        helpers.set_extensions_parallelism()
    def test_gateway_peer_sdn_update(self):
        # handle duplicates, keeps old, self ip, and maintain peers from CLI
        peer_gateways = [
            OutboundPeerModel(LOCALHOST, 8001, node_type=NodeType.EXTERNAL_GATEWAY),
            OutboundPeerModel(LOCALHOST, 8002, node_type=NodeType.EXTERNAL_GATEWAY)
        ]
        opts = gateway_helpers.get_gateway_opts(8000, peer_gateways=peer_gateways)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        node = GatewayNode(opts)
        node.peer_gateways.add(OutboundPeerModel(LOCALHOST, 8004, node_type=NodeType.EXTERNAL_GATEWAY))

        time.time = MagicMock(return_value=time.time() + 10)
        node.alarm_queue.fire_alarms()

        gateway_peers = [
            OutboundPeerModel(LOCALHOST, 8000, node_type=NodeType.EXTERNAL_GATEWAY),
            OutboundPeerModel(LOCALHOST, 8001, node_type=NodeType.EXTERNAL_GATEWAY),
            OutboundPeerModel(LOCALHOST, 8003, node_type=NodeType.EXTERNAL_GATEWAY)
        ]

        future_obj = Future()
        future_obj.set_result(gateway_peers)
        node._process_gateway_peers_from_sdn(future_obj)

        self.assertEqual(4, len(node.outbound_peers))
        self.assertEqual(4, len(node.peer_gateways))
        self.assertNotIn(OutboundPeerModel(LOCALHOST, 8000, node_type=NodeType.EXTERNAL_GATEWAY), node.peer_gateways)
        self.assertIn(OutboundPeerModel(LOCALHOST, 8001, node_type=NodeType.EXTERNAL_GATEWAY), node.peer_gateways)
        self.assertIn(OutboundPeerModel(LOCALHOST, 8002, node_type=NodeType.EXTERNAL_GATEWAY), node.peer_gateways)
        self.assertIn(OutboundPeerModel(LOCALHOST, 8003, node_type=NodeType.EXTERNAL_GATEWAY), node.peer_gateways)
        self.assertIn(OutboundPeerModel(LOCALHOST, 8004, node_type=NodeType.EXTERNAL_GATEWAY), node.peer_gateways)
Esempio n. 14
0
def make_spy_node(gateway_cls, port, **kwargs):
    opts = gateway_helpers.get_gateway_opts(port, **kwargs)
    if opts.use_extensions:
        helpers.set_extensions_parallelism()
    gateway_node = gateway_cls(
        opts, MockNodeSSLService(gateway_cls.NODE_TYPE, MagicMock()))
    gateway_node.broadcast = MagicMock(wraps=gateway_node.broadcast)
    gateway_node.requester = MagicMock()
    return gateway_node
Esempio n. 15
0
    def test_gateway_peer_sdn_update(self):
        # handle duplicates, keeps old, self ip, and maintain peers from CLI
        peer_gateways = [
            OutboundPeerModel(LOCALHOST,
                              8001,
                              node_type=NodeType.EXTERNAL_GATEWAY),
            OutboundPeerModel(LOCALHOST,
                              8002,
                              node_type=NodeType.EXTERNAL_GATEWAY)
        ]
        opts = gateway_helpers.get_gateway_opts(8000,
                                                peer_gateways=peer_gateways)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        node = GatewayNode(opts)
        node.peer_gateways.add(
            OutboundPeerModel(LOCALHOST,
                              8004,
                              node_type=NodeType.EXTERNAL_GATEWAY))

        sdn_http_service.fetch_gateway_peers = MagicMock(return_value=[
            OutboundPeerModel(
                LOCALHOST, 8000, node_type=NodeType.EXTERNAL_GATEWAY),
            OutboundPeerModel(
                LOCALHOST, 8001, node_type=NodeType.EXTERNAL_GATEWAY),
            OutboundPeerModel(
                LOCALHOST, 8003, node_type=NodeType.EXTERNAL_GATEWAY)
        ])

        node._send_request_for_gateway_peers()
        self.assertEqual(4, len(node.outbound_peers))
        self.assertEqual(4, len(node.peer_gateways))
        self.assertNotIn(
            OutboundPeerModel(LOCALHOST,
                              8000,
                              node_type=NodeType.EXTERNAL_GATEWAY),
            node.peer_gateways)
        self.assertIn(
            OutboundPeerModel(LOCALHOST,
                              8001,
                              node_type=NodeType.EXTERNAL_GATEWAY),
            node.peer_gateways)
        self.assertIn(
            OutboundPeerModel(LOCALHOST,
                              8002,
                              node_type=NodeType.EXTERNAL_GATEWAY),
            node.peer_gateways)
        self.assertIn(
            OutboundPeerModel(LOCALHOST,
                              8003,
                              node_type=NodeType.EXTERNAL_GATEWAY),
            node.peer_gateways)
        self.assertIn(
            OutboundPeerModel(LOCALHOST,
                              8004,
                              node_type=NodeType.EXTERNAL_GATEWAY),
            node.peer_gateways)
Esempio n. 16
0
    def setUp(self):
        super().setUp()

        self.node1.alarm_queue = AlarmQueue()
        self.node2.alarm_queue = AlarmQueue()

        self.network_num = 1
        self.magic = 12345
        self.version = 23456
        self.prev_block_hash = bytearray(crypto.double_sha256(b"123"))
        self.prev_block = BtcObjectHash(self.prev_block_hash,
                                        length=crypto.SHA256_HASH_LEN)
        self.merkle_root_hash = bytearray(crypto.double_sha256(b"234"))
        self.merkle_root = BtcObjectHash(self.merkle_root_hash,
                                         length=crypto.SHA256_HASH_LEN)
        self.bits = 2
        self.nonce = 3

        opts = self.gateway_1_opts()
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        self.btc_message_converter = btc_message_converter_factory.create_btc_message_converter(
            self.magic, opts)

        self.btc_transactions = [
            TxBtcMessage(self.magic, self.version, [], [], i)
            for i in range(self.TRANSACTIONS_COUNT)
        ]
        self.btc_transactions_for_block = [
            tx_btc_message.rawbytes()[btc_constants.BTC_HDR_COMMON_OFF:]
            for tx_btc_message in self.btc_transactions
        ]
        self.transactions = [
            self.btc_message_converter.tx_to_bx_txs(tx_btc_message,
                                                    self.network_num)[0][0]
            for tx_btc_message in self.btc_transactions
        ]
        self.transactions_with_short_ids = [
            TxMessage(tx_message.tx_hash(), tx_message.network_num(), "",
                      i + 1, tx_message.tx_val())
            for i, tx_message in enumerate(self.transactions)
        ]
        self.transactions_with_no_content = [
            TxMessage(tx_message.tx_hash(), tx_message.network_num(), "",
                      i + 1) for i, tx_message in enumerate(self.transactions)
        ]
        self.transactions_by_short_id = {
            tx_message.short_id(): tx_message
            for tx_message in self.transactions_with_short_ids
        }
        self.block = BlockBtcMessage(self.magic, self.version,
                                     self.prev_block, self.merkle_root,
                                     int(time.time()), self.bits, self.nonce,
                                     self.btc_transactions_for_block)
Esempio n. 17
0
    def setUp(self) -> None:
        pub_key = "a04f30a45aae413d0ca0f219b4dcb7049857bc3f91a6351288cce603a2c9646294a02b987bf6586b370b2c22d74662355677007a14238bb037aedf41c2d08866"

        self.opts = gateway_helpers.get_gateway_opts(
            8000,
            include_default_eth_args=True,
            pub_key=pub_key,
            track_detailed_sent_messages=True)

        if self.opts.use_extensions:
            helpers.set_extensions_parallelism()
        self.node = self._get_gateway_node()
        self.transaction_service = self._get_transaction_service()
    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,
                                                                  track_detailed_sent_messages=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        node_ssl_service = MockNodeSSLService(EthGatewayNode.NODE_TYPE, MagicMock())
        self.node = EthGatewayNode(opts, node_ssl_service)

        self.sut = EthNewBlockFeed(self.node)
Esempio n. 19
0
    async def test_gateway_peer_get_more_peers_when_too_few_gateways(self):
        peer_gateways = [
            OutboundPeerModel(LOCALHOST,
                              8001,
                              node_type=NodeType.EXTERNAL_GATEWAY),
        ]
        opts = gateway_helpers.get_gateway_opts(8000,
                                                peer_gateways=peer_gateways,
                                                min_peer_gateways=2)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        node = GatewayNode(opts)
        node.peer_gateways.add(
            OutboundPeerModel(LOCALHOST,
                              8002,
                              node_type=NodeType.EXTERNAL_GATEWAY))
        sdn_http_service.fetch_gateway_peers = MagicMock(return_value=[
            OutboundPeerModel(
                LOCALHOST, 8003, "12345", node_type=NodeType.EXTERNAL_GATEWAY)
        ])

        node.on_connection_added(
            MockSocketConnection(node=node, ip_address=LOCALHOST, port=8002))
        not_cli_peer_conn = node.connection_pool.get_by_ipport(LOCALHOST, 8002)
        not_cli_peer_conn.mark_for_close(False)

        time.time = MagicMock(return_value=time.time() +
                              constants.SDN_CONTACT_RETRY_SECONDS + 1)
        sdn_http_service.fetch_gateway_peers.assert_not_called()
        node.alarm_queue.fire_alarms()

        sdn_http_service.fetch_gateway_peers.assert_has_calls(
            [call(node.opts.node_id, False)])
        self.assertEqual(
            2,
            len([
                node for node in node.outbound_peers
                if node.node_type in NodeType.GATEWAY_TYPE
            ]))
        self.assertIn(
            OutboundPeerModel(LOCALHOST,
                              8001,
                              node_type=NodeType.EXTERNAL_GATEWAY),
            node.outbound_peers)
        self.assertIn(
            OutboundPeerModel(LOCALHOST,
                              8003,
                              "12345",
                              node_type=NodeType.EXTERNAL_GATEWAY),
            node.outbound_peers)
Esempio n. 20
0
    def setUp(self):
        opts = gateway_helpers.get_gateway_opts(8000, include_default_ont_args=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        self.node = MockOntGatewayNode(opts)
        self.node.block_processing_service = MagicMock()

        self.connection = MagicMock()
        self.connection.node = self.node
        self.connection.peer_ip = LOCALHOST
        self.connection.peer_port = 8001
        self.connection.network_num = 2

        self.sut = OntBaseConnectionProtocol(self.connection)
 def init(self, use_extensions: bool):
     opts = Namespace()
     opts.use_extensions = use_extensions
     opts.import_extensions = use_extensions
     opts.tx_mem_pool_bucket_size = DEFAULT_TX_MEM_POOL_BUCKET_SIZE
     ont_message_converter = converter_factory.create_ont_message_converter(self.MAGIC, opts)
     if use_extensions:
         helpers.set_extensions_parallelism()
         tx_service = ExtensionTransactionService(MockNode(
             gateway_helpers.get_gateway_opts(8999)), 0)
     else:
         tx_service = TransactionService(MockNode(
             gateway_helpers.get_gateway_opts(8999)), 0)
     return tx_service, ont_message_converter
    def setUp(self) -> None:
        self.block_confirmations_count = 3
        opts = gateway_helpers.get_gateway_opts(
            8000,
            block_confirmations_count=self.block_confirmations_count - 1,
            include_default_btc_args=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()

        self.node = MockGatewayNode(opts)
        self.transaction_service = self._get_transaction_service()
        self.cleanup_service = self._get_cleanup_service()
        self.node._tx_service = self.transaction_service
        self.node.block_cleanup_service = self.cleanup_service
        self.node.post_block_cleanup_tasks = MagicMock()
        self.tx_hashes = []
Esempio n. 23
0
    def _initialize_gateway(self, initialize_blockchain_conn: bool,
                            initialize_relay_conn: bool) -> GatewayNode:
        opts = gateway_helpers.get_gateway_opts(
            8000,
            blockchain_address=(LOCALHOST, 8001),
            peer_relays=[OutboundPeerModel(LOCALHOST, 8002)],
            include_default_btc_args=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        node = GatewayNode(opts)

        if initialize_blockchain_conn:
            node.on_connection_added(
                MockSocketConnection(1, node, ip_address=LOCALHOST, port=8001))
            self.assertEqual(
                1,
                len(
                    list(
                        node.connection_pool.get_by_connection_types(
                            [ConnectionType.BLOCKCHAIN_NODE]))))
            blockchain_conn = next(
                iter(
                    node.connection_pool.get_by_connection_types(
                        [ConnectionType.BLOCKCHAIN_NODE])))

            node.on_blockchain_connection_ready(blockchain_conn)
            self.assertIsNone(node._blockchain_liveliness_alarm)

        if initialize_relay_conn:
            node.on_connection_added(
                MockSocketConnection(2, node, ip_address=LOCALHOST, port=8002))
            self.assertEqual(
                1,
                len(
                    list(
                        node.connection_pool.get_by_connection_types(
                            [ConnectionType.RELAY_ALL]))))
            relay_conn = next(
                iter(
                    node.connection_pool.get_by_connection_types(
                        [ConnectionType.RELAY_ALL])))

            node.on_relay_connection_ready()
            self.assertIsNone(node._relay_liveliness_alarm)

        return node
Esempio n. 24
0
    def setUp(self) -> None:
        pub_key = "a04f30a45aae413d0ca0f219b4dcb7049857bc3f91a6351288cce603a2c9646294a02b987bf6586b370b2c22d74662355677007a14238bb037aedf41c2d08866"
        opts = gateway_helpers.get_gateway_opts(
            8000,
            include_default_eth_args=True,
            pub_key=pub_key,
            track_detailed_sent_messages=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        node_ssl_service = MockNodeSSLService(EthGatewayNode.NODE_TYPE,
                                              MagicMock())
        self.node = EthGatewayNode(opts, node_ssl_service)
        self.connection_fileno = 1
        self.connection = helpers.create_connection(
            EthNodeConnection, node=self.node, file_no=self.connection_fileno)

        cipher1, cipher2 = AbstractRLPxCipherTest().setup_ciphers()
        self.connection.connection_protocol.rlpx_cipher = cipher1
    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()
        self.connection.node = self.node
        self.connection.peer_ip = LOCALHOST
        self.connection.peer_port = 8001
        self.connection.network_num = 2

        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 = EthBaseConnectionProtocol(self.connection, True, dummy_private_key, dummy_public_key)
 def init(self, use_extensions: bool):
     opts = Namespace()
     opts.use_extensions = use_extensions
     opts.import_extensions = use_extensions
     opts.tx_mem_pool_bucket_size = DEFAULT_TX_MEM_POOL_BUCKET_SIZE
     btc_message_converter = converter_factory.create_btc_message_converter(self.MAGIC, opts=opts)
     if use_extensions:
         helpers.set_extensions_parallelism()
         tx_service = ExtensionTransactionService(MockNode(
             gateway_helpers.get_gateway_opts(8999)), 0)
     else:
         tx_service = TransactionService(MockNode(
             gateway_helpers.get_gateway_opts(8999)), 0)
     if self.txns:
         for idx, txn in enumerate(self.txns):
             sha = btc_common_utils.get_txid(txn)
             if idx % 2 == 0:
                 tx_service.assign_short_id(sha, self.short_ids[int(idx/2)])
                 tx_service.set_transaction_contents(sha, txn)
     return tx_service, btc_message_converter
    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)
Esempio n. 28
0
    def __init__(self, opts, node_ssl_service: Optional[NodeSSLService] = None):
        if opts.use_extensions:
            helpers.set_extensions_parallelism()
        if node_ssl_service is None:
            node_ssl_service = MockNodeSSLService(self.NODE_TYPE, MagicMock())
        super(MockOntGatewayNode, self).__init__(opts, node_ssl_service)
        self.requester = MagicMock()

        self.broadcast_messages = []
        self.send_to_node_messages = []
        self._tx_service = TransactionService(self, 0)
        self.block_cleanup_service = self._get_cleanup_service()
        self.block_queuing_service = OntBlockQueuingService(self)
        self.message_converter = MockMessageConverter()
        if opts.use_extensions:
            from bxcommon.services.extension_transaction_service import ExtensionTransactionService
            self._tx_service = ExtensionTransactionService(self, self.network_num)
        else:
            self._tx_service = TransactionService(self, self.network_num)
        self.opts.has_fully_updated_tx_service = True
        self.node_conn = MagicMock()
        self.node_conn.is_active = MagicMock(return_value=True)
Esempio n. 29
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)
Esempio n. 30
0
    def _set_up_test_node(self, initialize_handshake, generate_pub_key=False):
        # Dummy address
        self.server_ip = "127.0.0.1"
        self.server_port = 1234

        self.blockchain_ip = "0.0.0.0"
        self.blockchain_port = 30303

        # Setting up dummy server addresses
        self.servers = [
            OutboundPeerModel("172.0.0.1", 2222, node_type=NodeType.GATEWAY),
            OutboundPeerModel("172.0.0.2", 3333, node_type=NodeType.GATEWAY),
            OutboundPeerModel("172.0.0.3", 4444, node_type=NodeType.GATEWAY)
        ]
        if generate_pub_key:
            pub_key = convert.bytes_to_hex(self._get_dummy_public_key())
        else:
            pub_key = None
        opts = gateway_helpers.get_gateway_opts(
            self.server_port,
            sid_expire_time=0,
            external_ip=self.server_ip,
            test_mode=[],
            peer_gateways=[],
            peer_relays=self.servers,
            blockchain_address=(self.blockchain_ip, self.blockchain_port),
            include_default_eth_args=True,
            pub_key=pub_key,
            no_discovery=not initialize_handshake)
        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.requester = MagicMock()
        self.assertTrue(self.node)

        return self.node