diff --git a/ffi/athcon/bindings/go/athcon.go b/ffi/athcon/bindings/go/athcon.go index 8eef8437..20a86cff 100644 --- a/ffi/athcon/bindings/go/athcon.go +++ b/ffi/athcon/bindings/go/athcon.go @@ -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, diff --git a/ffi/athcon/bindings/go/host.c b/ffi/athcon/bindings/go/host.c deleted file mode 100644 index 1133c4f9..00000000 --- a/ffi/athcon/bindings/go/host.c +++ /dev/null @@ -1,68 +0,0 @@ -#include "_cgo_export.h" - -#include - -/* Go does not support exporting functions with parameters with const modifiers, - * so we have to cast function pointers to the function types defined in ATHCON. - * This disables any type checking of exported Go functions. To mitigate this - * problem the go_exported_functions_type_checks() function simulates usage - * of Go exported functions with expected types to check them during compilation. - */ -const struct athcon_host_interface athcon_go_host = { - (athcon_account_exists_fn)accountExists, - (athcon_get_storage_fn)getStorage, - (athcon_set_storage_fn)setStorage, - (athcon_get_balance_fn)getBalance, - (athcon_call_fn)call, - (athcon_get_tx_context_fn)getTxContext, - (athcon_get_block_hash_fn)getBlockHash, -}; - -#pragma GCC diagnostic error "-Wconversion" -static inline void go_exported_functions_type_checks() -{ - struct athcon_host_context *context = NULL; - athcon_address *address = NULL; - athcon_bytes32 bytes32; - int64_t number = 0; - struct athcon_message *message = NULL; - - athcon_uint256be uint256be; - (void)uint256be; - struct athcon_tx_context tx_context; - (void)tx_context; - struct athcon_result result; - (void)result; - enum athcon_storage_status storage_status; - (void)storage_status; - bool bool_flag; - (void)bool_flag; - - athcon_account_exists_fn account_exists_fn = NULL; - bool_flag = account_exists_fn(context, address); - bool_flag = accountExists(context, address); - - athcon_get_storage_fn get_storage_fn = NULL; - bytes32 = get_storage_fn(context, address, &bytes32); - bytes32 = getStorage(context, address, &bytes32); - - athcon_set_storage_fn set_storage_fn = NULL; - storage_status = set_storage_fn(context, address, &bytes32, &bytes32); - storage_status = setStorage(context, address, &bytes32, &bytes32); - - athcon_get_balance_fn get_balance_fn = NULL; - uint256be = get_balance_fn(context, address); - uint256be = getBalance(context, address); - - athcon_call_fn call_fn = NULL; - result = call_fn(context, message); - result = call(context, message); - - athcon_get_tx_context_fn get_tx_context_fn = NULL; - tx_context = get_tx_context_fn(context); - tx_context = getTxContext(context); - - athcon_get_block_hash_fn get_block_hash_fn = NULL; - bytes32 = get_block_hash_fn(context, number); - bytes32 = getBlockHash(context, number); -} diff --git a/ffi/athcon/bindings/go/host.go b/ffi/athcon/bindings/go/host.go index a672652e..fffabbd1 100644 --- a/ffi/athcon/bindings/go/host.go +++ b/ffi/athcon/bindings/go/host.go @@ -5,6 +5,16 @@ package athcon #include #include +#include + +// 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 ( @@ -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))) } @@ -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), + } +}