2024-06-27 07:30:39 -05:00
|
|
|
// Copyright 2024 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 (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
|
|
|
"github.com/ethereum/go-ethereum/triedb/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
// context wraps all fields for executing state diffs.
|
|
|
|
type context struct {
|
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
|
|
|
prevRoot common.Hash
|
|
|
|
postRoot common.Hash
|
|
|
|
accounts map[common.Address][]byte
|
|
|
|
storages map[common.Address]map[common.Hash][]byte
|
|
|
|
nodes *trienode.MergedNodeSet
|
|
|
|
rawStorageKey bool
|
2024-06-27 07:30:39 -05:00
|
|
|
|
|
|
|
// TODO (rjl493456442) abstract out the state hasher
|
|
|
|
// for supporting verkle tree.
|
|
|
|
accountTrie *trie.Trie
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply processes the given state diffs, updates the corresponding post-state
|
|
|
|
// and returns the trie nodes that have been modified.
|
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 apply(db database.NodeDatabase, prevRoot common.Hash, postRoot common.Hash, rawStorageKey bool, accounts map[common.Address][]byte, storages map[common.Address]map[common.Hash][]byte) (map[common.Hash]map[string]*trienode.Node, error) {
|
2024-06-27 07:30:39 -05:00
|
|
|
tr, err := trie.New(trie.TrieID(postRoot), db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx := &context{
|
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
|
|
|
prevRoot: prevRoot,
|
|
|
|
postRoot: postRoot,
|
|
|
|
accounts: accounts,
|
|
|
|
storages: storages,
|
|
|
|
accountTrie: tr,
|
|
|
|
rawStorageKey: rawStorageKey,
|
|
|
|
nodes: trienode.NewMergedNodeSet(),
|
2024-06-27 07:30:39 -05:00
|
|
|
}
|
|
|
|
for addr, account := range accounts {
|
|
|
|
var err error
|
|
|
|
if len(account) == 0 {
|
|
|
|
err = deleteAccount(ctx, db, addr)
|
|
|
|
} else {
|
|
|
|
err = updateAccount(ctx, db, addr)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to revert state, err: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
root, result := tr.Commit(false)
|
|
|
|
if root != prevRoot {
|
|
|
|
return nil, fmt.Errorf("failed to revert state, want %#x, got %#x", prevRoot, root)
|
|
|
|
}
|
|
|
|
if err := ctx.nodes.Merge(result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ctx.nodes.Flatten(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateAccount the account was present in prev-state, and may or may not
|
|
|
|
// existent in post-state. Apply the reverse diff and verify if the storage
|
|
|
|
// root matches the one in prev-state account.
|
2024-10-18 10:06:31 -05:00
|
|
|
func updateAccount(ctx *context, db database.NodeDatabase, addr common.Address) error {
|
2024-06-27 07:30:39 -05:00
|
|
|
// The account was present in prev-state, decode it from the
|
|
|
|
// 'slim-rlp' format bytes.
|
|
|
|
h := newHasher()
|
|
|
|
defer h.release()
|
|
|
|
|
|
|
|
addrHash := h.hash(addr.Bytes())
|
|
|
|
prev, err := types.FullAccount(ctx.accounts[addr])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// The account may or may not existent in post-state, try to
|
|
|
|
// load it and decode if it's found.
|
|
|
|
blob, err := ctx.accountTrie.Get(addrHash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
post := types.NewEmptyStateAccount()
|
|
|
|
if len(blob) != 0 {
|
|
|
|
if err := rlp.DecodeBytes(blob, &post); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Apply all storage changes into the post-state storage trie.
|
|
|
|
st, err := trie.New(trie.StorageTrieID(ctx.postRoot, addrHash, post.Root), db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for key, val := range ctx.storages[addr] {
|
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
|
|
|
tkey := key
|
|
|
|
if ctx.rawStorageKey {
|
|
|
|
tkey = h.hash(key.Bytes())
|
|
|
|
}
|
2024-06-27 07:30:39 -05:00
|
|
|
var err error
|
|
|
|
if len(val) == 0 {
|
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
|
|
|
err = st.Delete(tkey.Bytes())
|
2024-06-27 07:30:39 -05:00
|
|
|
} else {
|
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
|
|
|
err = st.Update(tkey.Bytes(), val)
|
2024-06-27 07:30:39 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
root, result := st.Commit(false)
|
|
|
|
if root != prev.Root {
|
|
|
|
return errors.New("failed to reset storage trie")
|
|
|
|
}
|
|
|
|
// The returned set can be nil if storage trie is not changed
|
|
|
|
// at all.
|
|
|
|
if result != nil {
|
|
|
|
if err := ctx.nodes.Merge(result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Write the prev-state account into the main trie
|
|
|
|
full, err := rlp.EncodeToBytes(prev)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ctx.accountTrie.Update(addrHash.Bytes(), full)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteAccount the account was not present in prev-state, and is expected
|
|
|
|
// to be existent in post-state. Apply the reverse diff and verify if the
|
|
|
|
// account and storage is wiped out correctly.
|
2024-10-18 10:06:31 -05:00
|
|
|
func deleteAccount(ctx *context, db database.NodeDatabase, addr common.Address) error {
|
2024-06-27 07:30:39 -05:00
|
|
|
// The account must be existent in post-state, load the account.
|
|
|
|
h := newHasher()
|
|
|
|
defer h.release()
|
|
|
|
|
|
|
|
addrHash := h.hash(addr.Bytes())
|
|
|
|
blob, err := ctx.accountTrie.Get(addrHash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(blob) == 0 {
|
|
|
|
return fmt.Errorf("account is non-existent %#x", addrHash)
|
|
|
|
}
|
|
|
|
var post types.StateAccount
|
|
|
|
if err := rlp.DecodeBytes(blob, &post); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
st, err := trie.New(trie.StorageTrieID(ctx.postRoot, addrHash, post.Root), db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for key, val := range ctx.storages[addr] {
|
|
|
|
if len(val) != 0 {
|
|
|
|
return errors.New("expect storage deletion")
|
|
|
|
}
|
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
|
|
|
tkey := key
|
|
|
|
if ctx.rawStorageKey {
|
|
|
|
tkey = h.hash(key.Bytes())
|
|
|
|
}
|
|
|
|
if err := st.Delete(tkey.Bytes()); err != nil {
|
2024-06-27 07:30:39 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
root, result := st.Commit(false)
|
|
|
|
if root != types.EmptyRootHash {
|
|
|
|
return errors.New("failed to clear storage trie")
|
|
|
|
}
|
|
|
|
// The returned set can be nil if storage trie is not changed
|
|
|
|
// at all.
|
|
|
|
if result != nil {
|
|
|
|
if err := ctx.nodes.Merge(result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Delete the post-state account from the main trie.
|
|
|
|
return ctx.accountTrie.Delete(addrHash.Bytes())
|
|
|
|
}
|