mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +00:00
Drop Ethereum.chainConfig and consistently read chain configuration from blockchain.Config() to avoid dual sources of truth. Changes include API/backend call sites and DebugAPI constructor cleanup. No functional behavior change is intended.
328 lines
11 KiB
Go
328 lines
11 KiB
Go
// Copyright 2023 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 (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/XinFinOrg/XDPoSChain/common"
|
|
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
|
|
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
|
|
"github.com/XinFinOrg/XDPoSChain/core/state"
|
|
"github.com/XinFinOrg/XDPoSChain/core/types"
|
|
"github.com/XinFinOrg/XDPoSChain/internal/ethapi"
|
|
"github.com/XinFinOrg/XDPoSChain/rlp"
|
|
"github.com/XinFinOrg/XDPoSChain/rpc"
|
|
"github.com/XinFinOrg/XDPoSChain/trie"
|
|
)
|
|
|
|
// DebugAPI is the collection of Ethereum full node APIs exposed over
|
|
// the private debugging endpoint.
|
|
type DebugAPI struct {
|
|
eth *Ethereum
|
|
}
|
|
|
|
// NewDebugAPI creates a new API definition for the full node-related
|
|
// private debug methods of the Ethereum service.
|
|
func NewDebugAPI(eth *Ethereum) *DebugAPI {
|
|
return &DebugAPI{eth: eth}
|
|
}
|
|
|
|
// DumpBlock retrieves the entire state of the database at a given block.
|
|
func (api *DebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error) {
|
|
opts := &state.DumpConfig{
|
|
OnlyWithAddresses: true,
|
|
Max: AccountRangeMaxResults, // Sanity limit over RPC
|
|
}
|
|
if blockNr == rpc.PendingBlockNumber {
|
|
blockNr = rpc.LatestBlockNumber
|
|
}
|
|
var header *types.Header
|
|
if blockNr == rpc.LatestBlockNumber {
|
|
header = api.eth.blockchain.CurrentBlock()
|
|
} else {
|
|
block := api.eth.blockchain.GetBlockByNumber(uint64(blockNr))
|
|
if block == nil {
|
|
return state.Dump{}, fmt.Errorf("block #%d not found", blockNr)
|
|
}
|
|
header = block.Header()
|
|
}
|
|
if header == nil {
|
|
return state.Dump{}, fmt.Errorf("block #%d not found", blockNr)
|
|
}
|
|
stateDb, err := api.eth.BlockChain().StateAt(header.Root)
|
|
if err != nil {
|
|
return state.Dump{}, err
|
|
}
|
|
return stateDb.RawDump(opts), nil
|
|
}
|
|
|
|
// Preimage is a debug API function that returns the preimage for a sha3 hash, if known.
|
|
func (api *DebugAPI) Preimage(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) {
|
|
if preimage := rawdb.ReadPreimage(api.eth.ChainDb(), hash); preimage != nil {
|
|
return preimage, nil
|
|
}
|
|
return nil, errors.New("unknown preimage")
|
|
}
|
|
|
|
// 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"`
|
|
}
|
|
|
|
// GetBadBlocks returns a list of the last 'bad blocks' that the client has seen on the network
|
|
// and returns them as a JSON list of block hashes.
|
|
func (api *DebugAPI) GetBadBlocks(ctx context.Context) ([]*BadBlockArgs, error) {
|
|
var (
|
|
blocks = rawdb.ReadAllBadBlocks(api.eth.chainDb)
|
|
results = make([]*BadBlockArgs, 0, len(blocks))
|
|
)
|
|
for _, block := range blocks {
|
|
var (
|
|
blockRlp string
|
|
blockJSON map[string]interface{}
|
|
)
|
|
if rlpBytes, err := rlp.EncodeToBytes(block); err != nil {
|
|
blockRlp = err.Error() // Hacky, but hey, it works
|
|
} else {
|
|
blockRlp = fmt.Sprintf("%#x", rlpBytes)
|
|
}
|
|
blockJSON = ethapi.RPCMarshalBlock(block, true, true, api.eth.APIBackend.ChainConfig())
|
|
results = append(results, &BadBlockArgs{
|
|
Hash: block.Hash(),
|
|
RLP: blockRlp,
|
|
Block: blockJSON,
|
|
})
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
// AccountRangeMaxResults is the maximum number of results to be returned per call
|
|
const AccountRangeMaxResults = 256
|
|
|
|
// AccountRangeAt enumerates all accounts in the given block and start point in paging request
|
|
func (api *DebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, start []byte, maxResults int, nocode, nostorage, incompletes bool) (state.IteratorDump, error) {
|
|
var stateDb *state.StateDB
|
|
var err error
|
|
|
|
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 {
|
|
var header *types.Header
|
|
if number == rpc.LatestBlockNumber {
|
|
header = api.eth.blockchain.CurrentBlock()
|
|
} else {
|
|
block := api.eth.blockchain.GetBlockByNumber(uint64(number))
|
|
if block == nil {
|
|
return state.IteratorDump{}, fmt.Errorf("block #%d not found", number)
|
|
}
|
|
header = block.Header()
|
|
}
|
|
if header == nil {
|
|
return state.IteratorDump{}, fmt.Errorf("block #%d not found", number)
|
|
}
|
|
stateDb, err = api.eth.BlockChain().StateAt(header.Root)
|
|
if err != nil {
|
|
return state.IteratorDump{}, err
|
|
}
|
|
}
|
|
} 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())
|
|
if err != nil {
|
|
return state.IteratorDump{}, err
|
|
}
|
|
}
|
|
|
|
opts := &state.DumpConfig{
|
|
SkipCode: nocode,
|
|
SkipStorage: nostorage,
|
|
OnlyWithAddresses: !incompletes,
|
|
Start: start,
|
|
Max: uint64(maxResults),
|
|
}
|
|
if maxResults > AccountRangeMaxResults || maxResults <= 0 {
|
|
opts.Max = AccountRangeMaxResults
|
|
}
|
|
return stateDb.IteratorDump(opts), nil
|
|
}
|
|
|
|
// 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.
|
|
func (api *DebugAPI) StorageRangeAt(ctx context.Context, blockHash common.Hash, txIndex int, contractAddress common.Address, keyStart hexutil.Bytes, maxResult int) (StorageRangeResult, error) {
|
|
// Retrieve the block
|
|
block := api.eth.blockchain.GetBlockByHash(blockHash)
|
|
if block == nil {
|
|
return StorageRangeResult{}, fmt.Errorf("block %#x not found", blockHash)
|
|
}
|
|
_, _, statedb, release, err := api.eth.stateAtTransaction(ctx, block, txIndex, 0)
|
|
if err != nil {
|
|
return StorageRangeResult{}, err
|
|
}
|
|
defer release()
|
|
|
|
st, err := statedb.StorageTrie(contractAddress)
|
|
if err != nil {
|
|
return StorageRangeResult{}, err
|
|
}
|
|
if st == nil {
|
|
return StorageRangeResult{}, fmt.Errorf("account %x doesn't exist", contractAddress)
|
|
}
|
|
return storageRangeAt(st, keyStart, maxResult)
|
|
}
|
|
|
|
func storageRangeAt(st state.Trie, start []byte, maxResult int) (StorageRangeResult, error) {
|
|
trieIt, err := st.NodeIterator(start)
|
|
if err != nil {
|
|
return StorageRangeResult{}, err
|
|
}
|
|
it := trie.NewIterator(trieIt)
|
|
result := StorageRangeResult{Storage: storageMap{}}
|
|
for i := 0; i < maxResult && it.Next(); i++ {
|
|
_, content, _, err := rlp.Split(it.Value)
|
|
if err != nil {
|
|
return StorageRangeResult{}, err
|
|
}
|
|
e := storageEntry{Value: common.BytesToHash(content)}
|
|
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
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// GetModifiedAccountsByumber 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.
|
|
func (api *DebugAPI) GetModifiedAccountsByNumber(startNum uint64, endNum *uint64) ([]common.Address, error) {
|
|
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.
|
|
func (api *DebugAPI) GetModifiedAccountsByHash(startHash common.Hash, endHash *common.Hash) ([]common.Address, error) {
|
|
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)
|
|
}
|
|
|
|
func (api *DebugAPI) getModifiedAccounts(startBlock, endBlock *types.Block) ([]common.Address, error) {
|
|
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())
|
|
}
|
|
triedb := api.eth.BlockChain().StateCache().TrieDB()
|
|
|
|
oldTrie, err := trie.NewStateTrie(trie.StateTrieID(startBlock.Root()), triedb)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
newTrie, err := trie.NewStateTrie(trie.StateTrieID(endBlock.Root()), triedb)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
oldIt, err := oldTrie.NodeIterator([]byte{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
newIt, err := newTrie.NodeIterator([]byte{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
diff, _ := trie.NewDifferenceIterator(oldIt, newIt)
|
|
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
|
|
}
|