mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 07:37:57 +00:00
Add get_global for Sandbox (#4756)
* Add `get_global` for `Sandbox` This pr adds `get_global` to retrieve a `global` variable from an instantiated sandbox wasm blob. * Bump `spec_version` * Update primitives/wasm-interface/src/lib.rs Co-Authored-By: Sergei Pepyakin <sergei@parity.io> * `get_global` -> `get_global_val` Co-authored-by: Sergei Pepyakin <s.pepyakin@gmail.com> Co-authored-by: Gavin Wood <github@gavwood.com>
This commit is contained in:
@@ -17,6 +17,8 @@ use sp_io::{
|
||||
use sp_runtime::{print, traits::{BlakeTwo256, Hash}};
|
||||
#[cfg(not(feature = "std"))]
|
||||
use sp_core::{ed25519, sr25519};
|
||||
#[cfg(not(feature = "std"))]
|
||||
use sp_sandbox::Value;
|
||||
|
||||
extern "C" {
|
||||
#[allow(dead_code)]
|
||||
@@ -133,8 +135,8 @@ sp_core::wasm_export_functions! {
|
||||
execute_sandboxed(
|
||||
&code,
|
||||
&[
|
||||
sp_sandbox::TypedValue::I32(0x12345678),
|
||||
sp_sandbox::TypedValue::I64(0x1234567887654321),
|
||||
Value::I32(0x12345678),
|
||||
Value::I64(0x1234567887654321),
|
||||
],
|
||||
).is_ok()
|
||||
}
|
||||
@@ -143,10 +145,10 @@ sp_core::wasm_export_functions! {
|
||||
let ok = match execute_sandboxed(
|
||||
&code,
|
||||
&[
|
||||
sp_sandbox::TypedValue::I32(0x1336),
|
||||
Value::I32(0x1336),
|
||||
]
|
||||
) {
|
||||
Ok(sp_sandbox::ReturnValue::Value(sp_sandbox::TypedValue::I32(0x1337))) => true,
|
||||
Ok(sp_sandbox::ReturnValue::Value(Value::I32(0x1337))) => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
@@ -165,6 +167,22 @@ sp_core::wasm_export_functions! {
|
||||
code
|
||||
}
|
||||
|
||||
|
||||
fn test_sandbox_get_global_val(code: Vec<u8>) -> i64 {
|
||||
let env_builder = sp_sandbox::EnvironmentDefinitionBuilder::new();
|
||||
let instance = if let Ok(i) = sp_sandbox::Instance::new(&code, &env_builder, &mut ()) {
|
||||
i
|
||||
} else {
|
||||
return 20;
|
||||
};
|
||||
|
||||
match instance.get_global_val("test_global") {
|
||||
Some(sp_sandbox::Value::I64(val)) => val,
|
||||
None => 30,
|
||||
val => 40,
|
||||
}
|
||||
}
|
||||
|
||||
fn test_offchain_local_storage() -> bool {
|
||||
let kind = sp_core::offchain::StorageKind::PERSISTENT;
|
||||
assert_eq!(sp_io::offchain::local_storage_get(kind, b"test"), None);
|
||||
@@ -262,7 +280,7 @@ sp_core::wasm_export_functions! {
|
||||
#[cfg(not(feature = "std"))]
|
||||
fn execute_sandboxed(
|
||||
code: &[u8],
|
||||
args: &[sp_sandbox::TypedValue],
|
||||
args: &[Value],
|
||||
) -> Result<sp_sandbox::ReturnValue, sp_sandbox::HostError> {
|
||||
struct State {
|
||||
counter: u32,
|
||||
@@ -270,7 +288,7 @@ fn execute_sandboxed(
|
||||
|
||||
fn env_assert(
|
||||
_e: &mut State,
|
||||
args: &[sp_sandbox::TypedValue],
|
||||
args: &[Value],
|
||||
) -> Result<sp_sandbox::ReturnValue, sp_sandbox::HostError> {
|
||||
if args.len() != 1 {
|
||||
return Err(sp_sandbox::HostError);
|
||||
@@ -284,14 +302,14 @@ fn execute_sandboxed(
|
||||
}
|
||||
fn env_inc_counter(
|
||||
e: &mut State,
|
||||
args: &[sp_sandbox::TypedValue],
|
||||
args: &[Value],
|
||||
) -> Result<sp_sandbox::ReturnValue, sp_sandbox::HostError> {
|
||||
if args.len() != 1 {
|
||||
return Err(sp_sandbox::HostError);
|
||||
}
|
||||
let inc_by = args[0].as_i32().ok_or_else(|| sp_sandbox::HostError)?;
|
||||
e.counter += inc_by as u32;
|
||||
Ok(sp_sandbox::ReturnValue::Value(sp_sandbox::TypedValue::I32(e.counter as i32)))
|
||||
Ok(sp_sandbox::ReturnValue::Value(Value::I32(e.counter as i32)))
|
||||
}
|
||||
|
||||
let mut state = State { counter: 0 };
|
||||
|
||||
Reference in New Issue
Block a user