Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie game #32

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/vm_library/benches/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
37 changes: 27 additions & 10 deletions crates/vm_library/src/outgoing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(t: &ContractType, serializer: S) -> Result<S::Ok, S::Error>
fn json_ser<S>(t: &ContractType, serializer: S) -> Result<S::Ok, S::Error>
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<ContractType, S::Error>
fn json_deser<'de, S>(deser: S) -> Result<ContractType, S::Error>
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<S>(t: &ContractType, serializer: S) -> Result<S::Ok, S::Error>
// 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<ContractType, S::Error>
// 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<String, String>);
Expand Down
26 changes: 13 additions & 13 deletions crates/vm_library/src/run_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions crates/vm_library/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ use crate::{
pub struct ContractType {
pub self_: ContractAddress,
pub originated_by: String,
pub storage: Vec<u8>,
pub storage: String,
#[serde(skip_deserializing, skip_serializing)]
pub module: Option<Box<Module>>,
pub serialized_module: Vec<u8>,
pub constants: Vec<u8>,
pub entrypoints: Option<FnvHashMap<String, Vec<Path>>>,
}
impl ContractType {
pub fn set_storage(&mut self, s: Vec<u8>) {
pub fn set_storage(&mut self, s: String) {
self.storage = s
}
pub fn init(&mut self) -> VMResult<()> {
Expand Down Expand Up @@ -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(())
})
Expand Down