Skip to content

Commit 5d44e9e

Browse files
authored
Merge pull request #4 from sei-protocol/tony/refactor-global-vars
Refactor global variables into struct vars
2 parents b5351eb + 9724bbd commit 5d44e9e

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

Diff for: bindings/go/evmc/evmc.go

+11-18
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,11 @@ const (
116116
LatestStableRevision Revision = C.EVMC_LATEST_STABLE_REVISION
117117
)
118118

119+
const HostContextCap = 20000
120+
119121
type VM struct {
120-
handle *C.struct_evmc_vm
122+
handle *C.struct_evmc_vm
123+
hostContexts []HostContext
121124
}
122125

123126
func Load(filename string) (vm *VM, err error) {
@@ -127,7 +130,7 @@ func Load(filename string) (vm *VM, err error) {
127130
C.free(unsafe.Pointer(cfilename))
128131

129132
if loaderErr == C.EVMC_LOADER_SUCCESS {
130-
vm = &VM{handle}
133+
vm = &VM{handle, make([]HostContext, HostContextCap)}
131134
} else {
132135
errMsg := C.evmc_last_error_msg()
133136
if errMsg != nil {
@@ -147,7 +150,7 @@ func LoadAndConfigure(config string) (vm *VM, err error) {
147150
C.free(unsafe.Pointer(cconfig))
148151

149152
if loaderErr == C.EVMC_LOADER_SUCCESS {
150-
vm = &VM{handle}
153+
vm = &VM{handle, make([]HostContext, HostContextCap)}
151154
} else {
152155
errMsg := C.evmc_last_error_msg()
153156
if errMsg != nil {
@@ -214,7 +217,7 @@ func (vm *VM) Execute(ctx HostContext, rev Revision,
214217
flags |= C.EVMC_STATIC
215218
}
216219

217-
ctxId := addHostContext(ctx)
220+
ctxId := vm.addHostContext(ctx)
218221
// FIXME: Clarify passing by pointer vs passing by value.
219222
evmcRecipient := evmcAddress(recipient)
220223
evmcSender := evmcAddress(sender)
@@ -223,7 +226,6 @@ func (vm *VM) Execute(ctx HostContext, rev Revision,
223226
C.enum_evmc_call_kind(kind), flags, C.int32_t(depth), C.int64_t(gas),
224227
&evmcRecipient, &evmcSender, bytesPtr(input), C.size_t(len(input)), &evmcValue,
225228
bytesPtr(code), C.size_t(len(code)))
226-
removeHostContext(ctxId)
227229

228230
res := ctx.GetResult()
229231
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,
240242
return err
241243
}
242244

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 {
250246
idx := ctx.GetTransactionIndex()
251-
if idx >= len(histContextSlots) {
247+
if idx >= len(vm.hostContexts) {
252248
panic(fmt.Sprintf("received more than 20000 transactions in a block: %d", idx))
253249
}
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]))
259252
}
260253

261254
func getHostContext(idx uintptr) HostContext {

0 commit comments

Comments
 (0)