vote schema

This commit is contained in:
Scott Sadler 2016-11-24 13:54:09 +01:00
parent efbc1e1b9a
commit 0d8a53cb68
2 changed files with 86 additions and 8 deletions

View File

@ -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)

View File

@ -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