コード例 #1
0
ファイル: base.py プロジェクト: ggs134/py-evm
    def import_block(self,
                     block: BlockAPI,
                     perform_validation: bool = True
                     ) -> BlockImportResult:

        try:
            parent_header = self.get_block_header_by_hash(block.header.parent_hash)
        except HeaderNotFound:
            raise ValidationError(
                f"Attempt to import block #{block.number}.  "
                f"Cannot import block {block.hash!r} before importing "
                f"its parent block at {block.header.parent_hash!r}"
            )

        base_header_for_import = self.create_header_from_parent(parent_header)
        # Make a copy of the empty header, adding in the expected amount of gas used. This
        #   allows for richer logging in the VM.
        annotated_header = base_header_for_import.copy(gas_used=block.header.gas_used)
        block_result = self.get_vm(annotated_header).import_block(block)
        imported_block = block_result.block

        # Validate the imported block.
        if perform_validation:
            try:
                validate_imported_block_unchanged(imported_block, block)
            except ValidationError:
                self.logger.warning("Proposed %s doesn't follow EVM rules, rejecting...", block)
                raise

        persist_result = self.persist_block(imported_block, perform_validation)
        return BlockImportResult(*persist_result, block_result.meta_witness)
コード例 #2
0
ファイル: base.py プロジェクト: ggs134/py-evm
    def mine_all(
            self,
            transactions: Sequence[SignedTransactionAPI],
            *args: Any,
            parent_header: BlockHeaderAPI = None,
            **kwargs: Any,
    ) -> Tuple[BlockImportResult, Tuple[ReceiptAPI, ...], Tuple[ComputationAPI, ...]]:

        if parent_header is None:
            base_header = self.header
        else:
            base_header = self.create_header_from_parent(parent_header)

        vm = self.get_vm(base_header)

        new_header, receipts, computations = vm.apply_all_transactions(transactions, base_header)
        filled_block = vm.set_block_transactions(vm.get_block(), new_header, transactions, receipts)

        block_result = vm.mine_block(filled_block, *args, **kwargs)
        imported_block = block_result.block

        block_persist_result = self.persist_block(imported_block)
        block_import_result = BlockImportResult(*block_persist_result, block_result.meta_witness)

        self.header = self.create_header_from_parent(imported_block.header)
        return (block_import_result, receipts, computations)
コード例 #3
0
    def import_block(self,
                     block: BlockAPI,
                     perform_validation: bool=True
                     ) -> BlockImportResult:

        try:
            parent_header = self.get_block_header_by_hash(block.header.parent_hash)
        except HeaderNotFound:
            raise ValidationError(
                f"Attempt to import block #{block.number}.  "
                f"Cannot import block {block.hash!r} before importing "
                f"its parent block at {block.header.parent_hash!r}"
            )

        base_header_for_import = self.create_header_from_parent(parent_header)
        # Make a copy of the empty header, adding in the expected amount of gas used. This
        #   allows for richer logging in the VM.
        annotated_header = base_header_for_import.copy(gas_used=block.header.gas_used)
        block_result = self.get_vm(annotated_header).import_block(block)
        imported_block = block_result.block

        # Validate the imported block.
        if perform_validation:
            try:
                validate_imported_block_unchanged(imported_block, block)
            except ValidationError:
                self.logger.warning("Proposed %s doesn't follow EVM rules, rejecting...", block)
                raise
            self.validate_block(imported_block)

        (
            new_canonical_hashes,
            old_canonical_hashes,
        ) = self.chaindb.persist_block(imported_block)

        self.logger.debug(
            'IMPORTED_BLOCK: number %s | hash %s',
            imported_block.number,
            encode_hex(imported_block.hash),
        )

        new_canonical_blocks = tuple(
            self.get_block_by_hash(header_hash)
            for header_hash
            in new_canonical_hashes
        )
        old_canonical_blocks = tuple(
            self.get_block_by_hash(header_hash)
            for header_hash
            in old_canonical_hashes
        )

        return BlockImportResult(
            imported_block=imported_block,
            new_canonical_blocks=new_canonical_blocks,
            old_canonical_blocks=old_canonical_blocks,
            meta_witness=block_result.meta_witness,
        )
コード例 #4
0
    def import_block(self,
                     block: BlockAPI,
                     perform_validation: bool = True) -> BlockImportResult:

        try:
            parent_header = self.get_block_header_by_hash(
                block.header.parent_hash)
        except HeaderNotFound:
            raise ValidationError(
                f"Attempt to import block #{block.number}.  "
                f"Cannot import block {block.hash} before importing "
                f"its parent block at {block.header.parent_hash}")

        base_header_for_import = self.create_header_from_parent(parent_header)
        imported_block = self.get_vm(base_header_for_import).import_block(
            block)

        # Validate the imported block.
        if perform_validation:
            try:
                validate_imported_block_unchanged(imported_block, block)
            except ValidationError:
                self.logger.warning(
                    "Proposed %s doesn't follow EVM rules, rejecting...",
                    block)
                raise
            self.validate_block(imported_block)

        (
            new_canonical_hashes,
            old_canonical_hashes,
        ) = self.chaindb.persist_block(imported_block)

        self.logger.debug(
            'IMPORTED_BLOCK: number %s | hash %s',
            imported_block.number,
            encode_hex(imported_block.hash),
        )

        new_canonical_blocks = tuple(
            self.get_block_by_hash(header_hash)
            for header_hash in new_canonical_hashes)
        old_canonical_blocks = tuple(
            self.get_block_by_hash(header_hash)
            for header_hash in old_canonical_hashes)

        return BlockImportResult(imported_block=imported_block,
                                 new_canonical_blocks=new_canonical_blocks,
                                 old_canonical_blocks=old_canonical_blocks)