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

trie: reject deletions when verifying range proofs #23960

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,17 @@ func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, key
if len(keys) != len(values) {
return false, fmt.Errorf("inconsistent proof data, keys: %d, values: %d", len(keys), len(values))
}
// Ensure the received batch is monotonic increasing.
// Ensure the received batch is monotonic increasing and contains no deletions
for i := 0; i < len(keys)-1; i++ {
if bytes.Compare(keys[i], keys[i+1]) >= 0 {
return false, errors.New("range is not monotonically increasing")
}
}
for _, value := range values {
if len(value) == 0 {
return false, errors.New("range contains deletion")
}
}
Comment on lines +475 to +485
Copy link
Contributor

Choose a reason for hiding this comment

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

len(keys) == len(values), so you could use the same loop, with a slight modification

Suggested change
// Ensure the received batch is monotonic increasing and contains no deletions
for i := 0; i < len(keys)-1; i++ {
if bytes.Compare(keys[i], keys[i+1]) >= 0 {
return false, errors.New("range is not monotonically increasing")
}
}
for _, value := range values {
if len(value) == 0 {
return false, errors.New("range contains deletion")
}
}
// Ensure the received batch is monotonic increasing and contains no deletions
for i := 0; i < len(keys); i++ {
if i > 0 && bytes.Compare(keys[i-1], keys[i]) >= 0 {
return false, errors.New("range is not monotonically increasing")
}
if len(values[i]) == 0 {
return false, errors.New("range contains deletion")
}
}

Feel free to ignore

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Member Author

Choose a reason for hiding this comment

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

It did occur to me, but the off by one and extra clause check seemed wonky. The code is cleaner this way, we're not saving anything by optimizing out an extra addition per array item. Unless you guys feel very strongly, I'd leave it as is.

// Special case, there is no edge proof at all. The given range is expected
// to be the whole leaf-set in the trie.
if proof == nil {
Expand Down
79 changes: 79 additions & 0 deletions trie/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,85 @@ func TestBloatedProof(t *testing.T) {
}
}

// TestEmptyValueRangeProof tests normal range proof with both edge proofs
// as the existent proof, but with an extra empty value included, which is a
// noop technically, but practically should be rejected.
func TestEmptyValueRangeProof(t *testing.T) {
trie, values := randomTrie(512)
var entries entrySlice
for _, kv := range values {
entries = append(entries, kv)
}
sort.Sort(entries)

// Create a new entry with a slightly modified key
mid := len(entries) / 2
key := common.CopyBytes(entries[mid-1].k)
for n := len(key) - 1; n >= 0; n-- {
if key[n] < 0xff {
key[n]++
break
}
}
noop := &kv{key, []byte{}, false}
entries = append(append(append([]*kv{}, entries[:mid]...), noop), entries[mid:]...)

start, end := 1, len(entries)-1

proof := memorydb.New()
if err := trie.Prove(entries[start].k, 0, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err)
}
if err := trie.Prove(entries[end-1].k, 0, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err)
}
var keys [][]byte
var vals [][]byte
for i := start; i < end; i++ {
keys = append(keys, entries[i].k)
vals = append(vals, entries[i].v)
}
_, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof)
if err == nil {
t.Fatalf("Expected failure on noop entry")
}
}

// TestAllElementsEmptyValueRangeProof tests the range proof with all elements,
// but with an extra empty value included, which is a noop technically, but
// practically should be rejected.
func TestAllElementsEmptyValueRangeProof(t *testing.T) {
trie, values := randomTrie(512)
var entries entrySlice
for _, kv := range values {
entries = append(entries, kv)
}
sort.Sort(entries)

// Create a new entry with a slightly modified key
mid := len(entries) / 2
key := common.CopyBytes(entries[mid-1].k)
for n := len(key) - 1; n >= 0; n-- {
if key[n] < 0xff {
key[n]++
break
}
}
noop := &kv{key, []byte{}, false}
entries = append(append(append([]*kv{}, entries[:mid]...), noop), entries[mid:]...)

var keys [][]byte
var vals [][]byte
for i := 0; i < len(entries); i++ {
keys = append(keys, entries[i].k)
vals = append(vals, entries[i].v)
}
_, err := VerifyRangeProof(trie.Hash(), nil, nil, keys, vals, nil)
if err == nil {
t.Fatalf("Expected failure on noop entry")
}
}

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