@@ -28,6 +28,74 @@ use trie::{TrieConfiguration, trie_types::Layout};
2828use std:: { collections:: HashMap , convert:: TryFrom } ;
2929
3030use externalities:: { with_externalities, set_and_run_with_externalities, ExternalitiesExt } ;
31+ use sandbox:: { EnvironmentDefinitionBuilder , Error , HostError , Instance , ReturnValue , TypedValue } ;
32+
33+ // environmental!(ext: trait Externalities<Blake2Hasher>);
34+
35+ fn execute_wasm ( code : & [ u8 ] , args : & [ TypedValue ] ) -> Result < ReturnValue , HostError > {
36+ struct State {
37+ counter : u32 ,
38+ }
39+ fn check_read_proof ( _e : & mut State , _args : & [ TypedValue ] ) -> Result < ReturnValue , HostError > {
40+ // TODO: Add true verification here
41+ Ok ( ReturnValue :: Value ( TypedValue :: I32 ( 1 ) ) )
42+ }
43+
44+ let mut env_builder = EnvironmentDefinitionBuilder :: new ( ) ;
45+
46+ let mut state = State { counter : 0 } ;
47+
48+ env_builder. add_host_func ( "env" , "ext_check_read_proof" , check_read_proof) ;
49+
50+ let memory = match sandbox:: Memory :: new ( 100 , Some ( 100 ) ) {
51+ Ok ( m) => m,
52+ Err ( _) => unreachable ! ( "
53+ Memory::new() can return Err only if parameters are borked; \
54+ We passing params here explicitly and they're correct; \
55+ Memory::new() can't return a Error qed"
56+ ) ,
57+ } ;
58+
59+ env_builder. add_memory ( "env" , "memory" , memory) ;
60+ let mut instance = Instance :: new ( code, & env_builder, & mut state) ?;
61+ let result = instance. invoke ( b"check_read_proof" , args, & mut state) ;
62+
63+ result. map_err ( |err| {
64+ HostError
65+ } )
66+ }
67+
68+ #[ test]
69+ fn invoke_proof ( ) {
70+ let code = wabt:: wat2wasm ( r#"
71+ (module
72+ (type $t0 (func (result i32)))
73+ (import "env" "memory" (memory $env.memory 17))
74+ (import "env" "ext_check_read_proof" (func $ext_check_read_proof (type $t0)))
75+ (func $check_read_proof (type $t0) (result i32)
76+ (local $l0 i32)
77+ call $ext_check_read_proof
78+ set_local $l0
79+ get_local $l0
80+ return)
81+ (table $__indirect_function_table 1 1 anyfunc)
82+ (global $__data_end i32 (i32.const 1048610))
83+ (global $__heap_base i32 (i32.const 1048610))
84+ (global $__rustc_debug_gdb_scripts_section__ i32 (i32.const 1048576))
85+ (export "__indirect_function_table" (table 0))
86+ (export "__data_end" (global 0))
87+ (export "__heap_base" (global 1))
88+ (export "__rustc_debug_gdb_scripts_section__" (global 2))
89+ (export "check_read_proof" (func $check_read_proof))
90+ ) "# ) . unwrap ( ) ;
91+
92+ let result = execute_wasm (
93+ & code,
94+ & [ ] ,
95+ ) ;
96+ assert_eq ! ( result. unwrap( ) , ReturnValue :: Value ( TypedValue :: I32 ( 1 ) ) ) ;
97+ }
98+
3199
32100/// Additional bounds for `Hasher` trait for with_std.
33101pub trait HasherBounds { }
@@ -179,6 +247,16 @@ impl OtherApi for () {
179247 ) . unwrap_or ( 0 )
180248 }
181249
250+ fn run_wasm ( ) {
251+ use std:: fs;
252+
253+ // TODO: Read wasm from chain
254+ let code = fs:: read ( "/tmp/proof.compact.wasm" ) . expect ( "Wasm file not found" ) ;
255+ let args = [ ] ;
256+ let res = execute_wasm ( & code, & args) ;
257+ println ! ( "result: {:?}" , res) ;
258+ }
259+
182260 fn print_num ( val : u64 ) {
183261 println ! ( "{}" , val) ;
184262 }
0 commit comments