Problem: There is no way to vote for an election

Solution: add `approve` command
This commit is contained in:
codegeschrei 2018-08-13 17:17:43 +02:00
parent 3092beb995
commit a535daf955
3 changed files with 85 additions and 1 deletions

View File

@ -14,7 +14,8 @@ from bigchaindb.common.exceptions import (DatabaseAlreadyExists,
DatabaseDoesNotExist, DatabaseDoesNotExist,
OperationError) OperationError)
import bigchaindb import bigchaindb
from bigchaindb import backend, ValidatorElection, BigchainDB from bigchaindb import (backend, ValidatorElection,
BigchainDB, ValidatorElectionVote)
from bigchaindb.backend import schema from bigchaindb.backend import schema
from bigchaindb.backend import query from bigchaindb.backend import query
from bigchaindb.backend.query import PRE_COMMIT_ID 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') 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(): def _run_init():
bdb = bigchaindb.BigchainDB() bdb = bigchaindb.BigchainDB()
@ -260,6 +286,14 @@ def create_parser():
dest='sk', dest='sk',
help='Path to the private key of the election initiator.') 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 # parsers for showing/exporting config values
subparsers.add_parser('show-config', subparsers.add_parser('show-config',
help='Show the current configuration') help='Show the current configuration')

View File

@ -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. 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 <election_id> --private-key PATH_TO_YOUR_PRIVATE_KEY
```
Here, `<election_id>` 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.

View File

@ -409,3 +409,39 @@ def test_upsert_validator_new_without_tendermint(b, priv_validator_path, user_sk
resp = run_upsert_validator_new(args, b) resp = run_upsert_validator_new(args, b)
assert b.get_transaction(resp) 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.'