Skip to content

Commit

Permalink
update base64
Browse files Browse the repository at this point in the history
  • Loading branch information
pkhry committed Sep 26, 2022
1 parent 3e40a75 commit e74a6a5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 26 deletions.
6 changes: 3 additions & 3 deletions crates/vm_library/src/incoming.rs
Original file line number Diff line number Diff line change
@@ -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<Ticket>,
pub tickets: &'a [Ticket],
pub source: String,
pub sender: String,
pub self_addr: String,
Expand Down
3 changes: 1 addition & 2 deletions crates/vm_library/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::{

pub fn invoke_managed(t: InvokeManaged) -> VMResult<ExecutionResult> {
let arena = unsafe { &mut ARENA };

let module = t.mod_;
let env = Context {
inner: Rc::new(RefCell::new(Inner {
Expand All @@ -30,7 +29,7 @@ pub fn invoke_managed(t: InvokeManaged) -> VMResult<ExecutionResult> {
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)))?,
);

Expand Down
6 changes: 3 additions & 3 deletions crates/vm_library/src/run_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions crates/vm_library/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct ContractType {
#[serde(with = "serde_bytes")]
pub storage: Vec<u8>,
#[serde(skip_deserializing, skip_serializing)]
pub module: Box<Option<Module>>,
pub module: Option<Box<Module>>,
#[serde(with = "serde_bytes")]
pub serialized_module: Vec<u8>,
#[serde(with = "serde_bytes")]
Expand All @@ -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>(())
Expand Down
24 changes: 17 additions & 7 deletions crates/vm_library/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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)>,
}
21 changes: 16 additions & 5 deletions crates/vm_library/tests/fa12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use vm_library::{
execution_result::ExecutionResult,
instance::invoke_managed,
managed::value::{Union, Value},
ticket_table::Ticket,
};

mod common;
Expand All @@ -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<Ticket> = vec![];
let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone());
let ExecutionResult {
new_storage, ops, ..
} = invoke_managed(init).unwrap();
Expand Down Expand Up @@ -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<Ticket> = vec![];
let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone());
let ExecutionResult {
new_storage, ops, ..
} = invoke_managed(init).unwrap();
Expand Down Expand Up @@ -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<Ticket> = 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(),
Expand Down Expand Up @@ -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<Ticket> = 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(),
Expand All @@ -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<Ticket> = vec![];
let init = common::create_incoming_managed(&module, &deser, &tickets, arg, storage.clone());
let ExecutionResult {
new_storage, ops, ..
} = invoke_managed(init).unwrap();
Expand Down
13 changes: 10 additions & 3 deletions crates/vm_library/tests/increment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use vm_library::{
execution_result::ExecutionResult,
instance::invoke_managed,
managed::value::{Union, Value},
ticket_table::Ticket,
};

mod common;
Expand All @@ -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<Ticket> = 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()))
}
Expand All @@ -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<Ticket> = 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()))
}
Expand All @@ -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<Ticket> = 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()))
}

0 comments on commit e74a6a5

Please sign in to comment.