def test_new_validator_set(b): node1 = { 'pub_key': { 'type': 'ed25519', 'data': 'FxjS2/8AFYoIUqF6AcePTc87qOT7e4WGgH+sGCpTUDQ=' }, 'voting_power': 10 } node1_new_power = { 'public_key': '1718D2DBFF00158A0852A17A01C78F4DCF3BA8E4FB7B8586807FAC182A535034', 'power': 20 } node2 = { 'public_key': '1888A353B181715CA2554701D06C1665BC42C5D936C55EA9C5DBCBDB8B3F02A3', 'power': 10 } validators = [node1] updates = [node1_new_power, node2] b.store_validator_set(1, validators, 'election_id') updated_validator_set = new_validator_set(b.get_validators(1), updates) updated_validators = [] for u in updates: updated_validators.append({ 'pub_key': { 'type': 'ed25519', 'data': public_key_to_base64(u['public_key']) }, 'voting_power': u['power'] }) assert updated_validator_set == updated_validators
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']['value']) 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 test_upsert_validator(b, node_key, node_keys, ed25519_node_keys): import time import requests if b.get_latest_block()['height'] == 0: generate_block(b) (node_pub, _) = list(node_keys.items())[0] validators = [{ 'pub_key': { 'type': 'ed25519', 'data': node_pub }, 'voting_power': 10 }] latest_block = b.get_latest_block() # reset the validator set b.store_validator_set(latest_block['height'], validators, 'previous_election_id') power = 1 public_key = '9B3119650DF82B9A5D8A12E38953EA47475C09F0C48A4E6A0ECE182944B24403' public_key64 = public_key_to_base64(public_key) 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, None).sign([node_key.private_key]) code, message = b.write_transaction(election, 'broadcast_tx_commit') assert code == 202 time.sleep(3) assert b.get_transaction(election.id) tx_vote = gen_vote(election, 0, ed25519_node_keys) assert tx_vote.validate(b) code, message = b.write_transaction(tx_vote, 'broadcast_tx_commit') assert code == 202 time.sleep(3) resp = requests.get(b.endpoint + 'validators') validator_pub_keys = [] for v in resp.json()['result']['validators']: validator_pub_keys.append(v['pub_key']['value']) assert (public_key64 in validator_pub_keys) new_validator_set = b.get_validators() validator_pub_keys = [] for v in new_validator_set: validator_pub_keys.append(v['pub_key']['data']) assert (public_key64 in validator_pub_keys)
def test_public_key_encoding_decoding(): from bigchaindb.tendermint_utils import (public_key_from_base64, public_key_to_base64) public_key = public_key_from_base64(SAMPLE_PUBLIC_KEY['pub_key']['value']) base64_public_key = public_key_to_base64(public_key) assert base64_public_key == SAMPLE_PUBLIC_KEY['pub_key']['value']
def test_get_validator_update(b, node_keys, node_key, ed25519_node_keys): reset_validator_set(b, node_keys, 1) power = 1 public_key = '9B3119650DF82B9A5D8A12E38953EA47475C09F0C48A4E6A0ECE182944B24403' public_key64 = public_key_to_base64(public_key) new_validator = {'public_key': {'value': public_key, 'type': 'ed25519-base16'}, '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) assert not election.has_concluded(b, [tx_vote0]) assert not election.has_concluded(b, [tx_vote0, tx_vote1]) assert election.has_concluded(b, [tx_vote0, tx_vote1, tx_vote2]) assert Election.process_block(b, 4, [tx_vote0]) == [] assert Election.process_block(b, 4, [tx_vote0, tx_vote1]) == [] update = Election.process_block(b, 4, [tx_vote0, tx_vote1, tx_vote2]) assert len(update) == 1 update_public_key = codecs.encode(update[0].pub_key.data, 'base64').decode().rstrip('\n') assert update_public_key == public_key64 # remove validator power = 0 new_validator = {'public_key': {'value': public_key, 'type': 'ed25519-base16'}, '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 = Election.process_block(b, 9, [tx_vote2]) assert len(update) == 1 update_public_key = codecs.encode(update[0].pub_key.data, 'base64').decode().rstrip('\n') 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['public_key']['value'] == public_key64
def test_upsert_validator(b, node_key, node_keys, ed25519_node_keys): if b.get_latest_block()['height'] == 0: generate_block(b) (node_pub, _) = list(node_keys.items())[0] validators = [{ 'public_key': { 'type': 'ed25519-base64', 'value': node_pub }, 'voting_power': 10 }] latest_block = b.get_latest_block() # reset the validator set b.store_validator_set(latest_block['height'], validators) generate_block(b) power = 1 public_key = '9B3119650DF82B9A5D8A12E38953EA47475C09F0C48A4E6A0ECE182944B24403' public_key64 = public_key_to_base64(public_key) new_validator = { 'public_key': { 'value': public_key, 'type': 'ed25519-base16' }, 'node_id': 'some_node_id', 'power': power } voters = ValidatorElection.recipients(b) election = ValidatorElection.generate([node_key.public_key], voters, new_validator, None).sign([node_key.private_key]) code, message = b.write_transaction(election, BROADCAST_TX_COMMIT) assert code == 202 assert b.get_transaction(election.id) tx_vote = gen_vote(election, 0, ed25519_node_keys) assert tx_vote.validate(b) code, message = b.write_transaction(tx_vote, BROADCAST_TX_COMMIT) assert code == 202 resp = b.get_validators() validator_pub_keys = [] for v in resp: validator_pub_keys.append(v['public_key']['value']) assert (public_key64 in validator_pub_keys) new_validator_set = b.get_validators() validator_pub_keys = [] for v in new_validator_set: validator_pub_keys.append(v['public_key']['value']) assert (public_key64 in validator_pub_keys)
def show_election(self, bigchain): data = self.asset['data'] if 'public_key' in data.keys(): data['public_key'] = public_key_to_base64(data['public_key']['value']) response = '' for k, v in data.items(): if k != 'seed': response += f'{k}={v}\n' response += f'status={self.get_status(bigchain)}' return response
def show_election(self, bigchain): data = self.asset['data'] if 'public_key' in data.keys(): data['public_key'] = public_key_to_base64( data['public_key']['value']) response = '' for k, v in data.items(): if k != 'seed': response += f'{k}={v}\n' response += f'status={self.get_status(bigchain)}' return response
def test_upsert_validator(b, node_key, node_keys, ed25519_node_keys): import time import requests if b.get_latest_block()['height'] == 0: generate_block(b) (node_pub, _) = list(node_keys.items())[0] validators = [{'public_key': {'type': 'ed25519-base64', 'value': node_pub}, 'voting_power': 10}] latest_block = b.get_latest_block() # reset the validator set b.store_validator_set(latest_block['height'], validators) power = 1 public_key = '9B3119650DF82B9A5D8A12E38953EA47475C09F0C48A4E6A0ECE182944B24403' public_key64 = public_key_to_base64(public_key) new_validator = {'public_key': {'value': public_key, 'type': 'ed25519-base16'}, 'node_id': 'some_node_id', 'power': power} voters = ValidatorElection.recipients(b) election = ValidatorElection.generate([node_key.public_key], voters, new_validator, None).sign([node_key.private_key]) code, message = b.write_transaction(election, 'broadcast_tx_commit') assert code == 202 time.sleep(3) assert b.get_transaction(election.id) tx_vote = gen_vote(election, 0, ed25519_node_keys) assert tx_vote.validate(b) code, message = b.write_transaction(tx_vote, 'broadcast_tx_commit') assert code == 202 time.sleep(3) resp = requests.get(b.endpoint + 'validators') validator_pub_keys = [] for v in resp.json()['result']['validators']: validator_pub_keys.append(v['pub_key']['value']) assert (public_key64 in validator_pub_keys) new_validator_set = b.get_validators() validator_pub_keys = [] for v in new_validator_set: validator_pub_keys.append(v['public_key']['value']) assert (public_key64 in validator_pub_keys)
def test_upsert_validator_show(caplog, ongoing_validator_election, b): from bigchaindb.commands.bigchaindb import run_election_show election_id = ongoing_validator_election.id public_key = public_key_to_base64( ongoing_validator_election.asset['data']['public_key']['value']) power = ongoing_validator_election.asset['data']['power'] node_id = ongoing_validator_election.asset['data']['node_id'] status = ValidatorElection.ONGOING show_args = Namespace(action='show', election_id=election_id) msg = run_election_show(show_args, b) assert msg == f'public_key={public_key}\npower={power}\nnode_id={node_id}\nstatus={status}'
def test_upsert_validator_show(caplog, ongoing_election, b): from bigchaindb.commands.bigchaindb import run_election_show election_id = ongoing_election.id public_key = public_key_to_base64(ongoing_election.asset['data']['public_key']['value']) power = ongoing_election.asset['data']['power'] node_id = ongoing_election.asset['data']['node_id'] status = ValidatorElection.ONGOING show_args = Namespace(action='show', election_id=election_id) msg = run_election_show(show_args, b) assert msg == f'public_key={public_key}\npower={power}\nnode_id={node_id}\nstatus={status}'
def test_get_validator_update(b, node_keys, node_key, ed25519_node_keys): reset_validator_set(b, node_keys, 1) power = 1 public_key = '9B3119650DF82B9A5D8A12E38953EA47475C09F0C48A4E6A0ECE182944B24403' public_key64 = public_key_to_base64(public_key) 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) assert not ValidatorElection.has_concluded(b, election.id, [tx_vote0]) assert not ValidatorElection.has_concluded(b, election.id, [tx_vote0, tx_vote1]) assert ValidatorElection.has_concluded(b, election.id, [tx_vote0, tx_vote1, tx_vote2]) assert ValidatorElection.get_validator_update(b, 4, [tx_vote0]) == [] assert ValidatorElection.get_validator_update(b, 4, [tx_vote0, tx_vote1]) == [] update = ValidatorElection.get_validator_update( b, 4, [tx_vote0, tx_vote1, 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 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
def new_validator_set(validators, height, updates): validators_dict = {} for v in validators: validators_dict[v['pub_key']['data']] = v updates_dict = {} for u in updates: public_key64 = public_key_to_base64(u['public_key']) updates_dict[public_key64] = { 'pub_key': { 'type': 'ed25519', 'data': public_key64 }, 'voting_power': u['power'] } new_validators_dict = {**validators_dict, **updates_dict} return list(new_validators_dict.values())
def test_upsert_validator(b, alice): from bigchaindb.backend.query import VALIDATOR_UPDATE_ID from bigchaindb.backend import query, connect from bigchaindb.models import Transaction from bigchaindb.tendermint_utils import public_key_to_base64 import time conn = connect() power = 1 public_key = '9B3119650DF82B9A5D8A12E38953EA47475C09F0C48A4E6A0ECE182944B24403' validator = { 'pub_key': { 'type': 'AC26791624DE60', 'data': public_key }, 'power': power } validator_update = { 'validator': validator, 'update_id': VALIDATOR_UPDATE_ID } query.store_validator_update(conn, deepcopy(validator_update)) tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], asset=None)\ .sign([alice.private_key]) code, message = b.write_transaction(tx, 'broadcast_tx_commit') assert code == 202 time.sleep(5) validators = b.get_validators() validators = [(v['pub_key']['value'], v['voting_power']) for v in validators] public_key64 = public_key_to_base64(public_key) assert ((public_key64, str(power)) in validators)
def test_new_validator_set(b): node1 = {'public_key': {'type': 'ed25519-base64', 'value': 'FxjS2/8AFYoIUqF6AcePTc87qOT7e4WGgH+sGCpTUDQ='}, 'voting_power': 10} node1_new_power = {'public_key': {'value': '1718D2DBFF00158A0852A17A01C78F4DCF3BA8E4FB7B8586807FAC182A535034', 'type': 'ed25519-base16'}, 'power': 20} node2 = {'public_key': {'value': '1888A353B181715CA2554701D06C1665BC42C5D936C55EA9C5DBCBDB8B3F02A3', 'type': 'ed25519-base16'}, 'power': 10} validators = [node1] updates = [node1_new_power, node2] b.store_validator_set(1, validators) updated_validator_set = new_validator_set(b.get_validators(1), updates) updated_validators = [] for u in updates: updated_validators.append({'public_key': {'type': 'ed25519-base64', 'value': public_key_to_base64(u['public_key']['value'])}, 'voting_power': u['power']}) assert updated_validator_set == updated_validators
def test_get_validator_update(b, node_keys, node_key, ed25519_node_keys): reset_validator_set(b, node_keys, 1) power = 1 public_key = '9B3119650DF82B9A5D8A12E38953EA47475C09F0C48A4E6A0ECE182944B24403' public_key64 = public_key_to_base64(public_key) new_validator = { 'public_key': { 'value': public_key, 'type': 'ed25519-base16' }, '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) assert not ValidatorElection.has_concluded(b, election.id, [tx_vote0]) assert not ValidatorElection.has_concluded(b, election.id, [tx_vote0, tx_vote1]) assert ValidatorElection.has_concluded(b, election.id, [tx_vote0, tx_vote1, tx_vote2]) assert not ValidatorElection.approved_update(b, 4, [tx_vote0]) assert not ValidatorElection.approved_update(b, 4, [tx_vote0, tx_vote1]) update = ValidatorElection.approved_update(b, 4, [tx_vote0, tx_vote1, tx_vote2]) assert update update_public_key = codecs.encode(update.pub_key.data, 'base64').decode().rstrip('\n') assert update_public_key == public_key64 b.store_bulk_transactions([tx_vote0, tx_vote1]) update = ValidatorElection.approved_update(b, 4, [tx_vote2]) assert update update_public_key = codecs.encode(update.pub_key.data, 'base64').decode().rstrip('\n') assert update_public_key == public_key64 # remove validator power = 0 new_validator = { 'public_key': { 'value': public_key, 'type': 'ed25519-base16' }, '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.approved_update(b, 9, [tx_vote2]) if update: update_public_key = codecs.encode(update.pub_key.data, 'base64').decode().rstrip('\n') assert update 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['public_key']['value'] == public_key64