|
28 | 28 | //! impl pallet_block_production_log::Config for Runtime { |
29 | 29 | //! type BlockProducerId = BlockAuthor; |
30 | 30 | //! |
31 | | -//! type Moment = Slot; |
| 31 | +//! type Moment = Slot; |
32 | 32 | //! |
33 | | -//! type GetMoment = Aura; |
34 | | -//! type GetAuthor = Aura; |
| 33 | +//! type GetMoment = FromStorage<pallet_aura::CurrentSlot<Runtime>>; |
| 34 | +//! type GetAuthor = FromFindAuthorIndex<Runtime, Aura, u32>; |
35 | 35 | //! } |
36 | 36 | //! ``` |
37 | 37 | //! |
38 | | -//! Implementations supporting use of Aura and SessionCommitteeManagement as sources of moment and block producer |
39 | | -//! can be enabled using the `aura-compat` feature flag. |
40 | | -//! |
41 | 38 | //! #### Defining block producer ID |
42 | 39 | //! |
43 | 40 | //! The pallet expects the Partner Chain to provide a type representing its block producers. |
@@ -98,26 +95,30 @@ mod mock; |
98 | 95 | #[cfg(test)] |
99 | 96 | mod test; |
100 | 97 |
|
| 98 | +use core::marker::PhantomData; |
| 99 | + |
101 | 100 | pub use pallet::*; |
102 | 101 | pub use weights::WeightInfo; |
103 | 102 |
|
104 | 103 | /// Source of the current block's author |
105 | | -/// |
106 | | -/// An implementation that will fetch block author from `pallet_session_validator_management` based on |
107 | | -/// an index provided by Aura pallet is provided under feature flag `aura-compat`. |
108 | 104 | pub trait GetAuthor<BlockProducerId> { |
109 | 105 | /// Returns the current block's author |
110 | 106 | fn get_author() -> Option<BlockProducerId>; |
111 | 107 | } |
112 | 108 |
|
| 109 | +/// [GetAuthor] implementation that uses a [FindAuthor] instance to get the current block's author index |
| 110 | +/// of type `I` and uses it to read the author from `pallet_session_validator_management`. |
| 111 | +pub struct FromFindAuthorIndex<T, FA, I>(PhantomData<(T, FA, I)>); |
| 112 | + |
113 | 113 | /// Source of the current block's moment |
114 | | -/// |
115 | | -/// Implementation that fetches current slot from Aura pallet is provided under feature flat `aura-compat`. |
116 | 114 | pub trait GetMoment<Moment> { |
117 | 115 | /// Returns the current block's moment |
118 | 116 | fn get_moment() -> Option<Moment>; |
119 | 117 | } |
120 | 118 |
|
| 119 | +/// [GetMoment] implementation that fetches current block's `Moment` from storage `S` |
| 120 | +pub struct FromStorage<S>(PhantomData<S>); |
| 121 | + |
121 | 122 | #[frame_support::pallet] |
122 | 123 | pub mod pallet { |
123 | 124 | use super::*; |
@@ -219,25 +220,40 @@ mod block_participation { |
219 | 220 | } |
220 | 221 | } |
221 | 222 |
|
222 | | -#[cfg(feature = "aura-compat")] |
223 | | -mod aura_support { |
224 | | - use crate::*; |
| 223 | +mod source_impls { |
| 224 | + use super::*; |
| 225 | + use frame_support::{ |
| 226 | + pallet_prelude::StorageValue, |
| 227 | + storage::types::QueryKindTrait, |
| 228 | + traits::{FindAuthor, StorageInstance}, |
| 229 | + }; |
225 | 230 | use pallet_session_validator_management as psvm; |
226 | | - use sp_consensus_aura::Slot; |
| 231 | + use parity_scale_codec::FullCodec; |
| 232 | + use sp_runtime::traits::Get; |
227 | 233 |
|
228 | | - impl<T: crate::Config + pallet_aura::Config> GetMoment<Slot> for pallet_aura::Pallet<T> { |
229 | | - fn get_moment() -> Option<Slot> { |
230 | | - Some(pallet_aura::CurrentSlot::<T>::get()) |
| 234 | + impl<BlockProducerId, I, FA, T> GetAuthor<BlockProducerId> for FromFindAuthorIndex<T, FA, I> |
| 235 | + where |
| 236 | + FA: FindAuthor<I>, |
| 237 | + I: TryInto<usize>, |
| 238 | + T: psvm::Config, |
| 239 | + psvm::CommitteeMemberOf<T>: Into<BlockProducerId>, |
| 240 | + { |
| 241 | + fn get_author() -> Option<BlockProducerId> { |
| 242 | + Some(psvm::Pallet::<T>::find_current_authority::<I, FA>()?.into()) |
231 | 243 | } |
232 | 244 | } |
233 | 245 |
|
234 | | - impl<BlockProducerId, T> GetAuthor<BlockProducerId> for pallet_aura::Pallet<T> |
| 246 | + impl<Prefix, Value, Moment, QueryKind, OnEmpty> GetMoment<Moment> |
| 247 | + for FromStorage<StorageValue<Prefix, Value, QueryKind, OnEmpty>> |
235 | 248 | where |
236 | | - T: crate::Config + psvm::Config + pallet_aura::Config, |
237 | | - psvm::CommitteeMemberOf<T>: Into<BlockProducerId>, |
| 249 | + Prefix: StorageInstance, |
| 250 | + Value: FullCodec, |
| 251 | + QueryKind: QueryKindTrait<Value, OnEmpty>, |
| 252 | + OnEmpty: Get<QueryKind::Query> + 'static, |
| 253 | + Option<Moment>: From<<QueryKind as QueryKindTrait<Value, OnEmpty>>::Query>, |
238 | 254 | { |
239 | | - fn get_author() -> Option<BlockProducerId> { |
240 | | - Some(psvm::Pallet::<T>::find_current_authority::<u32, pallet_aura::Pallet<T>>()?.into()) |
| 255 | + fn get_moment() -> Option<Moment> { |
| 256 | + StorageValue::<Prefix, Value, QueryKind, OnEmpty>::get().into() |
241 | 257 | } |
242 | 258 | } |
243 | 259 | } |
0 commit comments