aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/api/debug.go
blob: b451d8662489aa7f6eaf00c80e3b08f5887d7462 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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 (
    DebugApiVersion = "1.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 shared.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 {
        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.RawDump(), 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
    }
}