From a535daf955fc65c27ecef083e9ef37d8c7c93e0a Mon Sep 17 00:00:00 2001 From: codegeschrei Date: Mon, 13 Aug 2018 17:17:43 +0200 Subject: [PATCH] Problem: There is no way to vote for an election Solution: add `approve` command --- bigchaindb/commands/bigchaindb.py | 36 ++++++++++++++++++- .../source/server-reference/bigchaindb-cli.md | 14 ++++++++ tests/commands/test_commands.py | 36 +++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/bigchaindb/commands/bigchaindb.py b/bigchaindb/commands/bigchaindb.py index 63394df1..effc29dc 100644 --- a/bigchaindb/commands/bigchaindb.py +++ b/bigchaindb/commands/bigchaindb.py @@ -14,7 +14,8 @@ from bigchaindb.common.exceptions import (DatabaseAlreadyExists, DatabaseDoesNotExist, OperationError) import bigchaindb -from bigchaindb import backend, ValidatorElection, BigchainDB +from bigchaindb import (backend, ValidatorElection, + BigchainDB, ValidatorElectionVote) from bigchaindb.backend import schema from bigchaindb.backend import query from bigchaindb.backend.query import PRE_COMMIT_ID @@ -139,6 +140,31 @@ def run_upsert_validator_new(args, bigchain): raise OperationError('Failed to commit election') +def run_upsert_validator_approve(args, bigchain): + """Approve an election to add/update/remove a validator to an + existing BigchainDB network + :param args: dict + args = { + 'election_id': the election_id of the election (str) + 'sk': the path to the private key of the signer (str) + } + :param bigchain: an instance of BigchainDB + :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(), [ + ([key.public_key], voting_power)], tx.id).sign([key.private_key]) + approval_tx.validate(bigchain) + resp = bigchain.write_transaction(approval_tx, 'broadcast_tx_commit') + if resp == (202, ''): + return 'Your vote has been submitted.' + else: + raise OperationError('Failed to vote for election') + + def _run_init(): bdb = bigchaindb.BigchainDB() @@ -260,6 +286,14 @@ def create_parser(): dest='sk', help='Path to the private key of the election initiator.') + approve_election_parser = validator_subparser.add_parser('approve', + help='Approve the election.') + approve_election_parser.add_argument('election_id', + help='The election_id of the election.') + approve_election_parser.add_argument('--private-key', + dest='sk', + help='Path to the private key of the election initiator.') + # parsers for showing/exporting config values subparsers.add_parser('show-config', help='Show the current configuration') diff --git a/docs/server/source/server-reference/bigchaindb-cli.md b/docs/server/source/server-reference/bigchaindb-cli.md index 439e7160..cec7d4a8 100644 --- a/docs/server/source/server-reference/bigchaindb-cli.md +++ b/docs/server/source/server-reference/bigchaindb-cli.md @@ -104,3 +104,17 @@ $ bigchaindb upsert-validator new B0E42D2589A455EAD339A035D6CE1C8C3E25863F268120 ``` If the command succeeds, it will create an election and return an `election_id`. Elections consist of one vote token per voting power, issued to the members of the validator set. Validators can cast their votes to approve the change to the validator set by spending their vote tokens. The status of the election can be monitored by providing the `election_id` to the `show` subcommand. + +#### upsert-validator approve + Approve an election by voting for it. + Below is the command line syntax and the return value, + ```bash +$ bigchaindb upsert-validator approve --private-key PATH_TO_YOUR_PRIVATE_KEY +``` + Here, `` is the transaction id of the election the approval should be given for. `--private-key` should be +the path to wherever the private key for your validator node is stored, (*not* the private key itself). + Example usage, + ```bash +$ bigchaindb upsert-validator approve 04a067582cf03eba2b53b82e4adb5ece424474cbd4f7183780855a93ac5e3caa --private-key /home/user/.tendermint/config/priv_validator.json +``` + If the command succeeds, a message will be returned, that the vote was submitted successfully. diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 10e41205..0579c8e8 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -409,3 +409,39 @@ 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.abci +@pytest.mark.bdb +def test_upsert_validator_approve(b, priv_validator_path, user_sk, monkeypatch): + 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=' + 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.'