Example #1
0
    def validate_timestamp(self, blockchain):
        timestamp = self.timestamp
        validate_not_none(f'{self.humanized_class_name} timestamp', timestamp)
        validate_type(f'{self.humanized_class_name} timestamp', timestamp,
                      datetime)
        validate_is_none(f'{self.humanized_class_name} timestamp timezone',
                         timestamp.tzinfo)

        block_number = self.block_number
        assert block_number is not None

        if block_number > 0:
            prev_block_number = block_number - 1
            prev_block = blockchain.get_block_by_number(prev_block_number)
            if prev_block is None:
                logger.debug('Partial blockchain detected')
                blockchain_state = blockchain.get_closest_blockchain_state_snapshot(
                    block_number)
                validate_not_none('Closest blockchain state', blockchain_state)

                if blockchain_state.is_initial():
                    raise ValidationError(
                        'Unexpected initial account root file found')

                validate_exact_value('Blockchain state last block number',
                                     blockchain_state.last_block_number,
                                     prev_block_number)

                assert blockchain_state.last_block_timestamp
                min_timestamp = blockchain_state.last_block_timestamp
            else:
                min_timestamp = prev_block.message.timestamp

            validate_gt_value(f'{self.humanized_class_name} timestamp',
                              timestamp, min_timestamp)
Example #2
0
 def validate_fee_amount(self):
     validate_not_none(f'{self.humanized_class_name} fee_amount',
                       self.fee_amount)
     validate_type(f'{self.humanized_class_name} fee_amount',
                   self.fee_amount, int)
     validate_gte_value(f'{self.humanized_class_name} fee_amount',
                        self.fee_amount, 0)
Example #3
0
 def validate_block_number(self):
     block_number = self.block_number
     validate_not_none(f'{self.humanized_class_name} block_number',
                       block_number)
     validate_type(f'{self.humanized_class_name} block_number',
                   block_number, int)
     validate_gte_value(f'{self.humanized_class_name} block_number',
                        block_number, 0)
    def _transform_dict(cls, dict_, transform_map):
        field_types = cls.get_field_types(dict_)

        new_dict = {}
        for key, value in dict_.items():
            type_ = field_types.get(key)
            validate_not_none(f'{cls.__name__} {key} type', type_)
            value = transform_value(value, type_, transform_map)
            new_dict[key] = value

        return new_dict
Example #5
0
 def validate_last_block_timestamp(self, is_initial):
     timestamp = self.last_block_timestamp
     if is_initial:
         validate_is_none(
             f'Initial {self.humanized_class_name} last_block_timestamp',
             timestamp)
     else:
         validate_not_none(
             f'{self.humanized_class_name} last_block_timestamp', timestamp)
         validate_type(f'{self.humanized_class_name} last_block_timestamp',
                       timestamp, datetime)
         validate_is_none(
             f'{self.humanized_class_name} last_block_timestamp timezone',
             timestamp.tzinfo)
Example #6
0
    def validate_block_identifier(self, blockchain):
        block_identifier = self.block_identifier
        validate_not_none(f'{self.humanized_class_name} block_identifier',
                          block_identifier)
        validate_type(f'{self.humanized_class_name} block_identifier',
                      block_identifier, str)

        block_number = self.block_number
        assert block_number is not None

        expected_identifier = blockchain.get_expected_block_identifier(
            block_number)
        validate_exact_value(f'{self.humanized_class_name} block_identifier',
                             block_identifier, expected_identifier)
Example #7
0
    def deserialize_from_dict(cls, dict_, complain_excessive_keys=True, exclude=()):
        dict_ = dict_.copy()
        message_dict = dict_.pop('message', None)
        validate_not_none(f'{cls.humanized_class_name} message', message_dict)
        validate_type(f'{cls.humanized_class_name} message', message_dict, dict)

        signed_change_request_dict = message_dict.get('signed_change_request')
        validate_not_none(f'{cls.humanized_class_name} message.signed_change_request', signed_change_request_dict)
        validate_type(f'{cls.humanized_class_name} message.signed_change_request', signed_change_request_dict, dict)

        instance_block_type = message_dict.get('block_type')
        validate_not_none(f'{cls.humanized_class_name} message.block_type', instance_block_type)
        validate_in(
            f'{cls.humanized_class_name} message.block_type', instance_block_type, SIGNED_CHANGE_REQUEST_TYPE_MAP
        )
        signed_change_request_class = SIGNED_CHANGE_REQUEST_TYPE_MAP[instance_block_type]
        signed_change_request_obj = signed_change_request_class.deserialize_from_dict(signed_change_request_dict)

        message_obj = BlockMessage.deserialize_from_dict(
            message_dict, override={'signed_change_request': signed_change_request_obj}
        )

        return super().deserialize_from_dict(
            dict_, complain_excessive_keys=complain_excessive_keys, override={'message': message_obj}
        )
Example #8
0
 def validate_signed_change_request(self, blockchain):
     signed_change_request = self.signed_change_request
     validate_not_none(f'{self.humanized_class_name} signed_change_request',
                       self.signed_change_request)
     signed_change_request.validate(blockchain, self.block_number)