-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added named keys and dictionary methods to benchmark.
- Loading branch information
Showing
6 changed files
with
90 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
extern crate alloc; | ||
|
||
pub mod benchmark; | ||
mod storage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters