|
| 1 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Cumulus. |
| 3 | + |
| 4 | +// Cumulus is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Cumulus is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Cumulus. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//! Aura-related primitives for cumulus parachain collators. |
| 18 | +
|
| 19 | +use codec::Codec; |
| 20 | +use cumulus_primitives_aura::AuraUnincludedSegmentApi; |
| 21 | +use cumulus_primitives_core::BlockT; |
| 22 | +use sp_consensus_aura::AuraApi; |
| 23 | +use sp_runtime::app_crypto::{AppCrypto, AppPair, AppSignature, Pair}; |
| 24 | + |
| 25 | +/// Convenience trait for defining the basic bounds of an `AuraId`. |
| 26 | +pub trait AuraIdT: AppCrypto<Pair = Self::BoundedPair> + Codec + Send { |
| 27 | + /// Extra bounds for the `Pair`. |
| 28 | + type BoundedPair: AppPair + AppCrypto<Signature = Self::BoundedSignature>; |
| 29 | + |
| 30 | + /// Extra bounds for the `Signature`. |
| 31 | + type BoundedSignature: AppSignature |
| 32 | + + TryFrom<Vec<u8>> |
| 33 | + + std::hash::Hash |
| 34 | + + sp_runtime::traits::Member |
| 35 | + + Codec; |
| 36 | +} |
| 37 | + |
| 38 | +impl<T> AuraIdT for T |
| 39 | +where |
| 40 | + T: AppCrypto + Codec + Send + Sync, |
| 41 | + <<T as AppCrypto>::Pair as AppCrypto>::Signature: |
| 42 | + TryFrom<Vec<u8>> + std::hash::Hash + sp_runtime::traits::Member + Codec, |
| 43 | +{ |
| 44 | + type BoundedPair = <T as AppCrypto>::Pair; |
| 45 | + type BoundedSignature = <<T as AppCrypto>::Pair as AppCrypto>::Signature; |
| 46 | +} |
| 47 | + |
| 48 | +/// Convenience trait for defining the basic bounds of a parachain runtime that supports |
| 49 | +/// the Aura consensus. |
| 50 | +pub trait AuraRuntimeApi<Block: BlockT, AuraId: AuraIdT>: |
| 51 | + sp_api::ApiExt<Block> |
| 52 | + + AuraApi<Block, <AuraId::BoundedPair as Pair>::Public> |
| 53 | + + AuraUnincludedSegmentApi<Block> |
| 54 | + + Sized |
| 55 | +{ |
| 56 | + /// Check if the runtime has the Aura API. |
| 57 | + fn has_aura_api(&self, at: Block::Hash) -> bool { |
| 58 | + self.has_api::<dyn AuraApi<Block, <AuraId::BoundedPair as Pair>::Public>>(at) |
| 59 | + .unwrap_or(false) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<T, Block: BlockT, AuraId: AuraIdT> AuraRuntimeApi<Block, AuraId> for T where |
| 64 | + T: sp_api::ApiExt<Block> |
| 65 | + + AuraApi<Block, <AuraId::BoundedPair as Pair>::Public> |
| 66 | + + AuraUnincludedSegmentApi<Block> |
| 67 | +{ |
| 68 | +} |
0 commit comments