@@ -116,8 +116,11 @@ const (
116
116
LatestStableRevision Revision = C .EVMC_LATEST_STABLE_REVISION
117
117
)
118
118
119
+ const HostContextCap = 20000
120
+
119
121
type VM struct {
120
- handle * C.struct_evmc_vm
122
+ handle * C.struct_evmc_vm
123
+ hostContexts []HostContext
121
124
}
122
125
123
126
func Load (filename string ) (vm * VM , err error ) {
@@ -127,7 +130,7 @@ func Load(filename string) (vm *VM, err error) {
127
130
C .free (unsafe .Pointer (cfilename ))
128
131
129
132
if loaderErr == C .EVMC_LOADER_SUCCESS {
130
- vm = & VM {handle }
133
+ vm = & VM {handle , make ([] HostContext , HostContextCap ) }
131
134
} else {
132
135
errMsg := C .evmc_last_error_msg ()
133
136
if errMsg != nil {
@@ -147,7 +150,7 @@ func LoadAndConfigure(config string) (vm *VM, err error) {
147
150
C .free (unsafe .Pointer (cconfig ))
148
151
149
152
if loaderErr == C .EVMC_LOADER_SUCCESS {
150
- vm = & VM {handle }
153
+ vm = & VM {handle , make ([] HostContext , HostContextCap ) }
151
154
} else {
152
155
errMsg := C .evmc_last_error_msg ()
153
156
if errMsg != nil {
@@ -214,7 +217,7 @@ func (vm *VM) Execute(ctx HostContext, rev Revision,
214
217
flags |= C .EVMC_STATIC
215
218
}
216
219
217
- ctxId := addHostContext (ctx )
220
+ ctxId := vm . addHostContext (ctx )
218
221
// FIXME: Clarify passing by pointer vs passing by value.
219
222
evmcRecipient := evmcAddress (recipient )
220
223
evmcSender := evmcAddress (sender )
@@ -223,7 +226,6 @@ func (vm *VM) Execute(ctx HostContext, rev Revision,
223
226
C .enum_evmc_call_kind (kind ), flags , C .int32_t (depth ), C .int64_t (gas ),
224
227
& evmcRecipient , & evmcSender , bytesPtr (input ), C .size_t (len (input )), & evmcValue ,
225
228
bytesPtr (code ), C .size_t (len (code )))
226
- removeHostContext (ctxId )
227
229
228
230
res := ctx .GetResult ()
229
231
res .Output = C .GoBytes (unsafe .Pointer (result .output_data ), C .int (result .output_size ))
@@ -240,22 +242,13 @@ func (vm *VM) Execute(ctx HostContext, rev Revision,
240
242
return err
241
243
}
242
244
243
- var (
244
- hostContextCounter uintptr
245
-
246
- histContextSlots = make ([]HostContext , 20000 )
247
- )
248
-
249
- func addHostContext (ctx HostContext ) uintptr {
245
+ func (vm * VM ) addHostContext (ctx HostContext ) uintptr {
250
246
idx := ctx .GetTransactionIndex ()
251
- if idx >= len (histContextSlots ) {
247
+ if idx >= len (vm . hostContexts ) {
252
248
panic (fmt .Sprintf ("received more than 20000 transactions in a block: %d" , idx ))
253
249
}
254
- histContextSlots [idx ] = ctx
255
- return uintptr (unsafe .Pointer (& histContextSlots [idx ]))
256
- }
257
-
258
- func removeHostContext (id uintptr ) {
250
+ vm .hostContexts [idx ] = ctx
251
+ return uintptr (unsafe .Pointer (& vm .hostContexts [idx ]))
259
252
}
260
253
261
254
func getHostContext (idx uintptr ) HostContext {
0 commit comments