2021-01-25 07:36:39 -06:00
|
|
|
// Copyright 2021 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 eth
|
|
|
|
|
|
|
|
import (
|
2022-12-15 06:52:07 -06:00
|
|
|
"context"
|
2021-01-25 07:36:39 -06:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2021-01-25 07:36:39 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2022-09-07 13:25:58 -05:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
2021-01-25 07:36:39 -06:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
)
|
|
|
|
|
2022-09-07 13:25:58 -05:00
|
|
|
// noopReleaser is returned in case there is no operation expected
|
|
|
|
// for releasing state.
|
|
|
|
var noopReleaser = tracers.StateReleaseFunc(func() {})
|
|
|
|
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (statedb *state.StateDB, release tracers.StateReleaseFunc, err error) {
|
2021-04-07 02:30:26 -05:00
|
|
|
var (
|
|
|
|
current *types.Block
|
|
|
|
database state.Database
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
triedb *trie.Database
|
2021-04-07 02:30:26 -05:00
|
|
|
report = true
|
|
|
|
origin = block.NumberU64()
|
|
|
|
)
|
2022-09-07 13:25:58 -05:00
|
|
|
// The state is only for reading purposes, check the state presence in
|
|
|
|
// live database.
|
|
|
|
if readOnly {
|
|
|
|
// The state is available in live database, create a reference
|
|
|
|
// on top to prevent garbage collection and return a release
|
|
|
|
// function to deref it.
|
|
|
|
if statedb, err = eth.blockchain.StateAt(block.Root()); err == nil {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
eth.blockchain.TrieDB().Reference(block.Root(), common.Hash{})
|
2022-09-07 13:25:58 -05:00
|
|
|
return statedb, func() {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
eth.blockchain.TrieDB().Dereference(block.Root())
|
2022-09-07 13:25:58 -05:00
|
|
|
}, nil
|
2021-04-07 02:30:26 -05:00
|
|
|
}
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
2022-09-07 13:25:58 -05:00
|
|
|
// The state is both for reading and writing, or it's unavailable in disk,
|
|
|
|
// try to construct/recover the state over an ephemeral trie.Database for
|
|
|
|
// isolating the live one.
|
2021-04-07 02:30:26 -05:00
|
|
|
if base != nil {
|
2021-11-04 12:54:00 -05:00
|
|
|
if preferDisk {
|
|
|
|
// Create an ephemeral trie.Database for isolating the live one. Otherwise
|
|
|
|
// the internal junks created by tracing will be persisted into the disk.
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
// TODO(rjl493456442), clean cache is disabled to prevent memory leak,
|
|
|
|
// please re-enable it for better performance.
|
|
|
|
database = state.NewDatabaseWithConfig(eth.chainDb, trie.HashDefaults)
|
2021-11-04 12:54:00 -05:00
|
|
|
if statedb, err = state.New(block.Root(), database, nil); err == nil {
|
|
|
|
log.Info("Found disk backend for state trie", "root", block.Root(), "number", block.Number())
|
2022-09-07 13:25:58 -05:00
|
|
|
return statedb, noopReleaser, nil
|
2021-11-04 12:54:00 -05:00
|
|
|
}
|
|
|
|
}
|
2021-04-07 02:30:26 -05:00
|
|
|
// The optional base statedb is given, mark the start point as parent block
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
statedb, database, triedb, report = base, base.Database(), base.Database().TrieDB(), false
|
2021-04-07 02:30:26 -05:00
|
|
|
current = eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
|
|
|
|
} else {
|
2022-09-07 13:25:58 -05:00
|
|
|
// Otherwise, try to reexec blocks until we find a state or reach our limit
|
2021-04-07 02:30:26 -05:00
|
|
|
current = block
|
2021-01-25 07:36:39 -06:00
|
|
|
|
2021-04-07 02:30:26 -05:00
|
|
|
// Create an ephemeral trie.Database for isolating the live one. Otherwise
|
|
|
|
// the internal junks created by tracing will be persisted into the disk.
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
// TODO(rjl493456442), clean cache is disabled to prevent memory leak,
|
|
|
|
// please re-enable it for better performance.
|
|
|
|
triedb = trie.NewDatabase(eth.chainDb, trie.HashDefaults)
|
|
|
|
database = state.NewDatabaseWithNodeDB(eth.chainDb, triedb)
|
2021-01-25 07:36:39 -06:00
|
|
|
|
2022-09-07 13:25:58 -05:00
|
|
|
// If we didn't check the live database, do check state over ephemeral database,
|
|
|
|
// otherwise we would rewind past a persisted block (specific corner case is
|
|
|
|
// chain tracing from the genesis).
|
|
|
|
if !readOnly {
|
2021-04-07 10:14:24 -05:00
|
|
|
statedb, err = state.New(current.Root(), database, nil)
|
|
|
|
if err == nil {
|
2022-09-07 13:25:58 -05:00
|
|
|
return statedb, noopReleaser, nil
|
2021-04-07 10:14:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Database does not have the state for the given block, try to regenerate
|
2021-04-07 02:30:26 -05:00
|
|
|
for i := uint64(0); i < reexec; i++ {
|
2022-12-15 06:52:07 -06:00
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-04-07 02:30:26 -05:00
|
|
|
if current.NumberU64() == 0 {
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, nil, errors.New("genesis state is missing")
|
2021-04-07 02:30:26 -05:00
|
|
|
}
|
|
|
|
parent := eth.blockchain.GetBlock(current.ParentHash(), current.NumberU64()-1)
|
|
|
|
if parent == nil {
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, nil, fmt.Errorf("missing block %v %d", current.ParentHash(), current.NumberU64()-1)
|
2021-04-07 02:30:26 -05:00
|
|
|
}
|
|
|
|
current = parent
|
|
|
|
|
|
|
|
statedb, err = state.New(current.Root(), database, nil)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
2021-04-07 02:30:26 -05:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case *trie.MissingNodeError:
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, nil, fmt.Errorf("required historical state unavailable (reexec=%d)", reexec)
|
2021-04-07 02:30:26 -05:00
|
|
|
default:
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, nil, err
|
2021-04-07 02:30:26 -05:00
|
|
|
}
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
|
|
|
}
|
2022-09-07 13:25:58 -05:00
|
|
|
// State is available at historical point, re-execute the blocks on top for
|
|
|
|
// the desired state.
|
2021-01-25 07:36:39 -06:00
|
|
|
var (
|
|
|
|
start = time.Now()
|
|
|
|
logged time.Time
|
|
|
|
parent common.Hash
|
|
|
|
)
|
2021-04-07 02:30:26 -05:00
|
|
|
for current.NumberU64() < origin {
|
2022-12-15 06:52:07 -06:00
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-01-25 07:36:39 -06:00
|
|
|
// Print progress logs if long enough time elapsed
|
2021-04-07 02:30:26 -05:00
|
|
|
if time.Since(logged) > 8*time.Second && report {
|
|
|
|
log.Info("Regenerating historical state", "block", current.NumberU64()+1, "target", origin, "remaining", origin-current.NumberU64()-1, "elapsed", time.Since(start))
|
2021-01-25 07:36:39 -06:00
|
|
|
logged = time.Now()
|
|
|
|
}
|
|
|
|
// Retrieve the next block to regenerate and process it
|
2021-04-07 02:30:26 -05:00
|
|
|
next := current.NumberU64() + 1
|
|
|
|
if current = eth.blockchain.GetBlockByNumber(next); current == nil {
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, nil, fmt.Errorf("block #%d not found", next)
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
2021-04-07 02:30:26 -05:00
|
|
|
_, _, _, err := eth.blockchain.Processor().Process(current, statedb, vm.Config{})
|
2021-01-25 07:36:39 -06:00
|
|
|
if err != nil {
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, nil, fmt.Errorf("processing block %d failed: %v", current.NumberU64(), err)
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
|
|
|
// Finalize the state so any modifications are written to the trie
|
2023-07-24 05:22:09 -05:00
|
|
|
root, err := statedb.Commit(current.NumberU64(), eth.blockchain.Config().IsEIP158(current.Number()))
|
2021-01-25 07:36:39 -06:00
|
|
|
if err != nil {
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, nil, fmt.Errorf("stateAtBlock commit failed, number %d root %v: %w",
|
2021-09-28 06:06:19 -05:00
|
|
|
current.NumberU64(), current.Root().Hex(), err)
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
|
|
|
statedb, err = state.New(root, database, nil)
|
|
|
|
if err != nil {
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, nil, fmt.Errorf("state reset after block %d failed: %v", current.NumberU64(), err)
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
2022-09-07 13:25:58 -05:00
|
|
|
// Hold the state reference and also drop the parent state
|
|
|
|
// to prevent accumulating too many nodes in memory.
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
triedb.Reference(root, common.Hash{})
|
2021-01-25 07:36:39 -06:00
|
|
|
if parent != (common.Hash{}) {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
triedb.Dereference(parent)
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
|
|
|
parent = root
|
|
|
|
}
|
2021-04-07 02:30:26 -05:00
|
|
|
if report {
|
2023-08-23 06:08:39 -05:00
|
|
|
_, nodes, imgs := triedb.Size() // all memory is contained within the nodes return in hashdb
|
2021-04-07 02:30:26 -05:00
|
|
|
log.Info("Historical state regenerated", "block", current.NumberU64(), "elapsed", time.Since(start), "nodes", nodes, "preimages", imgs)
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
return statedb, func() { triedb.Dereference(block.Root()) }, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *Ethereum) pathState(block *types.Block) (*state.StateDB, func(), error) {
|
|
|
|
// Check if the requested state is available in the live chain.
|
|
|
|
statedb, err := eth.blockchain.StateAt(block.Root())
|
|
|
|
if err == nil {
|
|
|
|
return statedb, noopReleaser, nil
|
|
|
|
}
|
|
|
|
// TODO historic state is not supported in path-based scheme.
|
|
|
|
// Fully archive node in pbss will be implemented by relying
|
|
|
|
// on state history, but needs more work on top.
|
|
|
|
return nil, nil, errors.New("historical state not available in path scheme yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
// stateAtBlock retrieves the state database associated with a certain block.
|
|
|
|
// If no state is locally available for the given block, a number of blocks
|
|
|
|
// are attempted to be reexecuted to generate the desired state. The optional
|
|
|
|
// base layer statedb can be provided which is regarded as the statedb of the
|
|
|
|
// parent block.
|
|
|
|
//
|
|
|
|
// An additional release function will be returned if the requested state is
|
|
|
|
// available. Release is expected to be invoked when the returned state is no
|
|
|
|
// longer needed. Its purpose is to prevent resource leaking. Though it can be
|
|
|
|
// noop in some cases.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - block: The block for which we want the state(state = block.Root)
|
|
|
|
// - reexec: The maximum number of blocks to reprocess trying to obtain the desired state
|
|
|
|
// - base: If the caller is tracing multiple blocks, the caller can provide the parent
|
|
|
|
// state continuously from the callsite.
|
|
|
|
// - readOnly: If true, then the live 'blockchain' state database is used. No mutation should
|
|
|
|
// be made from caller, e.g. perform Commit or other 'save-to-disk' changes.
|
|
|
|
// Otherwise, the trash generated by caller may be persisted permanently.
|
|
|
|
// - preferDisk: This arg can be used by the caller to signal that even though the 'base' is
|
|
|
|
// provided, it would be preferable to start from a fresh state, if we have it
|
|
|
|
// on disk.
|
|
|
|
func (eth *Ethereum) stateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (statedb *state.StateDB, release tracers.StateReleaseFunc, err error) {
|
|
|
|
if eth.blockchain.TrieDB().Scheme() == rawdb.HashScheme {
|
|
|
|
return eth.hashState(ctx, block, reexec, base, readOnly, preferDisk)
|
|
|
|
}
|
|
|
|
return eth.pathState(block)
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// stateAtTransaction returns the execution environment of a certain transaction.
|
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and
types.Message gets removed.
This is a breaking change to packages core and core/types. While we do
not promise API stability for package core, we do for core/types. An
exception can be made for types.Message, since it doesn't have any
purpose apart from invoking the state transition in package core.
types.Message was also marked deprecated by the same commit it
got added in, 4dca5d4db7 (November 2016).
The core.Message interface was added in December 2014, in commit
db494170dc, for the purpose of 'testing' state transitions. It's the
same change that made transaction struct fields private. Before that,
the state transition used *types.Transaction directly.
Over time, multiple implementations of the interface accrued across
different packages, since constructing a Message is required whenever
one wants to invoke the state transition. These implementations all
looked very similar, a struct with private fields exposing the fields
as accessor methods.
By changing Message into a struct with public fields we can remove all
these useless interface implementations. It will also hopefully
simplify future changes to the type with less updates to apply across
all of go-ethereum when a field is added to Message.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2023-03-09 07:19:12 -06:00
|
|
|
func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*core.Message, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) {
|
2021-01-25 07:36:39 -06:00
|
|
|
// Short circuit if it's genesis block.
|
|
|
|
if block.NumberU64() == 0 {
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, vm.BlockContext{}, nil, nil, errors.New("no transaction in genesis")
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
|
|
|
// Create the parent state database
|
|
|
|
parent := eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
|
|
|
|
if parent == nil {
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("parent %#x not found", block.ParentHash())
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
2021-04-07 02:30:26 -05:00
|
|
|
// Lookup the statedb of parent block from the live database,
|
|
|
|
// otherwise regenerate it on the flight.
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
statedb, release, err := eth.stateAtBlock(ctx, parent, reexec, nil, true, false)
|
2021-01-25 07:36:39 -06:00
|
|
|
if err != nil {
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, vm.BlockContext{}, nil, nil, err
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
|
|
|
if txIndex == 0 && len(block.Transactions()) == 0 {
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, vm.BlockContext{}, statedb, release, nil
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
|
|
|
// Recompute transactions up to the target index.
|
2023-04-21 04:52:02 -05:00
|
|
|
signer := types.MakeSigner(eth.blockchain.Config(), block.Number(), block.Time())
|
2021-01-25 07:36:39 -06:00
|
|
|
for idx, tx := range block.Transactions() {
|
|
|
|
// Assemble the transaction call message and return if the requested offset
|
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and
types.Message gets removed.
This is a breaking change to packages core and core/types. While we do
not promise API stability for package core, we do for core/types. An
exception can be made for types.Message, since it doesn't have any
purpose apart from invoking the state transition in package core.
types.Message was also marked deprecated by the same commit it
got added in, 4dca5d4db7 (November 2016).
The core.Message interface was added in December 2014, in commit
db494170dc, for the purpose of 'testing' state transitions. It's the
same change that made transaction struct fields private. Before that,
the state transition used *types.Transaction directly.
Over time, multiple implementations of the interface accrued across
different packages, since constructing a Message is required whenever
one wants to invoke the state transition. These implementations all
looked very similar, a struct with private fields exposing the fields
as accessor methods.
By changing Message into a struct with public fields we can remove all
these useless interface implementations. It will also hopefully
simplify future changes to the type with less updates to apply across
all of go-ethereum when a field is added to Message.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2023-03-09 07:19:12 -06:00
|
|
|
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
|
2021-01-25 07:36:39 -06:00
|
|
|
txContext := core.NewEVMTxContext(msg)
|
|
|
|
context := core.NewEVMBlockContext(block.Header(), eth.blockchain, nil)
|
|
|
|
if idx == txIndex {
|
2022-09-07 13:25:58 -05:00
|
|
|
return msg, context, statedb, release, nil
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
|
|
|
// Not yet the searched for transaction, execute on top of the current state
|
|
|
|
vmenv := vm.NewEVM(context, txContext, statedb, eth.blockchain.Config(), vm.Config{})
|
2022-11-16 03:18:52 -06:00
|
|
|
statedb.SetTxContext(tx.Hash(), idx)
|
2021-01-25 07:36:39 -06:00
|
|
|
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|
|
|
|
// Ensure any modifications are committed to the state
|
|
|
|
// Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
|
|
|
|
statedb.Finalise(vmenv.ChainConfig().IsEIP158(block.Number()))
|
|
|
|
}
|
2022-09-07 13:25:58 -05:00
|
|
|
return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash())
|
2021-01-25 07:36:39 -06:00
|
|
|
}
|