2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-06-09 07:48:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-23 16:12:48 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2015-06-09 07:48:18 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/ethash"
|
2015-10-19 14:08:17 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2015-06-09 07:48:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc/codec"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc/shared"
|
|
|
|
"github.com/ethereum/go-ethereum/xeth"
|
2015-06-23 16:12:48 +00:00
|
|
|
"github.com/rcrowley/go-metrics"
|
2015-06-09 07:48:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-06-10 07:42:14 +00:00
|
|
|
DebugApiVersion = "1.0"
|
2015-06-09 07:48:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// mapping between methods and handlers
|
|
|
|
DebugMapping = map[string]debughandler{
|
2015-06-09 14:06:51 +00:00
|
|
|
"debug_dumpBlock": (*debugApi).DumpBlock,
|
|
|
|
"debug_getBlockRlp": (*debugApi).GetBlockRlp,
|
|
|
|
"debug_printBlock": (*debugApi).PrintBlock,
|
|
|
|
"debug_processBlock": (*debugApi).ProcessBlock,
|
|
|
|
"debug_seedHash": (*debugApi).SeedHash,
|
|
|
|
"debug_setHead": (*debugApi).SetHead,
|
2015-06-23 16:12:48 +00:00
|
|
|
"debug_metrics": (*debugApi).Metrics,
|
2015-06-09 07:48:18 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// debug callback handler
|
2015-06-09 14:06:51 +00:00
|
|
|
type debughandler func(*debugApi, *shared.Request) (interface{}, error)
|
2015-06-09 07:48:18 +00:00
|
|
|
|
|
|
|
// admin api provider
|
2015-06-09 14:06:51 +00:00
|
|
|
type debugApi struct {
|
2015-06-09 07:48:18 +00:00
|
|
|
xeth *xeth.XEth
|
|
|
|
ethereum *eth.Ethereum
|
|
|
|
methods map[string]debughandler
|
|
|
|
codec codec.ApiCoder
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a new debug api instance
|
2015-06-09 14:06:51 +00:00
|
|
|
func NewDebugApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *debugApi {
|
|
|
|
return &debugApi{
|
2015-06-09 07:48:18 +00:00
|
|
|
xeth: xeth,
|
|
|
|
ethereum: ethereum,
|
|
|
|
methods: DebugMapping,
|
|
|
|
codec: coder.New(nil),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collection with supported methods
|
2015-06-09 14:06:51 +00:00
|
|
|
func (self *debugApi) Methods() []string {
|
2015-06-09 07:48:18 +00:00
|
|
|
methods := make([]string, len(self.methods))
|
|
|
|
i := 0
|
|
|
|
for k := range self.methods {
|
|
|
|
methods[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return methods
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute given request
|
2015-06-09 14:06:51 +00:00
|
|
|
func (self *debugApi) Execute(req *shared.Request) (interface{}, error) {
|
2015-06-09 07:48:18 +00:00
|
|
|
if callback, ok := self.methods[req.Method]; ok {
|
|
|
|
return callback(self, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &shared.NotImplementedError{req.Method}
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:06:51 +00:00
|
|
|
func (self *debugApi) Name() string {
|
2015-06-22 10:47:32 +00:00
|
|
|
return shared.DebugApiName
|
2015-06-09 07:48:18 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 07:42:14 +00:00
|
|
|
func (self *debugApi) ApiVersion() string {
|
|
|
|
return DebugApiVersion
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:06:51 +00:00
|
|
|
func (self *debugApi) PrintBlock(req *shared.Request) (interface{}, error) {
|
2015-06-09 07:48:18 +00:00
|
|
|
args := new(BlockNumArg)
|
|
|
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
|
|
|
return nil, shared.NewDecodeParamError(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
block := self.xeth.EthBlockByNumber(args.BlockNumber)
|
|
|
|
return fmt.Sprintf("%s", block), nil
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:06:51 +00:00
|
|
|
func (self *debugApi) DumpBlock(req *shared.Request) (interface{}, error) {
|
2015-06-09 07:48:18 +00:00
|
|
|
args := new(BlockNumArg)
|
|
|
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
|
|
|
return nil, shared.NewDecodeParamError(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
block := self.xeth.EthBlockByNumber(args.BlockNumber)
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("block #%d not found", args.BlockNumber)
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:35:55 +00:00
|
|
|
stateDb, err := state.New(block.Root(), self.ethereum.ChainDb())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-06-09 07:48:18 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 07:42:14 +00:00
|
|
|
return stateDb.RawDump(), nil
|
2015-06-09 07:48:18 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 14:06:51 +00:00
|
|
|
func (self *debugApi) GetBlockRlp(req *shared.Request) (interface{}, error) {
|
2015-06-09 07:48:18 +00:00
|
|
|
args := new(BlockNumArg)
|
|
|
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
|
|
|
return nil, shared.NewDecodeParamError(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
block := self.xeth.EthBlockByNumber(args.BlockNumber)
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("block #%d not found", args.BlockNumber)
|
|
|
|
}
|
|
|
|
encoded, err := rlp.EncodeToBytes(block)
|
|
|
|
return fmt.Sprintf("%x", encoded), err
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:06:51 +00:00
|
|
|
func (self *debugApi) SetHead(req *shared.Request) (interface{}, error) {
|
2015-06-09 07:48:18 +00:00
|
|
|
args := new(BlockNumArg)
|
|
|
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
|
|
|
return nil, shared.NewDecodeParamError(err.Error())
|
|
|
|
}
|
2015-09-21 12:36:29 +00:00
|
|
|
self.ethereum.BlockChain().SetHead(uint64(args.BlockNumber))
|
2015-06-09 07:48:18 +00:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:06:51 +00:00
|
|
|
func (self *debugApi) ProcessBlock(req *shared.Request) (interface{}, error) {
|
2015-06-09 07:48:18 +00:00
|
|
|
args := new(BlockNumArg)
|
|
|
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
|
|
|
return nil, shared.NewDecodeParamError(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
block := self.xeth.EthBlockByNumber(args.BlockNumber)
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("block #%d not found", args.BlockNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
old := vm.Debug
|
|
|
|
defer func() { vm.Debug = old }()
|
|
|
|
vm.Debug = true
|
|
|
|
|
2015-10-19 14:08:17 +00:00
|
|
|
var (
|
|
|
|
blockchain = self.ethereum.BlockChain()
|
|
|
|
validator = blockchain.Validator()
|
|
|
|
processor = blockchain.Processor()
|
|
|
|
)
|
|
|
|
|
|
|
|
err := core.ValidateHeader(blockchain.AuxValidator(), block.Header(), blockchain.GetHeader(block.ParentHash()), true, false)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2015-06-09 07:48:18 +00:00
|
|
|
}
|
2015-10-19 14:08:17 +00:00
|
|
|
statedb, err := state.New(blockchain.GetBlock(block.ParentHash()).Root(), self.ethereum.ChainDb())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
receipts, _, usedGas, err := processor.Process(block, statedb)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
err = validator.ValidateState(block, blockchain.GetBlock(block.ParentHash()), statedb, receipts, usedGas)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
2015-06-09 07:48:18 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 14:06:51 +00:00
|
|
|
func (self *debugApi) SeedHash(req *shared.Request) (interface{}, error) {
|
2015-06-09 07:48:18 +00:00
|
|
|
args := new(BlockNumArg)
|
|
|
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
|
|
|
return nil, shared.NewDecodeParamError(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if hash, err := ethash.GetSeedHash(uint64(args.BlockNumber)); err == nil {
|
|
|
|
return fmt.Sprintf("0x%x", hash), nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-06-23 16:12:48 +00:00
|
|
|
|
|
|
|
func (self *debugApi) Metrics(req *shared.Request) (interface{}, error) {
|
2015-06-24 11:38:58 +00:00
|
|
|
args := new(MetricsArgs)
|
|
|
|
if err := self.codec.Decode(req.Params, &args); err != nil {
|
|
|
|
return nil, shared.NewDecodeParamError(err.Error())
|
|
|
|
}
|
2015-06-23 16:12:48 +00:00
|
|
|
// Create a rate formatter
|
|
|
|
units := []string{"", "K", "M", "G", "T", "E", "P"}
|
|
|
|
round := func(value float64, prec int) string {
|
|
|
|
unit := 0
|
|
|
|
for value >= 1000 {
|
|
|
|
unit, value, prec = unit+1, value/1000, 2
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(fmt.Sprintf("%%.%df%s", prec, units[unit]), value)
|
|
|
|
}
|
|
|
|
format := func(total float64, rate float64) string {
|
|
|
|
return fmt.Sprintf("%s (%s/s)", round(total, 0), round(rate, 2))
|
|
|
|
}
|
|
|
|
// Iterate over all the metrics, and just dump for now
|
|
|
|
counters := make(map[string]interface{})
|
|
|
|
metrics.DefaultRegistry.Each(func(name string, metric interface{}) {
|
|
|
|
// Create or retrieve the counter hierarchy for this metric
|
|
|
|
root, parts := counters, strings.Split(name, "/")
|
|
|
|
for _, part := range parts[:len(parts)-1] {
|
|
|
|
if _, ok := root[part]; !ok {
|
|
|
|
root[part] = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
root = root[part].(map[string]interface{})
|
|
|
|
}
|
|
|
|
name = parts[len(parts)-1]
|
|
|
|
|
2015-06-24 11:38:58 +00:00
|
|
|
// Fill the counter with the metric details, formatting if requested
|
|
|
|
if args.Raw {
|
|
|
|
switch metric := metric.(type) {
|
|
|
|
case metrics.Meter:
|
|
|
|
root[name] = map[string]interface{}{
|
2015-06-24 15:30:00 +00:00
|
|
|
"AvgRate01Min": metric.Rate1(),
|
|
|
|
"AvgRate05Min": metric.Rate5(),
|
|
|
|
"AvgRate15Min": metric.Rate15(),
|
|
|
|
"MeanRate": metric.RateMean(),
|
2015-06-25 12:33:26 +00:00
|
|
|
"Overall": float64(metric.Count()),
|
2015-06-24 11:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case metrics.Timer:
|
|
|
|
root[name] = map[string]interface{}{
|
2015-06-24 15:30:00 +00:00
|
|
|
"AvgRate01Min": metric.Rate1(),
|
|
|
|
"AvgRate05Min": metric.Rate5(),
|
|
|
|
"AvgRate15Min": metric.Rate15(),
|
|
|
|
"MeanRate": metric.RateMean(),
|
2015-06-25 12:33:26 +00:00
|
|
|
"Overall": float64(metric.Count()),
|
|
|
|
"Percentiles": map[string]interface{}{
|
|
|
|
"5": metric.Percentile(0.05),
|
|
|
|
"20": metric.Percentile(0.2),
|
|
|
|
"50": metric.Percentile(0.5),
|
|
|
|
"80": metric.Percentile(0.8),
|
|
|
|
"95": metric.Percentile(0.95),
|
|
|
|
},
|
2015-06-24 11:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
root[name] = "Unknown metric type"
|
2015-06-23 16:12:48 +00:00
|
|
|
}
|
2015-06-24 11:38:58 +00:00
|
|
|
} else {
|
|
|
|
switch metric := metric.(type) {
|
|
|
|
case metrics.Meter:
|
|
|
|
root[name] = map[string]interface{}{
|
|
|
|
"Avg01Min": format(metric.Rate1()*60, metric.Rate1()),
|
|
|
|
"Avg05Min": format(metric.Rate5()*300, metric.Rate5()),
|
|
|
|
"Avg15Min": format(metric.Rate15()*900, metric.Rate15()),
|
2015-06-25 12:33:26 +00:00
|
|
|
"Overall": format(float64(metric.Count()), metric.RateMean()),
|
2015-06-24 11:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case metrics.Timer:
|
|
|
|
root[name] = map[string]interface{}{
|
|
|
|
"Avg01Min": format(metric.Rate1()*60, metric.Rate1()),
|
|
|
|
"Avg05Min": format(metric.Rate5()*300, metric.Rate5()),
|
|
|
|
"Avg15Min": format(metric.Rate15()*900, metric.Rate15()),
|
2015-06-25 12:33:26 +00:00
|
|
|
"Overall": format(float64(metric.Count()), metric.RateMean()),
|
2015-06-24 11:38:58 +00:00
|
|
|
"Maximum": time.Duration(metric.Max()).String(),
|
|
|
|
"Minimum": time.Duration(metric.Min()).String(),
|
2015-06-24 15:30:00 +00:00
|
|
|
"Percentiles": map[string]interface{}{
|
2015-06-25 12:33:26 +00:00
|
|
|
"5": time.Duration(metric.Percentile(0.05)).String(),
|
2015-06-24 11:38:58 +00:00
|
|
|
"20": time.Duration(metric.Percentile(0.2)).String(),
|
|
|
|
"50": time.Duration(metric.Percentile(0.5)).String(),
|
|
|
|
"80": time.Duration(metric.Percentile(0.8)).String(),
|
|
|
|
"95": time.Duration(metric.Percentile(0.95)).String(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
root[name] = "Unknown metric type"
|
2015-06-23 16:12:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return counters, nil
|
|
|
|
}
|