Ejemplo n.º 1
0
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')
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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])
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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])