2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2014-10-31 08:45:03 -05:00
|
|
|
package trie
|
2014-02-14 16:56:09 -06:00
|
|
|
|
|
|
|
import (
|
2014-11-18 12:52:45 -06:00
|
|
|
"bytes"
|
2015-06-30 07:54:37 -05:00
|
|
|
"encoding/binary"
|
2020-09-30 06:45:56 -05:00
|
|
|
"errors"
|
2014-05-25 17:09:38 -05:00
|
|
|
"fmt"
|
2020-09-30 06:45:56 -05:00
|
|
|
"hash"
|
2023-11-14 07:34:29 -06:00
|
|
|
"io"
|
2016-09-29 09:51:32 -05:00
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
2024-02-13 07:49:53 -06:00
|
|
|
"sort"
|
2014-11-18 12:52:45 -06:00
|
|
|
"testing"
|
2016-09-29 09:51:32 -05:00
|
|
|
"testing/quick"
|
2014-11-15 20:21:55 -06:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2015-03-16 05:27:38 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-03-31 02:28:32 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2021-09-28 03:48:07 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-10-13 03:10:02 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-09-30 06:45:56 -05:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2017-10-13 03:10:02 -05:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2023-05-09 02:11:04 -05:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
2024-01-23 07:51:58 -06:00
|
|
|
"github.com/holiman/uint256"
|
2020-09-30 06:45:56 -05:00
|
|
|
"golang.org/x/crypto/sha3"
|
2014-02-14 16:56:09 -06:00
|
|
|
)
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
func init() {
|
|
|
|
spew.Config.Indent = " "
|
2017-06-20 11:26:09 -05:00
|
|
|
spew.Config.DisableMethods = false
|
2014-02-15 06:21:11 -06:00
|
|
|
}
|
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
func TestEmptyTrie(t *testing.T) {
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2015-01-08 04:47:04 -06:00
|
|
|
res := trie.Hash()
|
2023-02-21 05:12:27 -06:00
|
|
|
exp := types.EmptyRootHash
|
2019-07-22 02:30:09 -05:00
|
|
|
if res != exp {
|
2015-01-08 04:47:04 -06:00
|
|
|
t.Errorf("expected %x got %x", exp, res)
|
|
|
|
}
|
2014-02-15 06:21:11 -06:00
|
|
|
}
|
|
|
|
|
2015-03-19 04:57:02 -05:00
|
|
|
func TestNull(t *testing.T) {
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2015-03-19 04:57:02 -05:00
|
|
|
key := make([]byte, 32)
|
2017-01-09 04:16:06 -06:00
|
|
|
value := []byte("test")
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(key, value)
|
|
|
|
if !bytes.Equal(trie.MustGet(key), value) {
|
2017-01-09 04:16:06 -06:00
|
|
|
t.Fatal("wrong value")
|
|
|
|
}
|
2015-03-19 04:57:02 -05:00
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
func TestMissingRoot(t *testing.T) {
|
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 14:21:36 -05:00
|
|
|
testMissingRoot(t, rawdb.HashScheme)
|
|
|
|
testMissingRoot(t, rawdb.PathScheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMissingRoot(t *testing.T, scheme string) {
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
root := common.HexToHash("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
|
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 14:21:36 -05:00
|
|
|
trie, err := New(TrieID(root), newTestDatabase(rawdb.NewMemoryDatabase(), scheme))
|
2015-07-05 18:19:48 -05:00
|
|
|
if trie != nil {
|
|
|
|
t.Error("New returned non-nil trie for invalid root")
|
|
|
|
}
|
2015-11-25 11:28:21 -06:00
|
|
|
if _, ok := err.(*MissingNodeError); !ok {
|
2016-04-15 04:06:57 -05:00
|
|
|
t.Errorf("New returned wrong error: %v", err)
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 02:11:04 -05:00
|
|
|
func TestMissingNode(t *testing.T) {
|
|
|
|
testMissingNode(t, false, rawdb.HashScheme)
|
2023-08-01 07:17:32 -05:00
|
|
|
testMissingNode(t, false, rawdb.PathScheme)
|
2023-05-09 02:11:04 -05:00
|
|
|
testMissingNode(t, true, rawdb.HashScheme)
|
2023-08-01 07:17:32 -05:00
|
|
|
testMissingNode(t, true, rawdb.PathScheme)
|
2023-05-09 02:11:04 -05:00
|
|
|
}
|
2018-02-05 10:40:32 -06:00
|
|
|
|
2023-05-09 02:11:04 -05:00
|
|
|
func testMissingNode(t *testing.T, memonly bool, scheme string) {
|
2022-11-28 07:31:28 -06:00
|
|
|
diskdb := rawdb.NewMemoryDatabase()
|
2023-05-09 02:11:04 -05:00
|
|
|
triedb := newTestDatabase(diskdb, scheme)
|
2018-02-05 10:40:32 -06:00
|
|
|
|
2022-06-06 10:14:55 -05:00
|
|
|
trie := NewEmpty(triedb)
|
2015-11-25 11:28:21 -06:00
|
|
|
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer")
|
|
|
|
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf")
|
2023-06-27 07:36:38 -05:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2024-02-13 07:49:53 -06:00
|
|
|
triedb.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
|
2023-05-09 02:11:04 -05:00
|
|
|
|
2018-02-05 10:40:32 -06:00
|
|
|
if !memonly {
|
2024-02-13 07:49:53 -06:00
|
|
|
triedb.Commit(root)
|
2018-02-05 10:40:32 -06:00
|
|
|
}
|
2015-11-25 11:28:21 -06:00
|
|
|
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
trie, _ = New(TrieID(root), triedb)
|
2023-04-20 05:57:24 -05:00
|
|
|
_, err := trie.Get([]byte("120000"))
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
trie, _ = New(TrieID(root), triedb)
|
2023-04-20 05:57:24 -05:00
|
|
|
_, err = trie.Get([]byte("120099"))
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
trie, _ = New(TrieID(root), triedb)
|
2023-04-20 05:57:24 -05:00
|
|
|
_, err = trie.Get([]byte("123456"))
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
trie, _ = New(TrieID(root), triedb)
|
2023-04-20 05:57:24 -05:00
|
|
|
err = trie.Update([]byte("120099"), []byte("zxcvzxcvzxcvzxcvzxcvzxcvzxcvzxcv"))
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
trie, _ = New(TrieID(root), triedb)
|
2023-04-20 05:57:24 -05:00
|
|
|
err = trie.Delete([]byte("123456"))
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-05-09 02:11:04 -05:00
|
|
|
var (
|
|
|
|
path []byte
|
|
|
|
hash = common.HexToHash("0xe1d943cc8f061a0c0b98162830b970395ac9315654824bf21b73b891365262f9")
|
|
|
|
)
|
|
|
|
for p, n := range nodes.Nodes {
|
|
|
|
if n.Hash == hash {
|
|
|
|
path = common.CopyBytes([]byte(p))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trie, _ = New(TrieID(root), triedb)
|
2018-02-05 10:40:32 -06:00
|
|
|
if memonly {
|
2023-05-09 02:11:04 -05:00
|
|
|
trie.reader.banned = map[string]struct{}{string(path): {}}
|
2018-02-05 10:40:32 -06:00
|
|
|
} else {
|
2023-05-09 02:11:04 -05:00
|
|
|
rawdb.DeleteTrieNode(diskdb, common.Hash{}, path, hash, scheme)
|
2018-02-05 10:40:32 -06:00
|
|
|
}
|
2015-11-25 11:28:21 -06:00
|
|
|
|
2023-04-20 05:57:24 -05:00
|
|
|
_, err = trie.Get([]byte("120000"))
|
2015-11-25 11:28:21 -06:00
|
|
|
if _, ok := err.(*MissingNodeError); !ok {
|
|
|
|
t.Errorf("Wrong error: %v", err)
|
|
|
|
}
|
2023-04-20 05:57:24 -05:00
|
|
|
_, err = trie.Get([]byte("120099"))
|
2015-11-25 11:28:21 -06:00
|
|
|
if _, ok := err.(*MissingNodeError); !ok {
|
|
|
|
t.Errorf("Wrong error: %v", err)
|
|
|
|
}
|
2023-04-20 05:57:24 -05:00
|
|
|
_, err = trie.Get([]byte("123456"))
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
2023-04-20 05:57:24 -05:00
|
|
|
err = trie.Update([]byte("120099"), []byte("zxcv"))
|
2015-11-25 11:28:21 -06:00
|
|
|
if _, ok := err.(*MissingNodeError); !ok {
|
|
|
|
t.Errorf("Wrong error: %v", err)
|
|
|
|
}
|
2023-04-20 05:57:24 -05:00
|
|
|
err = trie.Delete([]byte("123456"))
|
2015-11-25 11:28:21 -06:00
|
|
|
if _, ok := err.(*MissingNodeError); !ok {
|
|
|
|
t.Errorf("Wrong error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
func TestInsert(t *testing.T) {
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2014-02-15 06:21:11 -06:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
updateString(trie, "doe", "reindeer")
|
|
|
|
updateString(trie, "dog", "puppy")
|
|
|
|
updateString(trie, "dogglesworth", "cat")
|
2014-02-15 06:21:11 -06:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
exp := common.HexToHash("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3")
|
2015-01-08 04:47:04 -06:00
|
|
|
root := trie.Hash()
|
2015-07-05 18:19:48 -05:00
|
|
|
if root != exp {
|
2020-01-17 05:59:45 -06:00
|
|
|
t.Errorf("case 1: exp %x got %x", exp, root)
|
2015-01-08 04:47:04 -06:00
|
|
|
}
|
2014-02-20 07:40:00 -06:00
|
|
|
|
2024-02-13 07:49:53 -06:00
|
|
|
trie = NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2015-07-05 18:19:48 -05:00
|
|
|
updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
2014-02-20 07:40:00 -06:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab")
|
2023-06-27 07:36:38 -05:00
|
|
|
root, _, _ = trie.Commit(false)
|
2015-07-05 18:19:48 -05:00
|
|
|
if root != exp {
|
2020-01-17 05:59:45 -06:00
|
|
|
t.Errorf("case 2: exp %x got %x", exp, root)
|
2015-01-08 04:47:04 -06:00
|
|
|
}
|
2014-02-20 07:40:00 -06:00
|
|
|
}
|
2014-02-24 05:11:00 -06:00
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
func TestGet(t *testing.T) {
|
2024-02-13 07:49:53 -06:00
|
|
|
db := newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)
|
2022-08-04 03:03:20 -05:00
|
|
|
trie := NewEmpty(db)
|
2015-07-05 18:19:48 -05:00
|
|
|
updateString(trie, "doe", "reindeer")
|
|
|
|
updateString(trie, "dog", "puppy")
|
|
|
|
updateString(trie, "dogglesworth", "cat")
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
res := getString(trie, "dog")
|
|
|
|
if !bytes.Equal(res, []byte("puppy")) {
|
|
|
|
t.Errorf("expected puppy got %x", res)
|
|
|
|
}
|
|
|
|
unknown := getString(trie, "unknown")
|
|
|
|
if unknown != nil {
|
|
|
|
t.Errorf("expected nil got %x", unknown)
|
|
|
|
}
|
|
|
|
if i == 1 {
|
|
|
|
return
|
|
|
|
}
|
2023-06-27 07:36:38 -05:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2024-02-13 07:49:53 -06:00
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
trie, _ = New(TrieID(root), db)
|
2014-06-17 11:05:46 -05:00
|
|
|
}
|
2014-06-30 06:08:00 -05:00
|
|
|
}
|
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
func TestDelete(t *testing.T) {
|
2024-02-13 07:49:53 -06:00
|
|
|
db := newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)
|
|
|
|
trie := NewEmpty(db)
|
2015-01-08 04:47:04 -06:00
|
|
|
vals := []struct{ k, v string }{
|
|
|
|
{"do", "verb"},
|
|
|
|
{"ether", "wookiedoo"},
|
|
|
|
{"horse", "stallion"},
|
|
|
|
{"shaman", "horse"},
|
|
|
|
{"doge", "coin"},
|
|
|
|
{"ether", ""},
|
|
|
|
{"dog", "puppy"},
|
|
|
|
{"shaman", ""},
|
2014-06-17 11:05:46 -05:00
|
|
|
}
|
2015-01-08 04:47:04 -06:00
|
|
|
for _, val := range vals {
|
|
|
|
if val.v != "" {
|
2015-07-05 18:19:48 -05:00
|
|
|
updateString(trie, val.k, val.v)
|
2015-01-08 04:47:04 -06:00
|
|
|
} else {
|
2015-07-05 18:19:48 -05:00
|
|
|
deleteString(trie, val.k)
|
2015-01-08 04:47:04 -06:00
|
|
|
}
|
2014-06-30 06:08:00 -05:00
|
|
|
}
|
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
hash := trie.Hash()
|
2015-07-05 18:19:48 -05:00
|
|
|
exp := common.HexToHash("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84")
|
|
|
|
if hash != exp {
|
2015-01-08 04:47:04 -06:00
|
|
|
t.Errorf("expected %x got %x", exp, hash)
|
2014-06-30 06:08:00 -05:00
|
|
|
}
|
2015-01-08 04:47:04 -06:00
|
|
|
}
|
2014-05-26 18:08:51 -05:00
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
func TestEmptyValues(t *testing.T) {
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2014-06-30 06:08:00 -05:00
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
vals := []struct{ k, v string }{
|
|
|
|
{"do", "verb"},
|
|
|
|
{"ether", "wookiedoo"},
|
|
|
|
{"horse", "stallion"},
|
|
|
|
{"shaman", "horse"},
|
|
|
|
{"doge", "coin"},
|
|
|
|
{"ether", ""},
|
|
|
|
{"dog", "puppy"},
|
|
|
|
{"shaman", ""},
|
2014-06-30 06:08:00 -05:00
|
|
|
}
|
2015-01-08 04:47:04 -06:00
|
|
|
for _, val := range vals {
|
2015-07-05 18:19:48 -05:00
|
|
|
updateString(trie, val.k, val.v)
|
2014-07-01 02:55:20 -05:00
|
|
|
}
|
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
hash := trie.Hash()
|
2015-07-05 18:19:48 -05:00
|
|
|
exp := common.HexToHash("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84")
|
|
|
|
if hash != exp {
|
2015-01-08 04:47:04 -06:00
|
|
|
t.Errorf("expected %x got %x", exp, hash)
|
2014-06-17 11:05:46 -05:00
|
|
|
}
|
2014-07-01 02:55:20 -05:00
|
|
|
}
|
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
func TestReplication(t *testing.T) {
|
2024-02-13 07:49:53 -06:00
|
|
|
db := newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)
|
2023-05-09 02:11:04 -05:00
|
|
|
trie := NewEmpty(db)
|
2015-01-08 04:47:04 -06:00
|
|
|
vals := []struct{ k, v string }{
|
|
|
|
{"do", "verb"},
|
|
|
|
{"ether", "wookiedoo"},
|
|
|
|
{"horse", "stallion"},
|
|
|
|
{"shaman", "horse"},
|
|
|
|
{"doge", "coin"},
|
|
|
|
{"dog", "puppy"},
|
|
|
|
{"somethingveryoddindeedthis is", "myothernodedata"},
|
2014-06-17 11:05:46 -05:00
|
|
|
}
|
2015-01-08 04:47:04 -06:00
|
|
|
for _, val := range vals {
|
2015-07-05 18:19:48 -05:00
|
|
|
updateString(trie, val.k, val.v)
|
2015-01-08 04:47:04 -06:00
|
|
|
}
|
2023-06-27 07:36:38 -05:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2024-02-13 07:49:53 -06:00
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
|
2014-07-15 08:29:54 -05:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// create a new trie on top of the database and check that lookups work.
|
2023-05-09 02:11:04 -05:00
|
|
|
trie2, err := New(TrieID(root), db)
|
2015-07-05 18:19:48 -05:00
|
|
|
if err != nil {
|
2023-05-09 02:11:04 -05:00
|
|
|
t.Fatalf("can't recreate trie at %x: %v", root, err)
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
for _, kv := range vals {
|
|
|
|
if string(getString(trie2, kv.k)) != kv.v {
|
|
|
|
t.Errorf("trie2 doesn't have %q => %q", kv.k, kv.v)
|
|
|
|
}
|
|
|
|
}
|
2023-06-27 07:36:38 -05:00
|
|
|
hash, nodes, _ := trie2.Commit(false)
|
2023-05-09 02:11:04 -05:00
|
|
|
if hash != root {
|
|
|
|
t.Errorf("root failure. expected %x got %x", root, hash)
|
2015-01-08 04:47:04 -06:00
|
|
|
}
|
2014-07-17 04:21:18 -05:00
|
|
|
|
2022-08-04 03:03:20 -05:00
|
|
|
// recreate the trie after commit
|
|
|
|
if nodes != nil {
|
2024-02-13 07:49:53 -06:00
|
|
|
db.Update(hash, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
|
2022-08-04 03:03:20 -05:00
|
|
|
}
|
2023-05-09 02:11:04 -05:00
|
|
|
trie2, err = New(TrieID(hash), db)
|
2022-08-04 03:03:20 -05:00
|
|
|
if err != nil {
|
2023-05-09 02:11:04 -05:00
|
|
|
t.Fatalf("can't recreate trie at %x: %v", hash, err)
|
2022-08-04 03:03:20 -05:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
// perform some insertions on the new trie.
|
|
|
|
vals2 := []struct{ k, v string }{
|
2015-01-08 04:47:04 -06:00
|
|
|
{"do", "verb"},
|
|
|
|
{"ether", "wookiedoo"},
|
|
|
|
{"horse", "stallion"},
|
2015-07-05 18:19:48 -05:00
|
|
|
// {"shaman", "horse"},
|
|
|
|
// {"doge", "coin"},
|
|
|
|
// {"ether", ""},
|
|
|
|
// {"dog", "puppy"},
|
|
|
|
// {"somethingveryoddindeedthis is", "myothernodedata"},
|
|
|
|
// {"shaman", ""},
|
2014-10-10 10:00:06 -05:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
for _, val := range vals2 {
|
|
|
|
updateString(trie2, val.k, val.v)
|
2014-10-10 10:00:06 -05:00
|
|
|
}
|
2023-05-09 02:11:04 -05:00
|
|
|
if trie2.Hash() != hash {
|
|
|
|
t.Errorf("root failure. expected %x got %x", hash, hash)
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
}
|
2014-11-14 15:19:50 -06:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
func TestLargeValue(t *testing.T) {
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate([]byte("key1"), []byte{99, 99, 99, 99})
|
|
|
|
trie.MustUpdate([]byte("key2"), bytes.Repeat([]byte{1}, 32))
|
2015-07-05 18:19:48 -05:00
|
|
|
trie.Hash()
|
2016-09-29 09:51:32 -05:00
|
|
|
}
|
|
|
|
|
2024-02-05 15:16:32 -06:00
|
|
|
// TestRandomCases tests some cases that were found via random fuzzing
|
2020-01-17 05:59:45 -06:00
|
|
|
func TestRandomCases(t *testing.T) {
|
2021-02-02 03:32:44 -06:00
|
|
|
var rt = []randTestStep{
|
2020-01-17 05:59:45 -06:00
|
|
|
{op: 6, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 0
|
|
|
|
{op: 6, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 1
|
|
|
|
{op: 0, key: common.Hex2Bytes("d51b182b95d677e5f1c82508c0228de96b73092d78ce78b2230cd948674f66fd1483bd"), value: common.Hex2Bytes("0000000000000002")}, // step 2
|
|
|
|
{op: 2, key: common.Hex2Bytes("c2a38512b83107d665c65235b0250002882ac2022eb00711552354832c5f1d030d0e408e"), value: common.Hex2Bytes("")}, // step 3
|
|
|
|
{op: 3, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 4
|
|
|
|
{op: 3, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 5
|
|
|
|
{op: 6, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 6
|
|
|
|
{op: 3, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 7
|
|
|
|
{op: 0, key: common.Hex2Bytes("c2a38512b83107d665c65235b0250002882ac2022eb00711552354832c5f1d030d0e408e"), value: common.Hex2Bytes("0000000000000008")}, // step 8
|
|
|
|
{op: 0, key: common.Hex2Bytes("d51b182b95d677e5f1c82508c0228de96b73092d78ce78b2230cd948674f66fd1483bd"), value: common.Hex2Bytes("0000000000000009")}, // step 9
|
|
|
|
{op: 2, key: common.Hex2Bytes("fd"), value: common.Hex2Bytes("")}, // step 10
|
|
|
|
{op: 6, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 11
|
|
|
|
{op: 6, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 12
|
|
|
|
{op: 0, key: common.Hex2Bytes("fd"), value: common.Hex2Bytes("000000000000000d")}, // step 13
|
|
|
|
{op: 6, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 14
|
|
|
|
{op: 1, key: common.Hex2Bytes("c2a38512b83107d665c65235b0250002882ac2022eb00711552354832c5f1d030d0e408e"), value: common.Hex2Bytes("")}, // step 15
|
|
|
|
{op: 3, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 16
|
|
|
|
{op: 0, key: common.Hex2Bytes("c2a38512b83107d665c65235b0250002882ac2022eb00711552354832c5f1d030d0e408e"), value: common.Hex2Bytes("0000000000000011")}, // step 17
|
|
|
|
{op: 5, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 18
|
|
|
|
{op: 3, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 19
|
|
|
|
{op: 0, key: common.Hex2Bytes("d51b182b95d677e5f1c82508c0228de96b73092d78ce78b2230cd948674f66fd1483bd"), value: common.Hex2Bytes("0000000000000014")}, // step 20
|
|
|
|
{op: 0, key: common.Hex2Bytes("d51b182b95d677e5f1c82508c0228de96b73092d78ce78b2230cd948674f66fd1483bd"), value: common.Hex2Bytes("0000000000000015")}, // step 21
|
|
|
|
{op: 0, key: common.Hex2Bytes("c2a38512b83107d665c65235b0250002882ac2022eb00711552354832c5f1d030d0e408e"), value: common.Hex2Bytes("0000000000000016")}, // step 22
|
|
|
|
{op: 5, key: common.Hex2Bytes(""), value: common.Hex2Bytes("")}, // step 23
|
|
|
|
{op: 1, key: common.Hex2Bytes("980c393656413a15c8da01978ed9f89feb80b502f58f2d640e3a2f5f7a99a7018f1b573befd92053ac6f78fca4a87268"), value: common.Hex2Bytes("")}, // step 24
|
|
|
|
{op: 1, key: common.Hex2Bytes("fd"), value: common.Hex2Bytes("")}, // step 25
|
|
|
|
}
|
2023-11-14 07:34:29 -06:00
|
|
|
if err := runRandTest(rt); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-01-17 05:59:45 -06:00
|
|
|
}
|
|
|
|
|
2016-10-17 09:13:50 -05:00
|
|
|
// randTest performs random trie operations.
|
|
|
|
// Instances of this test are created by Generate.
|
|
|
|
type randTest []randTestStep
|
|
|
|
|
2023-11-25 07:56:22 -06:00
|
|
|
// compile-time interface check
|
|
|
|
var _ quick.Generator = (randTest)(nil)
|
|
|
|
|
2016-09-29 09:51:32 -05:00
|
|
|
type randTestStep struct {
|
|
|
|
op int
|
|
|
|
key []byte // for opUpdate, opDelete, opGet
|
|
|
|
value []byte // for opUpdate
|
2017-06-20 11:26:09 -05:00
|
|
|
err error // for debugging
|
2016-09-29 09:51:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
opUpdate = iota
|
|
|
|
opDelete
|
|
|
|
opGet
|
|
|
|
opHash
|
2022-08-04 03:03:20 -05:00
|
|
|
opCommit
|
2016-09-29 09:51:32 -05:00
|
|
|
opItercheckhash
|
2022-03-31 02:28:32 -05:00
|
|
|
opNodeDiff
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
opProve
|
2016-09-29 09:51:32 -05:00
|
|
|
opMax // boundary value, not an actual op
|
|
|
|
)
|
|
|
|
|
|
|
|
func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
|
2023-11-14 07:34:29 -06:00
|
|
|
var finishedFn = func() bool {
|
|
|
|
size--
|
2023-11-25 07:56:22 -06:00
|
|
|
return size == 0
|
2023-11-14 07:34:29 -06:00
|
|
|
}
|
|
|
|
return reflect.ValueOf(generateSteps(finishedFn, r))
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSteps(finished func() bool, r io.Reader) randTest {
|
2016-09-29 09:51:32 -05:00
|
|
|
var allKeys [][]byte
|
2023-11-14 07:34:29 -06:00
|
|
|
var one = []byte{0}
|
2016-09-29 09:51:32 -05:00
|
|
|
genKey := func() []byte {
|
2023-11-14 07:34:29 -06:00
|
|
|
r.Read(one)
|
|
|
|
if len(allKeys) < 2 || one[0]%100 > 90 {
|
2016-09-29 09:51:32 -05:00
|
|
|
// new key
|
2023-11-14 07:34:29 -06:00
|
|
|
size := one[0] % 50
|
|
|
|
key := make([]byte, size)
|
2017-03-22 13:48:51 -05:00
|
|
|
r.Read(key)
|
2016-09-29 09:51:32 -05:00
|
|
|
allKeys = append(allKeys, key)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
// use existing key
|
2023-11-14 07:34:29 -06:00
|
|
|
idx := int(one[0]) % len(allKeys)
|
|
|
|
return allKeys[idx]
|
2016-09-29 09:51:32 -05:00
|
|
|
}
|
|
|
|
var steps randTest
|
2023-11-14 07:34:29 -06:00
|
|
|
for !finished() {
|
|
|
|
r.Read(one)
|
|
|
|
step := randTestStep{op: int(one[0]) % opMax}
|
2016-09-29 09:51:32 -05:00
|
|
|
switch step.op {
|
|
|
|
case opUpdate:
|
|
|
|
step.key = genKey()
|
|
|
|
step.value = make([]byte, 8)
|
2023-11-14 07:34:29 -06:00
|
|
|
binary.BigEndian.PutUint64(step.value, uint64(len(steps)))
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
case opGet, opDelete, opProve:
|
2016-09-29 09:51:32 -05:00
|
|
|
step.key = genKey()
|
|
|
|
}
|
|
|
|
steps = append(steps, step)
|
|
|
|
}
|
2023-11-14 07:34:29 -06:00
|
|
|
return steps
|
2016-09-29 09:51:32 -05:00
|
|
|
}
|
|
|
|
|
2023-05-09 02:11:04 -05:00
|
|
|
func verifyAccessList(old *Trie, new *Trie, set *trienode.NodeSet) error {
|
2023-03-14 03:50:53 -05:00
|
|
|
deletes, inserts, updates := diffTries(old, new)
|
|
|
|
|
|
|
|
// Check insertion set
|
|
|
|
for path := range inserts {
|
2023-05-09 02:11:04 -05:00
|
|
|
n, ok := set.Nodes[path]
|
2023-04-26 01:01:54 -05:00
|
|
|
if !ok || n.IsDeleted() {
|
2023-03-14 03:50:53 -05:00
|
|
|
return errors.New("expect new node")
|
|
|
|
}
|
2023-08-01 07:17:32 -05:00
|
|
|
//if len(n.Prev) > 0 {
|
|
|
|
// return errors.New("unexpected origin value")
|
|
|
|
//}
|
2023-03-14 03:50:53 -05:00
|
|
|
}
|
|
|
|
// Check deletion set
|
2023-08-01 07:17:32 -05:00
|
|
|
for path := range deletes {
|
2023-05-09 02:11:04 -05:00
|
|
|
n, ok := set.Nodes[path]
|
2023-04-26 01:01:54 -05:00
|
|
|
if !ok || !n.IsDeleted() {
|
2023-03-14 03:50:53 -05:00
|
|
|
return errors.New("expect deleted node")
|
|
|
|
}
|
2023-08-01 07:17:32 -05:00
|
|
|
//if len(n.Prev) == 0 {
|
|
|
|
// return errors.New("expect origin value")
|
|
|
|
//}
|
|
|
|
//if !bytes.Equal(n.Prev, blob) {
|
|
|
|
// return errors.New("invalid origin value")
|
|
|
|
//}
|
2023-03-14 03:50:53 -05:00
|
|
|
}
|
|
|
|
// Check update set
|
2023-08-01 07:17:32 -05:00
|
|
|
for path := range updates {
|
2023-05-09 02:11:04 -05:00
|
|
|
n, ok := set.Nodes[path]
|
2023-04-26 01:01:54 -05:00
|
|
|
if !ok || n.IsDeleted() {
|
2023-03-14 03:50:53 -05:00
|
|
|
return errors.New("expect updated node")
|
|
|
|
}
|
2023-08-01 07:17:32 -05:00
|
|
|
//if len(n.Prev) == 0 {
|
|
|
|
// return errors.New("expect origin value")
|
|
|
|
//}
|
|
|
|
//if !bytes.Equal(n.Prev, blob) {
|
|
|
|
// return errors.New("invalid origin value")
|
|
|
|
//}
|
2023-03-14 03:50:53 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-14 07:34:29 -06:00
|
|
|
// runRandTestBool coerces error to boolean, for use in quick.Check
|
|
|
|
func runRandTestBool(rt randTest) bool {
|
|
|
|
return runRandTest(rt) == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runRandTest(rt randTest) error {
|
2023-05-09 02:11:04 -05:00
|
|
|
var scheme = rawdb.HashScheme
|
2023-08-01 07:17:32 -05:00
|
|
|
if rand.Intn(2) == 0 {
|
|
|
|
scheme = rawdb.PathScheme
|
|
|
|
}
|
2022-03-31 02:28:32 -05:00
|
|
|
var (
|
2023-05-09 02:11:04 -05:00
|
|
|
origin = types.EmptyRootHash
|
|
|
|
triedb = newTestDatabase(rawdb.NewMemoryDatabase(), scheme)
|
2022-06-06 10:14:55 -05:00
|
|
|
tr = NewEmpty(triedb)
|
|
|
|
values = make(map[string]string) // tracks content of the trie
|
|
|
|
origTrie = NewEmpty(triedb)
|
2022-03-31 02:28:32 -05:00
|
|
|
)
|
2017-06-20 11:26:09 -05:00
|
|
|
for i, step := range rt {
|
2022-03-09 07:45:17 -06:00
|
|
|
// fmt.Printf("{op: %d, key: common.Hex2Bytes(\"%x\"), value: common.Hex2Bytes(\"%x\")}, // step %d\n",
|
|
|
|
// step.op, step.key, step.value, i)
|
|
|
|
|
2016-09-29 09:51:32 -05:00
|
|
|
switch step.op {
|
|
|
|
case opUpdate:
|
2023-04-20 05:57:24 -05:00
|
|
|
tr.MustUpdate(step.key, step.value)
|
2016-09-29 09:51:32 -05:00
|
|
|
values[string(step.key)] = string(step.value)
|
|
|
|
case opDelete:
|
2023-04-20 05:57:24 -05:00
|
|
|
tr.MustDelete(step.key)
|
2016-09-29 09:51:32 -05:00
|
|
|
delete(values, string(step.key))
|
|
|
|
case opGet:
|
2023-04-20 05:57:24 -05:00
|
|
|
v := tr.MustGet(step.key)
|
2016-09-29 09:51:32 -05:00
|
|
|
want := values[string(step.key)]
|
|
|
|
if string(v) != want {
|
2022-07-04 03:03:32 -05:00
|
|
|
rt[i].err = fmt.Errorf("mismatch for key %#x, got %#x want %#x", step.key, v, want)
|
2016-09-29 09:51:32 -05:00
|
|
|
}
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
case opProve:
|
|
|
|
hash := tr.Hash()
|
2023-02-21 05:12:27 -06:00
|
|
|
if hash == types.EmptyRootHash {
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
proofDb := rawdb.NewMemoryDatabase()
|
2023-06-19 09:28:40 -05:00
|
|
|
err := tr.Prove(step.key, proofDb)
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
if err != nil {
|
|
|
|
rt[i].err = fmt.Errorf("failed for proving key %#x, %v", step.key, err)
|
|
|
|
}
|
|
|
|
_, err = VerifyProof(hash, step.key, proofDb)
|
|
|
|
if err != nil {
|
|
|
|
rt[i].err = fmt.Errorf("failed for verifying key %#x, %v", step.key, err)
|
|
|
|
}
|
2016-09-29 09:51:32 -05:00
|
|
|
case opHash:
|
|
|
|
tr.Hash()
|
2022-08-04 03:03:20 -05:00
|
|
|
case opCommit:
|
2023-06-27 07:36:38 -05:00
|
|
|
root, nodes, _ := tr.Commit(true)
|
2022-08-04 03:03:20 -05:00
|
|
|
if nodes != nil {
|
2024-02-13 07:49:53 -06:00
|
|
|
triedb.Update(root, origin, trienode.NewWithNodeSet(nodes))
|
2022-08-04 03:03:20 -05:00
|
|
|
}
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
newtr, err := New(TrieID(root), triedb)
|
2016-09-29 09:51:32 -05:00
|
|
|
if err != nil {
|
2017-06-20 11:26:09 -05:00
|
|
|
rt[i].err = err
|
2023-11-14 07:34:29 -06:00
|
|
|
return err
|
2016-09-29 09:51:32 -05:00
|
|
|
}
|
2023-03-14 03:50:53 -05:00
|
|
|
if nodes != nil {
|
|
|
|
if err := verifyAccessList(origTrie, newtr, nodes); err != nil {
|
|
|
|
rt[i].err = err
|
2023-11-14 07:34:29 -06:00
|
|
|
return err
|
2023-03-14 03:50:53 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-29 09:51:32 -05:00
|
|
|
tr = newtr
|
2022-03-31 02:28:32 -05:00
|
|
|
origTrie = tr.Copy()
|
2023-05-09 02:11:04 -05:00
|
|
|
origin = root
|
2016-09-29 09:51:32 -05:00
|
|
|
case opItercheckhash:
|
2022-06-06 10:14:55 -05:00
|
|
|
checktr := NewEmpty(triedb)
|
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 14:31:45 -05:00
|
|
|
it := NewIterator(tr.MustNodeIterator(nil))
|
2016-09-29 09:51:32 -05:00
|
|
|
for it.Next() {
|
2023-04-20 05:57:24 -05:00
|
|
|
checktr.MustUpdate(it.Key, it.Value)
|
2016-09-29 09:51:32 -05:00
|
|
|
}
|
|
|
|
if tr.Hash() != checktr.Hash() {
|
2024-02-29 03:56:46 -06:00
|
|
|
rt[i].err = errors.New("hash mismatch in opItercheckhash")
|
2016-09-29 09:51:32 -05:00
|
|
|
}
|
2023-02-20 08:54:52 -06:00
|
|
|
case opNodeDiff:
|
|
|
|
var (
|
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 14:31:45 -05:00
|
|
|
origIter = origTrie.MustNodeIterator(nil)
|
|
|
|
curIter = tr.MustNodeIterator(nil)
|
2023-02-20 08:54:52 -06:00
|
|
|
origSeen = make(map[string]struct{})
|
|
|
|
curSeen = make(map[string]struct{})
|
|
|
|
)
|
|
|
|
for origIter.Next(true) {
|
|
|
|
if origIter.Leaf() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
origSeen[string(origIter.Path())] = struct{}{}
|
|
|
|
}
|
|
|
|
for curIter.Next(true) {
|
|
|
|
if curIter.Leaf() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
curSeen[string(curIter.Path())] = struct{}{}
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
insertExp = make(map[string]struct{})
|
|
|
|
deleteExp = make(map[string]struct{})
|
|
|
|
)
|
|
|
|
for path := range curSeen {
|
|
|
|
_, present := origSeen[path]
|
|
|
|
if !present {
|
|
|
|
insertExp[path] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for path := range origSeen {
|
|
|
|
_, present := curSeen[path]
|
|
|
|
if !present {
|
|
|
|
deleteExp[path] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 03:50:53 -05:00
|
|
|
if len(insertExp) != len(tr.tracer.inserts) {
|
2024-02-29 03:56:46 -06:00
|
|
|
rt[i].err = errors.New("insert set mismatch")
|
2023-02-20 08:54:52 -06:00
|
|
|
}
|
2023-03-14 03:50:53 -05:00
|
|
|
if len(deleteExp) != len(tr.tracer.deletes) {
|
2024-02-29 03:56:46 -06:00
|
|
|
rt[i].err = errors.New("delete set mismatch")
|
2023-02-20 08:54:52 -06:00
|
|
|
}
|
2023-03-14 03:50:53 -05:00
|
|
|
for insert := range tr.tracer.inserts {
|
|
|
|
if _, present := insertExp[insert]; !present {
|
2024-02-29 03:56:46 -06:00
|
|
|
rt[i].err = errors.New("missing inserted node")
|
2023-02-20 08:54:52 -06:00
|
|
|
}
|
|
|
|
}
|
2023-03-14 03:50:53 -05:00
|
|
|
for del := range tr.tracer.deletes {
|
|
|
|
if _, present := deleteExp[del]; !present {
|
2024-02-29 03:56:46 -06:00
|
|
|
rt[i].err = errors.New("missing deleted node")
|
2023-02-20 08:54:52 -06:00
|
|
|
}
|
|
|
|
}
|
2017-06-20 11:26:09 -05:00
|
|
|
}
|
|
|
|
// Abort the test on error.
|
|
|
|
if rt[i].err != nil {
|
2023-11-14 07:34:29 -06:00
|
|
|
return rt[i].err
|
2016-10-17 09:13:50 -05:00
|
|
|
}
|
|
|
|
}
|
2023-11-14 07:34:29 -06:00
|
|
|
return nil
|
2016-10-17 09:13:50 -05:00
|
|
|
}
|
|
|
|
|
2016-09-29 09:51:32 -05:00
|
|
|
func TestRandom(t *testing.T) {
|
2023-11-14 07:34:29 -06:00
|
|
|
if err := quick.Check(runRandTestBool, nil); err != nil {
|
2017-06-20 11:26:09 -05:00
|
|
|
if cerr, ok := err.(*quick.CheckError); ok {
|
|
|
|
t.Fatalf("random test iteration %d failed: %s", cerr.Count, spew.Sdump(cerr.In))
|
|
|
|
}
|
2016-09-29 09:51:32 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
|
2022-08-04 03:03:20 -05:00
|
|
|
func BenchmarkGet(b *testing.B) { benchGet(b) }
|
2015-06-30 07:54:37 -05:00
|
|
|
func BenchmarkUpdateBE(b *testing.B) { benchUpdate(b, binary.BigEndian) }
|
|
|
|
func BenchmarkUpdateLE(b *testing.B) { benchUpdate(b, binary.LittleEndian) }
|
|
|
|
|
|
|
|
const benchElemCount = 20000
|
|
|
|
|
2022-08-04 03:03:20 -05:00
|
|
|
func benchGet(b *testing.B) {
|
2024-02-13 07:49:53 -06:00
|
|
|
triedb := newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)
|
2022-08-04 03:03:20 -05:00
|
|
|
trie := NewEmpty(triedb)
|
2015-06-30 07:54:37 -05:00
|
|
|
k := make([]byte, 32)
|
|
|
|
for i := 0; i < benchElemCount; i++ {
|
|
|
|
binary.LittleEndian.PutUint64(k, uint64(i))
|
2023-10-03 06:46:22 -05:00
|
|
|
v := make([]byte, 32)
|
|
|
|
binary.LittleEndian.PutUint64(v, uint64(i))
|
|
|
|
trie.MustUpdate(k, v)
|
2015-06-30 07:54:37 -05:00
|
|
|
}
|
|
|
|
binary.LittleEndian.PutUint64(k, benchElemCount/2)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustGet(k)
|
2015-06-30 07:54:37 -05:00
|
|
|
}
|
2016-10-14 11:04:33 -05:00
|
|
|
b.StopTimer()
|
2015-06-30 07:54:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie {
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2015-06-30 07:54:37 -05:00
|
|
|
k := make([]byte, 32)
|
2020-01-17 05:59:45 -06:00
|
|
|
b.ReportAllocs()
|
2015-06-30 07:54:37 -05:00
|
|
|
for i := 0; i < b.N; i++ {
|
2023-10-03 06:46:22 -05:00
|
|
|
v := make([]byte, 32)
|
2015-06-30 07:54:37 -05:00
|
|
|
e.PutUint64(k, uint64(i))
|
2023-10-03 06:46:22 -05:00
|
|
|
e.PutUint64(v, uint64(i))
|
|
|
|
trie.MustUpdate(k, v)
|
2015-06-30 07:54:37 -05:00
|
|
|
}
|
|
|
|
return trie
|
|
|
|
}
|
|
|
|
|
2017-10-13 03:10:02 -05:00
|
|
|
// Benchmarks the trie hashing. Since the trie caches the result of any operation,
|
2022-10-11 02:37:00 -05:00
|
|
|
// we cannot use b.N as the number of hashing rounds, since all rounds apart from
|
2017-10-13 03:10:02 -05:00
|
|
|
// the first one will be NOOP. As such, we'll use b.N as the number of account to
|
|
|
|
// insert into the trie before measuring the hashing.
|
2020-01-17 05:59:45 -06:00
|
|
|
// BenchmarkHash-6 288680 4561 ns/op 682 B/op 9 allocs/op
|
|
|
|
// BenchmarkHash-6 275095 4800 ns/op 685 B/op 9 allocs/op
|
|
|
|
// pure hasher:
|
|
|
|
// BenchmarkHash-6 319362 4230 ns/op 675 B/op 9 allocs/op
|
|
|
|
// BenchmarkHash-6 257460 4674 ns/op 689 B/op 9 allocs/op
|
|
|
|
// With hashing in-between and pure hasher:
|
|
|
|
// BenchmarkHash-6 225417 7150 ns/op 982 B/op 12 allocs/op
|
|
|
|
// BenchmarkHash-6 220378 6197 ns/op 983 B/op 12 allocs/op
|
|
|
|
// same with old hasher
|
|
|
|
// BenchmarkHash-6 229758 6437 ns/op 981 B/op 12 allocs/op
|
|
|
|
// BenchmarkHash-6 212610 7137 ns/op 986 B/op 12 allocs/op
|
2017-10-13 03:10:02 -05:00
|
|
|
func BenchmarkHash(b *testing.B) {
|
2020-01-17 05:59:45 -06:00
|
|
|
// Create a realistic account trie to hash. We're first adding and hashing N
|
|
|
|
// entries, then adding N more.
|
|
|
|
addresses, accounts := makeAccounts(2 * b.N)
|
|
|
|
// Insert the accounts into the trie and hash it
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2020-01-17 05:59:45 -06:00
|
|
|
i := 0
|
|
|
|
for ; i < len(addresses)/2; i++ {
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-01-17 05:59:45 -06:00
|
|
|
}
|
|
|
|
trie.Hash()
|
|
|
|
for ; i < len(addresses); i++ {
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-01-17 05:59:45 -06:00
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
//trie.hashRoot(nil, nil)
|
|
|
|
trie.Hash()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmarks the trie Commit following a Hash. Since the trie caches the result of any operation,
|
2022-10-11 02:37:00 -05:00
|
|
|
// we cannot use b.N as the number of hashing rounds, since all rounds apart from
|
2020-01-17 05:59:45 -06:00
|
|
|
// the first one will be NOOP. As such, we'll use b.N as the number of account to
|
|
|
|
// insert into the trie before measuring the hashing.
|
|
|
|
func BenchmarkCommitAfterHash(b *testing.B) {
|
|
|
|
b.Run("no-onleaf", func(b *testing.B) {
|
2022-08-04 03:03:20 -05:00
|
|
|
benchmarkCommitAfterHash(b, false)
|
2020-01-17 05:59:45 -06:00
|
|
|
})
|
|
|
|
b.Run("with-onleaf", func(b *testing.B) {
|
2022-08-04 03:03:20 -05:00
|
|
|
benchmarkCommitAfterHash(b, true)
|
2020-01-17 05:59:45 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-04 03:03:20 -05:00
|
|
|
func benchmarkCommitAfterHash(b *testing.B, collectLeaf bool) {
|
2017-10-13 03:10:02 -05:00
|
|
|
// Make the random benchmark deterministic
|
2020-01-17 05:59:45 -06:00
|
|
|
addresses, accounts := makeAccounts(b.N)
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2020-01-17 05:59:45 -06:00
|
|
|
for i := 0; i < len(addresses); i++ {
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-01-17 05:59:45 -06:00
|
|
|
}
|
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
trie.Hash()
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
2022-08-04 03:03:20 -05:00
|
|
|
trie.Commit(collectLeaf)
|
2020-01-17 05:59:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTinyTrie(t *testing.T) {
|
|
|
|
// Create a realistic account trie to hash
|
2020-11-19 15:50:47 -06:00
|
|
|
_, accounts := makeAccounts(5)
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001337"), accounts[3])
|
2020-11-19 15:50:47 -06:00
|
|
|
if exp, root := common.HexToHash("8c6a85a4d9fda98feff88450299e574e5378e32391f75a055d470ac0653f1005"), trie.Hash(); exp != root {
|
|
|
|
t.Errorf("1: got %x, exp %x", root, exp)
|
2020-01-17 05:59:45 -06:00
|
|
|
}
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001338"), accounts[4])
|
2020-11-19 15:50:47 -06:00
|
|
|
if exp, root := common.HexToHash("ec63b967e98a5720e7f720482151963982890d82c9093c0d486b7eb8883a66b1"), trie.Hash(); exp != root {
|
|
|
|
t.Errorf("2: got %x, exp %x", root, exp)
|
2020-01-17 05:59:45 -06:00
|
|
|
}
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001339"), accounts[4])
|
2020-11-19 15:50:47 -06:00
|
|
|
if exp, root := common.HexToHash("0608c1d1dc3905fa22204c7a0e43644831c3b6d3def0f274be623a948197e64a"), trie.Hash(); exp != root {
|
|
|
|
t.Errorf("3: got %x, exp %x", root, exp)
|
2020-01-17 05:59:45 -06:00
|
|
|
}
|
2024-02-13 07:49:53 -06:00
|
|
|
checktr := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
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 14:31:45 -05:00
|
|
|
it := NewIterator(trie.MustNodeIterator(nil))
|
2020-01-17 05:59:45 -06:00
|
|
|
for it.Next() {
|
2023-04-20 05:57:24 -05:00
|
|
|
checktr.MustUpdate(it.Key, it.Value)
|
2020-01-17 05:59:45 -06:00
|
|
|
}
|
|
|
|
if troot, itroot := trie.Hash(), checktr.Hash(); troot != itroot {
|
|
|
|
t.Fatalf("hash mismatch in opItercheckhash, trie: %x, check: %x", troot, itroot)
|
|
|
|
}
|
|
|
|
}
|
2017-10-13 03:10:02 -05:00
|
|
|
|
2020-01-17 05:59:45 -06:00
|
|
|
func TestCommitAfterHash(t *testing.T) {
|
2017-10-13 03:10:02 -05:00
|
|
|
// Create a realistic account trie to hash
|
2020-01-17 05:59:45 -06:00
|
|
|
addresses, accounts := makeAccounts(1000)
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2020-01-17 05:59:45 -06:00
|
|
|
for i := 0; i < len(addresses); i++ {
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-01-17 05:59:45 -06:00
|
|
|
}
|
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
trie.Hash()
|
2022-08-04 03:03:20 -05:00
|
|
|
trie.Commit(false)
|
2020-01-17 05:59:45 -06:00
|
|
|
root := trie.Hash()
|
2020-11-19 15:50:47 -06:00
|
|
|
exp := common.HexToHash("72f9d3f3fe1e1dd7b8936442e7642aef76371472d94319900790053c493f3fe6")
|
2020-01-17 05:59:45 -06:00
|
|
|
if exp != root {
|
|
|
|
t.Errorf("got %x, exp %x", root, exp)
|
|
|
|
}
|
2023-06-27 07:36:38 -05:00
|
|
|
root, _, _ = trie.Commit(false)
|
2020-01-17 05:59:45 -06:00
|
|
|
if exp != root {
|
|
|
|
t.Errorf("got %x, exp %x", root, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) {
|
|
|
|
// Make the random benchmark deterministic
|
|
|
|
random := rand.New(rand.NewSource(0))
|
|
|
|
// Create a realistic account trie to hash
|
|
|
|
addresses = make([][20]byte, size)
|
2017-10-13 03:10:02 -05:00
|
|
|
for i := 0; i < len(addresses); i++ {
|
2020-11-19 15:50:47 -06:00
|
|
|
data := make([]byte, 20)
|
|
|
|
random.Read(data)
|
|
|
|
copy(addresses[i][:], data)
|
2017-10-13 03:10:02 -05:00
|
|
|
}
|
2020-01-17 05:59:45 -06:00
|
|
|
accounts = make([][]byte, len(addresses))
|
2017-10-13 03:10:02 -05:00
|
|
|
for i := 0; i < len(accounts); i++ {
|
|
|
|
var (
|
2020-11-19 15:50:47 -06:00
|
|
|
nonce = uint64(random.Int63())
|
2023-02-21 05:12:27 -06:00
|
|
|
root = types.EmptyRootHash
|
2020-11-19 15:50:47 -06:00
|
|
|
code = crypto.Keccak256(nil)
|
2017-10-13 03:10:02 -05:00
|
|
|
)
|
2020-11-19 15:50:47 -06:00
|
|
|
// The big.Rand function is not deterministic with regards to 64 vs 32 bit systems,
|
|
|
|
// and will consume different amount of data from the rand source.
|
|
|
|
//balance = new(big.Int).Rand(random, new(big.Int).Exp(common.Big2, common.Big256, nil))
|
|
|
|
// Therefore, we instead just read via byte buffer
|
|
|
|
numBytes := random.Uint32() % 33 // [0, 32] bytes
|
|
|
|
balanceBytes := make([]byte, numBytes)
|
|
|
|
random.Read(balanceBytes)
|
2024-01-23 07:51:58 -06:00
|
|
|
balance := new(uint256.Int).SetBytes(balanceBytes)
|
2021-09-28 03:48:07 -05:00
|
|
|
data, _ := rlp.EncodeToBytes(&types.StateAccount{Nonce: nonce, Balance: balance, Root: root, CodeHash: code})
|
2020-11-19 15:50:47 -06:00
|
|
|
accounts[i] = data
|
2017-10-13 03:10:02 -05:00
|
|
|
}
|
2020-01-17 05:59:45 -06:00
|
|
|
return addresses, accounts
|
|
|
|
}
|
|
|
|
|
2020-09-30 06:45:56 -05:00
|
|
|
// spongeDb is a dummy db backend which accumulates writes in a sponge
|
|
|
|
type spongeDb struct {
|
2020-09-30 12:49:20 -05:00
|
|
|
sponge hash.Hash
|
|
|
|
id string
|
|
|
|
journal []string
|
2024-02-13 07:49:53 -06:00
|
|
|
keys []string
|
|
|
|
values map[string]string
|
2020-09-30 06:45:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 07:15:13 -06:00
|
|
|
func (s *spongeDb) NewBatchWithSize(size int) ethdb.Batch { return &spongeBatch{s} }
|
2022-03-10 02:35:22 -06:00
|
|
|
func (s *spongeDb) NewSnapshot() (ethdb.Snapshot, error) { panic("implement me") }
|
2020-09-30 06:45:56 -05: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 {
|
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 14:21:36 -05:00
|
|
|
var (
|
|
|
|
keybrief = key
|
|
|
|
valbrief = value
|
|
|
|
)
|
|
|
|
if len(keybrief) > 8 {
|
|
|
|
keybrief = keybrief[:8]
|
|
|
|
}
|
2020-09-30 12:49:20 -05:00
|
|
|
if len(valbrief) > 8 {
|
|
|
|
valbrief = valbrief[:8]
|
|
|
|
}
|
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 14:21:36 -05:00
|
|
|
s.journal = append(s.journal, fmt.Sprintf("%v: PUT([%x...], [%d bytes] %x...)\n", s.id, keybrief, len(value), valbrief))
|
2024-02-13 07:49:53 -06:00
|
|
|
|
|
|
|
if s.values == nil {
|
|
|
|
s.sponge.Write(key)
|
|
|
|
s.sponge.Write(value)
|
|
|
|
} else {
|
|
|
|
s.keys = append(s.keys, string(key))
|
|
|
|
s.values[string(key)] = string(value)
|
|
|
|
}
|
2020-09-30 06:45:56 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s *spongeDb) NewIterator(prefix []byte, start []byte) ethdb.Iterator { panic("implement me") }
|
|
|
|
|
2024-02-13 07:49:53 -06:00
|
|
|
func (s *spongeDb) Flush() {
|
|
|
|
// Bottom-up, the longest path first
|
|
|
|
sort.Sort(sort.Reverse(sort.StringSlice(s.keys)))
|
|
|
|
for _, key := range s.keys {
|
|
|
|
s.sponge.Write([]byte(key))
|
|
|
|
s.sponge.Write([]byte(s.values[key]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 06:45:56 -05:00
|
|
|
// 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 }
|
|
|
|
|
|
|
|
// TestCommitSequence tests that the trie.Commit operation writes the elements of the trie
|
2023-02-08 05:14:34 -06:00
|
|
|
// in the expected order.
|
2020-09-30 06:45:56 -05:00
|
|
|
// The test data was based on the 'master' code, and is basically random. It can be used
|
|
|
|
// to check whether changes to the trie modifies the write order or data in any way.
|
|
|
|
func TestCommitSequence(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
2023-02-08 05:14:34 -06:00
|
|
|
count int
|
|
|
|
expWriteSeqHash []byte
|
2020-09-30 06:45:56 -05:00
|
|
|
}{
|
2024-02-13 07:49:53 -06:00
|
|
|
{20, common.FromHex("330b0afae2853d96b9f015791fbe0fb7f239bf65f335f16dfc04b76c7536276d")},
|
|
|
|
{200, common.FromHex("5162b3735c06b5d606b043a3ee8adbdbbb408543f4966bca9dcc63da82684eeb")},
|
|
|
|
{2000, common.FromHex("4574cd8e6b17f3fe8ad89140d1d0bf4f1bd7a87a8ac3fb623b33550544c77635")},
|
2020-09-30 06:45:56 -05:00
|
|
|
} {
|
|
|
|
addresses, accounts := makeAccounts(tc.count)
|
|
|
|
// This spongeDb is used to check the sequence of disk-db-writes
|
2024-04-30 09:25:35 -05:00
|
|
|
s := &spongeDb{sponge: crypto.NewKeccakState()}
|
2024-02-13 07:49:53 -06:00
|
|
|
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme)
|
2022-06-06 10:14:55 -05:00
|
|
|
trie := NewEmpty(db)
|
2020-09-30 06:45:56 -05:00
|
|
|
// Fill the trie with elements
|
|
|
|
for i := 0; i < tc.count; i++ {
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-09-30 06:45:56 -05:00
|
|
|
}
|
|
|
|
// Flush trie -> database
|
2023-06-27 07:36:38 -05:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2024-02-13 07:49:53 -06:00
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
|
2020-09-30 06:45:56 -05:00
|
|
|
// Flush memdb -> disk (sponge)
|
2024-02-13 07:49:53 -06:00
|
|
|
db.Commit(root)
|
2020-09-30 06:45:56 -05:00
|
|
|
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) {
|
2020-11-19 15:50:47 -06:00
|
|
|
t.Errorf("test %d, disk write sequence wrong:\ngot %x exp %x\n", i, got, exp)
|
2020-09-30 06:45:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCommitSequenceRandomBlobs is identical to TestCommitSequence
|
|
|
|
// but uses random blobs instead of 'accounts'
|
|
|
|
func TestCommitSequenceRandomBlobs(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
2023-02-08 05:14:34 -06:00
|
|
|
count int
|
|
|
|
expWriteSeqHash []byte
|
2020-09-30 06:45:56 -05:00
|
|
|
}{
|
2024-02-13 07:49:53 -06:00
|
|
|
{20, common.FromHex("8016650c7a50cf88485fd06cde52d634a89711051107f00d21fae98234f2f13d")},
|
|
|
|
{200, common.FromHex("dde92ca9812e068e6982d04b40846dc65a61a9fd4996fc0f55f2fde172a8e13c")},
|
|
|
|
{2000, common.FromHex("ab553a7f9aff82e3929c382908e30ef7dd17a332933e92ba3fe873fc661ef382")},
|
2020-09-30 06:45:56 -05:00
|
|
|
} {
|
|
|
|
prng := rand.New(rand.NewSource(int64(i)))
|
|
|
|
// This spongeDb is used to check the sequence of disk-db-writes
|
2024-04-30 09:25:35 -05:00
|
|
|
s := &spongeDb{sponge: crypto.NewKeccakState()}
|
2024-02-13 07:49:53 -06:00
|
|
|
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme)
|
2022-06-06 10:14:55 -05:00
|
|
|
trie := NewEmpty(db)
|
2020-09-30 06:45:56 -05:00
|
|
|
// Fill the trie with elements
|
|
|
|
for i := 0; i < tc.count; i++ {
|
|
|
|
key := make([]byte, 32)
|
|
|
|
var val []byte
|
|
|
|
// 50% short elements, 50% large elements
|
|
|
|
if prng.Intn(2) == 0 {
|
|
|
|
val = make([]byte, 1+prng.Intn(32))
|
|
|
|
} else {
|
|
|
|
val = make([]byte, 1+prng.Intn(4096))
|
|
|
|
}
|
|
|
|
prng.Read(key)
|
|
|
|
prng.Read(val)
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(key, val)
|
2020-09-30 06:45:56 -05:00
|
|
|
}
|
|
|
|
// Flush trie -> database
|
2023-06-27 07:36:38 -05:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2024-02-13 07:49:53 -06:00
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
|
2020-09-30 06:45:56 -05:00
|
|
|
// Flush memdb -> disk (sponge)
|
2024-02-13 07:49:53 -06:00
|
|
|
db.Commit(root)
|
2020-09-30 06:45:56 -05:00
|
|
|
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) {
|
|
|
|
t.Fatalf("test %d, disk write sequence wrong:\ngot %x exp %x\n", i, got, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 12:49:20 -05:00
|
|
|
func TestCommitSequenceStackTrie(t *testing.T) {
|
|
|
|
for count := 1; count < 200; count++ {
|
|
|
|
prng := rand.New(rand.NewSource(int64(count)))
|
|
|
|
// This spongeDb is used to check the sequence of disk-db-writes
|
2024-02-13 07:49:53 -06:00
|
|
|
s := &spongeDb{
|
|
|
|
sponge: sha3.NewLegacyKeccak256(),
|
|
|
|
id: "a",
|
|
|
|
values: make(map[string]string),
|
|
|
|
}
|
|
|
|
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme)
|
2022-06-06 10:14:55 -05:00
|
|
|
trie := NewEmpty(db)
|
2023-10-17 07:09:25 -05:00
|
|
|
|
2024-02-13 07:49:53 -06:00
|
|
|
// Another sponge is used for the stacktrie commits
|
|
|
|
stackTrieSponge := &spongeDb{
|
|
|
|
sponge: sha3.NewLegacyKeccak256(),
|
|
|
|
id: "b",
|
|
|
|
values: make(map[string]string),
|
|
|
|
}
|
2024-04-16 02:05:36 -05:00
|
|
|
stTrie := NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
|
2023-10-10 23:12:45 -05:00
|
|
|
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme())
|
2022-11-28 07:31:28 -06:00
|
|
|
})
|
2024-02-13 07:49:53 -06:00
|
|
|
|
2020-09-30 12:49:20 -05:00
|
|
|
// Fill the trie with elements
|
2022-08-04 03:03:20 -05:00
|
|
|
for i := 0; i < count; i++ {
|
2020-09-30 12:49:20 -05:00
|
|
|
// For the stack trie, we need to do inserts in proper order
|
|
|
|
key := make([]byte, 32)
|
|
|
|
binary.BigEndian.PutUint64(key, uint64(i))
|
|
|
|
var val []byte
|
|
|
|
// 50% short elements, 50% large elements
|
|
|
|
if prng.Intn(2) == 0 {
|
|
|
|
val = make([]byte, 1+prng.Intn(32))
|
|
|
|
} else {
|
|
|
|
val = make([]byte, 1+prng.Intn(1024))
|
|
|
|
}
|
|
|
|
prng.Read(val)
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.Update(key, val)
|
|
|
|
stTrie.Update(key, val)
|
2020-09-30 12:49:20 -05:00
|
|
|
}
|
|
|
|
// Flush trie -> database
|
2023-06-27 07:36:38 -05:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2020-09-30 12:49:20 -05:00
|
|
|
// Flush memdb -> disk (sponge)
|
2024-02-13 07:49:53 -06:00
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
|
|
|
|
db.Commit(root)
|
|
|
|
s.Flush()
|
|
|
|
|
2020-09-30 12:49:20 -05:00
|
|
|
// And flush stacktrie -> disk
|
2024-04-16 02:05:36 -05:00
|
|
|
stRoot := stTrie.Hash()
|
2020-09-30 12:49:20 -05:00
|
|
|
if stRoot != root {
|
|
|
|
t.Fatalf("root wrong, got %x exp %x", stRoot, root)
|
|
|
|
}
|
2024-02-13 07:49:53 -06:00
|
|
|
stackTrieSponge.Flush()
|
2020-09-30 12:49:20 -05:00
|
|
|
if got, exp := stackTrieSponge.sponge.Sum(nil), s.sponge.Sum(nil); !bytes.Equal(got, exp) {
|
|
|
|
// Show the journal
|
|
|
|
t.Logf("Expected:")
|
|
|
|
for i, v := range s.journal {
|
|
|
|
t.Logf("op %d: %v", i, v)
|
|
|
|
}
|
|
|
|
t.Logf("Stacktrie:")
|
|
|
|
for i, v := range stackTrieSponge.journal {
|
|
|
|
t.Logf("op %d: %v", i, v)
|
|
|
|
}
|
|
|
|
t.Fatalf("test %d, disk write sequence wrong:\ngot %x exp %x\n", count, got, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 08:08:12 -06:00
|
|
|
// TestCommitSequenceSmallRoot tests that a trie which is essentially only a
|
|
|
|
// small (<32 byte) shortnode with an included value is properly committed to a
|
|
|
|
// database.
|
|
|
|
// This case might not matter, since in practice, all keys are 32 bytes, which means
|
|
|
|
// that even a small trie which contains a leaf will have an extension making it
|
|
|
|
// not fit into 32 bytes, rlp-encoded. However, it's still the correct thing to do.
|
|
|
|
func TestCommitSequenceSmallRoot(t *testing.T) {
|
2024-02-13 07:49:53 -06:00
|
|
|
s := &spongeDb{
|
|
|
|
sponge: sha3.NewLegacyKeccak256(),
|
|
|
|
id: "a",
|
|
|
|
values: make(map[string]string),
|
|
|
|
}
|
|
|
|
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme)
|
2022-06-06 10:14:55 -05:00
|
|
|
trie := NewEmpty(db)
|
2023-10-17 07:09:25 -05:00
|
|
|
|
2024-02-13 07:49:53 -06:00
|
|
|
// Another sponge is used for the stacktrie commits
|
|
|
|
stackTrieSponge := &spongeDb{
|
|
|
|
sponge: sha3.NewLegacyKeccak256(),
|
|
|
|
id: "b",
|
|
|
|
values: make(map[string]string),
|
|
|
|
}
|
2024-04-16 02:05:36 -05:00
|
|
|
stTrie := NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
|
2023-10-10 23:12:45 -05:00
|
|
|
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme())
|
2022-11-28 07:31:28 -06:00
|
|
|
})
|
2020-11-09 08:08:12 -06:00
|
|
|
// Add a single small-element to the trie(s)
|
|
|
|
key := make([]byte, 5)
|
|
|
|
key[0] = 1
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.Update(key, []byte{0x1})
|
|
|
|
stTrie.Update(key, []byte{0x1})
|
2024-02-13 07:49:53 -06:00
|
|
|
|
2020-11-09 08:08:12 -06:00
|
|
|
// Flush trie -> database
|
2023-06-27 07:36:38 -05:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2020-11-09 08:08:12 -06:00
|
|
|
// Flush memdb -> disk (sponge)
|
2024-02-13 07:49:53 -06:00
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
|
|
|
|
db.Commit(root)
|
|
|
|
|
2020-11-09 08:08:12 -06:00
|
|
|
// And flush stacktrie -> disk
|
2024-04-16 02:05:36 -05:00
|
|
|
stRoot := stTrie.Hash()
|
2020-11-09 08:08:12 -06:00
|
|
|
if stRoot != root {
|
|
|
|
t.Fatalf("root wrong, got %x exp %x", stRoot, root)
|
|
|
|
}
|
2022-03-09 07:45:17 -06:00
|
|
|
t.Logf("root: %x\n", stRoot)
|
2024-02-13 07:49:53 -06:00
|
|
|
|
|
|
|
s.Flush()
|
|
|
|
stackTrieSponge.Flush()
|
2020-11-09 08:08:12 -06:00
|
|
|
if got, exp := stackTrieSponge.sponge.Sum(nil), s.sponge.Sum(nil); !bytes.Equal(got, exp) {
|
|
|
|
t.Fatalf("test, disk write sequence wrong:\ngot %x exp %x\n", got, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 15:01:28 -05:00
|
|
|
// BenchmarkHashFixedSize benchmarks the hash of a fixed number of updates to a trie.
|
2020-01-17 05:59:45 -06:00
|
|
|
// This benchmark is meant to capture the difference on efficiency of small versus large changes. Typically,
|
|
|
|
// storage tries are small (a couple of entries), whereas the full post-block account trie update is large (a couple
|
|
|
|
// of thousand entries)
|
|
|
|
func BenchmarkHashFixedSize(b *testing.B) {
|
|
|
|
b.Run("10", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(20)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkHashFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("100", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(100)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkHashFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Run("1K", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(1000)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkHashFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("10K", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(10000)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkHashFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("100K", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(100000)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkHashFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkHashFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) {
|
|
|
|
b.ReportAllocs()
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2017-10-13 03:10:02 -05:00
|
|
|
for i := 0; i < len(addresses); i++ {
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2015-06-30 07:54:37 -05:00
|
|
|
}
|
2020-01-17 05:59:45 -06:00
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
b.StartTimer()
|
|
|
|
trie.Hash()
|
|
|
|
b.StopTimer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCommitAfterHashFixedSize(b *testing.B) {
|
|
|
|
b.Run("10", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(20)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkCommitAfterHashFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("100", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(100)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkCommitAfterHashFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Run("1K", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(1000)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkCommitAfterHashFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("10K", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(10000)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkCommitAfterHashFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("100K", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(100000)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkCommitAfterHashFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkCommitAfterHashFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) {
|
2017-10-13 03:10:02 -05:00
|
|
|
b.ReportAllocs()
|
2024-02-13 07:49:53 -06:00
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2020-01-17 05:59:45 -06:00
|
|
|
for i := 0; i < len(addresses); i++ {
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-01-17 05:59:45 -06:00
|
|
|
}
|
|
|
|
// Insert the accounts into the trie and hash it
|
2017-10-13 03:10:02 -05:00
|
|
|
trie.Hash()
|
2020-01-17 05:59:45 -06:00
|
|
|
b.StartTimer()
|
2022-08-04 03:03:20 -05:00
|
|
|
trie.Commit(false)
|
2020-01-17 05:59:45 -06:00
|
|
|
b.StopTimer()
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
func getString(trie *Trie, k string) []byte {
|
2023-04-20 05:57:24 -05:00
|
|
|
return trie.MustGet([]byte(k))
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateString(trie *Trie, k, v string) {
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustUpdate([]byte(k), []byte(v))
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteString(trie *Trie, k string) {
|
2023-04-20 05:57:24 -05:00
|
|
|
trie.MustDelete([]byte(k))
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2019-02-16 09:16:12 -06:00
|
|
|
|
|
|
|
func TestDecodeNode(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-03-20 03:09:35 -05:00
|
|
|
|
2019-02-16 09:16:12 -06:00
|
|
|
var (
|
|
|
|
hash = make([]byte, 20)
|
|
|
|
elems = make([]byte, 20)
|
|
|
|
)
|
|
|
|
for i := 0; i < 5000000; i++ {
|
2023-03-20 03:09:35 -05:00
|
|
|
prng.Read(hash)
|
|
|
|
prng.Read(elems)
|
2019-03-14 08:25:12 -05:00
|
|
|
decodeNode(hash, elems)
|
2019-02-16 09:16:12 -06:00
|
|
|
}
|
|
|
|
}
|
2023-11-14 07:34:29 -06:00
|
|
|
|
|
|
|
func FuzzTrie(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
|
|
|
var steps = 500
|
|
|
|
var input = bytes.NewReader(data)
|
|
|
|
var finishedFn = func() bool {
|
|
|
|
steps--
|
|
|
|
return steps < 0 || input.Len() == 0
|
|
|
|
}
|
|
|
|
if err := runRandTest(generateSteps(finishedFn, input)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|