Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blockchain] Simplify block verification #544

Merged
merged 23 commits into from
Jun 1, 2022

Conversation

zivkovicmilos
Copy link
Contributor

Description

This PR aims to separate out block insertion and block verification.
There are 2 kinds of block verifications that can occur:

  • Node wants to verify a previously sealed block (from a different node)
  • Node wants to verify a proposed block (from a different node)

It also refactors the way the blockchain module is tested.

Changes include

  • Bugfix (non-breaking change that solves an issue)
  • Hotfix (change that solves an urgent issue, and requires immediate attention)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (change that is not backwards-compatible and/or changes current functionality)

Checklist

  • I have assigned this PR to myself
  • I have added at least 1 reviewer
  • I have added the relevant labels
  • I have updated the official documentation
  • I have added sufficient documentation in code

Testing

  • I have tested this code with the official test suite
  • I have tested this code manually

Additional comments

Fixes EDGE-469

@zivkovicmilos zivkovicmilos added the bug fix Functionality that fixes a bug label May 10, 2022
@zivkovicmilos zivkovicmilos self-assigned this May 10, 2022
@zivkovicmilos zivkovicmilos marked this pull request as ready for review May 11, 2022 09:40
blockchain/blockchain.go Outdated Show resolved Hide resolved
Copy link
Contributor

@dbrajovic dbrajovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Great job on breaking up the WriteBlock logic as well as adding test coverage 🙏

Copy link
Contributor

@Kourin1996 Kourin1996 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few comments

blockchain/blockchain.go Outdated Show resolved Hide resolved
blockchain/blockchain.go Outdated Show resolved Hide resolved
blockchain/blockchain.go Outdated Show resolved Hide resolved
blockchain/blockchain.go Outdated Show resolved Hide resolved
blockchain/blockchain.go Outdated Show resolved Hide resolved
consensus/ibft/ibft.go Outdated Show resolved Hide resolved
Copy link
Contributor

@Kourin1996 Kourin1996 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment. Blockchain struct will be no longer safe because it's possible to insert block with incorrect parent hash due to wrong way to use Blockchain. (Please correct me if I'm misreading)

My suggestion is to add checking ParentHash in WriteBlock at least.

@zivkovicmilos
Copy link
Contributor Author

zivkovicmilos commented May 16, 2022

@Kourin1996

You're right, but it comes down to the semantics of WriteBlock. Everywhere in the codebase it's been used with the assumption that it will also verify in addition to writing.

After I've decoupled these concepts, I needed to add a VerifySealedBlock from every module that was expecting the verification (I believe only restore and syncer).

@dbrajovic what do you think about @Kourin1996's suggestion?

@Kourin1996
Copy link
Contributor

Kourin1996 commented May 16, 2022

You're right, but it comes down to the semantics of WriteBlock. Everywhere in the codebase it's been used with the assumption that it will also verify in addition to writing.

@zivkovicmilos I don't think this is the verification. It's like a nil check before inserting.

Copy link
Contributor

@0xAleksaOpacic 0xAleksaOpacic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I have manually tested branch with old chain for backwards compactibility. It works

blockchain/blockchain.go Show resolved Hide resolved
@zivkovicmilos
Copy link
Contributor Author

zivkovicmilos commented May 17, 2022

@Kourin1996

I've added a notice to the WriteBlock method:

a74f79a

People who need to call WriteBlock, will not expect verification. If they want verification, they can call the verification method before committing the block locally

@dbrajovic
Copy link
Contributor

dbrajovic commented May 17, 2022

cc @zivkovicmilos @Kourin1996

Judging by all the places where WriteBlock() is used, there should be no side effects despite taking the block verification outside of the method. At each call site,I backtracked the stack to see if verification is still ensured after the call to WriteBlock - this does seem to be case.

As for the naming, it is anti-pattern, but so is the blockchain module doing any verification in the first place. This should be tackled in a separate PR, imo.

@Kourin1996
Copy link
Contributor

@zivkovicmilos Could you check whether node check each CommittedSeals signature before inserting block?

@zivkovicmilos
Copy link
Contributor Author

@Kourin1996

I've gone into a detailed comparison between checks done on develop and checks done on this branch - the only thing that I found missing was the CommittedSeals check before inserting a block, as it was previously contained inside WriteBlock.

I've fixed this in:

92f8113

@zivkovicmilos zivkovicmilos added this to the 0.4.1 Release milestone May 19, 2022
@zivkovicmilos zivkovicmilos merged commit 06dc155 into develop Jun 1, 2022
@zivkovicmilos zivkovicmilos deleted the bugfix/simplify-block-verification branch June 1, 2022 08:37
@github-actions github-actions bot locked and limited conversation to collaborators Jun 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug fix Functionality that fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants