Crates.io | mavryk-smart-rollup-storage |
lib.rs | mavryk-smart-rollup-storage |
version | 0.2.2 |
source | src |
created_at | 2024-07-30 11:31:21.014508 |
updated_at | 2024-07-30 11:31:21.014508 |
description | Higher-level transactional account view over Mavryk Smart Rollup durable storage. |
homepage | |
repository | https://gitlab.com/mavryk-network/mavryk-protocol.git |
max_upload_size | |
id | 1319692 |
size | 32,878 |
Transactional storage for Mavryk Smart Rollup kernels.
This crate supports dealing with objects for updating storage. All objects are stored in durable storage.
To use this crate, provide a definition of an object. The object structure should follow these guidelines:
From<OwnedPath>
mut
in
case of setters).NB an object must only look in durable storage prefixed by its
OwnedPath
.
To use this crate, create the wanted value struct and storage object like so:
use mavryk_smart_rollup_host::runtime::Runtime;
use mavryk_smart_rollup_host::path::{concat, RefPath, OwnedPath};
use mavryk_smart_rollup_storage::storage::Storage;
use mavryk_smart_rollup_mock::MockHost;
struct MyValue {
path: OwnedPath,
}
const VALUE_PATH: RefPath = RefPath::assert_from(b"/value");
impl MyValue {
pub fn setter(&mut self, host: &mut impl Runtime, v: &str) {
let value_path = concat(&self.path, &VALUE_PATH)
.expect("Could not get path for value");
host.store_write(&value_path, v.as_bytes(), 0)
.expect("Could not set value");
}
pub fn getter(
&mut self,
host: &impl Runtime,
) -> Vec<u8> {
let value_path = concat(&self.path, &VALUE_PATH)
.expect("Could not get path for value");
host.store_read(&value_path, 0, 1024)
.expect("Could not read value")
}
}
impl From<OwnedPath> for MyValue {
fn from(path: OwnedPath) -> Self {
Self { path }
}
}
const VALUES_PATH: RefPath = RefPath::assert_from(b"/values");
let mut host = MockHost::default();
let mut storage = Storage::<MyValue>::init(&VALUES_PATH)
.expect("Could not create storage interface");
storage.begin_transaction(&mut host)
.expect("Could not begin transaction");
let value_id = RefPath::assert_from(b"/my.value.id");
let mut value = storage.create_new(&mut host, &value_id)
.expect("Could not create new value")
.expect("Value already exists");
value.setter(&mut host, "some value");
storage.commit_transaction(&mut host)
.expect("Could not commit transaction");