Skip to content

Commit

Permalink
Fixup Go
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Feb 10, 2022
1 parent 6aa8d87 commit 6f7c59d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
13 changes: 9 additions & 4 deletions bindings/go/evmc/host.c
Original file line number Diff line number Diff line change
@@ -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.
*/

Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
49 changes: 29 additions & 20 deletions bindings/go/evmc/host.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// 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

/*
#cgo CFLAGS: -I${SRCDIR}/../../../include -Wall -Wextra -Wno-unused-parameter
#include <evmc/evmc.h>
#include <evmc/helpers.h>
#include </home/chfast/Projects/ethereum/evmc/include/evmc/evmc.h>
#include </home/chfast/Projects/ethereum/evmc/include/evmc/helpers.h>
*/
import "C"
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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{},
}
}

Expand Down
13 changes: 10 additions & 3 deletions bindings/go/evmc/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6f7c59d

Please sign in to comment.