diff --git a/bigchaindb/consensus.py b/bigchaindb/consensus.py index bfb054af..10024d76 100644 --- a/bigchaindb/consensus.py +++ b/bigchaindb/consensus.py @@ -1,4 +1,5 @@ from bigchaindb.util import verify_vote_signature +from bigchaindb.common.schema import validate_vote_schema, SchemaValidationError class BaseConsensusRules(): @@ -19,10 +20,16 @@ class BaseConsensusRules(): return block.validate(bigchain) @staticmethod - def verify_vote_signature(voters, signed_vote): + def verify_vote(voters, signed_vote): """Verify the signature of a vote. Refer to the documentation of :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) diff --git a/bigchaindb/core.py b/bigchaindb/core.py index 34dbab84..b59f4ebd 100644 --- a/bigchaindb/core.py +++ b/bigchaindb/core.py @@ -648,7 +648,7 @@ class Bigchain(object): prev_block = [vote['vote']['previous_block'] for vote in votes] # vote_validity checks whether a vote is valid # 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 # vote_cast = [True, True, False] and diff --git a/tests/pipelines/test_vote.py b/tests/pipelines/test_vote.py index edbc5f4c..227a23a2 100644 --- a/tests/pipelines/test_vote.py +++ b/tests/pipelines/test_vote.py @@ -17,6 +17,9 @@ def dummy_block(b): return block +DUMMY_SHA3 = '0123456789abcdef' * 4 + + def test_vote_creation_valid(b): from bigchaindb.common import crypto from bigchaindb.common.util import serialize @@ -24,11 +27,11 @@ def test_vote_creation_valid(b): # create valid block block = dummy_block(b) # retrieve vote - vote = b.vote(block.id, 'abc', True) + vote = b.vote(block.id, DUMMY_SHA3, True) # assert vote is correct 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']['invalid_reason'] is None assert vote['node_pubkey'] == b.me @@ -44,11 +47,11 @@ def test_vote_creation_invalid(b): # create valid block block = dummy_block(b) # retrieve vote - vote = b.vote(block.id, 'abc', False) + vote = b.vote(block.id, DUMMY_SHA3, False) # assert vote is correct 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']['invalid_reason'] is None assert vote['node_pubkey'] == b.me