tests, trie: use slices package for sorting (#27496)

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
Dan Laine
2023-06-19 11:41:31 +02:00
committed by GitHub
co-authored by Felix Lange
parent 87e510d963
commit 50ecb16de0
5 changed files with 52 additions and 67 deletions
@@ -21,12 +21,12 @@ import (
"encoding/binary"
"fmt"
"io"
"sort"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/trie"
"golang.org/x/exp/slices"
)
type kv struct {
@@ -34,12 +34,6 @@ type kv struct {
t bool
}
type entrySlice []*kv
func (p entrySlice) Len() int { return len(p) }
func (p entrySlice) Less(i, j int) bool { return bytes.Compare(p[i].k, p[j].k) < 0 }
func (p entrySlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type fuzzer struct {
input io.Reader
exhausted bool
@@ -97,14 +91,16 @@ func (f *fuzzer) fuzz() int {
if f.exhausted {
return 0 // input too short
}
var entries entrySlice
var entries []*kv
for _, kv := range vals {
entries = append(entries, kv)
}
if len(entries) <= 1 {
return 0
}
sort.Sort(entries)
slices.SortFunc(entries, func(a, b *kv) bool {
return bytes.Compare(a.k, b.k) < 0
})
var ok = 0
for {
+5 -16
View File
@@ -23,7 +23,6 @@ import (
"fmt"
"hash"
"io"
"sort"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
@@ -33,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
"golang.org/x/crypto/sha3"
"golang.org/x/exp/slices"
)
type fuzzer struct {
@@ -104,19 +104,6 @@ func (b *spongeBatch) Replay(w ethdb.KeyValueWriter) error { return nil }
type kv struct {
k, v []byte
}
type kvs []kv
func (k kvs) Len() int {
return len(k)
}
func (k kvs) Less(i, j int) bool {
return bytes.Compare(k[i].k, k[j].k) < 0
}
func (k kvs) Swap(i, j int) {
k[j], k[i] = k[i], k[j]
}
// Fuzz is the fuzzing entry-point.
// The function must return
@@ -156,7 +143,7 @@ func (f *fuzzer) fuzz() int {
trieB = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
rawdb.WriteTrieNode(spongeB, owner, path, hash, blob, dbB.Scheme())
})
vals kvs
vals []kv
useful bool
maxElements = 10000
// operate on unique keys only
@@ -192,7 +179,9 @@ func (f *fuzzer) fuzz() int {
dbA.Commit(rootA, false)
// Stacktrie requires sorted insertion
sort.Sort(vals)
slices.SortFunc(vals, func(a, b kv) bool {
return bytes.Compare(a.k, b.k) < 0
})
for _, kv := range vals {
if f.debugging {
fmt.Printf("{\"%#x\" , \"%#x\"} // stacktrie.Update\n", kv.k, kv.v)