2021-01-25 13:36:39 +00: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 (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
)
|
|
|
|
|
|
|
|
// stateAtBlock retrieves the state database associated with a certain block.
|
2021-04-07 07:30:26 +00:00
|
|
|
// 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 passed then it's regarded as the statedb of the
|
|
|
|
// parent block.
|
|
|
|
func (eth *Ethereum) stateAtBlock(block *types.Block, reexec uint64, base *state.StateDB, checkLive bool) (statedb *state.StateDB, err error) {
|
|
|
|
var (
|
|
|
|
current *types.Block
|
|
|
|
database state.Database
|
|
|
|
report = true
|
|
|
|
origin = block.NumberU64()
|
|
|
|
)
|
|
|
|
// Check the live database first if we have the state fully available, use that.
|
|
|
|
if checkLive {
|
|
|
|
statedb, err = eth.blockchain.StateAt(block.Root())
|
|
|
|
if err == nil {
|
|
|
|
return statedb, nil
|
|
|
|
}
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
2021-04-07 07:30:26 +00:00
|
|
|
if base != nil {
|
|
|
|
// The optional base statedb is given, mark the start point as parent block
|
|
|
|
statedb, database, report = base, base.Database(), false
|
|
|
|
current = eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
|
|
|
|
} else {
|
|
|
|
// Otherwise try to reexec blocks until we find a state or reach our limit
|
|
|
|
current = block
|
2021-01-25 13:36:39 +00:00
|
|
|
|
2021-04-07 07:30:26 +00:00
|
|
|
// Create an ephemeral trie.Database for isolating the live one. Otherwise
|
|
|
|
// the internal junks created by tracing will be persisted into the disk.
|
|
|
|
database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16})
|
2021-01-25 13:36:39 +00:00
|
|
|
|
2021-04-07 07:30:26 +00:00
|
|
|
for i := uint64(0); i < reexec; i++ {
|
|
|
|
if current.NumberU64() == 0 {
|
|
|
|
return nil, errors.New("genesis state is missing")
|
|
|
|
}
|
|
|
|
parent := eth.blockchain.GetBlock(current.ParentHash(), current.NumberU64()-1)
|
|
|
|
if parent == nil {
|
|
|
|
return nil, fmt.Errorf("missing block %v %d", current.ParentHash(), current.NumberU64()-1)
|
|
|
|
}
|
|
|
|
current = parent
|
|
|
|
|
|
|
|
statedb, err = state.New(current.Root(), database, nil)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
2021-04-07 07:30:26 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case *trie.MissingNodeError:
|
|
|
|
return nil, fmt.Errorf("required historical state unavailable (reexec=%d)", reexec)
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// State was available at historical point, regenerate
|
|
|
|
var (
|
|
|
|
start = time.Now()
|
|
|
|
logged time.Time
|
|
|
|
parent common.Hash
|
|
|
|
)
|
2021-04-07 07:30:26 +00:00
|
|
|
for current.NumberU64() < origin {
|
2021-01-25 13:36:39 +00:00
|
|
|
// Print progress logs if long enough time elapsed
|
2021-04-07 07:30:26 +00: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 13:36:39 +00:00
|
|
|
logged = time.Now()
|
|
|
|
}
|
|
|
|
// Retrieve the next block to regenerate and process it
|
2021-04-07 07:30:26 +00:00
|
|
|
next := current.NumberU64() + 1
|
|
|
|
if current = eth.blockchain.GetBlockByNumber(next); current == nil {
|
|
|
|
return nil, fmt.Errorf("block #%d not found", next)
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
2021-04-07 07:30:26 +00:00
|
|
|
_, _, _, err := eth.blockchain.Processor().Process(current, statedb, vm.Config{})
|
2021-01-25 13:36:39 +00:00
|
|
|
if err != nil {
|
2021-04-07 07:30:26 +00:00
|
|
|
return nil, fmt.Errorf("processing block %d failed: %v", current.NumberU64(), err)
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
|
|
|
// Finalize the state so any modifications are written to the trie
|
2021-04-07 07:30:26 +00:00
|
|
|
root, err := statedb.Commit(eth.blockchain.Config().IsEIP158(current.Number()))
|
2021-01-25 13:36:39 +00:00
|
|
|
if err != nil {
|
2021-04-07 07:30:26 +00:00
|
|
|
return nil, err
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
|
|
|
statedb, err = state.New(root, database, nil)
|
|
|
|
if err != nil {
|
2021-04-07 07:30:26 +00:00
|
|
|
return nil, fmt.Errorf("state reset after block %d failed: %v", current.NumberU64(), err)
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
|
|
|
database.TrieDB().Reference(root, common.Hash{})
|
|
|
|
if parent != (common.Hash{}) {
|
|
|
|
database.TrieDB().Dereference(parent)
|
|
|
|
}
|
|
|
|
parent = root
|
|
|
|
}
|
2021-04-07 07:30:26 +00:00
|
|
|
if report {
|
|
|
|
nodes, imgs := database.TrieDB().Size()
|
|
|
|
log.Info("Historical state regenerated", "block", current.NumberU64(), "elapsed", time.Since(start), "nodes", nodes, "preimages", imgs)
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
2021-04-07 07:30:26 +00:00
|
|
|
return statedb, nil
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stateAtTransaction returns the execution environment of a certain transaction.
|
2021-04-07 07:30:26 +00:00
|
|
|
func (eth *Ethereum) stateAtTransaction(block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) {
|
2021-01-25 13:36:39 +00:00
|
|
|
// Short circuit if it's genesis block.
|
|
|
|
if block.NumberU64() == 0 {
|
2021-04-07 07:30:26 +00:00
|
|
|
return nil, vm.BlockContext{}, nil, errors.New("no transaction in genesis")
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
|
|
|
// Create the parent state database
|
|
|
|
parent := eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
|
|
|
|
if parent == nil {
|
2021-04-07 07:30:26 +00:00
|
|
|
return nil, vm.BlockContext{}, nil, fmt.Errorf("parent %#x not found", block.ParentHash())
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
2021-04-07 07:30:26 +00:00
|
|
|
// Lookup the statedb of parent block from the live database,
|
|
|
|
// otherwise regenerate it on the flight.
|
|
|
|
statedb, err := eth.stateAtBlock(parent, reexec, nil, true)
|
2021-01-25 13:36:39 +00:00
|
|
|
if err != nil {
|
2021-04-07 07:30:26 +00:00
|
|
|
return nil, vm.BlockContext{}, nil, err
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
|
|
|
if txIndex == 0 && len(block.Transactions()) == 0 {
|
2021-04-07 07:30:26 +00:00
|
|
|
return nil, vm.BlockContext{}, statedb, nil
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|
|
|
|
// Recompute transactions up to the target index.
|
|
|
|
signer := types.MakeSigner(eth.blockchain.Config(), block.Number())
|
|
|
|
for idx, tx := range block.Transactions() {
|
|
|
|
// Assemble the transaction call message and return if the requested offset
|
|
|
|
msg, _ := tx.AsMessage(signer)
|
|
|
|
txContext := core.NewEVMTxContext(msg)
|
|
|
|
context := core.NewEVMBlockContext(block.Header(), eth.blockchain, nil)
|
|
|
|
if idx == txIndex {
|
2021-04-07 07:30:26 +00:00
|
|
|
return msg, context, statedb, nil
|
2021-01-25 13:36:39 +00: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{})
|
2021-03-14 15:13:25 +00:00
|
|
|
statedb.Prepare(tx.Hash(), block.Hash(), idx)
|
2021-01-25 13:36:39 +00:00
|
|
|
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
|
2021-04-07 07:30:26 +00:00
|
|
|
return nil, vm.BlockContext{}, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
|
2021-01-25 13:36:39 +00: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()))
|
|
|
|
}
|
2021-04-07 07:30:26 +00:00
|
|
|
return nil, vm.BlockContext{}, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash())
|
2021-01-25 13:36:39 +00:00
|
|
|
}
|