referrerpolicy=no-referrer-when-downgrade

pallet_authorship/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) 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//! Authorship tracking for FRAME runtimes.
19//!
20//! This tracks the current author of the block.
21
22#![cfg_attr(not(feature = "std"), no_std)]
23
24use frame_support::traits::FindAuthor;
25
26pub use pallet::*;
27
28/// An event handler for the authorship pallet. There is a dummy implementation
29/// for `()`, which does nothing.
30#[impl_trait_for_tuples::impl_for_tuples(30)]
31pub trait EventHandler<Author, BlockNumber> {
32	/// Note that the given account ID is the author of the current block.
33	fn note_author(author: Author);
34}
35
36#[frame_support::pallet]
37pub mod pallet {
38	use super::*;
39	use frame_support::pallet_prelude::*;
40	use frame_system::pallet_prelude::*;
41
42	#[pallet::config]
43	pub trait Config: frame_system::Config {
44		/// Find the author of a block.
45		type FindAuthor: FindAuthor<Self::AccountId>;
46		/// An event handler for authored blocks.
47		type EventHandler: EventHandler<Self::AccountId, BlockNumberFor<Self>>;
48	}
49
50	#[pallet::pallet]
51	pub struct Pallet<T>(_);
52
53	#[pallet::hooks]
54	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
55		fn on_initialize(_: BlockNumberFor<T>) -> Weight {
56			if let Some(author) = Self::author() {
57				T::EventHandler::note_author(author);
58			}
59
60			Weight::zero()
61		}
62
63		fn on_finalize(_: BlockNumberFor<T>) {
64			// ensure we never go to trie with these values.
65			<Author<T>>::kill();
66		}
67
68		#[cfg(feature = "try-runtime")]
69		fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
70			Self::do_try_state()
71		}
72	}
73
74	#[pallet::storage]
75	#[pallet::whitelist_storage]
76	/// Author of current block.
77	pub(super) type Author<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;
78}
79
80impl<T: Config> Pallet<T> {
81	/// Fetch the author of the block.
82	///
83	/// This is safe to invoke in `on_initialize` implementations, as well
84	/// as afterwards.
85	pub fn author() -> Option<T::AccountId> {
86		// Check the memorized storage value.
87		if let Some(author) = <Author<T>>::get() {
88			return Some(author);
89		}
90
91		let digest = <frame_system::Pallet<T>>::digest();
92		let pre_runtime_digests = digest.logs.iter().filter_map(|d| d.as_pre_runtime());
93		T::FindAuthor::find_author(pre_runtime_digests).inspect(|a| {
94			<Author<T>>::put(&a);
95		})
96	}
97}
98
99#[cfg(any(feature = "try-runtime", test))]
100impl<T: Config> Pallet<T> {
101	/// Ensure the correctness of the state of this pallet.
102	///
103	/// # Invariants
104	///
105	/// * If `Author` storage contains a value, it must match the author derived from the current
106	///   block's digest via `FindAuthor`.
107	pub fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> {
108		use frame_support::ensure;
109
110		if let Some(stored_author) = <Author<T>>::get() {
111			let digest = <frame_system::Pallet<T>>::digest();
112			let pre_runtime_digests = digest.logs.iter().filter_map(|d| d.as_pre_runtime());
113			if let Some(expected_author) = T::FindAuthor::find_author(pre_runtime_digests) {
114				ensure!(
115					stored_author == expected_author,
116					"Stored author does not match the author derived from digest"
117				);
118			}
119		}
120
121		Ok(())
122	}
123}
124
125#[cfg(test)]
126mod tests {
127	use super::*;
128	use crate as pallet_authorship;
129	use codec::{Decode, Encode};
130	use frame_support::{derive_impl, ConsensusEngineId};
131	use sp_core::H256;
132	use sp_runtime::{
133		generic::DigestItem, testing::Header, traits::Header as HeaderT, BuildStorage,
134	};
135
136	type Block = frame_system::mocking::MockBlock<Test>;
137
138	frame_support::construct_runtime!(
139		pub enum Test
140		{
141			System: frame_system,
142			Authorship: pallet_authorship,
143		}
144	);
145
146	#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
147	impl frame_system::Config for Test {
148		type Block = Block;
149	}
150
151	impl pallet::Config for Test {
152		type FindAuthor = AuthorGiven;
153		type EventHandler = ();
154	}
155
156	const TEST_ID: ConsensusEngineId = [1, 2, 3, 4];
157
158	pub struct AuthorGiven;
159
160	impl FindAuthor<u64> for AuthorGiven {
161		fn find_author<'a, I>(digests: I) -> Option<u64>
162		where
163			I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
164		{
165			for (id, mut data) in digests {
166				if id == TEST_ID {
167					return u64::decode(&mut data).ok();
168				}
169			}
170
171			None
172		}
173	}
174
175	fn seal_header(mut header: Header, author: u64) -> Header {
176		{
177			let digest = header.digest_mut();
178			digest.logs.push(DigestItem::PreRuntime(TEST_ID, author.encode()));
179			digest.logs.push(DigestItem::Seal(TEST_ID, author.encode()));
180		}
181
182		header
183	}
184
185	fn create_header(number: u64, parent_hash: H256, state_root: H256) -> Header {
186		Header::new(number, Default::default(), state_root, parent_hash, Default::default())
187	}
188
189	fn new_test_ext() -> sp_io::TestExternalities {
190		let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
191		t.into()
192	}
193
194	#[test]
195	fn sets_author_lazily() {
196		new_test_ext().execute_with(|| {
197			let author = 42;
198			let mut header =
199				seal_header(create_header(1, Default::default(), [1; 32].into()), author);
200
201			header.digest_mut().pop(); // pop the seal off.
202			System::reset_events();
203			System::initialize(&1, &Default::default(), header.digest());
204
205			assert_eq!(Authorship::author(), Some(author));
206			Authorship::do_try_state().unwrap();
207		});
208	}
209}