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

rust: make ExecutionContext optional in EvmcVm.execute() #350

Merged
merged 2 commits into from
Nov 5, 2019
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
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],
axic marked this conversation as resolved.
Show resolved Hide resolved
_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],
axic marked this conversation as resolved.
Show resolved Hide resolved
_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();
Copy link
Member

Choose a reason for hiding this comment

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

Why this is a failure?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because the code needs tx_context which is part of that?

}
let _context = _context.unwrap();

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