コード例 #1
0
def test_valid_election_conclude(b_mock, valid_upsert_validator_election, ed25519_node_keys):

    # Node 0: cast vote
    tx_vote0 = gen_vote(valid_upsert_validator_election, 0, ed25519_node_keys)

    # check if the vote is valid even before the election doesn't exist
    with pytest.raises(ValidationError):
        assert tx_vote0.validate(b_mock)

    # store election
    b_mock.store_bulk_transactions([valid_upsert_validator_election])
    # cannot conclude election as not votes exist
    assert not valid_upsert_validator_election.has_concluded(b_mock)

    # validate vote
    assert tx_vote0.validate(b_mock)
    assert not valid_upsert_validator_election.has_concluded(b_mock, [tx_vote0])

    b_mock.store_bulk_transactions([tx_vote0])
    assert not valid_upsert_validator_election.has_concluded(b_mock)

    # Node 1: cast vote
    tx_vote1 = gen_vote(valid_upsert_validator_election, 1, ed25519_node_keys)

    # Node 2: cast vote
    tx_vote2 = gen_vote(valid_upsert_validator_election, 2, ed25519_node_keys)

    # Node 3: cast vote
    tx_vote3 = gen_vote(valid_upsert_validator_election, 3, ed25519_node_keys)

    assert tx_vote1.validate(b_mock)
    assert not valid_upsert_validator_election.has_concluded(b_mock, [tx_vote1])

    # 2/3 is achieved in the same block so the election can be.has_concludedd
    assert valid_upsert_validator_election.has_concluded(b_mock, [tx_vote1, tx_vote2])

    b_mock.store_bulk_transactions([tx_vote1])
    assert not valid_upsert_validator_election.has_concluded(b_mock)

    assert tx_vote2.validate(b_mock)
    assert tx_vote3.validate(b_mock)

    # conclusion can be triggered my different votes in the same block
    assert valid_upsert_validator_election.has_concluded(b_mock, [tx_vote2])
    assert valid_upsert_validator_election.has_concluded(b_mock, [tx_vote2, tx_vote3])

    b_mock.store_bulk_transactions([tx_vote2])

    # Once the blockchain records >2/3 of the votes the election is assumed to be.has_concludedd
    # so any invocation of `.has_concluded` for that election should return False
    assert not valid_upsert_validator_election.has_concluded(b_mock)

    # Vote is still valid but the election cannot be.has_concludedd as it it assmed that it has
    # been.has_concludedd before
    assert tx_vote3.validate(b_mock)
    assert not valid_upsert_validator_election.has_concluded(b_mock, [tx_vote3])
コード例 #2
0
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)
コード例 #3
0
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
コード例 #4
0
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)
コード例 #5
0
def generate_votes(election, voters, keys):
    votes = []
    for voter, _ in enumerate(voters):
        v = gen_vote(election, voter, keys)
        votes.append(v)
    return votes
コード例 #6
0
ファイル: conftest.py プロジェクト: roderik/bigchaindb
def generate_votes(election, voters, keys):
    votes = []
    for voter, _ in enumerate(voters):
        v = gen_vote(election, voter, keys)
        votes.append(v)
    return votes
コード例 #7
0
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
コード例 #8
0
def test_valid_election_conclude(b_mock, valid_upsert_validator_election,
                                 ed25519_node_keys):

    # Node 0: cast vote
    tx_vote0 = gen_vote(valid_upsert_validator_election, 0, ed25519_node_keys)

    # check if the vote is valid even before the election doesn't exist
    with pytest.raises(ValidationError):
        assert tx_vote0.validate(b_mock)

    # store election
    b_mock.store_bulk_transactions([valid_upsert_validator_election])
    # cannot conclude election as not votes exist
    assert not valid_upsert_validator_election.has_concluded(b_mock)

    # validate vote
    assert tx_vote0.validate(b_mock)
    assert not valid_upsert_validator_election.has_concluded(
        b_mock, [tx_vote0])

    b_mock.store_bulk_transactions([tx_vote0])
    assert not valid_upsert_validator_election.has_concluded(b_mock)

    # Node 1: cast vote
    tx_vote1 = gen_vote(valid_upsert_validator_election, 1, ed25519_node_keys)

    # Node 2: cast vote
    tx_vote2 = gen_vote(valid_upsert_validator_election, 2, ed25519_node_keys)

    # Node 3: cast vote
    tx_vote3 = gen_vote(valid_upsert_validator_election, 3, ed25519_node_keys)

    assert tx_vote1.validate(b_mock)
    assert not valid_upsert_validator_election.has_concluded(
        b_mock, [tx_vote1])

    # 2/3 is achieved in the same block so the election can be.has_concludedd
    assert valid_upsert_validator_election.has_concluded(
        b_mock, [tx_vote1, tx_vote2])

    b_mock.store_bulk_transactions([tx_vote1])
    assert not valid_upsert_validator_election.has_concluded(b_mock)

    assert tx_vote2.validate(b_mock)
    assert tx_vote3.validate(b_mock)

    # conclusion can be triggered my different votes in the same block
    assert valid_upsert_validator_election.has_concluded(b_mock, [tx_vote2])
    assert valid_upsert_validator_election.has_concluded(
        b_mock, [tx_vote2, tx_vote3])

    b_mock.store_bulk_transactions([tx_vote2])

    # Once the blockchain records >2/3 of the votes the election is assumed to be.has_concludedd
    # so any invocation of `.has_concluded` for that election should return False
    assert not valid_upsert_validator_election.has_concluded(b_mock)

    # Vote is still valid but the election cannot be.has_concludedd as it it assmed that it has
    # been.has_concludedd before
    assert tx_vote3.validate(b_mock)
    assert not valid_upsert_validator_election.has_concluded(
        b_mock, [tx_vote3])