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

Add state consistency check during witness generation #13412

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

antonis19
Copy link
Member

@antonis19 antonis19 commented Jan 13, 2025

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.

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
Member

@shohamc1 shohamc1 Jan 14, 2025

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
Member 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
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

@@ -464,6 +464,55 @@ func verifyExecResult(execResult *core.EphemeralExecResult, block *types.Block)
return nil
}

// 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
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

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
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

// 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
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.

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.

3 participants