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 a00b06b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
34 changes: 17 additions & 17 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 @@ -207,7 +207,7 @@ type Result struct {
func (vm *VM) Execute(ctx HostContext, rev Revision,
kind CallKind, static bool, depth int, gas int64,
recipient Address, sender Address, input []byte, value Hash,
code []byte) (res Result, err error) {
code []byte, hcSlot int) (res Result, err error) {

flags := C.uint32_t(0)
if static {
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 a00b06b

Please sign in to comment.