question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`VM.validate_header()` is called twice during `Chain.import_block()`?

See original GitHub issue

What is wrong?

The current calling route:

class Chain(BaseChain):
    def import_block(self,
                     block: BaseBlock,
                     perform_validation: bool=True
                     ) -> Tuple[BaseBlock, Tuple[BaseBlock, ...], Tuple[BaseBlock, ...]]:
        ....
        base_header_for_import = self.create_header_from_parent(parent_header)
        imported_block = self.get_vm(base_header_for_import).import_block(block)
        # ^^^^^^ dive into VM.import_block()

        # Validate the imported block.
        if perform_validation:
        # ^^^^^^ perform_validation is True by default.
            validate_imported_block_unchanged(imported_block, block)
            self.validate_block(imported_block)
        ....

    def validate_block(self, block: BaseBlock) -> None:
        if block.is_genesis:
            raise ValidationError("Cannot validate genesis block this way")
        VM = self.get_vm_class_for_block_number(BlockNumber(block.number))
        parent_block = self.get_block_by_hash(block.header.parent_hash)
        VM.validate_header(block.header, parent_block.header, check_seal=True)
        # ^^^^^^ second time call VM.validate_header!
        ....
class VM(BaseVM):
    def import_block(self, block):
        ....
        return self.mine_block()

    def mine_block(self, *args, **kwargs):
        ....
        # Perform validation
        self.validate_block(final_block)

        return final_block

    def validate_block(self, block):
        ....
        if block.is_genesis:
            validate_length_lte(block.header.extra_data, 32, title="BlockHeader.extra_data")
        else:
            parent_header = get_parent_header(block.header, self.chaindb)
            self.validate_header(block.header, parent_header)
            # ^^^^^^ first time calling VM.validate_header! check_seal is True by default.
        ....

How can it be fixed

Is Chain.import_block the main API for syncing? I guess perform_validation is for testing. Can we remove the second call and set the first call check_seal=True?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
hwwhwwcommented, Oct 22, 2018

@pipermerriam whoops sorry, my question was do I have to extract the block validation from BeaconStateMachine.import_block and BeaconStateMachine.process_block, and make BeaconChain trigger the block validation. Your second response answered my question. 👍

0reactions
pipermerriamcommented, Oct 22, 2018

@hwwhww 1) I’m not sure I understand your question and 2) BeaconChain should not try to be shaped like Chain in any case where it doesn’t fit well. It’s better for BeaconChain to be properly architected than for it to have API parity with Chain.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Service functions is called twice - Stack Overflow
You set function onSubmit() on two events: Button (click) and form (submit). It will call this twice. <div class="container"> <form ...
Read more >
prepareForDML method called twice - Oracle Communities
Have concentrated business validation logic within the prepareForDML() of the updatable entity implementation. Under normal conditions, it works ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found