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: flush out trie cache more meaningfully on stop #16143

Merged
merged 2 commits into from
Feb 23, 2018

Conversation

karalabe
Copy link
Member

@karalabe karalabe commented Feb 20, 2018

The current code writes out the HEAD-127 trie on graceful shutdown. The idea was:

  • On startup, we load the latest 128 tries into memory, immediately being able to serve remote nodes.
  • As long as the network did a reorg smaller than 128 blocks, we don't reexecute large chain segments.

Unfortunately this has the annoying drawback that starting the node always re-executes 128 blocks. Since people in general won't constantly start/stop/start/stop/etc, it's annoying to reprocess that many blocks. If your node was offline for more than 30 mins, you'll either way load enough tries into memory to help remote nodes sync.

This PR fixes the issue by writing out 3 tries instead of only one on stop:

  • HEAD: So we don't need to reprocess any blocks in the general case.
  • HEAD-1: So we don't do large reorgs if our HEAD becomes an uncle.
  • HEAD-127: So we have a hard limit on the number of blocks reexecuted during deep reorgs.

The second commit in this PR upgrades all the legacy blockchain tests to use test chains generated by the chain maker. The old code used "invalid" chains with only some specific header fields set (others left unset) in combination with disabled block validation. This however was both murky even until now, as well as caused the trie cache to blow up due to invalid state roots. Instead of continuing to patch the "invalid" chains to somehow pass the tests, I just completely dumped them and replaces with valid ones generated via chain makers.

@karalabe karalabe added this to the 1.8.2 milestone Feb 20, 2018
log.Info("Writing cached state to disk", "block", recent.Number(), "hash", recent.Hash(), "root", recent.Root())
if err := triedb.Commit(recent.Root(), true); err != nil {
log.Error("Failed to commit recent state trie", "err", err)
for _, offset := range []uint64{0, 1, triesInMemory - 1} {
Copy link
Contributor

Choose a reason for hiding this comment

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

Random question: is triesInMemory guaranteed to never be 0? Or 1?..

Copy link
Member Author

Choose a reason for hiding this comment

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

triesInMemory is a constant 128. It's not meant to be changed, since it needs to be in sync with the fasy sync algorithm.

Copy link
Contributor

@holiman holiman left a comment

Choose a reason for hiding this comment

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

LGTM, but I wonder if it wouldn't work just as well to save only -1 and -127. Would make less impact on the "stop/restart/stop/restart" users hard drives, and would be hardly noticeable for normal users.

@karalabe
Copy link
Member Author

I added -0 as it was confusing to a lot of people why the chain rolls back at all if nothing went wrong.

@karalabe karalabe merged commit 89f914c into ethereum:master Feb 23, 2018
prestonvanloon pushed a commit to prestonvanloon/go-ethereum that referenced this pull request Apr 2, 2018
* core: flush out trie cache more meaningfully on stop

* core: upgrade legacy tests to chain maker
@kernoelpanic
Copy link

I would also suggest to provide the possibility to periodically write out the HEAD-x trie, especially when running in PoA mode.

In such a case it could happen that there is only one PoA geth instance for testing purposes. If this instance crashes the whole blockchain will be "lost", if the node was never gracefully shut down before (see #16131 and #16142):

WARN [06-04|10:40:37] Head state missing, repairing chain      number=65897 hash=…
INFO [06-04|10:40:43] Rewound blockchain to past state         number=37    hash=…

mariameda pushed a commit to NiluPlatform/go-nilu that referenced this pull request Aug 23, 2018
* core: flush out trie cache more meaningfully on stop

* core: upgrade legacy tests to chain maker
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.

4 participants