-
Notifications
You must be signed in to change notification settings - Fork 659
/
endpoints_debug.go
227 lines (195 loc) · 7.71 KB
/
endpoints_debug.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
package jsonrpc
import (
"context"
"encoding/hex"
"errors"
"fmt"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/state/runtime/instrumentation"
"github.com/ethereum/go-ethereum/common"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/jackc/pgx/v4"
)
// DebugEndpoints is the debug jsonrpc endpoint
type DebugEndpoints struct {
state types.StateInterface
txMan dbTxManager
}
type traceConfig struct {
DisableStorage bool `json:"disableStorage"`
DisableStack bool `json:"disableStack"`
EnableMemory bool `json:"enableMemory"`
EnableReturnData bool `json:"enableReturnData"`
Tracer *string `json:"tracer"`
}
// StructLogRes represents the debug trace information for each opcode
type StructLogRes struct {
Pc uint64 `json:"pc"`
Op string `json:"op"`
Gas uint64 `json:"gas"`
GasCost uint64 `json:"gasCost"`
Depth int `json:"depth"`
Error string `json:"error,omitempty"`
Stack *[]types.ArgBig `json:"stack,omitempty"`
Memory *[]string `json:"memory,omitempty"`
Storage *map[string]string `json:"storage,omitempty"`
RefundCounter uint64 `json:"refund,omitempty"`
}
type traceTransactionResponse struct {
Gas uint64 `json:"gas"`
Failed bool `json:"failed"`
ReturnValue interface{} `json:"returnValue"`
StructLogs []StructLogRes `json:"structLogs"`
}
type traceBlockTransactionResponse struct {
Result traceTransactionResponse `json:"result"`
}
// TraceTransaction creates a response for debug_traceTransaction request.
// See https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debugtracetransaction
func (d *DebugEndpoints) TraceTransaction(hash types.ArgHash, cfg *traceConfig) (interface{}, types.Error) {
return d.txMan.NewDbTxScope(d.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
return d.buildTraceTransaction(ctx, hash.Hash(), cfg, dbTx)
})
}
// TraceBlockByNumber creates a response for debug_traceBlockByNumber request.
// See https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debugtraceblockbynumber
func (d *DebugEndpoints) TraceBlockByNumber(number types.BlockNumber, cfg *traceConfig) (interface{}, types.Error) {
return d.txMan.NewDbTxScope(d.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
blockNumber, rpcErr := number.GetNumericBlockNumber(ctx, d.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
block, err := d.state.GetL2BlockByNumber(ctx, blockNumber, dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("block #%d not found", blockNumber))
} else if err == state.ErrNotFound {
return rpcErrorResponse(types.DefaultErrorCode, "failed to get block by number", err)
}
traces, rpcErr := d.buildTraceBlock(ctx, block.Transactions(), cfg, dbTx)
if err != nil {
return nil, rpcErr
}
return traces, nil
})
}
// TraceBlockByHash creates a response for debug_traceBlockByHash request.
// See https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debugtraceblockbyhash
func (d *DebugEndpoints) TraceBlockByHash(hash types.ArgHash, cfg *traceConfig) (interface{}, types.Error) {
return d.txMan.NewDbTxScope(d.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
block, err := d.state.GetL2BlockByHash(ctx, hash.Hash(), dbTx)
if errors.Is(err, state.ErrNotFound) {
return nil, types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("block %s not found", hash.Hash().String()))
} else if err == state.ErrNotFound {
return rpcErrorResponse(types.DefaultErrorCode, "failed to get block by hash", err)
}
traces, rpcErr := d.buildTraceBlock(ctx, block.Transactions(), cfg, dbTx)
if err != nil {
return nil, rpcErr
}
return traces, nil
})
}
func (d *DebugEndpoints) buildTraceBlock(ctx context.Context, txs []*ethTypes.Transaction, cfg *traceConfig, dbTx pgx.Tx) (interface{}, types.Error) {
traces := []traceBlockTransactionResponse{}
for _, tx := range txs {
traceTransaction, err := d.buildTraceTransaction(ctx, tx.Hash(), cfg, dbTx)
if err != nil {
errMsg := fmt.Sprintf("failed to get trace for transaction %v", tx.Hash().String())
return rpcErrorResponse(types.DefaultErrorCode, errMsg, err)
}
traceBlockTransaction := traceBlockTransactionResponse{
Result: traceTransaction.(traceTransactionResponse),
}
traces = append(traces, traceBlockTransaction)
}
return traces, nil
}
func (d *DebugEndpoints) buildTraceTransaction(ctx context.Context, hash common.Hash, cfg *traceConfig, dbTx pgx.Tx) (interface{}, types.Error) {
traceConfig := state.TraceConfig{}
if cfg != nil {
traceConfig.DisableStack = cfg.DisableStack
traceConfig.DisableStorage = cfg.DisableStorage
traceConfig.EnableMemory = cfg.EnableMemory
traceConfig.EnableReturnData = cfg.EnableReturnData
traceConfig.Tracer = cfg.Tracer
}
result, err := d.state.DebugTransaction(ctx, hash, traceConfig, dbTx)
if errors.Is(err, state.ErrNotFound) {
return rpcErrorResponse(types.DefaultErrorCode, "transaction not found", nil)
} else if err != nil {
const errorMessage = "failed to get trace"
log.Infof("%v: %v", errorMessage, err)
return nil, types.NewRPCError(types.DefaultErrorCode, errorMessage)
}
if traceConfig.Tracer != nil && *traceConfig.Tracer != "" && len(result.ExecutorTraceResult) > 0 {
return result.ExecutorTraceResult, nil
}
failed := result.Failed()
var returnValue interface{}
if traceConfig.EnableReturnData {
returnValue = common.Bytes2Hex(result.ReturnValue)
}
structLogs := d.buildStructLogs(result.StructLogs, cfg)
resp := traceTransactionResponse{
Gas: result.GasUsed,
Failed: failed,
ReturnValue: returnValue,
StructLogs: structLogs,
}
return resp, nil
}
func (d *DebugEndpoints) buildStructLogs(stateStructLogs []instrumentation.StructLog, cfg *traceConfig) []StructLogRes {
structLogs := make([]StructLogRes, 0, len(stateStructLogs))
for _, structLog := range stateStructLogs {
errRes := ""
if structLog.Err != nil {
errRes = structLog.Err.Error()
}
op := structLog.Op
if op == "SHA3" {
op = "KECCAK256"
}
structLogRes := StructLogRes{
Pc: structLog.Pc,
Op: op,
Gas: structLog.Gas,
GasCost: structLog.GasCost,
Depth: structLog.Depth,
Error: errRes,
RefundCounter: structLog.RefundCounter,
}
stack := make([]types.ArgBig, 0, len(structLog.Stack))
if !cfg.DisableStack && len(structLog.Stack) > 0 {
for _, stackItem := range structLog.Stack {
if stackItem != nil {
stack = append(stack, types.ArgBig(*stackItem))
}
}
}
structLogRes.Stack = &stack
const memoryChunkSize = 32
memory := make([]string, 0, len(structLog.Memory))
if cfg.EnableMemory {
for i := 0; i < len(structLog.Memory); i = i + memoryChunkSize {
slice32Bytes := make([]byte, memoryChunkSize)
copy(slice32Bytes, structLog.Memory[i:i+memoryChunkSize])
memoryStringItem := hex.EncodeToString(slice32Bytes)
memory = append(memory, memoryStringItem)
}
}
structLogRes.Memory = &memory
if !cfg.DisableStorage && len(structLog.Storage) > 0 {
storage := make(map[string]string, len(structLog.Storage))
for storageKey, storageValue := range structLog.Storage {
k := hex.EncodeToString(storageKey.Bytes())
v := hex.EncodeToString(storageValue.Bytes())
storage[k] = v
}
structLogRes.Storage = &storage
}
structLogs = append(structLogs, structLogRes)
}
return structLogs
}