-
Notifications
You must be signed in to change notification settings - Fork 180
/
tracer.go
180 lines (168 loc) · 4.15 KB
/
tracer.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
package types
import (
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
json "github.com/json-iterator/go"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
)
type TraceConfig struct {
// custom javascript tracer
Tracer string `json:"tracer"`
// disable stack capture
DisableStack bool `json:"disableStack"`
// disable storage capture
DisableStorage bool `json:"disableStorage"`
// print output during capture end
Debug bool `json:"debug"`
// enable memory capture
DisableMemory bool `json:"disableMemory"`
// enable return data capture
DisableReturnData bool `json:"disableReturnData"`
}
func GetTracerResult(tracer vm.Tracer, result *core.ExecutionResult) ([]byte, error) {
var (
res []byte
err error
)
// Depending on the tracer type, format and return the output
switch tracer := tracer.(type) {
case *vm.StructLogger:
// If the result contains a revert reason, return it.
returnVal := fmt.Sprintf("%x", result.Return())
if len(result.Revert()) > 0 {
returnVal = fmt.Sprintf("%x", result.Revert())
}
res, err = json.ConfigFastest.Marshal(&TraceExecutionResult{
Gas: result.UsedGas,
Failed: result.Failed(),
ReturnValue: returnVal,
StructLogs: FormatLogs(tracer.StructLogs()),
})
case *tracers.Tracer:
res, err = tracer.GetResult()
default:
res = []byte(fmt.Sprintf("bad tracer type %T", tracer))
}
return res, err
}
// NoOpTracer is an empty implementation of vm.Tracer interface
type NoOpTracer struct{}
// NewNoOpTracer creates a no-op vm.Tracer
func NewNoOpTracer() *NoOpTracer {
return &NoOpTracer{}
}
// CaptureStart implements vm.Tracer interface
func (dt NoOpTracer) CaptureStart(
env *vm.EVM,
from, to common.Address,
create bool,
input []byte,
gas uint64,
value *big.Int,
) {
}
// CaptureEnter implements vm.Tracer interface
func (dt NoOpTracer) CaptureEnter(
typ vm.OpCode,
from common.Address,
to common.Address,
input []byte,
gas uint64,
value *big.Int,
) {
}
// CaptureExit implements vm.Tracer interface
func (dt NoOpTracer) CaptureExit(output []byte, gasUsed uint64, err error) {}
// CaptureState implements vm.Tracer interface
func (dt NoOpTracer) CaptureState(
env *vm.EVM,
pc uint64,
op vm.OpCode,
gas, cost uint64,
scope *vm.ScopeContext,
rData []byte,
depth int,
err error,
) {
}
// CaptureFault implements vm.Tracer interface
func (dt NoOpTracer) CaptureFault(
env *vm.EVM,
pc uint64,
op vm.OpCode,
gas, cost uint64,
scope *vm.ScopeContext,
depth int,
err error,
) {
}
// CaptureEnd implements vm.Tracer interface
func (dt NoOpTracer) CaptureEnd(
output []byte,
gasUsed uint64,
t time.Duration,
err error,
) {
}
func defaultTracerConfig() *TraceConfig {
return &TraceConfig{
Tracer: "",
DisableMemory: false,
DisableStorage: false,
DisableStack: false,
DisableReturnData: false,
Debug: false,
}
}
func TestTracerConfig(traceConfig *TraceConfig) error {
if traceConfig.Tracer != "" {
_, err := tracers.New(traceConfig.Tracer, &tracers.Context{})
if err != nil {
return err
}
}
return nil
}
func newTracer(ctx sdk.Context, txHash *common.Hash) (tracer vm.Tracer) {
if ctx.IsTraceTxLog() {
var err error
configBytes := ctx.TraceTxLogConfig()
traceConfig := &TraceConfig{}
if configBytes == nil {
traceConfig = defaultTracerConfig()
} else {
err = json.Unmarshal(configBytes, traceConfig)
if err != nil {
return NewNoOpTracer()
}
}
if traceConfig.Tracer == "" {
//Basic tracer with config
logConfig := vm.LogConfig{
DisableMemory: traceConfig.DisableMemory,
DisableStorage: traceConfig.DisableStorage,
DisableStack: traceConfig.DisableStack,
DisableReturnData: traceConfig.DisableReturnData,
Debug: traceConfig.Debug,
}
return vm.NewStructLogger(&logConfig)
}
// Json-based tracer
tCtx := &tracers.Context{
TxHash: *txHash,
}
tracer, err = tracers.New(traceConfig.Tracer, tCtx)
if err != nil {
return NewNoOpTracer()
}
return tracer
} else {
//no op tracer
return NewNoOpTracer()
}
}