Пример #1
0
 def get_newer_txs_after(
         self, timestamp: int, hash_bytes: bytes,
         count: int) -> Tuple[List['BaseTransaction'], bool]:
     from hathor.transaction import tx_or_block_from_proto
     self._check_connection()
     if isinstance(timestamp, float):
         self.log.warn(
             'timestamp given in float will be truncated, use int instead')
         timestamp = int(timestamp)
     request = protos.ListRequest(
         tx_type=protos.TRANSACTION_TYPE,
         time_filter=protos.ONLY_NEWER,
         timestamp=timestamp,
         hash=hash_bytes,
         max_count=count,
     )
     result = self._stub.List(request)
     tx_list = []
     has_more = None
     for list_item in result:
         if list_item.HasField('transaction'):
             tx_proto = list_item.transaction
             tx_list.append(tx_or_block_from_proto(tx_proto, storage=self))
         elif list_item.HasField('has_more'):
             has_more = list_item.has_more
             # assuming there are no more items after `has_more`, break soon
             break
         else:
             raise ValueError('unexpected list_item_oneof')
     assert has_more is not None
     return tx_list, has_more
 def _reserialize(self, tx):
     from hathor.transaction import tx_or_block_from_proto
     tx_proto = tx.to_proto()
     print(tx.get_metadata().to_json(), flush=True)
     tx_re = tx_or_block_from_proto(tx_proto)
     print(tx_re.get_metadata().to_json(), flush=True)
     return tx_re
Пример #3
0
    def Remove(self, request: protos.RemoveRequest, context: _Context) -> protos.RemoveResponse:
        from hathor.transaction import tx_or_block_from_proto

        tx_proto = request.transaction

        result = protos.RemoveResponse(removed=False)

        tx = tx_or_block_from_proto(tx_proto, storage=self.storage)
        skip_warning(self.storage.remove_transaction)(tx)
        result.removed = True

        return result
Пример #4
0
    def get_transaction(self, hash_bytes: bytes) -> 'BaseTransaction':
        tx = self.get_transaction_from_weakref(hash_bytes)
        if tx is not None:
            return tx

        from hathor.transaction import tx_or_block_from_proto
        self._check_connection()
        request = protos.GetRequest(hash=hash_bytes)
        result = self._stub.Get(request)
        tx = tx_or_block_from_proto(result.transaction, storage=self)
        self._save_to_weakref(tx)
        return tx
Пример #5
0
    def Save(self, request: protos.SaveRequest, context: _Context) -> protos.SaveResponse:
        from hathor.transaction import tx_or_block_from_proto

        tx_proto = request.transaction
        only_metadata = request.only_metadata

        result = protos.SaveResponse(saved=False)

        tx = tx_or_block_from_proto(tx_proto, storage=self.storage)
        skip_warning(self.storage.save_transaction)(tx, only_metadata=only_metadata)
        result.saved = True

        return result
Пример #6
0
    def MarkAs(self, request, context):
        from hathor.transaction import tx_or_block_from_proto

        tx = tx_or_block_from_proto(request.transaction, storage=self.storage)

        if request.mark_type == protos.FOR_CACHING:
            if request.remove_mark:
                self.storage._del_from_cache(tx, relax_assert=request.relax_assert)
            else:
                self.storage._add_to_cache(tx)
        else:
            raise ValueError('invalid mark_type')

        # TODO: correct value for `marked`
        return protos.MarkAsResponse(marked=True)
Пример #7
0
    def _call_list_request_generators(self, kwargs):
        """ Execute a call for the ListRequest and yield the blocks or txs

            :param kwargs: Parameters to be sent to ListRequest
            :type kwargs: Dict[str,]
        """
        from hathor.transaction import tx_or_block_from_proto
        self._check_connection()
        request = protos.ListRequest(**kwargs)
        result = self._stub.List(request)
        for list_item in result:
            if not list_item.HasField('transaction'):
                break
            tx_proto = list_item.transaction
            yield tx_or_block_from_proto(tx_proto, storage=self)
Пример #8
0
 def get_transactions_before(self, hash_bytes, num_blocks=100):  # pragma: no cover
     from hathor.transaction import tx_or_block_from_proto
     self._check_connection()
     request = protos.ListRequest(
         tx_type=protos.TRANSACTION_TYPE,
         hash=hash_bytes,
         max_count=num_blocks,
         filter_before=True,
     )
     result = self._stub.List(request)
     tx_list = []
     for list_item in result:
         if not list_item.HasField('transaction'):
             break
         tx_proto = list_item.transaction
         tx_list.append(tx_or_block_from_proto(tx_proto, storage=self))
     return tx_list
