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

core: ensure the canonical block is written before the canonical hash is set #2873

Merged
merged 1 commit into from
Aug 16, 2016

Conversation

bas-vk
Copy link
Member

@bas-vk bas-vk commented Jul 27, 2016

Inserting a new chain into the blockchain can cause a temporary inconsistency in the database. The canonical hash for a particular block is set before the header and body are written.

Code that tries to fetch the current block by its number will first get the canonical hash for that block number through core.GetCanonicalHash. This will yield a valid block hash. When the block is fetched immediately after with core.GetBlock this can return nil because the header and/or body isn't yet written. This is unexpected since the canonical hash could be found and code that fetched the block can expect this function to return a proper block. Currently the filter system crashes on this and maybe other parts of the system as well.

This PR ensures that the header and body are written during chain insertion before the canonical hash is set.

Fixes:
#2858
ethereum/mist#999

PTAL: @fjl @karalabe

@robotally
Copy link

robotally commented Jul 27, 2016

Vote Count Reviewers
👍 2 @fjl @karalabe
👎 0

Updated: Tue Aug 16 14:07:22 UTC 2016

@fjl
Copy link
Contributor

fjl commented Jul 27, 2016

@karalabe is on vacation, added @obscuren

@fjl fjl assigned fjl and obscuren Jul 27, 2016
@fjl
Copy link
Contributor

fjl commented Jul 27, 2016

The code change looks good to me. Does the order also need to change in HeaderChain?

@fjl
Copy link
Contributor

fjl commented Aug 1, 2016

@bas-vk @obscuren ping

@GitCop
Copy link

GitCop commented Aug 1, 2016

Thank you for your contribution! Your commits seem to not adhere to the repository coding standards

  • Commit: e6759569de62608b22bfc2c1bf574d5aaba28eb8
    • Commit subjects should be kept under 80 characters

Please check the contribution guidelines for more details.


This message was auto-generated by https://gitcop.com

@@ -196,6 +187,18 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
if err := WriteHeader(hc.chainDb, header); err != nil {
glog.Fatalf("failed to write header contents: %v", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The loop above still rewrites canonical hashes before the header is written. A safer change would be to move the writes of Header and TD up so they happen before the canonical numbers are dealt with.

@GitCop
Copy link

GitCop commented Aug 3, 2016

Thank you for your contribution! Your commits seem to not adhere to the repository coding standards

  • Commit: de0a1545ad9098140cec70a6f6b4bd1d59901386
    • Commit subjects should be kept under 80 characters

Please check the contribution guidelines for more details.


This message was auto-generated by https://gitcop.com

@fjl
Copy link
Contributor

fjl commented Aug 3, 2016

👍

@fjl fjl removed their assignment Aug 3, 2016
@fjl fjl modified the milestone: 1.4.11 Aug 5, 2016

if err := WriteHeadHeaderHash(hc.chainDb, hash); err != nil {
glog.Fatalf("failed to insert head header hash: %v", err)
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this block should be moved. The head header is the topmost index that signals the current position of the header chain. Imho it should not be updated to the new head before the canonical number is written out as the invariant is that if head points to X, everything until X is present and complete.

Copy link
Member

Choose a reason for hiding this comment

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

I of course could be wrong, if so, please enlighten me :)

@karalabe
Copy link
Member

LGTM 👍

@fjl fjl merged commit c2ac446 into ethereum:develop Aug 16, 2016
@fjl fjl removed the review label Aug 16, 2016
@bas-vk bas-vk deleted the canonicalblock branch August 17, 2016 11:25
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.

6 participants