From 067e480a122e013b36a3b636721bc87ca6259ab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 10 Feb 2022 12:02:19 +0100 Subject: [PATCH] Fixup Go --- bindings/go/evmc/host.c | 29 ++++++++++++---------- bindings/go/evmc/host.go | 45 +++++++++++++++++++++-------------- bindings/go/evmc/host_test.go | 15 ++++++++---- 3 files changed, 54 insertions(+), 35 deletions(-) diff --git a/bindings/go/evmc/host.c b/bindings/go/evmc/host.c index e2c9d70be..968114d70 100644 --- a/bindings/go/evmc/host.c +++ b/bindings/go/evmc/host.c @@ -1,18 +1,16 @@ -/* EVMC: Ethereum Client-VM Connector API. - * Copyright 2018-2019 The EVMC Authors. - * Licensed under the Apache License, Version 2.0. - */ +// EVMC: Ethereum Client-VM Connector API. +// Copyright 2018 The EVMC Authors. +// Licensed under the Apache License, Version 2.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 EVMC. - * 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. - */ +// Go does not support exporting functions with parameters with const modifiers, +// so we have to cast function pointers to the function types defined in EVMC. +// 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 evmc_host_interface evmc_go_host = { (evmc_account_exists_fn)accountExists, (evmc_get_storage_fn)getStorage, @@ -24,6 +22,7 @@ const struct evmc_host_interface evmc_go_host = { (evmc_selfdestruct_fn)selfdestruct, (evmc_call_fn)call, (evmc_get_tx_context_fn)getTxContext, + (evmc_get_block_context_fn)getBlockContext, (evmc_get_block_hash_fn)getBlockHash, (evmc_emit_log_fn)emitLog, (evmc_access_account_fn)accessAccount, @@ -45,7 +44,7 @@ static inline void go_exported_functions_type_checks() evmc_uint256be uint256be; (void)uint256be; struct evmc_tx_context tx_context; - (void)tx_context; + struct evmc_block_context block_context; struct evmc_result result; (void)result; enum evmc_access_status access_status; @@ -92,8 +91,12 @@ static inline void go_exported_functions_type_checks() result = call(context, message); evmc_get_tx_context_fn get_tx_context_fn = NULL; - tx_context = get_tx_context_fn(context); - tx_context = getTxContext(context); + get_tx_context_fn(&tx_context, context); + getTxContext(&tx_context, context); + + evmc_get_block_context_fn get_block_context_fn = NULL; + get_block_context_fn(&block_context, context); + getBlockContext(&block_context, context); evmc_get_block_hash_fn get_block_hash_fn = NULL; bytes32 = get_block_hash_fn(context, number); diff --git a/bindings/go/evmc/host.go b/bindings/go/evmc/host.go index 15ca59452..15c1c82ee 100644 --- a/bindings/go/evmc/host.go +++ b/bindings/go/evmc/host.go @@ -1,5 +1,5 @@ // EVMC: Ethereum Client-VM Connector API. -// Copyright 2018-2020 The EVMC Authors. +// Copyright 2018 The EVMC Authors. // Licensed under the Apache License, Version 2.0. package evmc @@ -66,17 +66,21 @@ func goByteSlice(data *C.uint8_t, size C.size_t) []byte { return (*[1 << 30]byte)(unsafe.Pointer(data))[:size:size] } -// TxContext contains information about current transaction and block. +// TxContext contains information about the current transaction. type TxContext struct { - GasPrice Hash - Origin Address - Coinbase Address + Origin Address + GasPrice Hash +} + +// BlockContext contains information about the current block. +type BlockContext struct { Number int64 Timestamp int64 GasLimit int64 Difficulty Hash ChainID Hash BaseFee Hash + Coinbase Address } type HostContext interface { @@ -89,6 +93,7 @@ type HostContext interface { GetCode(addr Address) []byte Selfdestruct(addr Address, beneficiary Address) GetTxContext() TxContext + GetBlockContext() BlockContext GetBlockHash(number int64) Hash EmitLog(addr Address, topics []Hash, data []byte) Call(kind CallKind, @@ -161,21 +166,25 @@ func selfdestruct(pCtx unsafe.Pointer, pAddr *C.evmc_address, pBeneficiary *C.ev } //export getTxContext -func getTxContext(pCtx unsafe.Pointer) C.struct_evmc_tx_context { +func getTxContext(pTx *C.struct_evmc_tx_context, pCtx unsafe.Pointer) { ctx := getHostContext(uintptr(pCtx)) + tx := ctx.GetTxContext() + *pTx = C.struct_evmc_tx_context{evmcAddress(tx.Origin), evmcBytes32(tx.GasPrice)} +} - txContext := ctx.GetTxContext() - - return C.struct_evmc_tx_context{ - evmcBytes32(txContext.GasPrice), - evmcAddress(txContext.Origin), - evmcAddress(txContext.Coinbase), - C.int64_t(txContext.Number), - C.int64_t(txContext.Timestamp), - C.int64_t(txContext.GasLimit), - evmcBytes32(txContext.Difficulty), - evmcBytes32(txContext.ChainID), - evmcBytes32(txContext.BaseFee), +//export getBlockContext +func getBlockContext(pBlock *C.struct_evmc_block_context, pCtx unsafe.Pointer) { + ctx := getHostContext(uintptr(pCtx)) + block := ctx.GetBlockContext() + *pBlock = C.struct_evmc_block_context{ + C.int64_t(block.Number), + C.int64_t(block.Timestamp), + C.int64_t(block.GasLimit), + evmcBytes32(block.ChainID), + evmcBytes32(block.BaseFee), + evmcBytes32(block.Difficulty), + evmcAddress(block.Coinbase), + [4]byte{}, } } diff --git a/bindings/go/evmc/host_test.go b/bindings/go/evmc/host_test.go index edb00715c..c22c89940 100644 --- a/bindings/go/evmc/host_test.go +++ b/bindings/go/evmc/host_test.go @@ -1,5 +1,5 @@ // EVMC: Ethereum Client-VM Connector API. -// Copyright 2018-2020 The EVMC Authors. +// Copyright 2018 The EVMC Authors. // Licensed under the Apache License, Version 2.0. package evmc @@ -43,9 +43,16 @@ func (host *testHostContext) Selfdestruct(addr Address, beneficiary Address) { } func (host *testHostContext) GetTxContext() TxContext { - txContext := TxContext{} - txContext.Number = 42 - return txContext + tx := TxContext{} + tx.Origin[0] = 0x80 + tx.Origin[19] = 0x01 + return tx +} + +func (host *testHostContext) GetBlockContext() BlockContext { + block := BlockContext{} + block.Number = 42 + return block } func (host *testHostContext) GetBlockHash(number int64) Hash {