From bd88aca3471b8fef113309d66cee6c8e2e550f12 Mon Sep 17 00:00:00 2001 From: Vanshdeep Singh Date: Thu, 26 Jul 2018 11:54:11 +0200 Subject: [PATCH] Problem: Inconsitant error handling Solution: All errors should generate exceptions extended from ValidationError class --- bigchaindb/common/exceptions.py | 12 ++++++++++++ bigchaindb/upsert_validator/validator_election.py | 15 +++++++++++---- tests/upsert_validator/test_validator_election.py | 13 +++++++++---- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/bigchaindb/common/exceptions.py b/bigchaindb/common/exceptions.py index 2e5d1b8c..0c145d13 100644 --- a/bigchaindb/common/exceptions.py +++ b/bigchaindb/common/exceptions.py @@ -106,3 +106,15 @@ class MultipleValidatorOperationError(ValidationError): class MultipleInputsError(ValidationError): """Raised if there were multiple inputs when only one was expected""" + + +class InvalidProposer(ValidationError): + """Raised if the public key is not a part of the validator set""" + + +class UnequalValidatorSet(ValidationError): + """Raised if the validator sets differ""" + + +class InvalidPowerChange(ValidationError): + """Raised if proposed power change in validator set is >=1/3 total power""" diff --git a/bigchaindb/upsert_validator/validator_election.py b/bigchaindb/upsert_validator/validator_election.py index 3bc77b5f..3c909b06 100644 --- a/bigchaindb/upsert_validator/validator_election.py +++ b/bigchaindb/upsert_validator/validator_election.py @@ -1,4 +1,8 @@ -from bigchaindb.common.exceptions import (InvalidSignature, MultipleInputsError, +from bigchaindb.common.exceptions import (InvalidSignature, + MultipleInputsError, + InvalidProposer, + UnequalValidatorSet, + InvalidPowerChange, DuplicateTransaction) from bigchaindb.tendermint_utils import key_from_base64 from bigchaindb.common.crypto import (public_key_from_ed25519_key) @@ -104,15 +108,18 @@ class ValidatorElection(Transaction): # NOTE: change more than 1/3 of the current power is not allowed if self.asset['data']['power'] >= (1/3)*sum(current_validators.values()): - raise ValueError('`power` must be less than 1/3 of total power') + raise InvalidPowerChange('`power` change must be less than 1/3 of total power') # NOTE: Check if the proposer is a validator. [election_initiator_node_pub_key] = self.inputs[0].owners_before if election_initiator_node_pub_key not in current_validators.keys(): - return False + raise InvalidProposer('Public key is not a part of the validator set') # NOTE: Check if all validators have been assigned votes equal to their voting power - return self.is_same_topology(current_validators, self.outputs) + if not self.is_same_topology(current_validators, self.outputs): + raise UnequalValidatorSet('Validator set much be exactly same to the outputs of election') + + return True @classmethod def generate(cls, initiator, voters, election_data, metadata=None): diff --git a/tests/upsert_validator/test_validator_election.py b/tests/upsert_validator/test_validator_election.py index 2261c1a6..e4a84868 100644 --- a/tests/upsert_validator/test_validator_election.py +++ b/tests/upsert_validator/test_validator_election.py @@ -1,7 +1,9 @@ import pytest from bigchaindb.upsert_validator import ValidatorElection -from bigchaindb.common.exceptions import DuplicateTransaction +from bigchaindb.common.exceptions import (DuplicateTransaction, + UnequalValidatorSet, + InvalidPowerChange) pytestmark = [pytest.mark.tendermint, pytest.mark.bdb] @@ -21,7 +23,7 @@ def test_upsert_validator_invalid_power_election(b_mock, new_validator, node_key election = ValidatorElection.generate([node_key.public_key], voters, new_validator, None).sign([node_key.private_key]) - with pytest.raises(ValueError): + with pytest.raises(InvalidPowerChange): election.validate(b_mock) @@ -46,7 +48,9 @@ def test_upsert_validator_invalid_election(b_mock, new_validator, node_key): invalid_election = ValidatorElection.generate([node_key.public_key], voters[1:], new_validator, None).sign([node_key.private_key]) - assert not invalid_election.validate(b_mock) + + with pytest.raises(UnequalValidatorSet): + invalid_election.validate(b_mock) recipients = ValidatorElection.recipients(b_mock) altered_recipients = [] @@ -59,4 +63,5 @@ def test_upsert_validator_invalid_election(b_mock, new_validator, node_key): altered_recipients, new_validator, None).sign([node_key.private_key]) - assert not tx_election.validate(b_mock) + with pytest.raises(UnequalValidatorSet): + tx_election.validate(b_mock)