common_primitives/
stateful_storage.rs

1use crate::msa::{MessageSourceId, SchemaId};
2#[cfg(feature = "std")]
3use crate::utils;
4use parity_scale_codec::{Decode, Encode};
5use scale_info::TypeInfo;
6#[cfg(feature = "std")]
7use serde::{Deserialize, Serialize};
8extern crate alloc;
9use alloc::vec::Vec;
10#[cfg(feature = "std")]
11use utils::*;
12
13/// PageId is the unique identifier for a Page in Stateful Storage
14pub type PageId = u16;
15/// PageHash is the type/size of hash of the page content.
16pub type PageHash = u32;
17/// PageNonce is the type/size of a nonce value embedded into a Page
18pub type PageNonce = u16;
19
20/// A type to expose paginated type of stateful storage
21#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
22#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
23pub struct PaginatedStorageResponse {
24	///  id of the page
25	pub page_id: PageId,
26	///  Message source account id (the original source).
27	pub msa_id: MessageSourceId,
28	///  Schema id of requested storage
29	pub schema_id: SchemaId,
30	/// Hash of the page content
31	pub content_hash: PageHash,
32	/// Nonce of the page
33	pub nonce: PageNonce,
34	/// Serialized data in a the schemas.
35	#[cfg_attr(feature = "std", serde(with = "as_hex", default))]
36	pub payload: Vec<u8>,
37}
38
39/// A type to expose itemized page of stateful storage
40#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
41#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
42pub struct ItemizedStoragePageResponse {
43	///  Message source account id (the original source).
44	pub msa_id: MessageSourceId,
45	///  Schema id of requested storage
46	pub schema_id: SchemaId,
47	/// Hash of the page content
48	pub content_hash: PageHash,
49	/// Nonce of the page
50	pub nonce: PageNonce,
51	/// Items in a page
52	pub items: Vec<ItemizedStorageResponse>,
53}
54
55/// A type to expose itemized type of stateful storage
56#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
57#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
58pub struct ItemizedStorageResponse {
59	///  index of item
60	pub index: u16,
61	/// Serialized data of item.
62	#[cfg_attr(feature = "std", serde(with = "as_hex", default))]
63	pub payload: Vec<u8>,
64}
65
66impl PaginatedStorageResponse {
67	/// Returns a new instance with associated parameters
68	pub fn new(
69		index_number: u16,
70		msa_id: MessageSourceId,
71		schema_id: SchemaId,
72		content_hash: PageHash,
73		nonce: PageNonce,
74		payload: Vec<u8>,
75	) -> Self {
76		PaginatedStorageResponse {
77			page_id: index_number,
78			msa_id,
79			schema_id,
80			payload,
81			content_hash,
82			nonce,
83		}
84	}
85}
86
87impl ItemizedStorageResponse {
88	/// Returns a new instance with associated parameters
89	pub fn new(index: u16, payload: Vec<u8>) -> Self {
90		ItemizedStorageResponse { index, payload }
91	}
92}
93
94impl ItemizedStoragePageResponse {
95	/// Returns a new instance with associated parameters
96	pub fn new(
97		msa_id: MessageSourceId,
98		schema_id: SchemaId,
99		content_hash: PageHash,
100		nonce: PageNonce,
101		items: Vec<ItemizedStorageResponse>,
102	) -> Self {
103		ItemizedStoragePageResponse { msa_id, schema_id, content_hash, items, nonce }
104	}
105}