| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725 |
- // Copyright 2015 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 (
- "bytes"
- "compress/gzip"
- "context"
- "fmt"
- "io"
- "io/ioutil"
- "math/big"
- "os"
- "strings"
- "time"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/common/hexutil"
- "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/internal/ethapi"
- "github.com/ethereum/go-ethereum/log"
- "github.com/ethereum/go-ethereum/miner"
- "github.com/ethereum/go-ethereum/params"
- "github.com/ethereum/go-ethereum/rlp"
- "github.com/ethereum/go-ethereum/rpc"
- "github.com/ethereum/go-ethereum/trie"
- )
- const defaultTraceTimeout = 5 * time.Second
- // PublicEthereumAPI provides an API to access Ethereum full node-related
- // information.
- type PublicEthereumAPI struct {
- e *Ethereum
- }
- // NewPublicEthereumAPI creates a new Ethereum protocol API for full nodes.
- func NewPublicEthereumAPI(e *Ethereum) *PublicEthereumAPI {
- return &PublicEthereumAPI{e}
- }
- // Etherbase is the address that mining rewards will be send to
- func (api *PublicEthereumAPI) Etherbase() (common.Address, error) {
- return api.e.Etherbase()
- }
- // Coinbase is the address that mining rewards will be send to (alias for Etherbase)
- func (api *PublicEthereumAPI) Coinbase() (common.Address, error) {
- return api.Etherbase()
- }
- // Hashrate returns the POW hashrate
- func (api *PublicEthereumAPI) Hashrate() hexutil.Uint64 {
- return hexutil.Uint64(api.e.Miner().HashRate())
- }
- // PublicMinerAPI provides an API to control the miner.
- // It offers only methods that operate on data that pose no security risk when it is publicly accessible.
- type PublicMinerAPI struct {
- e *Ethereum
- agent *miner.RemoteAgent
- }
- // NewPublicMinerAPI create a new PublicMinerAPI instance.
- func NewPublicMinerAPI(e *Ethereum) *PublicMinerAPI {
- agent := miner.NewRemoteAgent(e.BlockChain(), e.Engine())
- e.Miner().Register(agent)
- return &PublicMinerAPI{e, agent}
- }
- // Mining returns an indication if this node is currently mining.
- func (api *PublicMinerAPI) Mining() bool {
- return api.e.IsMining()
- }
- // SubmitWork can be used by external miner to submit their POW solution. It returns an indication if the work was
- // accepted. Note, this is not an indication if the provided work was valid!
- func (api *PublicMinerAPI) SubmitWork(nonce types.BlockNonce, solution, digest common.Hash) bool {
- return api.agent.SubmitWork(nonce, digest, solution)
- }
- // GetWork returns a work package for external miner. The work package consists of 3 strings
- // result[0], 32 bytes hex encoded current block header pow-hash
- // result[1], 32 bytes hex encoded seed hash used for DAG
- // result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
- func (api *PublicMinerAPI) GetWork() ([3]string, error) {
- if !api.e.IsMining() {
- if err := api.e.StartMining(false); err != nil {
- return [3]string{}, err
- }
- }
- work, err := api.agent.GetWork()
- if err != nil {
- return work, fmt.Errorf("mining not ready: %v", err)
- }
- return work, nil
- }
- // SubmitHashrate can be used for remote miners to submit their hash rate. This enables the node to report the combined
- // hash rate of all miners which submit work through this node. It accepts the miner hash rate and an identifier which
- // must be unique between nodes.
- func (api *PublicMinerAPI) SubmitHashrate(hashrate hexutil.Uint64, id common.Hash) bool {
- api.agent.SubmitHashrate(id, uint64(hashrate))
- return true
- }
- // PrivateMinerAPI provides private RPC methods to control the miner.
- // These methods can be abused by external users and must be considered insecure for use by untrusted users.
- type PrivateMinerAPI struct {
- e *Ethereum
- }
- // NewPrivateMinerAPI create a new RPC service which controls the miner of this node.
- func NewPrivateMinerAPI(e *Ethereum) *PrivateMinerAPI {
- return &PrivateMinerAPI{e: e}
- }
- // Start 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.
- func (api *PrivateMinerAPI) Start(threads *int) error {
- // Set the number of threads if the seal engine supports it
- if threads == nil {
- threads = new(int)
- } else if *threads == 0 {
- *threads = -1 // Disable the miner from within
- }
- type threaded interface {
- SetThreads(threads int)
- }
- if th, ok := api.e.engine.(threaded); ok {
- log.Info("Updated mining threads", "threads", *threads)
- th.SetThreads(*threads)
- }
- // Start the miner and return
- if !api.e.IsMining() {
- // Propagate the initial price point to the transaction pool
- api.e.lock.RLock()
- price := api.e.gasPrice
- api.e.lock.RUnlock()
- api.e.txPool.SetGasPrice(price)
- return api.e.StartMining(true)
- }
- return nil
- }
- // Stop the miner
- func (api *PrivateMinerAPI) Stop() bool {
- type threaded interface {
- SetThreads(threads int)
- }
- if th, ok := api.e.engine.(threaded); ok {
- th.SetThreads(-1)
- }
- api.e.StopMining()
- return true
- }
- // SetExtra sets the extra data string that is included when this miner mines a block.
- func (api *PrivateMinerAPI) SetExtra(extra string) (bool, error) {
- if err := api.e.Miner().SetExtra([]byte(extra)); err != nil {
- return false, err
- }
- return true, nil
- }
- // SetGasPrice sets the minimum accepted gas price for the miner.
- func (api *PrivateMinerAPI) SetGasPrice(gasPrice hexutil.Big) bool {
- api.e.lock.Lock()
- api.e.gasPrice = (*big.Int)(&gasPrice)
- api.e.lock.Unlock()
- api.e.txPool.SetGasPrice((*big.Int)(&gasPrice))
- return true
- }
- // SetEtherbase sets the etherbase of the miner
- func (api *PrivateMinerAPI) SetEtherbase(etherbase common.Address) bool {
- api.e.SetEtherbase(etherbase)
- return true
- }
- // GetHashrate returns the current hashrate of the miner.
- func (api *PrivateMinerAPI) GetHashrate() uint64 {
- return uint64(api.e.miner.HashRate())
- }
- // PrivateAdminAPI is the collection of Ethereum full node-related APIs
- // exposed over the private admin endpoint.
- type PrivateAdminAPI struct {
- eth *Ethereum
- }
- // NewPrivateAdminAPI creates a new API definition for the full node private
- // admin methods of the Ethereum service.
- func NewPrivateAdminAPI(eth *Ethereum) *PrivateAdminAPI {
- return &PrivateAdminAPI{eth: eth}
- }
- // ExportChain exports the current blockchain into a local file.
- func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) {
- // 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()
- var writer io.Writer = out
- if strings.HasSuffix(file, ".gz") {
- writer = gzip.NewWriter(writer)
- defer writer.(*gzip.Writer).Close()
- }
- // Export the blockchain
- if err := api.eth.BlockChain().Export(writer); err != nil {
- return false, err
- }
- return true, nil
- }
- func hasAllBlocks(chain *core.BlockChain, bs []*types.Block) bool {
- for _, b := range bs {
- if !chain.HasBlock(b.Hash(), b.NumberU64()) {
- return false
- }
- }
- return true
- }
- // ImportChain imports a blockchain from a local file.
- func (api *PrivateAdminAPI) ImportChain(file string) (bool, error) {
- // Make sure the can access the file to import
- in, err := os.Open(file)
- if err != nil {
- return false, err
- }
- defer in.Close()
- var reader io.Reader = in
- if strings.HasSuffix(file, ".gz") {
- if reader, err = gzip.NewReader(reader); err != nil {
- return false, err
- }
- }
- // Run actual the import in pre-configured batches
- stream := rlp.NewStream(reader, 0)
- 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
- }
- if hasAllBlocks(api.eth.BlockChain(), blocks) {
- blocks = blocks[:0]
- continue
- }
- // 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
- }
- // PublicDebugAPI is the collection of Ethereum full node APIs exposed
- // over the public debugging endpoint.
- type PublicDebugAPI struct {
- eth *Ethereum
- }
- // NewPublicDebugAPI creates a new API definition for the full node-
- // related public debug methods of the Ethereum service.
- func NewPublicDebugAPI(eth *Ethereum) *PublicDebugAPI {
- return &PublicDebugAPI{eth: eth}
- }
- // DumpBlock retrieves the entire state of the database at a given block.
- func (api *PublicDebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error) {
- 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()
- return stateDb.RawDump(), nil
- }
- var block *types.Block
- if blockNr == rpc.LatestBlockNumber {
- block = 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)
- }
- stateDb, err := api.eth.BlockChain().StateAt(block.Root())
- if err != nil {
- return state.Dump{}, err
- }
- return stateDb.RawDump(), nil
- }
- // PrivateDebugAPI is the collection of Ethereum full node APIs exposed over
- // the private debugging endpoint.
- type PrivateDebugAPI struct {
- config *params.ChainConfig
- eth *Ethereum
- }
- // NewPrivateDebugAPI creates a new API definition for the full node-related
- // private debug methods of the Ethereum service.
- func NewPrivateDebugAPI(config *params.ChainConfig, eth *Ethereum) *PrivateDebugAPI {
- return &PrivateDebugAPI{config: config, eth: eth}
- }
- // BlockTraceResult is the returned value when replaying a block to check for
- // consensus results and full VM trace logs for all included transactions.
- type BlockTraceResult struct {
- Validated bool `json:"validated"`
- StructLogs []ethapi.StructLogRes `json:"structLogs"`
- Error string `json:"error"`
- }
- // TraceArgs holds extra parameters to trace functions
- type TraceArgs struct {
- *vm.LogConfig
- Tracer *string
- Timeout *string
- }
- // TraceBlock processes the given block'api RLP but does not import the block in to
- // the chain.
- func (api *PrivateDebugAPI) TraceBlock(blockRlp []byte, config *vm.LogConfig) BlockTraceResult {
- var block types.Block
- err := rlp.Decode(bytes.NewReader(blockRlp), &block)
- if err != nil {
- return BlockTraceResult{Error: fmt.Sprintf("could not decode block: %v", err)}
- }
- validated, logs, err := api.traceBlock(&block, config)
- return BlockTraceResult{
- Validated: validated,
- StructLogs: ethapi.FormatLogs(logs),
- Error: formatError(err),
- }
- }
- // TraceBlockFromFile loads the block'api RLP from the given file name and attempts to
- // process it but does not import the block in to the chain.
- func (api *PrivateDebugAPI) TraceBlockFromFile(file string, config *vm.LogConfig) BlockTraceResult {
- blockRlp, err := ioutil.ReadFile(file)
- if err != nil {
- return BlockTraceResult{Error: fmt.Sprintf("could not read file: %v", err)}
- }
- return api.TraceBlock(blockRlp, config)
- }
- // TraceBlockByNumber processes the block by canonical block number.
- func (api *PrivateDebugAPI) TraceBlockByNumber(blockNr rpc.BlockNumber, config *vm.LogConfig) BlockTraceResult {
- // Fetch the block that we aim to reprocess
- var block *types.Block
- switch blockNr {
- case rpc.PendingBlockNumber:
- // Pending block is only known by the miner
- block = api.eth.miner.PendingBlock()
- case rpc.LatestBlockNumber:
- block = api.eth.blockchain.CurrentBlock()
- default:
- block = api.eth.blockchain.GetBlockByNumber(uint64(blockNr))
- }
- if block == nil {
- return BlockTraceResult{Error: fmt.Sprintf("block #%d not found", blockNr)}
- }
- validated, logs, err := api.traceBlock(block, config)
- return BlockTraceResult{
- Validated: validated,
- StructLogs: ethapi.FormatLogs(logs),
- Error: formatError(err),
- }
- }
- // TraceBlockByHash processes the block by hash.
- func (api *PrivateDebugAPI) TraceBlockByHash(hash common.Hash, config *vm.LogConfig) BlockTraceResult {
- // Fetch the block that we aim to reprocess
- block := api.eth.BlockChain().GetBlockByHash(hash)
- if block == nil {
- return BlockTraceResult{Error: fmt.Sprintf("block #%x not found", hash)}
- }
- validated, logs, err := api.traceBlock(block, config)
- return BlockTraceResult{
- Validated: validated,
- StructLogs: ethapi.FormatLogs(logs),
- Error: formatError(err),
- }
- }
- // traceBlock processes the given block but does not save the state.
- func (api *PrivateDebugAPI) traceBlock(block *types.Block, logConfig *vm.LogConfig) (bool, []vm.StructLog, error) {
- // Validate and reprocess the block
- var (
- blockchain = api.eth.BlockChain()
- validator = blockchain.Validator()
- processor = blockchain.Processor()
- )
- structLogger := vm.NewStructLogger(logConfig)
- config := vm.Config{
- Debug: true,
- Tracer: structLogger,
- }
- if err := api.eth.engine.VerifyHeader(blockchain, block.Header(), true); err != nil {
- return false, structLogger.StructLogs(), err
- }
- statedb, err := blockchain.StateAt(blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1).Root())
- if err != nil {
- return false, structLogger.StructLogs(), err
- }
- receipts, _, usedGas, err := processor.Process(block, statedb, config)
- if err != nil {
- return false, structLogger.StructLogs(), err
- }
- if err := validator.ValidateState(block, blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1), statedb, receipts, usedGas); err != nil {
- return false, structLogger.StructLogs(), err
- }
- return true, structLogger.StructLogs(), nil
- }
- // formatError formats a Go error into either an empty string or the data content
- // of the error itself.
- func formatError(err error) string {
- if err == nil {
- return ""
- }
- return err.Error()
- }
- type timeoutError struct{}
- func (t *timeoutError) Error() string {
- return "Execution time exceeded"
- }
- // TraceTransaction returns the structured logs created during the execution of EVM
- // and returns them as a JSON object.
- func (api *PrivateDebugAPI) TraceTransaction(ctx context.Context, txHash common.Hash, config *TraceArgs) (interface{}, error) {
- var tracer vm.Tracer
- if config != nil && config.Tracer != nil {
- timeout := defaultTraceTimeout
- if config.Timeout != nil {
- var err error
- if timeout, err = time.ParseDuration(*config.Timeout); err != nil {
- return nil, err
- }
- }
- var err error
- if tracer, err = ethapi.NewJavascriptTracer(*config.Tracer); err != nil {
- return nil, err
- }
- // Handle timeouts and RPC cancellations
- deadlineCtx, cancel := context.WithTimeout(ctx, timeout)
- go func() {
- <-deadlineCtx.Done()
- tracer.(*ethapi.JavascriptTracer).Stop(&timeoutError{})
- }()
- defer cancel()
- } else if config == nil {
- tracer = vm.NewStructLogger(nil)
- } else {
- tracer = vm.NewStructLogger(config.LogConfig)
- }
- // Retrieve the tx from the chain and the containing block
- tx, blockHash, _, txIndex := core.GetTransaction(api.eth.ChainDb(), txHash)
- if tx == nil {
- return nil, fmt.Errorf("transaction %x not found", txHash)
- }
- msg, context, statedb, err := api.computeTxEnv(blockHash, int(txIndex))
- if err != nil {
- return nil, err
- }
- // Run the transaction with tracing enabled.
- vmenv := vm.NewEVM(context, statedb, api.config, vm.Config{Debug: true, Tracer: tracer})
- ret, gas, failed, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas()))
- if err != nil {
- return nil, fmt.Errorf("tracing failed: %v", err)
- }
- switch tracer := tracer.(type) {
- case *vm.StructLogger:
- return ðapi.ExecutionResult{
- Gas: gas,
- Failed: failed,
- ReturnValue: fmt.Sprintf("%x", ret),
- StructLogs: ethapi.FormatLogs(tracer.StructLogs()),
- }, nil
- case *ethapi.JavascriptTracer:
- return tracer.GetResult()
- default:
- panic(fmt.Sprintf("bad tracer type %T", tracer))
- }
- }
- // computeTxEnv returns the execution environment of a certain transaction.
- func (api *PrivateDebugAPI) computeTxEnv(blockHash common.Hash, txIndex int) (core.Message, vm.Context, *state.StateDB, error) {
- // Create the parent state.
- block := api.eth.BlockChain().GetBlockByHash(blockHash)
- if block == nil {
- return nil, vm.Context{}, nil, fmt.Errorf("block %x not found", blockHash)
- }
- parent := api.eth.BlockChain().GetBlock(block.ParentHash(), block.NumberU64()-1)
- if parent == nil {
- return nil, vm.Context{}, nil, fmt.Errorf("block parent %x not found", block.ParentHash())
- }
- statedb, err := api.eth.BlockChain().StateAt(parent.Root())
- if err != nil {
- return nil, vm.Context{}, nil, err
- }
- txs := block.Transactions()
- // Recompute transactions up to the target index.
- signer := types.MakeSigner(api.config, block.Number())
- for idx, tx := range txs {
- // Assemble the transaction call message
- msg, _ := tx.AsMessage(signer)
- context := core.NewEVMContext(msg, block.Header(), api.eth.BlockChain(), nil)
- if idx == txIndex {
- return msg, context, statedb, nil
- }
- vmenv := vm.NewEVM(context, statedb, api.config, vm.Config{})
- gp := new(core.GasPool).AddGas(tx.Gas())
- _, _, _, err := core.ApplyMessage(vmenv, msg, gp)
- if err != nil {
- return nil, vm.Context{}, nil, fmt.Errorf("tx %x failed: %v", tx.Hash(), err)
- }
- statedb.DeleteSuicides()
- }
- return nil, vm.Context{}, nil, fmt.Errorf("tx index %d out of range for block %x", txIndex, blockHash)
- }
- // Preimage is a debug API function that returns the preimage for a sha3 hash, if known.
- func (api *PrivateDebugAPI) Preimage(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) {
- db := core.PreimageTable(api.eth.ChainDb())
- return db.Get(hash.Bytes())
- }
- // 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 *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]core.BadBlockArgs, error) {
- return api.eth.BlockChain().BadBlocks()
- }
- // 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 *PrivateDebugAPI) StorageRangeAt(ctx context.Context, blockHash common.Hash, txIndex int, contractAddress common.Address, keyStart hexutil.Bytes, maxResult int) (StorageRangeResult, error) {
- _, _, statedb, err := api.computeTxEnv(blockHash, txIndex)
- if err != nil {
- return StorageRangeResult{}, err
- }
- st := statedb.StorageTrie(contractAddress)
- 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) {
- it := trie.NewIterator(st.NodeIterator(start))
- 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 *PrivateDebugAPI) 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 *PrivateDebugAPI) 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 *PrivateDebugAPI) 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())
- }
- oldTrie, err := trie.NewSecure(startBlock.Root(), api.eth.chainDb, 0)
- if err != nil {
- return nil, err
- }
- newTrie, err := trie.NewSecure(endBlock.Root(), api.eth.chainDb, 0)
- 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
- }
|