From df6af78a69ead4fc89a442eb2ff6d8d99830ee4e Mon Sep 17 00:00:00 2001 From: Vanshdeep Singh Date: Mon, 27 Aug 2018 12:19:16 +0200 Subject: [PATCH] Problem: Event stream cannot handle election txn Solution: generalize implemenation to handle election txn type --- bigchaindb/commands/bigchaindb.py | 25 +++++++++++------ .../upsert_validator/validator_election.py | 1 + bigchaindb/web/websocket_server.py | 3 +- .../test_validator_election_vote.py | 28 ++++++++++++++++++- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/bigchaindb/commands/bigchaindb.py b/bigchaindb/commands/bigchaindb.py index b0f419f3..8feabef6 100644 --- a/bigchaindb/commands/bigchaindb.py +++ b/bigchaindb/commands/bigchaindb.py @@ -16,7 +16,9 @@ import sys from bigchaindb.utils import load_node_key from bigchaindb.common.exceptions import (DatabaseAlreadyExists, DatabaseDoesNotExist, - OperationError, KeypairMismatchException) + ValidationError, + OperationError, + KeypairMismatchException) import bigchaindb from bigchaindb import (backend, ValidatorElection, BigchainDB, ValidatorElectionVote) @@ -129,16 +131,23 @@ def run_upsert_validator_new(args, bigchain): 'node_id': args.node_id } - key = load_node_key(args.sk) + try: + key = load_node_key(args.sk) + voters = ValidatorElection.recipients(bigchain) + election = ValidatorElection.generate([key.public_key], + voters, + new_validator, None).sign([key.private_key]) + election.validate(bigchain) + except ValidationError as e: + logger.error(e) + return False + except FileNotFoundError as fd_404: + logger.error(fd_404) + return False - voters = ValidatorElection.recipients(bigchain) - - election = ValidatorElection.generate([key.public_key], - voters, - new_validator, None).sign([key.private_key]) - election.validate(bigchain) resp = bigchain.write_transaction(election, 'broadcast_tx_commit') if resp == (202, ''): + print('[SUCCESS] Submitted proposal with id:', election.id) return election.id else: raise OperationError('Failed to commit election') diff --git a/bigchaindb/upsert_validator/validator_election.py b/bigchaindb/upsert_validator/validator_election.py index 17ea298f..f007c38b 100644 --- a/bigchaindb/upsert_validator/validator_election.py +++ b/bigchaindb/upsert_validator/validator_election.py @@ -220,6 +220,7 @@ class ValidatorElection(Transaction): updated_validator_set = new_validator_set(curr_validator_set, new_height, 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 [] diff --git a/bigchaindb/web/websocket_server.py b/bigchaindb/web/websocket_server.py index 8aea0e20..53cdd27f 100644 --- a/bigchaindb/web/websocket_server.py +++ b/bigchaindb/web/websocket_server.py @@ -102,7 +102,8 @@ class Dispatcher: block = event.data for tx in block['transactions']: - asset_id = tx['id'] if tx['operation'] == 'CREATE' else tx['asset']['id'] + asset_id = tx['asset'].get('id', None) + asset_id = tx['id'] if asset_id is None else asset_id data = {'height': block['height'], 'asset_id': asset_id, 'transaction_id': tx['id']} diff --git a/tests/upsert_validator/test_validator_election_vote.py b/tests/upsert_validator/test_validator_election_vote.py index d555b0d7..19eacbfa 100644 --- a/tests/upsert_validator/test_validator_election_vote.py +++ b/tests/upsert_validator/test_validator_election_vote.py @@ -310,11 +310,37 @@ def test_get_validator_update(b, node_keys, node_key, ed25519_node_keys): b.store_bulk_transactions([tx_vote0, tx_vote1]) update = ValidatorElection.get_validator_update(b, 4, [tx_vote2]) - print('update', update) update_public_key = codecs.encode(update[0].pub_key.data, 'base64').decode().rstrip('\n') assert len(update) == 1 assert update_public_key == public_key64 + # remove validator + power = 0 + new_validator = {'public_key': public_key, + 'node_id': 'some_node_id', + 'power': power} + voters = ValidatorElection.recipients(b) + election = ValidatorElection.generate([node_key.public_key], + voters, + new_validator).sign([node_key.private_key]) + # store election + b.store_bulk_transactions([election]) + + tx_vote0 = gen_vote(election, 0, ed25519_node_keys) + tx_vote1 = gen_vote(election, 1, ed25519_node_keys) + tx_vote2 = gen_vote(election, 2, ed25519_node_keys) + + b.store_bulk_transactions([tx_vote0, tx_vote1]) + + update = ValidatorElection.get_validator_update(b, 9, [tx_vote2]) + update_public_key = codecs.encode(update[0].pub_key.data, 'base64').decode().rstrip('\n') + assert len(update) == 1 + assert update_public_key == public_key64 + + # assert that the public key is not a part of the current validator set + for v in b.get_validators(10): + assert not v['pub_key']['data'] == public_key64 + # ============================================================================ # Helper functions