Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return code to get_block_hash_fn #73

Merged
merged 3 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bindings/go/evmc/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ static inline void go_exported_functions_type_checks()
tx_context = getTxContext(context);

evmc_get_block_hash_fn get_block_hash_fn = NULL;
get_block_hash_fn(uint256be, context, number);
getBlockHash(uint256be, context, number);
status = get_block_hash_fn(uint256be, context, number);
status = getBlockHash(uint256be, context, number);

evmc_emit_log_fn emit_log_fn = NULL;
emit_log_fn(context, address, data, size, uint256be, size);
Expand Down
12 changes: 9 additions & 3 deletions bindings/go/evmc/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type HostContext interface {
Selfdestruct(addr common.Address, beneficiary common.Address)
GetTxContext() (gasPrice common.Hash, origin common.Address, coinbase common.Address, number int64, timestamp int64,
gasLimit int64, difficulty common.Hash)
GetBlockHash(number int64) common.Hash
GetBlockHash(number int64) (common.Hash, error)
EmitLog(addr common.Address, topics []common.Hash, data []byte)
Call(kind CallKind,
destination common.Address, sender common.Address, value *big.Int, input []byte, gas int64, depth int,
Expand Down Expand Up @@ -182,11 +182,17 @@ func getTxContext(pCtx unsafe.Pointer) C.struct_evmc_tx_context {
}

//export getBlockHash
func getBlockHash(pResult *C.struct_evmc_uint256be, pCtx unsafe.Pointer, number int64) {
func getBlockHash(pResult *C.struct_evmc_uint256be, pCtx unsafe.Pointer, number int64) C.int {
idx := int((*C.struct_extended_context)(pCtx).index)
ctx := getHostContext(idx)

*pResult = evmcUint256be(ctx.GetBlockHash(number))
blockhash, err := ctx.GetBlockHash(number)
if err != nil {
return C.int(0)
}

*pResult = evmcUint256be(blockhash)
return C.int(1)
}

//export emitLog
Expand Down
26 changes: 18 additions & 8 deletions examples/example_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

#include <evmc/helpers.h>

struct example_host_context : evmc_context
{
example_host_context();

evmc_tx_context tx_context = {};
};

static evmc_uint256be balance(evmc_context* context, const evmc_address* address)
{
(void)context;
Expand Down Expand Up @@ -107,11 +114,17 @@ static evmc_tx_context get_tx_context(evmc_context* context)
return result;
}

static void get_block_hash(evmc_uint256be* result, evmc_context* context, int64_t number)
static int get_block_hash(evmc_uint256be* result, evmc_context* context, int64_t number)
{
(void)result;
(void)context;
(void)number;
example_host_context* host = static_cast<example_host_context*>(context);
int64_t current_block_number = host->tx_context.block_number;

if (number >= current_block_number || number < current_block_number - 256)
return 0;

evmc_uint256be example_block_hash{};
*result = example_block_hash;
return 1;
}

static void emit_log(evmc_context* context,
Expand All @@ -134,10 +147,7 @@ static const evmc_host_interface interface = {
copy_code, selfdestruct, call, get_tx_context, get_block_hash, emit_log,
};

struct example_host_context : evmc_context
{
example_host_context() : evmc_context{&interface} {}
};
example_host_context::example_host_context() : evmc_context{&interface} {}

extern "C" {

Expand Down
18 changes: 10 additions & 8 deletions include/evmc/evmc.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,19 @@ typedef struct evmc_tx_context (*evmc_get_tx_context_fn)(struct evmc_context* co
/**
* Get block hash callback function.
*
* This callback function is used by an EVM to query the block hash of
* a given block.
* This callback function is used by an VM to query the block hash of
* a given block. If the requested block is not found, then an appropriate
* result code is returned.
*
* @param[out] result The returned block hash value.
* @param[out] result The returned block hash value. Only written to
* if the return value is 1 (information is avialable).
* @param context The pointer to the Host execution context.
* @param number The block number. Must be a value between
* (and including) 0 and 255.
* @param number The block number.
* @return 1 if the information is available, 0 otherwise.
*/
typedef void (*evmc_get_block_hash_fn)(struct evmc_uint256be* result,
struct evmc_context* context,
int64_t number);
typedef int (*evmc_get_block_hash_fn)(struct evmc_uint256be* result,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, but we could consider having another enum for more regular cases.

Success/failure/invalid args.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chfast do we want an enum instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know yet. I have the review all similar cases and create the superset of possible error codes. If we end up with up to 4 items we can use it in all cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can merge it with int, but you have to improve documentation of @result and @param number.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we wait for #116 first?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The #116 is ready, just needs review.

struct evmc_context* context,
int64_t number);

/**
* The execution status code.
Expand Down