diff --git a/bigchaindb/commands/bigchaindb.py b/bigchaindb/commands/bigchaindb.py index effc29dc..8e88412a 100644 --- a/bigchaindb/commands/bigchaindb.py +++ b/bigchaindb/commands/bigchaindb.py @@ -152,15 +152,21 @@ def run_upsert_validator_approve(args, bigchain): :return: a success message :raises: OperationError if the write transaction fails for any reason """ + key = load_node_key(args.sk) tx = bigchain.get_transaction(args.election_id) - voting_power = ValidatorElection.current_validators(bigchain)[key.public_key] - approval_tx = ValidatorElectionVote.generate(tx.to_inputs(), [ + voting_power = ValidatorElection.current_validators(bigchain)[ + key.public_key] + + approval = ValidatorElectionVote.generate(tx.to_inputs(), [ ([key.public_key], voting_power)], tx.id).sign([key.private_key]) - approval_tx.validate(bigchain) - resp = bigchain.write_transaction(approval_tx, 'broadcast_tx_commit') + approval.validate(bigchain) + + resp = bigchain.write_transaction(approval, 'broadcast_tx_commit') + if resp == (202, ''): - return 'Your vote has been submitted.' + print('Your vote has been submitted.') + return approval.id else: raise OperationError('Failed to vote for election') diff --git a/bigchaindb/upsert_validator/validator_election.py b/bigchaindb/upsert_validator/validator_election.py index 4717e708..bdd5a22f 100644 --- a/bigchaindb/upsert_validator/validator_election.py +++ b/bigchaindb/upsert_validator/validator_election.py @@ -32,11 +32,10 @@ class ValidatorElection(Transaction): """Return a dictionary of validators with key as `public_key` and value as the `voting_power` """ - validators = {} for validator in bigchain.get_validators(): # NOTE: we assume that Tendermint encodes public key in base64 - public_key = public_key_from_ed25519_key(key_from_base64(validator['pub_key']['value'])) + public_key = public_key_from_ed25519_key(key_from_base64(validator['pub_key']['data'])) validators[public_key] = validator['voting_power'] return validators @@ -114,7 +113,7 @@ class ValidatorElection(Transaction): 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 + return self @classmethod def generate(cls, initiator, voters, election_data, metadata=None): diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 0579c8e8..743924be 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -411,37 +411,26 @@ def test_upsert_validator_new_without_tendermint(b, priv_validator_path, user_sk assert b.get_transaction(resp) -@pytest.mark.tendermint @pytest.mark.abci @pytest.mark.bdb -def test_upsert_validator_approve(b, priv_validator_path, user_sk, monkeypatch): +def test_upsert_validator_approve(b, priv_validator_path, user_sk, validators): from bigchaindb.commands.bigchaindb import run_upsert_validator_new, \ run_upsert_validator_approve - def mock_get(): - return [ - {'pub_key': {'value': 'zL/DasvKulXZzhSNFwx4cLRXKkSM9GPK7Y0nZ4FEylM=', - 'type': 'tendermint/PubKeyEd25519'}, - 'voting_power': 10} - ] - - def mock_write(tx, mode): - b.store_transaction(tx) - return 202, '' - b.get_validators = mock_get - b.write_transaction = mock_write - monkeypatch.setattr('requests.get', mock_get) - public_key = 'CJxdItf4lz2PwEf4SmYNAu/c/VpmX39JEgC5YpH7fxg=' + public_key = '8eJ8q9ZQpReWyQT5aFCiwtZ5wDZC4eDnCen88p3tQ6ie' new_args = Namespace(action='new', public_key=public_key, power=1, node_id='12345', sk=priv_validator_path, config={}) + election_id = run_upsert_validator_new(new_args, b) + args = Namespace(action='approve', election_id=election_id, sk=priv_validator_path, config={}) approve = run_upsert_validator_approve(args, b) - assert approve == 'Your vote has been submitted.' + + assert b.get_transaction(approve) diff --git a/tests/conftest.py b/tests/conftest.py index e08a3886..8b7c61d6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -661,7 +661,7 @@ def ed25519_node_keys(node_keys): return node_keys_dict -@pytest.fixture(scope='session') +@pytest.fixture def node_keys(): return {'zL/DasvKulXZzhSNFwx4cLRXKkSM9GPK7Y0nZ4FEylM=': 'cM5oW4J0zmUSZ/+QRoRlincvgCwR0pEjFoY//ZnnjD3Mv8Nqy8q6VdnOFI0XDHhwtFcqRIz0Y8rtjSdngUTKUw==', @@ -673,7 +673,12 @@ def node_keys(): 'uz8bYgoL4rHErWT1gjjrnA+W7bgD/uDQWSRKDmC8otc95wnnxJo1GxYlmh0OaqOkJaobpu13BcUcvITjRFiVgw=='} -@pytest.fixture(scope='session') +@pytest.fixture +def private_validator_keys(node_keys): + return list(node_keys.items())[0] + + +@pytest.fixture def priv_validator_path(node_keys): (public_key, private_key) = list(node_keys.items())[0] priv_validator = { @@ -695,3 +700,21 @@ def priv_validator_path(node_keys): json.dump(priv_validator, socket) socket.close() return path + + +@pytest.fixture +def validators(b, node_keys): + from bigchaindb.backend import query + + (public_key, private_key) = list(node_keys.items())[0] + + validator_set = [{'address': 'F5426F0980E36E03044F74DD414248D29ABCBDB2', + 'pub_key': { + 'data': public_key, + 'type': 'ed25519'}, + 'voting_power': 10}] + + validator_update = {'validators': validator_set, + 'height': 4266642} + + query.store_validator_set(b.connection, validator_update)