2020-11-09 14:08:12 +00:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package stacktrie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
|
2022-11-28 13:31:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2023-05-09 07:11:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2022-11-28 13:31:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-11-09 14:08:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2023-05-09 07:11:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
2020-11-09 14:08:12 +00:00
|
|
|
"golang.org/x/crypto/sha3"
|
2023-06-19 09:41:31 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2020-11-09 14:08:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fuzzer struct {
|
|
|
|
input io.Reader
|
|
|
|
exhausted bool
|
|
|
|
debugging bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fuzzer) read(size int) []byte {
|
|
|
|
out := make([]byte, size)
|
|
|
|
if _, err := f.input.Read(out); err != nil {
|
|
|
|
f.exhausted = true
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fuzzer) readSlice(min, max int) []byte {
|
|
|
|
var a uint16
|
|
|
|
binary.Read(f.input, binary.LittleEndian, &a)
|
|
|
|
size := min + int(a)%(max-min)
|
|
|
|
out := make([]byte, size)
|
|
|
|
if _, err := f.input.Read(out); err != nil {
|
|
|
|
f.exhausted = true
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// spongeDb is a dummy db backend which accumulates writes in a sponge
|
|
|
|
type spongeDb struct {
|
|
|
|
sponge hash.Hash
|
|
|
|
debug bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *spongeDb) Has(key []byte) (bool, error) { panic("implement me") }
|
|
|
|
func (s *spongeDb) Get(key []byte) ([]byte, error) { return nil, errors.New("no such elem") }
|
|
|
|
func (s *spongeDb) Delete(key []byte) error { panic("implement me") }
|
|
|
|
func (s *spongeDb) NewBatch() ethdb.Batch { return &spongeBatch{s} }
|
2022-02-15 13:15:13 +00:00
|
|
|
func (s *spongeDb) NewBatchWithSize(size int) ethdb.Batch { return &spongeBatch{s} }
|
2022-03-10 08:35:22 +00:00
|
|
|
func (s *spongeDb) NewSnapshot() (ethdb.Snapshot, error) { panic("implement me") }
|
2020-11-09 14:08:12 +00:00
|
|
|
func (s *spongeDb) Stat(property string) (string, error) { panic("implement me") }
|
|
|
|
func (s *spongeDb) Compact(start []byte, limit []byte) error { panic("implement me") }
|
|
|
|
func (s *spongeDb) Close() error { return nil }
|
|
|
|
|
|
|
|
func (s *spongeDb) Put(key []byte, value []byte) error {
|
|
|
|
if s.debug {
|
|
|
|
fmt.Printf("db.Put %x : %x\n", key, value)
|
|
|
|
}
|
|
|
|
s.sponge.Write(key)
|
|
|
|
s.sponge.Write(value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s *spongeDb) NewIterator(prefix []byte, start []byte) ethdb.Iterator { panic("implement me") }
|
|
|
|
|
|
|
|
// spongeBatch is a dummy batch which immediately writes to the underlying spongedb
|
|
|
|
type spongeBatch struct {
|
|
|
|
db *spongeDb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *spongeBatch) Put(key, value []byte) error {
|
|
|
|
b.db.Put(key, value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (b *spongeBatch) Delete(key []byte) error { panic("implement me") }
|
|
|
|
func (b *spongeBatch) ValueSize() int { return 100 }
|
|
|
|
func (b *spongeBatch) Write() error { return nil }
|
|
|
|
func (b *spongeBatch) Reset() {}
|
|
|
|
func (b *spongeBatch) Replay(w ethdb.KeyValueWriter) error { return nil }
|
|
|
|
|
|
|
|
type kv struct {
|
|
|
|
k, v []byte
|
|
|
|
}
|
|
|
|
|
2022-10-04 06:44:05 +00:00
|
|
|
// Fuzz is the fuzzing entry-point.
|
2020-11-09 14:08:12 +00:00
|
|
|
// The function must return
|
2022-09-10 11:25:40 +00:00
|
|
|
//
|
|
|
|
// - 1 if the fuzzer should increase priority of the
|
|
|
|
// given input during subsequent fuzzing (for example, the input is lexically
|
|
|
|
// correct and was parsed successfully);
|
|
|
|
// - -1 if the input must not be added to corpus even if gives new coverage; and
|
|
|
|
// - 0 otherwise
|
|
|
|
//
|
2020-11-09 14:08:12 +00:00
|
|
|
// other values are reserved for future use.
|
|
|
|
func Fuzz(data []byte) int {
|
|
|
|
f := fuzzer{
|
|
|
|
input: bytes.NewReader(data),
|
|
|
|
exhausted: false,
|
|
|
|
}
|
|
|
|
return f.fuzz()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debug(data []byte) int {
|
|
|
|
f := fuzzer{
|
|
|
|
input: bytes.NewReader(data),
|
|
|
|
exhausted: false,
|
|
|
|
debugging: true,
|
|
|
|
}
|
|
|
|
return f.fuzz()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fuzzer) fuzz() int {
|
|
|
|
// This spongeDb is used to check the sequence of disk-db-writes
|
|
|
|
var (
|
2022-11-28 13:31:28 +00:00
|
|
|
spongeA = &spongeDb{sponge: sha3.NewLegacyKeccak256()}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
dbA = trie.NewDatabase(rawdb.NewDatabase(spongeA), nil)
|
2022-11-28 13:31:28 +00:00
|
|
|
trieA = trie.NewEmpty(dbA)
|
|
|
|
spongeB = &spongeDb{sponge: sha3.NewLegacyKeccak256()}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 19:21:36 +00:00
|
|
|
dbB = trie.NewDatabase(rawdb.NewDatabase(spongeB), nil)
|
2022-11-28 13:31:28 +00:00
|
|
|
trieB = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
|
2023-02-06 15:28:40 +00:00
|
|
|
rawdb.WriteTrieNode(spongeB, owner, path, hash, blob, dbB.Scheme())
|
2022-11-28 13:31:28 +00:00
|
|
|
})
|
2023-06-19 09:41:31 +00:00
|
|
|
vals []kv
|
2020-11-09 14:08:12 +00:00
|
|
|
useful bool
|
|
|
|
maxElements = 10000
|
2020-11-13 11:36:38 +00:00
|
|
|
// operate on unique keys only
|
|
|
|
keys = make(map[string]struct{})
|
2020-11-09 14:08:12 +00:00
|
|
|
)
|
|
|
|
// Fill the trie with elements
|
|
|
|
for i := 0; !f.exhausted && i < maxElements; i++ {
|
|
|
|
k := f.read(32)
|
|
|
|
v := f.readSlice(1, 500)
|
|
|
|
if f.exhausted {
|
|
|
|
// If it was exhausted while reading, the value may be all zeroes,
|
|
|
|
// thus 'deletion' which is not supported on stacktrie
|
|
|
|
break
|
|
|
|
}
|
2020-11-13 11:36:38 +00:00
|
|
|
if _, present := keys[string(k)]; present {
|
|
|
|
// This key is a duplicate, ignore it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
keys[string(k)] = struct{}{}
|
2020-11-09 14:08:12 +00:00
|
|
|
vals = append(vals, kv{k: k, v: v})
|
2023-04-20 10:57:24 +00:00
|
|
|
trieA.MustUpdate(k, v)
|
2020-11-09 14:08:12 +00:00
|
|
|
useful = true
|
|
|
|
}
|
|
|
|
if !useful {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
// Flush trie -> database
|
2023-06-27 12:36:38 +00:00
|
|
|
rootA, nodes, err := trieA.Commit(false)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-08-04 08:03:20 +00:00
|
|
|
if nodes != nil {
|
2023-07-24 10:22:09 +00:00
|
|
|
dbA.Update(rootA, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
|
2022-08-04 08:03:20 +00:00
|
|
|
}
|
2020-11-09 14:08:12 +00:00
|
|
|
// Flush memdb -> disk (sponge)
|
2023-02-08 11:14:34 +00:00
|
|
|
dbA.Commit(rootA, false)
|
2020-11-09 14:08:12 +00:00
|
|
|
|
|
|
|
// Stacktrie requires sorted insertion
|
2023-08-11 22:04:12 +00:00
|
|
|
slices.SortFunc(vals, func(a, b kv) int {
|
|
|
|
return bytes.Compare(a.k, b.k)
|
2023-06-19 09:41:31 +00:00
|
|
|
})
|
2020-11-09 14:08:12 +00:00
|
|
|
for _, kv := range vals {
|
|
|
|
if f.debugging {
|
2022-07-04 08:03:32 +00:00
|
|
|
fmt.Printf("{\"%#x\" , \"%#x\"} // stacktrie.Update\n", kv.k, kv.v)
|
2020-11-09 14:08:12 +00:00
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
trieB.MustUpdate(kv.k, kv.v)
|
2020-11-09 14:08:12 +00:00
|
|
|
}
|
|
|
|
rootB := trieB.Hash()
|
2023-02-09 13:56:59 +00:00
|
|
|
trieB.Commit()
|
2020-11-09 14:08:12 +00:00
|
|
|
if rootA != rootB {
|
|
|
|
panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootB))
|
|
|
|
}
|
|
|
|
sumA := spongeA.sponge.Sum(nil)
|
|
|
|
sumB := spongeB.sponge.Sum(nil)
|
|
|
|
if !bytes.Equal(sumA, sumB) {
|
|
|
|
panic(fmt.Sprintf("sequence differ: (trie) %x != %x (stacktrie)", sumA, sumB))
|
|
|
|
}
|
2022-11-28 13:31:28 +00:00
|
|
|
|
|
|
|
// Ensure all the nodes are persisted correctly
|
|
|
|
var (
|
|
|
|
nodeset = make(map[string][]byte) // path -> blob
|
|
|
|
trieC = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
|
|
|
|
if crypto.Keccak256Hash(blob) != hash {
|
|
|
|
panic("invalid node blob")
|
|
|
|
}
|
|
|
|
if owner != (common.Hash{}) {
|
|
|
|
panic("invalid node owner")
|
|
|
|
}
|
|
|
|
nodeset[string(path)] = common.CopyBytes(blob)
|
|
|
|
})
|
|
|
|
checked int
|
|
|
|
)
|
|
|
|
for _, kv := range vals {
|
2023-04-20 10:57:24 +00:00
|
|
|
trieC.MustUpdate(kv.k, kv.v)
|
2022-11-28 13:31:28 +00:00
|
|
|
}
|
|
|
|
rootC, _ := trieC.Commit()
|
|
|
|
if rootA != rootC {
|
|
|
|
panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootC))
|
|
|
|
}
|
|
|
|
trieA, _ = trie.New(trie.TrieID(rootA), dbA)
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 19:31:45 +00:00
|
|
|
iterA := trieA.MustNodeIterator(nil)
|
2022-11-28 13:31:28 +00:00
|
|
|
for iterA.Next(true) {
|
|
|
|
if iterA.Hash() == (common.Hash{}) {
|
|
|
|
if _, present := nodeset[string(iterA.Path())]; present {
|
|
|
|
panic("unexpected tiny node")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nodeBlob, present := nodeset[string(iterA.Path())]
|
|
|
|
if !present {
|
|
|
|
panic("missing node")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(nodeBlob, iterA.NodeBlob()) {
|
|
|
|
panic("node blob is not matched")
|
|
|
|
}
|
|
|
|
checked += 1
|
|
|
|
}
|
|
|
|
if checked != len(nodeset) {
|
|
|
|
panic("node number is not matched")
|
|
|
|
}
|
2020-11-09 14:08:12 +00:00
|
|
|
return 1
|
|
|
|
}
|