@@ -22,13 +22,13 @@ import (
2222 "encoding/binary"
2323 "fmt"
2424 mrand "math/rand"
25+ "sort"
2526 "testing"
2627
2728 "github.com/ethereum/go-ethereum/common"
2829 "github.com/ethereum/go-ethereum/core/rawdb"
2930 "github.com/ethereum/go-ethereum/crypto"
3031 "github.com/ethereum/go-ethereum/ethdb/memorydb"
31- "golang.org/x/exp/slices"
3232)
3333
3434// Prng is a pseudo random number generator seeded by strong randomness.
@@ -165,15 +165,21 @@ func TestMissingKeyProof(t *testing.T) {
165165 }
166166}
167167
168+ type entrySlice []* kv
169+
170+ func (p entrySlice ) Len () int { return len (p ) }
171+ func (p entrySlice ) Less (i , j int ) bool { return bytes .Compare (p [i ].k , p [j ].k ) < 0 }
172+ func (p entrySlice ) Swap (i , j int ) { p [i ], p [j ] = p [j ], p [i ] }
173+
168174// TestRangeProof tests normal range proof with both edge proofs
169175// as the existent proof. The test cases are generated randomly.
170176func TestRangeProof (t * testing.T ) {
171177 trie , vals := randomTrie (4096 )
172- var entries [] * kv
178+ var entries entrySlice
173179 for _ , kv := range vals {
174180 entries = append (entries , kv )
175181 }
176- slices . SortFunc (entries , ( * kv ). less )
182+ sort . Sort (entries )
177183 for i := 0 ; i < 500 ; i ++ {
178184 start := mrand .Intn (len (entries ))
179185 end := mrand .Intn (len (entries )- start ) + start + 1
@@ -202,11 +208,11 @@ func TestRangeProof(t *testing.T) {
202208// The test cases are generated randomly.
203209func TestRangeProofWithNonExistentProof (t * testing.T ) {
204210 trie , vals := randomTrie (4096 )
205- var entries [] * kv
211+ var entries entrySlice
206212 for _ , kv := range vals {
207213 entries = append (entries , kv )
208214 }
209- slices . SortFunc (entries , ( * kv ). less )
215+ sort . Sort (entries )
210216 for i := 0 ; i < 500 ; i ++ {
211217 start := mrand .Intn (len (entries ))
212218 end := mrand .Intn (len (entries )- start ) + start + 1
@@ -274,11 +280,11 @@ func TestRangeProofWithNonExistentProof(t *testing.T) {
274280// - There exists a gap between the last element and the right edge proof
275281func TestRangeProofWithInvalidNonExistentProof (t * testing.T ) {
276282 trie , vals := randomTrie (4096 )
277- var entries [] * kv
283+ var entries entrySlice
278284 for _ , kv := range vals {
279285 entries = append (entries , kv )
280286 }
281- slices . SortFunc (entries , ( * kv ). less )
287+ sort . Sort (entries )
282288
283289 // Case 1
284290 start , end := 100 , 200
@@ -331,11 +337,11 @@ func TestRangeProofWithInvalidNonExistentProof(t *testing.T) {
331337// non-existent one.
332338func TestOneElementRangeProof (t * testing.T ) {
333339 trie , vals := randomTrie (4096 )
334- var entries [] * kv
340+ var entries entrySlice
335341 for _ , kv := range vals {
336342 entries = append (entries , kv )
337343 }
338- slices . SortFunc (entries , ( * kv ). less )
344+ sort . Sort (entries )
339345
340346 // One element with existent edge proof, both edge proofs
341347 // point to the SAME key.
@@ -418,11 +424,11 @@ func TestOneElementRangeProof(t *testing.T) {
418424// The edge proofs can be nil.
419425func TestAllElementsProof (t * testing.T ) {
420426 trie , vals := randomTrie (4096 )
421- var entries [] * kv
427+ var entries entrySlice
422428 for _ , kv := range vals {
423429 entries = append (entries , kv )
424430 }
425- slices . SortFunc (entries , ( * kv ). less )
431+ sort . Sort (entries )
426432
427433 var k [][]byte
428434 var v [][]byte
@@ -468,13 +474,13 @@ func TestAllElementsProof(t *testing.T) {
468474func TestSingleSideRangeProof (t * testing.T ) {
469475 for i := 0 ; i < 64 ; i ++ {
470476 trie := NewEmpty (NewDatabase (rawdb .NewMemoryDatabase ()))
471- var entries [] * kv
477+ var entries entrySlice
472478 for i := 0 ; i < 4096 ; i ++ {
473479 value := & kv {randBytes (32 ), randBytes (20 ), false }
474480 trie .MustUpdate (value .k , value .v )
475481 entries = append (entries , value )
476482 }
477- slices . SortFunc (entries , ( * kv ). less )
483+ sort . Sort (entries )
478484
479485 var cases = []int {0 , 1 , 50 , 100 , 1000 , 2000 , len (entries ) - 1 }
480486 for _ , pos := range cases {
@@ -503,13 +509,13 @@ func TestSingleSideRangeProof(t *testing.T) {
503509func TestReverseSingleSideRangeProof (t * testing.T ) {
504510 for i := 0 ; i < 64 ; i ++ {
505511 trie := NewEmpty (NewDatabase (rawdb .NewMemoryDatabase ()))
506- var entries [] * kv
512+ var entries entrySlice
507513 for i := 0 ; i < 4096 ; i ++ {
508514 value := & kv {randBytes (32 ), randBytes (20 ), false }
509515 trie .MustUpdate (value .k , value .v )
510516 entries = append (entries , value )
511517 }
512- slices . SortFunc (entries , ( * kv ). less )
518+ sort . Sort (entries )
513519
514520 var cases = []int {0 , 1 , 50 , 100 , 1000 , 2000 , len (entries ) - 1 }
515521 for _ , pos := range cases {
@@ -539,11 +545,11 @@ func TestReverseSingleSideRangeProof(t *testing.T) {
539545// The prover is expected to detect the error.
540546func TestBadRangeProof (t * testing.T ) {
541547 trie , vals := randomTrie (4096 )
542- var entries [] * kv
548+ var entries entrySlice
543549 for _ , kv := range vals {
544550 entries = append (entries , kv )
545551 }
546- slices . SortFunc (entries , ( * kv ). less )
552+ sort . Sort (entries )
547553
548554 for i := 0 ; i < 500 ; i ++ {
549555 start := mrand .Intn (len (entries ))
@@ -642,11 +648,11 @@ func TestGappedRangeProof(t *testing.T) {
642648// TestSameSideProofs tests the element is not in the range covered by proofs
643649func TestSameSideProofs (t * testing.T ) {
644650 trie , vals := randomTrie (4096 )
645- var entries [] * kv
651+ var entries entrySlice
646652 for _ , kv := range vals {
647653 entries = append (entries , kv )
648654 }
649- slices . SortFunc (entries , ( * kv ). less )
655+ sort . Sort (entries )
650656
651657 pos := 1000
652658 first := decreaseKey (common .CopyBytes (entries [pos ].k ))
@@ -684,13 +690,13 @@ func TestSameSideProofs(t *testing.T) {
684690
685691func TestHasRightElement (t * testing.T ) {
686692 trie := NewEmpty (NewDatabase (rawdb .NewMemoryDatabase ()))
687- var entries [] * kv
693+ var entries entrySlice
688694 for i := 0 ; i < 4096 ; i ++ {
689695 value := & kv {randBytes (32 ), randBytes (20 ), false }
690696 trie .MustUpdate (value .k , value .v )
691697 entries = append (entries , value )
692698 }
693- slices . SortFunc (entries , ( * kv ). less )
699+ sort . Sort (entries )
694700
695701 var cases = []struct {
696702 start int
@@ -758,11 +764,11 @@ func TestHasRightElement(t *testing.T) {
758764// The first edge proof must be a non-existent proof.
759765func TestEmptyRangeProof (t * testing.T ) {
760766 trie , vals := randomTrie (4096 )
761- var entries [] * kv
767+ var entries entrySlice
762768 for _ , kv := range vals {
763769 entries = append (entries , kv )
764770 }
765- slices . SortFunc (entries , ( * kv ). less )
771+ sort . Sort (entries )
766772
767773 var cases = []struct {
768774 pos int
@@ -793,11 +799,11 @@ func TestEmptyRangeProof(t *testing.T) {
793799func TestBloatedProof (t * testing.T ) {
794800 // Use a small trie
795801 trie , kvs := nonRandomTrie (100 )
796- var entries [] * kv
802+ var entries entrySlice
797803 for _ , kv := range kvs {
798804 entries = append (entries , kv )
799805 }
800- slices . SortFunc (entries , ( * kv ). less )
806+ sort . Sort (entries )
801807 var keys [][]byte
802808 var vals [][]byte
803809
@@ -827,11 +833,11 @@ func TestBloatedProof(t *testing.T) {
827833// noop technically, but practically should be rejected.
828834func TestEmptyValueRangeProof (t * testing.T ) {
829835 trie , values := randomTrie (512 )
830- var entries [] * kv
836+ var entries entrySlice
831837 for _ , kv := range values {
832838 entries = append (entries , kv )
833839 }
834- slices . SortFunc (entries , ( * kv ). less )
840+ sort . Sort (entries )
835841
836842 // Create a new entry with a slightly modified key
837843 mid := len (entries ) / 2
@@ -871,11 +877,11 @@ func TestEmptyValueRangeProof(t *testing.T) {
871877// practically should be rejected.
872878func TestAllElementsEmptyValueRangeProof (t * testing.T ) {
873879 trie , values := randomTrie (512 )
874- var entries [] * kv
880+ var entries entrySlice
875881 for _ , kv := range values {
876882 entries = append (entries , kv )
877883 }
878- slices . SortFunc (entries , ( * kv ). less )
884+ sort . Sort (entries )
879885
880886 // Create a new entry with a slightly modified key
881887 mid := len (entries ) / 2
@@ -977,11 +983,11 @@ func BenchmarkVerifyRangeProof5000(b *testing.B) { benchmarkVerifyRangeProof(b,
977983
978984func benchmarkVerifyRangeProof (b * testing.B , size int ) {
979985 trie , vals := randomTrie (8192 )
980- var entries [] * kv
986+ var entries entrySlice
981987 for _ , kv := range vals {
982988 entries = append (entries , kv )
983989 }
984- slices . SortFunc (entries , ( * kv ). less )
990+ sort . Sort (entries )
985991
986992 start := 2
987993 end := start + size
@@ -1014,11 +1020,11 @@ func BenchmarkVerifyRangeNoProof1000(b *testing.B) { benchmarkVerifyRangeNoProof
10141020
10151021func benchmarkVerifyRangeNoProof (b * testing.B , size int ) {
10161022 trie , vals := randomTrie (size )
1017- var entries [] * kv
1023+ var entries entrySlice
10181024 for _ , kv := range vals {
10191025 entries = append (entries , kv )
10201026 }
1021- slices . SortFunc (entries , ( * kv ). less )
1027+ sort . Sort (entries )
10221028
10231029 var keys [][]byte
10241030 var values [][]byte
0 commit comments