mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 13:16:42 +00:00
added debug api
This commit is contained in:
parent
21461acd1b
commit
1768d51244
2 changed files with 203 additions and 0 deletions
154
rpc/api/debug.go
Normal file
154
rpc/api/debug.go
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ethereum/ethash"
|
||||
"github.com/ethereum/go-ethereum/core/state"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
ethereum "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"
|
||||
)
|
||||
|
||||
const (
|
||||
DebugVersion = "1.0.0"
|
||||
)
|
||||
|
||||
var (
|
||||
// mapping between methods and handlers
|
||||
DebugMapping = map[string]debughandler{
|
||||
"debug_dumpBlock": (*debug).DumpBlock,
|
||||
"debug_getBlockRlp": (*debug).GetBlockRlp,
|
||||
"debug_printBlock": (*debug).PrintBlock,
|
||||
"debug_processBlock": (*debug).ProcessBlock,
|
||||
"debug_seedHash": (*debug).SeedHash,
|
||||
"debug_setHead": (*debug).SetHead,
|
||||
}
|
||||
)
|
||||
|
||||
// debug callback handler
|
||||
type debughandler func(*debug, *shared.Request) (interface{}, error)
|
||||
|
||||
// admin api provider
|
||||
type debug struct {
|
||||
xeth *xeth.XEth
|
||||
ethereum ethereum.Ethereum
|
||||
methods map[string]debughandler
|
||||
codec codec.ApiCoder
|
||||
}
|
||||
|
||||
// create a new debug api instance
|
||||
func NewDebug(xeth *xeth.XEth, ethereum ethereum.Ethereum, coder codec.Codec) *debug {
|
||||
return &debug{
|
||||
xeth: xeth,
|
||||
ethereum: ethereum,
|
||||
methods: DebugMapping,
|
||||
codec: coder.New(nil),
|
||||
}
|
||||
}
|
||||
|
||||
// collection with supported methods
|
||||
func (self *debug) Methods() []string {
|
||||
methods := make([]string, len(self.methods))
|
||||
i := 0
|
||||
for k := range self.methods {
|
||||
methods[i] = k
|
||||
i++
|
||||
}
|
||||
return methods
|
||||
}
|
||||
|
||||
func (self *debug) PrintBlock(req *shared.Request) (interface{}, error) {
|
||||
args := new(BlockNumArg)
|
||||
if err := self.codec.Decode(req.Params, &args); err != nil {
|
||||
return nil, shared.NewDecodeParamError(err.Error())
|
||||
}
|
||||
|
||||
return self.xeth.EthBlockByNumber(args.BlockNumber), nil
|
||||
}
|
||||
|
||||
func (self *debug) DumpBlock(req *shared.Request) (interface{}, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
stateDb := state.New(block.Root(), self.ethereum.StateDb())
|
||||
if stateDb == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return stateDb.Dump(), nil
|
||||
}
|
||||
|
||||
func (self *debug) GetBlockRlp(req *shared.Request) (interface{}, error) {
|
||||
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)
|
||||
}
|
||||
return rlp.EncodeToBytes(block)
|
||||
}
|
||||
|
||||
func (self *debug) SetHead(req *shared.Request) (interface{}, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
self.ethereum.ChainManager().SetHead(block)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (self *debug) ProcessBlock(req *shared.Request) (interface{}, error) {
|
||||
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
|
||||
|
||||
_, err := self.ethereum.BlockProcessor().RetryProcess(block)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func (self *debug) SeedHash(req *shared.Request) (interface{}, error) {
|
||||
args := new(SeedHashArgs)
|
||||
if err := self.codec.Decode(req.Params, &args); err != nil {
|
||||
return nil, shared.NewDecodeParamError(err.Error())
|
||||
}
|
||||
|
||||
if hash, err := ethash.GetSeedHash(args.Number); err == nil {
|
||||
return fmt.Sprintf("0x%x", hash), nil
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
49
rpc/api/debug_args.go
Normal file
49
rpc/api/debug_args.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package api
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/ethereum/go-ethereum/rpc/shared"
|
||||
"math/big"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type SeedHashArgs struct {
|
||||
Number uint64 `json:"number"`
|
||||
}
|
||||
|
||||
type WaitForBlockArgs struct {
|
||||
MinHeight int `json:"minHeight"`
|
||||
Timeout int `json:"timeout"` // in seconds
|
||||
}
|
||||
|
||||
func (args *WaitForBlockArgs) UnmarshalJSON(b []byte) (err error) {
|
||||
var obj []interface{}
|
||||
if err := json.Unmarshal(b, &obj); err != nil {
|
||||
return shared.NewDecodeParamError(err.Error())
|
||||
}
|
||||
|
||||
if len(obj) > 2 {
|
||||
return fmt.Errorf("waitForArgs needs 0, 1, 2 arguments")
|
||||
}
|
||||
|
||||
// default values when not provided
|
||||
args.MinHeight = -1
|
||||
args.Timeout = -1
|
||||
|
||||
if len(obj) >= 1 {
|
||||
var minHeight *big.Int
|
||||
if minHeight, err = numString(obj[0]); err != nil {
|
||||
return err
|
||||
}
|
||||
args.MinHeight = int(minHeight.Int64())
|
||||
}
|
||||
|
||||
if len(obj) >= 2 {
|
||||
timeout, err := numString(obj[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args.Timeout = int(timeout.Int64())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
Reference in a new issue