2015-10-15 09:07:19 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2016-04-14 11:18:24 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-10-15 09:07:19 -05:00
|
|
|
//
|
2016-04-14 11:18:24 -05: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 09:07:19 -05:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-10-15 09:07:19 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-04-14 11:18:24 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
2015-10-15 09:07:19 -05:00
|
|
|
//
|
2016-04-14 11:18:24 -05: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 09:07:19 -05:00
|
|
|
|
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-12-12 09:08:23 -06:00
|
|
|
"compress/gzip"
|
2015-10-15 09:07:19 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-12-04 12:56:11 -06:00
|
|
|
"io"
|
2016-02-03 16:47:58 -06:00
|
|
|
"io/ioutil"
|
2015-10-15 09:07:19 -05:00
|
|
|
"math/big"
|
2015-12-04 12:56:11 -06:00
|
|
|
"os"
|
2016-03-14 03:38:54 -05:00
|
|
|
"runtime"
|
2016-12-12 09:08:23 -06:00
|
|
|
"strings"
|
2016-08-22 09:03:30 -05:00
|
|
|
"time"
|
2015-10-15 09:07:19 -05:00
|
|
|
|
|
|
|
"github.com/ethereum/ethash"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-12-17 08:39:55 -06:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2015-10-15 09:07:19 -05:00
|
|
|
"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"
|
2015-12-15 21:26:23 -06:00
|
|
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
2015-10-15 09:07:19 -05:00
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
2015-12-16 03:58:01 -06:00
|
|
|
"github.com/ethereum/go-ethereum/miner"
|
2016-10-20 06:36:29 -05:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-10-15 09:07:19 -05:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2015-12-16 03:58:01 -06:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2016-08-22 09:03:30 -05:00
|
|
|
"golang.org/x/net/context"
|
2015-10-15 09:07:19 -05:00
|
|
|
)
|
|
|
|
|
2016-08-22 09:03:30 -05:00
|
|
|
const defaultTraceTimeout = 5 * time.Second
|
|
|
|
|
2016-06-30 05:03:26 -05:00
|
|
|
// PublicEthereumAPI provides an API to access Ethereum full node-related
|
2015-12-15 21:26:23 -06:00
|
|
|
// information.
|
2016-06-30 05:03:26 -05:00
|
|
|
type PublicEthereumAPI struct {
|
|
|
|
e *Ethereum
|
2016-01-27 10:01:30 -06:00
|
|
|
}
|
|
|
|
|
2016-06-30 05:03:26 -05:00
|
|
|
// NewPublicEthereumAPI creates a new Etheruem protocol API for full nodes.
|
|
|
|
func NewPublicEthereumAPI(e *Ethereum) *PublicEthereumAPI {
|
|
|
|
return &PublicEthereumAPI{e}
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Etherbase is the address that mining rewards will be send to
|
2016-06-30 05:03:26 -05:00
|
|
|
func (s *PublicEthereumAPI) Etherbase() (common.Address, error) {
|
2015-10-15 09:07:19 -05:00
|
|
|
return s.e.Etherbase()
|
|
|
|
}
|
|
|
|
|
2016-05-12 12:32:04 -05:00
|
|
|
// Coinbase is the address that mining rewards will be send to (alias for Etherbase)
|
2016-06-30 05:03:26 -05:00
|
|
|
func (s *PublicEthereumAPI) Coinbase() (common.Address, error) {
|
2015-10-15 09:07:19 -05:00
|
|
|
return s.Etherbase()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hashrate returns the POW hashrate
|
2016-12-17 08:39:55 -06:00
|
|
|
func (s *PublicEthereumAPI) Hashrate() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.e.Miner().HashRate())
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2016-02-09 08:03:04 -06:00
|
|
|
// 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 {
|
2016-06-30 05:03:26 -05:00
|
|
|
e *Ethereum
|
2016-02-09 08:03:04 -06:00
|
|
|
agent *miner.RemoteAgent
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPublicMinerAPI create a new PublicMinerAPI instance.
|
2016-06-30 05:03:26 -05:00
|
|
|
func NewPublicMinerAPI(e *Ethereum) *PublicMinerAPI {
|
2016-12-19 19:14:36 -06:00
|
|
|
agent := miner.NewRemoteAgent(e.Pow())
|
2016-02-09 08:03:04 -06:00
|
|
|
e.Miner().Register(agent)
|
|
|
|
|
|
|
|
return &PublicMinerAPI{e, agent}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mining returns an indication if this node is currently mining.
|
|
|
|
func (s *PublicMinerAPI) Mining() bool {
|
|
|
|
return s.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!
|
2017-01-12 17:37:23 -06:00
|
|
|
func (s *PublicMinerAPI) SubmitWork(nonce types.BlockNonce, solution, digest common.Hash) bool {
|
|
|
|
return s.agent.SubmitWork(nonce, digest, solution)
|
2016-02-09 08:03:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-05-12 12:32:04 -05:00
|
|
|
func (s *PublicMinerAPI) GetWork() (work [3]string, err error) {
|
2016-02-09 08:03:04 -06:00
|
|
|
if !s.e.IsMining() {
|
2016-10-28 12:05:01 -05:00
|
|
|
if err := s.e.StartMining(0); err != nil {
|
2016-05-12 12:32:04 -05:00
|
|
|
return work, err
|
2016-02-09 08:03:04 -06:00
|
|
|
}
|
|
|
|
}
|
2016-05-12 12:32:04 -05:00
|
|
|
if work, err = s.agent.GetWork(); err == nil {
|
|
|
|
return
|
2016-02-09 08:03:04 -06:00
|
|
|
}
|
2016-05-12 12:32:04 -05:00
|
|
|
glog.V(logger.Debug).Infof("%v", err)
|
|
|
|
return work, fmt.Errorf("mining not ready")
|
2016-02-09 08:03:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2016-12-17 08:39:55 -06:00
|
|
|
func (s *PublicMinerAPI) SubmitHashrate(hashrate hexutil.Uint64, id common.Hash) bool {
|
|
|
|
s.agent.SubmitHashrate(id, uint64(hashrate))
|
2016-02-09 08:03:04 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-10-15 09:07:19 -05:00
|
|
|
// 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 {
|
2016-06-30 05:03:26 -05:00
|
|
|
e *Ethereum
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPrivateMinerAPI create a new RPC service which controls the miner of this node.
|
2016-06-30 05:03:26 -05:00
|
|
|
func NewPrivateMinerAPI(e *Ethereum) *PrivateMinerAPI {
|
2015-10-15 09:07:19 -05:00
|
|
|
return &PrivateMinerAPI{e: e}
|
|
|
|
}
|
|
|
|
|
2016-03-14 03:38:54 -05:00
|
|
|
// 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 CPU's that are usable by this process.
|
2016-12-23 03:28:11 -06:00
|
|
|
func (s *PrivateMinerAPI) Start(threads *int) (bool, error) {
|
2015-10-15 09:07:19 -05:00
|
|
|
s.e.StartAutoDAG()
|
2016-12-17 08:39:55 -06:00
|
|
|
var err error
|
2016-03-14 03:38:54 -05:00
|
|
|
if threads == nil {
|
2016-12-17 08:39:55 -06:00
|
|
|
err = s.e.StartMining(runtime.NumCPU())
|
|
|
|
} else {
|
2016-12-23 03:28:11 -06:00
|
|
|
err = s.e.StartMining(*threads)
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
2016-12-17 08:39:55 -06:00
|
|
|
return err == nil, err
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the miner
|
|
|
|
func (s *PrivateMinerAPI) Stop() bool {
|
|
|
|
s.e.StopMining()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetExtra sets the extra data string that is included when this miner mines a block.
|
|
|
|
func (s *PrivateMinerAPI) SetExtra(extra string) (bool, error) {
|
|
|
|
if err := s.e.Miner().SetExtra([]byte(extra)); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGasPrice sets the minimum accepted gas price for the miner.
|
2016-12-17 08:39:55 -06:00
|
|
|
func (s *PrivateMinerAPI) SetGasPrice(gasPrice hexutil.Big) bool {
|
|
|
|
s.e.Miner().SetGasPrice((*big.Int)(&gasPrice))
|
2015-10-15 09:07:19 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEtherbase sets the etherbase of the miner
|
|
|
|
func (s *PrivateMinerAPI) SetEtherbase(etherbase common.Address) bool {
|
|
|
|
s.e.SetEtherbase(etherbase)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartAutoDAG starts auto DAG generation. This will prevent the DAG generating on epoch change
|
|
|
|
// which will cause the node to stop mining during the generation process.
|
|
|
|
func (s *PrivateMinerAPI) StartAutoDAG() bool {
|
|
|
|
s.e.StartAutoDAG()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopAutoDAG stops auto DAG generation
|
|
|
|
func (s *PrivateMinerAPI) StopAutoDAG() bool {
|
|
|
|
s.e.StopAutoDAG()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeDAG creates the new DAG for the given block number
|
|
|
|
func (s *PrivateMinerAPI) MakeDAG(blockNr rpc.BlockNumber) (bool, error) {
|
|
|
|
if err := ethash.MakeDAG(uint64(blockNr.Int64()), ""); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2016-06-30 05:03:26 -05:00
|
|
|
// PrivateAdminAPI is the collection of Etheruem full node-related APIs
|
2015-12-15 21:26:23 -06:00
|
|
|
// exposed over the private admin endpoint.
|
2016-06-30 05:03:26 -05:00
|
|
|
type PrivateAdminAPI struct {
|
|
|
|
eth *Ethereum
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
|
|
|
|
2015-12-15 21:26:23 -06:00
|
|
|
// NewPrivateAdminAPI creates a new API definition for the full node private
|
|
|
|
// admin methods of the Ethereum service.
|
2016-06-30 05:03:26 -05:00
|
|
|
func NewPrivateAdminAPI(eth *Ethereum) *PrivateAdminAPI {
|
|
|
|
return &PrivateAdminAPI{eth: eth}
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExportChain exports the current blockchain into a local file.
|
2016-06-30 05:03:26 -05:00
|
|
|
func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) {
|
2015-12-04 12:56:11 -06: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 09:08:23 -06:00
|
|
|
var writer io.Writer = out
|
|
|
|
if strings.HasSuffix(file, ".gz") {
|
|
|
|
writer = gzip.NewWriter(writer)
|
|
|
|
defer writer.(*gzip.Writer).Close()
|
|
|
|
}
|
|
|
|
|
2015-12-04 12:56:11 -06:00
|
|
|
// Export the blockchain
|
2016-12-12 09:08:23 -06:00
|
|
|
if err := api.eth.BlockChain().Export(writer); err != nil {
|
2015-12-04 12:56:11 -06:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2015-12-16 03:58:01 -06:00
|
|
|
func hasAllBlocks(chain *core.BlockChain, bs []*types.Block) bool {
|
|
|
|
for _, b := range bs {
|
|
|
|
if !chain.HasBlock(b.Hash()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-12-04 12:56:11 -06:00
|
|
|
// ImportChain imports a blockchain from a local file.
|
2016-06-30 05:03:26 -05:00
|
|
|
func (api *PrivateAdminAPI) ImportChain(file string) (bool, error) {
|
2015-12-04 12:56:11 -06: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 09:08:23 -06: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 12:56:11 -06:00
|
|
|
// Run actual the import in pre-configured batches
|
2016-12-12 09:08:23 -06:00
|
|
|
stream := rlp.NewStream(reader, 0)
|
2015-12-04 12:56:11 -06: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 03:58:01 -06:00
|
|
|
|
|
|
|
if hasAllBlocks(api.eth.BlockChain(), blocks) {
|
|
|
|
blocks = blocks[:0]
|
|
|
|
continue
|
|
|
|
}
|
2015-12-04 12:56:11 -06: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
|
|
|
|
}
|
|
|
|
|
2016-06-30 05:03:26 -05:00
|
|
|
// PublicDebugAPI is the collection of Etheruem full node APIs exposed
|
2015-12-15 21:26:23 -06:00
|
|
|
// over the public debugging endpoint.
|
2016-06-30 05:03:26 -05:00
|
|
|
type PublicDebugAPI struct {
|
|
|
|
eth *Ethereum
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
|
|
|
|
2016-06-30 05:03:26 -05:00
|
|
|
// NewPublicDebugAPI creates a new API definition for the full node-
|
2015-12-15 21:26:23 -06:00
|
|
|
// related public debug methods of the Ethereum service.
|
2016-06-30 05:03:26 -05:00
|
|
|
func NewPublicDebugAPI(eth *Ethereum) *PublicDebugAPI {
|
|
|
|
return &PublicDebugAPI{eth: eth}
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// DumpBlock retrieves the entire state of the database at a given block.
|
2016-09-22 14:04:58 -05:00
|
|
|
func (api *PublicDebugAPI) DumpBlock(number uint64) (state.Dump, error) {
|
2015-12-04 12:56:11 -06:00
|
|
|
block := api.eth.BlockChain().GetBlockByNumber(number)
|
|
|
|
if block == nil {
|
2016-09-22 14:04:58 -05:00
|
|
|
return state.Dump{}, fmt.Errorf("block #%d not found", number)
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
2016-09-27 05:13:13 -05:00
|
|
|
stateDb, err := api.eth.BlockChain().StateAt(block.Root())
|
2015-12-04 12:56:11 -06:00
|
|
|
if err != nil {
|
2016-09-22 14:04:58 -05:00
|
|
|
return state.Dump{}, err
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
|
|
|
return stateDb.RawDump(), nil
|
|
|
|
}
|
|
|
|
|
2016-06-30 05:03:26 -05:00
|
|
|
// PrivateDebugAPI is the collection of Etheruem full node APIs exposed over
|
2015-12-15 21:26:23 -06:00
|
|
|
// the private debugging endpoint.
|
2016-06-30 05:03:26 -05:00
|
|
|
type PrivateDebugAPI struct {
|
2016-10-20 06:36:29 -05:00
|
|
|
config *params.ChainConfig
|
2016-06-30 05:03:26 -05:00
|
|
|
eth *Ethereum
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
|
|
|
|
2016-06-30 05:03:26 -05:00
|
|
|
// NewPrivateDebugAPI creates a new API definition for the full node-related
|
2015-12-15 21:26:23 -06:00
|
|
|
// private debug methods of the Ethereum service.
|
2016-10-20 06:36:29 -05:00
|
|
|
func NewPrivateDebugAPI(config *params.ChainConfig, eth *Ethereum) *PrivateDebugAPI {
|
2016-06-30 05:03:26 -05:00
|
|
|
return &PrivateDebugAPI{config: config, eth: eth}
|
2016-02-20 07:36:34 -06:00
|
|
|
}
|
|
|
|
|
2016-05-12 12:32:04 -05:00
|
|
|
// BlockTraceResult is the returned value when replaying a block to check for
|
2016-02-03 16:47:58 -06:00
|
|
|
// consensus results and full VM trace logs for all included transactions.
|
|
|
|
type BlockTraceResult struct {
|
2015-12-15 21:26:23 -06:00
|
|
|
Validated bool `json:"validated"`
|
|
|
|
StructLogs []ethapi.StructLogRes `json:"structLogs"`
|
|
|
|
Error string `json:"error"`
|
2016-02-03 16:47:58 -06:00
|
|
|
}
|
|
|
|
|
2016-08-22 09:03:30 -05:00
|
|
|
// TraceArgs holds extra parameters to trace functions
|
|
|
|
type TraceArgs struct {
|
|
|
|
*vm.LogConfig
|
|
|
|
Tracer *string
|
|
|
|
Timeout *string
|
|
|
|
}
|
|
|
|
|
2016-02-03 16:47:58 -06:00
|
|
|
// TraceBlock processes the given block's RLP but does not import the block in to
|
|
|
|
// the chain.
|
2016-08-19 09:19:51 -05:00
|
|
|
func (api *PrivateDebugAPI) TraceBlock(blockRlp []byte, config *vm.LogConfig) BlockTraceResult {
|
2016-02-03 16:47:58 -06:00
|
|
|
var block types.Block
|
|
|
|
err := rlp.Decode(bytes.NewReader(blockRlp), &block)
|
|
|
|
if err != nil {
|
2016-03-31 09:41:26 -05:00
|
|
|
return BlockTraceResult{Error: fmt.Sprintf("could not decode block: %v", err)}
|
2016-02-03 16:47:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
validated, logs, err := api.traceBlock(&block, config)
|
|
|
|
return BlockTraceResult{
|
|
|
|
Validated: validated,
|
2015-12-15 21:26:23 -06:00
|
|
|
StructLogs: ethapi.FormatLogs(logs),
|
2016-03-31 10:15:38 -05:00
|
|
|
Error: formatError(err),
|
2016-02-03 16:47:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceBlockFromFile loads the block's RLP from the given file name and attempts to
|
|
|
|
// process it but does not import the block in to the chain.
|
2016-08-19 09:19:51 -05:00
|
|
|
func (api *PrivateDebugAPI) TraceBlockFromFile(file string, config *vm.LogConfig) BlockTraceResult {
|
2016-02-03 16:47:58 -06:00
|
|
|
blockRlp, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
2016-03-31 09:41:26 -05:00
|
|
|
return BlockTraceResult{Error: fmt.Sprintf("could not read file: %v", err)}
|
2016-02-03 16:47:58 -06:00
|
|
|
}
|
|
|
|
return api.TraceBlock(blockRlp, config)
|
|
|
|
}
|
|
|
|
|
2016-05-12 12:32:04 -05:00
|
|
|
// TraceBlockByNumber processes the block by canonical block number.
|
2016-08-19 09:19:51 -05:00
|
|
|
func (api *PrivateDebugAPI) TraceBlockByNumber(number uint64, config *vm.LogConfig) BlockTraceResult {
|
2015-12-04 12:56:11 -06:00
|
|
|
// Fetch the block that we aim to reprocess
|
|
|
|
block := api.eth.BlockChain().GetBlockByNumber(number)
|
|
|
|
if block == nil {
|
2016-03-31 09:41:26 -05:00
|
|
|
return BlockTraceResult{Error: fmt.Sprintf("block #%d not found", number)}
|
2016-02-03 16:47:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
validated, logs, err := api.traceBlock(block, config)
|
|
|
|
return BlockTraceResult{
|
|
|
|
Validated: validated,
|
2015-12-15 21:26:23 -06:00
|
|
|
StructLogs: ethapi.FormatLogs(logs),
|
2016-03-31 10:15:38 -05:00
|
|
|
Error: formatError(err),
|
2016-02-03 16:47:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceBlockByHash processes the block by hash.
|
2016-08-19 09:19:51 -05:00
|
|
|
func (api *PrivateDebugAPI) TraceBlockByHash(hash common.Hash, config *vm.LogConfig) BlockTraceResult {
|
2016-02-03 16:47:58 -06:00
|
|
|
// Fetch the block that we aim to reprocess
|
2016-04-05 08:22:04 -05:00
|
|
|
block := api.eth.BlockChain().GetBlockByHash(hash)
|
2016-02-03 16:47:58 -06:00
|
|
|
if block == nil {
|
2016-03-31 09:41:26 -05:00
|
|
|
return BlockTraceResult{Error: fmt.Sprintf("block #%x not found", hash)}
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
|
|
|
|
2016-02-03 16:47:58 -06:00
|
|
|
validated, logs, err := api.traceBlock(block, config)
|
|
|
|
return BlockTraceResult{
|
|
|
|
Validated: validated,
|
2015-12-15 21:26:23 -06:00
|
|
|
StructLogs: ethapi.FormatLogs(logs),
|
2016-03-31 10:15:38 -05:00
|
|
|
Error: formatError(err),
|
2016-02-03 16:47:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// traceBlock processes the given block but does not save the state.
|
2016-08-19 09:19:51 -05:00
|
|
|
func (api *PrivateDebugAPI) traceBlock(block *types.Block, logConfig *vm.LogConfig) (bool, []vm.StructLog, error) {
|
2015-12-04 12:56:11 -06:00
|
|
|
// Validate and reprocess the block
|
|
|
|
var (
|
|
|
|
blockchain = api.eth.BlockChain()
|
|
|
|
validator = blockchain.Validator()
|
|
|
|
processor = blockchain.Processor()
|
|
|
|
)
|
2016-08-19 09:19:51 -05:00
|
|
|
|
|
|
|
structLogger := vm.NewStructLogger(logConfig)
|
|
|
|
|
|
|
|
config := vm.Config{
|
|
|
|
Debug: true,
|
|
|
|
Tracer: structLogger,
|
2016-04-14 07:10:29 -05:00
|
|
|
}
|
2016-02-03 16:47:58 -06:00
|
|
|
|
2016-04-05 08:22:04 -05:00
|
|
|
if err := core.ValidateHeader(api.config, blockchain.AuxValidator(), block.Header(), blockchain.GetHeader(block.ParentHash(), block.NumberU64()-1), true, false); err != nil {
|
2016-08-19 09:19:51 -05:00
|
|
|
return false, structLogger.StructLogs(), err
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
2016-09-27 05:13:13 -05:00
|
|
|
statedb, err := blockchain.StateAt(blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1).Root())
|
2015-12-04 12:56:11 -06:00
|
|
|
if err != nil {
|
2016-08-19 09:19:51 -05:00
|
|
|
return false, structLogger.StructLogs(), err
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
2016-02-03 16:47:58 -06:00
|
|
|
|
2016-08-19 09:19:51 -05:00
|
|
|
receipts, _, usedGas, err := processor.Process(block, statedb, config)
|
2015-12-04 12:56:11 -06:00
|
|
|
if err != nil {
|
2016-08-19 09:19:51 -05:00
|
|
|
return false, structLogger.StructLogs(), err
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
2016-04-05 08:22:04 -05:00
|
|
|
if err := validator.ValidateState(block, blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1), statedb, receipts, usedGas); err != nil {
|
2016-08-19 09:19:51 -05:00
|
|
|
return false, structLogger.StructLogs(), err
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
2016-08-19 09:19:51 -05:00
|
|
|
return true, structLogger.StructLogs(), nil
|
2015-12-04 12:56:11 -06:00
|
|
|
}
|
|
|
|
|
2017-01-06 11:44:35 -06:00
|
|
|
// callmsg is the message type used for call transitions.
|
2015-12-15 21:26:23 -06:00
|
|
|
type callmsg struct {
|
|
|
|
addr common.Address
|
|
|
|
to *common.Address
|
|
|
|
gas, gasPrice *big.Int
|
|
|
|
value *big.Int
|
|
|
|
data []byte
|
2016-01-28 12:21:05 -06:00
|
|
|
}
|
|
|
|
|
2015-12-15 21:26:23 -06:00
|
|
|
// accessor boilerplate to implement core.Message
|
|
|
|
func (m callmsg) From() (common.Address, error) { return m.addr, nil }
|
|
|
|
func (m callmsg) FromFrontier() (common.Address, error) { return m.addr, nil }
|
2016-07-11 04:58:10 -05:00
|
|
|
func (m callmsg) Nonce() uint64 { return 0 }
|
|
|
|
func (m callmsg) CheckNonce() bool { return false }
|
2015-12-15 21:26:23 -06:00
|
|
|
func (m callmsg) To() *common.Address { return m.to }
|
|
|
|
func (m callmsg) GasPrice() *big.Int { return m.gasPrice }
|
|
|
|
func (m callmsg) Gas() *big.Int { return m.gas }
|
|
|
|
func (m callmsg) Value() *big.Int { return m.value }
|
|
|
|
func (m callmsg) Data() []byte { return m.data }
|
2016-02-03 16:47:58 -06:00
|
|
|
|
2016-03-31 10:15:38 -05:00
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2016-08-22 09:03:30 -05:00
|
|
|
type timeoutError struct{}
|
|
|
|
|
|
|
|
func (t *timeoutError) Error() string {
|
|
|
|
return "Execution time exceeded"
|
|
|
|
}
|
|
|
|
|
2016-02-03 16:47:58 -06:00
|
|
|
// TraceTransaction returns the structured logs created during the execution of EVM
|
|
|
|
// and returns them as a JSON object.
|
2016-08-22 09:03:30 -05:00
|
|
|
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)
|
|
|
|
}
|
2016-08-19 09:19:51 -05:00
|
|
|
|
2016-04-14 06:29:47 -05:00
|
|
|
// Retrieve the tx from the chain and the containing block
|
2016-05-12 12:32:04 -05:00
|
|
|
tx, blockHash, _, txIndex := core.GetTransaction(api.eth.ChainDb(), txHash)
|
2016-01-28 12:21:05 -06:00
|
|
|
if tx == nil {
|
2016-04-14 06:29:47 -05:00
|
|
|
return nil, fmt.Errorf("transaction %x not found", txHash)
|
2016-01-28 12:21:05 -06:00
|
|
|
}
|
2016-04-05 08:22:04 -05:00
|
|
|
block := api.eth.BlockChain().GetBlockByHash(blockHash)
|
2016-01-28 12:21:05 -06:00
|
|
|
if block == nil {
|
2016-04-14 06:29:47 -05:00
|
|
|
return nil, fmt.Errorf("block %x not found", blockHash)
|
2016-01-28 12:21:05 -06:00
|
|
|
}
|
2016-04-14 06:29:47 -05:00
|
|
|
// Create the state database to mutate and eventually trace
|
2016-04-05 08:22:04 -05:00
|
|
|
parent := api.eth.BlockChain().GetBlock(block.ParentHash(), block.NumberU64()-1)
|
2016-04-14 06:29:47 -05:00
|
|
|
if parent == nil {
|
|
|
|
return nil, fmt.Errorf("block parent %x not found", block.ParentHash())
|
2016-01-28 12:21:05 -06:00
|
|
|
}
|
2016-09-27 05:13:13 -05:00
|
|
|
stateDb, err := api.eth.BlockChain().StateAt(parent.Root())
|
2016-01-28 12:21:05 -06:00
|
|
|
if err != nil {
|
2016-04-14 06:29:47 -05:00
|
|
|
return nil, err
|
2016-01-28 12:21:05 -06:00
|
|
|
}
|
2016-11-02 07:44:13 -05:00
|
|
|
|
|
|
|
signer := types.MakeSigner(api.config, block.Number())
|
2016-04-14 06:29:47 -05:00
|
|
|
// Mutate the state and trace the selected transaction
|
|
|
|
for idx, tx := range block.Transactions() {
|
|
|
|
// Assemble the transaction call message
|
2016-11-02 07:44:13 -05:00
|
|
|
msg, err := tx.AsMessage(signer)
|
2016-04-14 06:29:47 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("sender retrieval failed: %v", err)
|
|
|
|
}
|
2016-12-05 19:16:03 -06:00
|
|
|
context := core.NewEVMContext(msg, block.Header(), api.eth.BlockChain())
|
|
|
|
|
2016-04-14 06:29:47 -05:00
|
|
|
// Mutate the state if we haven't reached the tracing transaction yet
|
|
|
|
if uint64(idx) < txIndex {
|
2017-01-05 04:52:10 -06:00
|
|
|
vmenv := vm.NewEVM(context, stateDb, api.config, vm.Config{})
|
2016-04-14 06:29:47 -05:00
|
|
|
_, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("mutation failed: %v", err)
|
|
|
|
}
|
2016-06-13 04:56:42 -05:00
|
|
|
stateDb.DeleteSuicides()
|
2016-04-14 06:29:47 -05:00
|
|
|
continue
|
|
|
|
}
|
2016-12-05 19:16:03 -06:00
|
|
|
|
2017-01-05 04:52:10 -06:00
|
|
|
vmenv := vm.NewEVM(context, stateDb, api.config, vm.Config{Debug: true, Tracer: tracer})
|
2016-04-14 06:29:47 -05:00
|
|
|
ret, gas, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("tracing failed: %v", err)
|
|
|
|
}
|
2016-08-22 09:03:30 -05:00
|
|
|
|
|
|
|
switch tracer := tracer.(type) {
|
|
|
|
case *vm.StructLogger:
|
|
|
|
return ðapi.ExecutionResult{
|
|
|
|
Gas: gas,
|
|
|
|
ReturnValue: fmt.Sprintf("%x", ret),
|
|
|
|
StructLogs: ethapi.FormatLogs(tracer.StructLogs()),
|
|
|
|
}, nil
|
|
|
|
case *ethapi.JavascriptTracer:
|
|
|
|
return tracer.GetResult()
|
|
|
|
}
|
2016-01-28 12:21:05 -06:00
|
|
|
}
|
2016-04-14 06:29:47 -05:00
|
|
|
return nil, errors.New("database inconsistency")
|
2016-01-28 12:21:05 -06:00
|
|
|
}
|
2017-01-17 05:19:50 -06:00
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|