From 96d39778c546cb40b13d797ad8182529c0c0b50c Mon Sep 17 00:00:00 2001 From: z-bowen Date: Mon, 13 Aug 2018 13:57:40 +0200 Subject: [PATCH] Problem: upsert-validator needs a command to 'show' election data Solution: Wrote `upsert-validator show` as specified in BEP-21 --- bigchaindb/commands/bigchaindb.py | 19 ++++++++++++++ tests/commands/test_commands.py | 41 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/bigchaindb/commands/bigchaindb.py b/bigchaindb/commands/bigchaindb.py index 63394df1..9be0b244 100644 --- a/bigchaindb/commands/bigchaindb.py +++ b/bigchaindb/commands/bigchaindb.py @@ -139,6 +139,19 @@ def run_upsert_validator_new(args, bigchain): raise OperationError('Failed to commit election') +def run_upsert_validator_show(args, bigchain): + + election = bigchain.get_transaction(args.election_id) + + new_validator = election.asset['data'] + + public_key = new_validator['public_key'] + power = new_validator['power'] + node_id = new_validator['node_id'] + + return public_key, power, node_id + + def _run_init(): bdb = bigchaindb.BigchainDB() @@ -260,6 +273,12 @@ def create_parser(): dest='sk', help='Path to the private key of the election initiator.') + show_election_parser = validator_subparser.add_parser('show', + help='Provides information about an election.') + + show_election_parser.add_argument('election_id', + help='The transaction id of the election you wish to query.') + # parsers for showing/exporting config values subparsers.add_parser('show-config', help='Show the current configuration') diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 10e41205..8b1957c0 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -409,3 +409,44 @@ def test_upsert_validator_new_without_tendermint(b, priv_validator_path, user_sk resp = run_upsert_validator_new(args, b) assert b.get_transaction(resp) + + +@pytest.mark.tendermint +@pytest.mark.bdb +def test_upsert_validator_show(b, priv_validator_path, user_sk, monkeypatch): + from bigchaindb.commands.bigchaindb import run_upsert_validator_show, run_upsert_validator_new + + 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=' + power = 1 + node_id = '12345' + + 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) + + show_args = Namespace(action='show', + election_id=election_id) + + resp = run_upsert_validator_show(show_args, b) + + assert resp == (public_key, power, node_id)