sp_domain_digests/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2
3use parity_scale_codec::{Decode, Encode};
4use sp_runtime::{ConsensusEngineId, DigestItem};
5
6const DOMAIN_REGISTRY_ENGINE_ID: ConsensusEngineId = *b"RGTR";
7
8/// Trait to provide simpler abstractions to create predigests for runtime.
9pub trait AsPredigest {
10    /// Return `consensus_block_hash`
11    fn as_consensus_block_info<Hash: Decode>(&self) -> Option<Hash>;
12
13    /// Creates a new digest of the consensus block that derive the domain block.
14    fn consensus_block_info<Hash: Encode>(consensus_block_hash: Hash) -> Self;
15}
16
17impl AsPredigest for DigestItem {
18    fn as_consensus_block_info<Hash: Decode>(&self) -> Option<Hash> {
19        self.pre_runtime_try_to(&DOMAIN_REGISTRY_ENGINE_ID)
20    }
21
22    fn consensus_block_info<Hash: Encode>(consensus_block_hash: Hash) -> Self {
23        DigestItem::PreRuntime(DOMAIN_REGISTRY_ENGINE_ID, consensus_block_hash.encode())
24    }
25}