Skip to content

Commit

Permalink
Merge pull request #350 from ethereum/rust-vm-fields
Browse files Browse the repository at this point in the history
rust: make ExecutionContext optional in EvmcVm.execute()
  • Loading branch information
axic committed Nov 5, 2019
2 parents f284e42 + d4e2ef1 commit dee9b57
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ and this project adheres to [Semantic Versioning].
- The helper function `evmc_is_abi_compatible()` returns now `bool`
instead of `int`.
[[#442](https://github.com/ethereum/evmc/pull/442)]
- In the Rust bindings make `ExecutionContext` optional within `execute`.
[[#350](https://github.com/ethereum/evmc/pull/350)]


## [6.3.1] - 2019-08-19
Expand Down
2 changes: 1 addition & 1 deletion bindings/rust/evmc-declare-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl EvmcVm for FooVM {
_revision: evmc_sys::evmc_revision,
_code: &[u8],
_message: &ExecutionMessage,
_context: &mut ExecutionContext,
_context: Option<&mut ExecutionContext>,
) -> ExecutionResult {
ExecutionResult::success(1337, None)
}
Expand Down
24 changes: 13 additions & 11 deletions bindings/rust/evmc-declare/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! ExampleVM {}
//! }
//!
//! fn execute(&self, revision: evmc_vm::ffi::evmc_revision, code: &[u8], message: &evmc_vm::ExecutionMessage, context: &mut evmc_vm::ExecutionContext) -> evmc_vm::ExecutionResult {
//! fn execute(&self, revision: evmc_vm::ffi::evmc_revision, code: &[u8], message: &evmc_vm::ExecutionMessage, context: Option<&mut evmc_vm::ExecutionContext>) -> evmc_vm::ExecutionResult {
//! evmc_vm::ExecutionResult::success(1337, None)
//! }
//! }
Expand Down Expand Up @@ -346,14 +346,12 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream {
use evmc_vm::EvmcVm;

// TODO: context is optional in case of the "precompiles" capability
if instance.is_null() || host.is_null() || msg.is_null() || (code.is_null() && code_size != 0) {
if instance.is_null() || msg.is_null() || (code.is_null() && code_size != 0) {
// These are irrecoverable errors that violate the EVMC spec.
std::process::abort();
}

assert!(!instance.is_null());
// TODO: host is optional in case of the "precompiles" capability
assert!(!host.is_null());
assert!(!msg.is_null());

let execution_message: ::evmc_vm::ExecutionMessage = unsafe {
Expand All @@ -376,13 +374,17 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream {
};

let result = ::std::panic::catch_unwind(|| {
let mut execution_context = unsafe {
::evmc_vm::ExecutionContext::new(
host.as_ref().expect("EVMC host is null"),
context,
)
};
container.execute(revision, code_ref, &execution_message, &mut execution_context)
if host.is_null() {
container.execute(revision, code_ref, &execution_message, None)
} else {
let mut execution_context = unsafe {
::evmc_vm::ExecutionContext::new(
host.as_ref().expect("EVMC host is null"),
context,
)
};
container.execute(revision, code_ref, &execution_message, Some(&mut execution_context))
}
});

let result = if result.is_err() {
Expand Down
6 changes: 3 additions & 3 deletions bindings/rust/evmc-vm/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ mod tests {
_revision: evmc_sys::evmc_revision,
_code: &[u8],
_message: &ExecutionMessage,
_context: &mut ExecutionContext,
_context: Option<&mut ExecutionContext>,
) -> ExecutionResult {
ExecutionResult::failure()
}
Expand Down Expand Up @@ -142,7 +142,7 @@ mod tests {
evmc_sys::evmc_revision::EVMC_PETERSBURG,
&code,
&message,
&mut context,
Some(&mut context)
)
.status_code(),
::evmc_sys::evmc_status_code::EVMC_FAILURE
Expand All @@ -158,7 +158,7 @@ mod tests {
evmc_sys::evmc_revision::EVMC_PETERSBURG,
&code,
&message,
&mut context,
Some(&mut context)
)
.status_code(),
::evmc_sys::evmc_status_code::EVMC_FAILURE
Expand Down
2 changes: 1 addition & 1 deletion bindings/rust/evmc-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait EvmcVm {
revision: ffi::evmc_revision,
code: &'a [u8],
message: &'a ExecutionMessage,
context: &'a mut ExecutionContext<'a>,
context: Option<&'a mut ExecutionContext<'a>>,
) -> ExecutionResult;
}

Expand Down
9 changes: 7 additions & 2 deletions examples/example-rust-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use evmc_declare::evmc_declare_vm;
use evmc_vm::*;

#[evmc_declare_vm("ExampleRustVM", "evm", "6.3.0-dev")]
#[evmc_declare_vm("ExampleRustVM", "evm, precompiles", "6.3.0-dev")]
pub struct ExampleRustVM;

impl EvmcVm for ExampleRustVM {
Expand All @@ -19,8 +19,13 @@ impl EvmcVm for ExampleRustVM {
_revision: evmc_sys::evmc_revision,
_code: &'a [u8],
message: &'a ExecutionMessage,
_context: &'a mut ExecutionContext<'a>,
_context: Option<&'a mut ExecutionContext<'a>>,
) -> ExecutionResult {
if _context.is_none() {
return ExecutionResult::failure();
}
let _context = _context.unwrap();

if message.kind() != evmc_sys::evmc_call_kind::EVMC_CALL {
return ExecutionResult::failure();
}
Expand Down

0 comments on commit dee9b57

Please sign in to comment.