Problem: Event stream cannot handle election txn

Solution: generalize implemenation to handle election txn type
This commit is contained in:
Vanshdeep Singh 2018-08-27 12:19:16 +02:00
parent dbcc7e538f
commit df6af78a69
4 changed files with 47 additions and 10 deletions

View File

@ -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')

View File

@ -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 []

View File

@ -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']}

View File

@ -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