Skip to content

Add state consistency check during witness generation#13412

Closed
antonis19 wants to merge 1 commit into
mainfrom
witness-verify-state
Closed

Add state consistency check during witness generation#13412
antonis19 wants to merge 1 commit into
mainfrom
witness-verify-state

Conversation

@antonis19

@antonis19 antonis19 commented Jan 13, 2025

Copy link
Copy Markdown
Contributor

This is to prevent the witness algorithm from running if the account and storage data that will be read by hph.ctx.Account and hph.ctx.Storage is inconsistent with the data from the StateReader for that block, as that would result in a root hash mismatch.

In general there are 2 cases where we can get a root hash mismatch:

  1. The witness trie structure was created improperly
  2. The witness trie structure is correct but the state data (i.e. account nodes and storage values) is incorrect. This could happen if hph.ctx.Account and hph.ctx.Storage return the wrong value.

Case 2 could happen because of for example stale data being read by hph.ctx after the unwind, or if the unwind was not applied properly, or a variety of other reasons. It's not clear what the underlying cause is and needs further investigation.

Comment thread turbo/jsonrpc/eth_call.go
func verifyStateConsistency(touchedPlainKeys [][]byte, stateReader state.StateReader, hph *commitment.HexPatriciaHashed) (bool, error) {
ctx := hph.GetPatriciaContext()
for _, key := range touchedPlainKeys {
if len(key) == 20 { //account address

@shohamc1 shohamc1 Jan 14, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should throw err if len<20?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, that will never happen. The keys will either be account keys (20 bytes), or storage keys (20+32=52 bytes).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

that would happen during tests at least

Comment thread turbo/jsonrpc/eth_call.go
}

// verify the state consistency between the history state reader and the hph.Ctx reader
func verifyStateConsistency(touchedPlainKeys [][]byte, stateReader state.StateReader, hph *commitment.HexPatriciaHashed) (bool, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

add notice that this is for debug. in general ctx and state reader should have same view, so it's just comparison with another reader which may or may not share same view with hph.ctx. So when every detail with reading is cleaned out, this comparison is a self-comparison and waste of resources during witness calculation

Comment thread turbo/jsonrpc/eth_call.go
func verifyStateConsistency(touchedPlainKeys [][]byte, stateReader state.StateReader, hph *commitment.HexPatriciaHashed) (bool, error) {
ctx := hph.GetPatriciaContext()
for _, key := range touchedPlainKeys {
if len(key) == 20 { //account address

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

that would happen during tests at least

Comment thread turbo/jsonrpc/eth_call.go
// and the patricia context (hph.ctx)
// If there is any inconsistency there will inevitably be a root hash mismatch later on when
// we try to construct the witness trie
isStateConsistent, err := verifyStateConsistency(touchedPlainKeys, store.Tds.StateReader, hph)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

id suppose to make code block like

{
 // Verify that ....
 ...
}

so it could catch an eye that this section actually is for debugging and may be omitted.

@antonis19 antonis19 mentioned this pull request Apr 9, 2025
@AskAlexSharov

Copy link
Copy Markdown
Collaborator

@antonis19 ping

@awskii

awskii commented Jul 25, 2025

Copy link
Copy Markdown
Member

edge cases has been closed by #16220
#16251

@awskii awskii closed this Jul 25, 2025
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