2022-05-23 05:26:22 -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 snapshot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2023-06-06 03:17:39 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2022-05-23 05:26:22 -05:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckDanglingStorage iterates the snap storage data, and verifies that all
|
|
|
|
// storage also has corresponding account data.
|
|
|
|
func CheckDanglingStorage(chaindb ethdb.KeyValueStore) error {
|
|
|
|
if err := checkDanglingDiskStorage(chaindb); err != nil {
|
2022-06-06 10:09:39 -05:00
|
|
|
log.Error("Database check error", "err", err)
|
2022-05-23 05:26:22 -05:00
|
|
|
}
|
|
|
|
return checkDanglingMemStorage(chaindb)
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkDanglingDiskStorage checks if there is any 'dangling' storage data in the
|
|
|
|
// disk-backed snapshot layer.
|
|
|
|
func checkDanglingDiskStorage(chaindb ethdb.KeyValueStore) error {
|
|
|
|
var (
|
|
|
|
lastReport = time.Now()
|
|
|
|
start = time.Now()
|
|
|
|
lastKey []byte
|
|
|
|
it = rawdb.NewKeyLengthIterator(chaindb.NewIterator(rawdb.SnapshotStoragePrefix, nil), 1+2*common.HashLength)
|
|
|
|
)
|
|
|
|
log.Info("Checking dangling snapshot disk storage")
|
|
|
|
|
|
|
|
defer it.Release()
|
|
|
|
for it.Next() {
|
|
|
|
k := it.Key()
|
|
|
|
accKey := k[1:33]
|
|
|
|
if bytes.Equal(accKey, lastKey) {
|
|
|
|
// No need to look up for every slot
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lastKey = common.CopyBytes(accKey)
|
|
|
|
if time.Since(lastReport) > time.Second*8 {
|
|
|
|
log.Info("Iterating snap storage", "at", fmt.Sprintf("%#x", accKey), "elapsed", common.PrettyDuration(time.Since(start)))
|
|
|
|
lastReport = time.Now()
|
|
|
|
}
|
|
|
|
if data := rawdb.ReadAccountSnapshot(chaindb, common.BytesToHash(accKey)); len(data) == 0 {
|
|
|
|
log.Warn("Dangling storage - missing account", "account", fmt.Sprintf("%#x", accKey), "storagekey", fmt.Sprintf("%#x", k))
|
|
|
|
return fmt.Errorf("dangling snapshot storage account %#x", accKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Info("Verified the snapshot disk storage", "time", common.PrettyDuration(time.Since(start)), "err", it.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkDanglingMemStorage checks if there is any 'dangling' storage in the journalled
|
|
|
|
// snapshot difflayers.
|
|
|
|
func checkDanglingMemStorage(db ethdb.KeyValueStore) error {
|
2022-06-06 10:09:39 -05:00
|
|
|
start := time.Now()
|
|
|
|
log.Info("Checking dangling journalled storage")
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
err := iterateJournal(db, func(pRoot, root common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) error {
|
2022-06-06 10:09:39 -05:00
|
|
|
for accHash := range storage {
|
|
|
|
if _, ok := accounts[accHash]; !ok {
|
|
|
|
log.Error("Dangling storage - missing account", "account", fmt.Sprintf("%#x", accHash), "root", root)
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 05:26:22 -05:00
|
|
|
return nil
|
2022-06-06 10:09:39 -05:00
|
|
|
})
|
2022-05-23 05:26:22 -05:00
|
|
|
if err != nil {
|
2022-06-06 10:09:39 -05:00
|
|
|
log.Info("Failed to resolve snapshot journal", "err", err)
|
2022-05-23 05:26:22 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Info("Verified the snapshot journalled storage", "time", common.PrettyDuration(time.Since(start)))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 10:09:39 -05:00
|
|
|
// CheckJournalAccount shows information about an account, from the disk layer and
|
|
|
|
// up through the diff layers.
|
|
|
|
func CheckJournalAccount(db ethdb.KeyValueStore, hash common.Hash) error {
|
|
|
|
// Look up the disk layer first
|
|
|
|
baseRoot := rawdb.ReadSnapshotRoot(db)
|
|
|
|
fmt.Printf("Disklayer: Root: %x\n", baseRoot)
|
|
|
|
if data := rawdb.ReadAccountSnapshot(db, hash); data != nil {
|
2023-06-06 03:17:39 -05:00
|
|
|
account, err := types.FullAccount(data)
|
|
|
|
if err != nil {
|
2022-06-06 10:09:39 -05:00
|
|
|
panic(err)
|
2022-05-23 05:26:22 -05:00
|
|
|
}
|
2022-06-06 10:09:39 -05:00
|
|
|
fmt.Printf("\taccount.nonce: %d\n", account.Nonce)
|
|
|
|
fmt.Printf("\taccount.balance: %x\n", account.Balance)
|
|
|
|
fmt.Printf("\taccount.root: %x\n", account.Root)
|
|
|
|
fmt.Printf("\taccount.codehash: %x\n", account.CodeHash)
|
|
|
|
}
|
|
|
|
// Check storage
|
|
|
|
{
|
|
|
|
it := rawdb.NewKeyLengthIterator(db.NewIterator(append(rawdb.SnapshotStoragePrefix, hash.Bytes()...), nil), 1+2*common.HashLength)
|
|
|
|
fmt.Printf("\tStorage:\n")
|
|
|
|
for it.Next() {
|
|
|
|
slot := it.Key()[33:]
|
|
|
|
fmt.Printf("\t\t%x: %x\n", slot, it.Value())
|
2022-05-23 05:26:22 -05:00
|
|
|
}
|
2022-06-06 10:09:39 -05:00
|
|
|
it.Release()
|
|
|
|
}
|
|
|
|
var depth = 0
|
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
return iterateJournal(db, func(pRoot, root common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) error {
|
2022-06-06 10:09:39 -05:00
|
|
|
_, a := accounts[hash]
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
_, b := storage[hash]
|
2022-06-06 10:09:39 -05:00
|
|
|
depth++
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
if !a && !b {
|
2022-06-06 10:09:39 -05:00
|
|
|
return nil
|
2022-05-23 05:26:22 -05:00
|
|
|
}
|
2022-06-06 10:09:39 -05:00
|
|
|
fmt.Printf("Disklayer+%d: Root: %x, parent %x\n", depth, root, pRoot)
|
|
|
|
if data, ok := accounts[hash]; ok {
|
2023-06-06 03:17:39 -05:00
|
|
|
account, err := types.FullAccount(data)
|
|
|
|
if err != nil {
|
2022-06-06 10:09:39 -05:00
|
|
|
panic(err)
|
2022-05-23 05:26:22 -05:00
|
|
|
}
|
2022-06-06 10:09:39 -05:00
|
|
|
fmt.Printf("\taccount.nonce: %d\n", account.Nonce)
|
|
|
|
fmt.Printf("\taccount.balance: %x\n", account.Balance)
|
|
|
|
fmt.Printf("\taccount.root: %x\n", account.Root)
|
|
|
|
fmt.Printf("\taccount.codehash: %x\n", account.CodeHash)
|
2022-05-23 05:26:22 -05:00
|
|
|
}
|
2022-06-06 10:09:39 -05:00
|
|
|
if data, ok := storage[hash]; ok {
|
|
|
|
fmt.Printf("\tStorage\n")
|
|
|
|
for k, v := range data {
|
|
|
|
fmt.Printf("\t\t%x: %x\n", k, v)
|
2022-05-23 05:26:22 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-06 10:09:39 -05:00
|
|
|
return nil
|
|
|
|
})
|
2022-05-23 05:26:22 -05:00
|
|
|
}
|