2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2014-10-31 13:45:03 +00:00
|
|
|
package trie
|
2014-02-14 22:56:09 +00:00
|
|
|
|
|
|
|
import (
|
2014-11-18 18:52:45 +00:00
|
|
|
"bytes"
|
2015-06-30 12:54:37 +00:00
|
|
|
"encoding/binary"
|
2020-09-30 11:45:56 +00:00
|
|
|
"errors"
|
2014-05-25 22:09:38 +00:00
|
|
|
"fmt"
|
2020-09-30 11:45:56 +00:00
|
|
|
"hash"
|
2017-10-13 08:10:02 +00:00
|
|
|
"math/big"
|
2016-09-29 14:51:32 +00:00
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
2014-11-18 18:52:45 +00:00
|
|
|
"testing"
|
2016-09-29 14:51:32 +00:00
|
|
|
"testing/quick"
|
2014-11-16 02:21:55 +00:00
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2015-03-16 10:27:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-03-31 07:28:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2021-09-28 08:48:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-10-13 08:10:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-09-30 11:45:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2017-10-13 08:10:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2023-05-09 07:11:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
2020-09-30 11:45:56 +00:00
|
|
|
"golang.org/x/crypto/sha3"
|
2014-02-14 22:56:09 +00:00
|
|
|
)
|
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
func init() {
|
|
|
|
spew.Config.Indent = " "
|
2017-06-20 16:26:09 +00:00
|
|
|
spew.Config.DisableMethods = false
|
2014-02-15 12:21:11 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
func TestEmptyTrie(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 19:21:36 +00:00
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2015-01-08 10:47:04 +00:00
|
|
|
res := trie.Hash()
|
2023-02-21 11:12:27 +00:00
|
|
|
exp := types.EmptyRootHash
|
2019-07-22 07:30:09 +00:00
|
|
|
if res != exp {
|
2015-01-08 10:47:04 +00:00
|
|
|
t.Errorf("expected %x got %x", exp, res)
|
|
|
|
}
|
2014-02-15 12:21:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 09:57:02 +00:00
|
|
|
func TestNull(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 19:21:36 +00:00
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2015-03-19 09:57:02 +00:00
|
|
|
key := make([]byte, 32)
|
2017-01-09 10:16:06 +00:00
|
|
|
value := []byte("test")
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(key, value)
|
|
|
|
if !bytes.Equal(trie.MustGet(key), value) {
|
2017-01-09 10:16:06 +00:00
|
|
|
t.Fatal("wrong value")
|
|
|
|
}
|
2015-03-19 09:57:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 23:19:48 +00: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 19:21:36 +00: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 08:01:02 +00: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 19:21:36 +00:00
|
|
|
trie, err := New(TrieID(root), newTestDatabase(rawdb.NewMemoryDatabase(), scheme))
|
2015-07-05 23:19:48 +00:00
|
|
|
if trie != nil {
|
|
|
|
t.Error("New returned non-nil trie for invalid root")
|
|
|
|
}
|
2015-11-25 17:28:21 +00:00
|
|
|
if _, ok := err.(*MissingNodeError); !ok {
|
2016-04-15 09:06:57 +00:00
|
|
|
t.Errorf("New returned wrong error: %v", err)
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 07:11:04 +00:00
|
|
|
func TestMissingNode(t *testing.T) {
|
|
|
|
testMissingNode(t, false, rawdb.HashScheme)
|
2023-08-01 12:17:32 +00:00
|
|
|
testMissingNode(t, false, rawdb.PathScheme)
|
2023-05-09 07:11:04 +00:00
|
|
|
testMissingNode(t, true, rawdb.HashScheme)
|
2023-08-01 12:17:32 +00:00
|
|
|
testMissingNode(t, true, rawdb.PathScheme)
|
2023-05-09 07:11:04 +00:00
|
|
|
}
|
2018-02-05 16:40:32 +00:00
|
|
|
|
2023-05-09 07:11:04 +00:00
|
|
|
func testMissingNode(t *testing.T, memonly bool, scheme string) {
|
2022-11-28 13:31:28 +00:00
|
|
|
diskdb := rawdb.NewMemoryDatabase()
|
2023-05-09 07:11:04 +00:00
|
|
|
triedb := newTestDatabase(diskdb, scheme)
|
2018-02-05 16:40:32 +00:00
|
|
|
|
2022-06-06 15:14:55 +00:00
|
|
|
trie := NewEmpty(triedb)
|
2015-11-25 17:28:21 +00:00
|
|
|
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer")
|
|
|
|
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf")
|
2023-06-27 12:36:38 +00:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2023-07-24 10:22:09 +00:00
|
|
|
triedb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
|
2023-05-09 07:11:04 +00:00
|
|
|
|
2018-02-05 16:40:32 +00:00
|
|
|
if !memonly {
|
2023-02-08 11:14:34 +00:00
|
|
|
triedb.Commit(root, false)
|
2018-02-05 16:40:32 +00:00
|
|
|
}
|
2015-11-25 17:28:21 +00: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 08:01:02 +00:00
|
|
|
trie, _ = New(TrieID(root), triedb)
|
2023-04-20 10:57:24 +00:00
|
|
|
_, err := trie.Get([]byte("120000"))
|
2015-11-25 17:28:21 +00: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 08:01:02 +00:00
|
|
|
trie, _ = New(TrieID(root), triedb)
|
2023-04-20 10:57:24 +00:00
|
|
|
_, err = trie.Get([]byte("120099"))
|
2015-11-25 17:28:21 +00: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 08:01:02 +00:00
|
|
|
trie, _ = New(TrieID(root), triedb)
|
2023-04-20 10:57:24 +00:00
|
|
|
_, err = trie.Get([]byte("123456"))
|
2015-11-25 17:28:21 +00: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 08:01:02 +00:00
|
|
|
trie, _ = New(TrieID(root), triedb)
|
2023-04-20 10:57:24 +00:00
|
|
|
err = trie.Update([]byte("120099"), []byte("zxcvzxcvzxcvzxcvzxcvzxcvzxcvzxcv"))
|
2015-11-25 17:28:21 +00: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 08:01:02 +00:00
|
|
|
trie, _ = New(TrieID(root), triedb)
|
2023-04-20 10:57:24 +00:00
|
|
|
err = trie.Delete([]byte("123456"))
|
2015-11-25 17:28:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-05-09 07:11:04 +00: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 16:40:32 +00:00
|
|
|
if memonly {
|
2023-05-09 07:11:04 +00:00
|
|
|
trie.reader.banned = map[string]struct{}{string(path): {}}
|
2018-02-05 16:40:32 +00:00
|
|
|
} else {
|
2023-05-09 07:11:04 +00:00
|
|
|
rawdb.DeleteTrieNode(diskdb, common.Hash{}, path, hash, scheme)
|
2018-02-05 16:40:32 +00:00
|
|
|
}
|
2015-11-25 17:28:21 +00:00
|
|
|
|
2023-04-20 10:57:24 +00:00
|
|
|
_, err = trie.Get([]byte("120000"))
|
2015-11-25 17:28:21 +00:00
|
|
|
if _, ok := err.(*MissingNodeError); !ok {
|
|
|
|
t.Errorf("Wrong error: %v", err)
|
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
_, err = trie.Get([]byte("120099"))
|
2015-11-25 17:28:21 +00:00
|
|
|
if _, ok := err.(*MissingNodeError); !ok {
|
|
|
|
t.Errorf("Wrong error: %v", err)
|
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
_, err = trie.Get([]byte("123456"))
|
2015-11-25 17:28:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
err = trie.Update([]byte("120099"), []byte("zxcv"))
|
2015-11-25 17:28:21 +00:00
|
|
|
if _, ok := err.(*MissingNodeError); !ok {
|
|
|
|
t.Errorf("Wrong error: %v", err)
|
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
err = trie.Delete([]byte("123456"))
|
2015-11-25 17:28:21 +00:00
|
|
|
if _, ok := err.(*MissingNodeError); !ok {
|
|
|
|
t.Errorf("Wrong error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
func TestInsert(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 19:21:36 +00:00
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2014-02-15 12:21:11 +00:00
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
updateString(trie, "doe", "reindeer")
|
|
|
|
updateString(trie, "dog", "puppy")
|
|
|
|
updateString(trie, "dogglesworth", "cat")
|
2014-02-15 12:21:11 +00:00
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
exp := common.HexToHash("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3")
|
2015-01-08 10:47:04 +00:00
|
|
|
root := trie.Hash()
|
2015-07-05 23:19:48 +00:00
|
|
|
if root != exp {
|
2020-01-17 11:59:45 +00:00
|
|
|
t.Errorf("case 1: exp %x got %x", exp, root)
|
2015-01-08 10:47:04 +00:00
|
|
|
}
|
2014-02-20 13:40:00 +00:00
|
|
|
|
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
|
|
|
trie = NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2015-07-05 23:19:48 +00:00
|
|
|
updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
2014-02-20 13:40:00 +00:00
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab")
|
2023-06-27 12:36:38 +00:00
|
|
|
root, _, _ = trie.Commit(false)
|
2015-07-05 23:19:48 +00:00
|
|
|
if root != exp {
|
2020-01-17 11:59:45 +00:00
|
|
|
t.Errorf("case 2: exp %x got %x", exp, root)
|
2015-01-08 10:47:04 +00:00
|
|
|
}
|
2014-02-20 13:40:00 +00:00
|
|
|
}
|
2014-02-24 11:11:00 +00:00
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
func TestGet(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 19:21:36 +00:00
|
|
|
db := NewDatabase(rawdb.NewMemoryDatabase(), nil)
|
2022-08-04 08:03:20 +00:00
|
|
|
trie := NewEmpty(db)
|
2015-07-05 23:19:48 +00: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 12:36:38 +00:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2023-07-24 10:22:09 +00:00
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
|
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 08:01:02 +00:00
|
|
|
trie, _ = New(TrieID(root), db)
|
2014-06-17 16:05:46 +00:00
|
|
|
}
|
2014-06-30 11:08:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
func TestDelete(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 19:21:36 +00:00
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2015-01-08 10:47:04 +00:00
|
|
|
vals := []struct{ k, v string }{
|
|
|
|
{"do", "verb"},
|
|
|
|
{"ether", "wookiedoo"},
|
|
|
|
{"horse", "stallion"},
|
|
|
|
{"shaman", "horse"},
|
|
|
|
{"doge", "coin"},
|
|
|
|
{"ether", ""},
|
|
|
|
{"dog", "puppy"},
|
|
|
|
{"shaman", ""},
|
2014-06-17 16:05:46 +00:00
|
|
|
}
|
2015-01-08 10:47:04 +00:00
|
|
|
for _, val := range vals {
|
|
|
|
if val.v != "" {
|
2015-07-05 23:19:48 +00:00
|
|
|
updateString(trie, val.k, val.v)
|
2015-01-08 10:47:04 +00:00
|
|
|
} else {
|
2015-07-05 23:19:48 +00:00
|
|
|
deleteString(trie, val.k)
|
2015-01-08 10:47:04 +00:00
|
|
|
}
|
2014-06-30 11:08:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
hash := trie.Hash()
|
2015-07-05 23:19:48 +00:00
|
|
|
exp := common.HexToHash("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84")
|
|
|
|
if hash != exp {
|
2015-01-08 10:47:04 +00:00
|
|
|
t.Errorf("expected %x got %x", exp, hash)
|
2014-06-30 11:08:00 +00:00
|
|
|
}
|
2015-01-08 10:47:04 +00:00
|
|
|
}
|
2014-05-26 23:08:51 +00:00
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
func TestEmptyValues(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 19:21:36 +00:00
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2014-06-30 11:08:00 +00:00
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
vals := []struct{ k, v string }{
|
|
|
|
{"do", "verb"},
|
|
|
|
{"ether", "wookiedoo"},
|
|
|
|
{"horse", "stallion"},
|
|
|
|
{"shaman", "horse"},
|
|
|
|
{"doge", "coin"},
|
|
|
|
{"ether", ""},
|
|
|
|
{"dog", "puppy"},
|
|
|
|
{"shaman", ""},
|
2014-06-30 11:08:00 +00:00
|
|
|
}
|
2015-01-08 10:47:04 +00:00
|
|
|
for _, val := range vals {
|
2015-07-05 23:19:48 +00:00
|
|
|
updateString(trie, val.k, val.v)
|
2014-07-01 07:55:20 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
hash := trie.Hash()
|
2015-07-05 23:19:48 +00:00
|
|
|
exp := common.HexToHash("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84")
|
|
|
|
if hash != exp {
|
2015-01-08 10:47:04 +00:00
|
|
|
t.Errorf("expected %x got %x", exp, hash)
|
2014-06-17 16:05:46 +00:00
|
|
|
}
|
2014-07-01 07:55:20 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
func TestReplication(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 19:21:36 +00:00
|
|
|
db := NewDatabase(rawdb.NewMemoryDatabase(), nil)
|
2023-05-09 07:11:04 +00:00
|
|
|
trie := NewEmpty(db)
|
2015-01-08 10:47:04 +00:00
|
|
|
vals := []struct{ k, v string }{
|
|
|
|
{"do", "verb"},
|
|
|
|
{"ether", "wookiedoo"},
|
|
|
|
{"horse", "stallion"},
|
|
|
|
{"shaman", "horse"},
|
|
|
|
{"doge", "coin"},
|
|
|
|
{"dog", "puppy"},
|
|
|
|
{"somethingveryoddindeedthis is", "myothernodedata"},
|
2014-06-17 16:05:46 +00:00
|
|
|
}
|
2015-01-08 10:47:04 +00:00
|
|
|
for _, val := range vals {
|
2015-07-05 23:19:48 +00:00
|
|
|
updateString(trie, val.k, val.v)
|
2015-01-08 10:47:04 +00:00
|
|
|
}
|
2023-06-27 12:36:38 +00:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2023-07-24 10:22:09 +00:00
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
|
2014-07-15 13:29:54 +00:00
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
// create a new trie on top of the database and check that lookups work.
|
2023-05-09 07:11:04 +00:00
|
|
|
trie2, err := New(TrieID(root), db)
|
2015-07-05 23:19:48 +00:00
|
|
|
if err != nil {
|
2023-05-09 07:11:04 +00:00
|
|
|
t.Fatalf("can't recreate trie at %x: %v", root, err)
|
2015-07-05 23:19:48 +00: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 12:36:38 +00:00
|
|
|
hash, nodes, _ := trie2.Commit(false)
|
2023-05-09 07:11:04 +00:00
|
|
|
if hash != root {
|
|
|
|
t.Errorf("root failure. expected %x got %x", root, hash)
|
2015-01-08 10:47:04 +00:00
|
|
|
}
|
2014-07-17 09:21:18 +00:00
|
|
|
|
2022-08-04 08:03:20 +00:00
|
|
|
// recreate the trie after commit
|
|
|
|
if nodes != nil {
|
2023-07-24 10:22:09 +00:00
|
|
|
db.Update(hash, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
|
2022-08-04 08:03:20 +00:00
|
|
|
}
|
2023-05-09 07:11:04 +00:00
|
|
|
trie2, err = New(TrieID(hash), db)
|
2022-08-04 08:03:20 +00:00
|
|
|
if err != nil {
|
2023-05-09 07:11:04 +00:00
|
|
|
t.Fatalf("can't recreate trie at %x: %v", hash, err)
|
2022-08-04 08:03:20 +00:00
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
// perform some insertions on the new trie.
|
|
|
|
vals2 := []struct{ k, v string }{
|
2015-01-08 10:47:04 +00:00
|
|
|
{"do", "verb"},
|
|
|
|
{"ether", "wookiedoo"},
|
|
|
|
{"horse", "stallion"},
|
2015-07-05 23:19:48 +00:00
|
|
|
// {"shaman", "horse"},
|
|
|
|
// {"doge", "coin"},
|
|
|
|
// {"ether", ""},
|
|
|
|
// {"dog", "puppy"},
|
|
|
|
// {"somethingveryoddindeedthis is", "myothernodedata"},
|
|
|
|
// {"shaman", ""},
|
2014-10-10 15:00:06 +00:00
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
for _, val := range vals2 {
|
|
|
|
updateString(trie2, val.k, val.v)
|
2014-10-10 15:00:06 +00:00
|
|
|
}
|
2023-05-09 07:11:04 +00:00
|
|
|
if trie2.Hash() != hash {
|
|
|
|
t.Errorf("root failure. expected %x got %x", hash, hash)
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-14 21:19:50 +00:00
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
func TestLargeValue(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 19:21:36 +00:00
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate([]byte("key1"), []byte{99, 99, 99, 99})
|
|
|
|
trie.MustUpdate([]byte("key2"), bytes.Repeat([]byte{1}, 32))
|
2015-07-05 23:19:48 +00:00
|
|
|
trie.Hash()
|
2016-09-29 14:51:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 11:59:45 +00:00
|
|
|
// TestRandomCases tests som cases that were found via random fuzzing
|
|
|
|
func TestRandomCases(t *testing.T) {
|
2021-02-02 09:32:44 +00:00
|
|
|
var rt = []randTestStep{
|
2020-01-17 11:59:45 +00: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
|
|
|
|
}
|
|
|
|
runRandTest(rt)
|
|
|
|
}
|
|
|
|
|
2016-10-17 14:13:50 +00:00
|
|
|
// randTest performs random trie operations.
|
|
|
|
// Instances of this test are created by Generate.
|
|
|
|
type randTest []randTestStep
|
|
|
|
|
2016-09-29 14:51:32 +00:00
|
|
|
type randTestStep struct {
|
|
|
|
op int
|
|
|
|
key []byte // for opUpdate, opDelete, opGet
|
|
|
|
value []byte // for opUpdate
|
2017-06-20 16:26:09 +00:00
|
|
|
err error // for debugging
|
2016-09-29 14:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
opUpdate = iota
|
|
|
|
opDelete
|
|
|
|
opGet
|
|
|
|
opHash
|
2022-08-04 08:03:20 +00:00
|
|
|
opCommit
|
2016-09-29 14:51:32 +00:00
|
|
|
opItercheckhash
|
2022-03-31 07:28:32 +00: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 08:01:02 +00:00
|
|
|
opProve
|
2016-09-29 14:51:32 +00:00
|
|
|
opMax // boundary value, not an actual op
|
|
|
|
)
|
|
|
|
|
|
|
|
func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
|
|
|
|
var allKeys [][]byte
|
|
|
|
genKey := func() []byte {
|
|
|
|
if len(allKeys) < 2 || r.Intn(100) < 10 {
|
|
|
|
// new key
|
|
|
|
key := make([]byte, r.Intn(50))
|
2017-03-22 18:48:51 +00:00
|
|
|
r.Read(key)
|
2016-09-29 14:51:32 +00:00
|
|
|
allKeys = append(allKeys, key)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
// use existing key
|
|
|
|
return allKeys[r.Intn(len(allKeys))]
|
|
|
|
}
|
|
|
|
|
|
|
|
var steps randTest
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
step := randTestStep{op: r.Intn(opMax)}
|
|
|
|
switch step.op {
|
|
|
|
case opUpdate:
|
|
|
|
step.key = genKey()
|
|
|
|
step.value = make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(step.value, uint64(i))
|
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 08:01:02 +00:00
|
|
|
case opGet, opDelete, opProve:
|
2016-09-29 14:51:32 +00:00
|
|
|
step.key = genKey()
|
|
|
|
}
|
|
|
|
steps = append(steps, step)
|
|
|
|
}
|
|
|
|
return reflect.ValueOf(steps)
|
|
|
|
}
|
|
|
|
|
2023-05-09 07:11:04 +00:00
|
|
|
func verifyAccessList(old *Trie, new *Trie, set *trienode.NodeSet) error {
|
2023-03-14 08:50:53 +00:00
|
|
|
deletes, inserts, updates := diffTries(old, new)
|
|
|
|
|
|
|
|
// Check insertion set
|
|
|
|
for path := range inserts {
|
2023-05-09 07:11:04 +00:00
|
|
|
n, ok := set.Nodes[path]
|
2023-04-26 06:01:54 +00:00
|
|
|
if !ok || n.IsDeleted() {
|
2023-03-14 08:50:53 +00:00
|
|
|
return errors.New("expect new node")
|
|
|
|
}
|
2023-08-01 12:17:32 +00:00
|
|
|
//if len(n.Prev) > 0 {
|
|
|
|
// return errors.New("unexpected origin value")
|
|
|
|
//}
|
2023-03-14 08:50:53 +00:00
|
|
|
}
|
|
|
|
// Check deletion set
|
2023-08-01 12:17:32 +00:00
|
|
|
for path := range deletes {
|
2023-05-09 07:11:04 +00:00
|
|
|
n, ok := set.Nodes[path]
|
2023-04-26 06:01:54 +00:00
|
|
|
if !ok || !n.IsDeleted() {
|
2023-03-14 08:50:53 +00:00
|
|
|
return errors.New("expect deleted node")
|
|
|
|
}
|
2023-08-01 12:17:32 +00: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 08:50:53 +00:00
|
|
|
}
|
|
|
|
// Check update set
|
2023-08-01 12:17:32 +00:00
|
|
|
for path := range updates {
|
2023-05-09 07:11:04 +00:00
|
|
|
n, ok := set.Nodes[path]
|
2023-04-26 06:01:54 +00:00
|
|
|
if !ok || n.IsDeleted() {
|
2023-03-14 08:50:53 +00:00
|
|
|
return errors.New("expect updated node")
|
|
|
|
}
|
2023-08-01 12:17:32 +00: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 08:50:53 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-29 14:51:32 +00:00
|
|
|
func runRandTest(rt randTest) bool {
|
2023-05-09 07:11:04 +00:00
|
|
|
var scheme = rawdb.HashScheme
|
2023-08-01 12:17:32 +00:00
|
|
|
if rand.Intn(2) == 0 {
|
|
|
|
scheme = rawdb.PathScheme
|
|
|
|
}
|
2022-03-31 07:28:32 +00:00
|
|
|
var (
|
2023-05-09 07:11:04 +00:00
|
|
|
origin = types.EmptyRootHash
|
|
|
|
triedb = newTestDatabase(rawdb.NewMemoryDatabase(), scheme)
|
2022-06-06 15:14:55 +00:00
|
|
|
tr = NewEmpty(triedb)
|
|
|
|
values = make(map[string]string) // tracks content of the trie
|
|
|
|
origTrie = NewEmpty(triedb)
|
2022-03-31 07:28:32 +00:00
|
|
|
)
|
2017-06-20 16:26:09 +00:00
|
|
|
for i, step := range rt {
|
2022-03-09 13:45:17 +00: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 14:51:32 +00:00
|
|
|
switch step.op {
|
|
|
|
case opUpdate:
|
2023-04-20 10:57:24 +00:00
|
|
|
tr.MustUpdate(step.key, step.value)
|
2016-09-29 14:51:32 +00:00
|
|
|
values[string(step.key)] = string(step.value)
|
|
|
|
case opDelete:
|
2023-04-20 10:57:24 +00:00
|
|
|
tr.MustDelete(step.key)
|
2016-09-29 14:51:32 +00:00
|
|
|
delete(values, string(step.key))
|
|
|
|
case opGet:
|
2023-04-20 10:57:24 +00:00
|
|
|
v := tr.MustGet(step.key)
|
2016-09-29 14:51:32 +00:00
|
|
|
want := values[string(step.key)]
|
|
|
|
if string(v) != want {
|
2022-07-04 08:03:32 +00:00
|
|
|
rt[i].err = fmt.Errorf("mismatch for key %#x, got %#x want %#x", step.key, v, want)
|
2016-09-29 14:51:32 +00: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 08:01:02 +00:00
|
|
|
case opProve:
|
|
|
|
hash := tr.Hash()
|
2023-02-21 11:12:27 +00: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 08:01:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
proofDb := rawdb.NewMemoryDatabase()
|
2023-06-19 14:28:40 +00: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 08:01:02 +00: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 14:51:32 +00:00
|
|
|
case opHash:
|
|
|
|
tr.Hash()
|
2022-08-04 08:03:20 +00:00
|
|
|
case opCommit:
|
2023-06-27 12:36:38 +00:00
|
|
|
root, nodes, _ := tr.Commit(true)
|
2022-08-04 08:03:20 +00:00
|
|
|
if nodes != nil {
|
2023-07-24 10:22:09 +00:00
|
|
|
triedb.Update(root, origin, 0, trienode.NewWithNodeSet(nodes), nil)
|
2022-08-04 08:03:20 +00: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 08:01:02 +00:00
|
|
|
newtr, err := New(TrieID(root), triedb)
|
2016-09-29 14:51:32 +00:00
|
|
|
if err != nil {
|
2017-06-20 16:26:09 +00:00
|
|
|
rt[i].err = err
|
|
|
|
return false
|
2016-09-29 14:51:32 +00:00
|
|
|
}
|
2023-03-14 08:50:53 +00:00
|
|
|
if nodes != nil {
|
|
|
|
if err := verifyAccessList(origTrie, newtr, nodes); err != nil {
|
|
|
|
rt[i].err = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2016-09-29 14:51:32 +00:00
|
|
|
tr = newtr
|
2022-03-31 07:28:32 +00:00
|
|
|
origTrie = tr.Copy()
|
2023-05-09 07:11:04 +00:00
|
|
|
origin = root
|
2016-09-29 14:51:32 +00:00
|
|
|
case opItercheckhash:
|
2022-06-06 15:14:55 +00: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 19:31:45 +00:00
|
|
|
it := NewIterator(tr.MustNodeIterator(nil))
|
2016-09-29 14:51:32 +00:00
|
|
|
for it.Next() {
|
2023-04-20 10:57:24 +00:00
|
|
|
checktr.MustUpdate(it.Key, it.Value)
|
2016-09-29 14:51:32 +00:00
|
|
|
}
|
|
|
|
if tr.Hash() != checktr.Hash() {
|
2017-06-20 16:26:09 +00:00
|
|
|
rt[i].err = fmt.Errorf("hash mismatch in opItercheckhash")
|
2016-09-29 14:51:32 +00:00
|
|
|
}
|
2023-02-20 14:54:52 +00: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 19:31:45 +00:00
|
|
|
origIter = origTrie.MustNodeIterator(nil)
|
|
|
|
curIter = tr.MustNodeIterator(nil)
|
2023-02-20 14:54:52 +00: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 08:50:53 +00:00
|
|
|
if len(insertExp) != len(tr.tracer.inserts) {
|
2023-02-20 14:54:52 +00:00
|
|
|
rt[i].err = fmt.Errorf("insert set mismatch")
|
|
|
|
}
|
2023-03-14 08:50:53 +00:00
|
|
|
if len(deleteExp) != len(tr.tracer.deletes) {
|
2023-02-20 14:54:52 +00:00
|
|
|
rt[i].err = fmt.Errorf("delete set mismatch")
|
|
|
|
}
|
2023-03-14 08:50:53 +00:00
|
|
|
for insert := range tr.tracer.inserts {
|
|
|
|
if _, present := insertExp[insert]; !present {
|
2023-02-20 14:54:52 +00:00
|
|
|
rt[i].err = fmt.Errorf("missing inserted node")
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 08:50:53 +00:00
|
|
|
for del := range tr.tracer.deletes {
|
|
|
|
if _, present := deleteExp[del]; !present {
|
2023-02-20 14:54:52 +00:00
|
|
|
rt[i].err = fmt.Errorf("missing deleted node")
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 16:26:09 +00:00
|
|
|
}
|
|
|
|
// Abort the test on error.
|
|
|
|
if rt[i].err != nil {
|
|
|
|
return false
|
2016-10-17 14:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-09-29 14:51:32 +00:00
|
|
|
func TestRandom(t *testing.T) {
|
|
|
|
if err := quick.Check(runRandTest, nil); err != nil {
|
2017-06-20 16:26:09 +00: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 14:51:32 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 08:03:20 +00:00
|
|
|
func BenchmarkGet(b *testing.B) { benchGet(b) }
|
2015-06-30 12:54:37 +00: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 08:03:20 +00:00
|
|
|
func benchGet(b *testing.B) {
|
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
|
|
|
triedb := NewDatabase(rawdb.NewMemoryDatabase(), nil)
|
2022-08-04 08:03:20 +00:00
|
|
|
trie := NewEmpty(triedb)
|
2015-06-30 12:54:37 +00:00
|
|
|
k := make([]byte, 32)
|
|
|
|
for i := 0; i < benchElemCount; i++ {
|
|
|
|
binary.LittleEndian.PutUint64(k, uint64(i))
|
2023-10-03 11:46:22 +00:00
|
|
|
v := make([]byte, 32)
|
|
|
|
binary.LittleEndian.PutUint64(v, uint64(i))
|
|
|
|
trie.MustUpdate(k, v)
|
2015-06-30 12:54:37 +00:00
|
|
|
}
|
|
|
|
binary.LittleEndian.PutUint64(k, benchElemCount/2)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustGet(k)
|
2015-06-30 12:54:37 +00:00
|
|
|
}
|
2016-10-14 16:04:33 +00:00
|
|
|
b.StopTimer()
|
2015-06-30 12:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie {
|
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
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2015-06-30 12:54:37 +00:00
|
|
|
k := make([]byte, 32)
|
2020-01-17 11:59:45 +00:00
|
|
|
b.ReportAllocs()
|
2015-06-30 12:54:37 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2023-10-03 11:46:22 +00:00
|
|
|
v := make([]byte, 32)
|
2015-06-30 12:54:37 +00:00
|
|
|
e.PutUint64(k, uint64(i))
|
2023-10-03 11:46:22 +00:00
|
|
|
e.PutUint64(v, uint64(i))
|
|
|
|
trie.MustUpdate(k, v)
|
2015-06-30 12:54:37 +00:00
|
|
|
}
|
|
|
|
return trie
|
|
|
|
}
|
|
|
|
|
2017-10-13 08:10:02 +00:00
|
|
|
// Benchmarks the trie hashing. Since the trie caches the result of any operation,
|
2022-10-11 07:37:00 +00:00
|
|
|
// we cannot use b.N as the number of hashing rounds, since all rounds apart from
|
2017-10-13 08:10:02 +00: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 11:59:45 +00: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 08:10:02 +00:00
|
|
|
func BenchmarkHash(b *testing.B) {
|
2020-01-17 11:59:45 +00: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
|
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
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2020-01-17 11:59:45 +00:00
|
|
|
i := 0
|
|
|
|
for ; i < len(addresses)/2; i++ {
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-01-17 11:59:45 +00:00
|
|
|
}
|
|
|
|
trie.Hash()
|
|
|
|
for ; i < len(addresses); i++ {
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-01-17 11:59:45 +00: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 07:37:00 +00:00
|
|
|
// we cannot use b.N as the number of hashing rounds, since all rounds apart from
|
2020-01-17 11:59:45 +00: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 08:03:20 +00:00
|
|
|
benchmarkCommitAfterHash(b, false)
|
2020-01-17 11:59:45 +00:00
|
|
|
})
|
|
|
|
b.Run("with-onleaf", func(b *testing.B) {
|
2022-08-04 08:03:20 +00:00
|
|
|
benchmarkCommitAfterHash(b, true)
|
2020-01-17 11:59:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-04 08:03:20 +00:00
|
|
|
func benchmarkCommitAfterHash(b *testing.B, collectLeaf bool) {
|
2017-10-13 08:10:02 +00:00
|
|
|
// Make the random benchmark deterministic
|
2020-01-17 11:59:45 +00:00
|
|
|
addresses, accounts := makeAccounts(b.N)
|
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
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2020-01-17 11:59:45 +00:00
|
|
|
for i := 0; i < len(addresses); i++ {
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-01-17 11:59:45 +00:00
|
|
|
}
|
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
trie.Hash()
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
2022-08-04 08:03:20 +00:00
|
|
|
trie.Commit(collectLeaf)
|
2020-01-17 11:59:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTinyTrie(t *testing.T) {
|
|
|
|
// Create a realistic account trie to hash
|
2020-11-19 21:50:47 +00:00
|
|
|
_, accounts := makeAccounts(5)
|
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
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001337"), accounts[3])
|
2020-11-19 21:50:47 +00:00
|
|
|
if exp, root := common.HexToHash("8c6a85a4d9fda98feff88450299e574e5378e32391f75a055d470ac0653f1005"), trie.Hash(); exp != root {
|
|
|
|
t.Errorf("1: got %x, exp %x", root, exp)
|
2020-01-17 11:59:45 +00:00
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001338"), accounts[4])
|
2020-11-19 21:50:47 +00:00
|
|
|
if exp, root := common.HexToHash("ec63b967e98a5720e7f720482151963982890d82c9093c0d486b7eb8883a66b1"), trie.Hash(); exp != root {
|
|
|
|
t.Errorf("2: got %x, exp %x", root, exp)
|
2020-01-17 11:59:45 +00:00
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001339"), accounts[4])
|
2020-11-19 21:50:47 +00:00
|
|
|
if exp, root := common.HexToHash("0608c1d1dc3905fa22204c7a0e43644831c3b6d3def0f274be623a948197e64a"), trie.Hash(); exp != root {
|
|
|
|
t.Errorf("3: got %x, exp %x", root, exp)
|
2020-01-17 11:59:45 +00:00
|
|
|
}
|
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
|
|
|
checktr := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
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
|
|
|
it := NewIterator(trie.MustNodeIterator(nil))
|
2020-01-17 11:59:45 +00:00
|
|
|
for it.Next() {
|
2023-04-20 10:57:24 +00:00
|
|
|
checktr.MustUpdate(it.Key, it.Value)
|
2020-01-17 11:59:45 +00: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 08:10:02 +00:00
|
|
|
|
2020-01-17 11:59:45 +00:00
|
|
|
func TestCommitAfterHash(t *testing.T) {
|
2017-10-13 08:10:02 +00:00
|
|
|
// Create a realistic account trie to hash
|
2020-01-17 11:59:45 +00:00
|
|
|
addresses, accounts := makeAccounts(1000)
|
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
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2020-01-17 11:59:45 +00:00
|
|
|
for i := 0; i < len(addresses); i++ {
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-01-17 11:59:45 +00:00
|
|
|
}
|
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
trie.Hash()
|
2022-08-04 08:03:20 +00:00
|
|
|
trie.Commit(false)
|
2020-01-17 11:59:45 +00:00
|
|
|
root := trie.Hash()
|
2020-11-19 21:50:47 +00:00
|
|
|
exp := common.HexToHash("72f9d3f3fe1e1dd7b8936442e7642aef76371472d94319900790053c493f3fe6")
|
2020-01-17 11:59:45 +00:00
|
|
|
if exp != root {
|
|
|
|
t.Errorf("got %x, exp %x", root, exp)
|
|
|
|
}
|
2023-06-27 12:36:38 +00:00
|
|
|
root, _, _ = trie.Commit(false)
|
2020-01-17 11:59:45 +00: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 08:10:02 +00:00
|
|
|
for i := 0; i < len(addresses); i++ {
|
2020-11-19 21:50:47 +00:00
|
|
|
data := make([]byte, 20)
|
|
|
|
random.Read(data)
|
|
|
|
copy(addresses[i][:], data)
|
2017-10-13 08:10:02 +00:00
|
|
|
}
|
2020-01-17 11:59:45 +00:00
|
|
|
accounts = make([][]byte, len(addresses))
|
2017-10-13 08:10:02 +00:00
|
|
|
for i := 0; i < len(accounts); i++ {
|
|
|
|
var (
|
2020-11-19 21:50:47 +00:00
|
|
|
nonce = uint64(random.Int63())
|
2023-02-21 11:12:27 +00:00
|
|
|
root = types.EmptyRootHash
|
2020-11-19 21:50:47 +00:00
|
|
|
code = crypto.Keccak256(nil)
|
2017-10-13 08:10:02 +00:00
|
|
|
)
|
2020-11-19 21:50:47 +00: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)
|
|
|
|
balance := new(big.Int).SetBytes(balanceBytes)
|
2021-09-28 08:48:07 +00:00
|
|
|
data, _ := rlp.EncodeToBytes(&types.StateAccount{Nonce: nonce, Balance: balance, Root: root, CodeHash: code})
|
2020-11-19 21:50:47 +00:00
|
|
|
accounts[i] = data
|
2017-10-13 08:10:02 +00:00
|
|
|
}
|
2020-01-17 11:59:45 +00:00
|
|
|
return addresses, accounts
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:45:56 +00:00
|
|
|
// spongeDb is a dummy db backend which accumulates writes in a sponge
|
|
|
|
type spongeDb struct {
|
2020-09-30 17:49:20 +00:00
|
|
|
sponge hash.Hash
|
|
|
|
id string
|
|
|
|
journal []string
|
2020-09-30 11:45:56 +00: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 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-09-30 11:45:56 +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 {
|
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
|
|
|
var (
|
|
|
|
keybrief = key
|
|
|
|
valbrief = value
|
|
|
|
)
|
|
|
|
if len(keybrief) > 8 {
|
|
|
|
keybrief = keybrief[:8]
|
|
|
|
}
|
2020-09-30 17:49:20 +00: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 19:21:36 +00:00
|
|
|
s.journal = append(s.journal, fmt.Sprintf("%v: PUT([%x...], [%d bytes] %x...)\n", s.id, keybrief, len(value), valbrief))
|
2020-09-30 11:45:56 +00:00
|
|
|
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 }
|
|
|
|
|
|
|
|
// TestCommitSequence tests that the trie.Commit operation writes the elements of the trie
|
2023-02-08 11:14:34 +00:00
|
|
|
// in the expected order.
|
2020-09-30 11:45:56 +00: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 11:14:34 +00:00
|
|
|
count int
|
|
|
|
expWriteSeqHash []byte
|
2020-09-30 11:45:56 +00:00
|
|
|
}{
|
2023-02-08 11:14:34 +00:00
|
|
|
{20, common.FromHex("873c78df73d60e59d4a2bcf3716e8bfe14554549fea2fc147cb54129382a8066")},
|
|
|
|
{200, common.FromHex("ba03d891bb15408c940eea5ee3d54d419595102648d02774a0268d892add9c8e")},
|
|
|
|
{2000, common.FromHex("f7a184f20df01c94f09537401d11e68d97ad0c00115233107f51b9c287ce60c7")},
|
2020-09-30 11:45:56 +00:00
|
|
|
} {
|
|
|
|
addresses, accounts := makeAccounts(tc.count)
|
|
|
|
// This spongeDb is used to check the sequence of disk-db-writes
|
|
|
|
s := &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
|
|
|
db := NewDatabase(rawdb.NewDatabase(s), nil)
|
2022-06-06 15:14:55 +00:00
|
|
|
trie := NewEmpty(db)
|
2020-09-30 11:45:56 +00:00
|
|
|
// Fill the trie with elements
|
|
|
|
for i := 0; i < tc.count; i++ {
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-09-30 11:45:56 +00:00
|
|
|
}
|
|
|
|
// Flush trie -> database
|
2023-06-27 12:36:38 +00:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2023-07-24 10:22:09 +00:00
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
|
2020-09-30 11:45:56 +00:00
|
|
|
// Flush memdb -> disk (sponge)
|
2023-02-08 11:14:34 +00:00
|
|
|
db.Commit(root, false)
|
2020-09-30 11:45:56 +00:00
|
|
|
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) {
|
2020-11-19 21:50:47 +00:00
|
|
|
t.Errorf("test %d, disk write sequence wrong:\ngot %x exp %x\n", i, got, exp)
|
2020-09-30 11:45:56 +00: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 11:14:34 +00:00
|
|
|
count int
|
|
|
|
expWriteSeqHash []byte
|
2020-09-30 11:45:56 +00:00
|
|
|
}{
|
2023-02-08 11:14:34 +00:00
|
|
|
{20, common.FromHex("8e4a01548551d139fa9e833ebc4e66fc1ba40a4b9b7259d80db32cff7b64ebbc")},
|
|
|
|
{200, common.FromHex("6869b4e7b95f3097a19ddb30ff735f922b915314047e041614df06958fc50554")},
|
|
|
|
{2000, common.FromHex("444200e6f4e2df49f77752f629a96ccf7445d4698c164f962bbd85a0526ef424")},
|
2020-09-30 11:45:56 +00:00
|
|
|
} {
|
|
|
|
prng := rand.New(rand.NewSource(int64(i)))
|
|
|
|
// This spongeDb is used to check the sequence of disk-db-writes
|
|
|
|
s := &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
|
|
|
db := NewDatabase(rawdb.NewDatabase(s), nil)
|
2022-06-06 15:14:55 +00:00
|
|
|
trie := NewEmpty(db)
|
2020-09-30 11:45:56 +00: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 10:57:24 +00:00
|
|
|
trie.MustUpdate(key, val)
|
2020-09-30 11:45:56 +00:00
|
|
|
}
|
|
|
|
// Flush trie -> database
|
2023-06-27 12:36:38 +00:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2023-07-24 10:22:09 +00:00
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
|
2020-09-30 11:45:56 +00:00
|
|
|
// Flush memdb -> disk (sponge)
|
2023-02-08 11:14:34 +00:00
|
|
|
db.Commit(root, false)
|
2020-09-30 11:45:56 +00: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 17:49:20 +00: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
|
|
|
|
s := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "a"}
|
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
|
|
|
db := NewDatabase(rawdb.NewDatabase(s), nil)
|
2022-06-06 15:14:55 +00:00
|
|
|
trie := NewEmpty(db)
|
2020-09-30 17:49:20 +00:00
|
|
|
// Another sponge is used for the stacktrie commits
|
|
|
|
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"}
|
2023-10-11 04:12:45 +00:00
|
|
|
stTrie := NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
|
|
|
|
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme())
|
2022-11-28 13:31:28 +00:00
|
|
|
})
|
2020-09-30 17:49:20 +00:00
|
|
|
// Fill the trie with elements
|
2022-08-04 08:03:20 +00:00
|
|
|
for i := 0; i < count; i++ {
|
2020-09-30 17:49:20 +00: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 10:57:24 +00:00
|
|
|
trie.Update(key, val)
|
|
|
|
stTrie.Update(key, val)
|
2020-09-30 17:49:20 +00:00
|
|
|
}
|
|
|
|
// Flush trie -> database
|
2023-06-27 12:36:38 +00:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2020-09-30 17:49:20 +00:00
|
|
|
// Flush memdb -> disk (sponge)
|
2023-07-24 10:22:09 +00:00
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
|
2023-02-08 11:14:34 +00:00
|
|
|
db.Commit(root, false)
|
2020-09-30 17:49:20 +00:00
|
|
|
// And flush stacktrie -> disk
|
2020-10-12 10:08:04 +00:00
|
|
|
stRoot, err := stTrie.Commit()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to commit stack trie %v", err)
|
|
|
|
}
|
2020-09-30 17:49:20 +00:00
|
|
|
if stRoot != root {
|
|
|
|
t.Fatalf("root wrong, got %x exp %x", stRoot, root)
|
|
|
|
}
|
|
|
|
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 14:08:12 +00: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) {
|
|
|
|
s := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "a"}
|
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
|
|
|
db := NewDatabase(rawdb.NewDatabase(s), nil)
|
2022-06-06 15:14:55 +00:00
|
|
|
trie := NewEmpty(db)
|
2020-11-09 14:08:12 +00:00
|
|
|
// Another sponge is used for the stacktrie commits
|
|
|
|
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"}
|
2023-10-11 04:12:45 +00:00
|
|
|
stTrie := NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
|
|
|
|
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme())
|
2022-11-28 13:31:28 +00:00
|
|
|
})
|
2020-11-09 14:08:12 +00:00
|
|
|
// Add a single small-element to the trie(s)
|
|
|
|
key := make([]byte, 5)
|
|
|
|
key[0] = 1
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.Update(key, []byte{0x1})
|
|
|
|
stTrie.Update(key, []byte{0x1})
|
2020-11-09 14:08:12 +00:00
|
|
|
// Flush trie -> database
|
2023-06-27 12:36:38 +00:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2020-11-09 14:08:12 +00:00
|
|
|
// Flush memdb -> disk (sponge)
|
2023-07-24 10:22:09 +00:00
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
|
2023-02-08 11:14:34 +00:00
|
|
|
db.Commit(root, false)
|
2020-11-09 14:08:12 +00:00
|
|
|
// And flush stacktrie -> disk
|
|
|
|
stRoot, err := stTrie.Commit()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to commit stack trie %v", err)
|
|
|
|
}
|
|
|
|
if stRoot != root {
|
|
|
|
t.Fatalf("root wrong, got %x exp %x", stRoot, root)
|
|
|
|
}
|
2022-03-09 13:45:17 +00:00
|
|
|
|
|
|
|
t.Logf("root: %x\n", stRoot)
|
2020-11-09 14:08:12 +00: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:59:45 +00:00
|
|
|
// BenchmarkCommitAfterHashFixedSize benchmarks the Commit (after Hash) of a fixed number of updates to a trie.
|
|
|
|
// 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()
|
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
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2017-10-13 08:10:02 +00:00
|
|
|
for i := 0; i < len(addresses); i++ {
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2015-06-30 12:54:37 +00:00
|
|
|
}
|
2020-01-17 11:59:45 +00: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 08:10:02 +00:00
|
|
|
b.ReportAllocs()
|
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
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil))
|
2020-01-17 11:59:45 +00:00
|
|
|
for i := 0; i < len(addresses); i++ {
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-01-17 11:59:45 +00:00
|
|
|
}
|
|
|
|
// Insert the accounts into the trie and hash it
|
2017-10-13 08:10:02 +00:00
|
|
|
trie.Hash()
|
2020-01-17 11:59:45 +00:00
|
|
|
b.StartTimer()
|
2022-08-04 08:03:20 +00:00
|
|
|
trie.Commit(false)
|
2020-01-17 11:59:45 +00:00
|
|
|
b.StopTimer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDerefRootFixedSize(b *testing.B) {
|
|
|
|
b.Run("10", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(20)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkDerefRootFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("100", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(100)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkDerefRootFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Run("1K", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(1000)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkDerefRootFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("10K", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(10000)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkDerefRootFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("100K", func(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
acc, add := makeAccounts(100000)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkDerefRootFixedSize(b, acc, add)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkDerefRootFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) {
|
|
|
|
b.ReportAllocs()
|
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
|
|
|
triedb := NewDatabase(rawdb.NewMemoryDatabase(), nil)
|
2022-08-04 08:03:20 +00:00
|
|
|
trie := NewEmpty(triedb)
|
2020-01-17 11:59:45 +00:00
|
|
|
for i := 0; i < len(addresses); i++ {
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i])
|
2020-01-17 11:59:45 +00:00
|
|
|
}
|
|
|
|
h := trie.Hash()
|
2023-06-27 12:36:38 +00:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2023-07-24 10:22:09 +00:00
|
|
|
triedb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil)
|
2020-01-17 11:59:45 +00:00
|
|
|
b.StartTimer()
|
2022-08-04 08:03:20 +00:00
|
|
|
triedb.Dereference(h)
|
2020-01-17 11:59:45 +00:00
|
|
|
b.StopTimer()
|
2015-06-30 12:54:37 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
func getString(trie *Trie, k string) []byte {
|
2023-04-20 10:57:24 +00:00
|
|
|
return trie.MustGet([]byte(k))
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateString(trie *Trie, k, v string) {
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustUpdate([]byte(k), []byte(v))
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteString(trie *Trie, k string) {
|
2023-04-20 10:57:24 +00:00
|
|
|
trie.MustDelete([]byte(k))
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
2019-02-16 15:16:12 +00:00
|
|
|
|
|
|
|
func TestDecodeNode(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-03-20 08:09:35 +00:00
|
|
|
|
2019-02-16 15:16:12 +00:00
|
|
|
var (
|
|
|
|
hash = make([]byte, 20)
|
|
|
|
elems = make([]byte, 20)
|
|
|
|
)
|
|
|
|
for i := 0; i < 5000000; i++ {
|
2023-03-20 08:09:35 +00:00
|
|
|
prng.Read(hash)
|
|
|
|
prng.Read(elems)
|
2019-03-14 13:25:12 +00:00
|
|
|
decodeNode(hash, elems)
|
2019-02-16 15:16:12 +00:00
|
|
|
}
|
|
|
|
}
|