Skip to content

Commit

Permalink
trie: support empty range proof (ethereum#21199)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 authored and sam bacha committed Jan 28, 2021
1 parent 97049e8 commit a3aba09
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
21 changes: 17 additions & 4 deletions trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,23 +393,24 @@ func hasRightElement(node node, key []byte) bool {
// (unless firstProof is an existent proof).
//
// Expect the normal case, this function can also be used to verify the following
// range proofs(note this function doesn't accept zero element proof):
// range proofs:
//
// - All elements proof. In this case the left and right proof can be nil, but the
// range should be all the leaves in the trie.
//
// - One element proof. In this case no matter the left edge proof is a non-existent
// proof or not, we can always verify the correctness of the proof.
//
// - Zero element proof(left edge proof should be a non-existent proof). In this
// case if there are still some other leaves available on the right side, then
// an error will be returned.
//
// Except returning the error to indicate the proof is valid or not, the function will
// also return a flag to indicate whether there exists more accounts/slots in the trie.
func VerifyRangeProof(rootHash common.Hash, firstKey []byte, keys [][]byte, values [][]byte, firstProof ethdb.KeyValueReader, lastProof ethdb.KeyValueReader) (error, bool) {
if len(keys) != len(values) {
return fmt.Errorf("inconsistent proof data, keys: %d, values: %d", len(keys), len(values)), false
}
if len(keys) == 0 {
return errors.New("empty proof"), false
}
// Ensure the received batch is monotonic increasing.
for i := 0; i < len(keys)-1; i++ {
if bytes.Compare(keys[i], keys[i+1]) >= 0 {
Expand All @@ -431,6 +432,18 @@ func VerifyRangeProof(rootHash common.Hash, firstKey []byte, keys [][]byte, valu
}
return nil, false // no more element.
}
// Special case, there is a provided left edge proof and zero key/value
// pairs, ensure there are no more accounts / slots in the trie.
if len(keys) == 0 {
root, val, err := proofToPath(rootHash, nil, firstKey, firstProof, true)
if err != nil {
return err, false
}
if val != nil || hasRightElement(root, firstKey) {
return errors.New("more entries available"), false
}
return nil, false
}
// Special case, there is only one element and left edge
// proof is an existent one.
if len(keys) == 1 && bytes.Equal(keys[0], firstKey) {
Expand Down
33 changes: 33 additions & 0 deletions trie/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,39 @@ func TestHasRightElement(t *testing.T) {
}
}

// TestEmptyRangeProof tests the range proof with "no" element.
// The first edge proof must be a non-existent proof.
func TestEmptyRangeProof(t *testing.T) {
trie, vals := randomTrie(4096)
var entries entrySlice
for _, kv := range vals {
entries = append(entries, kv)
}
sort.Sort(entries)

var cases = []struct {
pos int
err bool
}{
{len(entries) - 1, false},
{500, true},
}
for _, c := range cases {
firstProof := memorydb.New()
first := increseKey(common.CopyBytes(entries[c.pos].k))
if err := trie.Prove(first, 0, firstProof); err != nil {
t.Fatalf("Failed to prove the first node %v", err)
}
err, _ := VerifyRangeProof(trie.Hash(), first, nil, nil, firstProof, nil)
if c.err && err == nil {
t.Fatalf("Expected error, got nil")
}
if !c.err && err != nil {
t.Fatalf("Expected no error, got %v", err)
}
}
}

// mutateByte changes one byte in b.
func mutateByte(b []byte) {
for r := mrand.Intn(len(b)); ; {
Expand Down

0 comments on commit a3aba09

Please sign in to comment.