2018-02-14 06:49:11 -06:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
2017-04-19 05:09:04 -05: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 eth
|
|
|
|
|
|
|
|
import (
|
2019-07-13 08:48:55 -05:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-04-19 05:09:04 -05:00
|
|
|
"reflect"
|
2024-03-25 01:50:18 -05:00
|
|
|
"slices"
|
2023-11-28 06:54:17 -06:00
|
|
|
"strings"
|
2017-04-19 05:09:04 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-09-24 07:57:49 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2017-04-19 05:09:04 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2024-03-22 12:53:53 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
2023-05-11 02:19:42 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2019-07-13 08:48:55 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2024-02-13 07:49:53 -06:00
|
|
|
"github.com/ethereum/go-ethereum/triedb"
|
2024-01-23 07:51:58 -06:00
|
|
|
"github.com/holiman/uint256"
|
2017-04-19 05:09:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var dumper = spew.ConfigState{Indent: " "}
|
|
|
|
|
2023-11-28 06:54:17 -06:00
|
|
|
func accountRangeTest(t *testing.T, trie *state.Trie, statedb *state.StateDB, start common.Hash, requestedNum int, expectedNum int) state.Dump {
|
|
|
|
result := statedb.RawDump(&state.DumpConfig{
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 03:05:39 -05:00
|
|
|
SkipCode: true,
|
|
|
|
SkipStorage: true,
|
|
|
|
OnlyWithAddresses: false,
|
|
|
|
Start: start.Bytes(),
|
|
|
|
Max: uint64(requestedNum),
|
|
|
|
})
|
2019-07-13 08:48:55 -05:00
|
|
|
|
|
|
|
if len(result.Accounts) != expectedNum {
|
2020-03-31 05:08:44 -05:00
|
|
|
t.Fatalf("expected %d results, got %d", expectedNum, len(result.Accounts))
|
2019-07-13 08:48:55 -05:00
|
|
|
}
|
2023-11-28 06:54:17 -06:00
|
|
|
for addr, acc := range result.Accounts {
|
|
|
|
if strings.HasSuffix(addr, "pre") || acc.Address == nil {
|
|
|
|
t.Fatalf("account without prestate (address) returned: %v", addr)
|
2019-07-13 08:48:55 -05:00
|
|
|
}
|
2023-11-28 06:54:17 -06:00
|
|
|
if !statedb.Exist(*acc.Address) {
|
|
|
|
t.Fatalf("account not found in state %s", acc.Address.Hex())
|
2019-07-13 08:48:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountRange(t *testing.T) {
|
2020-12-14 03:27:15 -06:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-13 08:48:55 -05:00
|
|
|
var (
|
2024-09-05 05:10:47 -05:00
|
|
|
mdb = rawdb.NewMemoryDatabase()
|
|
|
|
statedb = state.NewDatabase(triedb.NewDatabase(mdb, &triedb.Config{Preimages: true}), nil)
|
|
|
|
sdb, _ = state.New(types.EmptyRootHash, statedb)
|
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
|
|
|
addrs = [AccountRangeMaxResults * 2]common.Address{}
|
|
|
|
m = map[common.Address]bool{}
|
2019-07-13 08:48:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
for i := range addrs {
|
|
|
|
hash := common.HexToHash(fmt.Sprintf("%x", i))
|
|
|
|
addr := common.BytesToAddress(crypto.Keccak256Hash(hash.Bytes()).Bytes())
|
|
|
|
addrs[i] = addr
|
2024-03-22 12:53:53 -05:00
|
|
|
sdb.SetBalance(addrs[i], uint256.NewInt(1), tracing.BalanceChangeUnspecified)
|
2019-07-13 08:48:55 -05:00
|
|
|
if _, ok := m[addr]; ok {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
} else {
|
|
|
|
m[addr] = true
|
|
|
|
}
|
|
|
|
}
|
all: implement state history v2 (#30107)
This pull request delivers the new version of the state history, where
the raw storage key is used instead of the hash.
Before the cancun fork, it's supported by protocol to destruct a
specific account and therefore, all the storage slot owned by it should
be wiped in the same transition.
Technically, storage wiping should be performed through storage
iteration, and only the storage key hash will be available for traversal
if the state snapshot is not available. Therefore, the storage key hash
is chosen as the identifier in the old version state history.
Fortunately, account self-destruction has been deprecated by the
protocol since the Cancun fork, and there are no empty accounts eligible
for deletion under EIP-158. Therefore, we can conclude that no storage
wiping should occur after the Cancun fork. In this case, it makes no
sense to keep using hash.
Besides, another big reason for making this change is the current format
state history is unusable if verkle is activated. Verkle tree has a
different key derivation scheme (merkle uses keccak256), the preimage of
key hash must be provided in order to make verkle rollback functional.
This pull request is a prerequisite for landing verkle.
Additionally, the raw storage key is more human-friendly for those who
want to manually check the history, even though Solidity already
performs some hashing to derive the storage location.
---
This pull request doesn't bump the database version, as I believe the
database should still be compatible if users degrade from the new geth
version to old one, the only side effect is the persistent new version
state history will be unusable.
---------
Co-authored-by: Zsolt Felfoldi <zsfelfoldi@gmail.com>
2025-01-16 19:59:02 -06:00
|
|
|
root, _ := sdb.Commit(0, true, false)
|
2024-09-05 05:10:47 -05:00
|
|
|
sdb, _ = state.New(root, statedb)
|
2019-07-13 08:48:55 -05:00
|
|
|
|
|
|
|
trie, err := statedb.OpenTrie(root)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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
|
|
|
accountRangeTest(t, &trie, sdb, common.Hash{}, AccountRangeMaxResults/2, AccountRangeMaxResults/2)
|
2019-07-13 08:48:55 -05:00
|
|
|
// test pagination
|
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
|
|
|
firstResult := accountRangeTest(t, &trie, sdb, common.Hash{}, AccountRangeMaxResults, AccountRangeMaxResults)
|
|
|
|
secondResult := accountRangeTest(t, &trie, sdb, common.BytesToHash(firstResult.Next), AccountRangeMaxResults, AccountRangeMaxResults)
|
2019-07-13 08:48:55 -05:00
|
|
|
|
2023-06-19 16:38:57 -05:00
|
|
|
hList := make([]common.Hash, 0)
|
2023-11-28 06:54:17 -06:00
|
|
|
for addr1, acc := range firstResult.Accounts {
|
|
|
|
// If address is non-available, then it makes no sense to compare
|
2020-03-31 05:08:44 -05:00
|
|
|
// them as they might be two different accounts.
|
2023-11-28 06:54:17 -06:00
|
|
|
if acc.Address == nil {
|
2020-03-31 05:08:44 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, duplicate := secondResult.Accounts[addr1]; duplicate {
|
|
|
|
t.Fatalf("pagination test failed: results should not overlap")
|
2019-07-13 08:48:55 -05:00
|
|
|
}
|
2023-11-28 06:54:17 -06:00
|
|
|
hList = append(hList, crypto.Keccak256Hash(acc.Address.Bytes()))
|
2019-07-13 08:48:55 -05:00
|
|
|
}
|
|
|
|
// Test to see if it's possible to recover from the middle of the previous
|
|
|
|
// set and get an even split between the first and second sets.
|
2023-08-11 17:04:12 -05:00
|
|
|
slices.SortFunc(hList, common.Hash.Cmp)
|
2019-07-13 08:48:55 -05:00
|
|
|
middleH := hList[AccountRangeMaxResults/2]
|
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
|
|
|
middleResult := accountRangeTest(t, &trie, sdb, middleH, AccountRangeMaxResults, AccountRangeMaxResults)
|
2020-03-31 05:08:44 -05:00
|
|
|
missing, infirst, insecond := 0, 0, 0
|
2019-07-13 08:48:55 -05:00
|
|
|
for h := range middleResult.Accounts {
|
|
|
|
if _, ok := firstResult.Accounts[h]; ok {
|
|
|
|
infirst++
|
|
|
|
} else if _, ok := secondResult.Accounts[h]; ok {
|
|
|
|
insecond++
|
|
|
|
} else {
|
2020-03-31 05:08:44 -05:00
|
|
|
missing++
|
2019-07-13 08:48:55 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-31 05:08:44 -05:00
|
|
|
if missing != 0 {
|
|
|
|
t.Fatalf("%d hashes in the 'middle' set were neither in the first not the second set", missing)
|
2019-07-13 08:48:55 -05:00
|
|
|
}
|
|
|
|
if infirst != AccountRangeMaxResults/2 {
|
|
|
|
t.Fatalf("Imbalance in the number of first-test results: %d != %d", infirst, AccountRangeMaxResults/2)
|
|
|
|
}
|
|
|
|
if insecond != AccountRangeMaxResults/2 {
|
|
|
|
t.Fatalf("Imbalance in the number of second-test results: %d != %d", insecond, AccountRangeMaxResults/2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyAccountRange(t *testing.T) {
|
2020-12-14 03:27:15 -06:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-13 08:48:55 -05:00
|
|
|
var (
|
2024-09-05 05:10:47 -05:00
|
|
|
statedb = state.NewDatabaseForTesting()
|
|
|
|
st, _ = state.New(types.EmptyRootHash, statedb)
|
2019-07-13 08:48:55 -05:00
|
|
|
)
|
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
|
|
|
// Commit(although nothing to flush) and re-init the statedb
|
all: implement state history v2 (#30107)
This pull request delivers the new version of the state history, where
the raw storage key is used instead of the hash.
Before the cancun fork, it's supported by protocol to destruct a
specific account and therefore, all the storage slot owned by it should
be wiped in the same transition.
Technically, storage wiping should be performed through storage
iteration, and only the storage key hash will be available for traversal
if the state snapshot is not available. Therefore, the storage key hash
is chosen as the identifier in the old version state history.
Fortunately, account self-destruction has been deprecated by the
protocol since the Cancun fork, and there are no empty accounts eligible
for deletion under EIP-158. Therefore, we can conclude that no storage
wiping should occur after the Cancun fork. In this case, it makes no
sense to keep using hash.
Besides, another big reason for making this change is the current format
state history is unusable if verkle is activated. Verkle tree has a
different key derivation scheme (merkle uses keccak256), the preimage of
key hash must be provided in order to make verkle rollback functional.
This pull request is a prerequisite for landing verkle.
Additionally, the raw storage key is more human-friendly for those who
want to manually check the history, even though Solidity already
performs some hashing to derive the storage location.
---
This pull request doesn't bump the database version, as I believe the
database should still be compatible if users degrade from the new geth
version to old one, the only side effect is the persistent new version
state history will be unusable.
---------
Co-authored-by: Zsolt Felfoldi <zsfelfoldi@gmail.com>
2025-01-16 19:59:02 -06:00
|
|
|
st.Commit(0, true, false)
|
2024-09-05 05:10:47 -05:00
|
|
|
st, _ = state.New(types.EmptyRootHash, statedb)
|
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
|
|
|
|
2023-11-28 06:54:17 -06:00
|
|
|
results := st.RawDump(&state.DumpConfig{
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 03:05:39 -05:00
|
|
|
SkipCode: true,
|
|
|
|
SkipStorage: true,
|
|
|
|
OnlyWithAddresses: true,
|
|
|
|
Max: uint64(AccountRangeMaxResults),
|
|
|
|
})
|
2020-03-31 05:08:44 -05:00
|
|
|
if bytes.Equal(results.Next, (common.Hash{}).Bytes()) {
|
2019-07-13 08:48:55 -05:00
|
|
|
t.Fatalf("Empty results should not return a second page")
|
|
|
|
}
|
|
|
|
if len(results.Accounts) != 0 {
|
|
|
|
t.Fatalf("Empty state should not return addresses: %v", results.Accounts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 05:09:04 -05:00
|
|
|
func TestStorageRangeAt(t *testing.T) {
|
2020-12-14 03:27:15 -06:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-04-19 05:09:04 -05:00
|
|
|
// Create a state where account 0x010000... has a few storage entries.
|
|
|
|
var (
|
2024-09-05 05:10:47 -05:00
|
|
|
mdb = rawdb.NewMemoryDatabase()
|
|
|
|
tdb = triedb.NewDatabase(mdb, &triedb.Config{Preimages: true})
|
|
|
|
db = state.NewDatabase(tdb, nil)
|
|
|
|
sdb, _ = state.New(types.EmptyRootHash, db)
|
2023-08-31 13:33:18 -05:00
|
|
|
addr = common.Address{0x01}
|
|
|
|
keys = []common.Hash{ // hashes of Keys of storage
|
2017-04-19 05:09:04 -05:00
|
|
|
common.HexToHash("340dd630ad21bf010b4e676dbfa9ba9a02175262d1fa356232cfde6cb5b47ef2"),
|
|
|
|
common.HexToHash("426fcb404ab2d5d8e61a3d918108006bbb0a9be65e92235bb10eefbdb6dcd053"),
|
|
|
|
common.HexToHash("48078cfed56339ea54962e72c37c7f588fc4f8e5bc173827ba75cb10a63a96a5"),
|
|
|
|
common.HexToHash("5723d2c3a83af9b735e3b7f21531e5623d183a9095a56604ead41f3582fdfb75"),
|
|
|
|
}
|
|
|
|
storage = storageMap{
|
|
|
|
keys[0]: {Key: &common.Hash{0x02}, Value: common.Hash{0x01}},
|
|
|
|
keys[1]: {Key: &common.Hash{0x04}, Value: common.Hash{0x02}},
|
|
|
|
keys[2]: {Key: &common.Hash{0x01}, Value: common.Hash{0x03}},
|
|
|
|
keys[3]: {Key: &common.Hash{0x03}, Value: common.Hash{0x04}},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
for _, entry := range storage {
|
2023-08-31 13:33:18 -05:00
|
|
|
sdb.SetState(addr, *entry.Key, entry.Value)
|
2017-04-19 05:09:04 -05:00
|
|
|
}
|
all: implement state history v2 (#30107)
This pull request delivers the new version of the state history, where
the raw storage key is used instead of the hash.
Before the cancun fork, it's supported by protocol to destruct a
specific account and therefore, all the storage slot owned by it should
be wiped in the same transition.
Technically, storage wiping should be performed through storage
iteration, and only the storage key hash will be available for traversal
if the state snapshot is not available. Therefore, the storage key hash
is chosen as the identifier in the old version state history.
Fortunately, account self-destruction has been deprecated by the
protocol since the Cancun fork, and there are no empty accounts eligible
for deletion under EIP-158. Therefore, we can conclude that no storage
wiping should occur after the Cancun fork. In this case, it makes no
sense to keep using hash.
Besides, another big reason for making this change is the current format
state history is unusable if verkle is activated. Verkle tree has a
different key derivation scheme (merkle uses keccak256), the preimage of
key hash must be provided in order to make verkle rollback functional.
This pull request is a prerequisite for landing verkle.
Additionally, the raw storage key is more human-friendly for those who
want to manually check the history, even though Solidity already
performs some hashing to derive the storage location.
---
This pull request doesn't bump the database version, as I believe the
database should still be compatible if users degrade from the new geth
version to old one, the only side effect is the persistent new version
state history will be unusable.
---------
Co-authored-by: Zsolt Felfoldi <zsfelfoldi@gmail.com>
2025-01-16 19:59:02 -06:00
|
|
|
root, _ := sdb.Commit(0, false, false)
|
2024-09-05 05:10:47 -05:00
|
|
|
sdb, _ = state.New(root, db)
|
2017-04-19 05:09:04 -05:00
|
|
|
|
|
|
|
// Check a few combinations of limit and start/end.
|
|
|
|
tests := []struct {
|
|
|
|
start []byte
|
|
|
|
limit int
|
|
|
|
want StorageRangeResult
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
start: []byte{}, limit: 0,
|
|
|
|
want: StorageRangeResult{storageMap{}, &keys[0]},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start: []byte{}, limit: 100,
|
|
|
|
want: StorageRangeResult{storage, nil},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start: []byte{}, limit: 2,
|
|
|
|
want: StorageRangeResult{storageMap{keys[0]: storage[keys[0]], keys[1]: storage[keys[1]]}, &keys[2]},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start: []byte{0x00}, limit: 4,
|
|
|
|
want: StorageRangeResult{storage, nil},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start: []byte{0x40}, limit: 2,
|
|
|
|
want: StorageRangeResult{storageMap{keys[1]: storage[keys[1]], keys[2]: storage[keys[2]]}, &keys[3]},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2023-08-31 13:33:18 -05:00
|
|
|
result, err := storageRangeAt(sdb, root, addr, test.start, test.limit)
|
2017-12-06 09:42:16 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-04-19 05:09:04 -05:00
|
|
|
if !reflect.DeepEqual(result, test.want) {
|
2022-07-04 03:03:32 -05:00
|
|
|
t.Fatalf("wrong result for range %#x.., limit %d:\ngot %s\nwant %s",
|
2017-04-19 05:09:04 -05:00
|
|
|
test.start, test.limit, dumper.Sdump(result), dumper.Sdump(&test.want))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|