2016-04-14 11:18:24 -05:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-01-20 04:09:24 -06:00
|
|
|
// 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 state
|
|
|
|
|
|
|
|
import (
|
2016-10-04 05:36:02 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
"errors"
|
2016-10-04 05:36:02 -05:00
|
|
|
"fmt"
|
2024-04-25 02:56:25 -05:00
|
|
|
"maps"
|
2016-10-04 05:36:02 -05:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
2024-04-25 02:56:25 -05:00
|
|
|
"slices"
|
2016-10-04 05:36:02 -05:00
|
|
|
"strings"
|
2019-09-26 04:09:59 -05:00
|
|
|
"sync"
|
2016-01-20 04:09:24 -06:00
|
|
|
"testing"
|
2016-10-04 05:36:02 -05:00
|
|
|
"testing/quick"
|
2016-01-20 04:09:24 -06:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-09-24 07:57:49 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2023-06-05 08:25:57 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/state/snapshot"
|
2024-03-22 12:53:53 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
2017-01-05 07:03:50 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-06-05 08:25:57 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2023-08-23 14:53:31 -05:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2023-06-05 08:25:57 -05:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2023-08-26 03:13:22 -05:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
2024-02-13 07:49:53 -06:00
|
|
|
"github.com/ethereum/go-ethereum/triedb"
|
|
|
|
"github.com/ethereum/go-ethereum/triedb/hashdb"
|
|
|
|
"github.com/ethereum/go-ethereum/triedb/pathdb"
|
2023-08-26 03:13:22 -05:00
|
|
|
"github.com/holiman/uint256"
|
2016-01-20 04:09:24 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Tests that updating a state trie does not leak any database writes prior to
|
|
|
|
// actually committing the state.
|
|
|
|
func TestUpdateLeaks(t *testing.T) {
|
|
|
|
// Create an empty state database
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
var (
|
|
|
|
db = rawdb.NewMemoryDatabase()
|
2024-02-13 07:49:53 -06:00
|
|
|
tdb = triedb.NewDatabase(db, nil)
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
)
|
|
|
|
state, _ := New(types.EmptyRootHash, NewDatabaseWithNodeDB(db, tdb), nil)
|
2016-01-20 04:09:24 -06:00
|
|
|
|
|
|
|
// Update it with some accounts
|
|
|
|
for i := byte(0); i < 255; i++ {
|
2016-10-04 05:36:02 -05:00
|
|
|
addr := common.BytesToAddress([]byte{i})
|
2024-03-22 12:53:53 -05:00
|
|
|
state.AddBalance(addr, uint256.NewInt(uint64(11*i)), tracing.BalanceChangeUnspecified)
|
2016-10-04 05:36:02 -05:00
|
|
|
state.SetNonce(addr, uint64(42*i))
|
2016-01-20 04:09:24 -06:00
|
|
|
if i%2 == 0 {
|
2016-10-04 05:36:02 -05:00
|
|
|
state.SetState(addr, common.BytesToHash([]byte{i, i, i}), common.BytesToHash([]byte{i, i, i, i}))
|
2016-01-20 04:09:24 -06:00
|
|
|
}
|
|
|
|
if i%3 == 0 {
|
2016-10-04 05:36:02 -05:00
|
|
|
state.SetCode(addr, []byte{i, i, i, i, i})
|
2016-01-20 04:09:24 -06:00
|
|
|
}
|
|
|
|
}
|
2019-09-26 04:09:59 -05:00
|
|
|
|
|
|
|
root := state.IntermediateRoot(false)
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
if err := tdb.Commit(root, false); err != nil {
|
2019-09-26 04:09:59 -05:00
|
|
|
t.Errorf("can not commit trie %v to persistent database", root.Hex())
|
|
|
|
}
|
|
|
|
|
2016-01-20 04:09:24 -06:00
|
|
|
// Ensure that no data was leaked into the database
|
2020-04-15 06:08:53 -05:00
|
|
|
it := db.NewIterator(nil, nil)
|
2018-09-24 07:57:49 -05:00
|
|
|
for it.Next() {
|
|
|
|
t.Errorf("State leaked into database: %x -> %x", it.Key(), it.Value())
|
2016-01-20 04:09:24 -06:00
|
|
|
}
|
2018-09-24 07:57:49 -05:00
|
|
|
it.Release()
|
2016-01-20 04:09:24 -06:00
|
|
|
}
|
2016-01-20 08:06:28 -06:00
|
|
|
|
|
|
|
// Tests that no intermediate state of an object is stored into the database,
|
|
|
|
// only the one right before the commit.
|
|
|
|
func TestIntermediateLeaks(t *testing.T) {
|
|
|
|
// Create two state databases, one transitioning to the final state, the other final from the beginning
|
2018-09-24 07:57:49 -05:00
|
|
|
transDb := rawdb.NewMemoryDatabase()
|
|
|
|
finalDb := rawdb.NewMemoryDatabase()
|
2024-02-13 07:49:53 -06:00
|
|
|
transNdb := triedb.NewDatabase(transDb, nil)
|
|
|
|
finalNdb := triedb.NewDatabase(finalDb, nil)
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
transState, _ := New(types.EmptyRootHash, NewDatabaseWithNodeDB(transDb, transNdb), nil)
|
|
|
|
finalState, _ := New(types.EmptyRootHash, NewDatabaseWithNodeDB(finalDb, finalNdb), nil)
|
2016-01-20 08:06:28 -06:00
|
|
|
|
2016-10-04 05:36:02 -05:00
|
|
|
modify := func(state *StateDB, addr common.Address, i, tweak byte) {
|
2024-03-22 12:53:53 -05:00
|
|
|
state.SetBalance(addr, uint256.NewInt(uint64(11*i)+uint64(tweak)), tracing.BalanceChangeUnspecified)
|
2016-10-04 05:36:02 -05:00
|
|
|
state.SetNonce(addr, uint64(42*i+tweak))
|
2016-01-20 08:06:28 -06:00
|
|
|
if i%2 == 0 {
|
2016-10-04 05:36:02 -05:00
|
|
|
state.SetState(addr, common.Hash{i, i, i, 0}, common.Hash{})
|
|
|
|
state.SetState(addr, common.Hash{i, i, i, tweak}, common.Hash{i, i, i, i, tweak})
|
2016-01-20 08:06:28 -06:00
|
|
|
}
|
|
|
|
if i%3 == 0 {
|
2016-10-04 05:36:02 -05:00
|
|
|
state.SetCode(addr, []byte{i, i, i, i, i, tweak})
|
2016-01-20 08:06:28 -06:00
|
|
|
}
|
2016-10-04 05:36:02 -05:00
|
|
|
}
|
2016-01-20 08:06:28 -06:00
|
|
|
|
2016-10-04 05:36:02 -05:00
|
|
|
// Modify the transient state.
|
|
|
|
for i := byte(0); i < 255; i++ {
|
2019-07-22 02:30:09 -05:00
|
|
|
modify(transState, common.Address{i}, i, 0)
|
2016-10-04 05:36:02 -05:00
|
|
|
}
|
|
|
|
// Write modifications to trie.
|
2016-10-20 06:36:29 -05:00
|
|
|
transState.IntermediateRoot(false)
|
2016-01-20 08:06:28 -06:00
|
|
|
|
2016-10-04 05:36:02 -05:00
|
|
|
// Overwrite all the data with new values in the transient database.
|
|
|
|
for i := byte(0); i < 255; i++ {
|
2019-07-22 02:30:09 -05:00
|
|
|
modify(transState, common.Address{i}, i, 99)
|
|
|
|
modify(finalState, common.Address{i}, i, 99)
|
2016-01-20 08:06:28 -06:00
|
|
|
}
|
2016-10-04 05:36:02 -05:00
|
|
|
|
|
|
|
// Commit and cross check the databases.
|
2023-07-24 05:22:09 -05:00
|
|
|
transRoot, err := transState.Commit(0, false)
|
2019-09-26 04:09:59 -05:00
|
|
|
if err != nil {
|
2016-01-20 08:06:28 -06:00
|
|
|
t.Fatalf("failed to commit transition state: %v", err)
|
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
if err = transNdb.Commit(transRoot, false); err != nil {
|
2019-09-26 04:09:59 -05:00
|
|
|
t.Errorf("can not commit trie %v to persistent database", transRoot.Hex())
|
|
|
|
}
|
|
|
|
|
2023-07-24 05:22:09 -05:00
|
|
|
finalRoot, err := finalState.Commit(0, false)
|
2019-09-26 04:09:59 -05:00
|
|
|
if err != nil {
|
2016-01-20 08:06:28 -06:00
|
|
|
t.Fatalf("failed to commit final state: %v", err)
|
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
if err = finalNdb.Commit(finalRoot, false); err != nil {
|
2019-09-26 04:09:59 -05:00
|
|
|
t.Errorf("can not commit trie %v to persistent database", finalRoot.Hex())
|
|
|
|
}
|
|
|
|
|
2020-04-15 06:08:53 -05:00
|
|
|
it := finalDb.NewIterator(nil, nil)
|
2018-09-24 07:57:49 -05:00
|
|
|
for it.Next() {
|
2019-09-26 04:09:59 -05:00
|
|
|
key, fvalue := it.Key(), it.Value()
|
|
|
|
tvalue, err := transDb.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("entry missing from the transition database: %x -> %x", key, fvalue)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(fvalue, tvalue) {
|
2021-05-31 05:43:18 -05:00
|
|
|
t.Errorf("value mismatch at key %x: %x in transition database, %x in final database", key, tvalue, fvalue)
|
2016-01-20 08:06:28 -06:00
|
|
|
}
|
|
|
|
}
|
2018-09-24 07:57:49 -05:00
|
|
|
it.Release()
|
|
|
|
|
2020-04-15 06:08:53 -05:00
|
|
|
it = transDb.NewIterator(nil, nil)
|
2018-09-24 07:57:49 -05:00
|
|
|
for it.Next() {
|
2019-09-26 04:09:59 -05:00
|
|
|
key, tvalue := it.Key(), it.Value()
|
|
|
|
fvalue, err := finalDb.Get(key)
|
|
|
|
if err != nil {
|
2018-09-24 07:57:49 -05:00
|
|
|
t.Errorf("extra entry in the transition database: %x -> %x", key, it.Value())
|
2016-01-20 08:06:28 -06:00
|
|
|
}
|
2019-09-26 04:09:59 -05:00
|
|
|
if !bytes.Equal(fvalue, tvalue) {
|
2021-05-31 05:43:18 -05:00
|
|
|
t.Errorf("value mismatch at key %x: %x in transition database, %x in final database", key, tvalue, fvalue)
|
2019-09-26 04:09:59 -05:00
|
|
|
}
|
2016-01-20 08:06:28 -06:00
|
|
|
}
|
|
|
|
}
|
2016-10-04 05:36:02 -05:00
|
|
|
|
2020-08-19 01:54:21 -05:00
|
|
|
// TestCopy tests that copying a StateDB object indeed makes the original and
|
2017-11-24 03:02:25 -06:00
|
|
|
// the copy independent of each other. This test is a regression test against
|
|
|
|
// https://github.com/ethereum/go-ethereum/pull/15549.
|
|
|
|
func TestCopy(t *testing.T) {
|
|
|
|
// Create a random state test to copy and modify "independently"
|
2023-05-11 02:19:42 -05:00
|
|
|
orig, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2017-11-24 03:02:25 -06:00
|
|
|
|
|
|
|
for i := byte(0); i < 255; i++ {
|
2024-01-14 05:32:23 -06:00
|
|
|
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
2024-03-22 12:53:53 -05:00
|
|
|
obj.AddBalance(uint256.NewInt(uint64(i)), tracing.BalanceChangeUnspecified)
|
2017-11-24 03:02:25 -06:00
|
|
|
orig.updateStateObject(obj)
|
|
|
|
}
|
|
|
|
orig.Finalise(false)
|
|
|
|
|
2019-09-26 04:09:59 -05:00
|
|
|
// Copy the state
|
2017-11-24 03:02:25 -06:00
|
|
|
copy := orig.Copy()
|
|
|
|
|
2019-09-26 04:09:59 -05:00
|
|
|
// Copy the copy state
|
|
|
|
ccopy := copy.Copy()
|
|
|
|
|
|
|
|
// modify all in memory
|
2017-11-24 03:02:25 -06:00
|
|
|
for i := byte(0); i < 255; i++ {
|
2024-01-14 05:32:23 -06:00
|
|
|
origObj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
|
|
|
copyObj := copy.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
|
|
|
ccopyObj := ccopy.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
2017-11-24 03:02:25 -06:00
|
|
|
|
2024-03-22 12:53:53 -05:00
|
|
|
origObj.AddBalance(uint256.NewInt(2*uint64(i)), tracing.BalanceChangeUnspecified)
|
|
|
|
copyObj.AddBalance(uint256.NewInt(3*uint64(i)), tracing.BalanceChangeUnspecified)
|
|
|
|
ccopyObj.AddBalance(uint256.NewInt(4*uint64(i)), tracing.BalanceChangeUnspecified)
|
2017-11-24 03:02:25 -06:00
|
|
|
|
|
|
|
orig.updateStateObject(origObj)
|
|
|
|
copy.updateStateObject(copyObj)
|
2019-09-26 04:09:59 -05:00
|
|
|
ccopy.updateStateObject(copyObj)
|
2017-11-24 03:02:25 -06:00
|
|
|
}
|
|
|
|
|
2019-09-26 04:09:59 -05:00
|
|
|
// Finalise the changes on all concurrently
|
|
|
|
finalise := func(wg *sync.WaitGroup, db *StateDB) {
|
|
|
|
defer wg.Done()
|
|
|
|
db.Finalise(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(3)
|
|
|
|
go finalise(&wg, orig)
|
|
|
|
go finalise(&wg, copy)
|
|
|
|
go finalise(&wg, ccopy)
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// Verify that the three states have been updated independently
|
2017-11-24 03:02:25 -06:00
|
|
|
for i := byte(0); i < 255; i++ {
|
2024-01-14 05:32:23 -06:00
|
|
|
origObj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
|
|
|
copyObj := copy.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
|
|
|
ccopyObj := ccopy.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
2017-11-24 03:02:25 -06:00
|
|
|
|
2024-01-23 07:51:58 -06:00
|
|
|
if want := uint256.NewInt(3 * uint64(i)); origObj.Balance().Cmp(want) != 0 {
|
2017-11-24 03:02:25 -06:00
|
|
|
t.Errorf("orig obj %d: balance mismatch: have %v, want %v", i, origObj.Balance(), want)
|
|
|
|
}
|
2024-01-23 07:51:58 -06:00
|
|
|
if want := uint256.NewInt(4 * uint64(i)); copyObj.Balance().Cmp(want) != 0 {
|
2017-11-24 03:02:25 -06:00
|
|
|
t.Errorf("copy obj %d: balance mismatch: have %v, want %v", i, copyObj.Balance(), want)
|
|
|
|
}
|
2024-01-23 07:51:58 -06:00
|
|
|
if want := uint256.NewInt(5 * uint64(i)); ccopyObj.Balance().Cmp(want) != 0 {
|
2019-09-26 04:09:59 -05:00
|
|
|
t.Errorf("copy obj %d: balance mismatch: have %v, want %v", i, ccopyObj.Balance(), want)
|
|
|
|
}
|
2017-11-24 03:02:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 04:59:06 -05:00
|
|
|
// TestCopyWithDirtyJournal tests if Copy can correct create a equal copied
|
|
|
|
// stateDB with dirty journal present.
|
|
|
|
func TestCopyWithDirtyJournal(t *testing.T) {
|
|
|
|
db := NewDatabase(rawdb.NewMemoryDatabase())
|
|
|
|
orig, _ := New(types.EmptyRootHash, db, nil)
|
|
|
|
|
|
|
|
// Fill up the initial states
|
|
|
|
for i := byte(0); i < 255; i++ {
|
|
|
|
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
|
|
|
obj.AddBalance(uint256.NewInt(uint64(i)), tracing.BalanceChangeUnspecified)
|
|
|
|
obj.data.Root = common.HexToHash("0xdeadbeef")
|
|
|
|
orig.updateStateObject(obj)
|
|
|
|
}
|
|
|
|
root, _ := orig.Commit(0, true)
|
|
|
|
orig, _ = New(root, db, nil)
|
|
|
|
|
|
|
|
// modify all in memory without finalizing
|
|
|
|
for i := byte(0); i < 255; i++ {
|
|
|
|
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
|
|
|
obj.SubBalance(uint256.NewInt(uint64(i)), tracing.BalanceChangeUnspecified)
|
|
|
|
orig.updateStateObject(obj)
|
|
|
|
}
|
|
|
|
cpy := orig.Copy()
|
|
|
|
|
|
|
|
orig.Finalise(true)
|
|
|
|
for i := byte(0); i < 255; i++ {
|
|
|
|
root := orig.GetStorageRoot(common.BytesToAddress([]byte{i}))
|
|
|
|
if root != (common.Hash{}) {
|
|
|
|
t.Errorf("Unexpected storage root %x", root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cpy.Finalise(true)
|
|
|
|
for i := byte(0); i < 255; i++ {
|
|
|
|
root := cpy.GetStorageRoot(common.BytesToAddress([]byte{i}))
|
|
|
|
if root != (common.Hash{}) {
|
|
|
|
t.Errorf("Unexpected storage root %x", root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cpy.IntermediateRoot(true) != orig.IntermediateRoot(true) {
|
|
|
|
t.Error("State is not equal after copy")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCopyObjectState creates an original state, S1, and makes a copy S2.
|
|
|
|
// It then proceeds to make changes to S1. Those changes are _not_ supposed
|
|
|
|
// to affect S2. This test checks that the copy properly deep-copies the objectstate
|
|
|
|
func TestCopyObjectState(t *testing.T) {
|
|
|
|
db := NewDatabase(rawdb.NewMemoryDatabase())
|
|
|
|
orig, _ := New(types.EmptyRootHash, db, nil)
|
|
|
|
|
|
|
|
// Fill up the initial states
|
|
|
|
for i := byte(0); i < 5; i++ {
|
|
|
|
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
|
|
|
obj.AddBalance(uint256.NewInt(uint64(i)), tracing.BalanceChangeUnspecified)
|
|
|
|
obj.data.Root = common.HexToHash("0xdeadbeef")
|
|
|
|
orig.updateStateObject(obj)
|
|
|
|
}
|
|
|
|
orig.Finalise(true)
|
|
|
|
cpy := orig.Copy()
|
|
|
|
for _, op := range cpy.mutations {
|
|
|
|
if have, want := op.applied, false; have != want {
|
|
|
|
t.Fatalf("Error in test itself, the 'done' flag should not be set before Commit, have %v want %v", have, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
orig.Commit(0, true)
|
|
|
|
for _, op := range cpy.mutations {
|
|
|
|
if have, want := op.applied, false; have != want {
|
|
|
|
t.Fatalf("Error: original state affected copy, have %v want %v", have, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-04 05:36:02 -05:00
|
|
|
func TestSnapshotRandom(t *testing.T) {
|
|
|
|
config := &quick.Config{MaxCount: 1000}
|
|
|
|
err := quick.Check((*snapshotTest).run, config)
|
|
|
|
if cerr, ok := err.(*quick.CheckError); ok {
|
|
|
|
test := cerr.In[0].(*snapshotTest)
|
|
|
|
t.Errorf("%v:\n%s", test.err, test)
|
|
|
|
} else if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A snapshotTest checks that reverting StateDB snapshots properly undoes all changes
|
|
|
|
// captured by the snapshot. Instances of this test with pseudorandom content are created
|
|
|
|
// by Generate.
|
|
|
|
//
|
|
|
|
// The test works as follows:
|
|
|
|
//
|
|
|
|
// A new state is created and all actions are applied to it. Several snapshots are taken
|
|
|
|
// in between actions. The test then reverts each snapshot. For each snapshot the actions
|
|
|
|
// leading up to it are replayed on a fresh, empty state. The behaviour of all public
|
|
|
|
// accessor methods on the reverted state must match the return value of the equivalent
|
|
|
|
// methods on the replayed state.
|
|
|
|
type snapshotTest struct {
|
|
|
|
addrs []common.Address // all account addresses
|
|
|
|
actions []testAction // modifications to the state
|
|
|
|
snapshots []int // actions indexes at which snapshot is taken
|
|
|
|
err error // failure details are reported through this field
|
|
|
|
}
|
|
|
|
|
|
|
|
type testAction struct {
|
|
|
|
name string
|
|
|
|
fn func(testAction, *StateDB)
|
|
|
|
args []int64
|
|
|
|
noAddr bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestAction creates a random action that changes state.
|
|
|
|
func newTestAction(addr common.Address, r *rand.Rand) testAction {
|
|
|
|
actions := []testAction{
|
|
|
|
{
|
|
|
|
name: "SetBalance",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
2024-03-22 12:53:53 -05:00
|
|
|
s.SetBalance(addr, uint256.NewInt(uint64(a.args[0])), tracing.BalanceChangeUnspecified)
|
2016-10-04 05:36:02 -05:00
|
|
|
},
|
|
|
|
args: make([]int64, 1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "AddBalance",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
2024-03-22 12:53:53 -05:00
|
|
|
s.AddBalance(addr, uint256.NewInt(uint64(a.args[0])), tracing.BalanceChangeUnspecified)
|
2016-10-04 05:36:02 -05:00
|
|
|
},
|
|
|
|
args: make([]int64, 1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SetNonce",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
s.SetNonce(addr, uint64(a.args[0]))
|
|
|
|
},
|
|
|
|
args: make([]int64, 1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SetState",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
var key, val common.Hash
|
|
|
|
binary.BigEndian.PutUint16(key[:], uint16(a.args[0]))
|
|
|
|
binary.BigEndian.PutUint16(val[:], uint16(a.args[1]))
|
|
|
|
s.SetState(addr, key, val)
|
|
|
|
},
|
|
|
|
args: make([]int64, 2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SetCode",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
code := make([]byte, 16)
|
|
|
|
binary.BigEndian.PutUint64(code, uint64(a.args[0]))
|
|
|
|
binary.BigEndian.PutUint64(code[8:], uint64(a.args[1]))
|
|
|
|
s.SetCode(addr, code)
|
|
|
|
},
|
|
|
|
args: make([]int64, 2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "CreateAccount",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
2024-04-24 04:59:06 -05:00
|
|
|
if !s.Exist(addr) {
|
|
|
|
s.CreateAccount(addr)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "CreateContract",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
if !s.Exist(addr) {
|
|
|
|
s.CreateAccount(addr)
|
|
|
|
}
|
|
|
|
contractHash := s.GetCodeHash(addr)
|
|
|
|
emptyCode := contractHash == (common.Hash{}) || contractHash == types.EmptyCodeHash
|
|
|
|
storageRoot := s.GetStorageRoot(addr)
|
|
|
|
emptyStorage := storageRoot == (common.Hash{}) || storageRoot == types.EmptyRootHash
|
|
|
|
if s.GetNonce(addr) == 0 && emptyCode && emptyStorage {
|
|
|
|
s.CreateContract(addr)
|
|
|
|
// We also set some code here, to prevent the
|
|
|
|
// CreateContract action from being performed twice in a row,
|
|
|
|
// which would cause a difference in state when unrolling
|
|
|
|
// the journal. (CreateContact assumes created was false prior to
|
|
|
|
// invocation, and the journal rollback sets it to false).
|
|
|
|
s.SetCode(addr, []byte{1})
|
|
|
|
}
|
2016-10-04 05:36:02 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-07-15 09:35:30 -05:00
|
|
|
name: "SelfDestruct",
|
2016-10-04 05:36:02 -05:00
|
|
|
fn: func(a testAction, s *StateDB) {
|
2023-07-15 09:35:30 -05:00
|
|
|
s.SelfDestruct(addr)
|
2016-10-04 05:36:02 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "AddRefund",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
2017-11-13 05:47:27 -06:00
|
|
|
s.AddRefund(uint64(a.args[0]))
|
2016-10-04 05:36:02 -05:00
|
|
|
},
|
|
|
|
args: make([]int64, 1),
|
|
|
|
noAddr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "AddLog",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
data := make([]byte, 2)
|
|
|
|
binary.BigEndian.PutUint16(data, uint16(a.args[0]))
|
2017-01-05 07:03:50 -06:00
|
|
|
s.AddLog(&types.Log{Address: addr, Data: data})
|
2016-10-04 05:36:02 -05:00
|
|
|
},
|
|
|
|
args: make([]int64, 1),
|
|
|
|
},
|
2019-02-07 03:44:45 -06:00
|
|
|
{
|
|
|
|
name: "AddPreimage",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
preimage := []byte{1}
|
|
|
|
hash := common.BytesToHash(preimage)
|
|
|
|
s.AddPreimage(hash, preimage)
|
|
|
|
},
|
|
|
|
args: make([]int64, 1),
|
|
|
|
},
|
2020-10-23 01:26:57 -05:00
|
|
|
{
|
|
|
|
name: "AddAddressToAccessList",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
s.AddAddressToAccessList(addr)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "AddSlotToAccessList",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
s.AddSlotToAccessList(addr,
|
|
|
|
common.Hash{byte(a.args[0])})
|
|
|
|
},
|
|
|
|
args: make([]int64, 1),
|
|
|
|
},
|
2022-11-16 03:18:52 -06:00
|
|
|
{
|
|
|
|
name: "SetTransientState",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
var key, val common.Hash
|
|
|
|
binary.BigEndian.PutUint16(key[:], uint16(a.args[0]))
|
|
|
|
binary.BigEndian.PutUint16(val[:], uint16(a.args[1]))
|
|
|
|
s.SetTransientState(addr, key, val)
|
|
|
|
},
|
|
|
|
args: make([]int64, 2),
|
|
|
|
},
|
2016-10-04 05:36:02 -05:00
|
|
|
}
|
|
|
|
action := actions[r.Intn(len(actions))]
|
|
|
|
var nameargs []string
|
|
|
|
if !action.noAddr {
|
|
|
|
nameargs = append(nameargs, addr.Hex())
|
|
|
|
}
|
2019-07-09 03:32:28 -05:00
|
|
|
for i := range action.args {
|
2016-10-04 05:36:02 -05:00
|
|
|
action.args[i] = rand.Int63n(100)
|
|
|
|
nameargs = append(nameargs, fmt.Sprint(action.args[i]))
|
|
|
|
}
|
|
|
|
action.name += strings.Join(nameargs, ", ")
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate returns a new snapshot test of the given size. All randomness is
|
|
|
|
// derived from r.
|
|
|
|
func (*snapshotTest) Generate(r *rand.Rand, size int) reflect.Value {
|
|
|
|
// Generate random actions.
|
|
|
|
addrs := make([]common.Address, 50)
|
|
|
|
for i := range addrs {
|
|
|
|
addrs[i][0] = byte(i)
|
|
|
|
}
|
|
|
|
actions := make([]testAction, size)
|
|
|
|
for i := range actions {
|
|
|
|
addr := addrs[r.Intn(len(addrs))]
|
|
|
|
actions[i] = newTestAction(addr, r)
|
|
|
|
}
|
|
|
|
// Generate snapshot indexes.
|
|
|
|
nsnapshots := int(math.Sqrt(float64(size)))
|
|
|
|
if size > 0 && nsnapshots == 0 {
|
|
|
|
nsnapshots = 1
|
|
|
|
}
|
|
|
|
snapshots := make([]int, nsnapshots)
|
|
|
|
snaplen := len(actions) / nsnapshots
|
|
|
|
for i := range snapshots {
|
|
|
|
// Try to place the snapshots some number of actions apart from each other.
|
|
|
|
snapshots[i] = (i * snaplen) + r.Intn(snaplen)
|
|
|
|
}
|
|
|
|
return reflect.ValueOf(&snapshotTest{addrs, actions, snapshots, nil})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *snapshotTest) String() string {
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
sindex := 0
|
|
|
|
for i, action := range test.actions {
|
|
|
|
if len(test.snapshots) > sindex && i == test.snapshots[sindex] {
|
|
|
|
fmt.Fprintf(out, "---- snapshot %d ----\n", sindex)
|
|
|
|
sindex++
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, "%4d: %s\n", i, action.name)
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *snapshotTest) run() bool {
|
|
|
|
// Run all actions and create snapshots.
|
|
|
|
var (
|
2023-05-11 02:19:42 -05:00
|
|
|
state, _ = New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2016-10-04 05:36:02 -05:00
|
|
|
snapshotRevs = make([]int, len(test.snapshots))
|
|
|
|
sindex = 0
|
2023-11-21 05:19:28 -06:00
|
|
|
checkstates = make([]*StateDB, len(test.snapshots))
|
2016-10-04 05:36:02 -05:00
|
|
|
)
|
|
|
|
for i, action := range test.actions {
|
|
|
|
if len(test.snapshots) > sindex && i == test.snapshots[sindex] {
|
|
|
|
snapshotRevs[sindex] = state.Snapshot()
|
2023-11-21 05:19:28 -06:00
|
|
|
checkstates[sindex] = state.Copy()
|
2016-10-04 05:36:02 -05:00
|
|
|
sindex++
|
|
|
|
}
|
|
|
|
action.fn(action, state)
|
|
|
|
}
|
|
|
|
// Revert all snapshots in reverse order. Each revert must yield a state
|
|
|
|
// that is equivalent to fresh state with all actions up the snapshot applied.
|
|
|
|
for sindex--; sindex >= 0; sindex-- {
|
|
|
|
state.RevertToSnapshot(snapshotRevs[sindex])
|
2023-11-21 05:19:28 -06:00
|
|
|
if err := test.checkEqual(state, checkstates[sindex]); err != nil {
|
2016-10-04 05:36:02 -05:00
|
|
|
test.err = fmt.Errorf("state mismatch after revert to snapshot %d\n%v", sindex, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:53:31 -05:00
|
|
|
func forEachStorage(s *StateDB, addr common.Address, cb func(key, value common.Hash) bool) error {
|
|
|
|
so := s.getStateObject(addr)
|
|
|
|
if so == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
tr, err := so.getTrie()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
trieIt, err := tr.NodeIterator(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-25 02:56:25 -05:00
|
|
|
var (
|
|
|
|
it = trie.NewIterator(trieIt)
|
|
|
|
visited = make(map[common.Hash]bool)
|
|
|
|
)
|
2023-08-23 14:53:31 -05:00
|
|
|
|
|
|
|
for it.Next() {
|
|
|
|
key := common.BytesToHash(s.trie.GetKey(it.Key))
|
2024-04-25 02:56:25 -05:00
|
|
|
visited[key] = true
|
2023-08-23 14:53:31 -05:00
|
|
|
if value, dirty := so.dirtyStorage[key]; dirty {
|
|
|
|
if !cb(key, value) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(it.Value) > 0 {
|
|
|
|
_, content, _, err := rlp.Split(it.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !cb(key, common.BytesToHash(content)) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-04 05:36:02 -05:00
|
|
|
// checkEqual checks that methods of state and checkstate return the same values.
|
|
|
|
func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error {
|
|
|
|
for _, addr := range test.addrs {
|
|
|
|
var err error
|
|
|
|
checkeq := func(op string, a, b interface{}) bool {
|
|
|
|
if err == nil && !reflect.DeepEqual(a, b) {
|
|
|
|
err = fmt.Errorf("got %s(%s) == %v, want %v", op, addr.Hex(), a, b)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Check basic accessor methods.
|
|
|
|
checkeq("Exist", state.Exist(addr), checkstate.Exist(addr))
|
2023-07-15 09:35:30 -05:00
|
|
|
checkeq("HasSelfdestructed", state.HasSelfDestructed(addr), checkstate.HasSelfDestructed(addr))
|
2016-10-04 05:36:02 -05:00
|
|
|
checkeq("GetBalance", state.GetBalance(addr), checkstate.GetBalance(addr))
|
|
|
|
checkeq("GetNonce", state.GetNonce(addr), checkstate.GetNonce(addr))
|
|
|
|
checkeq("GetCode", state.GetCode(addr), checkstate.GetCode(addr))
|
|
|
|
checkeq("GetCodeHash", state.GetCodeHash(addr), checkstate.GetCodeHash(addr))
|
|
|
|
checkeq("GetCodeSize", state.GetCodeSize(addr), checkstate.GetCodeSize(addr))
|
2024-04-25 02:56:25 -05:00
|
|
|
// Check newContract-flag
|
|
|
|
if obj := state.getStateObject(addr); obj != nil {
|
|
|
|
checkeq("IsNewContract", obj.newContract, checkstate.getStateObject(addr).newContract)
|
|
|
|
}
|
2016-10-04 05:36:02 -05:00
|
|
|
// Check storage.
|
2017-02-22 16:29:59 -06:00
|
|
|
if obj := state.getStateObject(addr); obj != nil {
|
2023-08-23 14:53:31 -05:00
|
|
|
forEachStorage(state, addr, func(key, value common.Hash) bool {
|
2018-09-18 08:24:35 -05:00
|
|
|
return checkeq("GetState("+key.Hex()+")", checkstate.GetState(addr, key), value)
|
2016-10-04 05:36:02 -05:00
|
|
|
})
|
2023-08-23 14:53:31 -05:00
|
|
|
forEachStorage(checkstate, addr, func(key, value common.Hash) bool {
|
2018-09-18 08:24:35 -05:00
|
|
|
return checkeq("GetState("+key.Hex()+")", checkstate.GetState(addr, key), value)
|
2016-10-04 05:36:02 -05:00
|
|
|
})
|
2024-04-25 02:56:25 -05:00
|
|
|
other := checkstate.getStateObject(addr)
|
|
|
|
// Check dirty storage which is not in trie
|
|
|
|
if !maps.Equal(obj.dirtyStorage, other.dirtyStorage) {
|
|
|
|
print := func(dirty map[common.Hash]common.Hash) string {
|
|
|
|
var keys []common.Hash
|
|
|
|
out := new(strings.Builder)
|
|
|
|
for key := range dirty {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
slices.SortFunc(keys, common.Hash.Cmp)
|
|
|
|
for i, key := range keys {
|
|
|
|
fmt.Fprintf(out, " %d. %v %v\n", i, key, dirty[key])
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
return fmt.Errorf("dirty storage err, have\n%v\nwant\n%v",
|
|
|
|
print(obj.dirtyStorage),
|
|
|
|
print(other.dirtyStorage))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check transient storage.
|
|
|
|
{
|
|
|
|
have := state.transientStorage
|
|
|
|
want := checkstate.transientStorage
|
|
|
|
eq := maps.EqualFunc(have, want,
|
|
|
|
func(a Storage, b Storage) bool {
|
|
|
|
return maps.Equal(a, b)
|
|
|
|
})
|
|
|
|
if !eq {
|
|
|
|
return fmt.Errorf("transient storage differs ,have\n%v\nwant\n%v",
|
|
|
|
have.PrettyPrint(),
|
|
|
|
want.PrettyPrint())
|
|
|
|
}
|
2016-10-04 05:36:02 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-25 02:56:25 -05:00
|
|
|
if !checkstate.accessList.Equal(state.accessList) { // Check access lists
|
|
|
|
return fmt.Errorf("AccessLists are wrong, have \n%v\nwant\n%v",
|
|
|
|
checkstate.accessList.PrettyPrint(),
|
|
|
|
state.accessList.PrettyPrint())
|
|
|
|
}
|
2017-11-13 05:47:27 -06:00
|
|
|
if state.GetRefund() != checkstate.GetRefund() {
|
2016-10-04 05:36:02 -05:00
|
|
|
return fmt.Errorf("got GetRefund() == %d, want GetRefund() == %d",
|
|
|
|
state.GetRefund(), checkstate.GetRefund())
|
|
|
|
}
|
2022-12-13 06:54:16 -06:00
|
|
|
if !reflect.DeepEqual(state.GetLogs(common.Hash{}, 0, common.Hash{}), checkstate.GetLogs(common.Hash{}, 0, common.Hash{})) {
|
2016-10-04 05:36:02 -05:00
|
|
|
return fmt.Errorf("got GetLogs(common.Hash{}) == %v, want GetLogs(common.Hash{}) == %v",
|
2022-12-13 06:54:16 -06:00
|
|
|
state.GetLogs(common.Hash{}, 0, common.Hash{}), checkstate.GetLogs(common.Hash{}, 0, common.Hash{}))
|
2016-10-04 05:36:02 -05:00
|
|
|
}
|
2024-04-25 02:56:25 -05:00
|
|
|
if !maps.Equal(state.journal.dirties, checkstate.journal.dirties) {
|
|
|
|
getKeys := func(dirty map[common.Address]int) string {
|
|
|
|
var keys []common.Address
|
|
|
|
out := new(strings.Builder)
|
|
|
|
for key := range dirty {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
slices.SortFunc(keys, common.Address.Cmp)
|
|
|
|
for i, key := range keys {
|
|
|
|
fmt.Fprintf(out, " %d. %v\n", i, key)
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
have := getKeys(state.journal.dirties)
|
|
|
|
want := getKeys(checkstate.journal.dirties)
|
|
|
|
return fmt.Errorf("dirty-journal set mismatch.\nhave:\n%v\nwant:\n%v\n", have, want)
|
|
|
|
}
|
2016-10-04 05:36:02 -05:00
|
|
|
return nil
|
|
|
|
}
|
2016-11-24 09:24:04 -06:00
|
|
|
|
2019-11-22 08:56:05 -06:00
|
|
|
func TestTouchDelete(t *testing.T) {
|
2023-07-11 08:43:23 -05:00
|
|
|
s := newStateEnv()
|
2024-01-14 05:32:23 -06:00
|
|
|
s.state.getOrNewStateObject(common.Address{})
|
2023-07-24 05:22:09 -05:00
|
|
|
root, _ := s.state.Commit(0, false)
|
2021-01-08 07:01:49 -06:00
|
|
|
s.state, _ = New(root, s.state.db, s.state.snaps)
|
2016-11-24 09:24:04 -06:00
|
|
|
|
2017-06-27 08:57:06 -05:00
|
|
|
snapshot := s.state.Snapshot()
|
2024-03-22 12:53:53 -05:00
|
|
|
s.state.AddBalance(common.Address{}, new(uint256.Int), tracing.BalanceChangeUnspecified)
|
2017-10-01 14:07:30 -05:00
|
|
|
|
2018-03-27 07:13:30 -05:00
|
|
|
if len(s.state.journal.dirties) != 1 {
|
2019-11-22 08:56:05 -06:00
|
|
|
t.Fatal("expected one dirty state object")
|
2016-11-24 09:24:04 -06:00
|
|
|
}
|
2017-06-27 08:57:06 -05:00
|
|
|
s.state.RevertToSnapshot(snapshot)
|
2018-03-27 07:13:30 -05:00
|
|
|
if len(s.state.journal.dirties) != 0 {
|
2019-11-22 08:56:05 -06:00
|
|
|
t.Fatal("expected no dirty state object")
|
2016-11-24 09:24:04 -06:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 13:59:07 -05:00
|
|
|
|
|
|
|
// TestCopyOfCopy tests that modified objects are carried over to the copy, and the copy of the copy.
|
|
|
|
// See https://github.com/ethereum/go-ethereum/pull/15225#issuecomment-380191512
|
|
|
|
func TestCopyOfCopy(t *testing.T) {
|
2023-05-11 02:19:42 -05:00
|
|
|
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2018-04-10 13:59:07 -05:00
|
|
|
addr := common.HexToAddress("aaaa")
|
2024-03-22 12:53:53 -05:00
|
|
|
state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified)
|
2018-04-10 13:59:07 -05:00
|
|
|
|
2019-09-24 02:49:59 -05:00
|
|
|
if got := state.Copy().GetBalance(addr).Uint64(); got != 42 {
|
2018-04-10 13:59:07 -05:00
|
|
|
t.Fatalf("1st copy fail, expected 42, got %v", got)
|
|
|
|
}
|
2019-09-24 02:49:59 -05:00
|
|
|
if got := state.Copy().Copy().GetBalance(addr).Uint64(); got != 42 {
|
2018-04-10 13:59:07 -05:00
|
|
|
t.Fatalf("2nd copy fail, expected 42, got %v", got)
|
|
|
|
}
|
|
|
|
}
|
2019-08-12 14:56:07 -05:00
|
|
|
|
2019-09-24 02:49:59 -05:00
|
|
|
// Tests a regression where committing a copy lost some internal meta information,
|
|
|
|
// leading to corrupted subsequent copies.
|
|
|
|
//
|
|
|
|
// See https://github.com/ethereum/go-ethereum/issues/20106.
|
|
|
|
func TestCopyCommitCopy(t *testing.T) {
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
tdb := NewDatabase(rawdb.NewMemoryDatabase())
|
|
|
|
state, _ := New(types.EmptyRootHash, tdb, nil)
|
2019-09-24 02:49:59 -05:00
|
|
|
|
|
|
|
// Create an account and check if the retrieved balance is correct
|
|
|
|
addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
|
|
|
|
skey := common.HexToHash("aaa")
|
|
|
|
sval := common.HexToHash("bbb")
|
|
|
|
|
2024-03-22 12:53:53 -05:00
|
|
|
state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie
|
|
|
|
state.SetCode(addr, []byte("hello")) // Change an external metadata
|
|
|
|
state.SetState(addr, skey, sval) // Change the storage trie
|
2019-09-24 02:49:59 -05:00
|
|
|
|
2024-01-23 07:51:58 -06:00
|
|
|
if balance := state.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
|
2019-09-24 02:49:59 -05:00
|
|
|
t.Fatalf("initial balance mismatch: have %v, want %v", balance, 42)
|
|
|
|
}
|
|
|
|
if code := state.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
|
|
|
|
t.Fatalf("initial code mismatch: have %x, want %x", code, []byte("hello"))
|
|
|
|
}
|
|
|
|
if val := state.GetState(addr, skey); val != sval {
|
|
|
|
t.Fatalf("initial non-committed storage slot mismatch: have %x, want %x", val, sval)
|
|
|
|
}
|
|
|
|
if val := state.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
|
|
|
t.Fatalf("initial committed storage slot mismatch: have %x, want %x", val, common.Hash{})
|
|
|
|
}
|
|
|
|
// Copy the non-committed state database and check pre/post commit balance
|
|
|
|
copyOne := state.Copy()
|
2024-01-23 07:51:58 -06:00
|
|
|
if balance := copyOne.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
|
2019-09-24 02:49:59 -05:00
|
|
|
t.Fatalf("first copy pre-commit balance mismatch: have %v, want %v", balance, 42)
|
|
|
|
}
|
|
|
|
if code := copyOne.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
|
|
|
|
t.Fatalf("first copy pre-commit code mismatch: have %x, want %x", code, []byte("hello"))
|
|
|
|
}
|
|
|
|
if val := copyOne.GetState(addr, skey); val != sval {
|
|
|
|
t.Fatalf("first copy pre-commit non-committed storage slot mismatch: have %x, want %x", val, sval)
|
|
|
|
}
|
|
|
|
if val := copyOne.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
|
|
|
t.Fatalf("first copy pre-commit committed storage slot mismatch: have %x, want %x", val, common.Hash{})
|
|
|
|
}
|
|
|
|
// Copy the copy and check the balance once more
|
|
|
|
copyTwo := copyOne.Copy()
|
2024-01-23 07:51:58 -06:00
|
|
|
if balance := copyTwo.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
|
2019-09-24 02:49:59 -05:00
|
|
|
t.Fatalf("second copy balance mismatch: have %v, want %v", balance, 42)
|
|
|
|
}
|
|
|
|
if code := copyTwo.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
|
|
|
|
t.Fatalf("second copy code mismatch: have %x, want %x", code, []byte("hello"))
|
|
|
|
}
|
|
|
|
if val := copyTwo.GetState(addr, skey); val != sval {
|
|
|
|
t.Fatalf("second copy non-committed storage slot mismatch: have %x, want %x", val, sval)
|
|
|
|
}
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
if val := copyTwo.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
|
|
|
t.Fatalf("second copy committed storage slot mismatch: have %x, want %x", val, sval)
|
|
|
|
}
|
|
|
|
// Commit state, ensure states can be loaded from disk
|
2023-07-24 05:22:09 -05:00
|
|
|
root, _ := state.Commit(0, false)
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
state, _ = New(root, tdb, nil)
|
2024-01-23 07:51:58 -06:00
|
|
|
if balance := state.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
t.Fatalf("state post-commit balance mismatch: have %v, want %v", balance, 42)
|
|
|
|
}
|
|
|
|
if code := state.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
|
|
|
|
t.Fatalf("state post-commit code mismatch: have %x, want %x", code, []byte("hello"))
|
|
|
|
}
|
|
|
|
if val := state.GetState(addr, skey); val != sval {
|
|
|
|
t.Fatalf("state post-commit non-committed storage slot mismatch: have %x, want %x", val, sval)
|
|
|
|
}
|
|
|
|
if val := state.GetCommittedState(addr, skey); val != sval {
|
|
|
|
t.Fatalf("state post-commit committed storage slot mismatch: have %x, want %x", val, sval)
|
2019-09-24 02:49:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests a regression where committing a copy lost some internal meta information,
|
|
|
|
// leading to corrupted subsequent copies.
|
|
|
|
//
|
|
|
|
// See https://github.com/ethereum/go-ethereum/issues/20106.
|
|
|
|
func TestCopyCopyCommitCopy(t *testing.T) {
|
2023-05-11 02:19:42 -05:00
|
|
|
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2019-09-24 02:49:59 -05:00
|
|
|
|
|
|
|
// Create an account and check if the retrieved balance is correct
|
|
|
|
addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
|
|
|
|
skey := common.HexToHash("aaa")
|
|
|
|
sval := common.HexToHash("bbb")
|
|
|
|
|
2024-03-22 12:53:53 -05:00
|
|
|
state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie
|
|
|
|
state.SetCode(addr, []byte("hello")) // Change an external metadata
|
|
|
|
state.SetState(addr, skey, sval) // Change the storage trie
|
2019-09-24 02:49:59 -05:00
|
|
|
|
2024-01-23 07:51:58 -06:00
|
|
|
if balance := state.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
|
2019-09-24 02:49:59 -05:00
|
|
|
t.Fatalf("initial balance mismatch: have %v, want %v", balance, 42)
|
|
|
|
}
|
|
|
|
if code := state.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
|
|
|
|
t.Fatalf("initial code mismatch: have %x, want %x", code, []byte("hello"))
|
|
|
|
}
|
|
|
|
if val := state.GetState(addr, skey); val != sval {
|
|
|
|
t.Fatalf("initial non-committed storage slot mismatch: have %x, want %x", val, sval)
|
|
|
|
}
|
|
|
|
if val := state.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
|
|
|
t.Fatalf("initial committed storage slot mismatch: have %x, want %x", val, common.Hash{})
|
|
|
|
}
|
|
|
|
// Copy the non-committed state database and check pre/post commit balance
|
|
|
|
copyOne := state.Copy()
|
2024-01-23 07:51:58 -06:00
|
|
|
if balance := copyOne.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
|
2019-09-24 02:49:59 -05:00
|
|
|
t.Fatalf("first copy balance mismatch: have %v, want %v", balance, 42)
|
|
|
|
}
|
|
|
|
if code := copyOne.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
|
|
|
|
t.Fatalf("first copy code mismatch: have %x, want %x", code, []byte("hello"))
|
|
|
|
}
|
|
|
|
if val := copyOne.GetState(addr, skey); val != sval {
|
|
|
|
t.Fatalf("first copy non-committed storage slot mismatch: have %x, want %x", val, sval)
|
|
|
|
}
|
|
|
|
if val := copyOne.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
|
|
|
t.Fatalf("first copy committed storage slot mismatch: have %x, want %x", val, common.Hash{})
|
|
|
|
}
|
|
|
|
// Copy the copy and check the balance once more
|
|
|
|
copyTwo := copyOne.Copy()
|
2024-01-23 07:51:58 -06:00
|
|
|
if balance := copyTwo.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
|
2019-09-24 02:49:59 -05:00
|
|
|
t.Fatalf("second copy pre-commit balance mismatch: have %v, want %v", balance, 42)
|
|
|
|
}
|
|
|
|
if code := copyTwo.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
|
|
|
|
t.Fatalf("second copy pre-commit code mismatch: have %x, want %x", code, []byte("hello"))
|
|
|
|
}
|
|
|
|
if val := copyTwo.GetState(addr, skey); val != sval {
|
|
|
|
t.Fatalf("second copy pre-commit non-committed storage slot mismatch: have %x, want %x", val, sval)
|
|
|
|
}
|
|
|
|
if val := copyTwo.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
|
|
|
t.Fatalf("second copy pre-commit committed storage slot mismatch: have %x, want %x", val, common.Hash{})
|
|
|
|
}
|
|
|
|
// Copy the copy-copy and check the balance once more
|
|
|
|
copyThree := copyTwo.Copy()
|
2024-01-23 07:51:58 -06:00
|
|
|
if balance := copyThree.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
|
2019-09-24 02:49:59 -05:00
|
|
|
t.Fatalf("third copy balance mismatch: have %v, want %v", balance, 42)
|
|
|
|
}
|
|
|
|
if code := copyThree.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
|
|
|
|
t.Fatalf("third copy code mismatch: have %x, want %x", code, []byte("hello"))
|
|
|
|
}
|
|
|
|
if val := copyThree.GetState(addr, skey); val != sval {
|
|
|
|
t.Fatalf("third copy non-committed storage slot mismatch: have %x, want %x", val, sval)
|
|
|
|
}
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
if val := copyThree.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
2019-09-24 02:49:59 -05:00
|
|
|
t.Fatalf("third copy committed storage slot mismatch: have %x, want %x", val, sval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 04:59:06 -05:00
|
|
|
// TestCommitCopy tests the copy from a committed state is not fully functional.
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
func TestCommitCopy(t *testing.T) {
|
2024-04-24 04:59:06 -05:00
|
|
|
db := NewDatabase(rawdb.NewMemoryDatabase())
|
|
|
|
state, _ := New(types.EmptyRootHash, db, 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 14:31:45 -05:00
|
|
|
|
|
|
|
// Create an account and check if the retrieved balance is correct
|
|
|
|
addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
|
2024-04-24 04:59:06 -05:00
|
|
|
skey1, skey2 := common.HexToHash("a1"), common.HexToHash("a2")
|
|
|
|
sval1, sval2 := common.HexToHash("b1"), common.HexToHash("b2")
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
|
2024-03-22 12:53:53 -05:00
|
|
|
state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie
|
|
|
|
state.SetCode(addr, []byte("hello")) // Change an external metadata
|
2024-04-24 04:59:06 -05:00
|
|
|
state.SetState(addr, skey1, sval1) // Change the storage trie
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
|
2024-01-23 07:51:58 -06:00
|
|
|
if balance := state.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
t.Fatalf("initial balance mismatch: have %v, want %v", balance, 42)
|
|
|
|
}
|
|
|
|
if code := state.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
|
|
|
|
t.Fatalf("initial code mismatch: have %x, want %x", code, []byte("hello"))
|
|
|
|
}
|
2024-04-24 04:59:06 -05:00
|
|
|
if val := state.GetState(addr, skey1); val != sval1 {
|
|
|
|
t.Fatalf("initial non-committed storage slot mismatch: have %x, want %x", val, sval1)
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
}
|
2024-04-24 04:59:06 -05:00
|
|
|
if val := state.GetCommittedState(addr, skey1); val != (common.Hash{}) {
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
t.Fatalf("initial committed storage slot mismatch: have %x, want %x", val, common.Hash{})
|
|
|
|
}
|
2024-04-24 04:59:06 -05:00
|
|
|
root, _ := state.Commit(0, true)
|
|
|
|
|
|
|
|
state, _ = New(root, db, nil)
|
|
|
|
state.SetState(addr, skey2, sval2)
|
|
|
|
state.Commit(1, true)
|
|
|
|
|
|
|
|
// Copy the committed state database, the copied one is not fully functional.
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
copied := state.Copy()
|
2024-04-24 04:59:06 -05:00
|
|
|
if balance := copied.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
t.Fatalf("unexpected balance: have %v", balance)
|
|
|
|
}
|
2024-04-24 04:59:06 -05:00
|
|
|
if code := copied.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
t.Fatalf("unexpected code: have %x", code)
|
|
|
|
}
|
2024-04-24 04:59:06 -05:00
|
|
|
// Miss slots because of non-functional trie after commit
|
|
|
|
if val := copied.GetState(addr, skey1); val != (common.Hash{}) {
|
|
|
|
t.Fatalf("unexpected storage slot: have %x", sval1)
|
|
|
|
}
|
|
|
|
if val := copied.GetCommittedState(addr, skey1); val != (common.Hash{}) {
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
t.Fatalf("unexpected storage slot: have %x", val)
|
|
|
|
}
|
2024-04-24 04:59:06 -05:00
|
|
|
// Slots cached in the stateDB, available after commit
|
|
|
|
if val := copied.GetState(addr, skey2); val != sval2 {
|
|
|
|
t.Fatalf("unexpected storage slot: have %x", sval1)
|
|
|
|
}
|
|
|
|
if val := copied.GetCommittedState(addr, skey2); val != sval2 {
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 14:31:45 -05:00
|
|
|
t.Fatalf("unexpected storage slot: have %x", val)
|
|
|
|
}
|
|
|
|
if !errors.Is(copied.Error(), trie.ErrCommitted) {
|
|
|
|
t.Fatalf("unexpected state error, %v", copied.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 14:56:07 -05:00
|
|
|
// TestDeleteCreateRevert tests a weird state transition corner case that we hit
|
2020-08-19 01:54:21 -05:00
|
|
|
// while changing the internals of StateDB. The workflow is that a contract is
|
|
|
|
// self-destructed, then in a follow-up transaction (but same block) it's created
|
2019-08-12 14:56:07 -05:00
|
|
|
// again and the transaction reverted.
|
|
|
|
//
|
2020-08-19 01:54:21 -05:00
|
|
|
// The original StateDB implementation flushed dirty objects to the tries after
|
2019-08-12 14:56:07 -05:00
|
|
|
// each transaction, so this works ok. The rework accumulated writes in memory
|
|
|
|
// first, but the journal wiped the entire state object on create-revert.
|
|
|
|
func TestDeleteCreateRevert(t *testing.T) {
|
|
|
|
// Create an initial state with a single contract
|
2023-05-11 02:19:42 -05:00
|
|
|
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2019-08-12 14:56:07 -05:00
|
|
|
|
2021-04-30 06:10:12 -05:00
|
|
|
addr := common.BytesToAddress([]byte("so"))
|
2024-03-22 12:53:53 -05:00
|
|
|
state.SetBalance(addr, uint256.NewInt(1), tracing.BalanceChangeUnspecified)
|
2019-08-12 14:56:07 -05:00
|
|
|
|
2023-07-24 05:22:09 -05:00
|
|
|
root, _ := state.Commit(0, false)
|
2021-01-08 07:01:49 -06:00
|
|
|
state, _ = New(root, state.db, state.snaps)
|
2019-08-12 14:56:07 -05:00
|
|
|
|
|
|
|
// Simulate self-destructing in one transaction, then create-reverting in another
|
2023-07-15 09:35:30 -05:00
|
|
|
state.SelfDestruct(addr)
|
2019-08-12 14:56:07 -05:00
|
|
|
state.Finalise(true)
|
|
|
|
|
|
|
|
id := state.Snapshot()
|
2024-03-22 12:53:53 -05:00
|
|
|
state.SetBalance(addr, uint256.NewInt(2), tracing.BalanceChangeUnspecified)
|
2019-08-12 14:56:07 -05:00
|
|
|
state.RevertToSnapshot(id)
|
|
|
|
|
|
|
|
// Commit the entire state and make sure we don't crash and have the correct state
|
2023-07-24 05:22:09 -05:00
|
|
|
root, _ = state.Commit(0, true)
|
2021-01-08 07:01:49 -06:00
|
|
|
state, _ = New(root, state.db, state.snaps)
|
2019-08-12 14:56:07 -05:00
|
|
|
|
|
|
|
if state.getStateObject(addr) != nil {
|
|
|
|
t.Fatalf("self-destructed contract came alive")
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 08:13:34 -05:00
|
|
|
|
2020-08-19 01:54:21 -05:00
|
|
|
// TestMissingTrieNodes tests that if the StateDB fails to load parts of the trie,
|
2020-05-07 08:13:34 -05:00
|
|
|
// the Commit operation fails with an error
|
|
|
|
// If we are missing trie nodes, we should not continue writing to the trie
|
|
|
|
func TestMissingTrieNodes(t *testing.T) {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
testMissingTrieNodes(t, rawdb.HashScheme)
|
|
|
|
testMissingTrieNodes(t, rawdb.PathScheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMissingTrieNodes(t *testing.T, scheme string) {
|
2020-05-07 08:13:34 -05:00
|
|
|
// Create an initial state with a few accounts
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
var (
|
2024-02-13 07:49:53 -06:00
|
|
|
tdb *triedb.Database
|
|
|
|
memDb = rawdb.NewMemoryDatabase()
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
)
|
|
|
|
if scheme == rawdb.PathScheme {
|
2024-02-13 07:49:53 -06:00
|
|
|
tdb = triedb.NewDatabase(memDb, &triedb.Config{PathDB: &pathdb.Config{
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
CleanCacheSize: 0,
|
|
|
|
DirtyCacheSize: 0,
|
|
|
|
}}) // disable caching
|
|
|
|
} else {
|
2024-02-13 07:49:53 -06:00
|
|
|
tdb = triedb.NewDatabase(memDb, &triedb.Config{HashDB: &hashdb.Config{
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
CleanCacheSize: 0,
|
|
|
|
}}) // disable caching
|
|
|
|
}
|
2024-02-13 07:49:53 -06:00
|
|
|
db := NewDatabaseWithNodeDB(memDb, tdb)
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
|
2020-05-07 08:13:34 -05:00
|
|
|
var root common.Hash
|
2023-05-11 02:19:42 -05:00
|
|
|
state, _ := New(types.EmptyRootHash, db, nil)
|
2021-04-30 06:10:12 -05:00
|
|
|
addr := common.BytesToAddress([]byte("so"))
|
2020-05-07 08:13:34 -05:00
|
|
|
{
|
2024-03-22 12:53:53 -05:00
|
|
|
state.SetBalance(addr, uint256.NewInt(1), tracing.BalanceChangeUnspecified)
|
2020-05-07 08:13:34 -05:00
|
|
|
state.SetCode(addr, []byte{1, 2, 3})
|
2021-04-30 06:10:12 -05:00
|
|
|
a2 := common.BytesToAddress([]byte("another"))
|
2024-03-22 12:53:53 -05:00
|
|
|
state.SetBalance(a2, uint256.NewInt(100), tracing.BalanceChangeUnspecified)
|
2020-05-07 08:13:34 -05:00
|
|
|
state.SetCode(a2, []byte{1, 2, 4})
|
2023-07-24 05:22:09 -05:00
|
|
|
root, _ = state.Commit(0, false)
|
2020-05-07 08:13:34 -05:00
|
|
|
t.Logf("root: %x", root)
|
|
|
|
// force-flush
|
2024-02-13 07:49:53 -06:00
|
|
|
tdb.Commit(root, false)
|
2020-05-07 08:13:34 -05:00
|
|
|
}
|
|
|
|
// Create a new state on the old root
|
|
|
|
state, _ = New(root, db, nil)
|
|
|
|
// Now we clear out the memdb
|
|
|
|
it := memDb.NewIterator(nil, nil)
|
|
|
|
for it.Next() {
|
|
|
|
k := it.Key()
|
|
|
|
// Leave the root intact
|
|
|
|
if !bytes.Equal(k, root[:]) {
|
|
|
|
t.Logf("key: %x", k)
|
|
|
|
memDb.Delete(k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
balance := state.GetBalance(addr)
|
|
|
|
// The removed elem should lead to it returning zero balance
|
|
|
|
if exp, got := uint64(0), balance.Uint64(); got != exp {
|
|
|
|
t.Errorf("expected %d, got %d", exp, got)
|
|
|
|
}
|
|
|
|
// Modify the state
|
2024-03-22 12:53:53 -05:00
|
|
|
state.SetBalance(addr, uint256.NewInt(2), tracing.BalanceChangeUnspecified)
|
2023-07-24 05:22:09 -05:00
|
|
|
root, err := state.Commit(0, false)
|
2020-05-07 08:13:34 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error, got root :%x", root)
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 01:26:57 -05:00
|
|
|
|
|
|
|
func TestStateDBAccessList(t *testing.T) {
|
|
|
|
// Some helpers
|
|
|
|
addr := func(a string) common.Address {
|
|
|
|
return common.HexToAddress(a)
|
|
|
|
}
|
|
|
|
slot := func(a string) common.Hash {
|
|
|
|
return common.HexToHash(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
memDb := rawdb.NewMemoryDatabase()
|
|
|
|
db := NewDatabase(memDb)
|
2023-05-11 02:19:42 -05:00
|
|
|
state, _ := New(types.EmptyRootHash, db, nil)
|
2020-10-23 01:26:57 -05:00
|
|
|
state.accessList = newAccessList()
|
|
|
|
|
|
|
|
verifyAddrs := func(astrings ...string) {
|
|
|
|
t.Helper()
|
|
|
|
// convert to common.Address form
|
|
|
|
var addresses []common.Address
|
|
|
|
var addressMap = make(map[common.Address]struct{})
|
|
|
|
for _, astring := range astrings {
|
|
|
|
address := addr(astring)
|
|
|
|
addresses = append(addresses, address)
|
|
|
|
addressMap[address] = struct{}{}
|
|
|
|
}
|
|
|
|
// Check that the given addresses are in the access list
|
|
|
|
for _, address := range addresses {
|
|
|
|
if !state.AddressInAccessList(address) {
|
|
|
|
t.Fatalf("expected %x to be in access list", address)
|
|
|
|
}
|
|
|
|
}
|
2022-08-19 01:00:21 -05:00
|
|
|
// Check that only the expected addresses are present in the access list
|
2020-10-23 01:26:57 -05:00
|
|
|
for address := range state.accessList.addresses {
|
|
|
|
if _, exist := addressMap[address]; !exist {
|
|
|
|
t.Fatalf("extra address %x in access list", address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
verifySlots := func(addrString string, slotStrings ...string) {
|
|
|
|
if !state.AddressInAccessList(addr(addrString)) {
|
|
|
|
t.Fatalf("scope missing address/slots %v", addrString)
|
|
|
|
}
|
|
|
|
var address = addr(addrString)
|
|
|
|
// convert to common.Hash form
|
|
|
|
var slots []common.Hash
|
|
|
|
var slotMap = make(map[common.Hash]struct{})
|
|
|
|
for _, slotString := range slotStrings {
|
|
|
|
s := slot(slotString)
|
|
|
|
slots = append(slots, s)
|
|
|
|
slotMap[s] = struct{}{}
|
|
|
|
}
|
|
|
|
// Check that the expected items are in the access list
|
|
|
|
for i, s := range slots {
|
|
|
|
if _, slotPresent := state.SlotInAccessList(address, s); !slotPresent {
|
|
|
|
t.Fatalf("input %d: scope missing slot %v (address %v)", i, s, addrString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check that no extra elements are in the access list
|
|
|
|
index := state.accessList.addresses[address]
|
|
|
|
if index >= 0 {
|
|
|
|
stateSlots := state.accessList.slots[index]
|
|
|
|
for s := range stateSlots {
|
|
|
|
if _, slotPresent := slotMap[s]; !slotPresent {
|
|
|
|
t.Fatalf("scope has extra slot %v (address %v)", s, addrString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.AddAddressToAccessList(addr("aa")) // 1
|
|
|
|
state.AddSlotToAccessList(addr("bb"), slot("01")) // 2,3
|
|
|
|
state.AddSlotToAccessList(addr("bb"), slot("02")) // 4
|
|
|
|
verifyAddrs("aa", "bb")
|
|
|
|
verifySlots("bb", "01", "02")
|
|
|
|
|
|
|
|
// Make a copy
|
|
|
|
stateCopy1 := state.Copy()
|
|
|
|
if exp, got := 4, state.journal.length(); exp != got {
|
|
|
|
t.Fatalf("journal length mismatch: have %d, want %d", got, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// same again, should cause no journal entries
|
|
|
|
state.AddSlotToAccessList(addr("bb"), slot("01"))
|
|
|
|
state.AddSlotToAccessList(addr("bb"), slot("02"))
|
|
|
|
state.AddAddressToAccessList(addr("aa"))
|
|
|
|
if exp, got := 4, state.journal.length(); exp != got {
|
|
|
|
t.Fatalf("journal length mismatch: have %d, want %d", got, exp)
|
|
|
|
}
|
|
|
|
// some new ones
|
|
|
|
state.AddSlotToAccessList(addr("bb"), slot("03")) // 5
|
|
|
|
state.AddSlotToAccessList(addr("aa"), slot("01")) // 6
|
|
|
|
state.AddSlotToAccessList(addr("cc"), slot("01")) // 7,8
|
|
|
|
state.AddAddressToAccessList(addr("cc"))
|
|
|
|
if exp, got := 8, state.journal.length(); exp != got {
|
|
|
|
t.Fatalf("journal length mismatch: have %d, want %d", got, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyAddrs("aa", "bb", "cc")
|
|
|
|
verifySlots("aa", "01")
|
|
|
|
verifySlots("bb", "01", "02", "03")
|
|
|
|
verifySlots("cc", "01")
|
|
|
|
|
|
|
|
// now start rolling back changes
|
|
|
|
state.journal.revert(state, 7)
|
|
|
|
if _, ok := state.SlotInAccessList(addr("cc"), slot("01")); ok {
|
|
|
|
t.Fatalf("slot present, expected missing")
|
|
|
|
}
|
|
|
|
verifyAddrs("aa", "bb", "cc")
|
|
|
|
verifySlots("aa", "01")
|
|
|
|
verifySlots("bb", "01", "02", "03")
|
|
|
|
|
|
|
|
state.journal.revert(state, 6)
|
|
|
|
if state.AddressInAccessList(addr("cc")) {
|
|
|
|
t.Fatalf("addr present, expected missing")
|
|
|
|
}
|
|
|
|
verifyAddrs("aa", "bb")
|
|
|
|
verifySlots("aa", "01")
|
|
|
|
verifySlots("bb", "01", "02", "03")
|
|
|
|
|
|
|
|
state.journal.revert(state, 5)
|
|
|
|
if _, ok := state.SlotInAccessList(addr("aa"), slot("01")); ok {
|
|
|
|
t.Fatalf("slot present, expected missing")
|
|
|
|
}
|
|
|
|
verifyAddrs("aa", "bb")
|
|
|
|
verifySlots("bb", "01", "02", "03")
|
|
|
|
|
|
|
|
state.journal.revert(state, 4)
|
|
|
|
if _, ok := state.SlotInAccessList(addr("bb"), slot("03")); ok {
|
|
|
|
t.Fatalf("slot present, expected missing")
|
|
|
|
}
|
|
|
|
verifyAddrs("aa", "bb")
|
|
|
|
verifySlots("bb", "01", "02")
|
|
|
|
|
|
|
|
state.journal.revert(state, 3)
|
|
|
|
if _, ok := state.SlotInAccessList(addr("bb"), slot("02")); ok {
|
|
|
|
t.Fatalf("slot present, expected missing")
|
|
|
|
}
|
|
|
|
verifyAddrs("aa", "bb")
|
|
|
|
verifySlots("bb", "01")
|
|
|
|
|
|
|
|
state.journal.revert(state, 2)
|
|
|
|
if _, ok := state.SlotInAccessList(addr("bb"), slot("01")); ok {
|
|
|
|
t.Fatalf("slot present, expected missing")
|
|
|
|
}
|
|
|
|
verifyAddrs("aa", "bb")
|
|
|
|
|
|
|
|
state.journal.revert(state, 1)
|
|
|
|
if state.AddressInAccessList(addr("bb")) {
|
|
|
|
t.Fatalf("addr present, expected missing")
|
|
|
|
}
|
|
|
|
verifyAddrs("aa")
|
|
|
|
|
|
|
|
state.journal.revert(state, 0)
|
|
|
|
if state.AddressInAccessList(addr("aa")) {
|
|
|
|
t.Fatalf("addr present, expected missing")
|
|
|
|
}
|
|
|
|
if got, exp := len(state.accessList.addresses), 0; got != exp {
|
|
|
|
t.Fatalf("expected empty, got %d", got)
|
|
|
|
}
|
|
|
|
if got, exp := len(state.accessList.slots), 0; got != exp {
|
|
|
|
t.Fatalf("expected empty, got %d", got)
|
|
|
|
}
|
|
|
|
// Check the copy
|
|
|
|
// Make a copy
|
|
|
|
state = stateCopy1
|
|
|
|
verifyAddrs("aa", "bb")
|
|
|
|
verifySlots("bb", "01", "02")
|
|
|
|
if got, exp := len(state.accessList.addresses), 2; got != exp {
|
|
|
|
t.Fatalf("expected empty, got %d", got)
|
|
|
|
}
|
|
|
|
if got, exp := len(state.accessList.slots), 1; got != exp {
|
|
|
|
t.Fatalf("expected empty, got %d", got)
|
|
|
|
}
|
|
|
|
}
|
2022-08-23 13:17:12 -05:00
|
|
|
|
|
|
|
// Tests that account and storage tries are flushed in the correct order and that
|
|
|
|
// no data loss occurs.
|
|
|
|
func TestFlushOrderDataLoss(t *testing.T) {
|
|
|
|
// Create a state trie with many accounts and slots
|
|
|
|
var (
|
|
|
|
memdb = rawdb.NewMemoryDatabase()
|
2024-02-13 07:49:53 -06:00
|
|
|
triedb = triedb.NewDatabase(memdb, nil)
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
statedb = NewDatabaseWithNodeDB(memdb, triedb)
|
2023-05-11 02:19:42 -05:00
|
|
|
state, _ = New(types.EmptyRootHash, statedb, nil)
|
2022-08-23 13:17:12 -05:00
|
|
|
)
|
|
|
|
for a := byte(0); a < 10; a++ {
|
|
|
|
state.CreateAccount(common.Address{a})
|
|
|
|
for s := byte(0); s < 10; s++ {
|
|
|
|
state.SetState(common.Address{a}, common.Hash{a, s}, common.Hash{a, s})
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 05:22:09 -05:00
|
|
|
root, err := state.Commit(0, false)
|
2022-08-23 13:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to commit state trie: %v", err)
|
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
triedb.Reference(root, common.Hash{})
|
|
|
|
if err := triedb.Cap(1024); err != nil {
|
2022-08-23 13:17:12 -05:00
|
|
|
t.Fatalf("failed to cap trie dirty cache: %v", err)
|
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
if err := triedb.Commit(root, false); err != nil {
|
2022-08-23 13:17:12 -05:00
|
|
|
t.Fatalf("failed to commit state trie: %v", err)
|
|
|
|
}
|
|
|
|
// Reopen the state trie from flushed disk and verify it
|
|
|
|
state, err = New(root, NewDatabase(memdb), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to reopen state trie: %v", err)
|
|
|
|
}
|
|
|
|
for a := byte(0); a < 10; a++ {
|
|
|
|
for s := byte(0); s < 10; s++ {
|
|
|
|
if have := state.GetState(common.Address{a}, common.Hash{a, s}); have != (common.Hash{a, s}) {
|
|
|
|
t.Errorf("account %d: slot %d: state mismatch: have %x, want %x", a, s, have, common.Hash{a, s})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 03:18:52 -06:00
|
|
|
|
|
|
|
func TestStateDBTransientStorage(t *testing.T) {
|
|
|
|
memDb := rawdb.NewMemoryDatabase()
|
|
|
|
db := NewDatabase(memDb)
|
2023-05-11 02:19:42 -05:00
|
|
|
state, _ := New(types.EmptyRootHash, db, nil)
|
2022-11-16 03:18:52 -06:00
|
|
|
|
|
|
|
key := common.Hash{0x01}
|
|
|
|
value := common.Hash{0x02}
|
|
|
|
addr := common.Address{}
|
|
|
|
|
|
|
|
state.SetTransientState(addr, key, value)
|
|
|
|
if exp, got := 1, state.journal.length(); exp != got {
|
|
|
|
t.Fatalf("journal length mismatch: have %d, want %d", got, exp)
|
|
|
|
}
|
|
|
|
// the retrieved value should equal what was set
|
|
|
|
if got := state.GetTransientState(addr, key); got != value {
|
|
|
|
t.Fatalf("transient storage mismatch: have %x, want %x", got, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// revert the transient state being set and then check that the
|
|
|
|
// value is now the empty hash
|
|
|
|
state.journal.revert(state, 0)
|
|
|
|
if got, exp := state.GetTransientState(addr, key), (common.Hash{}); exp != got {
|
|
|
|
t.Fatalf("transient storage mismatch: have %x, want %x", got, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set transient state and then copy the statedb and ensure that
|
|
|
|
// the transient state is copied
|
|
|
|
state.SetTransientState(addr, key, value)
|
|
|
|
cpy := state.Copy()
|
|
|
|
if got := cpy.GetTransientState(addr, key); got != value {
|
|
|
|
t.Fatalf("transient storage mismatch: have %x, want %x", got, value)
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 08:25:57 -05:00
|
|
|
|
2023-08-26 03:13:22 -05:00
|
|
|
func TestDeleteStorage(t *testing.T) {
|
|
|
|
var (
|
|
|
|
disk = rawdb.NewMemoryDatabase()
|
2024-02-13 07:49:53 -06:00
|
|
|
tdb = triedb.NewDatabase(disk, nil)
|
2023-08-26 03:13:22 -05:00
|
|
|
db = NewDatabaseWithNodeDB(disk, tdb)
|
|
|
|
snaps, _ = snapshot.New(snapshot.Config{CacheSize: 10}, disk, tdb, types.EmptyRootHash)
|
|
|
|
state, _ = New(types.EmptyRootHash, db, snaps)
|
|
|
|
addr = common.HexToAddress("0x1")
|
|
|
|
)
|
|
|
|
// Initialize account and populate storage
|
2024-03-22 12:53:53 -05:00
|
|
|
state.SetBalance(addr, uint256.NewInt(1), tracing.BalanceChangeUnspecified)
|
2023-08-26 03:13:22 -05:00
|
|
|
state.CreateAccount(addr)
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
slot := common.Hash(uint256.NewInt(uint64(i)).Bytes32())
|
|
|
|
value := common.Hash(uint256.NewInt(uint64(10 * i)).Bytes32())
|
|
|
|
state.SetState(addr, slot, value)
|
|
|
|
}
|
|
|
|
root, _ := state.Commit(0, true)
|
|
|
|
// Init phase done, create two states, one with snap and one without
|
|
|
|
fastState, _ := New(root, db, snaps)
|
|
|
|
slowState, _ := New(root, db, nil)
|
|
|
|
|
2024-01-14 05:32:23 -06:00
|
|
|
obj := fastState.getOrNewStateObject(addr)
|
2023-08-26 03:13:22 -05:00
|
|
|
storageRoot := obj.data.Root
|
|
|
|
|
2024-03-05 07:31:55 -06:00
|
|
|
_, fastNodes, err := fastState.deleteStorage(addr, crypto.Keccak256Hash(addr[:]), storageRoot)
|
2023-08-26 03:13:22 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-03-05 07:31:55 -06:00
|
|
|
_, slowNodes, err := slowState.deleteStorage(addr, crypto.Keccak256Hash(addr[:]), storageRoot)
|
2023-08-26 03:13:22 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
check := func(set *trienode.NodeSet) string {
|
|
|
|
var a []string
|
|
|
|
set.ForEachWithOrder(func(path string, n *trienode.Node) {
|
|
|
|
if n.Hash != (common.Hash{}) {
|
|
|
|
t.Fatal("delete should have empty hashes")
|
|
|
|
}
|
|
|
|
if len(n.Blob) != 0 {
|
2024-04-15 01:34:31 -05:00
|
|
|
t.Fatal("delete should have empty blobs")
|
2023-08-26 03:13:22 -05:00
|
|
|
}
|
|
|
|
a = append(a, fmt.Sprintf("%x", path))
|
|
|
|
})
|
|
|
|
return strings.Join(a, ",")
|
|
|
|
}
|
|
|
|
slowRes := check(slowNodes)
|
|
|
|
fastRes := check(fastNodes)
|
|
|
|
if slowRes != fastRes {
|
|
|
|
t.Fatalf("difference found:\nfast: %v\nslow: %v\n", fastRes, slowRes)
|
|
|
|
}
|
|
|
|
}
|
2024-05-10 02:57:38 -05:00
|
|
|
|
|
|
|
func TestStorageDirtiness(t *testing.T) {
|
|
|
|
var (
|
|
|
|
disk = rawdb.NewMemoryDatabase()
|
|
|
|
tdb = triedb.NewDatabase(disk, nil)
|
|
|
|
db = NewDatabaseWithNodeDB(disk, tdb)
|
|
|
|
state, _ = New(types.EmptyRootHash, db, nil)
|
|
|
|
addr = common.HexToAddress("0x1")
|
|
|
|
checkDirty = func(key common.Hash, value common.Hash, dirty bool) {
|
|
|
|
obj := state.getStateObject(addr)
|
|
|
|
v, exist := obj.dirtyStorage[key]
|
|
|
|
if exist != dirty {
|
|
|
|
t.Fatalf("Unexpected dirty marker, want: %t, got: %t", dirty, exist)
|
|
|
|
}
|
|
|
|
if v != value {
|
|
|
|
t.Fatalf("Unexpected storage slot, want: %t, got: %t", value, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
state.CreateAccount(addr)
|
|
|
|
|
|
|
|
// the storage change is noop, no dirty marker
|
|
|
|
state.SetState(addr, common.Hash{0x1}, common.Hash{})
|
|
|
|
checkDirty(common.Hash{0x1}, common.Hash{}, false)
|
|
|
|
|
|
|
|
// the storage change is valid, dirty marker is expected
|
|
|
|
snap := state.Snapshot()
|
|
|
|
state.SetState(addr, common.Hash{0x1}, common.Hash{0x1})
|
|
|
|
checkDirty(common.Hash{0x1}, common.Hash{0x1}, true)
|
|
|
|
|
|
|
|
// the storage change is reverted, dirtiness should be revoked
|
|
|
|
state.RevertToSnapshot(snap)
|
|
|
|
checkDirty(common.Hash{0x1}, common.Hash{}, false)
|
|
|
|
|
|
|
|
// the storage is reset back to its original value, dirtiness should be revoked
|
|
|
|
state.SetState(addr, common.Hash{0x1}, common.Hash{0x1})
|
|
|
|
snap = state.Snapshot()
|
|
|
|
state.SetState(addr, common.Hash{0x1}, common.Hash{})
|
|
|
|
checkDirty(common.Hash{0x1}, common.Hash{}, false)
|
|
|
|
|
|
|
|
// the storage change is reverted, dirty value should be set back
|
|
|
|
state.RevertToSnapshot(snap)
|
|
|
|
checkDirty(common.Hash{0x1}, common.Hash{0x1}, true)
|
|
|
|
}
|