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

[R4R] implement State Verification && Snapshot Commit pipeline #668

Merged
merged 12 commits into from
Jan 26, 2022

Conversation

unclezoro
Copy link
Collaborator

@unclezoro unclezoro commented Dec 16, 2021

Motivation 

The state verification && Snapshot/MPT commit takes a lot of time.  While in most cases, the verification can pass, there is no need to wait for the verification result serially. For snapshot/MPT commit, we only persist the bottom diff layer to disk, there is no need to wait for it to finish.  We propose an asynchronous state verification and asynchronous snapshot/MPT commit solution.

Spec

The insert blockchain can be split into three-phase:

  1. Process. Apply the transactions within the blocks.
  2. Apply the state change into MPT and calculate the state root, verify it against the block header.
  3. Commit MPT and Snapshot.

The three-phase are executed serially.
Phase2 occupies about 15-20% of the time, and in most case, it should succeed.
Phase3 occupies about 10-15% of the time.

State Verification Pipeline

A pipeline workflow is introduced in this PR:

image

Once Block Process and Snapshot Commit are done, there is no need to wait for State Verification, the blockchain can process the next block. While for the next block, before it starts State Verification, it has to wait until the State Verification of its parent finish.

Persistent Storage Pipeline

Both MPT and Snapshot need to be persisted into storage, the storage can be Memory DB and Disk DB, this PR will introduce the asynchronous persistence of Disk DB without breaking the processing of the following blocks.

Key Points

How to store the Verification Result

Since Snapshot already has a very good layout for the execution result of the block, we try to reuse it by adding a verifiedCh channel as the verification result.

How to Rollback if Verification Failed

There is an auto rollback mechanism when verification is failed.

How to avoid race condition

Introduce a few locks, but ensure it won't bring too much overhead.

Performance

Following is the 24hours comparison of a Pipeline node(10.211.32.131) and a normal node(10.211.31.49)。

Hardware: 24core, 96G, SSD, storage occupation 2.5T

Execution Time

Before:

image

After:
image

The execution time is mainly improved by this PR: #704, while in this PR, we further improve it by avoiding the CPU usage burst.

Validation Time

Before:
image

After:
image

Commit Time

Before:
image

After:
image

CommitTxs Time

Before:

image

After:
image

Further Discussion

Merge Multi Difflayer and Do Verification Once

  1. How to solve fork issue?
  2. How to avoid breaking the trie prefetcher?

Improve AccountsIntermediateRoot

  1. Do prehash?
  2. Calculation concurrently?

Improve Miner Module

  1. Improve L3 cache hit rate during mining.
  2. Reduce L2 cache overhead.

@unclezoro unclezoro force-pushed the pipeline_commit branch 21 times, most recently from a2ad058 to 4f4f94d Compare December 30, 2021 10:20
@unclezoro unclezoro changed the title [WIP]pipeline commit trie [WIP] implement State Verification pipeline Dec 30, 2021
@unclezoro unclezoro changed the title [WIP] implement State Verification pipeline [R4R] implement State Verification pipeline Dec 30, 2021
@unclezoro unclezoro force-pushed the pipeline_commit branch 6 times, most recently from ecf3134 to 85e5b73 Compare January 10, 2022 09:59
core/state/statedb.go Show resolved Hide resolved
core/state/statedb.go Show resolved Hide resolved
core/state/trie_prefetcher.go Show resolved Hide resolved
@unclezoro unclezoro force-pushed the pipeline_commit branch 4 times, most recently from 731474c to 226ff65 Compare January 21, 2022 11:07
remove prefetcher
@unclezoro unclezoro changed the title [R4R] implement State Verification pipeline [R4R] implement State Verification && Snapshot Commit pipeline Jan 23, 2022
cmd/utils/flags.go Outdated Show resolved Hide resolved
@unclezoro unclezoro merged commit eb7e309 into bnb-chain:develop Jan 26, 2022
This was referenced Jan 26, 2022
unclezoro added a commit that referenced this pull request Feb 17, 2022
* pipeline commit trie

add metrics

reopen trie

* add unit testcase

* resolve keefe's comment

* resolve igor's comments

* update prefetch

remove prefetcher

* no need to return error for precacheTransaction

* fix lint issue

* add some comments

* remove useless code

* add default option is false

* fix diffsync nil point

* fix panic on  GetProofByHash

Co-authored-by: zjubfd <[email protected]>
Copy link

@motobrowning motobrowning left a comment

Choose a reason for hiding this comment

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

K

keefel pushed a commit to keefel/bsc that referenced this pull request Jun 6, 2022
…hain#668)

* pipeline commit trie

add metrics

reopen trie

* add unit testcase

* resolve keefe's comment

* resolve igor's comments

* update prefetch

remove prefetcher

* no need to return error for precacheTransaction

* fix lint issue

* add some comments

* remove useless code

* add default option is false

* fix diffsync nil point

* fix panic on  GetProofByHash

Co-authored-by: zjubfd <[email protected]>
@buddh0 buddh0 mentioned this pull request Oct 23, 2024
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.

7 participants