2023-08-01 07:17:32 -05:00
|
|
|
// Copyright 2022 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package pathdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2024-03-18 21:50:08 -05:00
|
|
|
"github.com/ethereum/go-ethereum/internal/testrand"
|
2023-08-01 07:17:32 -05:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2024-06-27 07:30:39 -05:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2023-08-01 07:17:32 -05:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
2024-01-23 07:51:58 -06:00
|
|
|
"github.com/holiman/uint256"
|
2023-08-01 07:17:32 -05:00
|
|
|
)
|
|
|
|
|
2024-06-27 07:30:39 -05:00
|
|
|
func updateTrie(db *Database, stateRoot common.Hash, addrHash common.Hash, root common.Hash, dirties map[common.Hash][]byte) (common.Hash, *trienode.NodeSet) {
|
|
|
|
var id *trie.ID
|
|
|
|
if addrHash == (common.Hash{}) {
|
|
|
|
id = trie.StateTrieID(stateRoot)
|
|
|
|
} else {
|
|
|
|
id = trie.StorageTrieID(stateRoot, addrHash, root)
|
|
|
|
}
|
|
|
|
tr, err := trie.New(id, db)
|
2023-08-01 07:17:32 -05:00
|
|
|
if err != nil {
|
2024-06-27 07:30:39 -05:00
|
|
|
panic(fmt.Errorf("failed to load trie, err: %w", err))
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
for key, val := range dirties {
|
|
|
|
if len(val) == 0 {
|
2024-06-27 07:30:39 -05:00
|
|
|
tr.Delete(key.Bytes())
|
2023-08-01 07:17:32 -05:00
|
|
|
} else {
|
2024-06-27 07:30:39 -05:00
|
|
|
tr.Update(key.Bytes(), val)
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
}
|
2024-06-27 07:30:39 -05:00
|
|
|
return tr.Commit(false)
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateAccount(storageRoot common.Hash) types.StateAccount {
|
|
|
|
return types.StateAccount{
|
|
|
|
Nonce: uint64(rand.Intn(100)),
|
2024-01-23 07:51:58 -06:00
|
|
|
Balance: uint256.NewInt(rand.Uint64()),
|
2024-03-18 21:50:08 -05:00
|
|
|
CodeHash: testrand.Bytes(32),
|
2023-08-01 07:17:32 -05:00
|
|
|
Root: storageRoot,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
createAccountOp int = iota
|
|
|
|
modifyAccountOp
|
|
|
|
deleteAccountOp
|
|
|
|
opLen
|
|
|
|
)
|
|
|
|
|
|
|
|
type genctx struct {
|
2024-06-27 07:30:39 -05:00
|
|
|
stateRoot common.Hash
|
2023-08-01 07:17:32 -05:00
|
|
|
accounts map[common.Hash][]byte
|
|
|
|
storages map[common.Hash]map[common.Hash][]byte
|
|
|
|
accountOrigin map[common.Address][]byte
|
|
|
|
storageOrigin map[common.Address]map[common.Hash][]byte
|
|
|
|
nodes *trienode.MergedNodeSet
|
|
|
|
}
|
|
|
|
|
2024-06-27 07:30:39 -05:00
|
|
|
func newCtx(stateRoot common.Hash) *genctx {
|
2023-08-01 07:17:32 -05:00
|
|
|
return &genctx{
|
2024-06-27 07:30:39 -05:00
|
|
|
stateRoot: stateRoot,
|
2023-08-01 07:17:32 -05:00
|
|
|
accounts: make(map[common.Hash][]byte),
|
|
|
|
storages: make(map[common.Hash]map[common.Hash][]byte),
|
|
|
|
accountOrigin: make(map[common.Address][]byte),
|
|
|
|
storageOrigin: make(map[common.Address]map[common.Hash][]byte),
|
|
|
|
nodes: trienode.NewMergedNodeSet(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (ctx *genctx) storageOriginSet(rawStorageKey bool, t *tester) map[common.Address]map[common.Hash][]byte {
|
|
|
|
if !rawStorageKey {
|
|
|
|
return ctx.storageOrigin
|
|
|
|
}
|
|
|
|
set := make(map[common.Address]map[common.Hash][]byte)
|
|
|
|
for addr, storage := range ctx.storageOrigin {
|
|
|
|
subset := make(map[common.Hash][]byte)
|
|
|
|
for hash, val := range storage {
|
|
|
|
key := t.hashPreimage(hash)
|
|
|
|
subset[key] = val
|
|
|
|
}
|
|
|
|
set[addr] = subset
|
|
|
|
}
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
2023-08-01 07:17:32 -05:00
|
|
|
type tester struct {
|
|
|
|
db *Database
|
|
|
|
roots []common.Hash
|
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
|
|
|
preimages map[common.Hash][]byte
|
|
|
|
|
|
|
|
// current state set
|
|
|
|
accounts map[common.Hash][]byte
|
|
|
|
storages map[common.Hash]map[common.Hash][]byte
|
2023-08-01 07:17:32 -05:00
|
|
|
|
|
|
|
// state snapshots
|
|
|
|
snapAccounts map[common.Hash]map[common.Hash][]byte
|
|
|
|
snapStorages map[common.Hash]map[common.Hash]map[common.Hash][]byte
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func newTester(t *testing.T, historyLimit uint64, isVerkle bool) *tester {
|
2023-08-01 07:17:32 -05:00
|
|
|
var (
|
|
|
|
disk, _ = rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
|
2023-10-31 04:39:55 -05:00
|
|
|
db = New(disk, &Config{
|
2024-10-18 10:06:31 -05:00
|
|
|
StateHistory: historyLimit,
|
|
|
|
CleanCacheSize: 16 * 1024,
|
|
|
|
WriteBufferSize: 16 * 1024,
|
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
|
|
|
}, isVerkle)
|
2023-10-31 04:39:55 -05:00
|
|
|
obj = &tester{
|
2023-08-01 07:17:32 -05:00
|
|
|
db: db,
|
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
|
|
|
preimages: make(map[common.Hash][]byte),
|
2023-08-01 07:17:32 -05:00
|
|
|
accounts: make(map[common.Hash][]byte),
|
|
|
|
storages: make(map[common.Hash]map[common.Hash][]byte),
|
|
|
|
snapAccounts: make(map[common.Hash]map[common.Hash][]byte),
|
|
|
|
snapStorages: make(map[common.Hash]map[common.Hash]map[common.Hash][]byte),
|
|
|
|
}
|
|
|
|
)
|
2024-06-28 08:15:54 -05:00
|
|
|
for i := 0; i < 12; i++ {
|
2023-08-01 07:17:32 -05:00
|
|
|
var parent = types.EmptyRootHash
|
|
|
|
if len(obj.roots) != 0 {
|
|
|
|
parent = obj.roots[len(obj.roots)-1]
|
|
|
|
}
|
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, nodes, states := obj.generate(parent, i > 6)
|
|
|
|
|
2023-08-01 07:17:32 -05:00
|
|
|
if err := db.Update(root, parent, uint64(i), nodes, states); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to update state changes, err: %w", err))
|
|
|
|
}
|
|
|
|
obj.roots = append(obj.roots, root)
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (t *tester) accountPreimage(hash common.Hash) common.Address {
|
|
|
|
return common.BytesToAddress(t.preimages[hash])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tester) hashPreimage(hash common.Hash) common.Hash {
|
|
|
|
return common.BytesToHash(t.preimages[hash])
|
|
|
|
}
|
|
|
|
|
2023-08-01 07:17:32 -05:00
|
|
|
func (t *tester) release() {
|
|
|
|
t.db.Close()
|
|
|
|
t.db.diskdb.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tester) randAccount() (common.Address, []byte) {
|
|
|
|
for addrHash, account := range t.accounts {
|
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
|
|
|
return t.accountPreimage(addrHash), account
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
return common.Address{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tester) generateStorage(ctx *genctx, addr common.Address) common.Hash {
|
|
|
|
var (
|
|
|
|
addrHash = crypto.Keccak256Hash(addr.Bytes())
|
|
|
|
storage = make(map[common.Hash][]byte)
|
|
|
|
origin = make(map[common.Hash][]byte)
|
|
|
|
)
|
|
|
|
for i := 0; i < 10; i++ {
|
2024-03-18 21:50:08 -05:00
|
|
|
v, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(testrand.Bytes(32)))
|
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
|
|
|
key := testrand.Bytes(32)
|
|
|
|
hash := crypto.Keccak256Hash(key)
|
|
|
|
t.preimages[hash] = key
|
2023-08-01 07:17:32 -05:00
|
|
|
|
|
|
|
storage[hash] = v
|
|
|
|
origin[hash] = nil
|
|
|
|
}
|
2024-06-27 07:30:39 -05:00
|
|
|
root, set := updateTrie(t.db, ctx.stateRoot, addrHash, types.EmptyRootHash, storage)
|
2023-08-01 07:17:32 -05:00
|
|
|
|
|
|
|
ctx.storages[addrHash] = storage
|
|
|
|
ctx.storageOrigin[addr] = origin
|
|
|
|
ctx.nodes.Merge(set)
|
|
|
|
return root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tester) mutateStorage(ctx *genctx, addr common.Address, root common.Hash) common.Hash {
|
|
|
|
var (
|
|
|
|
addrHash = crypto.Keccak256Hash(addr.Bytes())
|
|
|
|
storage = make(map[common.Hash][]byte)
|
|
|
|
origin = make(map[common.Hash][]byte)
|
|
|
|
)
|
|
|
|
for hash, val := range t.storages[addrHash] {
|
|
|
|
origin[hash] = val
|
|
|
|
storage[hash] = nil
|
|
|
|
|
|
|
|
if len(origin) == 3 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < 3; i++ {
|
2024-03-18 21:50:08 -05:00
|
|
|
v, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(testrand.Bytes(32)))
|
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
|
|
|
key := testrand.Bytes(32)
|
|
|
|
hash := crypto.Keccak256Hash(key)
|
|
|
|
t.preimages[hash] = key
|
2023-08-01 07:17:32 -05:00
|
|
|
|
|
|
|
storage[hash] = v
|
|
|
|
origin[hash] = nil
|
|
|
|
}
|
2024-06-27 07:30:39 -05:00
|
|
|
root, set := updateTrie(t.db, ctx.stateRoot, crypto.Keccak256Hash(addr.Bytes()), root, storage)
|
2023-08-01 07:17:32 -05:00
|
|
|
|
|
|
|
ctx.storages[addrHash] = storage
|
|
|
|
ctx.storageOrigin[addr] = origin
|
|
|
|
ctx.nodes.Merge(set)
|
|
|
|
return root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tester) clearStorage(ctx *genctx, addr common.Address, root common.Hash) common.Hash {
|
|
|
|
var (
|
|
|
|
addrHash = crypto.Keccak256Hash(addr.Bytes())
|
|
|
|
storage = make(map[common.Hash][]byte)
|
|
|
|
origin = make(map[common.Hash][]byte)
|
|
|
|
)
|
|
|
|
for hash, val := range t.storages[addrHash] {
|
|
|
|
origin[hash] = val
|
|
|
|
storage[hash] = nil
|
|
|
|
}
|
2024-06-27 07:30:39 -05:00
|
|
|
root, set := updateTrie(t.db, ctx.stateRoot, addrHash, root, storage)
|
2023-08-01 07:17:32 -05:00
|
|
|
if root != types.EmptyRootHash {
|
|
|
|
panic("failed to clear storage trie")
|
|
|
|
}
|
|
|
|
ctx.storages[addrHash] = storage
|
|
|
|
ctx.storageOrigin[addr] = origin
|
|
|
|
ctx.nodes.Merge(set)
|
|
|
|
return root
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (t *tester) generate(parent common.Hash, rawStorageKey bool) (common.Hash, *trienode.MergedNodeSet, *StateSetWithOrigin) {
|
2023-08-01 07:17:32 -05:00
|
|
|
var (
|
2024-06-27 07:30:39 -05:00
|
|
|
ctx = newCtx(parent)
|
2023-08-01 07:17:32 -05:00
|
|
|
dirties = make(map[common.Hash]struct{})
|
|
|
|
)
|
|
|
|
for i := 0; i < 20; i++ {
|
2025-01-07 04:49:13 -06:00
|
|
|
// Start with account creation always
|
|
|
|
op := createAccountOp
|
|
|
|
if i > 0 {
|
|
|
|
op = rand.Intn(opLen)
|
|
|
|
}
|
|
|
|
switch op {
|
2023-08-01 07:17:32 -05:00
|
|
|
case createAccountOp:
|
|
|
|
// account creation
|
2024-03-18 21:50:08 -05:00
|
|
|
addr := testrand.Address()
|
2023-08-01 07:17:32 -05:00
|
|
|
addrHash := crypto.Keccak256Hash(addr.Bytes())
|
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
|
|
|
|
|
|
|
// short circuit if the account was already existent
|
2023-08-01 07:17:32 -05:00
|
|
|
if _, ok := t.accounts[addrHash]; ok {
|
|
|
|
continue
|
|
|
|
}
|
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
|
|
|
// short circuit if the account has been modified within the same transition
|
2023-08-01 07:17:32 -05:00
|
|
|
if _, ok := dirties[addrHash]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dirties[addrHash] = struct{}{}
|
|
|
|
|
|
|
|
root := t.generateStorage(ctx, addr)
|
|
|
|
ctx.accounts[addrHash] = types.SlimAccountRLP(generateAccount(root))
|
|
|
|
ctx.accountOrigin[addr] = nil
|
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
|
|
|
t.preimages[addrHash] = addr.Bytes()
|
2023-08-01 07:17:32 -05:00
|
|
|
|
|
|
|
case modifyAccountOp:
|
|
|
|
// account mutation
|
|
|
|
addr, account := t.randAccount()
|
|
|
|
if addr == (common.Address{}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrHash := crypto.Keccak256Hash(addr.Bytes())
|
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
|
|
|
|
|
|
|
// short circuit if the account has been modified within the same transition
|
2023-08-01 07:17:32 -05:00
|
|
|
if _, ok := dirties[addrHash]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dirties[addrHash] = struct{}{}
|
|
|
|
|
|
|
|
acct, _ := types.FullAccount(account)
|
|
|
|
stRoot := t.mutateStorage(ctx, addr, acct.Root)
|
|
|
|
newAccount := types.SlimAccountRLP(generateAccount(stRoot))
|
|
|
|
|
|
|
|
ctx.accounts[addrHash] = newAccount
|
|
|
|
ctx.accountOrigin[addr] = account
|
|
|
|
|
|
|
|
case deleteAccountOp:
|
|
|
|
// account deletion
|
|
|
|
addr, account := t.randAccount()
|
|
|
|
if addr == (common.Address{}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrHash := crypto.Keccak256Hash(addr.Bytes())
|
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
|
|
|
|
|
|
|
// short circuit if the account has been modified within the same transition
|
2023-08-01 07:17:32 -05:00
|
|
|
if _, ok := dirties[addrHash]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dirties[addrHash] = struct{}{}
|
|
|
|
|
|
|
|
acct, _ := types.FullAccount(account)
|
|
|
|
if acct.Root != types.EmptyRootHash {
|
|
|
|
t.clearStorage(ctx, addr, acct.Root)
|
|
|
|
}
|
|
|
|
ctx.accounts[addrHash] = nil
|
|
|
|
ctx.accountOrigin[addr] = account
|
|
|
|
}
|
|
|
|
}
|
2024-06-27 07:30:39 -05:00
|
|
|
root, set := updateTrie(t.db, parent, common.Hash{}, parent, ctx.accounts)
|
2023-08-01 07:17:32 -05:00
|
|
|
ctx.nodes.Merge(set)
|
|
|
|
|
|
|
|
// Save state snapshot before commit
|
|
|
|
t.snapAccounts[parent] = copyAccounts(t.accounts)
|
|
|
|
t.snapStorages[parent] = copyStorages(t.storages)
|
|
|
|
|
|
|
|
// Commit all changes to live state set
|
|
|
|
for addrHash, account := range ctx.accounts {
|
|
|
|
if len(account) == 0 {
|
|
|
|
delete(t.accounts, addrHash)
|
|
|
|
} else {
|
|
|
|
t.accounts[addrHash] = account
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for addrHash, slots := range ctx.storages {
|
|
|
|
if _, ok := t.storages[addrHash]; !ok {
|
|
|
|
t.storages[addrHash] = make(map[common.Hash][]byte)
|
|
|
|
}
|
|
|
|
for sHash, slot := range slots {
|
|
|
|
if len(slot) == 0 {
|
|
|
|
delete(t.storages[addrHash], sHash)
|
|
|
|
} else {
|
|
|
|
t.storages[addrHash][sHash] = slot
|
|
|
|
}
|
|
|
|
}
|
2024-06-27 07:30:39 -05:00
|
|
|
if len(t.storages[addrHash]) == 0 {
|
|
|
|
delete(t.storages, addrHash)
|
|
|
|
}
|
2023-08-01 07:17:32 -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
|
|
|
storageOrigin := ctx.storageOriginSet(rawStorageKey, t)
|
|
|
|
return root, ctx.nodes, NewStateSetWithOrigin(ctx.accounts, ctx.storages, ctx.accountOrigin, storageOrigin, rawStorageKey)
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
|
2024-03-26 15:01:28 -05:00
|
|
|
// lastHash returns the latest root hash, or empty if nothing is cached.
|
2023-08-01 07:17:32 -05:00
|
|
|
func (t *tester) lastHash() common.Hash {
|
|
|
|
if len(t.roots) == 0 {
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
return t.roots[len(t.roots)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tester) verifyState(root common.Hash) error {
|
2024-06-27 07:30:39 -05:00
|
|
|
tr, err := trie.New(trie.StateTrieID(root), t.db)
|
2023-08-01 07:17:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for addrHash, account := range t.snapAccounts[root] {
|
2024-06-27 07:30:39 -05:00
|
|
|
blob, err := tr.Get(addrHash.Bytes())
|
2023-08-01 07:17:32 -05:00
|
|
|
if err != nil || !bytes.Equal(blob, account) {
|
|
|
|
return fmt.Errorf("account is mismatched: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for addrHash, slots := range t.snapStorages[root] {
|
2024-06-27 07:30:39 -05:00
|
|
|
blob := t.snapAccounts[root][addrHash]
|
|
|
|
if len(blob) == 0 {
|
|
|
|
return fmt.Errorf("account %x is missing", addrHash)
|
|
|
|
}
|
|
|
|
account := new(types.StateAccount)
|
|
|
|
if err := rlp.DecodeBytes(blob, account); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
storageIt, err := trie.New(trie.StorageTrieID(root, addrHash, account.Root), t.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-01 07:17:32 -05:00
|
|
|
for hash, slot := range slots {
|
2024-06-27 07:30:39 -05:00
|
|
|
blob, err := storageIt.Get(hash.Bytes())
|
2023-08-01 07:17:32 -05:00
|
|
|
if err != nil || !bytes.Equal(blob, slot) {
|
|
|
|
return fmt.Errorf("slot is mismatched: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tester) verifyHistory() error {
|
|
|
|
bottom := t.bottomIndex()
|
|
|
|
for i, root := range t.roots {
|
|
|
|
// The state history related to the state above disk layer should not exist.
|
|
|
|
if i > bottom {
|
|
|
|
_, err := readHistory(t.db.freezer, uint64(i+1))
|
|
|
|
if err == nil {
|
|
|
|
return errors.New("unexpected state history")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// The state history related to the state below or equal to the disk layer
|
|
|
|
// should exist.
|
|
|
|
obj, err := readHistory(t.db.freezer, uint64(i+1))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
parent := types.EmptyRootHash
|
|
|
|
if i != 0 {
|
|
|
|
parent = t.roots[i-1]
|
|
|
|
}
|
|
|
|
if obj.meta.parent != parent {
|
|
|
|
return fmt.Errorf("unexpected parent, want: %x, got: %x", parent, obj.meta.parent)
|
|
|
|
}
|
|
|
|
if obj.meta.root != root {
|
|
|
|
return fmt.Errorf("unexpected root, want: %x, got: %x", root, obj.meta.root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bottomIndex returns the index of current disk layer.
|
|
|
|
func (t *tester) bottomIndex() int {
|
|
|
|
bottom := t.db.tree.bottom()
|
|
|
|
for i := 0; i < len(t.roots); i++ {
|
|
|
|
if t.roots[i] == bottom.rootHash() {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatabaseRollback(t *testing.T) {
|
2024-03-18 21:50:08 -05:00
|
|
|
// Redefine the diff layer depth allowance for faster testing.
|
|
|
|
maxDiffLayers = 4
|
|
|
|
defer func() {
|
|
|
|
maxDiffLayers = 128
|
|
|
|
}()
|
|
|
|
|
2023-08-01 07:17:32 -05:00
|
|
|
// Verify state histories
|
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
|
|
|
tester := newTester(t, 0, false)
|
2023-08-01 07:17:32 -05:00
|
|
|
defer tester.release()
|
|
|
|
|
|
|
|
if err := tester.verifyHistory(); err != nil {
|
|
|
|
t.Fatalf("Invalid state history, err: %v", err)
|
|
|
|
}
|
|
|
|
// Revert database from top to bottom
|
|
|
|
for i := tester.bottomIndex(); i >= 0; i-- {
|
|
|
|
parent := types.EmptyRootHash
|
|
|
|
if i > 0 {
|
|
|
|
parent = tester.roots[i-1]
|
|
|
|
}
|
2024-06-27 07:30:39 -05:00
|
|
|
if err := tester.db.Recover(parent); err != nil {
|
2023-08-01 07:17:32 -05:00
|
|
|
t.Fatalf("Failed to revert db, err: %v", err)
|
|
|
|
}
|
2024-03-04 04:03:53 -06:00
|
|
|
if i > 0 {
|
|
|
|
if err := tester.verifyState(parent); err != nil {
|
|
|
|
t.Fatalf("Failed to verify state, err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
if tester.db.tree.len() != 1 {
|
|
|
|
t.Fatal("Only disk layer is expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatabaseRecoverable(t *testing.T) {
|
2024-03-18 21:50:08 -05:00
|
|
|
// Redefine the diff layer depth allowance for faster testing.
|
|
|
|
maxDiffLayers = 4
|
|
|
|
defer func() {
|
|
|
|
maxDiffLayers = 128
|
|
|
|
}()
|
|
|
|
|
2023-08-01 07:17:32 -05:00
|
|
|
var (
|
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
|
|
|
tester = newTester(t, 0, false)
|
2023-08-01 07:17:32 -05:00
|
|
|
index = tester.bottomIndex()
|
|
|
|
)
|
|
|
|
defer tester.release()
|
|
|
|
|
|
|
|
var cases = []struct {
|
|
|
|
root common.Hash
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
// Unknown state should be unrecoverable
|
|
|
|
{common.Hash{0x1}, false},
|
|
|
|
|
|
|
|
// Initial state should be recoverable
|
|
|
|
{types.EmptyRootHash, true},
|
|
|
|
|
2025-01-10 06:51:19 -06:00
|
|
|
// common.Hash{} is not a valid state root for revert
|
|
|
|
{common.Hash{}, false},
|
2023-08-01 07:17:32 -05:00
|
|
|
|
|
|
|
// Layers below current disk layer are recoverable
|
|
|
|
{tester.roots[index-1], true},
|
|
|
|
|
|
|
|
// Disklayer itself is not recoverable, since it's
|
|
|
|
// available for accessing.
|
|
|
|
{tester.roots[index], false},
|
|
|
|
|
|
|
|
// Layers above current disk layer are not recoverable
|
|
|
|
// since they are available for accessing.
|
|
|
|
{tester.roots[index+1], false},
|
|
|
|
}
|
|
|
|
for i, c := range cases {
|
|
|
|
result := tester.db.Recoverable(c.root)
|
|
|
|
if result != c.expect {
|
|
|
|
t.Fatalf("case: %d, unexpected result, want %t, got %t", i, c.expect, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 02:00:53 -05:00
|
|
|
func TestDisable(t *testing.T) {
|
2024-03-18 21:50:08 -05:00
|
|
|
// Redefine the diff layer depth allowance for faster testing.
|
|
|
|
maxDiffLayers = 4
|
|
|
|
defer func() {
|
|
|
|
maxDiffLayers = 128
|
|
|
|
}()
|
|
|
|
|
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
|
|
|
tester := newTester(t, 0, false)
|
2023-08-01 07:17:32 -05:00
|
|
|
defer tester.release()
|
|
|
|
|
2024-04-30 09:25:35 -05:00
|
|
|
stored := crypto.Keccak256Hash(rawdb.ReadAccountTrieNode(tester.db.diskdb, nil))
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 02:00:53 -05:00
|
|
|
if err := tester.db.Disable(); err != nil {
|
2024-04-23 03:33:36 -05:00
|
|
|
t.Fatalf("Failed to deactivate database: %v", err)
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 02:00:53 -05:00
|
|
|
if err := tester.db.Enable(types.EmptyRootHash); err == nil {
|
2024-04-23 03:33:36 -05:00
|
|
|
t.Fatal("Invalid activation should be rejected")
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 02:00:53 -05:00
|
|
|
if err := tester.db.Enable(stored); err != nil {
|
2024-04-23 03:33:36 -05:00
|
|
|
t.Fatalf("Failed to activate database: %v", err)
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 02:00:53 -05:00
|
|
|
}
|
|
|
|
|
2023-08-01 07:17:32 -05:00
|
|
|
// Ensure journal is deleted from disk
|
|
|
|
if blob := rawdb.ReadTrieJournal(tester.db.diskdb); len(blob) != 0 {
|
|
|
|
t.Fatal("Failed to clean journal")
|
|
|
|
}
|
|
|
|
// Ensure all trie histories are removed
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 02:00:53 -05:00
|
|
|
n, err := tester.db.freezer.Ancients()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to clean state history")
|
|
|
|
}
|
|
|
|
if n != 0 {
|
|
|
|
t.Fatal("Failed to clean state history")
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
// Verify layer tree structure, single disk layer is expected
|
|
|
|
if tester.db.tree.len() != 1 {
|
|
|
|
t.Fatalf("Extra layer kept %d", tester.db.tree.len())
|
|
|
|
}
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 02:00:53 -05:00
|
|
|
if tester.db.tree.bottom().rootHash() != stored {
|
|
|
|
t.Fatalf("Root hash is not matched exp %x got %x", stored, tester.db.tree.bottom().rootHash())
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommit(t *testing.T) {
|
2024-03-18 21:50:08 -05:00
|
|
|
// Redefine the diff layer depth allowance for faster testing.
|
|
|
|
maxDiffLayers = 4
|
|
|
|
defer func() {
|
|
|
|
maxDiffLayers = 128
|
|
|
|
}()
|
|
|
|
|
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
|
|
|
tester := newTester(t, 0, false)
|
2023-08-01 07:17:32 -05:00
|
|
|
defer tester.release()
|
|
|
|
|
|
|
|
if err := tester.db.Commit(tester.lastHash(), false); err != nil {
|
|
|
|
t.Fatalf("Failed to cap database, err: %v", err)
|
|
|
|
}
|
|
|
|
// Verify layer tree structure, single disk layer is expected
|
|
|
|
if tester.db.tree.len() != 1 {
|
|
|
|
t.Fatal("Layer tree structure is invalid")
|
|
|
|
}
|
|
|
|
if tester.db.tree.bottom().rootHash() != tester.lastHash() {
|
|
|
|
t.Fatal("Layer tree structure is invalid")
|
|
|
|
}
|
|
|
|
// Verify states
|
|
|
|
if err := tester.verifyState(tester.lastHash()); err != nil {
|
|
|
|
t.Fatalf("State is invalid, err: %v", err)
|
|
|
|
}
|
|
|
|
// Verify state histories
|
|
|
|
if err := tester.verifyHistory(); err != nil {
|
|
|
|
t.Fatalf("State history is invalid, err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJournal(t *testing.T) {
|
2024-03-18 21:50:08 -05:00
|
|
|
// Redefine the diff layer depth allowance for faster testing.
|
|
|
|
maxDiffLayers = 4
|
|
|
|
defer func() {
|
|
|
|
maxDiffLayers = 128
|
|
|
|
}()
|
|
|
|
|
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
|
|
|
tester := newTester(t, 0, false)
|
2023-08-01 07:17:32 -05:00
|
|
|
defer tester.release()
|
|
|
|
|
|
|
|
if err := tester.db.Journal(tester.lastHash()); err != nil {
|
|
|
|
t.Errorf("Failed to journal, err: %v", err)
|
|
|
|
}
|
|
|
|
tester.db.Close()
|
2024-03-26 15:25:41 -05:00
|
|
|
tester.db = New(tester.db.diskdb, nil, false)
|
2023-08-01 07:17:32 -05:00
|
|
|
|
|
|
|
// Verify states including disk layer and all diff on top.
|
|
|
|
for i := 0; i < len(tester.roots); i++ {
|
|
|
|
if i >= tester.bottomIndex() {
|
|
|
|
if err := tester.verifyState(tester.roots[i]); err != nil {
|
|
|
|
t.Fatalf("Invalid state, err: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := tester.verifyState(tester.roots[i]); err == nil {
|
|
|
|
t.Fatal("Unexpected state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCorruptedJournal(t *testing.T) {
|
2024-03-18 21:50:08 -05:00
|
|
|
// Redefine the diff layer depth allowance for faster testing.
|
|
|
|
maxDiffLayers = 4
|
|
|
|
defer func() {
|
|
|
|
maxDiffLayers = 128
|
|
|
|
}()
|
|
|
|
|
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
|
|
|
tester := newTester(t, 0, false)
|
2023-08-01 07:17:32 -05:00
|
|
|
defer tester.release()
|
|
|
|
|
|
|
|
if err := tester.db.Journal(tester.lastHash()); err != nil {
|
|
|
|
t.Errorf("Failed to journal, err: %v", err)
|
|
|
|
}
|
|
|
|
tester.db.Close()
|
2024-04-30 09:25:35 -05:00
|
|
|
root := crypto.Keccak256Hash(rawdb.ReadAccountTrieNode(tester.db.diskdb, nil))
|
2023-08-01 07:17:32 -05:00
|
|
|
|
|
|
|
// Mutate the journal in disk, it should be regarded as invalid
|
|
|
|
blob := rawdb.ReadTrieJournal(tester.db.diskdb)
|
2024-03-05 07:31:55 -06:00
|
|
|
blob[0] = 0xa
|
2023-08-01 07:17:32 -05:00
|
|
|
rawdb.WriteTrieJournal(tester.db.diskdb, blob)
|
|
|
|
|
|
|
|
// Verify states, all not-yet-written states should be discarded
|
2024-03-26 15:25:41 -05:00
|
|
|
tester.db = New(tester.db.diskdb, nil, false)
|
2023-08-01 07:17:32 -05:00
|
|
|
for i := 0; i < len(tester.roots); i++ {
|
|
|
|
if tester.roots[i] == root {
|
|
|
|
if err := tester.verifyState(root); err != nil {
|
|
|
|
t.Fatalf("Disk state is corrupted, err: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := tester.verifyState(tester.roots[i]); err == nil {
|
|
|
|
t.Fatal("Unexpected state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 04:39:55 -05:00
|
|
|
// TestTailTruncateHistory function is designed to test a specific edge case where,
|
|
|
|
// when history objects are removed from the end, it should trigger a state flush
|
|
|
|
// if the ID of the new tail object is even higher than the persisted state ID.
|
|
|
|
//
|
|
|
|
// For example, let's say the ID of the persistent state is 10, and the current
|
|
|
|
// history objects range from ID(5) to ID(15). As we accumulate six more objects,
|
|
|
|
// the history will expand to cover ID(11) to ID(21). ID(11) then becomes the
|
|
|
|
// oldest history object, and its ID is even higher than the stored state.
|
|
|
|
//
|
|
|
|
// In this scenario, it is mandatory to update the persistent state before
|
|
|
|
// truncating the tail histories. This ensures that the ID of the persistent state
|
|
|
|
// always falls within the range of [oldest-history-id, latest-history-id].
|
|
|
|
func TestTailTruncateHistory(t *testing.T) {
|
2024-03-18 21:50:08 -05:00
|
|
|
// Redefine the diff layer depth allowance for faster testing.
|
|
|
|
maxDiffLayers = 4
|
|
|
|
defer func() {
|
|
|
|
maxDiffLayers = 128
|
|
|
|
}()
|
|
|
|
|
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
|
|
|
tester := newTester(t, 10, false)
|
2023-10-31 04:39:55 -05:00
|
|
|
defer tester.release()
|
|
|
|
|
|
|
|
tester.db.Close()
|
2024-03-26 15:25:41 -05:00
|
|
|
tester.db = New(tester.db.diskdb, &Config{StateHistory: 10}, false)
|
2023-10-31 04:39:55 -05:00
|
|
|
|
|
|
|
head, err := tester.db.freezer.Ancients()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to obtain freezer head")
|
|
|
|
}
|
|
|
|
stored := rawdb.ReadPersistentStateID(tester.db.diskdb)
|
|
|
|
if head != stored {
|
|
|
|
t.Fatalf("Failed to truncate excess history object above, stored: %d, head: %d", stored, head)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 07:17:32 -05:00
|
|
|
// copyAccounts returns a deep-copied account set of the provided one.
|
|
|
|
func copyAccounts(set map[common.Hash][]byte) map[common.Hash][]byte {
|
|
|
|
copied := make(map[common.Hash][]byte, len(set))
|
|
|
|
for key, val := range set {
|
|
|
|
copied[key] = common.CopyBytes(val)
|
|
|
|
}
|
|
|
|
return copied
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyStorages returns a deep-copied storage set of the provided one.
|
|
|
|
func copyStorages(set map[common.Hash]map[common.Hash][]byte) map[common.Hash]map[common.Hash][]byte {
|
|
|
|
copied := make(map[common.Hash]map[common.Hash][]byte, len(set))
|
|
|
|
for addrHash, subset := range set {
|
|
|
|
copied[addrHash] = make(map[common.Hash][]byte, len(subset))
|
|
|
|
for key, val := range subset {
|
|
|
|
copied[addrHash][key] = common.CopyBytes(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return copied
|
|
|
|
}
|