Example #1
0
def test_transform_txn_for_catchup_rep(alh, db_manager,
                                       initial_domain_size, initial_pool_size, initial_config_size):
    do_apply_audit_txn(alh,
                       txns_count=10, ledger_id=DOMAIN_LEDGER_ID,
                       view_no=0, pp_sq_no=1, txn_time=10000,
                       has_audit_txn=True)

    audit_txn_after_serialization = \
        JsonSerializer.loads(
            JsonSerializer.dumps(
                alh.ledger.get_last_txn()
            )
        )

    transformed_audit_txn = alh.transform_txn_for_ledger(audit_txn_after_serialization)
    check_audit_txn(txn=transformed_audit_txn,
                    view_no=0, pp_seq_no=1,
                    seq_no=1, txn_time=10000,
                    ledger_id=DOMAIN_LEDGER_ID,
                    txn_root=db_manager.get_ledger(DOMAIN_LEDGER_ID).uncommitted_root_hash,
                    state_root=db_manager.get_state(DOMAIN_LEDGER_ID).headHash,
                    pool_size=initial_pool_size,
                    domain_size=initial_domain_size + 10,
                    config_size=initial_config_size,
                    last_pool_seqno=None,
                    last_domain_seqno=None,
                    last_config_seqno=None)
Example #2
0
    def static_validation(self, request: Request):
        self._validate_request_type(request)
        try:
            content_as_dict = JsonSerializer.loads(
                request.operation[RS_CONTENT])
        except ValueError:
            raise InvalidClientRequest(
                request.identifier, request.reqId,
                "'{}' must be a JSON serialized string".format(RS_CONTENT))

        if self.is_json_ld_content():
            self.do_static_validation_json_ld(content_as_dict, request)

        self.do_static_validation_content(content_as_dict, request)
    def do_dynamic_validation_content(self, request):
        # it has been checked on static validation step that the content is a valid JSON.
        # and it has schema and mapping fields
        content_as_dict = JsonSerializer.loads(request.operation[RS_CONTENT])
        schema_id = content_as_dict[RS_CRED_DEF_SCHEMA]
        mapping_id = content_as_dict[RS_CRED_DEF_MAPPING]

        # 1. check that the schema field points to an existing object on the ledger
        schema, _, _ = self.get_from_state(schema_id)
        if not schema:
            raise InvalidClientRequest(
                request.identifier, request.reqId,
                "Can not find a referenced '{}' with id={}; please make sure that it has been added to the ledger"
                .format(RS_CRED_DEF_SCHEMA, schema_id))

        # 2. check that the mapping field points to an existing object on the ledger
        mapping, _, _ = self.get_from_state(mapping_id)
        if not mapping:
            raise InvalidClientRequest(
                request.identifier, request.reqId,
                "Can not find a referenced '{}' with id={}; please make sure that it has been added to the ledger"
                .format(RS_CRED_DEF_MAPPING, mapping_id))

        # 3. check that the schema field points to an object of the Schema type
        if schema.get(RS_TYPE) != RS_SCHEMA_TYPE_VALUE:
            raise InvalidClientRequest(
                request.identifier, request.reqId,
                "'{}' field must reference a schema with {}={}".format(
                    RS_CRED_DEF_SCHEMA, RS_TYPE, RS_SCHEMA_TYPE_VALUE))

        # 4. check that the mapping fields points to an object of the Mapping type
        if mapping.get(RS_TYPE) != RS_MAPPING_TYPE_VALUE:
            raise InvalidClientRequest(
                request.identifier, request.reqId,
                "'{}' field must reference a mapping with {}={}".format(
                    RS_CRED_DEF_MAPPING, RS_TYPE, RS_MAPPING_TYPE_VALUE))
    def do_dynamic_validation_content(self, request):
        # it has been checked on static validation step that the content is a valid JSON.
        # and it has schema and attributes fields
        content_as_dict = JsonSerializer.loads(request.operation[RS_CONTENT])

        # 1. check that the schema field points to an existing object on the ledger
        schema_id = content_as_dict[RS_MAPPING_SCHEMA]
        schema, _, _ = self.get_from_state(schema_id)
        if not schema:
            raise InvalidClientRequest(request.identifier,
                                       request.reqId,
                                       'Can not find a schema with id={}; please make sure that it has been added to the ledger'.format(
                                           schema_id))

        # 2. check that the schema field points to an object of the Schema type
        if schema.get(RS_TYPE) != RS_SCHEMA_TYPE_VALUE:
            raise InvalidClientRequest(request.identifier,
                                       request.reqId,
                                       "'{}' field must reference a schema with {}={}".format(
                                           RS_MAPPING_SCHEMA, RS_TYPE, RS_SCHEMA_TYPE_VALUE))

        # 3. find all attribute leaf dicts with encoding-rank pairs
        enc_desc_dicts = list(find_encoding_desc_dicts(content_as_dict[RS_MAPPING_ATTRIBUTES]))

        # 4. check that every dict has encoding and rank fields
        # Note: this check can be done in static validation, but then we will have to traverse the leaf dicts twice
        for desc_dict, attr in enc_desc_dicts:
            if not isinstance(desc_dict, dict):
                raise InvalidClientRequest(request.identifier,
                                           request.reqId,
                                           "{} and {} must be set for the attribute '{}'".format(RS_MAPPING_ENC,
                                                                                                 RS_MAPPING_RANK, attr))

            missing_fields = []
            for field in [RS_MAPPING_ENC, RS_MAPPING_RANK]:
                v = desc_dict.get(field)
                if not v and v != 0:
                    missing_fields.append(field)

            if missing_fields:
                missing_fields_str = " and ".join(missing_fields)
                raise InvalidClientRequest(request.identifier, request.reqId,
                                           "{} must be set for the attribute '{}'".format(missing_fields_str, attr))

        # 5. check that all ranks are unique and form a sequence without gaps
        # Note: this check can be done in static validation, but then we will have to traverse the leaf dicts twice
        expected_ranks = list(range(1, len(enc_desc_dicts) + 1))
        ranks = sorted([desc_dict[RS_MAPPING_RANK] for desc_dict, attr in enc_desc_dicts])
        if ranks != expected_ranks:
            raise InvalidClientRequest(request.identifier, request.reqId,
                                       "the attribute's ranks are not sequential: expected ranks are all values from 1 to {}".format(
                                           len(enc_desc_dicts)))

        # 6. check that all the enc fields point to an existing object on the ledger of the type Encoding
        for desc_dict, attr in enc_desc_dicts:
            encoding_id = desc_dict[RS_MAPPING_ENC]
            encoding, _, _ = self.get_from_state(encoding_id)
            if not encoding:
                raise InvalidClientRequest(request.identifier,
                                           request.reqId,
                                           "Can not find a referenced '{}' with id={} in the '{}' attribute; please make sure that it has been added to the ledger".format(
                                               RS_MAPPING_ENC, encoding_id, attr))
            if encoding.get(RS_TYPE) != RS_ENCODING_TYPE_VALUE:
                raise InvalidClientRequest(request.identifier,
                                           request.reqId,
                                           "'{}' field in the '{}' attribute must reference an encoding with {}={}".format(
                                               RS_MAPPING_ENC, attr, RS_TYPE, RS_ENCODING_TYPE_VALUE))