예제 #1
0
파일: core.py 프로젝트: shahbazn/bigchaindb
    def end_block(self, request_end_block):
        """Calculate block hash using transaction ids and previous block
        hash to be stored in the next block.

        Args:
            height (int): new height of the chain.
        """

        height = request_end_block.height
        self.new_height = height
        block_txn_hash = calculate_hash(self.block_txn_ids)
        block = self.bigchaindb.get_latest_block()

        if self.block_txn_ids:
            self.block_txn_hash = calculate_hash([block['app_hash'], block_txn_hash])
        else:
            self.block_txn_hash = block['app_hash']

        # Check if the current block concluded any validator elections and
        # update the locally tracked validator set
        validator_updates = ValidatorElection.get_validator_update(self.bigchaindb,
                                                                   self.new_height,
                                                                   self.block_transactions)

        # Store pre-commit state to recover in case there is a crash
        # during `commit`
        pre_commit_state = PreCommitState(commit_id=PRE_COMMIT_ID,
                                          height=self.new_height,
                                          transactions=self.block_txn_ids)
        logger.debug('Updating PreCommitState: %s', self.new_height)
        self.bigchaindb.store_pre_commit_state(pre_commit_state._asdict())
        return ResponseEndBlock(validator_updates=validator_updates)
예제 #2
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
예제 #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 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])
    update_public_key = codecs.encode(update[0].pub_key.data,
                                      'base64').decode().rstrip('\n')
    assert len(update) == 1
    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.get_validator_update(b, 9, [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

    # 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