Skip to content

Commit

Permalink
Problem: Users want to know upsert-validator election status.
Browse files Browse the repository at this point in the history
Solution: Introduce the `upsert-validator show` command. Soon to be re-implemented via storing and querying identifiers of concluded elections.
  • Loading branch information
z-bowen authored and ldmberman committed Aug 30, 2018
1 parent 3cf368a commit 7a0b474
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Dockerfile-dev
Expand Up @@ -33,4 +33,4 @@ RUN mkdir -p /usr/src/app
COPY . /usr/src/app/
WORKDIR /usr/src/app
RUN pip install --no-cache-dir --process-dependency-links -e .[dev]
RUN bigchaindb -y configure
RUN bigchaindb -y configure
38 changes: 36 additions & 2 deletions bigchaindb/commands/bigchaindb.py
Expand Up @@ -27,8 +27,7 @@
from bigchaindb.commands.utils import (configure_bigchaindb,
input_on_stderr)
from bigchaindb.log import setup_logging
from bigchaindb.tendermint_utils import public_key_from_base64

from bigchaindb.tendermint_utils import public_key_from_base64, public_key_to_base64

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -191,6 +190,35 @@ def run_upsert_validator_approve(args, bigchain):
return False


def run_upsert_validator_show(args, bigchain):
"""Retrieves information about an upsert-validator election
:param args: dict
args = {
'election_id': the transaction_id for an election (str)
}
:param bigchain: an instance of BigchainDB
"""

election = bigchain.get_transaction(args.election_id)
if not election:
logger.error(f'No election found with election_id {args.election_id}')
return

new_validator = election.asset['data']

public_key = public_key_to_base64(new_validator['public_key'])
power = new_validator['power']
node_id = new_validator['node_id']
status = election.get_status(bigchain)

response = f'public_key={public_key}\npower={power}\nnode_id={node_id}\nstatus={status}'

logger.info(response)

return response


def _run_init():
bdb = bigchaindb.BigchainDB()

Expand Down Expand Up @@ -320,6 +348,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')
Expand Down
57 changes: 56 additions & 1 deletion bigchaindb/upsert_validator/validator_election.py
Expand Up @@ -5,6 +5,7 @@
import base58

from bigchaindb import backend
from bigchaindb.backend.localmongodb.query import get_asset_tokens_for_public_key
from bigchaindb.common.exceptions import (InvalidSignature,
MultipleInputsError,
InvalidProposer,
Expand All @@ -29,6 +30,11 @@ class ValidatorElection(Transaction):
# by renaming CREATE to VALIDATOR_ELECTION
CREATE = VALIDATOR_ELECTION
ALLOWED_OPERATIONS = (VALIDATOR_ELECTION,)
# Election Statuses:
ONGOING = 'ongoing'
CONCLUDED = 'concluded'
INCONCLUSIVE = 'inconclusive'
ELECTION_THRESHOLD = 2 / 3

def __init__(self, operation, asset, inputs, outputs,
metadata=None, version=None, hash_id=None):
Expand Down Expand Up @@ -218,9 +224,58 @@ def get_validator_update(cls, bigchain, new_height, txns):
validator_updates = [election.asset['data']]
curr_validator_set = bigchain.get_validators(new_height)
updated_validator_set = new_validator_set(curr_validator_set,
new_height, validator_updates)
validator_updates)

updated_validator_set = [v for v in updated_validator_set if v['voting_power'] > 0]
bigchain.store_validator_set(new_height+1, updated_validator_set)
return [encode_validator(election.asset['data'])]
return []

def _vote_ratio(self, bigchain, height):
cast_votes = self._get_vote_ids(bigchain)
votes = [(tx['outputs'][0]['amount'], bigchain.get_block_containing_tx(tx['id'])[0]) for tx in cast_votes]
votes_cast = [int(vote[0]) for vote in votes if vote[1] <= height]
total_votes_cast = sum(votes_cast)
total_votes = sum([voter.amount for voter in self.outputs])
vote_ratio = total_votes_cast/total_votes
return vote_ratio

def _get_vote_ids(self, bigchain):
election_key = self.to_public_key(self.id)
votes = get_asset_tokens_for_public_key(bigchain.connection, self.id, election_key)
return votes

def initial_height(self, bigchain):
heights = bigchain.get_block_containing_tx(self.id)
initial_height = 0
if len(heights) != 0:
initial_height = min(bigchain.get_block_containing_tx(self.id))
return initial_height

