def test_valid_election_conclude(b_mock, valid_election, ed25519_node_keys):

    # Node 0: cast vote
    tx_vote0 = gen_vote(valid_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_election])
    # cannot conclude election as not votes exist
    assert not ValidatorElection.has_concluded(b_mock, valid_election.id)

    # validate vote
    assert tx_vote0.validate(b_mock)
    assert not ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote0])

    b_mock.store_bulk_transactions([tx_vote0])
    assert not ValidatorElection.has_concluded(b_mock, valid_election.id)

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

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

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

    assert tx_vote1.validate(b_mock)
    assert not ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote1])

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

    b_mock.store_bulk_transactions([tx_vote1])
    assert not ValidatorElection.has_concluded(b_mock, valid_election.id)

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

    # conclusion can be triggered my different votes in the same block
    assert ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote2])
    assert ValidatorElection.has_concluded(b_mock, valid_election.id, [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 ValidatorElection.has_concluded(b_mock, valid_election.id)

    # 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 ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote3])
def test_valid_election_conclude(b_mock, valid_election, ed25519_node_keys):

    # Node 0: cast vote
    tx_vote0 = gen_vote(valid_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_election])
    # cannot conclude election as not votes exist
    assert not ValidatorElection.has_concluded(b_mock, valid_election.id)

    # validate vote
    assert tx_vote0.validate(b_mock)
    assert not ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote0])

    b_mock.store_bulk_transactions([tx_vote0])
    assert not ValidatorElection.has_concluded(b_mock, valid_election.id)

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

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

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

    assert tx_vote1.validate(b_mock)
    assert not ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote1])

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

    b_mock.store_bulk_transactions([tx_vote1])
    assert not ValidatorElection.has_concluded(b_mock, valid_election.id)

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

    # conclusion can be triggered my different votes in the same block
    assert ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote2])
    assert ValidatorElection.has_concluded(b_mock, valid_election.id, [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 ValidatorElection.has_concluded(b_mock, valid_election.id)

    # 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 ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote3])
Esempio n. 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': 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 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
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