Пример #9
0
 def get_all_transactions(self) -> Iterator['BaseTransaction']:
     from hathor.transaction import tx_or_block_from_proto
     self._check_connection()
     request = protos.ListRequest()
     result = self._stub.List(request)
     for list_item in result:
         if not list_item.HasField('transaction'):
             break
         tx_proto = list_item.transaction
         tx = tx_or_block_from_proto(tx_proto, storage=self)
         assert tx.hash is not None
         tx2 = self.get_transaction_from_weakref(tx.hash)
         if tx2:
             yield tx2
         else:
             self._save_to_weakref(tx)
             yield tx
Пример #10
0
 def get_blocks_before(self, hash_bytes: bytes, num_blocks: int = 100) -> List[Block]:
     from hathor.transaction import tx_or_block_from_proto
     self._check_connection()
     request = protos.ListRequest(
         tx_type=protos.BLOCK_TYPE,
         hash=hash_bytes,
         max_count=num_blocks,
         filter_before=True,
     )
     result = self._stub.List(request)
     tx_list: List[Block] = []
     for list_item in result:
         if not list_item.HasField('transaction'):
             break
         tx_proto = list_item.transaction
         block = tx_or_block_from_proto(tx_proto, storage=self)
         assert isinstance(block, Block)
         tx_list.append(block)
     return tx_list
Пример #11
0
 def get_newest_txs(self, count: int) -> Tuple[List['BaseTransaction'], bool]:
     from hathor.transaction import tx_or_block_from_proto
     self._check_connection()
     request = protos.ListNewestRequest(tx_type=protos.TRANSACTION_TYPE, count=count)
     result = self._stub.ListNewest(request)
     tx_list = []
     has_more = None
     for list_item in result:
         if list_item.HasField('transaction'):
             tx_proto = list_item.transaction
             tx_list.append(tx_or_block_from_proto(tx_proto, storage=self))
         elif list_item.HasField('has_more'):
             has_more = list_item.has_more
             # assuming there are no more items after `has_more`, break soon
             break
         else:
             raise ValueError('unexpected list_item_oneof')
     assert has_more is not None
     return tx_list, has_more
Пример #12
0
 def get_newest_blocks(self, count: int) -> Tuple[List['Block'], bool]:
     from hathor.transaction import tx_or_block_from_proto
     self._check_connection()
     request = protos.ListNewestRequest(tx_type=protos.BLOCK_TYPE, count=count)
     result = self._stub.ListNewest(request)
     tx_list: List['Block'] = []
     has_more = None
     for list_item in result:
         if list_item.HasField('transaction'):
             tx_proto = list_item.transaction
             blk = tx_or_block_from_proto(tx_proto, storage=self)
             assert isinstance(blk, Block)
             tx_list.append(blk)
         elif list_item.HasField('has_more'):
             has_more = list_item.has_more
             # assuming there are no more items after `has_more`, break soon
             break
         else:
             raise ValueError('unexpected list_item_oneof')
     assert isinstance(has_more, bool)
     return tx_list, has_more
Пример #13
0
    def _call_list_request_generators(
        self,
        kwargs: Optional[Dict[str,
                              Any]] = None) -> Iterator['BaseTransaction']:
        """ Execute a call for the ListRequest and yield the blocks or txs

            :param kwargs: Parameters to be sent to ListRequest
            :type kwargs: Dict[str,]
        """
        from hathor.transaction import tx_or_block_from_proto

        def get_tx(tx):
            tx2 = self.get_transaction_from_weakref(tx.hash)
            if tx2:
                tx = tx2
            else:
                self._save_to_weakref(tx)
            return tx

        self._check_connection()
        if kwargs:
            request = protos.ListRequest(**kwargs)
        else:
            request = protos.ListRequest()

        result = self._stub.List(request)
        for list_item in result:
            if not list_item.HasField('transaction'):
                break
            tx_proto = list_item.transaction
            tx = tx_or_block_from_proto(tx_proto, storage=self)
            assert tx.hash is not None
            lock = self._get_lock(tx.hash)

            if lock:
                with lock:
                    tx = get_tx(tx)
            else:
                tx = get_tx(tx)
            yield tx
Пример #14
0
 def _reserialize(self, tx):
     from hathor.transaction import tx_or_block_from_proto
     tx_proto = tx.to_proto()
     tx_re = tx_or_block_from_proto(tx_proto)
     return tx_re