def get_status(self, bigchain, height=None):

initial_validators = self.get_validators(bigchain, height=self.initial_height(bigchain))

# get all heights where a vote was cast
vote_heights = set([bigchain.get_block_containing_tx(tx['id'])[0] for tx in self._get_vote_ids(bigchain)])

# find the least height where the vote succeeds
confirmation_height = None
confirmed_heights = [h for h in vote_heights if self._vote_ratio(bigchain, h) > self.ELECTION_THRESHOLD]
if height:
confirmed_heights = [h for h in confirmed_heights if h <= height]
if len(confirmed_heights) > 0:
confirmation_height = min(confirmed_heights)

# get the validator set at the confirmation height/current height
if confirmation_height:
final_validators = self.get_validators(bigchain, height=confirmation_height)
else:
final_validators = self.get_validators(bigchain)

if initial_validators != final_validators:
return self.INCONCLUSIVE
elif confirmation_height:
return self.CONCLUDED
else:
return self.ONGOING
2 changes: 1 addition & 1 deletion bigchaindb/upsert_validator/validator_utils.py
Expand Up @@ -21,7 +21,7 @@ def decode_validator(v):
'voting_power': v.power}


def new_validator_set(validators, height, updates):
def new_validator_set(validators, updates):
validators_dict = {}
for v in validators:
validators_dict[v['pub_key']['data']] = v
Expand Down
16 changes: 16 additions & 0 deletions docs/server/source/server-reference/bigchaindb-cli.md
Expand Up @@ -139,3 +139,19 @@ $ bigchaindb upsert-validator approve 04a067582cf03eba2b53b82e4adb5ece424474cbd4
```

If the command succeeds a message will be returned stating that the vote was submitted successfully. Once a proposal has been approved by sufficent validators (more than `2/3` of the total voting power) then the proposed change is applied to the network. For example, consider a network wherein the total power is `90` then the proposed changed applied only after `60` (`2/3 * 90`) have been received.

#### upsert-validator show

Retrieves information about an election initiated by `upsert-validator new`.

Below is the command line syntax and the return value,

```bash
$ bigchaindb upsert-validator show ELECTION_ID
public_key=<e_pub_key>
power=<e_power>
node_id=<e_node_id>
status=<status>
```

The `public_key`, `power`, and `node_id` are the same values used in the `upsert-validator new` command that originally triggered the election. `status` takes three possible values, `ongoing`, if the election has not yet reached a 2/3 majority, `concluded`, if the election reached the 2/3 majority needed to pass, or `inconclusive`, if the validator set changed while the election was in process, rendering it undecidable.
42 changes: 14 additions & 28 deletions tests/commands/test_commands.py
Expand Up @@ -347,34 +347,20 @@ def json(self):
return {'result': {'latest_block_height': self.height}}


# @pytest.mark.execute
# @patch('bigchaindb.lib.BigchainDB.get_validators')
# @pytest.mark.abci
@pytest.mark.skip
def test_upsert_validator_new_with_tendermint(b, priv_validator_path, user_sk, monkeypatch):
"""WIP: Will be fixed and activated in the next PR
"""
@pytest.mark.abci
def test_upsert_validator_new_with_tendermint(b, priv_validator_path, user_sk, validators):
from bigchaindb.commands.bigchaindb import run_upsert_validator_new
import time

time.sleep(3)

# b.get_validators = mock_get
# mock_get_validators = mock_get
# monkeypatch.setattr('requests.get', mock_get)

proposer_key = b.get_validators()[0]['pub_key']['value']
new_args = Namespace(action='new',
public_key='8eJ8q9ZQpReWyQT5aFCiwtZ5wDZC4eDnCen88p3tQ6ie',
power=1,
node_id='unique_node_id_for_test_upsert_validator_new_with_tendermint',
sk=priv_validator_path,
config={})

args = Namespace(action='new',
public_key=proposer_key,
power=1,
node_id='12345',
sk=priv_validator_path,
config={})
resp = run_upsert_validator_new(args, b)
time.sleep(3)
election_id = run_upsert_validator_new(new_args, b)

assert b.get_transaction(resp)
assert b.get_transaction(election_id)


@pytest.mark.tendermint
Expand All @@ -386,7 +372,7 @@ def mock_write(tx, mode):
b.store_bulk_transactions([tx])
return (202, '')

b.get_validators = mock_get
b.get_validators = mock_get_validators
b.write_transaction = mock_write

args = Namespace(action='new',
Expand Down Expand Up @@ -430,7 +416,7 @@ def mock_write(tx, mode):
return (400, '')

b.write_transaction = mock_write
b.get_validators = mock_get
b.get_validators = mock_get_validators
args = Namespace(action='new',
public_key='CJxdItf4lz2PwEf4SmYNAu/c/VpmX39JEgC5YpH7fxg=',
power=10,
Expand Down Expand Up @@ -533,7 +519,7 @@ def test_upsert_validator_approve_called_with_bad_key(caplog, b, bad_validator_p
'the eligible voters in this election.'


def mock_get(height):
def mock_get_validators(height):
keys = node_keys()
pub_key = list(keys.keys())[0]
return [
Expand All @@ -550,7 +536,7 @@ def mock_write(tx, mode):
return (202, '')

# patch the validator set. We now have one validator with power 10
b.get_validators = mock_get
b.get_validators = mock_get_validators
b.write_transaction = mock_write

# our voters is a list of length 1, populated from our mocked validator
Expand Down
6 changes: 6 additions & 0 deletions tests/commands/test_utils.py
Expand Up @@ -72,6 +72,12 @@ def test_configure(args):
logging.CRITICAL)
)))
def test_configure_bigchaindb_logging(log_level):
# TODO: See following comment:
# This is a dirty test. If a test *preceding* this test makes use of the logger, and then another test *after* this
# test also makes use of the logger, somehow we get logger.disabled == True, and the later test fails. We need to
# either engineer this somehow to leave the test env in the same state as it finds it, or make an assessment
# whether or not we even need this test, and potentially just remove it.

from bigchaindb.commands.utils import configure_bigchaindb

@configure_bigchaindb
Expand Down
2 changes: 1 addition & 1 deletion tests/tendermint/test_core.py
Expand Up @@ -238,7 +238,7 @@ def test_new_validator_set(b):
validators = [node1]
updates = [node1_new_power, node2]
b.store_validator_set(1, validators)
updated_validator_set = new_validator_set(b.get_validators(1), 1, updates)
updated_validator_set = new_validator_set(b.get_validators(1), updates)

updated_validators = []
for u in updates:
Expand Down
50 changes: 50 additions & 0 deletions tests/upsert_validator/conftest.py
Expand Up @@ -4,6 +4,9 @@

import pytest

from bigchaindb import ValidatorElectionVote
from bigchaindb.backend.localmongodb import query
from bigchaindb.lib import Block
from bigchaindb.upsert_validator import ValidatorElection


Expand Down Expand Up @@ -41,3 +44,50 @@ def valid_election_b(b, node_key, new_validator):
return ValidatorElection.generate([node_key.public_key],
voters,
new_validator, None).sign([node_key.private_key])


@pytest.fixture
def ongoing_election(b, valid_election, ed25519_node_keys):
b.store_bulk_transactions([valid_election])
block_1 = Block(app_hash='hash_1', height=1, transactions=[valid_election.id])
vote_0 = vote(valid_election, 0, ed25519_node_keys, b)
vote_1 = vote(valid_election, 1, ed25519_node_keys, b)
block_2 = Block(app_hash='hash_2', height=2, transactions=[vote_0.id, vote_1.id])
b.store_block(block_1._asdict())
b.store_block(block_2._asdict())
return valid_election


@pytest.fixture
def concluded_election(b, ongoing_election, ed25519_node_keys):
vote_2 = vote(ongoing_election, 2, ed25519_node_keys, b)
block_4 = Block(app_hash='hash_4', height=4, transactions=[vote_2.id])
b.store_block(block_4._asdict())
return ongoing_election


@pytest.fixture
def inconclusive_election(b, concluded_election, new_validator):
validators = b.get_validators(height=1)
validators[0]['voting_power'] = 15
validator_update = {'validators': validators,
'height': 3}

query.store_validator_set(b.connection, validator_update)
return concluded_election


def vote(election, voter, keys, b):
election_input = election.to_inputs()[voter]
votes = election.outputs[voter].amount
public_key = election_input.owners_before[0]
key = keys[public_key]

election_pub_key = ValidatorElection.to_public_key(election.id)

v = ValidatorElectionVote.generate([election_input],
[([election_pub_key], votes)],
election_id=election.id)\
.sign([key.private_key])
b.store_bulk_transactions([v])
return v

0 comments on commit 7a0b474

Please sign in to comment.