diff --git a/bigchaindb/common/schema/__init__.py b/bigchaindb/common/schema/__init__.py index 9fdb3136..a49495f9 100644 --- a/bigchaindb/common/schema/__init__.py +++ b/bigchaindb/common/schema/__init__.py @@ -7,18 +7,31 @@ import yaml from bigchaindb.common.exceptions import SchemaValidationError -TX_SCHEMA_PATH = os.path.join(os.path.dirname(__file__), 'transaction.yaml') -with open(TX_SCHEMA_PATH) as handle: - TX_SCHEMA_YAML = handle.read() -TX_SCHEMA = yaml.safe_load(TX_SCHEMA_YAML) +def _load_schema(name): + """ Load a schema from disk """ + path = os.path.join(os.path.dirname(__file__), name + '.yaml') + with open(path) as handle: + return path, yaml.safe_load(handle) -def validate_transaction_schema(tx_body): - """ Validate a transaction dict against a schema """ +def _validate_schema(schema, body): + """ Validate data against a schema """ try: - jsonschema.validate(tx_body, TX_SCHEMA) + jsonschema.validate(body, schema) except jsonschema.ValidationError as exc: raise SchemaValidationError(str(exc)) from exc -__all__ = ['TX_SCHEMA', 'TX_SCHEMA_YAML', 'validate_transaction_schema'] +TX_SCHEMA_PATH, TX_SCHEMA = _load_schema('transaction') +VOTE_SCHEMA_PATH, VOTE_SCHEMA = _load_schema('vote') + + +def validate_transaction_schema(tx_body): + """ Validate a transaction dict """ + _validate_schema(TX_SCHEMA, tx_body) + + +def validate_vote_schema(tx_body): + """ Validate a vote dict """ + _validate_schema(VOTE_SCHEMA, tx_body) + diff --git a/bigchaindb/common/schema/vote.yaml b/bigchaindb/common/schema/vote.yaml new file mode 100644 index 00000000..5bd02cda --- /dev/null +++ b/bigchaindb/common/schema/vote.yaml @@ -0,0 +1,65 @@ +--- +"$schema": "http://json-schema.org/draft-04/schema#" +id: "http://www.bigchaindb.com/schema/vote.json" +type: object +additionalProperties: false +title: Vote Schema +description: | + A Vote is an endorsement of a Block (identified by a hash) by + a node (identified by a public key). +required: +- node_pubkey +- signature +- vote +properties: + node_pubkey: + type: "string" + pattern: "[1-9a-zA-Z^OIl]{43,44}" + description: | + Ed25519 public key identifying the voting node. + signature: + type: "string" + pattern: "[1-9a-zA-Z^OIl]{86,88}" + description: + Ed25519 signature of the ``vote`` object. + vote: + type: "object" + additionalProperties: false + description: | + Vote details to be signed. + required: + - invalid_reason + - is_block_valid + - previous_block + - voting_for_block + - timestamp + properties: + invalid_reason: + anyOf: + - type: "string" + description: | + Reason the block is voted invalid, or ``null``. + - type: "null" + is_block_valid: + type: "boolean" + description: | + This field is ``true`` if the block was deemed valid by the node. + previous_block: + "$ref": "#/definitions/sha3_hexdigest" + description: | + Sha3 identifier of the block that preceeds the block being voted on. + The notion of a "previous" block is subject to vote. + voting_for_block: + "$ref": "#/definitions/sha3_hexdigest" + description: | + Sha3 identifier of the block being voted on. + timestamp: + type: "string" + pattern: "[0-9]{10}" + description: | + Unix timestamp that the vote was created by the node, according + to the system time of the node. +definitions: + sha3_hexdigest: + pattern: "[0-9a-f]{64}" + type: string