From a4dc251fff779649afcc6dc11c5ced8de7b2a072 Mon Sep 17 00:00:00 2001 From: prin-r Date: Tue, 7 Jul 2020 01:37:39 +0700 Subject: [PATCH] add cache --- mumu/contract/src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 mumu/contract/src/main.rs diff --git a/mumu/contract/src/main.rs b/mumu/contract/src/main.rs new file mode 100644 index 0000000..1fc0bab --- /dev/null +++ b/mumu/contract/src/main.rs @@ -0,0 +1,38 @@ +#![cfg_attr( + not(target_arch = "wasm32"), + crate_type = "target arch should be wasm32" +)] +#![no_main] + +use casperlabs_contract::{ + contract_api::{runtime, storage}, + unwrap_or_revert::UnwrapOrRevert, +}; +use casperlabs_types::{ApiError, Key, URef}; + +const KEY: &str = "special_value"; + +fn store(value: String) { + // Store `value` under a new unforgeable reference. + let value_ref: URef = storage::new_uref(value); + + // Wrap the unforgeable reference in a value of type `Key`. + let value_key: Key = value_ref.into(); + + // Store this key under the name "special_value" in context-local storage. + runtime::put_key(KEY, value_key); +} + +// All session code must have a `call` entrypoint. +#[no_mangle] +pub extern "C" fn call() { + // Get the optional first argument supplied to the argument. + let value: String = runtime::get_arg(0) + // Unwrap the `Option`, returning an error if there was no argument supplied. + .unwrap_or_revert_with(ApiError::MissingArgument) + // Unwrap the `Result` containing the deserialized argument or return an error if there was + // a deserialization error. + .unwrap_or_revert_with(ApiError::InvalidArgument); + + store(value); +}