2019-12-05 07:37:25 -06:00
|
|
|
// Copyright 2019 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"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2020-04-29 04:53:08 -05:00
|
|
|
// binaryIterator is a simplistic iterator to step over the accounts or storage
|
|
|
|
// in a snapshot, which may or may not be composed of multiple layers. Performance
|
2019-12-05 07:37:25 -06:00
|
|
|
// wise this iterator is slow, it's meant for cross validating the fast one,
|
2020-04-29 04:53:08 -05:00
|
|
|
type binaryIterator struct {
|
|
|
|
a Iterator
|
|
|
|
b Iterator
|
|
|
|
aDone bool
|
|
|
|
bDone bool
|
|
|
|
accountIterator bool
|
|
|
|
k common.Hash
|
|
|
|
account common.Hash
|
|
|
|
fail error
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
|
2020-04-29 04:53:08 -05:00
|
|
|
// initBinaryAccountIterator creates a simplistic iterator to step over all the
|
2021-01-07 00:36:21 -06:00
|
|
|
// accounts in a slow, but easily verifiable way. Note this function is used for
|
2020-04-29 04:53:08 -05:00
|
|
|
// initialization, use `newBinaryAccountIterator` as the API.
|
2024-11-15 00:59:06 -06:00
|
|
|
func (dl *diffLayer) initBinaryAccountIterator(seek common.Hash) Iterator {
|
2020-04-29 04:53:08 -05:00
|
|
|
parent, ok := dl.parent.(*diffLayer)
|
|
|
|
if !ok {
|
|
|
|
l := &binaryIterator{
|
2024-11-15 00:59:06 -06:00
|
|
|
a: dl.AccountIterator(seek),
|
|
|
|
b: dl.Parent().AccountIterator(seek),
|
2020-04-29 04:53:08 -05:00
|
|
|
accountIterator: true,
|
|
|
|
}
|
|
|
|
l.aDone = !l.a.Next()
|
|
|
|
l.bDone = !l.b.Next()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
l := &binaryIterator{
|
2024-11-15 00:59:06 -06:00
|
|
|
a: dl.AccountIterator(seek),
|
|
|
|
b: parent.initBinaryAccountIterator(seek),
|
2020-04-29 04:53:08 -05:00
|
|
|
accountIterator: true,
|
|
|
|
}
|
|
|
|
l.aDone = !l.a.Next()
|
|
|
|
l.bDone = !l.b.Next()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// initBinaryStorageIterator creates a simplistic iterator to step over all the
|
2021-01-07 00:36:21 -06:00
|
|
|
// storage slots in a slow, but easily verifiable way. Note this function is used
|
2020-04-29 04:53:08 -05:00
|
|
|
// for initialization, use `newBinaryStorageIterator` as the API.
|
2024-11-15 00:59:06 -06:00
|
|
|
func (dl *diffLayer) initBinaryStorageIterator(account, seek common.Hash) Iterator {
|
2019-12-05 07:37:25 -06:00
|
|
|
parent, ok := dl.parent.(*diffLayer)
|
|
|
|
if !ok {
|
2020-04-29 04:53:08 -05:00
|
|
|
l := &binaryIterator{
|
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
|
|
|
a: dl.StorageIterator(account, seek),
|
|
|
|
b: dl.Parent().StorageIterator(account, seek),
|
2020-04-29 04:53:08 -05:00
|
|
|
account: account,
|
2020-04-24 06:43:49 -05:00
|
|
|
}
|
|
|
|
l.aDone = !l.a.Next()
|
|
|
|
l.bDone = !l.b.Next()
|
|
|
|
return l
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
2020-04-29 04:53:08 -05:00
|
|
|
l := &binaryIterator{
|
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
|
|
|
a: dl.StorageIterator(account, seek),
|
2024-11-15 00:59:06 -06:00
|
|
|
b: parent.initBinaryStorageIterator(account, seek),
|
2020-04-29 04:53:08 -05:00
|
|
|
account: account,
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
l.aDone = !l.a.Next()
|
|
|
|
l.bDone = !l.b.Next()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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).
|
2020-04-29 04:53:08 -05:00
|
|
|
func (it *binaryIterator) Next() bool {
|
2024-11-15 00:59:06 -06:00
|
|
|
for {
|
|
|
|
if !it.next() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(it.Account()) != 0 || len(it.Slot()) != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// it.fail might be set if error occurs by calling
|
|
|
|
// it.Account() or it.Slot(), stop iteration if so.
|
|
|
|
if it.fail != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *binaryIterator) next() bool {
|
2019-12-05 07:37:25 -06:00
|
|
|
if it.aDone && it.bDone {
|
|
|
|
return false
|
|
|
|
}
|
2024-11-15 00:59:06 -06:00
|
|
|
for {
|
|
|
|
if it.aDone {
|
|
|
|
it.k = it.b.Hash()
|
|
|
|
it.bDone = !it.b.Next()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if it.bDone {
|
|
|
|
it.k = it.a.Hash()
|
|
|
|
it.aDone = !it.a.Next()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
nextA, nextB := it.a.Hash(), it.b.Hash()
|
|
|
|
if diff := bytes.Compare(nextA[:], nextB[:]); diff < 0 {
|
|
|
|
it.aDone = !it.a.Next()
|
|
|
|
it.k = nextA
|
|
|
|
return true
|
|
|
|
} else if diff == 0 {
|
|
|
|
// Now we need to advance one of them
|
|
|
|
it.aDone = !it.a.Next()
|
|
|
|
continue
|
|
|
|
}
|
2019-12-05 07:37:25 -06:00
|
|
|
it.bDone = !it.b.Next()
|
2024-11-15 00:59:06 -06:00
|
|
|
it.k = nextB
|
2019-12-05 07:37:25 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns any failure that occurred during iteration, which might have
|
|
|
|
// caused a premature iteration exit (e.g. snapshot stack becoming stale).
|
2020-04-29 04:53:08 -05:00
|
|
|
func (it *binaryIterator) Error() error {
|
2019-12-05 07:37:25 -06:00
|
|
|
return it.fail
|
|
|
|
}
|
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
// Hash returns the hash of the account the iterator is currently at.
|
2020-04-29 04:53:08 -05:00
|
|
|
func (it *binaryIterator) Hash() common.Hash {
|
2019-12-05 07:37:25 -06:00
|
|
|
return it.k
|
|
|
|
}
|
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
// Account returns the RLP encoded slim account the iterator is currently at, or
|
2019-12-05 07:37:25 -06:00
|
|
|
// nil if the iterated snapshot stack became stale (you can check Error after
|
|
|
|
// to see if it failed or not).
|
2020-04-29 04:53:08 -05:00
|
|
|
//
|
|
|
|
// Note the returned account is not a copy, please don't modify it.
|
|
|
|
func (it *binaryIterator) Account() []byte {
|
|
|
|
if !it.accountIterator {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-24 06:43:49 -05:00
|
|
|
// The topmost iterator must be `diffAccountIterator`
|
|
|
|
blob, err := it.a.(*diffAccountIterator).layer.AccountRLP(it.k)
|
2019-12-05 07:37:25 -06:00
|
|
|
if err != nil {
|
|
|
|
it.fail = err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return blob
|
|
|
|
}
|
2019-12-10 03:00:03 -06:00
|
|
|
|
2020-04-29 04:53:08 -05:00
|
|
|
// Slot returns the raw storage slot data the iterator is currently at, or
|
|
|
|
// nil if the iterated snapshot stack became stale (you can check Error after
|
|
|
|
// to see if it failed or not).
|
|
|
|
//
|
|
|
|
// Note the returned slot is not a copy, please don't modify it.
|
|
|
|
func (it *binaryIterator) Slot() []byte {
|
|
|
|
if it.accountIterator {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
blob, err := it.a.(*diffStorageIterator).layer.Storage(it.account, it.k)
|
|
|
|
if err != nil {
|
|
|
|
it.fail = err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return blob
|
|
|
|
}
|
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
// Release recursively releases all the iterators in the stack.
|
2020-04-29 04:53:08 -05:00
|
|
|
func (it *binaryIterator) Release() {
|
2019-12-10 03:00:03 -06:00
|
|
|
it.a.Release()
|
2024-11-15 00:59:06 -06:00
|
|
|
if it.b != nil {
|
|
|
|
it.b.Release()
|
|
|
|
}
|
2019-12-10 03:00:03 -06:00
|
|
|
}
|
2020-04-29 04:53:08 -05:00
|
|
|
|
|
|
|
// newBinaryAccountIterator creates a simplistic account iterator to step over
|
2021-01-07 00:36:21 -06:00
|
|
|
// all the accounts in a slow, but easily verifiable way.
|
2024-11-15 00:59:06 -06:00
|
|
|
func (dl *diffLayer) newBinaryAccountIterator(seek common.Hash) AccountIterator {
|
|
|
|
iter := dl.initBinaryAccountIterator(seek)
|
2020-04-29 04:53:08 -05:00
|
|
|
return iter.(AccountIterator)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newBinaryStorageIterator creates a simplistic account iterator to step over
|
2021-01-07 00:36:21 -06:00
|
|
|
// all the storage slots in a slow, but easily verifiable way.
|
2024-11-15 00:59:06 -06:00
|
|
|
func (dl *diffLayer) newBinaryStorageIterator(account, seek common.Hash) StorageIterator {
|
|
|
|
iter := dl.initBinaryStorageIterator(account, seek)
|
2020-04-29 04:53:08 -05:00
|
|
|
return iter.(StorageIterator)
|
|
|
|
}
|