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. """ self.abort_if_abci_chain_is_not_synced() chain_shift = 0 if self.chain is None else self.chain['height'] height = request_end_block.height + chain_shift 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_update = ValidatorElection.approved_update(self.bigchaindb, self.new_height, self.block_transactions) update = [validator_update] if validator_update else [] # 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=update)
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. """ self.abort_if_abci_chain_is_not_synced() chain_shift = 0 if self.chain is None else self.chain['height'] height = request_end_block.height + chain_shift 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_update = ValidatorElection.approved_update(self.bigchaindb, self.new_height, self.block_transactions) update = [validator_update] if validator_update else [] # 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=update)
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