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

fix(p2p): validate block before applying and not before caching in p2p gossiping #723

Merged
merged 6 commits into from
Apr 26, 2024

Conversation

srene
Copy link
Contributor

@srene srene commented Apr 26, 2024

PR Standards

P2P gossiping tries to validate blocks on reception. However, the validation fails if blocks are received unordered, dropping them, and being unable to sync using p2p for any block received after. This PR fixes the issue validating blocks in order, moving the validation call after caching them, and before applying, once all blocks are received and can be applied in order.

Opening a pull request should be able to meet the following requirements


Close #722

<-- Briefly describe the content of this pull request -->

For Author:

  • Targeted PR against correct branch
  • included the correct type prefix in the PR title
  • Linked to Github issue with discussion and accepted design
  • Targets only one github issue
  • Wrote unit and integration tests
  • All CI checks have passed
  • Added relevant godoc comments

For Reviewer:

  • confirmed the correct type prefix in the PR title
  • Reviewers assigned
  • confirmed all author checklist items have been addressed

After reviewer approval:

  • In case targets main branch, PR should be squashed and merged.
  • In case PR targets a release branch, PR should be rebased.

@srene srene self-assigned this Apr 26, 2024
@srene srene requested a review from a team as a code owner April 26, 2024 20:57
@@ -130,8 +130,12 @@ func (m *Manager) attemptApplyCachedBlocks() error {
if !blockExists {
break
}
if err := m.validateBlock(cachedBlock.Block, cachedBlock.Commit); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

thinking we should delete it and not keep it in cache.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

block deleted from cache if invalid.

@omritoptix omritoptix merged commit 98371b5 into main Apr 26, 2024
3 of 5 checks passed
@omritoptix omritoptix deleted the srene/722-p2p-gossip-fix branch April 26, 2024 22:02
Copy link
Contributor

@danwt danwt left a comment

Choose a reason for hiding this comment

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

this reintroduces a problem found in the audit which is that bad blocks can overwrite good ones in the cache

if err := m.validateBlock(cachedBlock.Block, cachedBlock.Commit); err != nil {
delete(m.blockCache, cachedBlock.Block.Header.Height)
/// TODO: can we take an action here such as dropping the peer / reducing their reputation?
return fmt.Errorf("block not valid at height %d, dropping it: err:%w", cachedBlock.Block.Header.Height, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

height should go at the end
don't need 'err'

@danwt
Copy link
Contributor

danwt commented Apr 27, 2024

see audit issue #656

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

p2p gossiped blocks fails in validation
3 participants