From cbf4329f9794217b596d61ed0a9ddea168d95512 Mon Sep 17 00:00:00 2001 From: Zett98 Date: Wed, 12 Oct 2022 12:09:47 +0200 Subject: [PATCH 1/3] up --- crates/vm_library/src/run_loop.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/vm_library/src/run_loop.rs b/crates/vm_library/src/run_loop.rs index 8d1cb07..2860068 100644 --- a/crates/vm_library/src/run_loop.rs +++ b/crates/vm_library/src/run_loop.rs @@ -95,16 +95,16 @@ fn handle_transaction( .io .write_with_fail(&ServerMessage::TakeTickets(address.address.clone())) .map_err(|err| VmError::RuntimeErr(err.to_string()))?; - 'd: loop { - match context.io.read() { - ClientMessage::GiveTickets(ticket) => { - tickets2.extend(ticket.into_iter().map(|(x, y)| Ticket::new(x, y))); - break 'd; - } - ClientMessage::NoopTransaction => (), - _ => panic!("bad format"), - } - } + // 'd: loop { + // match context.io.read() { + // ClientMessage::GiveTickets(ticket) => { + // tickets2.extend(ticket.into_iter().map(|(x, y)| Ticket::new(x, y))); + // break 'd; + // } + // ClientMessage::NoopTransaction => (), + // _ => panic!("bad format"), + // } + // } context.ticket_table.populate(&tickets2); let new_limit = From 449c652a848a79e2687979a9f8db53561e19855e Mon Sep 17 00:00:00 2001 From: Zett98 Date: Tue, 18 Oct 2022 15:08:34 +0200 Subject: [PATCH 2/3] move to json --- crates/vm_library/benches/util.rs | 2 +- crates/vm_library/src/outgoing.rs | 37 ++++++++++++++++++++++--------- crates/vm_library/src/state.rs | 6 ++--- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/crates/vm_library/benches/util.rs b/crates/vm_library/benches/util.rs index 68cc346..3d36c3a 100644 --- a/crates/vm_library/benches/util.rs +++ b/crates/vm_library/benches/util.rs @@ -137,7 +137,7 @@ pub fn invoke(operation: String) -> impl FnMut(&mut IO) { dbg!(s); todo!() } - ServerMessage::TakeTickets(_) => io.write(tickets.as_bytes()), + ServerMessage::TakeTickets(_) => (), _ => continue, } } diff --git a/crates/vm_library/src/outgoing.rs b/crates/vm_library/src/outgoing.rs index b7c6452..c769e47 100644 --- a/crates/vm_library/src/outgoing.rs +++ b/crates/vm_library/src/outgoing.rs @@ -6,27 +6,44 @@ use crate::state::ContractType; #[derive(Debug, Deserialize, Serialize)] pub struct SetOwned { pub key: String, - #[serde(serialize_with = "base64ser", deserialize_with = "base64deser")] + #[serde(serialize_with = "json_ser", deserialize_with = "json_deser")] pub value: ContractType, } -fn base64ser(t: &ContractType, serializer: S) -> Result +fn json_ser(t: &ContractType, serializer: S) -> Result where S: Serializer, { - let ok = base64::encode( - bincode::serialize(&t).map_err(|err| serde::ser::Error::custom(err.to_string()))?, - ); - serializer.serialize_str(&ok) + let val = + serde_json::to_string(&t).map_err(|err| serde::ser::Error::custom(err.to_string()))?; + + serializer.serialize_str(&val) } -fn base64deser<'de, S>(deser: S) -> Result +fn json_deser<'de, S>(deser: S) -> Result where S: Deserializer<'de>, { - let s: &str = Deserialize::deserialize(deser)?; - let ok = base64::decode(s).map_err(|err| serde::de::Error::custom(err.to_string()))?; - let ok = bincode::deserialize(&ok).map_err(|err| serde::de::Error::custom(err.to_string()))?; + let s: String = Deserialize::deserialize(deser)?; + let ok = serde_json::from_str(&s).map_err(|err| serde::de::Error::custom(err.to_string()))?; Ok(ok) } +// fn base64ser(t: &ContractType, serializer: S) -> Result +// where +// S: Serializer, +// { +// let ok = base64::encode( +// bincode::serialize(&t).map_err(|err| serde::ser::Error::custom(err.to_string()))?, +// ); +// serializer.serialize_str(&ok) +// } +// fn base64deser<'de, S>(deser: S) -> Result +// where +// S: Deserializer<'de>, +// { +// let s: &str = Deserialize::deserialize(deser)?; +// let ok = base64::decode(s).map_err(|err| serde::de::Error::custom(err.to_string()))?; +// let ok = bincode::deserialize(&ok).map_err(|err| serde::de::Error::custom(err.to_string()))?; +// Ok(ok) +// } #[repr(transparent)] #[derive(Deserialize, Serialize, Debug)] pub struct Init(pub FnvHashMap); diff --git a/crates/vm_library/src/state.rs b/crates/vm_library/src/state.rs index 421611e..4cc22f7 100644 --- a/crates/vm_library/src/state.rs +++ b/crates/vm_library/src/state.rs @@ -68,10 +68,8 @@ impl State { pub fn from_init(&mut self, init: Init) -> VMResult<()> { self.table.clear(); init.0.iter().try_for_each(|(key, value)| { - let contract_type: ContractType = bincode::deserialize( - &base64::decode(value).map_err(|err| VmError::DeserializeErr(err.to_string()))?, - ) - .map_err(|err| VmError::DeserializeErr(err.to_string()))?; + let contract_type: ContractType = serde_json::from_str(value) + .map_err(|err| VmError::DeserializeErr(err.to_string()))?; self.table.insert(key.clone(), contract_type); Ok(()) }) From da60e2415de4cad0169dee3bfb7a119d839474b6 Mon Sep 17 00:00:00 2001 From: Zett98 Date: Tue, 18 Oct 2022 16:28:50 +0200 Subject: [PATCH 3/3] fix storage --- crates/vm_library/src/run_loop.rs | 6 +++--- crates/vm_library/src/state.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/vm_library/src/run_loop.rs b/crates/vm_library/src/run_loop.rs index 2860068..f855b93 100644 --- a/crates/vm_library/src/run_loop.rs +++ b/crates/vm_library/src/run_loop.rs @@ -170,7 +170,7 @@ fn handle_originate( let contract_type = ContractType { self_: addr.clone(), originated_by, - storage: serde_json::to_vec(&initial_storage).expect("error"), + storage: serde_json::to_string(&initial_storage).expect("error"), module: Some(Box::from(module)), serialized_module: serialized, constants: serde_json::to_vec(&constants).expect("error"), @@ -211,7 +211,7 @@ fn handle_invoke( let mut contract = contract; contract.init()?; let initial_storage: Value = - serde_json::from_slice(&contract.storage).expect("error"); + serde_json::from_str(&contract.storage).expect("error"); let constantst: Vec<(i32, Value)> = serde_json::from_slice(&contract.constants).expect("error"); let invoke_payload = InvokeManaged { @@ -244,7 +244,7 @@ fn handle_invoke( gas_limit = remaining_gas; context.ticket_table.finalize(); let serialized_storage = - serde_json::to_vec(&new_storage).expect("serialization_error"); + serde_json::to_string(&new_storage).expect("serialization_error"); { let deposit = unsafe { &mut CONSUMEDTICKETS }; let address = contract_addr_to_string(&address); diff --git a/crates/vm_library/src/state.rs b/crates/vm_library/src/state.rs index 4cc22f7..1811e48 100644 --- a/crates/vm_library/src/state.rs +++ b/crates/vm_library/src/state.rs @@ -13,7 +13,7 @@ use crate::{ pub struct ContractType { pub self_: ContractAddress, pub originated_by: String, - pub storage: Vec, + pub storage: String, #[serde(skip_deserializing, skip_serializing)] pub module: Option>, pub serialized_module: Vec, @@ -21,7 +21,7 @@ pub struct ContractType { pub entrypoints: Option>>, } impl ContractType { - pub fn set_storage(&mut self, s: Vec) { + pub fn set_storage(&mut self, s: String) { self.storage = s } pub fn init(&mut self) -> VMResult<()> {