Skip to content

Commit

Permalink
Merge pull request #102 from athenavm/refactor/export-host-interface-…
Browse files Browse the repository at this point in the history
…from-go
  • Loading branch information
poszu authored Aug 27, 2024
2 parents 563bcd5 + 32cf55e commit 0fbaf7d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 70 deletions.
4 changes: 3 additions & 1 deletion ffi/athcon/bindings/go/athcon.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,11 @@ func (vm *VM) Execute(
}

ctxHandle := cgo.NewHandle(ctx)

hostInterface := newHostInterface()
result := C.athcon_execute(
vm.handle,
&C.athcon_go_host,
hostInterface,
(*C.struct_athcon_host_context)(unsafe.Pointer(uintptr(ctxHandle))),
uint32(rev),
&msg,
Expand Down
68 changes: 0 additions & 68 deletions ffi/athcon/bindings/go/host.c

This file was deleted.

24 changes: 23 additions & 1 deletion ffi/athcon/bindings/go/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ package athcon
#include <athcon/athcon.h>
#include <athcon/helpers.h>
#include <stdint.h>
// Forward declarations of exported functions that are shared with the VM.
bool accountExists(void *ctx, athcon_address *addr);
athcon_bytes32 getStorage(void *ctx, athcon_address *addr, athcon_bytes32 *key);
enum athcon_storage_status setStorage(void *ctx, athcon_address *addr, athcon_bytes32 *key, athcon_bytes32 *val);
athcon_uint256be getBalance(void *ctx, athcon_address *addr);
struct athcon_tx_context getTxContext(void *ctx);
athcon_bytes32 getBlockHash(void *ctx, long long int number);
struct athcon_result call(void *ctx, struct athcon_message *msg);
*/
import "C"
import (
Expand Down Expand Up @@ -84,7 +94,7 @@ func accountExists(pCtx unsafe.Pointer, pAddr *C.athcon_address) C.bool {
}

//export getStorage
func getStorage(pCtx unsafe.Pointer, pAddr *C.struct_athcon_address, pKey *C.athcon_bytes32) C.athcon_bytes32 {
func getStorage(pCtx unsafe.Pointer, pAddr *C.athcon_address, pKey *C.athcon_bytes32) C.athcon_bytes32 {
ctx := cgo.Handle(pCtx).Value().(HostContext)
return athconBytes32(ctx.GetStorage(goAddress(*pAddr), goHash(*pKey)))
}
Expand Down Expand Up @@ -144,3 +154,15 @@ func call(pCtx unsafe.Pointer, msg *C.struct_athcon_message) C.struct_athcon_res
result.create_address = athconAddress(createAddr)
return result
}

func newHostInterface() *C.struct_athcon_host_interface {
return &C.struct_athcon_host_interface{
account_exists: (C.athcon_account_exists_fn)(C.accountExists),
get_storage: (C.athcon_get_storage_fn)(C.getStorage),
set_storage: (C.athcon_set_storage_fn)(C.setStorage),
get_balance: (C.athcon_get_balance_fn)(C.getBalance),
get_tx_context: (C.athcon_get_tx_context_fn)(C.getTxContext),
get_block_hash: (C.athcon_get_block_hash_fn)(C.getBlockHash),
call: (C.athcon_call_fn)(C.call),
}
}

0 comments on commit 0fbaf7d

Please sign in to comment.