def finalize_block(self, block: BlockAPI) -> BlockAPI: block = super().finalize_block(block) nonce, mix_hash = pow.mine_pow_nonce(block.number, block.header.mining_hash, block.header.difficulty) return block.copy( header=block.header.copy(nonce=nonce, mix_hash=mix_hash))
def finalize_block(self, block: BlockAPI) -> BlockAndMetaWitness: if block.number > 0: snapshot = self.state.snapshot() try: self._assign_block_rewards(block) except EVMMissingData as exc: self.state.revert(snapshot) raise else: self.state.commit(snapshot) # We need to call `persist` here since the state db batches # all writes until we tell it to write to the underlying db meta_witness = self.state.persist() final_block = block.copy(header=block.header.copy( state_root=self.state.state_root)) self.logger.debug( "%s reads %d unique node hashes, %d addresses, %d bytecodes, and %d storage slots", final_block, len(meta_witness.hashes), len(meta_witness.accounts_queried), len(meta_witness.account_bytecodes_queried), meta_witness.total_slots_queried, ) return BlockAndMetaWitness(final_block, meta_witness)
def finalize_block(self, block: BlockAPI) -> BlockAndMetaWitness: block_result = super().finalize_block(block) block = block_result.block nonce, mix_hash = pow.mine_pow_nonce(block.number, block.header.mining_hash, block.header.difficulty) mined_block = block.copy( header=block.header.copy(nonce=nonce, mix_hash=mix_hash)) return BlockAndMetaWitness(mined_block, block_result.meta_witness)
def finalize_block(self, block: BlockAPI) -> BlockAPI: if block.number > 0: snapshot = self.state.snapshot() try: self._assign_block_rewards(block) except EVMMissingData as exc: self.state.revert(snapshot) raise else: self.state.commit(snapshot) # 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: BlockAPI, new_header: BlockHeaderAPI, transactions: Sequence[SignedTransactionAPI], receipts: Sequence[ReceiptAPI]) -> BlockAPI: 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: BlockAPI, *args: Any, **kwargs: Any) -> BlockAPI: 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( f"Unable to set the field(s) {', '.join(known_fields)} " "on the `BlockHeader` class. " f"Received the following unexpected fields: {', '.join(unknown_fields)}." ) header = block.header.copy(**kwargs) packed_block = block.copy(uncles=uncles, header=header) return packed_block
def pack_block(self, block: BlockAPI, *args: Any, **kwargs: Any) -> BlockAPI: """ 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