vrs_core_sdk/lib.rs
1//! The core sdk for developing nucleus on Verisense.
2//! NOTE: This crate is currently under heavy development and is not stable yet. We release it just for testing and collecting feedback.
3//!
4//! # Examples
5//!
6//! ```
7//! use parity_scale_codec::{Decode, Encode};
8//! use vrs_core_sdk::{get, post, storage};
9//!
10//! #[derive(Debug, Decode, Encode)]
11//! pub struct User {
12//! pub id: u64,
13//! pub name: String,
14//! }
15//!
16//! #[post]
17//! pub fn add_user(user: User) -> Result<u64, String> {
18//! let max_id_key = [&b"user:"[..], &u64::MAX.to_be_bytes()[..]].concat();
19//! let max_id = match storage::search(&max_id_key, storage::Direction::Reverse)
20//! .map_err(|e| e.to_string())?
21//! {
22//! Some((id, _)) => u64::from_be_bytes(id[5..].try_into().unwrap()) + 1,
23//! None => 1u64,
24//! };
25//! let key = [&b"user:"[..], &max_id.to_be_bytes()[..]].concat();
26//! storage::put(&key, user.encode()).map_err(|e| e.to_string())?;
27//! Ok(max_id)
28//! }
29//!
30//! #[get]
31//! pub fn get_user(id: u64) -> Result<Option<User>, String> {
32//! let key = [&b"user:"[..], &id.to_be_bytes()[..]].concat();
33//! let r = storage::get(&key).map_err(|e| e.to_string())?;
34//! let user = r.map(|d| User::decode(&mut &d[..]).unwrap());
35//! Ok(user)
36//! }
37//! ```
38
39pub mod abi;
40pub mod error;
41pub mod http;
42pub mod io;
43pub mod storage;
44pub mod timer;
45pub mod tss;
46
47pub use codec;
48pub use io::{_eprint, _print, nucleus_id};
49pub use lazy_static;
50pub use scale_info;
51pub use sp_core::crypto::AccountId32 as AccountId;
52pub use vrs_core_macros::*;
53
54/// the buffer used for transfering data from host to wasm
55/// this should be equal to a page size
56pub const BUFFER_LEN: usize = 64 * 1024;
57
58/// if host function returns this value, it means there is no more data to read
59pub const NO_MORE_DATA: i32 = 0;
60
61/// result of host function, T should be `codec::Codec`
62pub type CallResult<T> = Result<T, error::RuntimeError>;
63
64/// the id of the nucleus, same as AccountId32
65pub type NucleusId = AccountId;
66
67#[inline]
68pub(crate) fn allocate_buffer() -> Vec<u8> {
69 vec![0u8; BUFFER_LEN]
70}