示例#1
0
class ClientMessageValidator(MessageValidator):
    schema = (
        (f.IDENTIFIER.nm, IdentifierField(optional=True, nullable=True)),
        (f.REQ_ID.nm, NonNegativeNumberField()),
        (OPERATION, ClientOperationField()),
        (f.SIG.nm, SignatureField(max_length=SIGNATURE_FIELD_LIMIT,
                                  optional=True)),
        (f.DIGEST.nm, LimitedLengthStringField(max_length=DIGEST_FIELD_LIMIT,
                                               optional=True)),
        (f.PROTOCOL_VERSION.nm, ProtocolVersionField()),
        (f.SIGS.nm, MapField(IdentifierField(),
                             SignatureField(max_length=SIGNATURE_FIELD_LIMIT),
                             optional=True, nullable=True)),
    )

    def __init__(self, operation_schema_is_strict, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Following code is for support of non-strict schema
        # TODO: refactor this
        # TODO: this (and all related functionality) can be removed when
        # when fixed problem with transaction serialization (INDY-338)
        # Adding fields from enabled plugins to schema.
        self.schema = self.schema + tuple(PLUGIN_CLIENT_REQUEST_FIELDS.items())
        if operation_schema_is_strict:
            operation_field_index = 2
            op = ClientOperationField(schema_is_strict=operation_schema_is_strict)
            schema = list(self.schema)
            schema[operation_field_index] = (OPERATION, op)
            self.schema = tuple(schema)

    def validate(self, dct):
        super().validate(dct)
        if not (dct.get(f.IDENTIFIER.nm) or dct.get(f.SIGS.nm)):
            self._raise_invalid_message(
                'Missing both signatures and identifier')
示例#2
0
class LedgerStatus(MessageBase):
    """
    Purpose: spread status of ledger copy on a specific node.
    When node receives this message and see that it has different
    status of ledger it should reply with LedgerStatus that contains its
    status
    """
    typename = LEDGER_STATUS
    schema = ((f.LEDGER_ID.nm, LedgerIdField()), (f.TXN_SEQ_NO.nm,
                                                  NonNegativeNumberField()),
              (f.VIEW_NO.nm, NonNegativeNumberField(nullable=True)),
              (f.PP_SEQ_NO.nm, NonNegativeNumberField(nullable=True)),
              (f.MERKLE_ROOT.nm, MerkleRootField()), (f.PROTOCOL_VERSION.nm,
                                                      ProtocolVersionField()))
示例#3
0
class ClientMessageValidator(MessageValidator):

    def __init__(self, operation_schema_is_strict, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Following code is for support of non-strict schema
        # TODO: refactor this
        # TODO: this (and all related functionality) can be removed when
        # when fixed problem with transaction serialization (INDY-338)
        strict = operation_schema_is_strict
        if not strict:
            operation_field_index = 2
            op = ClientOperationField(schema_is_strict=False)
            schema = list(self.schema)
            schema[operation_field_index] = (OPERATION, op)
            self.schema = tuple(schema)

    schema = (
        (f.IDENTIFIER.nm, IdentifierField()),
        (f.REQ_ID.nm, NonNegativeNumberField()),
        (OPERATION, ClientOperationField()),
        (f.SIG.nm, SignatureField(max_length=SIGNATURE_FIELD_LIMIT, optional=True)),
        (f.DIGEST.nm, LimitedLengthStringField(max_length=DIGEST_FIELD_LIMIT, optional=True)),
        (f.PROTOCOL_VERSION.nm, ProtocolVersionField(optional=True)),
    )
示例#4
0
from plenum.common.messages.fields import ProtocolVersionField
from plenum.common.plenum_protocol_version import PlenumProtocolVersion

validator = ProtocolVersionField()


def test_valid():
    assert not validator.validate(2)
    assert not validator.validate(
        PlenumProtocolVersion.TXN_FORMAT_1_0_SUPPORT.value)


def test_invalid():
    assert validator.validate(None)
    assert validator.validate(PlenumProtocolVersion.STATE_PROOF_SUPPORT.value)
    assert validator.validate(1)
    assert validator.validate(3)
    assert validator.validate("1")
    assert validator.validate("")
    assert validator.validate(0)
    assert validator.validate(1.0)
    assert validator.validate(0.1)
示例#5
0
class ClientMessageValidator(MessageValidator):
    schema = (
        (f.IDENTIFIER.nm, IdentifierField(optional=True, nullable=True)),
        (f.REQ_ID.nm, NonNegativeNumberField()),
        (OPERATION, ClientOperationField()),
        (f.SIG.nm,
         SignatureField(max_length=SIGNATURE_FIELD_LIMIT, optional=True)),
        (f.DIGEST.nm,
         LimitedLengthStringField(max_length=DIGEST_FIELD_LIMIT,
                                  optional=True)),
        (f.PROTOCOL_VERSION.nm, ProtocolVersionField()),
        (f.TAA_ACCEPTANCE.nm, ClientTAAAcceptance(optional=True)),
        (f.SIGS.nm,
         MapField(IdentifierField(),
                  SignatureField(max_length=SIGNATURE_FIELD_LIMIT),
                  optional=True,
                  nullable=True)),
        (f.ENDORSER.nm, IdentifierField(optional=True)),
    )

    def __init__(self, operation_schema_is_strict, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Following code is for support of non-strict schema
        # TODO: refactor this
        # TODO: this (and all related functionality) can be removed when
        # when fixed problem with transaction serialization (INDY-338)
        # Adding fields from enabled plugins to schema.
        self.schema = self.schema + tuple(PLUGIN_CLIENT_REQUEST_FIELDS.items())
        if operation_schema_is_strict:
            operation_field_index = 2
            op = ClientOperationField(
                schema_is_strict=operation_schema_is_strict)
            schema = list(self.schema)
            schema[operation_field_index] = (OPERATION, op)
            self.schema = tuple(schema)

    def validate(self, dct):
        super().validate(dct)
        identifier = dct.get(f.IDENTIFIER.nm, None)
        signatures = dct.get(f.SIGS.nm, None)
        signature = dct.get(f.SIG.nm, None)
        endorser = dct.get(f.ENDORSER.nm, None)
        if signatures and signature:
            self._raise_invalid_message(
                'Request can not contain both fields "signatures" and "signature"'
            )
        if endorser is not None:
            if not signatures or endorser not in signatures:
                self._raise_invalid_message("Endorser must sign the request")
            if identifier is None:
                self._raise_invalid_message(
                    "Author's Identifier must be present when sending via Endorser"
                )
            if not signatures or identifier not in signatures:
                self._raise_invalid_message(
                    "Author must sign the request when sending via Endorser")
        if identifier and signatures and identifier not in signatures:
            self._raise_invalid_message(
                'The identifier is not contained in signatures')
        if not (identifier or signatures):
            self._raise_invalid_message(
                'Missing both signatures and identifier')