Update Go bindings

This commit is contained in:
Paweł Bylica 2019-09-20 14:16:27 +02:00
parent cbd068a979
commit e0ab5b1d53
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
2 changed files with 21 additions and 45 deletions

View File

@ -23,16 +23,10 @@ static inline enum evmc_set_option_result set_option(struct evmc_vm* vm, char* n
return ret;
}
struct extended_context
{
struct evmc_host_context context;
int64_t index;
};
extern const struct evmc_host_interface evmc_go_host;
static struct evmc_result execute_wrapper(struct evmc_vm* vm,
int64_t context_index, enum evmc_revision rev,
uintptr_t context_index, enum evmc_revision rev,
enum evmc_call_kind kind, uint32_t flags, int32_t depth, int64_t gas,
const evmc_address* destination, const evmc_address* sender,
const uint8_t* input_data, size_t input_size, const evmc_uint256be* value,
@ -51,8 +45,8 @@ static struct evmc_result execute_wrapper(struct evmc_vm* vm,
*create2_salt,
};
struct extended_context ctx = {{&evmc_go_host}, context_index};
return evmc_execute(vm, &ctx.context, rev, &msg, code, code_size);
struct evmc_host_context* context = (struct evmc_host_context*)context_index;
return evmc_execute(vm, &evmc_go_host, context, rev, &msg, code, code_size);
}
*/
import "C"
@ -242,7 +236,7 @@ func (vm *VM) Execute(ctx HostContext, rev Revision,
evmcSender := evmcAddress(sender)
evmcValue := evmcBytes32(value)
evmcCreate2Salt := evmcBytes32(create2Salt)
result := C.execute_wrapper(vm.handle, C.int64_t(ctxId), uint32(rev),
result := C.execute_wrapper(vm.handle, C.uintptr_t(ctxId), uint32(rev),
C.enum_evmc_call_kind(kind), flags, C.int32_t(depth), C.int64_t(gas),
&evmcDestination, &evmcSender, bytesPtr(input), C.size_t(len(input)), &evmcValue,
bytesPtr(code), C.size_t(len(code)), &evmcCreate2Salt)
@ -262,12 +256,12 @@ func (vm *VM) Execute(ctx HostContext, rev Revision,
}
var (
hostContextCounter int
hostContextMap = map[int]HostContext{}
hostContextCounter uintptr
hostContextMap = map[uintptr]HostContext{}
hostContextMapMu sync.Mutex
)
func addHostContext(ctx HostContext) int {
func addHostContext(ctx HostContext) uintptr {
hostContextMapMu.Lock()
id := hostContextCounter
hostContextCounter++
@ -276,13 +270,13 @@ func addHostContext(ctx HostContext) int {
return id
}
func removeHostContext(id int) {
func removeHostContext(id uintptr) {
hostContextMapMu.Lock()
delete(hostContextMap, id)
hostContextMapMu.Unlock()
}
func getHostContext(idx int) HostContext {
func getHostContext(idx uintptr) HostContext {
hostContextMapMu.Lock()
ctx := hostContextMap[idx]
hostContextMapMu.Unlock()

View File

@ -10,12 +10,6 @@ package evmc
#include <evmc/evmc.h>
#include <evmc/helpers.h>
struct extended_context
{
struct evmc_host_context context;
int64_t index;
};
*/
import "C"
import (
@ -99,50 +93,43 @@ type HostContext interface {
//export accountExists
func accountExists(pCtx unsafe.Pointer, pAddr *C.evmc_address) C.bool {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
return C.bool(ctx.AccountExists(goAddress(*pAddr)))
}
//export getStorage
func getStorage(pCtx unsafe.Pointer, pAddr *C.struct_evmc_address, pKey *C.evmc_bytes32) C.evmc_bytes32 {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
return evmcBytes32(ctx.GetStorage(goAddress(*pAddr), goHash(*pKey)))
}
//export setStorage
func setStorage(pCtx unsafe.Pointer, pAddr *C.evmc_address, pKey *C.evmc_bytes32, pVal *C.evmc_bytes32) C.enum_evmc_storage_status {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
return C.enum_evmc_storage_status(ctx.SetStorage(goAddress(*pAddr), goHash(*pKey), goHash(*pVal)))
}
//export getBalance
func getBalance(pCtx unsafe.Pointer, pAddr *C.evmc_address) C.evmc_uint256be {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
return evmcBytes32(ctx.GetBalance(goAddress(*pAddr)))
}
//export getCodeSize
func getCodeSize(pCtx unsafe.Pointer, pAddr *C.evmc_address) C.size_t {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
return C.size_t(ctx.GetCodeSize(goAddress(*pAddr)))
}
//export getCodeHash
func getCodeHash(pCtx unsafe.Pointer, pAddr *C.evmc_address) C.evmc_bytes32 {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
return evmcBytes32(ctx.GetCodeHash(goAddress(*pAddr)))
}
//export copyCode
func copyCode(pCtx unsafe.Pointer, pAddr *C.evmc_address, offset C.size_t, p *C.uint8_t, size C.size_t) C.size_t {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
code := ctx.GetCode(goAddress(*pAddr))
length := C.size_t(len(code))
@ -162,15 +149,13 @@ func copyCode(pCtx unsafe.Pointer, pAddr *C.evmc_address, offset C.size_t, p *C.
//export selfdestruct
func selfdestruct(pCtx unsafe.Pointer, pAddr *C.evmc_address, pBeneficiary *C.evmc_address) {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
ctx.Selfdestruct(goAddress(*pAddr), goAddress(*pBeneficiary))
}
//export getTxContext
func getTxContext(pCtx unsafe.Pointer) C.struct_evmc_tx_context {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
txContext := ctx.GetTxContext()
@ -188,15 +173,13 @@ func getTxContext(pCtx unsafe.Pointer) C.struct_evmc_tx_context {
//export getBlockHash
func getBlockHash(pCtx unsafe.Pointer, number int64) C.evmc_bytes32 {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
return evmcBytes32(ctx.GetBlockHash(number))
}
//export emitLog
func emitLog(pCtx unsafe.Pointer, pAddr *C.evmc_address, pData unsafe.Pointer, dataSize C.size_t, pTopics unsafe.Pointer, topicsCount C.size_t) {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
// FIXME: Optimize memory copy
data := C.GoBytes(pData, C.int(dataSize))
@ -213,8 +196,7 @@ func emitLog(pCtx unsafe.Pointer, pAddr *C.evmc_address, pData unsafe.Pointer, d
//export call
func call(pCtx unsafe.Pointer, msg *C.struct_evmc_message) C.struct_evmc_result {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)
ctx := getHostContext(uintptr(pCtx))
kind := CallKind(msg.kind)
output, gasLeft, createAddr, err := ctx.Call(kind, goAddress(msg.destination), goAddress(msg.sender), goHash(msg.value).Big(),