chainql_frame_metadata/
v14.rs

1// This file is part of Substrate.
2
3// Copyright (C) 2018-2020 Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18#[cfg(feature = "decode")]
19use codec::Decode;
20#[cfg(feature = "serde_full")]
21use serde::{de::DeserializeOwned, Deserialize, Serialize};
22
23use super::RuntimeMetadataPrefixed;
24use codec::Encode;
25use scale_info::prelude::vec::Vec;
26use scale_info::{
27	form::{Form, MetaForm, PortableForm},
28	IntoPortable, MetaType, PortableRegistry, Registry,
29};
30
31/// Current prefix of metadata
32pub const META_RESERVED: u32 = 0x6174656d; // 'meta' warn endianness
33
34/// Latest runtime metadata
35pub type RuntimeMetadataLastVersion = RuntimeMetadataV14;
36
37impl From<RuntimeMetadataLastVersion> for super::RuntimeMetadataPrefixed {
38	fn from(metadata: RuntimeMetadataLastVersion) -> RuntimeMetadataPrefixed {
39		RuntimeMetadataPrefixed(META_RESERVED, super::RuntimeMetadata::V14(metadata))
40	}
41}
42
43/// The metadata of a runtime.
44#[derive(Clone, PartialEq, Eq, Encode, Debug)]
45#[cfg_attr(feature = "decode", derive(Decode))]
46#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
47pub struct RuntimeMetadataV14 {
48	/// Type registry containing all types used in the metadata.
49	pub types: PortableRegistry,
50	/// Metadata of all the pallets.
51	pub pallets: Vec<PalletMetadata<PortableForm>>,
52	/// Metadata of the extrinsic.
53	pub extrinsic: ExtrinsicMetadata<PortableForm>,
54	/// The type of the `Runtime`.
55	pub ty: <PortableForm as Form>::Type,
56}
57
58impl RuntimeMetadataV14 {
59	/// Create a new instance of [`RuntimeMetadataV14`].
60	pub fn new(
61		pallets: Vec<PalletMetadata>,
62		extrinsic: ExtrinsicMetadata,
63		runtime_type: MetaType,
64	) -> Self {
65		let mut registry = Registry::new();
66		let pallets = registry.map_into_portable(pallets);
67		let extrinsic = extrinsic.into_portable(&mut registry);
68		let ty = registry.register_type(&runtime_type);
69		Self {
70			types: registry.into(),
71			pallets,
72			extrinsic,
73			ty,
74		}
75	}
76}
77
78/// Metadata of the extrinsic used by the runtime.
79#[derive(Clone, PartialEq, Eq, Encode, Debug)]
80#[cfg_attr(feature = "decode", derive(Decode))]
81#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
82#[cfg_attr(
83	feature = "serde_full",
84	serde(bound(
85		serialize = "T::Type: Serialize, T::String: Serialize",
86		deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned",
87	))
88)]
89pub struct ExtrinsicMetadata<T: Form = MetaForm> {
90	/// The type of the extrinsic.
91	pub ty: T::Type,
92	/// Extrinsic version.
93	pub version: u8,
94	/// The signed extensions in the order they appear in the extrinsic.
95	pub signed_extensions: Vec<SignedExtensionMetadata<T>>,
96}
97
98impl IntoPortable for ExtrinsicMetadata {
99	type Output = ExtrinsicMetadata<PortableForm>;
100
101	fn into_portable(self, registry: &mut Registry) -> Self::Output {
102		ExtrinsicMetadata {
103			ty: registry.register_type(&self.ty),
104			version: self.version,
105			signed_extensions: registry.map_into_portable(self.signed_extensions),
106		}
107	}
108}
109
110/// Metadata of an extrinsic's signed extension.
111#[derive(Clone, PartialEq, Eq, Encode, Debug)]
112#[cfg_attr(feature = "decode", derive(Decode))]
113#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
114#[cfg_attr(
115	feature = "serde_full",
116	serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
117)]
118pub struct SignedExtensionMetadata<T: Form = MetaForm> {
119	/// The unique signed extension identifier, which may be different from the type name.
120	pub identifier: T::String,
121	/// The type of the signed extension, with the data to be included in the extrinsic.
122	pub ty: T::Type,
123	/// The type of the additional signed data, with the data to be included in the signed payload
124	pub additional_signed: T::Type,
125}
126
127impl IntoPortable for SignedExtensionMetadata {
128	type Output = SignedExtensionMetadata<PortableForm>;
129
130	fn into_portable(self, registry: &mut Registry) -> Self::Output {
131		SignedExtensionMetadata {
132			identifier: self.identifier.into_portable(registry),
133			ty: registry.register_type(&self.ty),
134			additional_signed: registry.register_type(&self.additional_signed),
135		}
136	}
137}
138
139/// All metadata about an runtime pallet.
140#[derive(Clone, PartialEq, Eq, Encode, Debug)]
141#[cfg_attr(feature = "decode", derive(Decode))]
142#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
143#[cfg_attr(
144	feature = "serde_full",
145	serde(bound(
146		serialize = "T::Type: Serialize, T::String: Serialize",
147		deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned",
148	))
149)]
150pub struct PalletMetadata<T: Form = MetaForm> {
151	/// Pallet name.
152	pub name: T::String,
153	/// Pallet storage metadata.
154	pub storage: Option<PalletStorageMetadata<T>>,
155	/// Pallet calls metadata.
156	pub calls: Option<PalletCallMetadata<T>>,
157	/// Pallet event metadata.
158	pub event: Option<PalletEventMetadata<T>>,
159	/// Pallet constants metadata.
160	pub constants: Vec<PalletConstantMetadata<T>>,
161	/// Pallet error metadata.
162	pub error: Option<PalletErrorMetadata<T>>,
163	/// Define the index of the pallet, this index will be used for the encoding of pallet event,
164	/// call and origin variants.
165	pub index: u8,
166}
167
168impl IntoPortable for PalletMetadata {
169	type Output = PalletMetadata<PortableForm>;
170
171	fn into_portable(self, registry: &mut Registry) -> Self::Output {
172		PalletMetadata {
173			name: self.name.into_portable(registry),
174			storage: self.storage.map(|storage| storage.into_portable(registry)),
175			calls: self.calls.map(|calls| calls.into_portable(registry)),
176			event: self.event.map(|event| event.into_portable(registry)),
177			constants: registry.map_into_portable(self.constants),
178			error: self.error.map(|error| error.into_portable(registry)),
179			index: self.index,
180		}
181	}
182}
183
184/// All metadata of the pallet's storage.
185#[derive(Clone, PartialEq, Eq, Encode, Debug)]
186#[cfg_attr(feature = "decode", derive(Decode))]
187#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
188#[cfg_attr(
189	feature = "serde_full",
190	serde(bound(
191		serialize = "T::Type: Serialize, T::String: Serialize",
192		deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned",
193	))
194)]
195pub struct PalletStorageMetadata<T: Form = MetaForm> {
196	/// The common prefix used by all storage entries.
197	pub prefix: T::String,
198	/// Metadata for all storage entries.
199	pub entries: Vec<StorageEntryMetadata<T>>,
200}
201
202impl IntoPortable for PalletStorageMetadata {
203	type Output = PalletStorageMetadata<PortableForm>;
204
205	fn into_portable(self, registry: &mut Registry) -> Self::Output {
206		PalletStorageMetadata {
207			prefix: self.prefix.into_portable(registry),
208			entries: registry.map_into_portable(self.entries),
209		}
210	}
211}
212
213/// Metadata about one storage entry.
214#[derive(Clone, PartialEq, Eq, Encode, Debug)]
215#[cfg_attr(feature = "decode", derive(Decode))]
216#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
217#[cfg_attr(
218	feature = "serde_full",
219	serde(bound(
220		serialize = "T::Type: Serialize, T::String: Serialize",
221		deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned",
222	))
223)]
224pub struct StorageEntryMetadata<T: Form = MetaForm> {
225	/// Variable name of the storage entry.
226	pub name: T::String,
227	/// An `Option` modifier of that storage entry.
228	pub modifier: StorageEntryModifier,
229	/// Type of the value stored in the entry.
230	pub ty: StorageEntryType<T>,
231	/// Default value (SCALE encoded).
232	pub default: Vec<u8>,
233	/// Storage entry documentation.
234	pub docs: Vec<T::String>,
235}
236
237impl IntoPortable for StorageEntryMetadata {
238	type Output = StorageEntryMetadata<PortableForm>;
239
240	fn into_portable(self, registry: &mut Registry) -> Self::Output {
241		StorageEntryMetadata {
242			name: self.name.into_portable(registry),
243			modifier: self.modifier,
244			ty: self.ty.into_portable(registry),
245			default: self.default,
246			docs: registry.map_into_portable(self.docs),
247		}
248	}
249}
250
251/// A storage entry modifier indicates how a storage entry is returned when fetched and what the value will be if the key is not present.
252/// Specifically this refers to the "return type" when fetching a storage entry, and what the value will be if the key is not present.
253///
254/// `Optional` means you should expect an `Option<T>`, with `None` returned if the key is not present.
255/// `Default` means you should expect a `T` with the default value of default if the key is not present.
256#[derive(Clone, PartialEq, Eq, Encode, Debug)]
257#[cfg_attr(feature = "decode", derive(Decode))]
258#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
259pub enum StorageEntryModifier {
260	/// The storage entry returns an `Option<T>`, with `None` if the key is not present.
261	Optional,
262	/// The storage entry returns `T::Default` if the key is not present.
263	Default,
264}
265
266/// Hasher used by storage maps
267#[derive(Clone, PartialEq, Eq, Encode, Debug)]
268#[cfg_attr(feature = "decode", derive(Decode))]
269#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
270pub enum StorageHasher {
271	/// 128-bit Blake2 hash.
272	Blake2_128,
273	/// 256-bit Blake2 hash.
274	Blake2_256,
275	/// Multiple 128-bit Blake2 hashes concatenated.
276	Blake2_128Concat,
277	/// 128-bit XX hash.
278	Twox128,
279	/// 256-bit XX hash.
280	Twox256,
281	/// Multiple 64-bit XX hashes concatenated.
282	Twox64Concat,
283	/// Identity hashing (no hashing).
284	Identity,
285}
286
287/// A type of storage value.
288#[derive(Clone, PartialEq, Eq, Encode, Debug)]
289#[cfg_attr(feature = "decode", derive(Decode))]
290#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
291#[cfg_attr(
292	feature = "serde_full",
293	serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
294)]
295pub enum StorageEntryType<T: Form = MetaForm> {
296	/// Plain storage entry (just the value).
297	Plain(T::Type),
298	/// A storage map.
299	Map {
300		/// One or more hashers, should be one hasher per key element.
301		hashers: Vec<StorageHasher>,
302		/// The type of the key, can be a tuple with elements for each of the hashers.
303		key: T::Type,
304		/// The type of the value.
305		value: T::Type,
306	},
307}
308
309impl IntoPortable for StorageEntryType {
310	type Output = StorageEntryType<PortableForm>;
311
312	fn into_portable(self, registry: &mut Registry) -> Self::Output {
313		match self {
314			Self::Plain(plain) => StorageEntryType::Plain(registry.register_type(&plain)),
315			Self::Map {
316				hashers,
317				key,
318				value,
319			} => StorageEntryType::Map {
320				hashers,
321				key: registry.register_type(&key),
322				value: registry.register_type(&value),
323			},
324		}
325	}
326}
327
328/// Metadata for all calls in a pallet
329#[derive(Clone, PartialEq, Eq, Encode, Debug)]
330#[cfg_attr(feature = "decode", derive(Decode))]
331#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
332#[cfg_attr(
333	feature = "serde_full",
334	serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
335)]
336pub struct PalletCallMetadata<T: Form = MetaForm> {
337	/// The corresponding enum type for the pallet call.
338	pub ty: T::Type,
339}
340
341impl IntoPortable for PalletCallMetadata {
342	type Output = PalletCallMetadata<PortableForm>;
343
344	fn into_portable(self, registry: &mut Registry) -> Self::Output {
345		PalletCallMetadata {
346			ty: registry.register_type(&self.ty),
347		}
348	}
349}
350
351impl From<MetaType> for PalletCallMetadata {
352	fn from(ty: MetaType) -> Self {
353		Self { ty }
354	}
355}
356
357/// Metadata about the pallet Event type.
358#[derive(Clone, PartialEq, Eq, Encode, Debug)]
359#[cfg_attr(feature = "decode", derive(Decode))]
360#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
361pub struct PalletEventMetadata<T: Form = MetaForm> {
362	/// The Event type.
363	pub ty: T::Type,
364}
365
366impl IntoPortable for PalletEventMetadata {
367	type Output = PalletEventMetadata<PortableForm>;
368
369	fn into_portable(self, registry: &mut Registry) -> Self::Output {
370		PalletEventMetadata {
371			ty: registry.register_type(&self.ty),
372		}
373	}
374}
375
376impl From<MetaType> for PalletEventMetadata {
377	fn from(ty: MetaType) -> Self {
378		Self { ty }
379	}
380}
381
382/// Metadata about one pallet constant.
383#[derive(Clone, PartialEq, Eq, Encode, Debug)]
384#[cfg_attr(feature = "decode", derive(Decode))]
385#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
386#[cfg_attr(
387	feature = "serde_full",
388	serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
389)]
390pub struct PalletConstantMetadata<T: Form = MetaForm> {
391	/// Name of the pallet constant.
392	pub name: T::String,
393	/// Type of the pallet constant.
394	pub ty: T::Type,
395	/// Value stored in the constant (SCALE encoded).
396	pub value: Vec<u8>,
397	/// Documentation of the constant.
398	pub docs: Vec<T::String>,
399}
400
401impl IntoPortable for PalletConstantMetadata {
402	type Output = PalletConstantMetadata<PortableForm>;
403
404	fn into_portable(self, registry: &mut Registry) -> Self::Output {
405		PalletConstantMetadata {
406			name: self.name.into_portable(registry),
407			ty: registry.register_type(&self.ty),
408			value: self.value,
409			docs: registry.map_into_portable(self.docs),
410		}
411	}
412}
413
414/// Metadata about a pallet error.
415#[derive(Clone, PartialEq, Eq, Encode, Debug)]
416#[cfg_attr(feature = "decode", derive(Decode))]
417#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
418#[cfg_attr(feature = "serde_full", serde(bound(serialize = "T::Type: Serialize")))]
419pub struct PalletErrorMetadata<T: Form = MetaForm> {
420	/// The error type information.
421	pub ty: T::Type,
422}
423
424impl IntoPortable for PalletErrorMetadata {
425	type Output = PalletErrorMetadata<PortableForm>;
426
427	fn into_portable(self, registry: &mut Registry) -> Self::Output {
428		PalletErrorMetadata {
429			ty: registry.register_type(&self.ty),
430		}
431	}
432}
433
434impl From<MetaType> for PalletErrorMetadata {
435	fn from(ty: MetaType) -> Self {
436		Self { ty }
437	}
438}