// Copyright 2025 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 . // Package state provides a caching layer atop the Ethereum state trie. package state import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" ) // Snapshot represents the functionality supported by a snapshot storage layer. type Snapshot interface { // Root returns the root hash for which this snapshot was made. Root() common.Hash // Account directly retrieves the account associated with a particular hash in // the snapshot slim data format. Account(hash common.Hash) (*types.SlimAccount, error) // AccountRLP directly retrieves the account RLP associated with a particular // hash in the snapshot slim data format. AccountRLP(hash common.Hash) ([]byte, error) // Storage directly retrieves the storage data associated with a particular hash, // within a particular account. Storage(accountHash, storageHash common.Hash) ([]byte, error) } // Tree is an Ethereum state snapshot tree. It consists of one persistent base // layer backed by a key-value store, on top of which arbitrarily many in-memory // diff layers are topped. The memory diffs can form a tree with branching, but // the disk layer is singleton and common to all. If a reorg goes deeper than the // disk layer, everything needs to be deleted. // // The goal of a state snapshot is twofold: to allow direct access to account and // storage data to avoid expensive multi-level trie lookups; and to allow sorted, // cheap iteration of the account/storage tries for sync aid. type SnapshotTree interface { // Snapshot retrieves a snapshot belonging to the given block root, or nil if no // snapshot is maintained for that block. Snapshot(blockRoot common.Hash) Snapshot // Update adds a new snapshot into the tree, if that can be linked to an existing // old parent. It is disallowed to insert a disk layer (the origin of all). Update(blockRoot common.Hash, parentRoot common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) error // Cap traverses downwards the snapshot tree from a head block hash until the // number of allowed layers are crossed. All layers beyond the permitted number // are flattened downwards. Cap(root common.Hash, layers int) error // StorageIterator creates a new storage iterator for the specified root hash and // account. The iterator will be move to the specific start position. StorageIterator(root common.Hash, account common.Hash, seek common.Hash) (StorageIterator, error) // AccountIterator creates a new account iterator for the specified root hash and // seeks to a starting account hash. AccountIterator(root common.Hash, seek common.Hash) (AccountIterator, error) } // Iterator is an iterator to step over all the accounts or the specific // storage in a snapshot which may or may not be composed of multiple layers. type Iterator interface { // Next steps the iterator forward one element, returning false if exhausted, // or an error if iteration failed for some reason (e.g. root being iterated // becomes stale and garbage collected). Next() bool // Error returns any failure that occurred during iteration, which might have // caused a premature iteration exit (e.g. snapshot stack becoming stale). Error() error // Hash returns the hash of the account or storage slot the iterator is // currently at. Hash() common.Hash // Release releases associated resources. Release should always succeed and // can be called multiple times without causing error. Release() } // AccountIterator is an iterator to step over all the accounts in a snapshot, // which may or may not be composed of multiple layers. type AccountIterator interface { Iterator // Account returns the RLP encoded slim account the iterator is currently at. // An error will be returned if the iterator becomes invalid Account() []byte } // StorageIterator is an iterator to step over the specific storage in a snapshot, // which may or may not be composed of multiple layers. type StorageIterator interface { Iterator // Slot returns the storage slot the iterator is currently at. An error will // be returned if the iterator becomes invalid Slot() []byte }