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

verify block witness and proof #496

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package core

import (
"bytes"
"errors"
"fmt"

Expand Down Expand Up @@ -131,6 +132,53 @@
if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error())
}
// In verkle mode, verify the proof.
if v.config.IsVerkle(header.Number, header.Time) {
// Check that all keys in the witness were used
keys := statedb.Witness().Keys()
var (
key [32]byte // reconstructed key, to be searched for in witness
keycount int // number of keys found
)
for _, stemdiff := range block.ExecutionWitness().StateDiff {
copy(key[:31], stemdiff.Stem[:])
for _, suffixdiff := range stemdiff.SuffixDiffs {
key[31] = suffixdiff.Suffix

var found bool
for _, k := range keys {
if bytes.Equal(k, key[:]) {
found = true
break
}
}
if !found {
return fmt.Errorf("superfluous key %x could not be found in witness", key)
}
keycount++
}
}

// In order to make sure that the provided witness isn't missing any keys,
// compare the counts. This will catch incomplete witnesses at the post root,
// the key count and the inclusion check should be enough to garantee these

Check failure on line 164 in core/block_validator.go

View workflow job for this annotation

GitHub Actions / lint

`garantee` is a misspelling of `guarantee` (misspell)
// two trees are the same, without executing the block statelessly.
if keycount != len(keys) {
return fmt.Errorf("locations seem to be missing from the tree: got %d locations, expected %d", keycount, len(keys))
}

// Open the pre-tree to prove the pre-state against
parent := v.bc.GetHeaderByNumber(header.Number.Uint64() - 1)
if parent == nil {
return fmt.Errorf("nil parent header for block %d", header.Number)
}
Comment on lines +170 to +174
Copy link
Owner Author

Choose a reason for hiding this comment

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

The parent has most definitely already been loaded before, see if it's possible to pass it as a parameter.


// Verify the proof
if err := trie.DeserializeAndVerifyVerkleProof(block.ExecutionWitness().VerkleProof, parent.Root.Bytes(), block.Root().Bytes(), block.ExecutionWitness().StateDiff); err != nil {
return fmt.Errorf("error verifying proof at block %d: %w", block.NumberU64(), err)
}
}

// Verify that the advertised root is correct before
// it can be used as an identifier for the conversion
// status.
Expand Down
Loading