From 09d0d55fc579701191ff34f38cc20b437ee23577 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Tue, 9 Jun 2015 09:48:18 +0200 Subject: added debug API --- rpc/api/debug.go | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 rpc/api/debug.go (limited to 'rpc/api/debug.go') diff --git a/rpc/api/debug.go b/rpc/api/debug.go new file mode 100644 index 000000000..26f43fe74 --- /dev/null +++ b/rpc/api/debug.go @@ -0,0 +1,169 @@ +package api + +import ( + "fmt" + + "github.com/ethereum/ethash" + "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" +) + +const ( + DebugVersion = "1.0.0" +) + +var ( + // mapping between methods and handlers + DebugMapping = map[string]debughandler{ + "debug_dumpBlock": (*DebugApi).DumpBlock, + "debug_getBlockRlp": (*DebugApi).GetBlockRlp, + "debug_printBlock": (*DebugApi).PrintBlock, + "debug_processBlock": (*DebugApi).ProcessBlock, + "debug_seedHash": (*DebugApi).SeedHash, + "debug_setHead": (*DebugApi).SetHead, + } +) + +// debug callback handler +type debughandler func(*DebugApi, *shared.Request) (interface{}, error) + +// admin api provider +type DebugApi struct { + xeth *xeth.XEth + ethereum *eth.Ethereum + methods map[string]debughandler + codec codec.ApiCoder +} + +// create a new debug api instance +func NewDebugApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *DebugApi { + return &DebugApi{ + xeth: xeth, + ethereum: ethereum, + methods: DebugMapping, + codec: coder.New(nil), + } +} + +// collection with supported methods +func (self *DebugApi) Methods() []string { + methods := make([]string, len(self.methods)) + i := 0 + for k := range self.methods { + methods[i] = k + i++ + } + return methods +} + +// Execute given request +func (self *DebugApi) Execute(req *shared.Request) (interface{}, error) { + if callback, ok := self.methods[req.Method]; ok { + return callback(self, req) + } + + return nil, &shared.NotImplementedError{req.Method} +} + +func (self *DebugApi) Name() string { + return DebugApiName +} + +func (self *DebugApi) 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()) + } + + block := self.xeth.EthBlockByNumber(args.BlockNumber) + return fmt.Sprintf("%s", block), nil +} + +func (self *DebugApi) 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 *DebugApi) 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) + } + encoded, err := rlp.EncodeToBytes(block) + return fmt.Sprintf("%x", encoded), err +} + +func (self *DebugApi) 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 *DebugApi) 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 *DebugApi) SeedHash(req *shared.Request) (interface{}, error) { + 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 + } +} -- cgit v1.2.3 From cc9ae399338557b6671e8fc83bb696c5ddb068fe Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Tue, 9 Jun 2015 16:06:51 +0200 Subject: added admin API --- rpc/api/debug.go | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'rpc/api/debug.go') diff --git a/rpc/api/debug.go b/rpc/api/debug.go index 26f43fe74..2930ad870 100644 --- a/rpc/api/debug.go +++ b/rpc/api/debug.go @@ -20,20 +20,20 @@ const ( var ( // mapping between methods and handlers DebugMapping = map[string]debughandler{ - "debug_dumpBlock": (*DebugApi).DumpBlock, - "debug_getBlockRlp": (*DebugApi).GetBlockRlp, - "debug_printBlock": (*DebugApi).PrintBlock, - "debug_processBlock": (*DebugApi).ProcessBlock, - "debug_seedHash": (*DebugApi).SeedHash, - "debug_setHead": (*DebugApi).SetHead, + "debug_dumpBlock": (*debugApi).DumpBlock, + "debug_getBlockRlp": (*debugApi).GetBlockRlp, + "debug_printBlock": (*debugApi).PrintBlock, + "debug_processBlock": (*debugApi).ProcessBlock, + "debug_seedHash": (*debugApi).SeedHash, + "debug_setHead": (*debugApi).SetHead, } ) // debug callback handler -type debughandler func(*DebugApi, *shared.Request) (interface{}, error) +type debughandler func(*debugApi, *shared.Request) (interface{}, error) // admin api provider -type DebugApi struct { +type debugApi struct { xeth *xeth.XEth ethereum *eth.Ethereum methods map[string]debughandler @@ -41,8 +41,8 @@ type DebugApi struct { } // create a new debug api instance -func NewDebugApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *DebugApi { - return &DebugApi{ +func NewDebugApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *debugApi { + return &debugApi{ xeth: xeth, ethereum: ethereum, methods: DebugMapping, @@ -51,7 +51,7 @@ func NewDebugApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *De } // collection with supported methods -func (self *DebugApi) Methods() []string { +func (self *debugApi) Methods() []string { methods := make([]string, len(self.methods)) i := 0 for k := range self.methods { @@ -62,7 +62,7 @@ func (self *DebugApi) Methods() []string { } // Execute given request -func (self *DebugApi) Execute(req *shared.Request) (interface{}, error) { +func (self *debugApi) Execute(req *shared.Request) (interface{}, error) { if callback, ok := self.methods[req.Method]; ok { return callback(self, req) } @@ -70,11 +70,11 @@ func (self *DebugApi) Execute(req *shared.Request) (interface{}, error) { return nil, &shared.NotImplementedError{req.Method} } -func (self *DebugApi) Name() string { +func (self *debugApi) Name() string { return DebugApiName } -func (self *DebugApi) PrintBlock(req *shared.Request) (interface{}, error) { +func (self *debugApi) 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()) @@ -84,7 +84,7 @@ func (self *DebugApi) PrintBlock(req *shared.Request) (interface{}, error) { return fmt.Sprintf("%s", block), nil } -func (self *DebugApi) DumpBlock(req *shared.Request) (interface{}, error) { +func (self *debugApi) 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()) @@ -103,7 +103,7 @@ func (self *DebugApi) DumpBlock(req *shared.Request) (interface{}, error) { return stateDb.Dump(), nil } -func (self *DebugApi) GetBlockRlp(req *shared.Request) (interface{}, error) { +func (self *debugApi) 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()) @@ -117,7 +117,7 @@ func (self *DebugApi) GetBlockRlp(req *shared.Request) (interface{}, error) { return fmt.Sprintf("%x", encoded), err } -func (self *DebugApi) SetHead(req *shared.Request) (interface{}, error) { +func (self *debugApi) 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()) @@ -133,7 +133,7 @@ func (self *DebugApi) SetHead(req *shared.Request) (interface{}, error) { return nil, nil } -func (self *DebugApi) ProcessBlock(req *shared.Request) (interface{}, error) { +func (self *debugApi) 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()) @@ -155,7 +155,7 @@ func (self *DebugApi) ProcessBlock(req *shared.Request) (interface{}, error) { return false, err } -func (self *DebugApi) SeedHash(req *shared.Request) (interface{}, error) { +func (self *debugApi) SeedHash(req *shared.Request) (interface{}, error) { args := new(BlockNumArg) if err := self.codec.Decode(req.Params, &args); err != nil { return nil, shared.NewDecodeParamError(err.Error()) -- cgit v1.2.3 From 7584e68c21cfd155a9e72b29422d8d458691d4ae Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 10 Jun 2015 09:42:14 +0200 Subject: upgrade web3.js with _extend support --- rpc/api/debug.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'rpc/api/debug.go') diff --git a/rpc/api/debug.go b/rpc/api/debug.go index 2930ad870..5b6a449dc 100644 --- a/rpc/api/debug.go +++ b/rpc/api/debug.go @@ -14,7 +14,7 @@ import ( ) const ( - DebugVersion = "1.0.0" + DebugApiVersion = "1.0" ) var ( @@ -74,6 +74,10 @@ func (self *debugApi) Name() string { return DebugApiName } +func (self *debugApi) ApiVersion() string { + return DebugApiVersion +} + func (self *debugApi) PrintBlock(req *shared.Request) (interface{}, error) { args := new(BlockNumArg) if err := self.codec.Decode(req.Params, &args); err != nil { @@ -100,7 +104,7 @@ func (self *debugApi) DumpBlock(req *shared.Request) (interface{}, error) { return nil, nil } - return stateDb.Dump(), nil + return stateDb.RawDump(), nil } func (self *debugApi) GetBlockRlp(req *shared.Request) (interface{}, error) { -- cgit v1.2.3