Esempio n. 1
0
 def from_python_object(cls, py_obj) -> 'TimestampType':
     if isinstance(py_obj, int):
         value = py_obj
     elif isinstance(py_obj, str):
         value = optimize_timestamp(py_obj)
     else:
         assert False, f'unexpected value type {py_obj}'
     return cls.from_value(value)
Esempio n. 2
0
def forge_block_header(shell_header: Dict[str, Any]) -> bytes:
    res = forge_int_fixed(shell_header['level'], 4)
    res += forge_int_fixed(shell_header['proto'], 1)
    res += forge_base58(shell_header['predecessor'])
    res += forge_int_fixed(optimize_timestamp(shell_header['timestamp']), 8)
    res += forge_int_fixed(shell_header['validation_pass'], 1)
    res += forge_base58(shell_header['operations_hash'])
    res += forge_fitness(shell_header['fitness'])
    res += forge_base58(shell_header['context'])
    res += bytes.fromhex(shell_header['protocol_data'])
    return res
Esempio n. 3
0
    def fill(self, block_id='head') -> 'BlockHeader':
        """Fill missing fields essential for preapply

        :param block_id: head or genesis
        :rtype: BlockHeader
        """
        pred_shell_header = self.shell.blocks[block_id].header.shell()
        timestamp = optimize_timestamp(pred_shell_header['timestamp']) + 1
        protocol = self.shell.blocks[block_id].protocols()['next_protocol']
        level = int(pred_shell_header['level']) + 1
        dummy_signature = base58_encode(b'\x00' * 64, b'sig').decode()

        protocol_data = {
            'protocol': protocol,
            **self.protocol_data,
        }

        if level % int(
                sandbox_params['blocks_per_commitment']) == 0:  # type: ignore
            protocol_data['seed_nonce_hash'] = base58_encode(
                b'\x00' * 32, b'nce').decode()

        if 'priority' in protocol_data:
            baker = self.key.public_key_hash()
            baking_rights = self.shell.blocks[block_id].helpers.baking_rights(
                delegate=baker)
            # NOTE: Fails if baker has no baking rights
            protocol_data['priority'] = next(item['priority']
                                             for item in baking_rights
                                             if item['delegate'] == baker)

        operations = [[{
            'protocol': protocol,
            'branch': operation['branch'],
            'contents': operation['contents'],
            'signature': operation['signature'],
        } for operation in operation_list]
                      for operation_list in self.operations]

        payload = {
            'protocol_data': {
                **protocol_data,
                'signature': dummy_signature,
            },
            'operations': operations,
        }

        res = self.shell.blocks[block_id].helpers.preapply.block.post(
            block=payload,
            sort=True,
            timestamp=timestamp,
        )

        forged_operations = [[{
            'branch': operation['branch'],
            'data': operation['data'],
        } for operation in operation_list['applied']]
                             for operation_list in res['operations']]

        return self._spawn(
            shell_header=res['shell_header'],
            operations=forged_operations,
            protocol_data=protocol_data,
            signature=dummy_signature,
        )
Esempio n. 4
0
    def fill(self, block_id='head', timestamp: Optional[int] = None) -> 'BlockHeader':
        """Fill missing fields essential for preapply

        :param block_id: head or genesis
        :param timestamp: override header timestamp (unix seconds).
            NOTE that the minimal block granularity in Tezos is 1 sec, so you cannot bake faster that once per second.
            You also cannot bake with timestamp in the future, it will end with error.
            The workaround is to set the genesis block timestamp to zero (or very old date)
            so that you can continuously increase it by 1 sec.
        :rtype: BlockHeader
        """
        pred_shell_header = self.shell.blocks[block_id].header.shell()
        if timestamp is None:
            timestamp = optimize_timestamp(pred_shell_header['timestamp']) + 1
        protocol = self.shell.blocks[block_id].protocols()['next_protocol']
        level = int(pred_shell_header['level']) + 1
        dummy_signature = base58_encode(b'\x00' * 64, b'sig').decode()

        protocol_data = {
            'protocol': protocol,
            **self.protocol_data,
        }

        if level % int(sandbox_params['blocks_per_commitment']) == 0:  # type: ignore
            protocol_data['seed_nonce_hash'] = base58_encode(b'\x00' * 32, b'nce').decode()

        if 'priority' in protocol_data:
            baker = self.key.public_key_hash()
            baking_rights = self.shell.blocks[block_id].helpers.baking_rights(delegate=baker)
            # NOTE: Fails if baker has no baking rights
            protocol_data['priority'] = next(item['priority'] for item in baking_rights if item['delegate'] == baker)

        operations = [
            [
                {
                    'protocol': protocol,
                    'branch': operation['branch'],
                    'contents': operation['contents'],
                    'signature': operation['signature'],
                }
                for operation in operation_list
            ]
            for operation_list in self.operations
        ]

        payload = {
            'protocol_data': {
                **protocol_data,
                'signature': dummy_signature,
            },
            'operations': operations,
        }

        res = self.shell.blocks[block_id].helpers.preapply.block.post(
            block=payload,
            sort=True,
            timestamp=timestamp,
        )

        forged_operations = [
            [
                {
                    'branch': operation['branch'],
                    'data': operation['data'],
                }
                for operation in operation_list['applied']
            ]
            for operation_list in res['operations']
        ]

        return self._spawn(
            shell_header=res['shell_header'],
            operations=forged_operations,
            protocol_data=protocol_data,
            signature=dummy_signature,
        )