2015-07-06 01:19:48 +02:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package trie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-08-04 16:03:20 +08:00
|
|
|
"fmt"
|
2016-09-27 13:13:13 +03:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
2015-07-06 01:19:48 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-07-29 17:16:53 +08:00
|
|
|
"github.com/ethereum/go-ethereum/common/gopool"
|
2022-11-28 21:31:28 +08:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2023-05-09 15:11:04 +08:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-07-06 01:19:48 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2023-05-09 15:11:04 +08:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
2015-07-06 01:19:48 +02:00
|
|
|
)
|
|
|
|
|
2022-08-04 16:13:18 +02:00
|
|
|
func newEmptySecure() *StateTrie {
|
2024-02-13 21:49:53 +08:00
|
|
|
trie, _ := NewStateTrie(TrieID(types.EmptyRootHash), newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme))
|
2015-07-06 01:19:48 +02:00
|
|
|
return trie
|
|
|
|
}
|
|
|
|
|
2022-08-04 16:13:18 +02:00
|
|
|
// makeTestStateTrie creates a large enough secure trie for testing.
|
2024-02-13 21:49:53 +08:00
|
|
|
func makeTestStateTrie() (*testDb, *StateTrie, map[string][]byte) {
|
2016-09-27 13:13:13 +03:00
|
|
|
// Create an empty trie
|
2024-02-13 21:49:53 +08:00
|
|
|
triedb := newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)
|
2023-05-11 15:19:42 +08:00
|
|
|
trie, _ := NewStateTrie(TrieID(types.EmptyRootHash), triedb)
|
2016-09-27 13:13:13 +03:00
|
|
|
|
|
|
|
// Fill it with some arbitrary data
|
|
|
|
content := make(map[string][]byte)
|
|
|
|
for i := byte(0); i < 255; i++ {
|
|
|
|
// Map the same data under multiple keys
|
|
|
|
key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i}
|
|
|
|
content[string(key)] = val
|
2023-04-20 18:57:24 +08:00
|
|
|
trie.MustUpdate(key, val)
|
2016-09-27 13:13:13 +03:00
|
|
|
|
|
|
|
key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i}
|
|
|
|
content[string(key)] = val
|
2023-04-20 18:57:24 +08:00
|
|
|
trie.MustUpdate(key, val)
|
2016-09-27 13:13:13 +03:00
|
|
|
|
2017-06-12 14:45:17 +02:00
|
|
|
// Add some other data to inflate the trie
|
2016-09-27 13:13:13 +03:00
|
|
|
for j := byte(3); j < 13; j++ {
|
|
|
|
key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i}
|
|
|
|
content[string(key)] = val
|
2023-04-20 18:57:24 +08:00
|
|
|
trie.MustUpdate(key, val)
|
2016-09-27 13:13:13 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-27 14:36:38 +02:00
|
|
|
root, nodes, _ := trie.Commit(false)
|
2024-02-13 21:49:53 +08:00
|
|
|
if err := triedb.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)); err != nil {
|
2022-08-04 16:03:20 +08:00
|
|
|
panic(fmt.Errorf("failed to commit db %v", err))
|
|
|
|
}
|
|
|
|
// Re-create the trie based on the new state
|
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 16:01:02 +08:00
|
|
|
trie, _ = NewStateTrie(TrieID(root), triedb)
|
2018-02-05 18:40:32 +02:00
|
|
|
return triedb, trie, content
|
2016-09-27 13:13:13 +03:00
|
|
|
}
|
|
|
|
|
2015-07-06 01:19:48 +02:00
|
|
|
func TestSecureDelete(t *testing.T) {
|
|
|
|
trie := newEmptySecure()
|
|
|
|
vals := []struct{ k, v string }{
|
|
|
|
{"do", "verb"},
|
|
|
|
{"ether", "wookiedoo"},
|
|
|
|
{"horse", "stallion"},
|
|
|
|
{"shaman", "horse"},
|
|
|
|
{"doge", "coin"},
|
|
|
|
{"ether", ""},
|
|
|
|
{"dog", "puppy"},
|
|
|
|
{"shaman", ""},
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
if val.v != "" {
|
2023-04-20 18:57:24 +08:00
|
|
|
trie.MustUpdate([]byte(val.k), []byte(val.v))
|
2015-07-06 01:19:48 +02:00
|
|
|
} else {
|
2023-04-20 18:57:24 +08:00
|
|
|
trie.MustDelete([]byte(val.k))
|
2015-07-06 01:19:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hash := trie.Hash()
|
|
|
|
exp := common.HexToHash("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d")
|
|
|
|
if hash != exp {
|
|
|
|
t.Errorf("expected %x got %x", exp, hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecureGetKey(t *testing.T) {
|
|
|
|
trie := newEmptySecure()
|
2023-04-20 18:57:24 +08:00
|
|
|
trie.MustUpdate([]byte("foo"), []byte("bar"))
|
2015-07-06 01:19:48 +02:00
|
|
|
|
|
|
|
key := []byte("foo")
|
|
|
|
value := []byte("bar")
|
2016-02-21 18:40:27 +00:00
|
|
|
seckey := crypto.Keccak256(key)
|
2015-07-06 01:19:48 +02:00
|
|
|
|
2023-04-20 18:57:24 +08:00
|
|
|
if !bytes.Equal(trie.MustGet(key), value) {
|
2015-07-06 01:19:48 +02:00
|
|
|
t.Errorf("Get did not return bar")
|
|
|
|
}
|
|
|
|
if k := trie.GetKey(seckey); !bytes.Equal(k, key) {
|
|
|
|
t.Errorf("GetKey returned %q, want %q", k, key)
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 13:13:13 +03:00
|
|
|
|
2022-08-04 16:13:18 +02:00
|
|
|
func TestStateTrieConcurrency(t *testing.T) {
|
2016-09-27 13:13:13 +03:00
|
|
|
// Create an initial trie and copy if for concurrent access
|
2022-08-04 16:13:18 +02:00
|
|
|
_, trie, _ := makeTestStateTrie()
|
2016-09-27 13:13:13 +03:00
|
|
|
|
|
|
|
threads := runtime.NumCPU()
|
2022-08-04 16:13:18 +02:00
|
|
|
tries := make([]*StateTrie, threads)
|
2016-09-27 13:13:13 +03:00
|
|
|
for i := 0; i < threads; i++ {
|
2022-03-31 15:28:32 +08:00
|
|
|
tries[i] = trie.Copy()
|
2016-09-27 13:13:13 +03:00
|
|
|
}
|
2022-08-19 01:00:21 -05:00
|
|
|
// Start a batch of goroutines interacting with the trie
|
2016-09-27 13:13:13 +03:00
|
|
|
pend := new(sync.WaitGroup)
|
|
|
|
pend.Add(threads)
|
|
|
|
for i := 0; i < threads; i++ {
|
2021-07-29 17:16:53 +08:00
|
|
|
index := i
|
|
|
|
gopool.Submit(func() {
|
2016-09-27 13:13:13 +03:00
|
|
|
defer pend.Done()
|
|
|
|
|
|
|
|
for j := byte(0); j < 255; j++ {
|
|
|
|
// Map the same data under multiple keys
|
|
|
|
key, val := common.LeftPadBytes([]byte{byte(index), 1, j}, 32), []byte{j}
|
2023-04-20 18:57:24 +08:00
|
|
|
tries[index].MustUpdate(key, val)
|
2016-09-27 13:13:13 +03:00
|
|
|
|
|
|
|
key, val = common.LeftPadBytes([]byte{byte(index), 2, j}, 32), []byte{j}
|
2023-04-20 18:57:24 +08:00
|
|
|
tries[index].MustUpdate(key, val)
|
2016-09-27 13:13:13 +03:00
|
|
|
|
|
|
|
// Add some other data to inflate the trie
|
|
|
|
for k := byte(3); k < 13; k++ {
|
|
|
|
key, val = common.LeftPadBytes([]byte{byte(index), k, j}, 32), []byte{k, j}
|
2023-04-20 18:57:24 +08:00
|
|
|
tries[index].MustUpdate(key, val)
|
2016-09-27 13:13:13 +03:00
|
|
|
}
|
|
|
|
}
|
2022-08-04 16:03:20 +08:00
|
|
|
tries[index].Commit(false)
|
2021-07-29 17:16:53 +08:00
|
|
|
})
|
2016-09-27 13:13:13 +03:00
|
|
|
}
|
|
|
|
// Wait for all threads to finish
|
|
|
|
pend.Wait()
|
|
|
|
}
|