examples: Better get_block_hash example

This commit is contained in:
Paweł Bylica 2018-09-03 17:24:15 +02:00
parent d0bc50792a
commit ced1c2676f
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
1 changed files with 17 additions and 8 deletions

View File

@ -9,6 +9,13 @@
#include <evmc/helpers.h> #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) static evmc_uint256be balance(evmc_context* context, const evmc_address* address)
{ {
(void)context; (void)context;
@ -109,10 +116,15 @@ static evmc_tx_context get_tx_context(evmc_context* context)
static int 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; example_host_context* host = static_cast<example_host_context*>(context);
(void)context; int64_t current_block_number = host->tx_context.block_number;
(void)number;
return 0; 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, static void emit_log(evmc_context* context,
@ -135,10 +147,7 @@ static const evmc_host_interface interface = {
copy_code, selfdestruct, call, get_tx_context, get_block_hash, emit_log, copy_code, selfdestruct, call, get_tx_context, get_block_hash, emit_log,
}; };
struct example_host_context : evmc_context example_host_context::example_host_context() : evmc_context{&interface} {}
{
example_host_context() : evmc_context{&interface} {}
};
extern "C" { extern "C" {