github.com/core-coin/go-core/v2@v2.1.9/trie/proof_test.go (about)

     1  // Copyright 2015 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-core library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package trie
    18  
    19  import (
    20  	"bytes"
    21  	crand "crypto/rand"
    22  	mrand "math/rand"
    23  	"sort"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/core-coin/go-core/v2/xcbdb/memorydb"
    28  
    29  	"github.com/core-coin/go-core/v2/common"
    30  	"github.com/core-coin/go-core/v2/crypto"
    31  )
    32  
    33  func init() {
    34  	mrand.Seed(time.Now().Unix())
    35  }
    36  
    37  // makeProvers creates Merkle trie provers based on different implementations to
    38  // test all variations.
    39  func makeProvers(trie *Trie) []func(key []byte) *memorydb.Database {
    40  	var provers []func(key []byte) *memorydb.Database
    41  
    42  	// Create a direct trie based Merkle prover
    43  	provers = append(provers, func(key []byte) *memorydb.Database {
    44  		proof := memorydb.New()
    45  		trie.Prove(key, 0, proof)
    46  		return proof
    47  	})
    48  	// Create a leaf iterator based Merkle prover
    49  	provers = append(provers, func(key []byte) *memorydb.Database {
    50  		proof := memorydb.New()
    51  		if it := NewIterator(trie.NodeIterator(key)); it.Next() && bytes.Equal(key, it.Key) {
    52  			for _, p := range it.Prove() {
    53  				proof.Put(crypto.SHA3(p), p)
    54  			}
    55  		}
    56  		return proof
    57  	})
    58  	return provers
    59  }
    60  
    61  func TestProof(t *testing.T) {
    62  	trie, vals := randomTrie(500)
    63  	root := trie.Hash()
    64  	for i, prover := range makeProvers(trie) {
    65  		for _, kv := range vals {
    66  			proof := prover(kv.k)
    67  			if proof == nil {
    68  				t.Fatalf("prover %d: missing key %x while constructing proof", i, kv.k)
    69  			}
    70  			val, err := VerifyProof(root, kv.k, proof)
    71  			if err != nil {
    72  				t.Fatalf("prover %d: failed to verify proof for key %x: %v\nraw proof: %x", i, kv.k, err, proof)
    73  			}
    74  			if !bytes.Equal(val, kv.v) {
    75  				t.Fatalf("prover %d: verified value mismatch for key %x: have %x, want %x", i, kv.k, val, kv.v)
    76  			}
    77  		}
    78  	}
    79  }
    80  
    81  func TestOneElementProof(t *testing.T) {
    82  	trie := new(Trie)
    83  	updateString(trie, "k", "v")
    84  	for i, prover := range makeProvers(trie) {
    85  		proof := prover([]byte("k"))
    86  		if proof == nil {
    87  			t.Fatalf("prover %d: nil proof", i)
    88  		}
    89  		if proof.Len() != 1 {
    90  			t.Errorf("prover %d: proof should have one element", i)
    91  		}
    92  		val, err := VerifyProof(trie.Hash(), []byte("k"), proof)
    93  		if err != nil {
    94  			t.Fatalf("prover %d: failed to verify proof: %v\nraw proof: %x", i, err, proof)
    95  		}
    96  		if !bytes.Equal(val, []byte("v")) {
    97  			t.Fatalf("prover %d: verified value mismatch: have %x, want 'k'", i, val)
    98  		}
    99  	}
   100  }
   101  
   102  func TestBadProof(t *testing.T) {
   103  	trie, vals := randomTrie(800)
   104  	root := trie.Hash()
   105  	for i, prover := range makeProvers(trie) {
   106  		for _, kv := range vals {
   107  			proof := prover(kv.k)
   108  			if proof == nil {
   109  				t.Fatalf("prover %d: nil proof", i)
   110  			}
   111  			it := proof.NewIterator(nil, nil)
   112  			for i, d := 0, mrand.Intn(proof.Len()); i <= d; i++ {
   113  				it.Next()
   114  			}
   115  			key := it.Key()
   116  			val, _ := proof.Get(key)
   117  			proof.Delete(key)
   118  			it.Release()
   119  
   120  			mutateByte(val)
   121  			proof.Put(crypto.SHA3(val), val)
   122  
   123  			if _, err := VerifyProof(root, kv.k, proof); err == nil {
   124  				t.Fatalf("prover %d: expected proof to fail for key %x", i, kv.k)
   125  			}
   126  		}
   127  	}
   128  }
   129  
   130  // Tests that missing keys can also be proven. The test explicitly uses a single
   131  // entry trie and checks for missing keys both before and after the single entry.
   132  func TestMissingKeyProof(t *testing.T) {
   133  	trie := new(Trie)
   134  	updateString(trie, "k", "v")
   135  
   136  	for i, key := range []string{"a", "j", "l", "z"} {
   137  		proof := memorydb.New()
   138  		trie.Prove([]byte(key), 0, proof)
   139  
   140  		if proof.Len() != 1 {
   141  			t.Errorf("test %d: proof should have one element", i)
   142  		}
   143  		val, err := VerifyProof(trie.Hash(), []byte(key), proof)
   144  		if err != nil {
   145  			t.Fatalf("test %d: failed to verify proof: %v\nraw proof: %x", i, err, proof)
   146  		}
   147  		if val != nil {
   148  			t.Fatalf("test %d: verified value mismatch: have %x, want nil", i, val)
   149  		}
   150  	}
   151  }
   152  
   153  type entrySlice []*kv
   154  
   155  func (p entrySlice) Len() int           { return len(p) }
   156  func (p entrySlice) Less(i, j int) bool { return bytes.Compare(p[i].k, p[j].k) < 0 }
   157  func (p entrySlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
   158  
   159  // TestRangeProof tests normal range proof with both edge proofs
   160  // as the existent proof. The test cases are generated randomly.
   161  func TestRangeProof(t *testing.T) {
   162  	trie, vals := randomTrie(4096)
   163  	var entries entrySlice
   164  	for _, kv := range vals {
   165  		entries = append(entries, kv)
   166  	}
   167  	sort.Sort(entries)
   168  	for i := 0; i < 500; i++ {
   169  		start := mrand.Intn(len(entries))
   170  		end := mrand.Intn(len(entries)-start) + start + 1
   171  
   172  		proof := memorydb.New()
   173  		if err := trie.Prove(entries[start].k, 0, proof); err != nil {
   174  			t.Fatalf("Failed to prove the first node %v", err)
   175  		}
   176  		if err := trie.Prove(entries[end-1].k, 0, proof); err != nil {
   177  			t.Fatalf("Failed to prove the last node %v", err)
   178  		}
   179  		var keys [][]byte
   180  		var vals [][]byte
   181  		for i := start; i < end; i++ {
   182  			keys = append(keys, entries[i].k)
   183  			vals = append(vals, entries[i].v)
   184  		}
   185  		err, _ := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof)
   186  		if err != nil {
   187  			t.Fatalf("Case %d(%d->%d) expect no error, got %v", i, start, end-1, err)
   188  		}
   189  	}
   190  }
   191  
   192  // TestRangeProof tests normal range proof with two non-existent proofs.
   193  // The test cases are generated randomly.
   194  func TestRangeProofWithNonExistentProof(t *testing.T) {
   195  	trie, vals := randomTrie(4096)
   196  	var entries entrySlice
   197  	for _, kv := range vals {
   198  		entries = append(entries, kv)
   199  	}
   200  	sort.Sort(entries)
   201  	for i := 0; i < 500; i++ {
   202  		start := mrand.Intn(len(entries))
   203  		end := mrand.Intn(len(entries)-start) + start + 1
   204  		proof := memorydb.New()
   205  
   206  		// Short circuit if the decreased key is same with the previous key
   207  		first := decreseKey(common.CopyBytes(entries[start].k))
   208  		if start != 0 && bytes.Equal(first, entries[start-1].k) {
   209  			continue
   210  		}
   211  		// Short circuit if the decreased key is underflow
   212  		if bytes.Compare(first, entries[start].k) > 0 {
   213  			continue
   214  		}
   215  		// Short circuit if the increased key is same with the next key
   216  		last := increseKey(common.CopyBytes(entries[end-1].k))
   217  		if end != len(entries) && bytes.Equal(last, entries[end].k) {
   218  			continue
   219  		}
   220  		// Short circuit if the increased key is overflow
   221  		if bytes.Compare(last, entries[end-1].k) < 0 {
   222  			continue
   223  		}
   224  		if err := trie.Prove(first, 0, proof); err != nil {
   225  			t.Fatalf("Failed to prove the first node %v", err)
   226  		}
   227  		if err := trie.Prove(last, 0, proof); err != nil {
   228  			t.Fatalf("Failed to prove the last node %v", err)
   229  		}
   230  		var keys [][]byte
   231  		var vals [][]byte
   232  		for i := start; i < end; i++ {
   233  			keys = append(keys, entries[i].k)
   234  			vals = append(vals, entries[i].v)
   235  		}
   236  		err, _ := VerifyRangeProof(trie.Hash(), first, last, keys, vals, proof)
   237  		if err != nil {
   238  			t.Fatalf("Case %d(%d->%d) expect no error, got %v", i, start, end-1, err)
   239  		}
   240  	}
   241  	// Special case, two edge proofs for two edge key.
   242  	proof := memorydb.New()
   243  	first := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes()
   244  	last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes()
   245  	if err := trie.Prove(first, 0, proof); err != nil {
   246  		t.Fatalf("Failed to prove the first node %v", err)
   247  	}
   248  	if err := trie.Prove(last, 0, proof); err != nil {
   249  		t.Fatalf("Failed to prove the last node %v", err)
   250  	}
   251  	var k [][]byte
   252  	var v [][]byte
   253  	for i := 0; i < len(entries); i++ {
   254  		k = append(k, entries[i].k)
   255  		v = append(v, entries[i].v)
   256  	}
   257  	err, _ := VerifyRangeProof(trie.Hash(), first, last, k, v, proof)
   258  	if err != nil {
   259  		t.Fatal("Failed to verify whole rang with non-existent edges")
   260  	}
   261  }
   262  
   263  // TestRangeProofWithInvalidNonExistentProof tests such scenarios:
   264  // - There exists a gap between the first element and the left edge proof
   265  // - There exists a gap between the last element and the right edge proof
   266  func TestRangeProofWithInvalidNonExistentProof(t *testing.T) {
   267  	trie, vals := randomTrie(4096)
   268  	var entries entrySlice
   269  	for _, kv := range vals {
   270  		entries = append(entries, kv)
   271  	}
   272  	sort.Sort(entries)
   273  
   274  	// Case 1
   275  	start, end := 100, 200
   276  	first := decreseKey(common.CopyBytes(entries[start].k))
   277  
   278  	proof := memorydb.New()
   279  	if err := trie.Prove(first, 0, proof); err != nil {
   280  		t.Fatalf("Failed to prove the first node %v", err)
   281  	}
   282  	if err := trie.Prove(entries[end-1].k, 0, proof); err != nil {
   283  		t.Fatalf("Failed to prove the last node %v", err)
   284  	}
   285  	start = 105 // Gap created
   286  	k := make([][]byte, 0)
   287  	v := make([][]byte, 0)
   288  	for i := start; i < end; i++ {
   289  		k = append(k, entries[i].k)
   290  		v = append(v, entries[i].v)
   291  	}
   292  	err, _ := VerifyRangeProof(trie.Hash(), first, k[len(k)-1], k, v, proof)
   293  	if err == nil {
   294  		t.Fatalf("Expected to detect the error, got nil")
   295  	}
   296  
   297  	// Case 2
   298  	start, end = 100, 200
   299  	last := increseKey(common.CopyBytes(entries[end-1].k))
   300  	proof = memorydb.New()
   301  	if err := trie.Prove(entries[start].k, 0, proof); err != nil {
   302  		t.Fatalf("Failed to prove the first node %v", err)
   303  	}
   304  	if err := trie.Prove(last, 0, proof); err != nil {
   305  		t.Fatalf("Failed to prove the last node %v", err)
   306  	}
   307  	end = 195 // Capped slice
   308  	k = make([][]byte, 0)
   309  	v = make([][]byte, 0)
   310  	for i := start; i < end; i++ {
   311  		k = append(k, entries[i].k)
   312  		v = append(v, entries[i].v)
   313  	}
   314  	err, _ = VerifyRangeProof(trie.Hash(), k[0], last, k, v, proof)
   315  	if err == nil {
   316  		t.Fatalf("Expected to detect the error, got nil")
   317  	}
   318  }
   319  
   320  // TestOneElementRangeProof tests the proof with only one
   321  // element. The first edge proof can be existent one or
   322  // non-existent one.
   323  func TestOneElementRangeProof(t *testing.T) {
   324  	trie, vals := randomTrie(4096)
   325  	var entries entrySlice
   326  	for _, kv := range vals {
   327  		entries = append(entries, kv)
   328  	}
   329  	sort.Sort(entries)
   330  
   331  	// One element with existent edge proof, both edge proofs
   332  	// point to the SAME key.
   333  	start := 1000
   334  	proof := memorydb.New()
   335  	if err := trie.Prove(entries[start].k, 0, proof); err != nil {
   336  		t.Fatalf("Failed to prove the first node %v", err)
   337  	}
   338  	err, _ := VerifyRangeProof(trie.Hash(), entries[start].k, entries[start].k, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
   339  	if err != nil {
   340  		t.Fatalf("Expected no error, got %v", err)
   341  	}
   342  
   343  	// One element with left non-existent edge proof
   344  	start = 1000
   345  	first := decreseKey(common.CopyBytes(entries[start].k))
   346  	proof = memorydb.New()
   347  	if err := trie.Prove(first, 0, proof); err != nil {
   348  		t.Fatalf("Failed to prove the first node %v", err)
   349  	}
   350  	if err := trie.Prove(entries[start].k, 0, proof); err != nil {
   351  		t.Fatalf("Failed to prove the last node %v", err)
   352  	}
   353  	err, _ = VerifyRangeProof(trie.Hash(), first, entries[start].k, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
   354  	if err != nil {
   355  		t.Fatalf("Expected no error, got %v", err)
   356  	}
   357  
   358  	// One element with right non-existent edge proof
   359  	start = 1000
   360  	last := increseKey(common.CopyBytes(entries[start].k))
   361  	proof = memorydb.New()
   362  	if err := trie.Prove(entries[start].k, 0, proof); err != nil {
   363  		t.Fatalf("Failed to prove the first node %v", err)
   364  	}
   365  	if err := trie.Prove(last, 0, proof); err != nil {
   366  		t.Fatalf("Failed to prove the last node %v", err)
   367  	}
   368  	err, _ = VerifyRangeProof(trie.Hash(), entries[start].k, last, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
   369  	if err != nil {
   370  		t.Fatalf("Expected no error, got %v", err)
   371  	}
   372  
   373  	// One element with two non-existent edge proofs
   374  	start = 1000
   375  	first, last = decreseKey(common.CopyBytes(entries[start].k)), increseKey(common.CopyBytes(entries[start].k))
   376  	proof = memorydb.New()
   377  	if err := trie.Prove(first, 0, proof); err != nil {
   378  		t.Fatalf("Failed to prove the first node %v", err)
   379  	}
   380  	if err := trie.Prove(last, 0, proof); err != nil {
   381  		t.Fatalf("Failed to prove the last node %v", err)
   382  	}
   383  	err, _ = VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
   384  	if err != nil {
   385  		t.Fatalf("Expected no error, got %v", err)
   386  	}
   387  }
   388  
   389  // TestAllElementsProof tests the range proof with all elements.
   390  // The edge proofs can be nil.
   391  func TestAllElementsProof(t *testing.T) {
   392  	trie, vals := randomTrie(4096)
   393  	var entries entrySlice
   394  	for _, kv := range vals {
   395  		entries = append(entries, kv)
   396  	}
   397  	sort.Sort(entries)
   398  
   399  	var k [][]byte
   400  	var v [][]byte
   401  	for i := 0; i < len(entries); i++ {
   402  		k = append(k, entries[i].k)
   403  		v = append(v, entries[i].v)
   404  	}
   405  	err, _ := VerifyRangeProof(trie.Hash(), nil, nil, k, v, nil)
   406  	if err != nil {
   407  		t.Fatalf("Expected no error, got %v", err)
   408  	}
   409  
   410  	// With edge proofs, it should still work.
   411  	proof := memorydb.New()
   412  	if err := trie.Prove(entries[0].k, 0, proof); err != nil {
   413  		t.Fatalf("Failed to prove the first node %v", err)
   414  	}
   415  	if err := trie.Prove(entries[len(entries)-1].k, 0, proof); err != nil {
   416  		t.Fatalf("Failed to prove the last node %v", err)
   417  	}
   418  	err, _ = VerifyRangeProof(trie.Hash(), k[0], k[len(k)-1], k, v, proof)
   419  	if err != nil {
   420  		t.Fatalf("Expected no error, got %v", err)
   421  	}
   422  
   423  	// Even with non-existent edge proofs, it should still work.
   424  	proof = memorydb.New()
   425  	first := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes()
   426  	last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes()
   427  	if err := trie.Prove(first, 0, proof); err != nil {
   428  		t.Fatalf("Failed to prove the first node %v", err)
   429  	}
   430  	if err := trie.Prove(last, 0, proof); err != nil {
   431  		t.Fatalf("Failed to prove the last node %v", err)
   432  	}
   433  	err, _ = VerifyRangeProof(trie.Hash(), first, last, k, v, proof)
   434  	if err != nil {
   435  		t.Fatalf("Expected no error, got %v", err)
   436  	}
   437  }
   438  
   439  // TestSingleSideRangeProof tests the range starts from zero.
   440  func TestSingleSideRangeProof(t *testing.T) {
   441  	for i := 0; i < 64; i++ {
   442  		trie := new(Trie)
   443  		var entries entrySlice
   444  		for i := 0; i < 4096; i++ {
   445  			value := &kv{randBytes(32), randBytes(20), false}
   446  			trie.Update(value.k, value.v)
   447  			entries = append(entries, value)
   448  		}
   449  		sort.Sort(entries)
   450  
   451  		var cases = []int{0, 1, 50, 100, 1000, 2000, len(entries) - 1}
   452  		for _, pos := range cases {
   453  			proof := memorydb.New()
   454  			if err := trie.Prove(common.Hash{}.Bytes(), 0, proof); err != nil {
   455  				t.Fatalf("Failed to prove the first node %v", err)
   456  			}
   457  			if err := trie.Prove(entries[pos].k, 0, proof); err != nil {
   458  				t.Fatalf("Failed to prove the first node %v", err)
   459  			}
   460  			k := make([][]byte, 0)
   461  			v := make([][]byte, 0)
   462  			for i := 0; i <= pos; i++ {
   463  				k = append(k, entries[i].k)
   464  				v = append(v, entries[i].v)
   465  			}
   466  			err, _ := VerifyRangeProof(trie.Hash(), common.Hash{}.Bytes(), k[len(k)-1], k, v, proof)
   467  			if err != nil {
   468  				t.Fatalf("Expected no error, got %v", err)
   469  			}
   470  		}
   471  	}
   472  }
   473  
   474  // TestReverseSingleSideRangeProof tests the range ends with 0xffff...fff.
   475  func TestReverseSingleSideRangeProof(t *testing.T) {
   476  	for i := 0; i < 64; i++ {
   477  		trie := new(Trie)
   478  		var entries entrySlice
   479  		for i := 0; i < 4096; i++ {
   480  			value := &kv{randBytes(32), randBytes(20), false}
   481  			trie.Update(value.k, value.v)
   482  			entries = append(entries, value)
   483  		}
   484  		sort.Sort(entries)
   485  
   486  		var cases = []int{0, 1, 50, 100, 1000, 2000, len(entries) - 1}
   487  		for _, pos := range cases {
   488  			proof := memorydb.New()
   489  			if err := trie.Prove(entries[pos].k, 0, proof); err != nil {
   490  				t.Fatalf("Failed to prove the first node %v", err)
   491  			}
   492  			last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
   493  			if err := trie.Prove(last.Bytes(), 0, proof); err != nil {
   494  				t.Fatalf("Failed to prove the last node %v", err)
   495  			}
   496  			k := make([][]byte, 0)
   497  			v := make([][]byte, 0)
   498  			for i := pos; i < len(entries); i++ {
   499  				k = append(k, entries[i].k)
   500  				v = append(v, entries[i].v)
   501  			}
   502  			err, _ := VerifyRangeProof(trie.Hash(), k[0], last.Bytes(), k, v, proof)
   503  			if err != nil {
   504  				t.Fatalf("Expected no error, got %v", err)
   505  			}
   506  		}
   507  	}
   508  }
   509  
   510  // TestBadRangeProof tests a few cases which the proof is wrong.
   511  // The prover is expected to detect the error.
   512  func TestBadRangeProof(t *testing.T) {
   513  	trie, vals := randomTrie(4096)
   514  	var entries entrySlice
   515  	for _, kv := range vals {
   516  		entries = append(entries, kv)
   517  	}
   518  	sort.Sort(entries)
   519  
   520  	for i := 0; i < 500; i++ {
   521  		start := mrand.Intn(len(entries))
   522  		end := mrand.Intn(len(entries)-start) + start + 1
   523  		proof := memorydb.New()
   524  		if err := trie.Prove(entries[start].k, 0, proof); err != nil {
   525  			t.Fatalf("Failed to prove the first node %v", err)
   526  		}
   527  		if err := trie.Prove(entries[end-1].k, 0, proof); err != nil {
   528  			t.Fatalf("Failed to prove the last node %v", err)
   529  		}
   530  		var keys [][]byte
   531  		var vals [][]byte
   532  		for i := start; i < end; i++ {
   533  			keys = append(keys, entries[i].k)
   534  			vals = append(vals, entries[i].v)
   535  		}
   536  		var first, last = keys[0], keys[len(keys)-1]
   537  		testcase := mrand.Intn(6)
   538  		var index int
   539  		switch testcase {
   540  		case 0:
   541  			// Modified key
   542  			index = mrand.Intn(end - start)
   543  			keys[index] = randBytes(32) // In theory it can't be same
   544  		case 1:
   545  			// Modified val
   546  			index = mrand.Intn(end - start)
   547  			vals[index] = randBytes(20) // In theory it can't be same
   548  		case 2:
   549  			// Gapped entry slice
   550  			index = mrand.Intn(end - start)
   551  			if (index == 0 && start < 100) || (index == end-start-1 && end <= 100) {
   552  				continue
   553  			}
   554  			keys = append(keys[:index], keys[index+1:]...)
   555  			vals = append(vals[:index], vals[index+1:]...)
   556  		case 3:
   557  			// Out of order
   558  			index1 := mrand.Intn(end - start)
   559  			index2 := mrand.Intn(end - start)
   560  			if index1 == index2 {
   561  				continue
   562  			}
   563  			keys[index1], keys[index2] = keys[index2], keys[index1]
   564  			vals[index1], vals[index2] = vals[index2], vals[index1]
   565  		case 4:
   566  			// Set random key to nil, do nothing
   567  			index = mrand.Intn(end - start)
   568  			keys[index] = nil
   569  		case 5:
   570  			// Set random value to nil, deletion
   571  			index = mrand.Intn(end - start)
   572  			vals[index] = nil
   573  		}
   574  		err, _ := VerifyRangeProof(trie.Hash(), first, last, keys, vals, proof)
   575  		if err == nil {
   576  			t.Fatalf("%d Case %d index %d range: (%d->%d) expect error, got nil", i, testcase, index, start, end-1)
   577  		}
   578  	}
   579  }
   580  
   581  // TestGappedRangeProof focuses on the small trie with embedded nodes.
   582  // If the gapped node is embedded in the trie, it should be detected too.
   583  func TestGappedRangeProof(t *testing.T) {
   584  	trie := new(Trie)
   585  	var entries []*kv // Sorted entries
   586  	for i := byte(0); i < 10; i++ {
   587  		value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
   588  		trie.Update(value.k, value.v)
   589  		entries = append(entries, value)
   590  	}
   591  	first, last := 2, 8
   592  	proof := memorydb.New()
   593  	if err := trie.Prove(entries[first].k, 0, proof); err != nil {
   594  		t.Fatalf("Failed to prove the first node %v", err)
   595  	}
   596  	if err := trie.Prove(entries[last-1].k, 0, proof); err != nil {
   597  		t.Fatalf("Failed to prove the last node %v", err)
   598  	}
   599  	var keys [][]byte
   600  	var vals [][]byte
   601  	for i := first; i < last; i++ {
   602  		if i == (first+last)/2 {
   603  			continue
   604  		}
   605  		keys = append(keys, entries[i].k)
   606  		vals = append(vals, entries[i].v)
   607  	}
   608  	err, _ := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof)
   609  	if err == nil {
   610  		t.Fatal("expect error, got nil")
   611  	}
   612  }
   613  
   614  // TestSameSideProofs tests the element is not in the range covered by proofs
   615  func TestSameSideProofs(t *testing.T) {
   616  	trie, vals := randomTrie(4096)
   617  	var entries entrySlice
   618  	for _, kv := range vals {
   619  		entries = append(entries, kv)
   620  	}
   621  	sort.Sort(entries)
   622  
   623  	pos := 1000
   624  	first := decreseKey(common.CopyBytes(entries[pos].k))
   625  	first = decreseKey(first)
   626  	last := decreseKey(common.CopyBytes(entries[pos].k))
   627  
   628  	proof := memorydb.New()
   629  	if err := trie.Prove(first, 0, proof); err != nil {
   630  		t.Fatalf("Failed to prove the first node %v", err)
   631  	}
   632  	if err := trie.Prove(last, 0, proof); err != nil {
   633  		t.Fatalf("Failed to prove the last node %v", err)
   634  	}
   635  	err, _ := VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[pos].k}, [][]byte{entries[pos].v}, proof)
   636  	if err == nil {
   637  		t.Fatalf("Expected error, got nil")
   638  	}
   639  
   640  	first = increseKey(common.CopyBytes(entries[pos].k))
   641  	last = increseKey(common.CopyBytes(entries[pos].k))
   642  	last = increseKey(last)
   643  
   644  	proof = memorydb.New()
   645  	if err := trie.Prove(first, 0, proof); err != nil {
   646  		t.Fatalf("Failed to prove the first node %v", err)
   647  	}
   648  	if err := trie.Prove(last, 0, proof); err != nil {
   649  		t.Fatalf("Failed to prove the last node %v", err)
   650  	}
   651  	err, _ = VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[pos].k}, [][]byte{entries[pos].v}, proof)
   652  	if err == nil {
   653  		t.Fatalf("Expected error, got nil")
   654  	}
   655  }
   656  
   657  func TestHasRightElement(t *testing.T) {
   658  	trie := new(Trie)
   659  	var entries entrySlice
   660  	for i := 0; i < 4096; i++ {
   661  		value := &kv{randBytes(32), randBytes(20), false}
   662  		trie.Update(value.k, value.v)
   663  		entries = append(entries, value)
   664  	}
   665  	sort.Sort(entries)
   666  
   667  	var cases = []struct {
   668  		start   int
   669  		end     int
   670  		hasMore bool
   671  	}{
   672  		{-1, 1, true}, // single element with non-existent left proof
   673  		{0, 1, true},  // single element with existent left proof
   674  		{0, 10, true},
   675  		{50, 100, true},
   676  		{50, len(entries), false},               // No more element expected
   677  		{len(entries) - 1, len(entries), false}, // Single last element with two existent proofs(point to same key)
   678  		{len(entries) - 1, -1, false},           // Single last element with non-existent right proof
   679  		{0, len(entries), false},                // The whole set with existent left proof
   680  		{-1, len(entries), false},               // The whole set with non-existent left proof
   681  		{-1, -1, false},                         // The whole set with non-existent left/right proof
   682  	}
   683  	for _, c := range cases {
   684  		var (
   685  			firstKey []byte
   686  			lastKey  []byte
   687  			start    = c.start
   688  			end      = c.end
   689  			proof    = memorydb.New()
   690  		)
   691  		if c.start == -1 {
   692  			firstKey, start = common.Hash{}.Bytes(), 0
   693  			if err := trie.Prove(firstKey, 0, proof); err != nil {
   694  				t.Fatalf("Failed to prove the first node %v", err)
   695  			}
   696  		} else {
   697  			firstKey = entries[c.start].k
   698  			if err := trie.Prove(entries[c.start].k, 0, proof); err != nil {
   699  				t.Fatalf("Failed to prove the first node %v", err)
   700  			}
   701  		}
   702  		if c.end == -1 {
   703  			lastKey, end = common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes(), len(entries)
   704  			if err := trie.Prove(lastKey, 0, proof); err != nil {
   705  				t.Fatalf("Failed to prove the first node %v", err)
   706  			}
   707  		} else {
   708  			lastKey = entries[c.end-1].k
   709  			if err := trie.Prove(entries[c.end-1].k, 0, proof); err != nil {
   710  				t.Fatalf("Failed to prove the first node %v", err)
   711  			}
   712  		}
   713  		k := make([][]byte, 0)
   714  		v := make([][]byte, 0)
   715  		for i := start; i < end; i++ {
   716  			k = append(k, entries[i].k)
   717  			v = append(v, entries[i].v)
   718  		}
   719  		err, hasMore := VerifyRangeProof(trie.Hash(), firstKey, lastKey, k, v, proof)
   720  		if err != nil {
   721  			t.Fatalf("Expected no error, got %v", err)
   722  		}
   723  		if hasMore != c.hasMore {
   724  			t.Fatalf("Wrong hasMore indicator, want %t, got %t", c.hasMore, hasMore)
   725  		}
   726  	}
   727  }
   728  
   729  // TestEmptyRangeProof tests the range proof with "no" element.
   730  // The first edge proof must be a non-existent proof.
   731  func TestEmptyRangeProof(t *testing.T) {
   732  	trie, vals := randomTrie(4096)
   733  	var entries entrySlice
   734  	for _, kv := range vals {
   735  		entries = append(entries, kv)
   736  	}
   737  	sort.Sort(entries)
   738  
   739  	var cases = []struct {
   740  		pos int
   741  		err bool
   742  	}{
   743  		{len(entries) - 1, false},
   744  		{500, true},
   745  	}
   746  	for _, c := range cases {
   747  		proof := memorydb.New()
   748  		first := increseKey(common.CopyBytes(entries[c.pos].k))
   749  		if err := trie.Prove(first, 0, proof); err != nil {
   750  			t.Fatalf("Failed to prove the first node %v", err)
   751  		}
   752  		err, _ := VerifyRangeProof(trie.Hash(), first, nil, nil, nil, proof)
   753  		if c.err && err == nil {
   754  			t.Fatalf("Expected error, got nil")
   755  		}
   756  		if !c.err && err != nil {
   757  			t.Fatalf("Expected no error, got %v", err)
   758  		}
   759  	}
   760  }
   761  
   762  // mutateByte changes one byte in b.
   763  func mutateByte(b []byte) {
   764  	for r := mrand.Intn(len(b)); ; {
   765  		new := byte(mrand.Intn(255))
   766  		if new != b[r] {
   767  			b[r] = new
   768  			break
   769  		}
   770  	}
   771  }
   772  
   773  func increseKey(key []byte) []byte {
   774  	for i := len(key) - 1; i >= 0; i-- {
   775  		key[i]++
   776  		if key[i] != 0x0 {
   777  			break
   778  		}
   779  	}
   780  	return key
   781  }
   782  
   783  func decreseKey(key []byte) []byte {
   784  	for i := len(key) - 1; i >= 0; i-- {
   785  		key[i]--
   786  		if key[i] != 0xff {
   787  			break
   788  		}
   789  	}
   790  	return key
   791  }
   792  
   793  func BenchmarkProve(b *testing.B) {
   794  	trie, vals := randomTrie(100)
   795  	var keys []string
   796  	for k := range vals {
   797  		keys = append(keys, k)
   798  	}
   799  
   800  	b.ResetTimer()
   801  	for i := 0; i < b.N; i++ {
   802  		kv := vals[keys[i%len(keys)]]
   803  		proofs := memorydb.New()
   804  		if trie.Prove(kv.k, 0, proofs); proofs.Len() == 0 {
   805  			b.Fatalf("zero length proof for %x", kv.k)
   806  		}
   807  	}
   808  }
   809  
   810  func BenchmarkVerifyProof(b *testing.B) {
   811  	trie, vals := randomTrie(100)
   812  	root := trie.Hash()
   813  	var keys []string
   814  	var proofs []*memorydb.Database
   815  	for k := range vals {
   816  		keys = append(keys, k)
   817  		proof := memorydb.New()
   818  		trie.Prove([]byte(k), 0, proof)
   819  		proofs = append(proofs, proof)
   820  	}
   821  
   822  	b.ResetTimer()
   823  	for i := 0; i < b.N; i++ {
   824  		im := i % len(keys)
   825  		if _, err := VerifyProof(root, []byte(keys[im]), proofs[im]); err != nil {
   826  			b.Fatalf("key %x: %v", keys[im], err)
   827  		}
   828  	}
   829  }
   830  
   831  func BenchmarkVerifyRangeProof10(b *testing.B)   { benchmarkVerifyRangeProof(b, 10) }
   832  func BenchmarkVerifyRangeProof100(b *testing.B)  { benchmarkVerifyRangeProof(b, 100) }
   833  func BenchmarkVerifyRangeProof1000(b *testing.B) { benchmarkVerifyRangeProof(b, 1000) }
   834  func BenchmarkVerifyRangeProof5000(b *testing.B) { benchmarkVerifyRangeProof(b, 5000) }
   835  
   836  func benchmarkVerifyRangeProof(b *testing.B, size int) {
   837  	trie, vals := randomTrie(8192)
   838  	var entries entrySlice
   839  	for _, kv := range vals {
   840  		entries = append(entries, kv)
   841  	}
   842  	sort.Sort(entries)
   843  
   844  	start := 2
   845  	end := start + size
   846  	proof := memorydb.New()
   847  	if err := trie.Prove(entries[start].k, 0, proof); err != nil {
   848  		b.Fatalf("Failed to prove the first node %v", err)
   849  	}
   850  	if err := trie.Prove(entries[end-1].k, 0, proof); err != nil {
   851  		b.Fatalf("Failed to prove the last node %v", err)
   852  	}
   853  	var keys [][]byte
   854  	var values [][]byte
   855  	for i := start; i < end; i++ {
   856  		keys = append(keys, entries[i].k)
   857  		values = append(values, entries[i].v)
   858  	}
   859  
   860  	b.ResetTimer()
   861  	for i := 0; i < b.N; i++ {
   862  		err, _ := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, values, proof)
   863  		if err != nil {
   864  			b.Fatalf("Case %d(%d->%d) expect no error, got %v", i, start, end-1, err)
   865  		}
   866  	}
   867  }
   868  
   869  func randomTrie(n int) (*Trie, map[string]*kv) {
   870  	trie := new(Trie)
   871  	vals := make(map[string]*kv)
   872  	for i := byte(0); i < 100; i++ {
   873  		value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
   874  		value2 := &kv{common.LeftPadBytes([]byte{i + 10}, 32), []byte{i}, false}
   875  		trie.Update(value.k, value.v)
   876  		trie.Update(value2.k, value2.v)
   877  		vals[string(value.k)] = value
   878  		vals[string(value2.k)] = value2
   879  	}
   880  	for i := 0; i < n; i++ {
   881  		value := &kv{randBytes(32), randBytes(20), false}
   882  		trie.Update(value.k, value.v)
   883  		vals[string(value.k)] = value
   884  	}
   885  	return trie, vals
   886  }
   887  
   888  func randBytes(n int) []byte {
   889  	r := make([]byte, n)
   890  	crand.Read(r)
   891  	return r
   892  }