def test_upsert_validator_delegate_election_vote(b_mock, valid_upsert_validator_election, ed25519_node_keys):
    alice = generate_key_pair()

    b_mock.store_bulk_transactions([valid_upsert_validator_election])

    input0 = valid_upsert_validator_election.to_inputs()[0]
    votes = valid_upsert_validator_election.outputs[0].amount
    public_key0 = input0.owners_before[0]
    key0 = ed25519_node_keys[public_key0]

    delegate_vote = Vote.generate([input0],
                                  [([alice.public_key], 3), ([key0.public_key], votes-3)],
                                  election_id=valid_upsert_validator_election.id)\
        .sign([key0.private_key])

    assert delegate_vote.validate(b_mock)

    b_mock.store_bulk_transactions([delegate_vote])
    election_pub_key = ValidatorElection.to_public_key(valid_upsert_validator_election.id)

    alice_votes = delegate_vote.to_inputs()[0]
    alice_casted_vote = Vote.generate([alice_votes],
                                      [([election_pub_key], 3)],
                                      election_id=valid_upsert_validator_election.id)\
        .sign([alice.private_key])
    assert alice_casted_vote.validate(b_mock)

    key0_votes = delegate_vote.to_inputs()[1]
    key0_casted_vote = Vote.generate([key0_votes],
                                     [([election_pub_key], votes-3)],
                                     election_id=valid_upsert_validator_election.id)\
        .sign([key0.private_key])
    assert key0_casted_vote.validate(b_mock)
def test_upsert_validator_delegate_election_vote(b_mock, valid_election,
                                                 ed25519_node_keys):
    alice = generate_key_pair()

    b_mock.store_bulk_transactions([valid_election])

    input0 = valid_election.to_inputs()[0]
    votes = valid_election.outputs[0].amount
    public_key0 = input0.owners_before[0]
    key0 = ed25519_node_keys[public_key0]

    delegate_vote = Vote.generate([input0],
                                  [([alice.public_key], 3), ([key0.public_key], votes-3)],
                                  election_id=valid_election.id)\
        .sign([key0.private_key])

    assert delegate_vote.validate(b_mock)

    b_mock.store_bulk_transactions([delegate_vote])
    election_pub_key = ValidatorElection.to_public_key(valid_election.id)

    alice_votes = delegate_vote.to_inputs()[0]
    alice_casted_vote = Vote.generate([alice_votes],
                                      [([election_pub_key], 3)],
                                      election_id=valid_election.id)\
        .sign([alice.private_key])
    assert alice_casted_vote.validate(b_mock)

    key0_votes = delegate_vote.to_inputs()[1]
    key0_casted_vote = Vote.generate([key0_votes],
                                     [([election_pub_key], votes-3)],
                                     election_id=valid_election.id)\
        .sign([key0.private_key])
    assert key0_casted_vote.validate(b_mock)
def test_upsert_validator_valid_non_election_vote(b_mock, valid_upsert_validator_election, ed25519_node_keys):
    b_mock.store_bulk_transactions([valid_upsert_validator_election])

    input0 = valid_upsert_validator_election.to_inputs()[0]
    votes = valid_upsert_validator_election.outputs[0].amount
    public_key0 = input0.owners_before[0]
    key0 = ed25519_node_keys[public_key0]

    election_pub_key = ValidatorElection.to_public_key(valid_upsert_validator_election.id)

    # Ensure that threshold conditions are now allowed
    with pytest.raises(ValidationError):
        Vote.generate([input0],
                      [([election_pub_key, key0.public_key], votes)],
                      election_id=valid_upsert_validator_election.id)\
             .sign([key0.private_key])
def gen_vote(election, i, ed25519_node_keys):
    (input_i, votes_i, key_i) = to_inputs(election, i, ed25519_node_keys)
    election_pub_key = ValidatorElection.to_public_key(election.id)
    return Vote.generate([input_i],
                         [([election_pub_key], votes_i)],
                         election_id=election.id)\
        .sign([key_i.private_key])
Пример #5
0
def gen_vote(election, i, ed25519_node_keys):
    (input_i, votes_i, key_i) = to_inputs(election, i, ed25519_node_keys)
    election_pub_key = ValidatorElection.to_public_key(election.id)
    return Vote.generate([input_i],
                         [([election_pub_key], votes_i)],
                         election_id=election.id)\
        .sign([key_i.private_key])
def test_upsert_validator_valid_non_election_vote(b_mock, valid_election,
                                                  ed25519_node_keys):
    b_mock.store_bulk_transactions([valid_election])

    input0 = valid_election.to_inputs()[0]
    votes = valid_election.outputs[0].amount
    public_key0 = input0.owners_before[0]
    key0 = ed25519_node_keys[public_key0]

    election_pub_key = ValidatorElection.to_public_key(valid_election.id)

    # Ensure that threshold conditions are now allowed
    with pytest.raises(ValidationError):
        Vote.generate([input0],
                      [([election_pub_key, key0.public_key], votes)],
                      election_id=valid_election.id)\
             .sign([key0.private_key])
