def run_upsert_validator_new(args, bigchain): """Initiates an election to add/update/remove a validator to an existing BigchainDB network :param args: dict args = { 'public_key': the public key of the proposed peer, (str) 'power': the proposed validator power for the new peer, (str) 'node_id': the node_id of the new peer (str) 'sk': the path to the private key of the node calling the election (str) } :param bigchain: an instance of BigchainDB :return: election_id (tx_id) :raises: OperationError if the write transaction fails for any reason """ new_validator = { 'public_key': args.public_key, 'power': args.power, 'node_id': args.node_id } 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) resp = bigchain.write_transaction(election, 'broadcast_tx_commit') if resp == (202, ''): return election.id else: raise OperationError('Failed to commit election')
def run_upsert_validator_new(args, bigchain): """Initiates an election to add/update/remove a validator to an existing BigchainDB network :param args: dict args = { 'public_key': the public key of the proposed peer, (str) 'power': the proposed validator power for the new peer, (str) 'node_id': the node_id of the new peer (str) 'sk': the path to the private key of the node calling the election (str) } :param bigchain: an instance of BigchainDB :return: election_id or `False` in case of failure """ new_validator = { 'public_key': { 'value': public_key_from_base64(args.public_key), 'type': 'ed25519-base16' }, 'power': args.power, 'node_id': args.node_id } 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 resp = bigchain.write_transaction(election, 'broadcast_tx_commit') if resp == (202, ''): logger.info('[SUCCESS] Submitted proposal with id: {}'.format( election.id)) return election.id else: logger.error('Failed to commit election proposal') return False
def run_election_new_upsert_validator(args, bigchain): """Initiates an election to add/update/remove a validator to an existing BigchainDB network :param args: dict args = { 'public_key': the public key of the proposed peer, (str) 'power': the proposed validator power for the new peer, (str) 'node_id': the node_id of the new peer (str) 'sk': the path to the private key of the node calling the election (str) } :param bigchain: an instance of BigchainDB :return: election_id or `False` in case of failure """ new_validator = { 'public_key': {'value': public_key_from_base64(args.public_key), 'type': 'ed25519-base16'}, 'power': args.power, 'node_id': args.node_id } 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 resp = bigchain.write_transaction(election, 'broadcast_tx_commit') if resp == (202, ''): logger.info('[SUCCESS] Submitted proposal with id: {}'.format(election.id)) return election.id else: logger.error('Failed to commit election proposal') return False
def call_election(b, new_validator, node_key): def mock_write(tx, mode): b.store_bulk_transactions([tx]) return (202, '') # patch the validator set. We now have one validator with power 10 b.get_validators = mock_get_validators b.write_transaction = mock_write # our voters is a list of length 1, populated from our mocked validator voters = ValidatorElection.recipients(b) # and our voter is the public key from the voter list voter = node_key.public_key valid_election = ValidatorElection.generate( [voter], voters, new_validator, None).sign([node_key.private_key]) # patch in an election with a vote issued to the user election_id = valid_election.id b.store_bulk_transactions([valid_election]) return b, election_id
def call_election(b, new_validator, node_key): def mock_write(tx, mode): b.store_bulk_transactions([tx]) return (202, '') # patch the validator set. We now have one validator with power 10 b.get_validators = mock_get_validators b.write_transaction = mock_write # our voters is a list of length 1, populated from our mocked validator voters = ValidatorElection.recipients(b) # and our voter is the public key from the voter list voter = node_key.public_key valid_election = ValidatorElection.generate([voter], voters, new_validator, None).sign([node_key.private_key]) # patch in an election with a vote issued to the user election_id = valid_election.id b.store_bulk_transactions([valid_election]) return b, election_id
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: success log message or `False` in case of error """ key = load_node_key(args.sk) tx = bigchain.get_transaction(args.election_id) voting_powers = [ v.amount for v in tx.outputs if key.public_key in v.public_keys ] if len(voting_powers) > 0: voting_power = voting_powers[0] else: logger.error( 'The key you provided does not match any of the eligible voters in this election.' ) return False inputs = [i for i in tx.to_inputs() if key.public_key in i.owners_before] election_pub_key = ValidatorElection.to_public_key(tx.id) approval = ValidatorElectionVote.generate( inputs, [([election_pub_key], voting_power)], tx.id).sign([key.private_key]) approval.validate(bigchain) resp = bigchain.write_transaction(approval, 'broadcast_tx_commit') if resp == (202, ''): logger.info('[SUCCESS] Your vote has been submitted') return approval.id else: logger.error('Failed to commit vote') return False
def run_election_approve(args, bigchain): """Approve an election :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: success log message or `False` in case of error """ key = load_node_key(args.sk) tx = bigchain.get_transaction(args.election_id) voting_powers = [v.amount for v in tx.outputs if key.public_key in v.public_keys] if len(voting_powers) > 0: voting_power = voting_powers[0] else: logger.error('The key you provided does not match any of the eligible voters in this election.') return False inputs = [i for i in tx.to_inputs() if key.public_key in i.owners_before] election_pub_key = ValidatorElection.to_public_key(tx.id) approval = Vote.generate(inputs, [([election_pub_key], voting_power)], tx.id).sign([key.private_key]) approval.validate(bigchain) resp = bigchain.write_transaction(approval, 'broadcast_tx_commit') if resp == (202, ''): logger.info('[SUCCESS] Your vote has been submitted') return approval.id else: logger.error('Failed to commit vote') return False
def validator_election_votes_2(b_mock, ongoing_validator_election_2, ed25519_node_keys): voters = ValidatorElection.recipients(b_mock) votes = generate_votes(ongoing_validator_election_2, voters, ed25519_node_keys) return votes
def valid_upsert_validator_election_2(b_mock, node_key, new_validator): voters = ValidatorElection.recipients(b_mock) return ValidatorElection.generate([node_key.public_key], voters, new_validator, None).sign([node_key.private_key])