diff --git a/bindings/go/evmc/host.c b/bindings/go/evmc/host.c index e2c9d70be..e6c88dc99 100644 --- a/bindings/go/evmc/host.c +++ b/bindings/go/evmc/host.c @@ -1,5 +1,5 @@ /* EVMC: Ethereum Client-VM Connector API. - * Copyright 2018-2019 The EVMC Authors. + * Copyright 2018 The EVMC Authors. * Licensed under the Apache License, Version 2.0. */ @@ -24,6 +24,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 +46,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 +93,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..674297e99 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 @@ -7,8 +7,8 @@ package evmc /* #cgo CFLAGS: -I${SRCDIR}/../../../include -Wall -Wextra -Wno-unused-parameter -#include -#include +#include +#include */ import "C" @@ -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..761ff05ce 100644 --- a/bindings/go/evmc/host_test.go +++ b/bindings/go/evmc/host_test.go @@ -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 {