2015-10-15 14:07:19 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2016-04-14 16:18:24 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-10-15 14:07:19 +00:00
|
|
|
//
|
2016-04-14 16:18:24 +00:00
|
|
|
// 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
|
2015-10-15 14:07:19 +00:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2016-04-14 16:18:24 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-10-15 14:07:19 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-04-14 16:18:24 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
2015-10-15 14:07:19 +00:00
|
|
|
//
|
2016-04-14 16:18:24 +00:00
|
|
|
// 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/>.
|
2015-10-15 14:07:19 +00:00
|
|
|
|
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
2016-12-12 15:08:23 +00:00
|
|
|
"compress/gzip"
|
2017-03-22 17:20:33 +00:00
|
|
|
"context"
|
2018-05-07 11:35:06 +00:00
|
|
|
"errors"
|
2015-10-15 14:07:19 +00:00
|
|
|
"fmt"
|
2015-12-04 18:56:11 +00:00
|
|
|
"io"
|
2015-10-15 14:07:19 +00:00
|
|
|
"math/big"
|
2015-12-04 18:56:11 +00:00
|
|
|
"os"
|
2018-08-23 10:02:36 +00:00
|
|
|
"runtime"
|
2016-12-12 15:08:23 +00:00
|
|
|
"strings"
|
2018-08-21 19:56:54 +00:00
|
|
|
"time"
|
2015-10-15 14:07:19 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-12-17 14:39:55 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2015-10-15 14:07:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-05-07 11:35:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2015-10-15 14:07:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-06-11 08:03:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
2021-10-05 06:13:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2015-10-15 14:07:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2017-04-05 15:49:54 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2017-04-19 10:09:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2015-10-15 14:07:19 +00:00
|
|
|
)
|
|
|
|
|
2022-06-21 09:05:43 +00:00
|
|
|
// EthereumAPI provides an API to access Ethereum full node-related information.
|
|
|
|
type EthereumAPI struct {
|
2016-06-30 10:03:26 +00:00
|
|
|
e *Ethereum
|
2016-01-27 16:01:30 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 09:05:43 +00:00
|
|
|
// NewEthereumAPI creates a new Ethereum protocol API for full nodes.
|
|
|
|
func NewEthereumAPI(e *Ethereum) *EthereumAPI {
|
|
|
|
return &EthereumAPI{e}
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:29:23 +00:00
|
|
|
// Etherbase is the address that mining rewards will be send to.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *EthereumAPI) Etherbase() (common.Address, error) {
|
2017-04-04 22:16:29 +00:00
|
|
|
return api.e.Etherbase()
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:29:23 +00:00
|
|
|
// Coinbase is the address that mining rewards will be send to (alias for Etherbase).
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *EthereumAPI) Coinbase() (common.Address, error) {
|
2017-04-04 22:16:29 +00:00
|
|
|
return api.Etherbase()
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:29:23 +00:00
|
|
|
// Hashrate returns the POW hashrate.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *EthereumAPI) Hashrate() hexutil.Uint64 {
|
2021-04-29 09:02:30 +00:00
|
|
|
return hexutil.Uint64(api.e.Miner().Hashrate())
|
|
|
|
}
|
|
|
|
|
2016-02-09 14:03:04 +00:00
|
|
|
// Mining returns an indication if this node is currently mining.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *EthereumAPI) Mining() bool {
|
2017-04-04 22:16:29 +00:00
|
|
|
return api.e.IsMining()
|
2016-02-09 14:03:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 09:05:43 +00:00
|
|
|
// MinerAPI provides an API to control the miner.
|
|
|
|
type MinerAPI struct {
|
2016-06-30 10:03:26 +00:00
|
|
|
e *Ethereum
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 09:05:43 +00:00
|
|
|
// NewMinerAPI create a new MinerAPI instance.
|
|
|
|
func NewMinerAPI(e *Ethereum) *MinerAPI {
|
|
|
|
return &MinerAPI{e}
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 10:02:36 +00:00
|
|
|
// Start starts the miner with the given number of threads. If threads is nil,
|
|
|
|
// the number of workers started is equal to the number of logical CPUs that are
|
|
|
|
// usable by this process. If mining is already running, this method adjust the
|
|
|
|
// number of threads allowed to use and updates the minimum price required by the
|
|
|
|
// transaction pool.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *MinerAPI) Start(threads *int) error {
|
2017-04-07 14:22:06 +00:00
|
|
|
if threads == nil {
|
2018-08-23 10:02:36 +00:00
|
|
|
return api.e.StartMining(runtime.NumCPU())
|
2017-04-07 14:22:06 +00:00
|
|
|
}
|
2018-08-23 10:02:36 +00:00
|
|
|
return api.e.StartMining(*threads)
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 10:02:36 +00:00
|
|
|
// Stop terminates the miner, both at the consensus engine level as well as at
|
|
|
|
// the block creation level.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *MinerAPI) Stop() {
|
2017-04-04 22:16:29 +00:00
|
|
|
api.e.StopMining()
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetExtra sets the extra data string that is included when this miner mines a block.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *MinerAPI) SetExtra(extra string) (bool, error) {
|
2017-04-04 22:16:29 +00:00
|
|
|
if err := api.e.Miner().SetExtra([]byte(extra)); err != nil {
|
2015-10-15 14:07:19 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGasPrice sets the minimum accepted gas price for the miner.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *MinerAPI) SetGasPrice(gasPrice hexutil.Big) bool {
|
2017-05-29 07:21:34 +00:00
|
|
|
api.e.lock.Lock()
|
|
|
|
api.e.gasPrice = (*big.Int)(&gasPrice)
|
|
|
|
api.e.lock.Unlock()
|
|
|
|
|
2017-05-16 19:07:27 +00:00
|
|
|
api.e.txPool.SetGasPrice((*big.Int)(&gasPrice))
|
2015-10-15 14:07:19 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-07-06 08:35:39 +00:00
|
|
|
// SetGasLimit sets the gaslimit to target towards during mining.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *MinerAPI) SetGasLimit(gasLimit hexutil.Uint64) bool {
|
2021-07-06 08:35:39 +00:00
|
|
|
api.e.Miner().SetGasCeil(uint64(gasLimit))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-06-15 07:29:23 +00:00
|
|
|
// SetEtherbase sets the etherbase of the miner.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *MinerAPI) SetEtherbase(etherbase common.Address) bool {
|
2017-04-04 22:16:29 +00:00
|
|
|
api.e.SetEtherbase(etherbase)
|
2015-10-15 14:07:19 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-08-21 19:56:54 +00:00
|
|
|
// SetRecommitInterval updates the interval for miner sealing work recommitting.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *MinerAPI) SetRecommitInterval(interval int) {
|
2018-08-21 19:56:54 +00:00
|
|
|
api.e.Miner().SetRecommitInterval(time.Duration(interval) * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2022-06-21 09:05:43 +00:00
|
|
|
// AdminAPI is the collection of Ethereum full node related APIs for node
|
|
|
|
// administration.
|
|
|
|
type AdminAPI struct {
|
2016-06-30 10:03:26 +00:00
|
|
|
eth *Ethereum
|
2015-12-04 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 09:05:43 +00:00
|
|
|
// NewAdminAPI creates a new instance of AdminAPI.
|
|
|
|
func NewAdminAPI(eth *Ethereum) *AdminAPI {
|
|
|
|
return &AdminAPI{eth: eth}
|
2015-12-04 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 11:10:15 +00:00
|
|
|
// ExportChain exports the current blockchain into a local file,
|
2022-06-15 07:29:23 +00:00
|
|
|
// or a range of blocks if first and last are non-nil.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *AdminAPI) ExportChain(file string, first *uint64, last *uint64) (bool, error) {
|
2019-12-17 11:10:15 +00:00
|
|
|
if first == nil && last != nil {
|
|
|
|
return false, errors.New("last cannot be specified without first")
|
|
|
|
}
|
|
|
|
if first != nil && last == nil {
|
|
|
|
head := api.eth.BlockChain().CurrentHeader().Number.Uint64()
|
|
|
|
last = &head
|
|
|
|
}
|
2019-08-30 08:39:29 +00:00
|
|
|
if _, err := os.Stat(file); err == nil {
|
2021-11-22 01:53:16 +00:00
|
|
|
// File already exists. Allowing overwrite could be a DoS vector,
|
2022-07-29 16:29:01 +00:00
|
|
|
// since the 'file' may point to arbitrary paths on the drive.
|
2019-08-30 08:39:29 +00:00
|
|
|
return false, errors.New("location would overwrite an existing file")
|
|
|
|
}
|
2015-12-04 18:56:11 +00:00
|
|
|
// Make sure we can create the file to export into
|
|
|
|
out, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
2016-12-12 15:08:23 +00:00
|
|
|
var writer io.Writer = out
|
|
|
|
if strings.HasSuffix(file, ".gz") {
|
|
|
|
writer = gzip.NewWriter(writer)
|
|
|
|
defer writer.(*gzip.Writer).Close()
|
|
|
|
}
|
|
|
|
|
2015-12-04 18:56:11 +00:00
|
|
|
// Export the blockchain
|
2019-12-17 11:10:15 +00:00
|
|
|
if first != nil {
|
|
|
|
if err := api.eth.BlockChain().ExportN(writer, *first, *last); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
} else if err := api.eth.BlockChain().Export(writer); err != nil {
|
2015-12-04 18:56:11 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2015-12-16 09:58:01 +00:00
|
|
|
func hasAllBlocks(chain *core.BlockChain, bs []*types.Block) bool {
|
|
|
|
for _, b := range bs {
|
2017-09-09 16:03:07 +00:00
|
|
|
if !chain.HasBlock(b.Hash(), b.NumberU64()) {
|
2015-12-16 09:58:01 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-12-04 18:56:11 +00:00
|
|
|
// ImportChain imports a blockchain from a local file.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *AdminAPI) ImportChain(file string) (bool, error) {
|
2015-12-04 18:56:11 +00:00
|
|
|
// Make sure the can access the file to import
|
|
|
|
in, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
2016-12-12 15:08:23 +00:00
|
|
|
var reader io.Reader = in
|
|
|
|
if strings.HasSuffix(file, ".gz") {
|
|
|
|
if reader, err = gzip.NewReader(reader); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 18:56:11 +00:00
|
|
|
// Run actual the import in pre-configured batches
|
2016-12-12 15:08:23 +00:00
|
|
|
stream := rlp.NewStream(reader, 0)
|
2015-12-04 18:56:11 +00:00
|
|
|
|
|
|
|
blocks, index := make([]*types.Block, 0, 2500), 0
|
|
|
|
for batch := 0; ; batch++ {
|
|
|
|
// Load a batch of blocks from the input file
|
|
|
|
for len(blocks) < cap(blocks) {
|
|
|
|
block := new(types.Block)
|
|
|
|
if err := stream.Decode(block); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return false, fmt.Errorf("block %d: failed to parse: %v", index, err)
|
|
|
|
}
|
|
|
|
blocks = append(blocks, block)
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
if len(blocks) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2015-12-16 09:58:01 +00:00
|
|
|
|
|
|
|
if hasAllBlocks(api.eth.BlockChain(), blocks) {
|
|
|
|
blocks = blocks[:0]
|
|
|
|
continue
|
|
|
|
}
|
2015-12-04 18:56:11 +00:00
|
|
|
// Import the batch and reset the buffer
|
|
|
|
if _, err := api.eth.BlockChain().InsertChain(blocks); err != nil {
|
|
|
|
return false, fmt.Errorf("batch %d: failed to insert: %v", batch, err)
|
|
|
|
}
|
|
|
|
blocks = blocks[:0]
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-06-21 09:05:43 +00:00
|
|
|
// DebugAPI is the collection of Ethereum full node APIs for debugging the
|
|
|
|
// protocol.
|
|
|
|
type DebugAPI struct {
|
2016-06-30 10:03:26 +00:00
|
|
|
eth *Ethereum
|
2015-12-04 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 09:05:43 +00:00
|
|
|
// NewDebugAPI creates a new DebugAPI instance.
|
|
|
|
func NewDebugAPI(eth *Ethereum) *DebugAPI {
|
|
|
|
return &DebugAPI{eth: eth}
|
2015-12-04 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DumpBlock retrieves the entire state of the database at a given block.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *DebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error) {
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
opts := &state.DumpConfig{
|
|
|
|
OnlyWithAddresses: true,
|
|
|
|
Max: AccountRangeMaxResults, // Sanity limit over RPC
|
|
|
|
}
|
2017-04-05 15:49:54 +00:00
|
|
|
if blockNr == rpc.PendingBlockNumber {
|
|
|
|
// If we're dumping the pending state, we need to request
|
|
|
|
// both the pending block as well as the pending state from
|
|
|
|
// the miner and operate on those
|
|
|
|
_, stateDb := api.eth.miner.Pending()
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
return stateDb.RawDump(opts), nil
|
2017-04-05 15:49:54 +00:00
|
|
|
}
|
2023-03-02 06:29:15 +00:00
|
|
|
var header *types.Header
|
2017-04-05 15:49:54 +00:00
|
|
|
if blockNr == rpc.LatestBlockNumber {
|
2023-03-02 06:29:15 +00:00
|
|
|
header = api.eth.blockchain.CurrentBlock()
|
2022-05-18 14:30:42 +00:00
|
|
|
} else if blockNr == rpc.FinalizedBlockNumber {
|
2023-03-02 06:29:15 +00:00
|
|
|
header = api.eth.blockchain.CurrentFinalBlock()
|
2022-07-25 15:42:05 +00:00
|
|
|
} else if blockNr == rpc.SafeBlockNumber {
|
2023-03-02 06:29:15 +00:00
|
|
|
header = api.eth.blockchain.CurrentSafeBlock()
|
2017-04-05 15:49:54 +00:00
|
|
|
} else {
|
2023-03-02 06:29:15 +00:00
|
|
|
block := api.eth.blockchain.GetBlockByNumber(uint64(blockNr))
|
|
|
|
if block == nil {
|
|
|
|
return state.Dump{}, fmt.Errorf("block #%d not found", blockNr)
|
|
|
|
}
|
|
|
|
header = block.Header()
|
2017-04-05 15:49:54 +00:00
|
|
|
}
|
2023-03-02 06:29:15 +00:00
|
|
|
if header == nil {
|
2017-04-05 15:49:54 +00:00
|
|
|
return state.Dump{}, fmt.Errorf("block #%d not found", blockNr)
|
2015-12-04 18:56:11 +00:00
|
|
|
}
|
2023-03-02 06:29:15 +00:00
|
|
|
stateDb, err := api.eth.BlockChain().StateAt(header.Root)
|
2015-12-04 18:56:11 +00:00
|
|
|
if err != nil {
|
2016-09-22 19:04:58 +00:00
|
|
|
return state.Dump{}, err
|
2015-12-04 18:56:11 +00:00
|
|
|
}
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
return stateDb.RawDump(opts), nil
|
2015-12-04 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 11:19:50 +00:00
|
|
|
// Preimage is a debug API function that returns the preimage for a sha3 hash, if known.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *DebugAPI) Preimage(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) {
|
2018-05-07 11:35:06 +00:00
|
|
|
if preimage := rawdb.ReadPreimage(api.eth.ChainDb(), hash); preimage != nil {
|
|
|
|
return preimage, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("unknown preimage")
|
2017-01-17 11:19:50 +00:00
|
|
|
}
|
2017-02-13 20:44:06 +00:00
|
|
|
|
2018-06-11 08:03:40 +00:00
|
|
|
// BadBlockArgs represents the entries in the list returned when bad blocks are queried.
|
|
|
|
type BadBlockArgs struct {
|
|
|
|
Hash common.Hash `json:"hash"`
|
|
|
|
Block map[string]interface{} `json:"block"`
|
|
|
|
RLP string `json:"rlp"`
|
|
|
|
}
|
|
|
|
|
2018-06-14 10:14:52 +00:00
|
|
|
// GetBadBlocks returns a list of the last 'bad blocks' that the client has seen on the network
|
2022-06-15 07:29:23 +00:00
|
|
|
// and returns them as a JSON list of block hashes.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *DebugAPI) GetBadBlocks(ctx context.Context) ([]*BadBlockArgs, error) {
|
2021-01-10 11:54:15 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
blocks = rawdb.ReadAllBadBlocks(api.eth.chainDb)
|
|
|
|
results = make([]*BadBlockArgs, 0, len(blocks))
|
|
|
|
)
|
|
|
|
for _, block := range blocks {
|
|
|
|
var (
|
|
|
|
blockRlp string
|
|
|
|
blockJSON map[string]interface{}
|
|
|
|
)
|
2018-06-11 08:03:40 +00:00
|
|
|
if rlpBytes, err := rlp.EncodeToBytes(block); err != nil {
|
2021-01-10 11:54:15 +00:00
|
|
|
blockRlp = err.Error() // Hacky, but hey, it works
|
2018-06-11 08:03:40 +00:00
|
|
|
} else {
|
2022-07-04 08:03:32 +00:00
|
|
|
blockRlp = fmt.Sprintf("%#x", rlpBytes)
|
2018-06-11 08:03:40 +00:00
|
|
|
}
|
2021-10-07 13:51:14 +00:00
|
|
|
if blockJSON, err = ethapi.RPCMarshalBlock(block, true, true, api.eth.APIBackend.ChainConfig()); err != nil {
|
2021-01-10 11:54:15 +00:00
|
|
|
blockJSON = map[string]interface{}{"error": err.Error()}
|
2018-06-11 08:03:40 +00:00
|
|
|
}
|
2021-01-10 11:54:15 +00:00
|
|
|
results = append(results, &BadBlockArgs{
|
|
|
|
Hash: block.Hash(),
|
|
|
|
RLP: blockRlp,
|
|
|
|
Block: blockJSON,
|
|
|
|
})
|
2018-06-11 08:03:40 +00:00
|
|
|
}
|
|
|
|
return results, nil
|
2017-02-13 20:44:06 +00:00
|
|
|
}
|
2017-04-19 10:09:04 +00:00
|
|
|
|
2019-07-13 13:48:55 +00:00
|
|
|
// AccountRangeMaxResults is the maximum number of results to be returned per call
|
|
|
|
const AccountRangeMaxResults = 256
|
|
|
|
|
2020-08-26 10:20:12 +00:00
|
|
|
// AccountRange enumerates all accounts in the given block and start point in paging request
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *DebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, start hexutil.Bytes, maxResults int, nocode, nostorage, incompletes bool) (state.IteratorDump, error) {
|
2020-03-31 10:08:44 +00:00
|
|
|
var stateDb *state.StateDB
|
2019-07-13 13:48:55 +00:00
|
|
|
var err error
|
|
|
|
|
2020-03-31 10:08:44 +00:00
|
|
|
if number, ok := blockNrOrHash.Number(); ok {
|
|
|
|
if number == rpc.PendingBlockNumber {
|
|
|
|
// If we're dumping the pending state, we need to request
|
|
|
|
// both the pending block as well as the pending state from
|
|
|
|
// the miner and operate on those
|
|
|
|
_, stateDb = api.eth.miner.Pending()
|
|
|
|
} else {
|
2023-03-02 06:29:15 +00:00
|
|
|
var header *types.Header
|
2020-03-31 10:08:44 +00:00
|
|
|
if number == rpc.LatestBlockNumber {
|
2023-03-02 06:29:15 +00:00
|
|
|
header = api.eth.blockchain.CurrentBlock()
|
2022-05-18 14:30:42 +00:00
|
|
|
} else if number == rpc.FinalizedBlockNumber {
|
2023-03-02 06:29:15 +00:00
|
|
|
header = api.eth.blockchain.CurrentFinalBlock()
|
2022-07-25 15:42:05 +00:00
|
|
|
} else if number == rpc.SafeBlockNumber {
|
2023-03-02 06:29:15 +00:00
|
|
|
header = api.eth.blockchain.CurrentSafeBlock()
|
2020-03-31 10:08:44 +00:00
|
|
|
} else {
|
2023-03-02 06:29:15 +00:00
|
|
|
block := api.eth.blockchain.GetBlockByNumber(uint64(number))
|
|
|
|
if block == nil {
|
|
|
|
return state.IteratorDump{}, fmt.Errorf("block #%d not found", number)
|
|
|
|
}
|
|
|
|
header = block.Header()
|
2020-03-31 10:08:44 +00:00
|
|
|
}
|
2023-03-02 06:29:15 +00:00
|
|
|
if header == nil {
|
2020-03-31 10:08:44 +00:00
|
|
|
return state.IteratorDump{}, fmt.Errorf("block #%d not found", number)
|
|
|
|
}
|
2023-03-02 06:29:15 +00:00
|
|
|
stateDb, err = api.eth.BlockChain().StateAt(header.Root)
|
2020-03-31 10:08:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return state.IteratorDump{}, err
|
|
|
|
}
|
2019-07-13 13:48:55 +00:00
|
|
|
}
|
2020-03-31 10:08:44 +00:00
|
|
|
} else if hash, ok := blockNrOrHash.Hash(); ok {
|
|
|
|
block := api.eth.blockchain.GetBlockByHash(hash)
|
|
|
|
if block == nil {
|
|
|
|
return state.IteratorDump{}, fmt.Errorf("block %s not found", hash.Hex())
|
|
|
|
}
|
|
|
|
stateDb, err = api.eth.BlockChain().StateAt(block.Root())
|
2019-07-13 13:48:55 +00:00
|
|
|
if err != nil {
|
2020-03-31 10:08:44 +00:00
|
|
|
return state.IteratorDump{}, err
|
2019-07-13 13:48:55 +00:00
|
|
|
}
|
2020-10-20 18:19:21 +00:00
|
|
|
} else {
|
|
|
|
return state.IteratorDump{}, errors.New("either block number or block hash must be specified")
|
2019-07-13 13:48:55 +00:00
|
|
|
}
|
|
|
|
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
opts := &state.DumpConfig{
|
|
|
|
SkipCode: nocode,
|
|
|
|
SkipStorage: nostorage,
|
|
|
|
OnlyWithAddresses: !incompletes,
|
|
|
|
Start: start,
|
|
|
|
Max: uint64(maxResults),
|
|
|
|
}
|
2020-03-31 10:08:44 +00:00
|
|
|
if maxResults > AccountRangeMaxResults || maxResults <= 0 {
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
opts.Max = AccountRangeMaxResults
|
2019-07-13 13:48:55 +00:00
|
|
|
}
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
return stateDb.IteratorDump(opts), nil
|
2019-07-13 13:48:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 10:09:04 +00:00
|
|
|
// StorageRangeResult is the result of a debug_storageRangeAt API call.
|
|
|
|
type StorageRangeResult struct {
|
|
|
|
Storage storageMap `json:"storage"`
|
|
|
|
NextKey *common.Hash `json:"nextKey"` // nil if Storage includes the last key in the trie.
|
|
|
|
}
|
|
|
|
|
|
|
|
type storageMap map[common.Hash]storageEntry
|
|
|
|
|
|
|
|
type storageEntry struct {
|
|
|
|
Key *common.Hash `json:"key"`
|
|
|
|
Value common.Hash `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageRangeAt returns the storage at the given block height and transaction index.
|
2022-12-15 12:52:07 +00:00
|
|
|
func (api *DebugAPI) StorageRangeAt(ctx context.Context, blockHash common.Hash, txIndex int, contractAddress common.Address, keyStart hexutil.Bytes, maxResult int) (StorageRangeResult, error) {
|
2020-09-07 08:52:01 +00:00
|
|
|
// Retrieve the block
|
|
|
|
block := api.eth.blockchain.GetBlockByHash(blockHash)
|
|
|
|
if block == nil {
|
|
|
|
return StorageRangeResult{}, fmt.Errorf("block %#x not found", blockHash)
|
|
|
|
}
|
2022-12-15 12:52:07 +00:00
|
|
|
_, _, statedb, release, err := api.eth.stateAtTransaction(ctx, block, txIndex, 0)
|
2017-04-19 10:09:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return StorageRangeResult{}, err
|
|
|
|
}
|
2022-09-07 18:25:58 +00:00
|
|
|
defer release()
|
|
|
|
|
2022-12-21 09:21:21 +00:00
|
|
|
st, err := statedb.StorageTrie(contractAddress)
|
|
|
|
if err != nil {
|
|
|
|
return StorageRangeResult{}, err
|
|
|
|
}
|
2017-04-19 10:09:04 +00:00
|
|
|
if st == nil {
|
|
|
|
return StorageRangeResult{}, fmt.Errorf("account %x doesn't exist", contractAddress)
|
|
|
|
}
|
2017-12-06 15:42:16 +00:00
|
|
|
return storageRangeAt(st, keyStart, maxResult)
|
2017-04-19 10:09:04 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 15:42:16 +00:00
|
|
|
func storageRangeAt(st state.Trie, start []byte, maxResult int) (StorageRangeResult, error) {
|
2017-04-19 10:09:04 +00:00
|
|
|
it := trie.NewIterator(st.NodeIterator(start))
|
|
|
|
result := StorageRangeResult{Storage: storageMap{}}
|
|
|
|
for i := 0; i < maxResult && it.Next(); i++ {
|
2017-12-06 15:42:16 +00:00
|
|
|
_, content, _, err := rlp.Split(it.Value)
|
|
|
|
if err != nil {
|
|
|
|
return StorageRangeResult{}, err
|
|
|
|
}
|
|
|
|
e := storageEntry{Value: common.BytesToHash(content)}
|
2017-04-19 10:09:04 +00:00
|
|
|
if preimage := st.GetKey(it.Key); preimage != nil {
|
|
|
|
preimage := common.BytesToHash(preimage)
|
|
|
|
e.Key = &preimage
|
|
|
|
}
|
|
|
|
result.Storage[common.BytesToHash(it.Key)] = e
|
|
|
|
}
|
|
|
|
// Add the 'next key' so clients can continue downloading.
|
|
|
|
if it.Next() {
|
|
|
|
next := common.BytesToHash(it.Key)
|
|
|
|
result.NextKey = &next
|
|
|
|
}
|
2017-12-06 15:42:16 +00:00
|
|
|
return result, nil
|
2017-04-19 10:09:04 +00:00
|
|
|
}
|
2017-11-20 15:18:50 +00:00
|
|
|
|
2018-06-14 10:14:52 +00:00
|
|
|
// GetModifiedAccountsByNumber returns all accounts that have changed between the
|
2017-11-20 15:18:50 +00:00
|
|
|
// two blocks specified. A change is defined as a difference in nonce, balance,
|
|
|
|
// code hash, or storage hash.
|
|
|
|
//
|
|
|
|
// With one parameter, returns the list of accounts modified in the specified block.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *DebugAPI) GetModifiedAccountsByNumber(startNum uint64, endNum *uint64) ([]common.Address, error) {
|
2017-11-20 15:18:50 +00:00
|
|
|
var startBlock, endBlock *types.Block
|
|
|
|
|
|
|
|
startBlock = api.eth.blockchain.GetBlockByNumber(startNum)
|
|
|
|
if startBlock == nil {
|
|
|
|
return nil, fmt.Errorf("start block %x not found", startNum)
|
|
|
|
}
|
|
|
|
|
|
|
|
if endNum == nil {
|
|
|
|
endBlock = startBlock
|
|
|
|
startBlock = api.eth.blockchain.GetBlockByHash(startBlock.ParentHash())
|
|
|
|
if startBlock == nil {
|
|
|
|
return nil, fmt.Errorf("block %x has no parent", endBlock.Number())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
endBlock = api.eth.blockchain.GetBlockByNumber(*endNum)
|
|
|
|
if endBlock == nil {
|
|
|
|
return nil, fmt.Errorf("end block %d not found", *endNum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return api.getModifiedAccounts(startBlock, endBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetModifiedAccountsByHash returns all accounts that have changed between the
|
|
|
|
// two blocks specified. A change is defined as a difference in nonce, balance,
|
|
|
|
// code hash, or storage hash.
|
|
|
|
//
|
|
|
|
// With one parameter, returns the list of accounts modified in the specified block.
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *DebugAPI) GetModifiedAccountsByHash(startHash common.Hash, endHash *common.Hash) ([]common.Address, error) {
|
2017-11-20 15:18:50 +00:00
|
|
|
var startBlock, endBlock *types.Block
|
|
|
|
startBlock = api.eth.blockchain.GetBlockByHash(startHash)
|
|
|
|
if startBlock == nil {
|
|
|
|
return nil, fmt.Errorf("start block %x not found", startHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
if endHash == nil {
|
|
|
|
endBlock = startBlock
|
|
|
|
startBlock = api.eth.blockchain.GetBlockByHash(startBlock.ParentHash())
|
|
|
|
if startBlock == nil {
|
|
|
|
return nil, fmt.Errorf("block %x has no parent", endBlock.Number())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
endBlock = api.eth.blockchain.GetBlockByHash(*endHash)
|
|
|
|
if endBlock == nil {
|
|
|
|
return nil, fmt.Errorf("end block %x not found", *endHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return api.getModifiedAccounts(startBlock, endBlock)
|
|
|
|
}
|
|
|
|
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *DebugAPI) getModifiedAccounts(startBlock, endBlock *types.Block) ([]common.Address, error) {
|
2017-11-20 15:18:50 +00:00
|
|
|
if startBlock.Number().Uint64() >= endBlock.Number().Uint64() {
|
|
|
|
return nil, fmt.Errorf("start block height (%d) must be less than end block height (%d)", startBlock.Number().Uint64(), endBlock.Number().Uint64())
|
|
|
|
}
|
2018-11-12 16:47:34 +00:00
|
|
|
triedb := api.eth.BlockChain().StateCache().TrieDB()
|
2017-11-20 15:18:50 +00:00
|
|
|
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
oldTrie, err := trie.NewStateTrie(trie.StateTrieID(startBlock.Root()), triedb)
|
2017-11-20 15:18:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
newTrie, err := trie.NewStateTrie(trie.StateTrieID(endBlock.Root()), triedb)
|
2017-11-20 15:18:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
diff, _ := trie.NewDifferenceIterator(oldTrie.NodeIterator([]byte{}), newTrie.NodeIterator([]byte{}))
|
|
|
|
iter := trie.NewIterator(diff)
|
|
|
|
|
|
|
|
var dirty []common.Address
|
|
|
|
for iter.Next() {
|
|
|
|
key := newTrie.GetKey(iter.Key)
|
|
|
|
if key == nil {
|
|
|
|
return nil, fmt.Errorf("no preimage found for hash %x", iter.Key)
|
|
|
|
}
|
|
|
|
dirty = append(dirty, common.BytesToAddress(key))
|
|
|
|
}
|
|
|
|
return dirty, nil
|
|
|
|
}
|
2021-10-05 06:13:00 +00:00
|
|
|
|
|
|
|
// GetAccessibleState returns the first number where the node has accessible
|
|
|
|
// state on disk. Note this being the post-state of that block and the pre-state
|
|
|
|
// of the next block.
|
|
|
|
// The (from, to) parameters are the sequence of blocks to search, which can go
|
|
|
|
// either forwards or backwards
|
2022-06-21 09:05:43 +00:00
|
|
|
func (api *DebugAPI) GetAccessibleState(from, to rpc.BlockNumber) (uint64, error) {
|
2021-10-05 06:13:00 +00:00
|
|
|
db := api.eth.ChainDb()
|
|
|
|
var pivot uint64
|
|
|
|
if p := rawdb.ReadLastPivotNumber(db); p != nil {
|
|
|
|
pivot = *p
|
|
|
|
log.Info("Found fast-sync pivot marker", "number", pivot)
|
|
|
|
}
|
|
|
|
var resolveNum = func(num rpc.BlockNumber) (uint64, error) {
|
|
|
|
// We don't have state for pending (-2), so treat it as latest
|
|
|
|
if num.Int64() < 0 {
|
|
|
|
block := api.eth.blockchain.CurrentBlock()
|
|
|
|
if block == nil {
|
|
|
|
return 0, fmt.Errorf("current block missing")
|
|
|
|
}
|
2023-03-02 06:29:15 +00:00
|
|
|
return block.Number.Uint64(), nil
|
2021-10-05 06:13:00 +00:00
|
|
|
}
|
|
|
|
return uint64(num.Int64()), nil
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
start uint64
|
|
|
|
end uint64
|
|
|
|
delta = int64(1)
|
|
|
|
lastLog time.Time
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if start, err = resolveNum(from); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if end, err = resolveNum(to); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if start == end {
|
|
|
|
return 0, fmt.Errorf("from and to needs to be different")
|
|
|
|
}
|
|
|
|
if start > end {
|
|
|
|
delta = -1
|
|
|
|
}
|
|
|
|
for i := int64(start); i != int64(end); i += delta {
|
|
|
|
if time.Since(lastLog) > 8*time.Second {
|
|
|
|
log.Info("Finding roots", "from", start, "to", end, "at", i)
|
|
|
|
lastLog = time.Now()
|
|
|
|
}
|
|
|
|
if i < int64(pivot) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
h := api.eth.BlockChain().GetHeaderByNumber(uint64(i))
|
|
|
|
if h == nil {
|
|
|
|
return 0, fmt.Errorf("missing header %d", i)
|
|
|
|
}
|
|
|
|
if ok, _ := api.eth.ChainDb().Has(h.Root[:]); ok {
|
|
|
|
return uint64(i), nil
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 08:23:41 +00:00
|
|
|
return 0, errors.New("no state found")
|
2021-10-05 06:13:00 +00:00
|
|
|
}
|
core,eth: add `debug_setTrieFlushInterval` to change trie flush frequency (#24785)
This PR makes it possible to modify the flush interval time via RPC. On one extreme, `0s`, it would act as an archive node. If set to `1h`, means that after one hour of effective block processing time, the trie would be flushed. If one block takes 200ms, this means that a flush would occur every `5*3600=18000` blocks -- however, if the memory size of the cached states grows too large, it will flush sooner.
Essentially, this makes it possible to configure the node to be more or less "archive:ish", and without restarting the node while reconfiguring it.
2022-12-09 12:40:17 +00:00
|
|
|
|
|
|
|
// SetTrieFlushInterval configures how often in-memory tries are persisted
|
|
|
|
// to disk. The value is in terms of block processing time, not wall clock.
|
|
|
|
func (api *DebugAPI) SetTrieFlushInterval(interval string) error {
|
|
|
|
t, err := time.ParseDuration(interval)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
api.eth.blockchain.SetTrieFlushInterval(t)
|
|
|
|
return nil
|
|
|
|
}
|