1#![cfg_attr(not(feature = "std"), no_std)]
12
13use topsoil_core::traits::FindAuthor;
14
15pub use pallet::*;
16
17#[impl_trait_for_tuples::impl_for_tuples(30)]
20pub trait EventHandler<Author, BlockNumber> {
21 fn note_author(author: Author);
23}
24
25#[topsoil_core::pallet]
26pub mod pallet {
27 use super::*;
28 use topsoil_core::pallet_prelude::*;
29 use topsoil_core::system::pallet_prelude::*;
30
31 #[pallet::config]
32 pub trait Config: topsoil_core::system::Config {
33 type FindAuthor: FindAuthor<Self::AccountId>;
35 type EventHandler: EventHandler<Self::AccountId, BlockNumberFor<Self>>;
37 }
38
39 #[pallet::pallet]
40 pub struct Pallet<T>(_);
41
42 #[pallet::hooks]
43 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
44 fn on_initialize(_: BlockNumberFor<T>) -> Weight {
45 if let Some(author) = Self::author() {
46 T::EventHandler::note_author(author);
47 }
48
49 Weight::zero()
50 }
51
52 fn on_finalize(_: BlockNumberFor<T>) {
53 <Author<T>>::kill();
55 }
56 }
57
58 #[pallet::storage]
59 #[pallet::whitelist_storage]
60 pub(super) type Author<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;
62}
63
64impl<T: Config> Pallet<T> {
65 pub fn author() -> Option<T::AccountId> {
70 if let Some(author) = <Author<T>>::get() {
72 return Some(author);
73 }
74
75 let digest = <topsoil_core::system::Pallet<T>>::digest();
76 let pre_runtime_digests = digest.logs.iter().filter_map(|d| d.as_pre_runtime());
77 T::FindAuthor::find_author(pre_runtime_digests).inspect(|a| {
78 <Author<T>>::put(&a);
79 })
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86 use crate as plant_authorship;
87 use codec::{Decode, Encode};
88 use subsoil::core::H256;
89 use subsoil::runtime::{
90 generic::DigestItem, testing::Header, traits::Header as HeaderT, BuildStorage,
91 };
92 use topsoil_core::{derive_impl, ConsensusEngineId};
93
94 type Block = topsoil_core::system::mocking::MockBlock<Test>;
95
96 topsoil_core::construct_runtime!(
97 pub enum Test
98 {
99 System: topsoil_core::system,
100 Authorship: plant_authorship,
101 }
102 );
103
104 #[derive_impl(topsoil_core::system::config_preludes::TestDefaultConfig)]
105 impl topsoil_core::system::Config for Test {
106 type Block = Block;
107 }
108
109 impl pallet::Config for Test {
110 type FindAuthor = AuthorGiven;
111 type EventHandler = ();
112 }
113
114 const TEST_ID: ConsensusEngineId = [1, 2, 3, 4];
115
116 pub struct AuthorGiven;
117
118 impl FindAuthor<u64> for AuthorGiven {
119 fn find_author<'a, I>(digests: I) -> Option<u64>
120 where
121 I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
122 {
123 for (id, mut data) in digests {
124 if id == TEST_ID {
125 return u64::decode(&mut data).ok();
126 }
127 }
128
129 None
130 }
131 }
132
133 fn seal_header(mut header: Header, author: u64) -> Header {
134 {
135 let digest = header.digest_mut();
136 digest.logs.push(DigestItem::PreRuntime(TEST_ID, author.encode()));
137 digest.logs.push(DigestItem::Seal(TEST_ID, author.encode()));
138 }
139
140 header
141 }
142
143 fn create_header(number: u64, parent_hash: H256, state_root: H256) -> Header {
144 Header::new(number, Default::default(), state_root, parent_hash, Default::default())
145 }
146
147 fn new_test_ext() -> subsoil::io::TestExternalities {
148 let t = topsoil_core::system::GenesisConfig::<Test>::default().build_storage().unwrap();
149 t.into()
150 }
151
152 #[test]
153 fn sets_author_lazily() {
154 new_test_ext().execute_with(|| {
155 let author = 42;
156 let mut header =
157 seal_header(create_header(1, Default::default(), [1; 32].into()), author);
158
159 header.digest_mut().pop(); System::reset_events();
161 System::initialize(&1, &Default::default(), header.digest());
162
163 assert_eq!(Authorship::author(), Some(author));
164 });
165 }
166}