debug.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package api
  2. import (
  3. "fmt"
  4. "github.com/ethereum/ethash"
  5. "github.com/ethereum/go-ethereum/core/state"
  6. "github.com/ethereum/go-ethereum/core/vm"
  7. "github.com/ethereum/go-ethereum/eth"
  8. "github.com/ethereum/go-ethereum/rlp"
  9. "github.com/ethereum/go-ethereum/rpc/codec"
  10. "github.com/ethereum/go-ethereum/rpc/shared"
  11. "github.com/ethereum/go-ethereum/xeth"
  12. )
  13. const (
  14. DebugVersion = "1.0.0"
  15. )
  16. var (
  17. // mapping between methods and handlers
  18. DebugMapping = map[string]debughandler{
  19. "debug_dumpBlock": (*DebugApi).DumpBlock,
  20. "debug_getBlockRlp": (*DebugApi).GetBlockRlp,
  21. "debug_printBlock": (*DebugApi).PrintBlock,
  22. "debug_processBlock": (*DebugApi).ProcessBlock,
  23. "debug_seedHash": (*DebugApi).SeedHash,
  24. "debug_setHead": (*DebugApi).SetHead,
  25. }
  26. )
  27. // debug callback handler
  28. type debughandler func(*DebugApi, *shared.Request) (interface{}, error)
  29. // admin api provider
  30. type DebugApi struct {
  31. xeth *xeth.XEth
  32. ethereum *eth.Ethereum
  33. methods map[string]debughandler
  34. codec codec.ApiCoder
  35. }
  36. // create a new debug api instance
  37. func NewDebugApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *DebugApi {
  38. return &DebugApi{
  39. xeth: xeth,
  40. ethereum: ethereum,
  41. methods: DebugMapping,
  42. codec: coder.New(nil),
  43. }
  44. }
  45. // collection with supported methods
  46. func (self *DebugApi) Methods() []string {
  47. methods := make([]string, len(self.methods))
  48. i := 0
  49. for k := range self.methods {
  50. methods[i] = k
  51. i++
  52. }
  53. return methods
  54. }
  55. // Execute given request
  56. func (self *DebugApi) Execute(req *shared.Request) (interface{}, error) {
  57. if callback, ok := self.methods[req.Method]; ok {
  58. return callback(self, req)
  59. }
  60. return nil, &shared.NotImplementedError{req.Method}
  61. }
  62. func (self *DebugApi) Name() string {
  63. return DebugApiName
  64. }
  65. func (self *DebugApi) PrintBlock(req *shared.Request) (interface{}, error) {
  66. args := new(BlockNumArg)
  67. if err := self.codec.Decode(req.Params, &args); err != nil {
  68. return nil, shared.NewDecodeParamError(err.Error())
  69. }
  70. block := self.xeth.EthBlockByNumber(args.BlockNumber)
  71. return fmt.Sprintf("%s", block), nil
  72. }
  73. func (self *DebugApi) DumpBlock(req *shared.Request) (interface{}, error) {
  74. args := new(BlockNumArg)
  75. if err := self.codec.Decode(req.Params, &args); err != nil {
  76. return nil, shared.NewDecodeParamError(err.Error())
  77. }
  78. block := self.xeth.EthBlockByNumber(args.BlockNumber)
  79. if block == nil {
  80. return nil, fmt.Errorf("block #%d not found", args.BlockNumber)
  81. }
  82. stateDb := state.New(block.Root(), self.ethereum.StateDb())
  83. if stateDb == nil {
  84. return nil, nil
  85. }
  86. return stateDb.Dump(), nil
  87. }
  88. func (self *DebugApi) GetBlockRlp(req *shared.Request) (interface{}, error) {
  89. args := new(BlockNumArg)
  90. if err := self.codec.Decode(req.Params, &args); err != nil {
  91. return nil, shared.NewDecodeParamError(err.Error())
  92. }
  93. block := self.xeth.EthBlockByNumber(args.BlockNumber)
  94. if block == nil {
  95. return nil, fmt.Errorf("block #%d not found", args.BlockNumber)
  96. }
  97. encoded, err := rlp.EncodeToBytes(block)
  98. return fmt.Sprintf("%x", encoded), err
  99. }
  100. func (self *DebugApi) SetHead(req *shared.Request) (interface{}, error) {
  101. args := new(BlockNumArg)
  102. if err := self.codec.Decode(req.Params, &args); err != nil {
  103. return nil, shared.NewDecodeParamError(err.Error())
  104. }
  105. block := self.xeth.EthBlockByNumber(args.BlockNumber)
  106. if block == nil {
  107. return nil, fmt.Errorf("block #%d not found", args.BlockNumber)
  108. }
  109. self.ethereum.ChainManager().SetHead(block)
  110. return nil, nil
  111. }
  112. func (self *DebugApi) ProcessBlock(req *shared.Request) (interface{}, error) {
  113. args := new(BlockNumArg)
  114. if err := self.codec.Decode(req.Params, &args); err != nil {
  115. return nil, shared.NewDecodeParamError(err.Error())
  116. }
  117. block := self.xeth.EthBlockByNumber(args.BlockNumber)
  118. if block == nil {
  119. return nil, fmt.Errorf("block #%d not found", args.BlockNumber)
  120. }
  121. old := vm.Debug
  122. defer func() { vm.Debug = old }()
  123. vm.Debug = true
  124. _, err := self.ethereum.BlockProcessor().RetryProcess(block)
  125. if err == nil {
  126. return true, nil
  127. }
  128. return false, err
  129. }
  130. func (self *DebugApi) SeedHash(req *shared.Request) (interface{}, error) {
  131. args := new(BlockNumArg)
  132. if err := self.codec.Decode(req.Params, &args); err != nil {
  133. return nil, shared.NewDecodeParamError(err.Error())
  134. }
  135. if hash, err := ethash.GetSeedHash(uint64(args.BlockNumber)); err == nil {
  136. return fmt.Sprintf("0x%x", hash), nil
  137. } else {
  138. return nil, err
  139. }
  140. }