add call to vote schema validate in consensus.py

This commit is contained in:
Scott Sadler 2016-11-24 16:57:21 +01:00
parent 855dc7a5e8
commit c43bf10151
3 changed files with 17 additions and 7 deletions

View File

@ -1,4 +1,5 @@
from bigchaindb.util import verify_vote_signature from bigchaindb.util import verify_vote_signature
from bigchaindb.common.schema import validate_vote_schema, SchemaValidationError
class BaseConsensusRules(): class BaseConsensusRules():
@ -19,10 +20,16 @@ class BaseConsensusRules():
return block.validate(bigchain) return block.validate(bigchain)
@staticmethod @staticmethod
def verify_vote_signature(voters, signed_vote): def verify_vote(voters, signed_vote):
"""Verify the signature of a vote. """Verify the signature of a vote.
Refer to the documentation of Refer to the documentation of
:func:`bigchaindb.util.verify_signature`. :func:`bigchaindb.util.verify_signature`.
""" """
return verify_vote_signature(voters, signed_vote) try:
validate_vote_schema(signed_vote)
except SchemaValidationError:
# TODO: log this.
return False
else:
return verify_vote_signature(voters, signed_vote)

View File

@ -648,7 +648,7 @@ class Bigchain(object):
prev_block = [vote['vote']['previous_block'] for vote in votes] prev_block = [vote['vote']['previous_block'] for vote in votes]
# vote_validity checks whether a vote is valid # vote_validity checks whether a vote is valid
# or invalid, e.g. [False, True, True] # or invalid, e.g. [False, True, True]
vote_validity = [self.consensus.verify_vote_signature(voters, vote) for vote in votes] vote_validity = [self.consensus.verify_vote(voters, vote) for vote in votes]
# element-wise product of stated vote and validity of vote # element-wise product of stated vote and validity of vote
# vote_cast = [True, True, False] and # vote_cast = [True, True, False] and

View File

@ -17,6 +17,9 @@ def dummy_block(b):
return block return block
DUMMY_SHA3 = '0123456789abcdef' * 4
def test_vote_creation_valid(b): def test_vote_creation_valid(b):
from bigchaindb.common import crypto from bigchaindb.common import crypto
from bigchaindb.common.util import serialize from bigchaindb.common.util import serialize
@ -24,11 +27,11 @@ def test_vote_creation_valid(b):
# create valid block # create valid block
block = dummy_block(b) block = dummy_block(b)
# retrieve vote # retrieve vote
vote = b.vote(block.id, 'abc', True) vote = b.vote(block.id, DUMMY_SHA3, True)
# assert vote is correct # assert vote is correct
assert vote['vote']['voting_for_block'] == block.id assert vote['vote']['voting_for_block'] == block.id
assert vote['vote']['previous_block'] == 'abc' assert vote['vote']['previous_block'] == DUMMY_SHA3
assert vote['vote']['is_block_valid'] is True assert vote['vote']['is_block_valid'] is True
assert vote['vote']['invalid_reason'] is None assert vote['vote']['invalid_reason'] is None
assert vote['node_pubkey'] == b.me assert vote['node_pubkey'] == b.me
@ -44,11 +47,11 @@ def test_vote_creation_invalid(b):
# create valid block # create valid block
block = dummy_block(b) block = dummy_block(b)
# retrieve vote # retrieve vote
vote = b.vote(block.id, 'abc', False) vote = b.vote(block.id, DUMMY_SHA3, False)
# assert vote is correct # assert vote is correct
assert vote['vote']['voting_for_block'] == block.id assert vote['vote']['voting_for_block'] == block.id
assert vote['vote']['previous_block'] == 'abc' assert vote['vote']['previous_block'] == DUMMY_SHA3
assert vote['vote']['is_block_valid'] is False assert vote['vote']['is_block_valid'] is False
assert vote['vote']['invalid_reason'] is None assert vote['vote']['invalid_reason'] is None
assert vote['node_pubkey'] == b.me assert vote['node_pubkey'] == b.me