sp_runtime/generic/
digest.rs

1// This file is part of Substrate.
2
3// Copyright (C) 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//! Generic implementation of a digest.
19
20#[cfg(all(not(feature = "std"), feature = "serde"))]
21use alloc::format;
22use alloc::vec::Vec;
23#[cfg(feature = "serde")]
24use serde::{Deserialize, Serialize};
25
26use crate::{
27	codec::{Decode, Encode, Error, Input},
28	scale_info::{
29		build::{Fields, Variants},
30		Path, Type, TypeInfo,
31	},
32	ConsensusEngineId,
33};
34use sp_core::RuntimeDebug;
35
36/// Generic header digest.
37#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)]
38#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
39pub struct Digest {
40	/// A list of logs in the digest.
41	pub logs: Vec<DigestItem>,
42}
43
44impl Digest {
45	/// Get reference to all digest items.
46	pub fn logs(&self) -> &[DigestItem] {
47		&self.logs
48	}
49
50	/// Push new digest item.
51	pub fn push(&mut self, item: DigestItem) {
52		self.logs.push(item);
53	}
54
55	/// Pop a digest item.
56	pub fn pop(&mut self) -> Option<DigestItem> {
57		self.logs.pop()
58	}
59
60	/// Get reference to the first digest item that matches the passed predicate.
61	pub fn log<T: ?Sized, F: Fn(&DigestItem) -> Option<&T>>(&self, predicate: F) -> Option<&T> {
62		self.logs().iter().find_map(predicate)
63	}
64
65	/// Get a conversion of the first digest item that successfully converts using the function.
66	pub fn convert_first<T, F: Fn(&DigestItem) -> Option<T>>(&self, predicate: F) -> Option<T> {
67		self.logs().iter().find_map(predicate)
68	}
69}
70
71/// Digest item that is able to encode/decode 'system' digest items and
72/// provide opaque access to other items.
73#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
74pub enum DigestItem {
75	/// A pre-runtime digest.
76	///
77	/// These are messages from the consensus engine to the runtime, although
78	/// the consensus engine can (and should) read them itself to avoid
79	/// code and state duplication. It is erroneous for a runtime to produce
80	/// these, but this is not (yet) checked.
81	///
82	/// NOTE: the runtime is not allowed to panic or fail in an `on_initialize`
83	/// call if an expected `PreRuntime` digest is not present. It is the
84	/// responsibility of a external block verifier to check this. Runtime API calls
85	/// will initialize the block without pre-runtime digests, so initialization
86	/// cannot fail when they are missing.
87	PreRuntime(ConsensusEngineId, Vec<u8>),
88
89	/// A message from the runtime to the consensus engine. This should *never*
90	/// be generated by the native code of any consensus engine, but this is not
91	/// checked (yet).
92	Consensus(ConsensusEngineId, Vec<u8>),
93
94	/// Put a Seal on it. This is only used by native code, and is never seen
95	/// by runtimes.
96	Seal(ConsensusEngineId, Vec<u8>),
97
98	/// Some other thing. Unsupported and experimental.
99	Other(Vec<u8>),
100
101	/// An indication for the light clients that the runtime execution
102	/// environment is updated.
103	///
104	/// Currently this is triggered when:
105	/// 1. Runtime code blob is changed or
106	/// 2. `heap_pages` value is changed.
107	RuntimeEnvironmentUpdated,
108}
109
110#[cfg(feature = "serde")]
111impl serde::Serialize for DigestItem {
112	fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error>
113	where
114		S: serde::Serializer,
115	{
116		self.using_encoded(|bytes| sp_core::bytes::serialize(bytes, seq))
117	}
118}
119
120#[cfg(feature = "serde")]
121impl<'a> serde::Deserialize<'a> for DigestItem {
122	fn deserialize<D>(de: D) -> Result<Self, D::Error>
123	where
124		D: serde::Deserializer<'a>,
125	{
126		let r = sp_core::bytes::deserialize(de)?;
127		Decode::decode(&mut &r[..])
128			.map_err(|e| serde::de::Error::custom(format!("Decode error: {}", e)))
129	}
130}
131
132impl TypeInfo for DigestItem {
133	type Identity = Self;
134
135	fn type_info() -> Type {
136		Type::builder().path(Path::new("DigestItem", module_path!())).variant(
137			Variants::new()
138				.variant("PreRuntime", |v| {
139					v.index(DigestItemType::PreRuntime as u8).fields(
140						Fields::unnamed()
141							.field(|f| f.ty::<ConsensusEngineId>().type_name("ConsensusEngineId"))
142							.field(|f| f.ty::<Vec<u8>>().type_name("Vec<u8>")),
143					)
144				})
145				.variant("Consensus", |v| {
146					v.index(DigestItemType::Consensus as u8).fields(
147						Fields::unnamed()
148							.field(|f| f.ty::<ConsensusEngineId>().type_name("ConsensusEngineId"))
149							.field(|f| f.ty::<Vec<u8>>().type_name("Vec<u8>")),
150					)
151				})
152				.variant("Seal", |v| {
153					v.index(DigestItemType::Seal as u8).fields(
154						Fields::unnamed()
155							.field(|f| f.ty::<ConsensusEngineId>().type_name("ConsensusEngineId"))
156							.field(|f| f.ty::<Vec<u8>>().type_name("Vec<u8>")),
157					)
158				})
159				.variant("Other", |v| {
160					v.index(DigestItemType::Other as u8)
161						.fields(Fields::unnamed().field(|f| f.ty::<Vec<u8>>().type_name("Vec<u8>")))
162				})
163				.variant("RuntimeEnvironmentUpdated", |v| {
164					v.index(DigestItemType::RuntimeEnvironmentUpdated as u8).fields(Fields::unit())
165				}),
166		)
167	}
168}
169
170/// A 'referencing view' for digest item. Does not own its contents. Used by
171/// final runtime implementations for encoding/decoding its log items.
172#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
173pub enum DigestItemRef<'a> {
174	/// A pre-runtime digest.
175	///
176	/// These are messages from the consensus engine to the runtime, although
177	/// the consensus engine can (and should) read them itself to avoid
178	/// code and state duplication.  It is erroneous for a runtime to produce
179	/// these, but this is not (yet) checked.
180	PreRuntime(&'a ConsensusEngineId, &'a [u8]),
181	/// A message from the runtime to the consensus engine. This should *never*
182	/// be generated by the native code of any consensus engine, but this is not
183	/// checked (yet).
184	Consensus(&'a ConsensusEngineId, &'a [u8]),
185	/// Put a Seal on it. This is only used by native code, and is never seen
186	/// by runtimes.
187	Seal(&'a ConsensusEngineId, &'a [u8]),
188	/// Any 'non-system' digest item, opaque to the native code.
189	Other(&'a [u8]),
190	/// Runtime code or heap pages updated.
191	RuntimeEnvironmentUpdated,
192}
193
194/// Type of the digest item. Used to gain explicit control over `DigestItem` encoding
195/// process. We need an explicit control, because final runtimes are encoding their own
196/// digest items using `DigestItemRef` type and we can't auto-derive `Decode`
197/// trait for `DigestItemRef`.
198#[repr(u32)]
199#[derive(Encode, Decode)]
200pub enum DigestItemType {
201	Other = 0,
202	Consensus = 4,
203	Seal = 5,
204	PreRuntime = 6,
205	RuntimeEnvironmentUpdated = 8,
206}
207
208/// Type of a digest item that contains raw data; this also names the consensus engine ID where
209/// applicable. Used to identify one or more digest items of interest.
210#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
211pub enum OpaqueDigestItemId<'a> {
212	/// Type corresponding to DigestItem::PreRuntime.
213	PreRuntime(&'a ConsensusEngineId),
214	/// Type corresponding to DigestItem::Consensus.
215	Consensus(&'a ConsensusEngineId),
216	/// Type corresponding to DigestItem::Seal.
217	Seal(&'a ConsensusEngineId),
218	/// Some other (non-prescribed) type.
219	Other,
220}
221
222impl DigestItem {
223	/// Returns a 'referencing view' for this digest item.
224	pub fn dref(&self) -> DigestItemRef {
225		match *self {
226			Self::PreRuntime(ref v, ref s) => DigestItemRef::PreRuntime(v, s),
227			Self::Consensus(ref v, ref s) => DigestItemRef::Consensus(v, s),
228			Self::Seal(ref v, ref s) => DigestItemRef::Seal(v, s),
229			Self::Other(ref v) => DigestItemRef::Other(v),
230			Self::RuntimeEnvironmentUpdated => DigestItemRef::RuntimeEnvironmentUpdated,
231		}
232	}
233
234	/// Returns `Some` if this entry is the `PreRuntime` entry.
235	pub fn as_pre_runtime(&self) -> Option<(ConsensusEngineId, &[u8])> {
236		self.dref().as_pre_runtime()
237	}
238
239	/// Returns `Some` if this entry is the `Consensus` entry.
240	pub fn as_consensus(&self) -> Option<(ConsensusEngineId, &[u8])> {
241		self.dref().as_consensus()
242	}
243
244	/// Returns `Some` if this entry is the `Seal` entry.
245	pub fn as_seal(&self) -> Option<(ConsensusEngineId, &[u8])> {
246		self.dref().as_seal()
247	}
248
249	/// Returns Some if `self` is a `DigestItem::Other`.
250	pub fn as_other(&self) -> Option<&[u8]> {
251		self.dref().as_other()
252	}
253
254	/// Returns the opaque data contained in the item if `Some` if this entry has the id given.
255	pub fn try_as_raw(&self, id: OpaqueDigestItemId) -> Option<&[u8]> {
256		self.dref().try_as_raw(id)
257	}
258
259	/// Returns the data contained in the item if `Some` if this entry has the id given, decoded
260	/// to the type provided `T`.
261	pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
262		self.dref().try_to::<T>(id)
263	}
264
265	/// Try to match this to a `Self::Seal`, check `id` matches and decode it.
266	///
267	/// Returns `None` if this isn't a seal item, the `id` doesn't match or when the decoding fails.
268	pub fn seal_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
269		self.dref().seal_try_to(id)
270	}
271
272	/// Try to match this to a `Self::Consensus`, check `id` matches and decode it.
273	///
274	/// Returns `None` if this isn't a consensus item, the `id` doesn't match or
275	/// when the decoding fails.
276	pub fn consensus_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
277		self.dref().consensus_try_to(id)
278	}
279
280	/// Try to match this to a `Self::PreRuntime`, check `id` matches and decode it.
281	///
282	/// Returns `None` if this isn't a pre-runtime item, the `id` doesn't match or
283	/// when the decoding fails.
284	pub fn pre_runtime_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
285		self.dref().pre_runtime_try_to(id)
286	}
287}
288
289impl Encode for DigestItem {
290	fn encode(&self) -> Vec<u8> {
291		self.dref().encode()
292	}
293}
294
295impl codec::EncodeLike for DigestItem {}
296
297impl Decode for DigestItem {
298	#[allow(deprecated)]
299	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
300		let item_type: DigestItemType = Decode::decode(input)?;
301		match item_type {
302			DigestItemType::PreRuntime => {
303				let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
304				Ok(Self::PreRuntime(vals.0, vals.1))
305			},
306			DigestItemType::Consensus => {
307				let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
308				Ok(Self::Consensus(vals.0, vals.1))
309			},
310			DigestItemType::Seal => {
311				let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
312				Ok(Self::Seal(vals.0, vals.1))
313			},
314			DigestItemType::Other => Ok(Self::Other(Decode::decode(input)?)),
315			DigestItemType::RuntimeEnvironmentUpdated => Ok(Self::RuntimeEnvironmentUpdated),
316		}
317	}
318}
319
320impl<'a> DigestItemRef<'a> {
321	/// Cast this digest item into `PreRuntime`
322	pub fn as_pre_runtime(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
323		match *self {
324			Self::PreRuntime(consensus_engine_id, data) => Some((*consensus_engine_id, data)),
325			_ => None,
326		}
327	}
328
329	/// Cast this digest item into `Consensus`
330	pub fn as_consensus(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
331		match *self {
332			Self::Consensus(consensus_engine_id, data) => Some((*consensus_engine_id, data)),
333			_ => None,
334		}
335	}
336
337	/// Cast this digest item into `Seal`
338	pub fn as_seal(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
339		match *self {
340			Self::Seal(consensus_engine_id, data) => Some((*consensus_engine_id, data)),
341			_ => None,
342		}
343	}
344
345	/// Cast this digest item into `PreRuntime`
346	pub fn as_other(&self) -> Option<&'a [u8]> {
347		match *self {
348			Self::Other(data) => Some(data),
349			_ => None,
350		}
351	}
352
353	/// Try to match this digest item to the given opaque item identifier; if it matches, then
354	/// return the opaque data it contains.
355	pub fn try_as_raw(&self, id: OpaqueDigestItemId) -> Option<&'a [u8]> {
356		match (id, self) {
357			(OpaqueDigestItemId::Consensus(w), &Self::Consensus(v, s)) |
358			(OpaqueDigestItemId::Seal(w), &Self::Seal(v, s)) |
359			(OpaqueDigestItemId::PreRuntime(w), &Self::PreRuntime(v, s))
360				if v == w =>
361				Some(s),
362			(OpaqueDigestItemId::Other, &Self::Other(s)) => Some(s),
363			_ => None,
364		}
365	}
366
367	/// Try to match this digest item to the given opaque item identifier; if it matches, then
368	/// try to cast to the given data type; if that works, return it.
369	pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
370		self.try_as_raw(id).and_then(|mut x| Decode::decode(&mut x).ok())
371	}
372
373	/// Try to match this to a `Self::Seal`, check `id` matches and decode it.
374	///
375	/// Returns `None` if this isn't a seal item, the `id` doesn't match or when the decoding fails.
376	pub fn seal_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
377		match self {
378			Self::Seal(v, s) if *v == id => Decode::decode(&mut &s[..]).ok(),
379			_ => None,
380		}
381	}
382
383	/// Try to match this to a `Self::Consensus`, check `id` matches and decode it.
384	///
385	/// Returns `None` if this isn't a consensus item, the `id` doesn't match or
386	/// when the decoding fails.
387	pub fn consensus_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
388		match self {
389			Self::Consensus(v, s) if *v == id => Decode::decode(&mut &s[..]).ok(),
390			_ => None,
391		}
392	}
393
394	/// Try to match this to a `Self::PreRuntime`, check `id` matches and decode it.
395	///
396	/// Returns `None` if this isn't a pre-runtime item, the `id` doesn't match or
397	/// when the decoding fails.
398	pub fn pre_runtime_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
399		match self {
400			Self::PreRuntime(v, s) if *v == id => Decode::decode(&mut &s[..]).ok(),
401			_ => None,
402		}
403	}
404}
405
406impl<'a> Encode for DigestItemRef<'a> {
407	fn encode(&self) -> Vec<u8> {
408		let mut v = Vec::new();
409
410		match *self {
411			Self::Consensus(val, data) => {
412				DigestItemType::Consensus.encode_to(&mut v);
413				(val, data).encode_to(&mut v);
414			},
415			Self::Seal(val, sig) => {
416				DigestItemType::Seal.encode_to(&mut v);
417				(val, sig).encode_to(&mut v);
418			},
419			Self::PreRuntime(val, data) => {
420				DigestItemType::PreRuntime.encode_to(&mut v);
421				(val, data).encode_to(&mut v);
422			},
423			Self::Other(val) => {
424				DigestItemType::Other.encode_to(&mut v);
425				val.encode_to(&mut v);
426			},
427			Self::RuntimeEnvironmentUpdated => {
428				DigestItemType::RuntimeEnvironmentUpdated.encode_to(&mut v);
429			},
430		}
431
432		v
433	}
434}
435
436impl<'a> codec::EncodeLike for DigestItemRef<'a> {}
437
438#[cfg(test)]
439mod tests {
440	use super::*;
441
442	#[test]
443	fn should_serialize_digest() {
444		let digest = Digest {
445			logs: vec![DigestItem::Other(vec![1, 2, 3]), DigestItem::Seal(*b"test", vec![1, 2, 3])],
446		};
447
448		assert_eq!(
449			serde_json::to_string(&digest).unwrap(),
450			r#"{"logs":["0x000c010203","0x05746573740c010203"]}"#
451		);
452	}
453
454	#[test]
455	fn digest_item_type_info() {
456		let type_info = DigestItem::type_info();
457		let variants = if let scale_info::TypeDef::Variant(variant) = type_info.type_def {
458			variant.variants
459		} else {
460			panic!("Should be a TypeDef::TypeDefVariant")
461		};
462
463		// ensure that all variants are covered by manual TypeInfo impl
464		let check = |digest_item_type: DigestItemType| {
465			let (variant_name, digest_item) = match digest_item_type {
466				DigestItemType::Other => ("Other", DigestItem::Other(Default::default())),
467				DigestItemType::Consensus =>
468					("Consensus", DigestItem::Consensus(Default::default(), Default::default())),
469				DigestItemType::Seal =>
470					("Seal", DigestItem::Seal(Default::default(), Default::default())),
471				DigestItemType::PreRuntime =>
472					("PreRuntime", DigestItem::PreRuntime(Default::default(), Default::default())),
473				DigestItemType::RuntimeEnvironmentUpdated =>
474					("RuntimeEnvironmentUpdated", DigestItem::RuntimeEnvironmentUpdated),
475			};
476			let encoded = digest_item.encode();
477			let variant = variants
478				.iter()
479				.find(|v| v.name == variant_name)
480				.expect(&format!("Variant {} not found", variant_name));
481
482			assert_eq!(encoded[0], variant.index)
483		};
484
485		check(DigestItemType::Other);
486		check(DigestItemType::Consensus);
487		check(DigestItemType::Seal);
488		check(DigestItemType::PreRuntime);
489		check(DigestItemType::RuntimeEnvironmentUpdated);
490	}
491}