Ejemplo n.º 1
0
    def __getitem__(self, block_id):
        """Construct block query or get a block range.

        block_id could be one of:
        - int: Block level or offset from the head if negative;
        - str: Block hash (base58) or special names (head, genesis), expressions like `head~1` etc;
        - slice [:]: First value (start) must be int, second (stop) can be any Block ID or empty.

        :param block_id: Block identity or block range
        :rtype: BlockQuery or BlockSliceQuery
        """
        if isinstance(block_id, slice):
            if isinstance(block_id.start, str):
                block_id = slice(
                    self._get_block(block_id.start).level(), block_id.stop,
                    None)
            elif not isinstance(block_id.start, int):
                raise NotImplementedError('Slice start should be an integer.')

            return BlockSliceQuery(
                start=block_id.start,
                stop=block_id.stop,
                node=self.node,
                path=self._wild_path,
                params=self._params,
            )

        if isinstance(block_id, int) and block_id < 0:
            head_level = self._get_block('head').level()
            block_id = max(0, head_level + block_id)

        return self._get_block(block_id)
Ejemplo n.º 2
0
    def __getitem__(self, block_id):
        """ Construct block query or get a block range.

        :param block_id: Block identity or block range

          - int: Block level or offset from the head if negative;
          - str: Block hash (base58) or special names (head, genesis), expressions like `head~1` etc;
          - slice [:]: First value (start) must be int, second (stop) can be any Block ID or empty.
        :rtype: BlockQuery or BlockSliceQuery
        """
        if isinstance(block_id, slice):
            if not isinstance(block_id.start, int):
                raise NotImplementedError('Slice start should be an integer.')

            return BlockSliceQuery(
                start=block_id.start,
                stop=block_id.stop,
                node=self.node,
                path=self._wild_path,
                params=self._params
            )

        if isinstance(block_id, int) and block_id < 0:
            return self.blocks[f'head~{block_id}']

        return super(BlocksQuery, self).__getitem__(block_id)
Ejemplo n.º 3
0
 def current_cycle(self):
     """
     Get block range for the current cycle.
     :return: BlockSliceQuery
     """
     metadata = self.head.metadata()
     return BlockSliceQuery(start=metadata['level']['level'] -
                            metadata['level']['cycle_position'],
                            stop='head',
                            node=self.node,
                            path=self._wild_path,
                            params=self._params)
Ejemplo n.º 4
0
    def current_voting_period(self):
        """ Get block range for the current voting period.

        :rtype: BlockSliceQuery
        """
        metadata = self.head.metadata()
        return BlockSliceQuery(start=metadata['level']['level'] -
                               metadata['level']['voting_period_position'],
                               stop='head',
                               node=self.node,
                               path=self._wild_path,
                               params=self._params)