Problem: No tests for invalid proposer or multiple inputs

Solution: Add tests which raise InvalidPropser exception if the public key not a
part of the validator set and raises MultipleInputsError if more than 1
validators `owners_before` in the election.
This commit is contained in:
Vanshdeep Singh 2018-07-26 13:37:44 +02:00
parent bd88aca347
commit c6d7667a80
2 changed files with 32 additions and 6 deletions

View File

@ -73,6 +73,8 @@ class ValidatorElection(Transaction):
def validate(self, bigchain, current_transactions=[]): def validate(self, bigchain, current_transactions=[]):
"""Validate election transaction """Validate election transaction
For more details refer BEP-21: https://github.com/bigchaindb/BEPs/tree/master/21
NOTE: NOTE:
* A valid election is initiated by an existing validator. * A valid election is initiated by an existing validator.
@ -80,15 +82,13 @@ class ValidatorElection(Transaction):
alloacted according to the voting power of each validator node. alloacted according to the voting power of each validator node.
Args: Args:
bigchain (BigchainDB): an instantiated bigchaindb.tendermint.BigchainDB object. bigchain (BigchainDB): an instantiated bigchaindb.lib.BigchainDB object.
Returns: Returns:
The transaction (Transaction) if the transaction is valid else it `True` if the election is valid
raises an exception describing the reason why the transaction is
invalid.
Raises: Raises:
ValidationError: If the transaction is invalid ValidationError: If the election is invalid
""" """
input_conditions = [] input_conditions = []
@ -103,7 +103,7 @@ class ValidatorElection(Transaction):
current_validators = self.current_validators(bigchain) current_validators = self.current_validators(bigchain)
# NOTE: Proposer should be a single node # NOTE: Proposer should be a single node
if len(self.inputs) != 1: if len(self.inputs) != 1 or len(self.inputs[0].owners_before) != 1:
raise MultipleInputsError('`tx_signers` must be a list instance of length one') raise MultipleInputsError('`tx_signers` must be a list instance of length one')
# 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

View File

@ -3,6 +3,8 @@ 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, UnequalValidatorSet,
InvalidProposer,
MultipleInputsError,
InvalidPowerChange) InvalidPowerChange)
pytestmark = [pytest.mark.tendermint, pytest.mark.bdb] pytestmark = [pytest.mark.tendermint, pytest.mark.bdb]
@ -27,6 +29,30 @@ def test_upsert_validator_invalid_power_election(b_mock, new_validator, node_key
election.validate(b_mock) election.validate(b_mock)
def test_upsert_validator_invalid_proposed_election(b_mock, new_validator, node_key):
from bigchaindb.common.crypto import generate_key_pair
alice = generate_key_pair()
voters = ValidatorElection.recipients(b_mock)
election = ValidatorElection.generate([alice.public_key],
voters,
new_validator, None).sign([alice.private_key])
with pytest.raises(InvalidProposer):
election.validate(b_mock)
def test_upsert_validator_invalid_inputs_election(b_mock, new_validator, node_key):
from bigchaindb.common.crypto import generate_key_pair
alice = generate_key_pair()
voters = ValidatorElection.recipients(b_mock)
election = ValidatorElection.generate([node_key.public_key, alice.public_key],
voters,
new_validator, None).sign([node_key.private_key, alice.private_key])
with pytest.raises(MultipleInputsError):
election.validate(b_mock)
def test_upsert_validator_invalid_election(b_mock, new_validator, node_key): def test_upsert_validator_invalid_election(b_mock, new_validator, node_key):
voters = ValidatorElection.recipients(b_mock) voters = ValidatorElection.recipients(b_mock)
valid_election = ValidatorElection.generate([node_key.public_key], valid_election = ValidatorElection.generate([node_key.public_key],