common_primitives/
stateful_storage.rs

1use crate::msa::MessageSourceId;
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 crate::schema::{IntentId, SchemaId};
10use alloc::vec::Vec;
11#[cfg(feature = "std")]
12use utils::*;
13
14/// PageId is the unique identifier for a Page in Stateful Storage
15pub type PageId = u16;
16/// PageHash is the type/size of the hashed page content.
17pub type PageHash = u32;
18/// PageNonce is the type/size of a nonce value embedded into a Page
19pub type PageNonce = u16;
20
21/// A type to expose paginated type of stateful storage
22// TODO: Remove once v1 RPC API is gone (ie, all nodes have upgraded and v1 Runtime API is removed)
23#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
24#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
25pub struct PaginatedStorageResponse {
26	///  id of the page
27	pub page_id: PageId,
28	///  Message source account id (the original source).
29	pub msa_id: MessageSourceId,
30	///  Schema id of requested storage
31	pub schema_id: SchemaId,
32	/// Hash of the page content
33	pub content_hash: PageHash,
34	/// Nonce of the page
35	pub nonce: PageNonce,
36	/// Serialized data in the schema.
37	#[cfg_attr(feature = "std", serde(with = "as_hex", default))]
38	pub payload: Vec<u8>,
39}
40
41/// A type to expose paginated type of stateful storage
42#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
43#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
44pub struct PaginatedStorageResponseV2 {
45	/// IntentId of the page
46	pub intent_id: IntentId,
47	///  id of the page
48	pub page_id: PageId,
49	///  Message source account id (the original source).
50	pub msa_id: MessageSourceId,
51	///  Schema id of requested storage
52	pub schema_id: SchemaId,
53	/// Hash of the page content
54	pub content_hash: PageHash,
55	/// Nonce of the page
56	pub nonce: PageNonce,
57	/// Serialized data in the schema.
58	#[cfg_attr(feature = "std", serde(with = "as_hex", default))]
59	pub payload: Vec<u8>,
60}
61
62/// A type to expose itemized page of stateful storage
63// TODO: Remove once v1 RPC API is gone (ie, all nodes have upgraded and v1 Runtime API is removed)
64#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
65#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
66pub struct ItemizedStoragePageResponse {
67	///  Message source account id (the original source).
68	pub msa_id: MessageSourceId,
69	///  Schema id of requested storage
70	pub schema_id: SchemaId,
71	/// Hash of the page content
72	pub content_hash: PageHash,
73	/// Nonce of the page
74	pub nonce: PageNonce,
75	/// Items in a page
76	pub items: Vec<ItemizedStorageResponse>,
77}
78
79/// A type to expose itemized page of stateful storage
80#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
81#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
82pub struct ItemizedStoragePageResponseV2 {
83	///  Message source account id (the original source).
84	pub msa_id: MessageSourceId,
85	///  Intent id of requested storage
86	pub intent_id: IntentId,
87	/// Hash of the page content
88	pub content_hash: PageHash,
89	/// Nonce of the page
90	pub nonce: PageNonce,
91	/// Items in a page
92	pub items: Vec<ItemizedStorageResponseV2>,
93}
94
95/// A type to expose itemized type of stateful storage
96// TODO: Remove once v1 RPC API is gone (ie, all nodes have upgraded and v1 Runtime API is removed)
97#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
98#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
99pub struct ItemizedStorageResponse {
100	///  index of item
101	pub index: u16,
102	/// Serialized data of item.
103	#[cfg_attr(feature = "std", serde(with = "as_hex", default))]
104	pub payload: Vec<u8>,
105}
106
107/// A type to expose itemized type of stateful storage
108#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
109#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
110pub struct ItemizedStorageResponseV2 {
111	///  index of item
112	pub index: u16,
113	/// SchemaId of the item.
114	pub schema_id: SchemaId,
115	/// Serialized data of item.
116	#[cfg_attr(feature = "std", serde(with = "as_hex", default))]
117	pub payload: Vec<u8>,
118}
119
120impl PaginatedStorageResponseV2 {
121	/// Returns a new instance with associated parameters
122	pub fn new(
123		index_number: u16,
124		msa_id: MessageSourceId,
125		intent_id: IntentId,
126		schema_id: SchemaId,
127		content_hash: PageHash,
128		nonce: PageNonce,
129		payload: Vec<u8>,
130	) -> Self {
131		PaginatedStorageResponseV2 {
132			page_id: index_number,
133			msa_id,
134			intent_id,
135			schema_id,
136			payload,
137			content_hash,
138			nonce,
139		}
140	}
141}
142
143impl Into<PaginatedStorageResponse> for PaginatedStorageResponseV2 {
144	fn into(self) -> PaginatedStorageResponse {
145		PaginatedStorageResponse {
146			page_id: self.page_id,
147			msa_id: self.msa_id,
148			schema_id: self.schema_id,
149			payload: self.payload,
150			content_hash: self.content_hash,
151			nonce: self.nonce,
152		}
153	}
154}
155
156impl ItemizedStorageResponseV2 {
157	/// Returns a new instance with associated parameters
158	pub fn new(index: u16, schema_id: SchemaId, payload: Vec<u8>) -> Self {
159		ItemizedStorageResponseV2 { index, schema_id, payload }
160	}
161}
162
163impl Into<ItemizedStorageResponse> for ItemizedStorageResponseV2 {
164	fn into(self) -> ItemizedStorageResponse {
165		ItemizedStorageResponse { index: self.index, payload: self.payload }
166	}
167}
168
169impl ItemizedStoragePageResponseV2 {
170	/// Returns a new instance with associated parameters
171	pub fn new(
172		msa_id: MessageSourceId,
173		intent_id: IntentId,
174		content_hash: PageHash,
175		nonce: PageNonce,
176		items: Vec<ItemizedStorageResponseV2>,
177	) -> Self {
178		ItemizedStoragePageResponseV2 { msa_id, intent_id, content_hash, items, nonce }
179	}
180}
181
182impl Into<ItemizedStoragePageResponse> for ItemizedStoragePageResponseV2 {
183	fn into(self) -> ItemizedStoragePageResponse {
184		ItemizedStoragePageResponse {
185			msa_id: self.msa_id,
186			schema_id: self.items.get(0).map(|item| item.schema_id).unwrap_or(0),
187			content_hash: self.content_hash,
188			nonce: self.nonce,
189			items: self.items.into_iter().map(|item| item.into()).collect(),
190		}
191	}
192}