Problem: Inconsitant error handling

Solution: All errors should generate exceptions extended from ValidationError class
This commit is contained in:
Vanshdeep Singh 2018-07-26 11:54:11 +02:00
parent 67d439cc92
commit bd88aca347
3 changed files with 32 additions and 8 deletions

View File

@ -106,3 +106,15 @@ class MultipleValidatorOperationError(ValidationError):
class MultipleInputsError(ValidationError): class MultipleInputsError(ValidationError):
"""Raised if there were multiple inputs when only one was expected""" """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"""

View File

@ -1,4 +1,8 @@
from bigchaindb.common.exceptions import (InvalidSignature, MultipleInputsError, from bigchaindb.common.exceptions import (InvalidSignature,
MultipleInputsError,
InvalidProposer,
UnequalValidatorSet,
InvalidPowerChange,
DuplicateTransaction) DuplicateTransaction)
from bigchaindb.tendermint_utils import key_from_base64 from bigchaindb.tendermint_utils import key_from_base64
from bigchaindb.common.crypto import (public_key_from_ed25519_key) 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 # NOTE: change more than 1/3 of the current power is not allowed
if self.asset['data']['power'] >= (1/3)*sum(current_validators.values()): 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. # NOTE: Check if the proposer is a validator.
[election_initiator_node_pub_key] = self.inputs[0].owners_before [election_initiator_node_pub_key] = self.inputs[0].owners_before
if election_initiator_node_pub_key not in current_validators.keys(): 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 # 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 @classmethod
def generate(cls, initiator, voters, election_data, metadata=None): def generate(cls, initiator, voters, election_data, metadata=None):

View File

@ -1,7 +1,9 @@
import pytest import pytest
from bigchaindb.upsert_validator import ValidatorElection 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] 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], election = ValidatorElection.generate([node_key.public_key],
voters, voters,
new_validator, None).sign([node_key.private_key]) new_validator, None).sign([node_key.private_key])
with pytest.raises(ValueError): with pytest.raises(InvalidPowerChange):
election.validate(b_mock) 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], invalid_election = ValidatorElection.generate([node_key.public_key],
voters[1:], voters[1:],
new_validator, None).sign([node_key.private_key]) 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) recipients = ValidatorElection.recipients(b_mock)
altered_recipients = [] altered_recipients = []
@ -59,4 +63,5 @@ def test_upsert_validator_invalid_election(b_mock, new_validator, node_key):
altered_recipients, altered_recipients,
new_validator, None).sign([node_key.private_key]) new_validator, None).sign([node_key.private_key])
assert not tx_election.validate(b_mock) with pytest.raises(UnequalValidatorSet):
tx_election.validate(b_mock)