tp_runtime/generic/
digest.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2017-2021 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(feature = "std")]
21use serde::{Deserialize, Serialize};
22
23use tetcore_std::prelude::*;
24
25use crate::ConsensusEngineId;
26use crate::codec::{Decode, Encode, Input, Error};
27use tet_core::{ChangesTrieConfiguration, RuntimeDebug};
28
29/// Generic header digest.
30#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
31#[cfg_attr(feature = "std", derive(Serialize, Deserialize, tetsy_util_mem::MallocSizeOf))]
32pub struct Digest<Hash> {
33	/// A list of logs in the digest.
34	#[cfg_attr(
35		feature = "std",
36		serde(bound(serialize = "Hash: codec::Codec", deserialize = "Hash: codec::Codec"))
37	)]
38	pub logs: Vec<DigestItem<Hash>>,
39}
40
41impl<Item> Default for Digest<Item> {
42	fn default() -> Self {
43		Digest { logs: Vec::new(), }
44	}
45}
46
47impl<Hash> Digest<Hash> {
48	/// Get reference to all digest items.
49	pub fn logs(&self) -> &[DigestItem<Hash>] {
50		&self.logs
51	}
52
53	/// Push new digest item.
54	pub fn push(&mut self, item: DigestItem<Hash>) {
55		self.logs.push(item);
56	}
57
58	/// Pop a digest item.
59	pub fn pop(&mut self) -> Option<DigestItem<Hash>> {
60		self.logs.pop()
61	}
62
63	/// Get reference to the first digest item that matches the passed predicate.
64	pub fn log<T: ?Sized, F: Fn(&DigestItem<Hash>) -> Option<&T>>(&self, predicate: F) -> Option<&T> {
65		self.logs().iter()
66			.filter_map(predicate)
67			.next()
68	}
69
70	/// Get a conversion of the first digest item that successfully converts using the function.
71	pub fn convert_first<T, F: Fn(&DigestItem<Hash>) -> Option<T>>(&self, predicate: F) -> Option<T> {
72		self.logs().iter()
73			.filter_map(predicate)
74			.next()
75	}
76}
77
78
79/// Digest item that is able to encode/decode 'system' digest items and
80/// provide opaque access to other items.
81#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
82#[cfg_attr(feature = "std", derive(tetsy_util_mem::MallocSizeOf))]
83pub enum DigestItem<Hash> {
84	/// System digest item that contains the root of changes trie at given
85	/// block. It is created for every block iff runtime supports changes
86	/// trie creation.
87	ChangesTrieRoot(Hash),
88
89	/// A pre-runtime digest.
90	///
91	/// These are messages from the consensus engine to the runtime, although
92	/// the consensus engine can (and should) read them itself to avoid
93	/// code and state duplication. It is erroneous for a runtime to produce
94	/// these, but this is not (yet) checked.
95	///
96	/// NOTE: the runtime is not allowed to panic or fail in an `on_initialize`
97	/// call if an expected `PreRuntime` digest is not present. It is the
98	/// responsibility of a external block verifier to check this. Runtime API calls
99	/// will initialize the block without pre-runtime digests, so initialization
100	/// cannot fail when they are missing.
101	PreRuntime(ConsensusEngineId, Vec<u8>),
102
103	/// A message from the runtime to the consensus engine. This should *never*
104	/// be generated by the native code of any consensus engine, but this is not
105	/// checked (yet).
106	Consensus(ConsensusEngineId, Vec<u8>),
107
108	/// Put a Seal on it. This is only used by native code, and is never seen
109	/// by runtimes.
110	Seal(ConsensusEngineId, Vec<u8>),
111
112	/// Digest item that contains signal from changes tries manager to the
113	/// native code.
114	ChangesTrieSignal(ChangesTrieSignal),
115
116	/// Some other thing. Unsupported and experimental.
117	Other(Vec<u8>),
118}
119
120/// Available changes trie signals.
121#[derive(PartialEq, Eq, Clone, Encode, Decode)]
122#[cfg_attr(feature = "std", derive(Debug, tetsy_util_mem::MallocSizeOf))]
123pub enum ChangesTrieSignal {
124	/// New changes trie configuration is enacted, starting from **next block**.
125	///
126	/// The block that emits this signal will contain changes trie (CT) that covers
127	/// blocks range [BEGIN; current block], where BEGIN is (order matters):
128	/// - LAST_TOP_LEVEL_DIGEST_BLOCK+1 if top level digest CT has ever been created
129	///   using current configuration AND the last top level digest CT has been created
130	///   at block LAST_TOP_LEVEL_DIGEST_BLOCK;
131	/// - LAST_CONFIGURATION_CHANGE_BLOCK+1 if there has been CT configuration change
132	///   before and the last configuration change happened at block
133	///   LAST_CONFIGURATION_CHANGE_BLOCK;
134	/// - 1 otherwise.
135	NewConfiguration(Option<ChangesTrieConfiguration>),
136}
137
138#[cfg(feature = "std")]
139impl<Hash: Encode> serde::Serialize for DigestItem<Hash> {
140	fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
141		self.using_encoded(|bytes| {
142			tet_core::bytes::serialize(bytes, seq)
143		})
144	}
145}
146
147#[cfg(feature = "std")]
148impl<'a, Hash: Decode> serde::Deserialize<'a> for DigestItem<Hash> {
149	fn deserialize<D>(de: D) -> Result<Self, D::Error> where
150		D: serde::Deserializer<'a>,
151	{
152		let r = tet_core::bytes::deserialize(de)?;
153		Decode::decode(&mut &r[..])
154			.map_err(|e| serde::de::Error::custom(format!("Decode error: {}", e)))
155	}
156}
157
158/// A 'referencing view' for digest item. Does not own its contents. Used by
159/// final runtime implementations for encoding/decoding its log items.
160#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
161pub enum DigestItemRef<'a, Hash: 'a> {
162	/// Reference to `DigestItem::ChangesTrieRoot`.
163	ChangesTrieRoot(&'a Hash),
164	/// A pre-runtime digest.
165	///
166	/// These are messages from the consensus engine to the runtime, although
167	/// the consensus engine can (and should) read them itself to avoid
168	/// code and state duplication.  It is erroneous for a runtime to produce
169	/// these, but this is not (yet) checked.
170	PreRuntime(&'a ConsensusEngineId, &'a Vec<u8>),
171	/// A message from the runtime to the consensus engine. This should *never*
172	/// be generated by the native code of any consensus engine, but this is not
173	/// checked (yet).
174	Consensus(&'a ConsensusEngineId, &'a Vec<u8>),
175	/// Put a Seal on it. This is only used by native code, and is never seen
176	/// by runtimes.
177	Seal(&'a ConsensusEngineId, &'a Vec<u8>),
178	/// Digest item that contains signal from changes tries manager to the
179	/// native code.
180	ChangesTrieSignal(&'a ChangesTrieSignal),
181	/// Any 'non-system' digest item, opaque to the native code.
182	Other(&'a Vec<u8>),
183}
184
185/// Type of the digest item. Used to gain explicit control over `DigestItem` encoding
186/// process. We need an explicit control, because final runtimes are encoding their own
187/// digest items using `DigestItemRef` type and we can't auto-derive `Decode`
188/// trait for `DigestItemRef`.
189#[repr(u32)]
190#[derive(Encode, Decode)]
191pub enum DigestItemType {
192	Other = 0,
193	ChangesTrieRoot = 2,
194	Consensus = 4,
195	Seal = 5,
196	PreRuntime = 6,
197	ChangesTrieSignal = 7,
198}
199
200/// Type of a digest item that contains raw data; this also names the consensus engine ID where
201/// applicable. Used to identify one or more digest items of interest.
202#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
203pub enum OpaqueDigestItemId<'a> {
204	/// Type corresponding to DigestItem::PreRuntime.
205	PreRuntime(&'a ConsensusEngineId),
206	/// Type corresponding to DigestItem::Consensus.
207	Consensus(&'a ConsensusEngineId),
208	/// Type corresponding to DigestItem::Seal.
209	Seal(&'a ConsensusEngineId),
210	/// Some other (non-prescribed) type.
211	Other,
212}
213
214impl<Hash> DigestItem<Hash> {
215	/// Returns a 'referencing view' for this digest item.
216	pub fn dref<'a>(&'a self) -> DigestItemRef<'a, Hash> {
217		match *self {
218			DigestItem::ChangesTrieRoot(ref v) => DigestItemRef::ChangesTrieRoot(v),
219			DigestItem::PreRuntime(ref v, ref s) => DigestItemRef::PreRuntime(v, s),
220			DigestItem::Consensus(ref v, ref s) => DigestItemRef::Consensus(v, s),
221			DigestItem::Seal(ref v, ref s) => DigestItemRef::Seal(v, s),
222			DigestItem::ChangesTrieSignal(ref s) => DigestItemRef::ChangesTrieSignal(s),
223			DigestItem::Other(ref v) => DigestItemRef::Other(v),
224		}
225	}
226
227	/// Returns `Some` if the entry is the `ChangesTrieRoot` entry.
228	pub fn as_changes_trie_root(&self) -> Option<&Hash> {
229		self.dref().as_changes_trie_root()
230	}
231
232	/// Returns `Some` if this entry is the `PreRuntime` entry.
233	pub fn as_pre_runtime(&self) -> Option<(ConsensusEngineId, &[u8])> {
234		self.dref().as_pre_runtime()
235	}
236
237	/// Returns `Some` if this entry is the `Consensus` entry.
238	pub fn as_consensus(&self) -> Option<(ConsensusEngineId, &[u8])> {
239		self.dref().as_consensus()
240	}
241
242	/// Returns `Some` if this entry is the `Seal` entry.
243	pub fn as_seal(&self) -> Option<(ConsensusEngineId, &[u8])> {
244		self.dref().as_seal()
245	}
246
247	/// Returns `Some` if the entry is the `ChangesTrieSignal` entry.
248	pub fn as_changes_trie_signal(&self) -> Option<&ChangesTrieSignal> {
249		self.dref().as_changes_trie_signal()
250	}
251
252	/// Returns Some if `self` is a `DigestItem::Other`.
253	pub fn as_other(&self) -> Option<&[u8]> {
254		match *self {
255			DigestItem::Other(ref v) => Some(&v[..]),
256			_ => None,
257		}
258	}
259
260	/// Returns the opaque data contained in the item if `Some` if this entry has the id given.
261	pub fn try_as_raw(&self, id: OpaqueDigestItemId) -> Option<&[u8]> {
262		self.dref().try_as_raw(id)
263	}
264
265	/// Returns the data contained in the item if `Some` if this entry has the id given, decoded
266	/// to the type provided `T`.
267	pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
268		self.dref().try_to::<T>(id)
269	}
270}
271
272impl<Hash: Encode> Encode for DigestItem<Hash> {
273	fn encode(&self) -> Vec<u8> {
274		self.dref().encode()
275	}
276}
277
278impl<Hash: Encode> codec::EncodeLike for DigestItem<Hash> {}
279
280impl<Hash: Decode> Decode for DigestItem<Hash> {
281	#[allow(deprecated)]
282	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
283		let item_type: DigestItemType = Decode::decode(input)?;
284		match item_type {
285			DigestItemType::ChangesTrieRoot => Ok(DigestItem::ChangesTrieRoot(
286				Decode::decode(input)?,
287			)),
288			DigestItemType::PreRuntime => {
289				let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
290				Ok(DigestItem::PreRuntime(vals.0, vals.1))
291			},
292			DigestItemType::Consensus => {
293				let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
294				Ok(DigestItem::Consensus(vals.0, vals.1))
295			}
296			DigestItemType::Seal => {
297				let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
298				Ok(DigestItem::Seal(vals.0, vals.1))
299			},
300			DigestItemType::ChangesTrieSignal => Ok(DigestItem::ChangesTrieSignal(
301				Decode::decode(input)?,
302			)),
303			DigestItemType::Other => Ok(DigestItem::Other(
304				Decode::decode(input)?,
305			)),
306		}
307	}
308}
309
310impl<'a, Hash> DigestItemRef<'a, Hash> {
311	/// Cast this digest item into `ChangesTrieRoot`.
312	pub fn as_changes_trie_root(&self) -> Option<&'a Hash> {
313		match *self {
314			DigestItemRef::ChangesTrieRoot(ref changes_trie_root) => Some(changes_trie_root),
315			_ => None,
316		}
317	}
318
319	/// Cast this digest item into `PreRuntime`
320	pub fn as_pre_runtime(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
321		match *self {
322			DigestItemRef::PreRuntime(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
323			_ => None,
324		}
325	}
326
327	/// Cast this digest item into `Consensus`
328	pub fn as_consensus(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
329		match *self {
330			DigestItemRef::Consensus(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
331			_ => None,
332		}
333	}
334
335	/// Cast this digest item into `Seal`
336	pub fn as_seal(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
337		match *self {
338			DigestItemRef::Seal(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
339			_ => None,
340		}
341	}
342
343	/// Cast this digest item into `ChangesTrieSignal`.
344	pub fn as_changes_trie_signal(&self) -> Option<&'a ChangesTrieSignal> {
345		match *self {
346			DigestItemRef::ChangesTrieSignal(ref changes_trie_signal) => Some(changes_trie_signal),
347			_ => None,
348		}
349	}
350
351	/// Cast this digest item into `PreRuntime`
352	pub fn as_other(&self) -> Option<&'a [u8]> {
353		match *self {
354			DigestItemRef::Other(ref data) => Some(data),
355			_ => None,
356		}
357	}
358
359	/// Try to match this digest item to the given opaque item identifier; if it matches, then
360	/// return the opaque data it contains.
361	pub fn try_as_raw(&self, id: OpaqueDigestItemId) -> Option<&'a [u8]> {
362		match (id, self) {
363			(OpaqueDigestItemId::Consensus(w), &DigestItemRef::Consensus(v, s)) |
364				(OpaqueDigestItemId::Seal(w), &DigestItemRef::Seal(v, s)) |
365				(OpaqueDigestItemId::PreRuntime(w), &DigestItemRef::PreRuntime(v, s))
366				if v == w => Some(&s[..]),
367			(OpaqueDigestItemId::Other, &DigestItemRef::Other(s)) => Some(&s[..]),
368			_ => None,
369		}
370	}
371
372	/// Try to match this digest item to the given opaque item identifier; if it matches, then
373	/// try to cast to the given data type; if that works, return it.
374	pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
375		self.try_as_raw(id).and_then(|mut x| Decode::decode(&mut x).ok())
376	}
377}
378
379impl<'a, Hash: Encode> Encode for DigestItemRef<'a, Hash> {
380	fn encode(&self) -> Vec<u8> {
381		let mut v = Vec::new();
382
383		match *self {
384			DigestItemRef::ChangesTrieRoot(changes_trie_root) => {
385				DigestItemType::ChangesTrieRoot.encode_to(&mut v);
386				changes_trie_root.encode_to(&mut v);
387			},
388			DigestItemRef::Consensus(val, data) => {
389				DigestItemType::Consensus.encode_to(&mut v);
390				(val, data).encode_to(&mut v);
391			},
392			DigestItemRef::Seal(val, sig) => {
393				DigestItemType::Seal.encode_to(&mut v);
394				(val, sig).encode_to(&mut v);
395			},
396			DigestItemRef::PreRuntime(val, data) => {
397				DigestItemType::PreRuntime.encode_to(&mut v);
398				(val, data).encode_to(&mut v);
399			},
400			DigestItemRef::ChangesTrieSignal(changes_trie_signal) => {
401				DigestItemType::ChangesTrieSignal.encode_to(&mut v);
402				changes_trie_signal.encode_to(&mut v);
403			},
404			DigestItemRef::Other(val) => {
405				DigestItemType::Other.encode_to(&mut v);
406				val.encode_to(&mut v);
407			},
408		}
409
410		v
411	}
412}
413
414impl ChangesTrieSignal {
415	/// Try to cast this signal to NewConfiguration.
416	pub fn as_new_configuration(&self) -> Option<&Option<ChangesTrieConfiguration>> {
417		match self {
418			ChangesTrieSignal::NewConfiguration(config) => Some(config),
419		}
420	}
421}
422
423impl<'a, Hash: Encode> codec::EncodeLike for DigestItemRef<'a, Hash> {}
424
425#[cfg(test)]
426mod tests {
427	use super::*;
428
429	#[test]
430	fn should_serialize_digest() {
431		let digest = Digest {
432			logs: vec![
433				DigestItem::ChangesTrieRoot(4),
434				DigestItem::Other(vec![1, 2, 3]),
435				DigestItem::Seal(*b"test", vec![1, 2, 3])
436			],
437		};
438
439		assert_eq!(
440			::serde_json::to_string(&digest).unwrap(),
441			r#"{"logs":["0x0204000000","0x000c010203","0x05746573740c010203"]}"#
442		);
443	}
444}