/
debug_endpoint.go
executable file
·236 lines (188 loc) · 5.55 KB
/
debug_endpoint.go
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package jsonrpc
import (
"context"
"errors"
"fmt"
"time"
"github.com/definenulls/komerco-chain/helper/hex"
"github.com/definenulls/komerco-chain/state/runtime/tracer"
"github.com/definenulls/komerco-chain/state/runtime/tracer/structtracer"
"github.com/definenulls/komerco-chain/types"
)
var (
defaultTraceTimeout = 5 * time.Second
// ErrExecutionTimeout indicates the execution was terminated due to timeout
ErrExecutionTimeout = errors.New("execution timeout")
// ErrTraceGenesisBlock is an error returned when tracing genesis block which can't be traced
ErrTraceGenesisBlock = errors.New("genesis is not traceable")
// ErrNoConfig is an error returns when config is empty
ErrNoConfig = errors.New("missing config object")
)
type debugBlockchainStore interface {
// Header returns the current header of the chain (genesis if empty)
Header() *types.Header
// GetHeaderByNumber gets a header using the provided number
GetHeaderByNumber(uint64) (*types.Header, bool)
// ReadTxLookup returns a block hash in which a given txn was mined
ReadTxLookup(txnHash types.Hash) (types.Hash, bool)
// GetBlockByHash gets a block using the provided hash
GetBlockByHash(hash types.Hash, full bool) (*types.Block, bool)
// GetBlockByNumber gets a block using the provided height
GetBlockByNumber(num uint64, full bool) (*types.Block, bool)
// TraceBlock traces all transactions in the given block
TraceBlock(*types.Block, tracer.Tracer) ([]interface{}, error)
// TraceTxn traces a transaction in the block, associated with the given hash
TraceTxn(*types.Block, types.Hash, tracer.Tracer) (interface{}, error)
// TraceCall traces a single call at the point when the given header is mined
TraceCall(*types.Transaction, *types.Header, tracer.Tracer) (interface{}, error)
}
type debugTxPoolStore interface {
GetNonce(types.Address) uint64
}
type debugStateStore interface {
GetAccount(root types.Hash, addr types.Address) (*Account, error)
}
type debugStore interface {
debugBlockchainStore
debugTxPoolStore
debugStateStore
}
// Debug is the debug jsonrpc endpoint
type Debug struct {
store debugStore
}
type TraceConfig struct {
EnableMemory bool `json:"enableMemory"`
DisableStack bool `json:"disableStack"`
DisableStorage bool `json:"disableStorage"`
EnableReturnData bool `json:"enableReturnData"`
Timeout *string `json:"timeout"`
}
func (d *Debug) TraceBlockByNumber(
blockNumber BlockNumber,
config *TraceConfig,
) (interface{}, error) {
num, err := GetNumericBlockNumber(blockNumber, d.store)
if err != nil {
return nil, err
}
block, ok := d.store.GetBlockByNumber(num, true)
if !ok {
return nil, fmt.Errorf("block %d not found", num)
}
return d.traceBlock(block, config)
}
func (d *Debug) TraceBlockByHash(
blockHash types.Hash,
config *TraceConfig,
) (interface{}, error) {
block, ok := d.store.GetBlockByHash(blockHash, true)
if !ok {
return nil, fmt.Errorf("block %s not found", blockHash)
}
return d.traceBlock(block, config)
}
func (d *Debug) TraceBlock(
input string,
config *TraceConfig,
) (interface{}, error) {
blockByte, decodeErr := hex.DecodeHex(input)
if decodeErr != nil {
return nil, fmt.Errorf("unable to decode block, %w", decodeErr)
}
block := &types.Block{}
if err := block.UnmarshalRLP(blockByte); err != nil {
return nil, err
}
return d.traceBlock(block, config)
}
func (d *Debug) TraceTransaction(
txHash types.Hash,
config *TraceConfig,
) (interface{}, error) {
tx, block := GetTxAndBlockByTxHash(txHash, d.store)
if tx == nil {
return nil, fmt.Errorf("tx %s not found", txHash.String())
}
if block.Number() == 0 {
return nil, ErrTraceGenesisBlock
}
tracer, cancel, err := newTracer(config)
if err != nil {
return nil, err
}
defer cancel()
return d.store.TraceTxn(block, tx.Hash, tracer)
}
func (d *Debug) TraceCall(
arg *txnArgs,
filter BlockNumberOrHash,
config *TraceConfig,
) (interface{}, error) {
header, err := GetHeaderFromBlockNumberOrHash(filter, d.store)
if err != nil {
return nil, ErrHeaderNotFound
}
tx, err := DecodeTxn(arg, d.store)
if err != nil {
return nil, err
}
// If the caller didn't supply the gas limit in the message, then we set it to maximum possible => block gas limit
if tx.Gas == 0 {
tx.Gas = header.GasLimit
}
tracer, cancel, err := newTracer(config)
defer cancel()
if err != nil {
return nil, err
}
return d.store.TraceCall(tx, header, tracer)
}
func (d *Debug) traceBlock(
block *types.Block,
config *TraceConfig,
) (interface{}, error) {
if block.Number() == 0 {
return nil, ErrTraceGenesisBlock
}
tracer, cancel, err := newTracer(config)
defer cancel()
if err != nil {
return nil, err
}
return d.store.TraceBlock(block, tracer)
}
// newTracer creates new tracer by config
func newTracer(config *TraceConfig) (
tracer.Tracer,
context.CancelFunc,
error,
) {
var (
timeout = defaultTraceTimeout
err error
)
if config == nil {
return nil, nil, ErrNoConfig
}
if config.Timeout != nil {
if timeout, err = time.ParseDuration(*config.Timeout); err != nil {
return nil, nil, err
}
}
tracer := structtracer.NewStructTracer(structtracer.Config{
EnableMemory: config.EnableMemory,
EnableStack: !config.DisableStack,
EnableStorage: !config.DisableStorage,
EnableReturnData: config.EnableReturnData,
})
timeoutCtx, cancel := context.WithTimeout(context.Background(), timeout)
go func() {
<-timeoutCtx.Done()
if errors.Is(timeoutCtx.Err(), context.DeadlineExceeded) {
tracer.Cancel(ErrExecutionTimeout)
}
}()
// cancellation of context is done by caller
return tracer, cancel, nil
}