forked from alpe/cosmos-tracing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasmvm.go
282 lines (249 loc) · 11.9 KB
/
wasmvm.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package tracing
import (
"encoding/hex"
"fmt"
"github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
cosmwasm "github.com/CosmWasm/wasmvm"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/opentracing/opentracing-go"
)
var _ wasmtypes.WasmEngine = TraceWasmVm{}
type TraceWasmVm struct {
other wasmtypes.WasmEngine
}
// NewTraceWasmVm constructor
func NewTraceWasmVm(other wasmtypes.WasmEngine) wasmtypes.WasmEngine {
if !tracerEnabled {
return other
}
return &TraceWasmVm{other: other}
}
func (t TraceWasmVm) Create(code cosmwasm.WasmCode) (cosmwasm.Checksum, error) {
return t.other.StoreCode(code)
}
func (t TraceWasmVm) AnalyzeCode(checksum cosmwasm.Checksum) (*wasmvmtypes.AnalysisReport, error) {
return t.other.AnalyzeCode(checksum)
}
func (t TraceWasmVm) Instantiate(checksum cosmwasm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, initMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (resp *wasmvmtypes.Response, gasUsed uint64, err error) {
return wasmvmDoWithTracing(
"wasmvm_instantiate",
querier,
ContractJsonInputMsgRecorder(initMsg),
ContractVmResponseRecorder(),
func() (resp *wasmvmtypes.Response, gasUsed uint64, err error) {
return t.other.Instantiate(checksum, env, info, initMsg, store, goapi, querier, gasMeter, gasLimit, deserCost)
},
)
}
func (t TraceWasmVm) Execute(checksum cosmwasm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, executeMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (resp *wasmvmtypes.Response, gasUsed uint64, err error) {
return wasmvmDoWithTracing(
"wasmvm_execute",
querier,
ContractJsonInputMsgRecorder(executeMsg),
ContractVmResponseRecorder(),
func() (resp *wasmvmtypes.Response, gasUsed uint64, err error) {
return t.other.Execute(checksum, env, info, executeMsg, store, goapi, querier, gasMeter, gasLimit, deserCost)
},
)
}
func (t TraceWasmVm) Query(checksum cosmwasm.Checksum, env wasmvmtypes.Env, queryMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (resp []byte, gasUsed uint64, err error) {
rootCtx := fetchCtx(querier)
DoWithTracing(rootCtx, "wasmvm_query", all, func(workCtx sdk.Context, span opentracing.Span) error {
span.LogFields(safeLogField(logRawQueryData, string(queryMsg)))
resp, gasUsed, err = t.other.Query(checksum, env, queryMsg, store, goapi, querier, gasMeter, gasLimit, deserCost)
if err == nil && resp != nil {
span.LogFields(safeLogField(logRawQueryResult, string(resp)))
}
return err
})
return
}
// fetch current sdk context, hack
func fetchCtx(querier cosmwasm.Querier) sdk.Context {
switch q := querier.(type) {
case *keeper.QueryHandler:
return q.Ctx
case keeper.QueryHandler:
return q.Ctx
default:
fmt.Printf("+++++: UNSUPPORTED QUERY HANDLER: %T\n", querier)
return sdk.Context{}
}
}
func (t TraceWasmVm) Migrate(checksum cosmwasm.Checksum, env wasmvmtypes.Env, migrateMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (resp *wasmvmtypes.Response, gasUsed uint64, err error) {
return wasmvmDoWithTracing(
"wasmvm_migrate",
querier,
ContractJsonInputMsgRecorder(migrateMsg),
ContractVmResponseRecorder(),
func() (resp *wasmvmtypes.Response, gasUsed uint64, err error) {
return t.other.Migrate(checksum, env, migrateMsg, store, goapi, querier, gasMeter, gasLimit, deserCost)
},
)
}
func (t TraceWasmVm) Sudo(checksum cosmwasm.Checksum, env wasmvmtypes.Env, sudoMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (resp *wasmvmtypes.Response, gasUsed uint64, err error) {
return wasmvmDoWithTracing(
"wasmvm_sudo",
querier,
ContractJsonInputMsgRecorder(sudoMsg),
ContractVmResponseRecorder(),
func() (resp *wasmvmtypes.Response, gasUsed uint64, err error) {
return t.other.Sudo(checksum, env, sudoMsg, store, goapi, querier, gasMeter, gasLimit, deserCost)
},
)
}
func (t TraceWasmVm) Reply(checksum cosmwasm.Checksum, env wasmvmtypes.Env, reply wasmvmtypes.Reply, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (resp *wasmvmtypes.Response, gasUsed uint64, err error) {
return wasmvmDoWithTracing(
"wasmvm_reply",
querier,
ContractGenericInputMsgRecorder(reply),
ContractVmResponseRecorder(),
func() (resp *wasmvmtypes.Response, gasUsed uint64, err error) {
return t.other.Reply(checksum, env, reply, store, goapi, querier, gasMeter, gasLimit, deserCost)
},
)
}
func (t TraceWasmVm) GetCode(checksum cosmwasm.Checksum) (cosmwasm.WasmCode, error) {
return t.other.GetCode(checksum)
}
func (t TraceWasmVm) Cleanup() {
t.other.Cleanup()
}
func (t TraceWasmVm) IBCChannelOpen(checksum cosmwasm.Checksum, env wasmvmtypes.Env, channel wasmvmtypes.IBCChannelOpenMsg, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBC3ChannelOpenResponse, uint64, error) {
return wasmvmDoWithTracing(
"wasmvm_chan_open",
querier,
ContractGenericInputMsgRecorder(channel),
ContractIBCChannelOpenResponseRecorder(),
func() (resp *wasmvmtypes.IBC3ChannelOpenResponse, gasUsed uint64, err error) {
return t.other.IBCChannelOpen(checksum, env, channel, store, goapi, querier, gasMeter, gasLimit, deserCost)
},
)
}
func (t TraceWasmVm) IBCChannelConnect(checksum cosmwasm.Checksum, env wasmvmtypes.Env, channel wasmvmtypes.IBCChannelConnectMsg, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBCBasicResponse, uint64, error) {
return wasmvmDoWithTracing(
"wasmvm_chan_connect",
querier,
ContractGenericInputMsgRecorder(channel),
ContractGenericResponseRecorder[wasmvmtypes.IBCBasicResponse](),
func() (resp *wasmvmtypes.IBCBasicResponse, gasUsed uint64, err error) {
return t.other.IBCChannelConnect(checksum, env, channel, store, goapi, querier, gasMeter, gasLimit, deserCost)
},
)
}
func (t TraceWasmVm) IBCChannelClose(checksum cosmwasm.Checksum, env wasmvmtypes.Env, channel wasmvmtypes.IBCChannelCloseMsg, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBCBasicResponse, uint64, error) {
return wasmvmDoWithTracing(
"wasmvm_chan_close",
querier,
ContractGenericInputMsgRecorder(channel),
ContractGenericResponseRecorder[wasmvmtypes.IBCBasicResponse](),
func() (resp *wasmvmtypes.IBCBasicResponse, gasUsed uint64, err error) {
return t.other.IBCChannelClose(checksum, env, channel, store, goapi, querier, gasMeter, gasLimit, deserCost)
},
)
}
func (t TraceWasmVm) IBCPacketReceive(checksum cosmwasm.Checksum, env wasmvmtypes.Env, packet wasmvmtypes.IBCPacketReceiveMsg, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBCReceiveResult, uint64, error) {
return wasmvmDoWithTracing(
"wasmvm_pkg_recv",
querier,
ContractGenericInputMsgRecorder(packet),
ContractGenericResponseRecorder[wasmvmtypes.IBCReceiveResult](),
func() (resp *wasmvmtypes.IBCReceiveResult, gasUsed uint64, err error) {
return t.other.IBCPacketReceive(checksum, env, packet, store, goapi, querier, gasMeter, gasLimit, deserCost)
},
)
}
func (t TraceWasmVm) IBCPacketAck(checksum cosmwasm.Checksum, env wasmvmtypes.Env, ack wasmvmtypes.IBCPacketAckMsg, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBCBasicResponse, uint64, error) {
return wasmvmDoWithTracing(
"wasmvm_pkg_ack",
querier,
ContractGenericInputMsgRecorder(ack),
ContractGenericResponseRecorder[wasmvmtypes.IBCBasicResponse](),
func() (*wasmvmtypes.IBCBasicResponse, uint64, error) {
return t.other.IBCPacketAck(checksum, env, ack, store, goapi, querier, gasMeter, gasLimit, deserCost)
},
)
}
func (t TraceWasmVm) IBCPacketTimeout(checksum cosmwasm.Checksum, env wasmvmtypes.Env, packet wasmvmtypes.IBCPacketTimeoutMsg, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBCBasicResponse, uint64, error) {
return wasmvmDoWithTracing(
"wasmvm_pkg_timeout",
querier,
ContractGenericInputMsgRecorder(packet),
ContractGenericResponseRecorder[wasmvmtypes.IBCBasicResponse](),
func() (resp *wasmvmtypes.IBCBasicResponse, gasUsed uint64, err error) {
return t.other.IBCPacketTimeout(checksum, env, packet, store, goapi, querier, gasMeter, gasLimit, deserCost)
},
)
}
func (t TraceWasmVm) Pin(checksum cosmwasm.Checksum) error {
return t.other.Pin(checksum)
}
func (t TraceWasmVm) Unpin(checksum cosmwasm.Checksum) error {
return t.other.Unpin(checksum)
}
func (t TraceWasmVm) GetMetrics() (*wasmvmtypes.Metrics, error) {
return t.other.GetMetrics()
}
func (t TraceWasmVm) StoreCode(code cosmwasm.WasmCode) (cosmwasm.Checksum, error) {
return t.other.StoreCode(code)
}
func (t TraceWasmVm) StoreCodeUnchecked(code cosmwasm.WasmCode) (cosmwasm.Checksum, error) {
return t.other.StoreCodeUnchecked(code)
}
func ContractJsonInputMsgRecorder(msg []byte) func(span opentracing.Span) {
return func(span opentracing.Span) {
span.LogFields(safeLogField(logRawContractMsg, string(msg)))
}
}
func ContractGenericInputMsgRecorder(obj interface{}) func(opentracing.Span) {
return func(span opentracing.Span) {
span.LogFields(safeLogField(logRawContractMsg, toJson(obj)))
}
}
func ContractVmResponseRecorder() func(opentracing.Span, *wasmvmtypes.Response) {
return func(span opentracing.Span, resp *wasmvmtypes.Response) {
span.LogFields(safeLogField(logRawResponseMsg, toJson(resp)))
for i, v := range resp.Messages {
span.LogFields(safeLogField(fmt.Sprintf("%s_%d", logRawSubMsg, i), toJson(v)))
}
span.LogFields(safeLogField(logRawResponseData, hex.EncodeToString(resp.Data)))
span.LogFields(safeLogField(logRawResponseEvents, toJson(resp.Events)))
}
}
func ContractIBCChannelOpenResponseRecorder() func(opentracing.Span, *wasmvmtypes.IBC3ChannelOpenResponse) {
return func(span opentracing.Span, resp *wasmvmtypes.IBC3ChannelOpenResponse) {
span.LogFields(safeLogField(logIBCVersion, resp.Version))
}
}
// ContractGenericResponseRecorder logs response as json which gives a much better output than log.Object
func ContractGenericResponseRecorder[T any]() func(opentracing.Span, *T) {
return func(span opentracing.Span, resp *T) {
span.LogFields(safeLogField(logRawResponseMsg, toJson(resp)))
}
}
// generic helper function to execute the callback in a tracing context
// customized input/response tracers are used to log type specific data
func wasmvmDoWithTracing[T wasmvmtypes.Response | wasmvmtypes.IBCBasicResponse | wasmvmtypes.IBC3ChannelOpenResponse | wasmvmtypes.IBCReceiveResult](
name string,
querier cosmwasm.Querier,
inputTracer func(opentracing.Span),
responseTracer func(opentracing.Span, *T),
cb func() (*T, uint64, error),
) (resp *T, gasUsed uint64, err error) {
rootCtx := fetchCtx(querier)
if rootCtx.IsZero() {
fmt.Println("+++++ not tracing wasmvm call due to missing root context")
return cb()
}
DoWithTracing(rootCtx, name, all, func(workCtx sdk.Context, span opentracing.Span) error {
inputTracer(span)
resp, gasUsed, err = cb()
if err == nil && resp != nil {
responseTracer(span, resp)
}
return err
})
return
}