def __init__( self, node: "AbstractGatewayNode", connection: AbstractGatewayBlockchainConnection ): self.node = node self.connection = connection self._blocks: ExpiringSet[Sha256Hash] = ExpiringSet( node.alarm_queue, gateway_constants.MAX_BLOCK_CACHE_TIME_S, f"block_queuing_service_hashes_{self.connection.endpoint}" ) # queue of tuple (block hash, timestamp) for blocks that need to be # sent to blockchain node self._block_queue: Deque[BlockQueueEntry] = deque( maxlen=gateway_constants.BLOCK_QUEUE_LENGTH_LIMIT ) self._blocks_waiting_for_recovery: Dict[Sha256Hash, bool] = {} self._blocks_seen_by_blockchain_node: ExpiringSet[ Sha256Hash ] = ExpiringSet( node.alarm_queue, gateway_constants.GATEWAY_BLOCKS_SEEN_EXPIRATION_TIME_S, f"block_queuing_service_blocks_seen_{self.connection.endpoint}", )
def __init__(self, node: "EthGatewayNode", network_num: int = constants.ALL_NETWORK_NUM, ) -> None: super().__init__(self.NAME, network_num) self.node = node self.last_block_number = 0 self.published_blocks = ExpiringSet( node.alarm_queue, gateway_constants.MAX_BLOCK_CACHE_TIME_S, name="receipts_feed_published_blocks" ) self.published_blocks_height = ExpiringSet( node.alarm_queue, gateway_constants.MAX_BLOCK_CACHE_TIME_S, name="receipts_feed_published_blocks_height" ) self.blocks_confirmed_by_new_heads_notification = ExpiringSet( node.alarm_queue, gateway_constants.MAX_BLOCK_CACHE_TIME_S, name="receipts_feed_newHeads_confirmed_blocks" )
def __init__(self, node: "EthGatewayNode") -> None: super().__init__(self.NAME) self.last_block_number = 0 self.hash_for_last_block_number = set() self.node = node self.published_blocks = ExpiringSet( node.alarm_queue, gateway_constants.MAX_BLOCK_CACHE_TIME_S, name="published_blocks") self.published_blocks_height = ExpiringSet( node.alarm_queue, gateway_constants.MAX_BLOCK_CACHE_TIME_S, name="published_blocks_height")
def test_get_object_size(self): mock_node = MockNode(get_common_opts(1234)) object_size = memory_utils.get_object_size(mock_node) self.assertIsInstance(object_size, ObjectSize) self.assertTrue(object_size.size > 0) self.assertTrue(object_size.flat_size > 0) self.assertTrue(object_size.is_actual_size) self.assertEqual(0, len(object_size.references)) ex_set = ExpiringSet(AlarmQueue(), 10, "testset") s = set() h1 = Sha256Hash(b"1" * 32) h2 = Sha256Hash(b"0" * 32) print(memory_utils.get_object_size(ex_set).size) print(memory_utils.get_object_size(s).size) print(memory_utils.get_object_size(h1).size) print(memory_utils.get_object_size(h2).size) print(memory_utils.get_special_size(ex_set).size) print(memory_utils.get_special_size(s).size) print(memory_utils.get_special_size(h1).size) print(memory_utils.get_special_size(h2).size) ex_set.add(h1) ex_set.add(h2) s.add(h1) s.add(h2) print(memory_utils.get_object_size(ex_set).size) print(memory_utils.get_special_size(ex_set).size) print(memory_utils.get_object_size(s).size)
def __init__(self, alarm_queue: AlarmQueue) -> None: super().__init__(self.NAME) # enforce uniqueness, since multiple sources can publish to # pending transactions (eth ws + remote) self.published_transactions = ExpiringSet( alarm_queue, EXPIRATION_TIME_S, "pendingTxs" )
def __init__( self, alarm_queue: AlarmQueue, network_num: int = constants.ALL_NETWORK_NUM, ) -> None: super().__init__(self.NAME, network_num=network_num) # enforce uniqueness, since multiple sources can publish to # pending transactions (eth ws + remote) self.published_transactions = ExpiringSet(alarm_queue, EXPIRATION_TIME_S, "pendingTxs")
def test_request_block_propagation(self): block = btc_block().rawbytes() # propagate block helpers.receive_node_message(self.node1, self.blockchain_fileno, block) block_hold_request_relay = self.node1.get_bytes_to_send( self.relay_fileno) self.assertIn(BlockHoldingMessage.MESSAGE_TYPE, block_hold_request_relay.tobytes()) self.node1.on_bytes_sent(self.relay_fileno, len(block_hold_request_relay)) relayed_block = self.node1.get_bytes_to_send(self.relay_fileno) self.assertIn(BroadcastMessage.MESSAGE_TYPE, relayed_block.tobytes()) block_hold_request_gateway = self.node1.get_bytes_to_send( self.gateway_fileno) self.assertIn(BlockHoldingMessage.MESSAGE_TYPE, block_hold_request_gateway.tobytes()) self.clear_all_buffers() # receipt timeout time.time = MagicMock( return_value=time.time() + gateway_constants.NEUTRALITY_BROADCAST_BLOCK_TIMEOUT_S) self.node1.alarm_queue.fire_alarms() key_msg_gateway = self.node1.get_bytes_to_send(self.gateway_fileno) self.assertIn(KeyMessage.MESSAGE_TYPE, key_msg_gateway.tobytes()) self.node1.on_bytes_sent(self.gateway_fileno, len(key_msg_gateway)) block_prop_request = self.node1.get_bytes_to_send(self.gateway_fileno) self.assertIn(BlockPropagationRequestMessage.MESSAGE_TYPE, block_prop_request.tobytes()) self.clear_all_buffers() # get new block to send helpers.receive_node_message(self.node2, self.gateway_fileno, block_prop_request) new_relayed_block = self.node2.get_bytes_to_send(self.relay_fileno) self.assertIn(BroadcastMessage.MESSAGE_TYPE, new_relayed_block.tobytes()) helpers.clear_node_buffer(self.node2, self.relay_fileno) # receive new block helpers.receive_node_message(self.node1, self.relay_fileno, new_relayed_block) block_receipt = self.node1.get_bytes_to_send(self.gateway_fileno) self.assertIn(BlockReceivedMessage.MESSAGE_TYPE, block_receipt.tobytes()) # receive block receipt helpers.receive_node_message(self.node2, self.gateway_fileno, block_receipt) key_message = self.node2.get_bytes_to_send(self.relay_fileno) self.assertIn(KeyMessage.MESSAGE_TYPE, key_message.tobytes()) # receive key, but already seen so dont forward to blockchain helpers.receive_node_message(self.node1, self.relay_fileno, key_message) bytes_to_blockchain = self.node1.get_bytes_to_send( self.blockchain_fileno) self.assertEqual(OutputBuffer.EMPTY, bytes_to_blockchain) # clear blocks seen, rereceive self.node1.blocks_seen = ExpiringSet( self.node1.alarm_queue, gateway_constants.GATEWAY_BLOCKS_SEEN_EXPIRATION_TIME_S, "testset") helpers.receive_node_message(self.node1, self.relay_fileno, key_message) # ignore key message even if block is not in "blocks_seen" bytes_to_blockchain = self.node1.get_bytes_to_send( self.blockchain_fileno) self.assertEqual(OutputBuffer.EMPTY, bytes_to_blockchain)
def setUp(self): self.sut = ExpiringSet(self.ALARM_QUEUE, self.EXPIRATION_TIME_S, "testset")