From e74a6a5b6674bed45d078027ef7374a1adb60627 Mon Sep 17 00:00:00 2001 From: Zett98 Date: Mon, 26 Sep 2022 21:56:26 +0200 Subject: [PATCH] update base64 --- crates/vm_library/src/incoming.rs | 6 +++--- crates/vm_library/src/instance.rs | 3 +-- crates/vm_library/src/run_loop.rs | 6 +++--- crates/vm_library/src/state.rs | 6 +++--- crates/vm_library/tests/common/mod.rs | 24 +++++++++++++++++------- crates/vm_library/tests/fa12.rs | 21 ++++++++++++++++----- crates/vm_library/tests/increment.rs | 13 ++++++++++--- 7 files changed, 53 insertions(+), 26 deletions(-) diff --git a/crates/vm_library/src/incoming.rs b/crates/vm_library/src/incoming.rs index fafc037..89733b5 100644 --- a/crates/vm_library/src/incoming.rs +++ b/crates/vm_library/src/incoming.rs @@ -1,12 +1,12 @@ use wasmer::Module; use crate::{managed::value::Value, ticket_table::Ticket}; -pub struct InvokeManaged { - pub mod_: Module, +pub struct InvokeManaged<'a> { + pub mod_: &'a Module, pub arg: Value, pub initial_storage: Value, pub constants: Vec<(i32, Value)>, - pub tickets: Vec, + pub tickets: &'a [Ticket], pub source: String, pub sender: String, pub self_addr: String, diff --git a/crates/vm_library/src/instance.rs b/crates/vm_library/src/instance.rs index e6103e6..c8f11cd 100644 --- a/crates/vm_library/src/instance.rs +++ b/crates/vm_library/src/instance.rs @@ -14,7 +14,6 @@ use crate::{ pub fn invoke_managed(t: InvokeManaged) -> VMResult { let arena = unsafe { &mut ARENA }; - let module = t.mod_; let env = Context { inner: Rc::new(RefCell::new(Inner { @@ -30,7 +29,7 @@ pub fn invoke_managed(t: InvokeManaged) -> VMResult { let store = module.store(); let instance = Box::from( - Instance::new(&module, &imports::make_imports(&env, store)) + Instance::new(module, &imports::make_imports(&env, store)) .map_err(|err| VmError::RuntimeErr(format!("Failed to create instance {}", err)))?, ); diff --git a/crates/vm_library/src/run_loop.rs b/crates/vm_library/src/run_loop.rs index c2c4459..7b875c5 100644 --- a/crates/vm_library/src/run_loop.rs +++ b/crates/vm_library/src/run_loop.rs @@ -165,7 +165,7 @@ fn handle_originate( self_: addr.clone(), originated_by, storage: bincode::serialize(&initial_storage).expect("error"), - module: Box::from(Some(module)), + module: Some(Box::from(module)), serialized_module: serialized, constants: bincode::serialize(&constants).expect("error"), }; @@ -207,11 +207,11 @@ fn handle_invoke( let constantst: Vec<(i32, Value)> = bincode::deserialize(&contract.constants).expect("error"); let invoke_payload = InvokeManaged { - mod_: (*contract.module).clone().unwrap(), + mod_: (contract.module.as_ref().unwrap()), arg, initial_storage, constants: constantst, - tickets, + tickets: &tickets, source: transaction.source.clone(), sender: transaction .sender diff --git a/crates/vm_library/src/state.rs b/crates/vm_library/src/state.rs index b0d7ffe..ef36051 100644 --- a/crates/vm_library/src/state.rs +++ b/crates/vm_library/src/state.rs @@ -15,7 +15,7 @@ pub struct ContractType { #[serde(with = "serde_bytes")] pub storage: Vec, #[serde(skip_deserializing, skip_serializing)] - pub module: Box>, + pub module: Option>, #[serde(with = "serde_bytes")] pub serialized_module: Vec, #[serde(with = "serde_bytes")] @@ -26,9 +26,9 @@ impl ContractType { self.storage = s } pub fn init(&mut self) -> VMResult<()> { - match *self.module { + match self.module { None => { - self.module = Box::from(Some(unsafe { + self.module = Some(Box::from(unsafe { Module::deserialize(&compile_store::new_headless(), &self.serialized_module) }?)); Ok::<(), VmError>(()) diff --git a/crates/vm_library/tests/common/mod.rs b/crates/vm_library/tests/common/mod.rs index 2647270..7850d88 100644 --- a/crates/vm_library/tests/common/mod.rs +++ b/crates/vm_library/tests/common/mod.rs @@ -1,20 +1,30 @@ use serde::{Deserialize, Serialize}; -use vm_library::{compile_store, incoming::InvokeManaged, managed::value::Value}; +use vm_library::{ + compile_store, incoming::InvokeManaged, managed::value::Value, ticket_table::Ticket, +}; use wasmer::{wat2wasm, Module}; - -pub fn create_incoming_managed(s: String, arg: Value, initial_storage: Value) -> InvokeManaged { +pub fn deser(s: String) -> (Init, Module) { let deser: Init = serde_json::from_str(&s).unwrap(); let mod_ = Module::new( &compile_store::new_compile_store(), wat2wasm(deser.module_.as_bytes()).unwrap(), ) .unwrap(); + (deser, mod_) +} +pub fn create_incoming_managed<'a>( + mode: &'a Module, + deser: &'a Init, + tickets: &'a [Ticket], + arg: Value, + initial_storage: Value, +) -> InvokeManaged<'a> { InvokeManaged { - mod_, + mod_: mode, arg, initial_storage, - constants: deser.constants, - tickets: vec![], + constants: deser.constants.clone(), + tickets, source: "tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM".to_string(), sender: "tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM".to_string(), self_addr: "KT1WiBZHtvv3EczaN628DkNob4cayHzTEDNK".to_string(), @@ -23,7 +33,7 @@ pub fn create_incoming_managed(s: String, arg: Value, initial_storage: Value) -> } #[derive(Deserialize, Serialize)] -struct Init { +pub struct Init { module_: String, constants: Vec<(i32, Value)>, } diff --git a/crates/vm_library/tests/fa12.rs b/crates/vm_library/tests/fa12.rs index 73bc728..e86fed6 100644 --- a/crates/vm_library/tests/fa12.rs +++ b/crates/vm_library/tests/fa12.rs @@ -3,6 +3,7 @@ use vm_library::{ execution_result::ExecutionResult, instance::invoke_managed, managed::value::{Union, Value}, + ticket_table::Ticket, }; mod common; @@ -29,7 +30,9 @@ fn get_balance() { let arg = Value::Union(Union::Right(bump)); let bump = arena.insert(arg); let arg = Value::Union(Union::Left(bump)); - let init = common::create_incoming_managed(payload, arg, storage.clone()); + let (deser, module) = common::deser(payload); + let tickets: Vec = vec![]; + let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone()); let ExecutionResult { new_storage, ops, .. } = invoke_managed(init).unwrap(); @@ -66,7 +69,9 @@ fn get_total_supply() { let arg = Value::Union(Union::Right(bump)); let bump = arena.insert(arg); let arg = Value::Union(Union::Left(bump)); - let init = common::create_incoming_managed(payload, arg, storage.clone()); + let (deser, module) = common::deser(payload); + let tickets: Vec = vec![]; + let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone()); let ExecutionResult { new_storage, ops, .. } = invoke_managed(init).unwrap(); @@ -108,7 +113,9 @@ fn approve() { let arg = Value::Union(Union::Left(bump)); let bump = arena.insert(arg); let arg = Value::Union(Union::Left(bump)); - let init = common::create_incoming_managed(payload, arg, storage); + let (deser, module) = common::deser(payload); + let tickets: Vec = vec![]; + let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone()); let ExecutionResult { new_storage, .. } = invoke_managed(init).unwrap(); assert_eq!( serde_json::to_string(&new_storage).unwrap(), @@ -154,7 +161,9 @@ fn transfer() { let arg = serde_json::from_str(r#"["Pair",["String","tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM"],["Pair",["String","tz2AcXz8WUu51YYdE5Rsnosxd1hkhW9tG7pd"],["Int","5"]]]"#).unwrap(); let bump = arena.insert(arg); let arg = Value::Union(Union::Right(bump)); - let init = common::create_incoming_managed(payload, arg, storage); + let (deser, module) = common::deser(payload); + let tickets: Vec = vec![]; + let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone()); let ExecutionResult { new_storage, .. } = invoke_managed(init).unwrap(); assert_eq!( serde_json::to_string(&new_storage).unwrap(), @@ -175,7 +184,9 @@ fn get_allowance() { let arg = Value::Union(Union::Left(bump)); let bump = arena.insert(arg); let arg = Value::Union(Union::Left(bump)); - let init = common::create_incoming_managed(payload, arg, storage.clone()); + let (deser, module) = common::deser(payload); + let tickets: Vec = vec![]; + let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone()); let ExecutionResult { new_storage, ops, .. } = invoke_managed(init).unwrap(); diff --git a/crates/vm_library/tests/increment.rs b/crates/vm_library/tests/increment.rs index e6a4e96..c0082f5 100644 --- a/crates/vm_library/tests/increment.rs +++ b/crates/vm_library/tests/increment.rs @@ -3,6 +3,7 @@ use vm_library::{ execution_result::ExecutionResult, instance::invoke_managed, managed::value::{Union, Value}, + ticket_table::Ticket, }; mod common; @@ -21,7 +22,9 @@ fn increment() { let arg = Value::Union(Union::Right(bump)); let bump = arena.insert(arg); let arg = Value::Union(Union::Left(bump)); - let init = common::create_incoming_managed(payload, arg, storage); + let (deser, module) = common::deser(payload); + let tickets: Vec = vec![]; + let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone()); let ExecutionResult { new_storage, .. } = invoke_managed(init).unwrap(); assert_eq!(new_storage, Value::Int(5.into())) } @@ -39,7 +42,9 @@ fn decrement() { let arg = Value::Union(Union::Left(bump)); let bump = arena.insert(arg); let arg = Value::Union(Union::Left(bump)); - let init = common::create_incoming_managed(payload, arg, storage); + let (deser, module) = common::deser(payload); + let tickets: Vec = vec![]; + let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone()); let ExecutionResult { new_storage, .. } = invoke_managed(init).unwrap(); assert_eq!(new_storage, Value::Int(2.into())) } @@ -55,7 +60,9 @@ fn reset() { let arena = unsafe { &mut ARENA }; let bump = arena.insert(arg); let arg = Value::Union(Union::Right(bump)); - let init = common::create_incoming_managed(payload, arg, storage); + let (deser, module) = common::deser(payload); + let tickets: Vec = vec![]; + let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone()); let ExecutionResult { new_storage, .. } = invoke_managed(init).unwrap(); assert_eq!(new_storage, Value::Int(0.into())) }