From 84a0d3f19729349f3a824ebdab6a15173568d6db Mon Sep 17 00:00:00 2001 From: Vanshdeep Singh Date: Mon, 13 Aug 2018 12:24:35 +0200 Subject: [PATCH] Problem: new_validator_set function not covered in tests Solution: Add test to assert that the validator set is correctly updated --- bigchaindb/core.py | 8 +++++--- tests/tendermint/test_core.py | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/bigchaindb/core.py b/bigchaindb/core.py index 08d3888a..bdc852cf 100644 --- a/bigchaindb/core.py +++ b/bigchaindb/core.py @@ -199,8 +199,10 @@ def new_validator_set(bigchain, height, updates): updates_dict = {} for u in updates: - updates_dict[u['public_key']] = {'pub_key': {'type': 'ed25519', - 'data': public_key_to_base64(u['public_key'])}, - 'voting_power': u['power']} + public_key64 = public_key_to_base64(u['public_key']) + updates_dict[public_key64] = {'pub_key': {'type': 'ed25519', + 'data': public_key64}, + 'voting_power': u['power']} + new_validators_dict = {**validators_dict, **updates_dict} return list(new_validators_dict.values()) diff --git a/tests/tendermint/test_core.py b/tests/tendermint/test_core.py index f1a3d92d..5909f003 100644 --- a/tests/tendermint/test_core.py +++ b/tests/tendermint/test_core.py @@ -6,7 +6,10 @@ from abci.types_pb2 import ( RequestEndBlock ) -from bigchaindb.core import CodeTypeOk, CodeTypeError +from bigchaindb.core import (CodeTypeOk, + CodeTypeError, + new_validator_set) +from bigchaindb.tendermint_utils import public_key_to_base64 pytestmark = [pytest.mark.tendermint, pytest.mark.bdb] @@ -216,3 +219,26 @@ def test_store_pre_commit_state_in_end_block(b, alice, init_chain_request): assert resp['commit_id'] == PRE_COMMIT_ID assert resp['height'] == 100 assert resp['transactions'] == [tx.id] + + +def test_new_validator_set(b): + node1 = {'pub_key': {'type': 'ed25519', + 'data': 'FxjS2/8AFYoIUqF6AcePTc87qOT7e4WGgH+sGCpTUDQ='}, + 'voting_power': 10} + node1_new_power = {'public_key': '1718D2DBFF00158A0852A17A01C78F4DCF3BA8E4FB7B8586807FAC182A535034', + 'power': 20} + node2 = {'public_key': '1888A353B181715CA2554701D06C1665BC42C5D936C55EA9C5DBCBDB8B3F02A3', + 'power': 10} + + validators = [node1] + updates = [node1_new_power, node2] + b.store_validator_set(1, validators) + updated_validator_set = new_validator_set(b, 1, updates) + + updated_validators = [] + for u in updates: + updated_validators.append({'pub_key': {'type': 'ed25519', + 'data': public_key_to_base64(u['public_key'])}, + 'voting_power': u['power']}) + + assert updated_validator_set == updated_validators