Merge pull request #144 from ethereum/capabilities

Introduce evmc_capabilities and emvc_get_capabilities_fn
This commit is contained in:
Paweł Bylica 2018-09-08 22:24:50 +02:00 committed by GitHub
commit 7cfc2bf39e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 0 deletions

View File

@ -28,6 +28,12 @@ static void destroy(struct evmc_instance* evm)
free(evm);
}
static evmc_capabilities_flagset get_capabilities(struct evmc_instance* vm)
{
(void)vm;
return EVMC_CAPABILITY_EVM1 | EVMC_CAPABILITY_EWASM;
}
/// Example options.
///
/// VMs are allowed to omit this function implementation.
@ -150,6 +156,7 @@ struct evmc_instance* evmc_create_example_vm()
.version = STR(PROJECT_VERSION),
.destroy = destroy,
.execute = execute,
.get_capabilites = get_capabilities,
.set_option = set_option,
.set_tracer = set_tracer,
};

View File

@ -763,6 +763,32 @@ typedef struct evmc_result (*evmc_execute_fn)(struct evmc_instance* instance,
uint8_t const* code,
size_t code_size);
/**
* Possible capabilities of a VM.
*/
enum evmc_capabilities
{
EVMC_CAPABILITY_EVM1 = (1u << 0), /**< The VM is capable of executing EVM1 bytecode. */
EVMC_CAPABILITY_EWASM = (1u << 1) /**< The VM is capable of execution ewasm bytecode. */
};
/**
* Alias for unsigned integer representing a set of bit flags of EVMC capabilities.
*
* @see evmc_capabilities
*/
typedef uint32_t evmc_capabilities_flagset;
/**
* Return the supported capabilities of the VM instance.
*
* This function MAY be invoked multiple times for a single VM instance,
* and its value MAY be influenced by calls to evmc_instance::set_option.
*
* @param instance The EVM instance.
* @return The supported capabilities of the VM. @see evmc_capabilities.
*/
typedef evmc_capabilities_flagset (*evmc_get_capabilities_fn)(struct evmc_instance* instance);
/** The opaque type representing a Client-side tracer object. */
struct evmc_tracer_context;
@ -870,6 +896,16 @@ struct evmc_instance
/** Pointer to function executing a code by the EVM instance. */
evmc_execute_fn execute;
/**
* Pointer to function returning capabilities supported by the VM instance.
*
* The value returned might change when different options are requested via set_option.
*
* A Client SHOULD only rely on the value returned here if it has queried it after
* it has called set_option.
*/
evmc_get_capabilities_fn get_capabilites;
/**
* Optional pointer to function setting the EVM instruction tracer.
*

View File

@ -41,6 +41,17 @@ static inline const char* evmc_vm_version(struct evmc_instance* instance)
return instance->version;
}
/**
* Checks if the VM instance has the given capability.
*
* @see evmc_get_capabilities_fn
*/
static inline bool evmc_vm_has_capability(struct evmc_instance* vm,
enum evmc_capabilities capability)
{
return (vm->get_capabilites(vm) & (evmc_capabilities_flagset)capability) != 0;
}
/**
* Destroys the VM instance.
*

View File

@ -117,3 +117,10 @@ TEST_F(evmc_vm_test, set_tracer)
if (vm->set_tracer)
vm->set_tracer(vm, tracer_callback, nullptr);
}
TEST_F(evmc_vm_test, capabilities)
{
// The VM should have at least one of EVM1 or EWASM capabilities.
EXPECT_TRUE(evmc_vm_has_capability(vm, EVMC_CAPABILITY_EVM1) ||
evmc_vm_has_capability(vm, EVMC_CAPABILITY_EWASM));
}