From b3253ab7781d5e8ba720e1eaa33a9d717bb92582 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Mon, 22 Jun 2020 10:49:45 +0800 Subject: [PATCH] trie: improve test --- trie/proof_test.go | 104 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 5 deletions(-) diff --git a/trie/proof_test.go b/trie/proof_test.go index 6f933c13a89d..9e8b8ba77d3a 100644 --- a/trie/proof_test.go +++ b/trie/proof_test.go @@ -188,7 +188,7 @@ func TestRangeProof(t *testing.T) { } } -// TestRangeProof tests normal range proof with the non-existent proof. +// TestRangeProof tests normal range proof with two non-existent proofs. // The test cases are generated randomly. func TestRangeProofWithNonExistentProof(t *testing.T) { trie, vals := randomTrie(4096) @@ -237,6 +237,26 @@ func TestRangeProofWithNonExistentProof(t *testing.T) { t.Fatalf("Case %d(%d->%d) expect no error, got %v", i, start, end-1, err) } } + // Special case, two edge proofs for two edge key. + proof := memorydb.New() + first := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes() + last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes() + if err := trie.Prove(first, 0, proof); err != nil { + t.Fatalf("Failed to prove the first node %v", err) + } + if err := trie.Prove(last, 0, proof); err != nil { + t.Fatalf("Failed to prove the last node %v", err) + } + var k [][]byte + var v [][]byte + for i := 0; i < len(entries); i++ { + k = append(k, entries[i].k) + v = append(v, entries[i].v) + } + err, _ := VerifyRangeProof(trie.Hash(), first, last, k, v, proof) + if err != nil { + t.Fatal("Failed to verify whole rang with non-existent edges") + } } // TestRangeProofWithInvalidNonExistentProof tests such scenarios: @@ -318,6 +338,7 @@ func TestOneElementRangeProof(t *testing.T) { if err != nil { t.Fatalf("Expected no error, got %v", err) } + // One element with left non-existent edge proof start = 1000 first := decreseKey(common.CopyBytes(entries[start].k)) @@ -332,6 +353,7 @@ func TestOneElementRangeProof(t *testing.T) { if err != nil { t.Fatalf("Expected no error, got %v", err) } + // One element with right non-existent edge proof start = 1000 last := increseKey(common.CopyBytes(entries[start].k)) @@ -346,6 +368,21 @@ func TestOneElementRangeProof(t *testing.T) { if err != nil { t.Fatalf("Expected no error, got %v", err) } + + // One element with two non-existent edge proofs + start = 1000 + first, last = decreseKey(common.CopyBytes(entries[start].k)), increseKey(common.CopyBytes(entries[start].k)) + proof = memorydb.New() + if err := trie.Prove(first, 0, proof); err != nil { + t.Fatalf("Failed to prove the first node %v", err) + } + if err := trie.Prove(last, 0, proof); err != nil { + t.Fatalf("Failed to prove the last node %v", err) + } + err, _ = VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } } // TestAllElementsProof tests the range proof with all elements. @@ -369,7 +406,7 @@ func TestAllElementsProof(t *testing.T) { t.Fatalf("Expected no error, got %v", err) } - // Even with edge proofs, it should still work. + // With edge proofs, it should still work. proof := memorydb.New() if err := trie.Prove(entries[0].k, 0, proof); err != nil { t.Fatalf("Failed to prove the first node %v", err) @@ -381,6 +418,21 @@ func TestAllElementsProof(t *testing.T) { if err != nil { t.Fatalf("Expected no error, got %v", err) } + + // Even with non-existent edge proofs, it should still work. + proof = memorydb.New() + first := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes() + last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes() + if err := trie.Prove(first, 0, proof); err != nil { + t.Fatalf("Failed to prove the first node %v", err) + } + if err := trie.Prove(last, 0, proof); err != nil { + t.Fatalf("Failed to prove the last node %v", err) + } + err, _ = VerifyRangeProof(trie.Hash(), first, last, k, v, proof) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } } // TestSingleSideRangeProof tests the range starts from zero. @@ -507,9 +559,8 @@ func TestBadRangeProof(t *testing.T) { if index1 == index2 { continue } - tmpk, tmpv := keys[index1], vals[index1] - keys[index1], vals[index1] = keys[index2], vals[index2] - keys[index2], vals[index2] = tmpk, tmpv + keys[index1], keys[index2] = keys[index2], keys[index1] + vals[index1], vals[index2] = vals[index2], vals[index1] case 4: // Set random key to nil, do nothing index = mrand.Intn(end - start) @@ -559,6 +610,49 @@ func TestGappedRangeProof(t *testing.T) { } } +// TestSameSideProofs tests the element is not in the range covered by proofs +func TestSameSideProofs(t *testing.T) { + trie, vals := randomTrie(4096) + var entries entrySlice + for _, kv := range vals { + entries = append(entries, kv) + } + sort.Sort(entries) + + pos := 1000 + first := decreseKey(common.CopyBytes(entries[pos].k)) + first = decreseKey(first) + last := decreseKey(common.CopyBytes(entries[pos].k)) + + proof := memorydb.New() + if err := trie.Prove(first, 0, proof); err != nil { + t.Fatalf("Failed to prove the first node %v", err) + } + if err := trie.Prove(last, 0, proof); err != nil { + t.Fatalf("Failed to prove the last node %v", err) + } + err, _ := VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[pos].k}, [][]byte{entries[pos].v}, proof) + if err == nil { + t.Fatalf("Expected error, got nil") + } + + first = increseKey(common.CopyBytes(entries[pos].k)) + last = increseKey(common.CopyBytes(entries[pos].k)) + last = increseKey(last) + + proof = memorydb.New() + if err := trie.Prove(first, 0, proof); err != nil { + t.Fatalf("Failed to prove the first node %v", err) + } + if err := trie.Prove(last, 0, proof); err != nil { + t.Fatalf("Failed to prove the last node %v", err) + } + err, _ = VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[pos].k}, [][]byte{entries[pos].v}, proof) + if err == nil { + t.Fatalf("Expected error, got nil") + } +} + func TestHasRightElement(t *testing.T) { trie := new(Trie) var entries entrySlice