Skip to content

Commit

Permalink
pass pointer to cgo.Handle with host context
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Oct 4, 2024
1 parent c5e2eb9 commit 8e1456b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ffi/athcon/bindings/go/athcon.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (vm *VM) Execute(
result := C.athcon_execute(
vm.handle,
hostInterface,
C.uintptr_t(ctxHandle),
(*C.struct_athcon_host_context)(unsafe.Pointer(&ctxHandle)),
uint32(rev),
&msg,
(*C.uint8_t)(unsafe.Pointer(&code[0])),
Expand Down
14 changes: 7 additions & 7 deletions ffi/athcon/bindings/go/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,31 @@ type HostContext interface {

//export accountExists
func accountExists(pCtx unsafe.Pointer, pAddr *C.athcon_address) C.bool {
ctx := cgo.Handle(pCtx).Value().(HostContext)
ctx := (*cgo.Handle)(pCtx).Value().(HostContext)
return C.bool(ctx.AccountExists(goAddress(*pAddr)))
}

//export getStorage
func getStorage(pCtx unsafe.Pointer, pAddr *C.athcon_address, pKey *C.athcon_bytes32) C.athcon_bytes32 {
ctx := cgo.Handle(pCtx).Value().(HostContext)
ctx := (*cgo.Handle)(pCtx).Value().(HostContext)
return athconBytes32(ctx.GetStorage(goAddress(*pAddr), goHash(*pKey)))
}

//export setStorage
func setStorage(pCtx unsafe.Pointer, pAddr *C.athcon_address, pKey *C.athcon_bytes32, pVal *C.athcon_bytes32) C.enum_athcon_storage_status {
ctx := cgo.Handle(pCtx).Value().(HostContext)
ctx := (*cgo.Handle)(pCtx).Value().(HostContext)
return C.enum_athcon_storage_status(ctx.SetStorage(goAddress(*pAddr), goHash(*pKey), goHash(*pVal)))
}

//export getBalance
func getBalance(pCtx unsafe.Pointer, pAddr *C.athcon_address) C.athcon_uint256be {
ctx := cgo.Handle(pCtx).Value().(HostContext)
ctx := (*cgo.Handle)(pCtx).Value().(HostContext)
return athconBytes32(ctx.GetBalance(goAddress(*pAddr)))
}

//export getTxContext
func getTxContext(pCtx unsafe.Pointer) C.struct_athcon_tx_context {
ctx := cgo.Handle(pCtx).Value().(HostContext)
ctx := (*cgo.Handle)(pCtx).Value().(HostContext)
txContext := ctx.GetTxContext()

return C.struct_athcon_tx_context{
Expand All @@ -128,13 +128,13 @@ func getTxContext(pCtx unsafe.Pointer) C.struct_athcon_tx_context {

//export getBlockHash
func getBlockHash(pCtx unsafe.Pointer, number int64) C.athcon_bytes32 {
ctx := cgo.Handle(pCtx).Value().(HostContext)
ctx := (*cgo.Handle)(pCtx).Value().(HostContext)
return athconBytes32(ctx.GetBlockHash(number))
}

//export call
func call(pCtx unsafe.Pointer, msg *C.struct_athcon_message) C.struct_athcon_result {
ctx := cgo.Handle(pCtx).Value().(HostContext)
ctx := (*cgo.Handle)(pCtx).Value().(HostContext)

kind := CallKind(msg.kind)
output, gasLeft, createAddr, err := ctx.Call(kind, goAddress(msg.recipient), goAddress(msg.sender), goHash(msg.value),
Expand Down
4 changes: 2 additions & 2 deletions ffi/athcon/include/athcon/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ extern "C"
*/
static inline struct athcon_result athcon_execute(struct athcon_vm *vm,
const struct athcon_host_interface *host,
uintptr_t context,
struct athcon_host_context* context,
enum athcon_revision rev,
const struct athcon_message *msg,
uint8_t const *code,
size_t code_size)
{

return vm->execute(vm, host, (struct athcon_host_context*)context, rev, msg, code, code_size);
return vm->execute(vm, host, context, rev, msg, code, code_size);
}

/// The athcon_result release function using free() for releasing the memory.
Expand Down

0 comments on commit 8e1456b

Please sign in to comment.