Пример #7
0
def test_valid_election_votes_received(b_mock, valid_upsert_validator_election,
                                       ed25519_node_keys):
    alice = generate_key_pair()
    b_mock.store_bulk_transactions([valid_upsert_validator_election])
    assert valid_upsert_validator_election.get_commited_votes(b_mock) == 0

    input0 = valid_upsert_validator_election.to_inputs()[0]
    votes = valid_upsert_validator_election.outputs[0].amount
    public_key0 = input0.owners_before[0]
    key0 = ed25519_node_keys[public_key0]

    # delegate some votes to alice
    delegate_vote = Vote.generate([input0],
                                  [([alice.public_key], 4), ([key0.public_key], votes-4)],
                                  election_id=valid_upsert_validator_election.id)\
        .sign([key0.private_key])
    b_mock.store_bulk_transactions([delegate_vote])
    assert valid_upsert_validator_election.get_commited_votes(b_mock) == 0

    election_public_key = ValidatorElection.to_public_key(
        valid_upsert_validator_election.id)
    alice_votes = delegate_vote.to_inputs()[0]
    key0_votes = delegate_vote.to_inputs()[1]

    alice_casted_vote = Vote.generate([alice_votes],
                                      [([election_public_key], 2), ([alice.public_key], 2)],
                                      election_id=valid_upsert_validator_election.id)\
        .sign([alice.private_key])

    assert alice_casted_vote.validate(b_mock)
    b_mock.store_bulk_transactions([alice_casted_vote])

    # Check if the delegated vote is count as valid vote
    assert valid_upsert_validator_election.get_commited_votes(b_mock) == 2

    key0_casted_vote = Vote.generate([key0_votes],
                                     [([election_public_key], votes-4)],
                                     election_id=valid_upsert_validator_election.id)\
        .sign([key0.private_key])

    assert key0_casted_vote.validate(b_mock)
    b_mock.store_bulk_transactions([key0_casted_vote])

    assert valid_upsert_validator_election.get_commited_votes(
        b_mock) == votes - 2
def test_valid_election_votes_received(b_mock, valid_upsert_validator_election, ed25519_node_keys):
    alice = generate_key_pair()
    b_mock.store_bulk_transactions([valid_upsert_validator_election])
    assert valid_upsert_validator_election.get_commited_votes(b_mock) == 0

    input0 = valid_upsert_validator_election.to_inputs()[0]
    votes = valid_upsert_validator_election.outputs[0].amount
    public_key0 = input0.owners_before[0]
    key0 = ed25519_node_keys[public_key0]

    # delegate some votes to alice
    delegate_vote = Vote.generate([input0],
                                  [([alice.public_key], 4), ([key0.public_key], votes-4)],
                                  election_id=valid_upsert_validator_election.id)\
        .sign([key0.private_key])
    b_mock.store_bulk_transactions([delegate_vote])
    assert valid_upsert_validator_election.get_commited_votes(b_mock) == 0

    election_public_key = ValidatorElection.to_public_key(valid_upsert_validator_election.id)
    alice_votes = delegate_vote.to_inputs()[0]
    key0_votes = delegate_vote.to_inputs()[1]

    alice_casted_vote = Vote.generate([alice_votes],
                                      [([election_public_key], 2), ([alice.public_key], 2)],
                                      election_id=valid_upsert_validator_election.id)\
        .sign([alice.private_key])

    assert alice_casted_vote.validate(b_mock)
    b_mock.store_bulk_transactions([alice_casted_vote])

    # Check if the delegated vote is count as valid vote
    assert valid_upsert_validator_election.get_commited_votes(b_mock) == 2

    key0_casted_vote = Vote.generate([key0_votes],
                                     [([election_public_key], votes-4)],
                                     election_id=valid_upsert_validator_election.id)\
        .sign([key0.private_key])

    assert key0_casted_vote.validate(b_mock)
    b_mock.store_bulk_transactions([key0_casted_vote])

    assert valid_upsert_validator_election.get_commited_votes(b_mock) == votes - 2
def test_upsert_validator_valid_election_vote(b_mock, valid_upsert_validator_election, ed25519_node_keys):
    b_mock.store_bulk_transactions([valid_upsert_validator_election])

    input0 = valid_upsert_validator_election.to_inputs()[0]
    votes = valid_upsert_validator_election.outputs[0].amount
    public_key0 = input0.owners_before[0]
    key0 = ed25519_node_keys[public_key0]

    election_pub_key = ValidatorElection.to_public_key(valid_upsert_validator_election.id)

    vote = Vote.generate([input0],
                         [([election_pub_key], votes)],
                         election_id=valid_upsert_validator_election.id)\
        .sign([key0.private_key])
    assert vote.validate(b_mock)
def test_upsert_validator_valid_election_vote(b_mock, valid_election,
                                              ed25519_node_keys):
    b_mock.store_bulk_transactions([valid_election])

    input0 = valid_election.to_inputs()[0]
    votes = valid_election.outputs[0].amount
    public_key0 = input0.owners_before[0]
    key0 = ed25519_node_keys[public_key0]

    election_pub_key = ValidatorElection.to_public_key(valid_election.id)

    vote = Vote.generate([input0],
                         [([election_pub_key], votes)],
                         election_id=valid_election.id)\
        .sign([key0.private_key])
    assert vote.validate(b_mock)
Пример #11
0
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
Пример #12
0
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