Skip to content

Commit

Permalink
use array for host contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Sep 14, 2024
1 parent 5a66578 commit 033dbb6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
32 changes: 16 additions & 16 deletions bindings/go/evmc/evmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import "C"

import (
"fmt"
"sync"
"reflect"
"unsafe"
)

Expand Down Expand Up @@ -241,30 +241,30 @@ func (vm *VM) Execute(ctx HostContext, rev Revision,

var (
hostContextCounter uintptr
hostContextMap = map[uintptr]HostContext{}
hostContextMapMu sync.Mutex

histContextSlots = make([]HostContext, 5000)
)

func addHostContext(ctx HostContext) uintptr {
hostContextMapMu.Lock()
id := hostContextCounter
hostContextCounter++
hostContextMap[id] = ctx
hostContextMapMu.Unlock()
return id
idx := ctx.GetTransactionIndex()
if idx >= len(histContextSlots) {
panic("received more than 5000 transactions in a block")
}
histContextSlots[idx] = ctx
return uintptr(unsafe.Pointer(&histContextSlots[idx]))
}

func removeHostContext(id uintptr) {
hostContextMapMu.Lock()
delete(hostContextMap, id)
hostContextMapMu.Unlock()
}

func getHostContext(idx uintptr) HostContext {
hostContextMapMu.Lock()
ctx := hostContextMap[idx]
hostContextMapMu.Unlock()
return ctx
sh := &reflect.SliceHeader{
Data: idx,
Len: 1,
Cap: 1,
}
data := *(*[]HostContext)(unsafe.Pointer(sh))
return data[0]
}

func evmcBytes32(in Hash) C.evmc_bytes32 {
Expand Down
1 change: 1 addition & 0 deletions bindings/go/evmc/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type HostContext interface {
AccessStorage(addr Address, key Hash) AccessStatus
GetTransientStorage(addr Address, key Hash) Hash
SetTransientStorage(addr Address, key Hash, value Hash)
GetTransactionIndex() int
}

//export accountExists
Expand Down

0 comments on commit 033dbb6

Please sign in to comment.