def finalize_block(self, block: BaseBlock) -> BaseBlock: """ Perform any finalization steps like awarding the block mining reward. """ block_reward = self.get_block_reward() + (len(block.uncles) * self.get_nephew_reward()) self.state.account_db.delta_balance(block.header.coinbase, block_reward) self.logger.debug( "BLOCK REWARD: %s -> %s", block_reward, block.header.coinbase, ) for uncle in block.uncles: uncle_reward = self.get_uncle_reward(block.number, uncle) self.state.account_db.delta_balance(uncle.coinbase, uncle_reward) self.logger.debug( "UNCLE REWARD REWARD: %s -> %s", uncle_reward, uncle.coinbase, ) # We need to call `persist` here since the state db batches # all writes until we tell it to write to the underlying db # TODO: Refactor to only use batching/journaling for tx processing self.state.account_db.persist() return block.copy(header=block.header.copy( state_root=self.state.state_root))
def finalize_block(self, block: BaseBlock) -> BaseBlock: """ Perform any finalization steps like awarding the block mining reward, and persisting the final state root. """ if block.number > 0: self._assign_block_rewards(block) # We need to call `persist` here since the state db batches # all writes until we tell it to write to the underlying db self.state.persist() return block.copy(header=block.header.copy( state_root=self.state.state_root))
def set_block_transactions(self, base_block: BaseBlock, new_header: BlockHeader, transactions: Tuple[BaseTransaction, ...], receipts: Tuple[Receipt, ...]) -> BaseBlock: tx_root_hash, tx_kv_nodes = make_trie_root_and_nodes(transactions) self.chaindb.persist_trie_data_dict(tx_kv_nodes) receipt_root_hash, receipt_kv_nodes = make_trie_root_and_nodes(receipts) self.chaindb.persist_trie_data_dict(receipt_kv_nodes) return base_block.copy( transactions=transactions, header=new_header.copy( transaction_root=tx_root_hash, receipt_root=receipt_root_hash, ), )
def pack_block(self, block: BaseBlock, *args: Any, **kwargs: Any) -> BaseBlock: """ Pack block for mining. :param bytes coinbase: 20-byte public address to receive block reward :param bytes uncles_hash: 32 bytes :param bytes state_root: 32 bytes :param bytes transaction_root: 32 bytes :param bytes receipt_root: 32 bytes :param int bloom: :param int gas_used: :param bytes extra_data: 32 bytes :param bytes mix_hash: 32 bytes :param bytes nonce: 8 bytes """ if 'uncles' in kwargs: uncles = kwargs.pop('uncles') kwargs.setdefault('uncles_hash', keccak(rlp.encode(uncles))) else: uncles = block.uncles provided_fields = set(kwargs.keys()) known_fields = set(BlockHeader._meta.field_names) unknown_fields = provided_fields.difference(known_fields) if unknown_fields: raise AttributeError( "Unable to set the field(s) {0} on the `BlockHeader` class. " "Received the following unexpected fields: {1}.".format( ", ".join(known_fields), ", ".join(unknown_fields), ) ) header = block.header.copy(**kwargs) packed_block = block.copy(uncles=uncles, header=header) return packed_block
def finalize_block(self, block: BaseBlock) -> BaseBlock: block = super().finalize_block(block) # type: ignore nonce, mix_hash = pow.mine_eccpow_nonce( block.header.parent_hash, block.header.mining_hash, 24, 3, 6) return block.copy(header=block.header.copy(nonce=nonce, mix_hash=mix_hash))