mirror of https://github.com/status-im/evmc.git
Merge pull request #242 from ethereum/vmtester-create
vmtester: add test case for CREATE
This commit is contained in:
commit
45996842e2
|
@ -10,13 +10,29 @@ extern "C" fn execute(
|
|||
code: *const u8,
|
||||
code_size: usize,
|
||||
) -> ffi::evmc_result {
|
||||
let result = evmc_vm::ExecutionResult::new(
|
||||
ffi::evmc_status_code::EVMC_SUCCESS,
|
||||
66,
|
||||
None,
|
||||
ffi::evmc_address { bytes: [0u8; 20] },
|
||||
);
|
||||
result.into()
|
||||
if msg == std::ptr::null() {
|
||||
panic!()
|
||||
}
|
||||
|
||||
let is_create = unsafe { (*msg).kind == ffi::evmc_call_kind::EVMC_CREATE };
|
||||
|
||||
if is_create {
|
||||
evmc_vm::ExecutionResult::new(
|
||||
ffi::evmc_status_code::EVMC_FAILURE,
|
||||
0,
|
||||
None,
|
||||
ffi::evmc_address { bytes: [0u8; 20] },
|
||||
)
|
||||
.into()
|
||||
} else {
|
||||
evmc_vm::ExecutionResult::new(
|
||||
ffi::evmc_status_code::EVMC_SUCCESS,
|
||||
66,
|
||||
None,
|
||||
ffi::evmc_address { bytes: [0u8; 20] },
|
||||
)
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn get_capabilities(
|
||||
|
|
|
@ -110,8 +110,17 @@ static struct evmc_result execute(struct evmc_instance* instance,
|
|||
// Assembly: `{ mstore(0, number()) return(0, msize()) }`
|
||||
const char return_block_number[] = "\x43\x60\x00\x52\x59\x60\x00\xf3";
|
||||
|
||||
if (code_size == (sizeof(return_address) - 1) &&
|
||||
strncmp((const char*)code, return_address, code_size) == 0)
|
||||
if (msg->kind == EVMC_CREATE)
|
||||
{
|
||||
evmc_address create_address = {
|
||||
{1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
|
||||
ret.status_code = EVMC_SUCCESS;
|
||||
ret.create_address = create_address;
|
||||
ret.gas_left = msg->gas / 10;
|
||||
return ret;
|
||||
}
|
||||
else if (code_size == (sizeof(return_address) - 1) &&
|
||||
strncmp((const char*)code, return_address, code_size) == 0)
|
||||
{
|
||||
static const size_t address_size = sizeof(msg->destination);
|
||||
uint8_t* output_data = (uint8_t*)malloc(address_size);
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "vmtester.hpp"
|
||||
|
||||
#include <evmc/helpers.h>
|
||||
#include <evmc/helpers.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
|
@ -45,7 +46,7 @@ TEST_F(evmc_vm_test, version)
|
|||
EXPECT_GT(std::strlen(vm->version), 0) << "VM name cannot be empty";
|
||||
}
|
||||
|
||||
TEST_F(evmc_vm_test, execute)
|
||||
TEST_F(evmc_vm_test, execute_call)
|
||||
{
|
||||
evmc_context* context = example_host_create_context();
|
||||
evmc_message msg{};
|
||||
|
@ -76,6 +77,45 @@ TEST_F(evmc_vm_test, execute)
|
|||
example_host_destroy_context(context);
|
||||
}
|
||||
|
||||
TEST_F(evmc_vm_test, execute_create)
|
||||
{
|
||||
evmc_context* context = example_host_create_context();
|
||||
evmc_message msg{
|
||||
EVMC_CREATE, 0, 0, 65536, evmc_address{}, evmc_address{}, NULL, 0, evmc_uint256be{},
|
||||
evmc_bytes32{}};
|
||||
std::array<uint8_t, 2> code = {{0xfe, 0x00}};
|
||||
|
||||
evmc_result result =
|
||||
vm->execute(vm, context, EVMC_MAX_REVISION, &msg, code.data(), code.size());
|
||||
|
||||
// Validate some constraints
|
||||
if (result.status_code != EVMC_SUCCESS && result.status_code != EVMC_REVERT)
|
||||
{
|
||||
EXPECT_EQ(result.gas_left, 0);
|
||||
}
|
||||
|
||||
if (result.output_data == NULL)
|
||||
{
|
||||
EXPECT_EQ(result.output_size, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
EXPECT_NE(result.output_size, 0);
|
||||
read_buffer(result.output_data, result.output_size);
|
||||
}
|
||||
|
||||
if (result.status_code == EVMC_SUCCESS)
|
||||
{
|
||||
// This assumes that CREATE returns a non-zero address on success.
|
||||
EXPECT_FALSE(is_zero(result.create_address));
|
||||
}
|
||||
|
||||
if (result.release)
|
||||
result.release(&result);
|
||||
|
||||
example_host_destroy_context(context);
|
||||
}
|
||||
|
||||
TEST_F(evmc_vm_test, set_option_unknown_name)
|
||||
{
|
||||
if (vm->set_option)
|
||||
|
|
Loading…
Reference in New Issue