Skip to content

Commit 070a5e1

Browse files
authored
trie: fix for range proof (#21107)
* trie: fix for range proof * trie: fix typo
1 parent 81e9cae commit 070a5e1

2 files changed

Lines changed: 73 additions & 56 deletions

File tree

‎trie/proof.go‎

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -219,54 +219,69 @@ func unsetInternal(n node, left []byte, right []byte) error {
219219
if len(left) != len(right) {
220220
return errors.New("inconsistent edge path")
221221
}
222-
// Step down to the fork point
223-
prefix, pos := prefixLen(left, right), 0
224-
var parent node
222+
// Step down to the fork point. There are two scenarios can happen:
223+
// - the fork point is a shortnode: the left proof MUST point to a
224+
// non-existent key and the key doesn't match with the shortnode
225+
// - the fork point is a fullnode: the left proof can point to an
226+
// existent key or not.
227+
var (
228+
pos = 0
229+
parent node
230+
)
231+
findFork:
225232
for {
226-
if pos >= prefix {
227-
break
228-
}
229233
switch rn := (n).(type) {
230234
case *shortNode:
235+
// The right proof must point to an existent key.
231236
if len(right)-pos < len(rn.Key) || !bytes.Equal(rn.Key, right[pos:pos+len(rn.Key)]) {
232237
return errors.New("invalid edge path")
233238
}
239+
rn.flags = nodeFlag{dirty: true}
234240
// Special case, the non-existent proof points to the same path
235241
// as the existent proof, but the path of existent proof is longer.
236-
// In this case, truncate the extra path(it should be recovered
237-
// by node insertion).
242+
// In this case, the fork point is this shortnode.
238243
if len(left)-pos < len(rn.Key) || !bytes.Equal(rn.Key, left[pos:pos+len(rn.Key)]) {
239-
fn := parent.(*fullNode)
240-
fn.Children[left[pos-1]] = nil
241-
return nil
244+
break findFork
242245
}
243-
rn.flags = nodeFlag{dirty: true}
244246
parent = n
245247
n, pos = rn.Val, pos+len(rn.Key)
246248
case *fullNode:
249+
leftnode, rightnode := rn.Children[left[pos]], rn.Children[right[pos]]
250+
// The right proof must point to an existent key.
251+
if rightnode == nil {
252+
return errors.New("invalid edge path")
253+
}
247254
rn.flags = nodeFlag{dirty: true}
255+
if leftnode != rightnode {
256+
break findFork
257+
}
248258
parent = n
249-
n, pos = rn.Children[right[pos]], pos+1
259+
n, pos = rn.Children[left[pos]], pos+1
250260
default:
251261
panic(fmt.Sprintf("%T: invalid node: %v", n, n))
252262
}
253263
}
254-
fn, ok := n.(*fullNode)
255-
if !ok {
256-
return errors.New("the fork point must be a fullnode")
257-
}
258-
// Find the fork point! Unset all intermediate references
259-
for i := left[prefix] + 1; i < right[prefix]; i++ {
260-
fn.Children[i] = nil
261-
}
262-
fn.flags = nodeFlag{dirty: true}
263-
if err := unset(fn, fn.Children[left[prefix]], left[prefix:], 1, false); err != nil {
264-
return err
265-
}
266-
if err := unset(fn, fn.Children[right[prefix]], right[prefix:], 1, true); err != nil {
267-
return err
264+
switch rn := n.(type) {
265+
case *shortNode:
266+
if _, ok := rn.Val.(valueNode); ok {
267+
parent.(*fullNode).Children[right[pos-1]] = nil
268+
return nil
269+
}
270+
return unset(rn, rn.Val, right[pos:], len(rn.Key), true)
271+
case *fullNode:
272+
for i := left[pos] + 1; i < right[pos]; i++ {
273+
rn.Children[i] = nil
274+
}
275+
if err := unset(rn, rn.Children[left[pos]], left[pos:], 1, false); err != nil {
276+
return err
277+
}
278+
if err := unset(rn, rn.Children[right[pos]], right[pos:], 1, true); err != nil {
279+
return err
280+
}
281+
return nil
282+
default:
283+
panic(fmt.Sprintf("%T: invalid node: %v", n, n))
268284
}
269-
return nil
270285
}
271286

272287
// unset removes all internal node references either the left most or right most.
@@ -314,8 +329,8 @@ func unset(parent node, child node, key []byte, pos int, removeLeft bool) error
314329
// The key of fork shortnode is less than the
315330
// path(it doesn't belong to the range), keep
316331
// it with the cached hash available.
317-
return nil
318332
}
333+
return nil
319334
}
320335
if _, ok := cld.Val.(valueNode); ok {
321336
fn := parent.(*fullNode)

‎trie/proof_test.go‎

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -397,33 +397,35 @@ func TestAllElementsProof(t *testing.T) {
397397

398398
// TestSingleSideRangeProof tests the range starts from zero.
399399
func TestSingleSideRangeProof(t *testing.T) {
400-
trie := new(Trie)
401-
var entries entrySlice
402-
for i := 0; i < 4096; i++ {
403-
value := &kv{randBytes(32), randBytes(20), false}
404-
trie.Update(value.k, value.v)
405-
entries = append(entries, value)
406-
}
407-
sort.Sort(entries)
408-
409-
var cases = []int{0, 1, 50, 100, 1000, 2000, len(entries) - 1}
410-
for _, pos := range cases {
411-
firstProof, lastProof := memorydb.New(), memorydb.New()
412-
if err := trie.Prove(common.Hash{}.Bytes(), 0, firstProof); err != nil {
413-
t.Fatalf("Failed to prove the first node %v", err)
414-
}
415-
if err := trie.Prove(entries[pos].k, 0, lastProof); err != nil {
416-
t.Fatalf("Failed to prove the first node %v", err)
417-
}
418-
k := make([][]byte, 0)
419-
v := make([][]byte, 0)
420-
for i := 0; i <= pos; i++ {
421-
k = append(k, entries[i].k)
422-
v = append(v, entries[i].v)
423-
}
424-
err := VerifyRangeProof(trie.Hash(), common.Hash{}.Bytes(), k, v, firstProof, lastProof)
425-
if err != nil {
426-
t.Fatalf("Expected no error, got %v", err)
400+
for i := 0; i < 64; i++ {
401+
trie := new(Trie)
402+
var entries entrySlice
403+
for i := 0; i < 4096; i++ {
404+
value := &kv{randBytes(32), randBytes(20), false}
405+
trie.Update(value.k, value.v)
406+
entries = append(entries, value)
407+
}
408+
sort.Sort(entries)
409+
410+
var cases = []int{0, 1, 50, 100, 1000, 2000, len(entries) - 1}
411+
for _, pos := range cases {
412+
firstProof, lastProof := memorydb.New(), memorydb.New()
413+
if err := trie.Prove(common.Hash{}.Bytes(), 0, firstProof); err != nil {
414+
t.Fatalf("Failed to prove the first node %v", err)
415+
}
416+
if err := trie.Prove(entries[pos].k, 0, lastProof); err != nil {
417+
t.Fatalf("Failed to prove the first node %v", err)
418+
}
419+
k := make([][]byte, 0)
420+
v := make([][]byte, 0)
421+
for i := 0; i <= pos; i++ {
422+
k = append(k, entries[i].k)
423+
v = append(v, entries[i].v)
424+
}
425+
err := VerifyRangeProof(trie.Hash(), common.Hash{}.Bytes(), k, v, firstProof, lastProof)
426+
if err != nil {
427+
t.Fatalf("Expected no error, got %v", err)
428+
}
427429
}
428430
}
429431
}

0 commit comments

Comments
 (0)