Skip to content

Commit

Permalink
Fixed macro tests.
Browse files Browse the repository at this point in the history
Added named keys and dictionary methods to benchmark.
  • Loading branch information
kubaplas committed Apr 26, 2024
1 parent 4f87480 commit 6199aa9
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 7 deletions.
1 change: 1 addition & 0 deletions benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
odra = { path = "../odra" }
odra-test = { path = "../odra-test", optional = true }
odra-modules = { path = "../modules" }
base64 = { version = "0.22.0", features = ["alloc"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
serde_json = "1.0.113"
Expand Down
8 changes: 8 additions & 0 deletions benchmark/bin/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ pub fn main() {
// Events
contract.emit_event();

// Named key
contract.set_named_key("Hello, world!".to_string());
assert_eq!(contract.get_named_key(), "Hello, world!".to_string());

// Dictionary
contract.set_dictionary("My key".to_string(), 42.into());
assert_eq!(contract.get_dictionary("My key".to_string()), 42.into());

// convert gas_report to json and dump it into a file
let gas_report = env.gas_report();
let gas_report_json = serde_json::to_string(&gas_report).unwrap();
Expand Down
21 changes: 20 additions & 1 deletion benchmark/src/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::storage::{DictionaryStorage, NamedKeyStorage};
use odra::casper_types::{U256, U512};
use odra::prelude::*;
use odra::{List, Mapping, SubModule, Var};
Expand All @@ -10,7 +11,9 @@ pub struct Benchmark {
struct_variable: Var<StructVariable>,
mapping: Mapping<u32, bool>,
list: List<u32>,
submodule: SubModule<Erc20>
submodule: SubModule<Erc20>,
named_key: SubModule<NamedKeyStorage>,
dictionary: SubModule<DictionaryStorage>
}

#[odra::module]
Expand Down Expand Up @@ -43,6 +46,22 @@ impl Benchmark {
self.mapping.get_or_default(&key)
}

pub fn set_named_key(&mut self, value: String) {
self.named_key.set(value);
}

pub fn get_named_key(&self) -> String {
self.named_key.get()
}

pub fn set_dictionary(&mut self, key: String, value: U256) {
self.dictionary.set(key, value);
}

pub fn get_dictionary(&self, key: String) -> U256 {
self.dictionary.get_or_default(key)
}

pub fn push_list(&mut self, value: u32) {
self.list.push(value);
}
Expand Down
1 change: 1 addition & 0 deletions benchmark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
extern crate alloc;

pub mod benchmark;
mod storage;
54 changes: 54 additions & 0 deletions benchmark/src/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use base64::prelude::BASE64_STANDARD;
use base64::Engine;

use odra::casper_types::bytesrepr::ToBytes;
use odra::casper_types::U256;
use odra::prelude::*;
use odra::ExecutionError::UnwrapError;
use odra::UnwrapOrRevert;

const VALUE_KEY: &str = "value";
const DICT_KEY: &str = "dict";

#[odra::module]
/// Storage module for the named key.
pub struct NamedKeyStorage;

#[odra::module]
impl NamedKeyStorage {
/// Sets the value.
pub fn set(&self, value: String) {
self.env().set_named_value(VALUE_KEY, value);
}

/// Gets the value.
pub fn get(&self) -> String {
self.env()
.get_named_value(VALUE_KEY)
.unwrap_or_revert_with(&self.env(), UnwrapError)
}
}

#[odra::module]
/// Storage module for the dictionary value.
pub struct DictionaryStorage;

#[odra::module]
impl DictionaryStorage {
/// Sets the value.
pub fn set(&self, key: String, value: U256) {
self.env()
.set_dictionary_value(DICT_KEY, self.key(key), value);
}

/// Gets the value.
pub fn get_or_default(&self, key: String) -> U256 {
self.env()
.get_dictionary_value(DICT_KEY, self.key(key))
.unwrap_or_default()
}

fn key(&self, key: String) -> String {
BASE64_STANDARD.encode(self.env().hash(key.to_bytes().unwrap()))
}
}
12 changes: 6 additions & 6 deletions odra-macros/src/ast/schema/custom_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ mod tests {
.chain(odra::prelude::vec![])
.chain(<String as odra::schema::SchemaCustomTypes>::schema_types())
.chain(<u32 as odra::schema::SchemaCustomTypes>::schema_types())
.collect::<Vec<_>>()
.collect::<odra::prelude::Vec<_>>()
}
}

#[automatically_derived]
#[cfg(not(target_arch = "wasm32"))]
impl odra::schema::NamedCLTyped for MyType {
fn ty() -> odra::schema::casper_contract_schema::NamedCLType {
odra::schema::casper_contract_schema::NamedCLType::Custom(String::from(
odra::schema::casper_contract_schema::NamedCLType::Custom(odra::prelude::String::from(
"MyType"
))
}
Expand Down Expand Up @@ -215,15 +215,15 @@ mod tests {
))
])
.chain(odra::prelude::vec![])
.collect::<Vec<_>>()
.collect::<odra::prelude::Vec<_>>()
}
}

#[automatically_derived]
#[cfg(not(target_arch = "wasm32"))]
impl odra::schema::NamedCLTyped for MyType {
fn ty() -> odra::schema::casper_contract_schema::NamedCLType {
odra::schema::casper_contract_schema::NamedCLType::Custom(String::from(
odra::schema::casper_contract_schema::NamedCLType::Custom(odra::prelude::String::from(
"MyType"
))
}
Expand Down Expand Up @@ -269,15 +269,15 @@ mod tests {
)),
Some(odra::schema::custom_struct("MyType::D", odra::prelude::vec![]))
])
.collect::<Vec<_>>()
.collect::<odra::prelude::Vec<_>>()
}
}

#[automatically_derived]
#[cfg(not(target_arch = "wasm32"))]
impl odra::schema::NamedCLTyped for MyType {
fn ty() -> odra::schema::casper_contract_schema::NamedCLType {
odra::schema::casper_contract_schema::NamedCLType::Custom(String::from(
odra::schema::casper_contract_schema::NamedCLType::Custom(odra::prelude::String::from(
"MyType"
))
}
Expand Down

0 comments on commit 6199aa9

Please sign in to comment.