Skip to content

Commit

Permalink
Add fail_with_status host function, fix #146 (#425)
Browse files Browse the repository at this point in the history
* Add fail_with_status host function, fix #146

* Regenerate wasms and update env.
  • Loading branch information
graydon authored Sep 20, 2022
1 parent cc2c0b0 commit c9ea486
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 42 deletions.
2 changes: 1 addition & 1 deletion soroban-env-common/src/checked_env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::call_macro_with_all_host_functions;
use crate::{EnvBase, Object, RawVal, Symbol};
use crate::{EnvBase, Object, RawVal, Status, Symbol};
use core::fmt::Debug;

/// The CheckedEnv trait is similar to the Env trait -- it provides all the
Expand Down
6 changes: 5 additions & 1 deletion soroban-env-common/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::Symbol;
use super::{Object, RawVal};
use super::{Object, RawVal, Status};
use core::any;

/// Base trait extended by the [Env](crate::Env) trait, providing various special-case
Expand Down Expand Up @@ -168,6 +168,10 @@ macro_rules! call_macro_with_all_host_functions {
/// vector contains the contract id as Hash, and a function as
/// a Symbol.
{"8", fn get_current_call_stack() -> Object }
/// Causes the currently executing contract to fail immediately
/// with a provided status code, which must be of error-type
/// `ScStatusType::ContractError`. Does not actually return.
{"9", fn fail_with_status(status:Status) -> RawVal }
}

mod u64 "u" {
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@
// implementations over a long period of time.

soroban_env_macros::generate_env_meta_consts!(
interface_version: 13,
interface_version: 14,
);
2 changes: 1 addition & 1 deletion soroban-env-common/src/unimplemented_env.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{call_macro_with_all_host_functions, Env, EnvBase, Object, RawVal, Symbol};
use super::{call_macro_with_all_host_functions, Env, EnvBase, Object, RawVal, Status, Symbol};
use core::any;

/// A dummy implementation of the [Env] trait that fails with `unimplemented!()` in
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/vmcaller_checked_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::xdr::ScHostContextErrorCode;

use crate::call_macro_with_all_host_functions;
use crate::{Object, RawVal, Symbol};
use crate::{Object, RawVal, Status, Symbol};
use core::fmt::Debug;
#[cfg(not(feature = "vm"))]
use core::marker::PhantomData;
Expand Down
4 changes: 2 additions & 2 deletions soroban-env-guest/src/guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use soroban_env_common::call_macro_with_all_host_functions;

use super::{Env, EnvBase, Object, RawVal, Symbol};
use super::{Env, EnvBase, Object, RawVal, Status, Symbol};
#[cfg(target_family = "wasm")]
use static_assertions as sa;

Expand Down Expand Up @@ -270,7 +270,7 @@ macro_rules! generate_extern_modules {
$(#[$mod_attr])*
mod $mod_id {
#[allow(unused_imports)]
use crate::{RawVal,Object,Symbol};
use crate::{RawVal,Object,Symbol,Status};
#[link(wasm_import_module = $mod_str)]
extern "C" {
$(
Expand Down
17 changes: 16 additions & 1 deletion soroban-env-host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use soroban_env_common::{

use soroban_env_common::xdr::{
AccountId, ContractEvent, ContractEventBody, ContractEventType, ContractEventV0,
ExtensionPoint, Hash, PublicKey, ReadXdr, ThresholdIndexes, WriteXdr,
ExtensionPoint, Hash, PublicKey, ReadXdr, ScStatusType, ThresholdIndexes, WriteXdr,
};

use crate::budget::{Budget, CostType};
Expand Down Expand Up @@ -2520,4 +2520,19 @@ impl VmCallerCheckedEnv for Host {
let res = HostVec::from_vec(self.0.budget.clone(), outer)?;
Ok(self.add_host_object(res)?.into())
}

fn fail_with_status(
&self,
vmcaller: &mut VmCaller<Self::VmUserState>,
status: Status,
) -> Result<RawVal, Self::Error> {
if status.is_type(ScStatusType::ContractError) {
Err(self.err_status_msg(status, "failing with contract error status code"))
} else {
Err(self.err_status_msg(
ScHostValErrorCode::UnexpectedValType,
"contract attempted to fail with non-ContractError status code",
))
}
}
}
2 changes: 1 addition & 1 deletion soroban-env-host/src/vm/dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{budget::CostType, Host, Object, RawVal, Symbol, VmCaller, VmCallerCheckedEnv};
use crate::{budget::CostType, Host, Object, RawVal, Status, Symbol, VmCaller, VmCallerCheckedEnv};
use soroban_env_common::call_macro_with_all_host_functions;
use wasmi::core::{FromValue, Trap, TrapCode::UnexpectedSignature, Value};

Expand Down
2 changes: 1 addition & 1 deletion soroban-env-host/src/weak_host.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use soroban_env_common::call_macro_with_all_host_functions;

use crate::{host::HostImpl, Env, EnvBase, Host, Object, RawVal, Symbol};
use crate::{host::HostImpl, Env, EnvBase, Host, Object, RawVal, Status, Symbol};
use core::fmt::Debug;
use std::rc::Weak;

Expand Down
65 changes: 38 additions & 27 deletions soroban-test-wasms/wasm-workspace/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions soroban-test-wasms/wasm-workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ soroban-native-sdk-macros = { path = "../../soroban-native-sdk-macros" }

# You should update this rev to the XDR rev used by the outer workspace
# anytime you're going to recompile this workspace.
stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "fee9a436" }
stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "fb78a6b8" }

# You should actually be able to set this to be any existing version of wasmi because we are
# not building the SDK in host mode, so the "vm" feature is off.
wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", rev = "3bda8032" }
wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", rev = "a61b6df" }

# The following SDK patch lines must denote an SDK version that is _compatible_
# with the current version of soroban-env-guest and soroban-env-common, as found
Expand Down Expand Up @@ -93,8 +93,13 @@ wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", r
# when you change the Env trait itself, and whatever change you make to Env may
# well cause recompilation to fail. Again, you will know if it doesn't compile!

soroban-sdk = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "e10f13cbe3c5c2dc3d7a3d0bc3616467352181b0" }
soroban-sdk-macros = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "e10f13cbe3c5c2dc3d7a3d0bc3616467352181b0" }
soroban-sdk = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "8c35444b52ce5b59edde69415ff4cb7f9127b983" }
soroban-sdk-macros = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "8c35444b52ce5b59edde69415ff4cb7f9127b983" }
soroban-spec = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "8c35444b52ce5b59edde69415ff4cb7f9127b983" }

# soroban-auth is currently not-used by these examples, but is in the SDK repo
# and must be patched in lock-step with the others if it's ever used here.
# soroban-auth = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "8c35444b52ce5b59edde69415ff4cb7f9127b983" }

[profile.release]
opt-level = "z"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl Contract {
env.invoke_contract(
&contract_id,
&Symbol::from_str("add"),
vec![&env, x.into_env_val(&env), y.into_env_val(&env)],
vec![&env, x.into_val(&env), y.into_val(&env)],
)
}
}
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_add_i32.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_contract_data.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_linear_memory.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_vec.wasm
Binary file not shown.

0 comments on commit c9ea486

Please sign in to comment.