referrerpolicy=no-referrer-when-downgrade

sc_consensus_manual_seal/consensus/
aura.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Aura consensus data provider, This allows manual seal author blocks that are valid for
20//! runtimes that expect the aura-specific digests.
21
22use crate::{ConsensusDataProvider, Error};
23use sc_client_api::{AuxStore, UsageProvider};
24use sc_consensus::BlockImportParams;
25use sp_api::ProvideRuntimeApi;
26use sp_blockchain::{HeaderBackend, HeaderMetadata};
27use sp_consensus_aura::{
28	digests::CompatibleDigestItem,
29	sr25519::{AuthorityId, AuthoritySignature},
30	AuraApi, Slot, SlotDuration,
31};
32use sp_inherents::InherentData;
33use sp_runtime::{traits::Block as BlockT, Digest, DigestItem};
34use sp_timestamp::TimestampInherentData;
35use std::{marker::PhantomData, sync::Arc};
36
37/// Consensus data provider for Aura.
38pub struct AuraConsensusDataProvider<B, C, P> {
39	// slot duration
40	slot_duration: SlotDuration,
41	// phantom data for required generics
42	_phantom: PhantomData<(B, C, P)>,
43}
44
45impl<B, C, P> AuraConsensusDataProvider<B, C, P>
46where
47	B: BlockT,
48	C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>,
49	C::Api: AuraApi<B, AuthorityId>,
50{
51	/// Creates a new instance of the [`AuraConsensusDataProvider`], requires that `client`
52	/// implements [`sp_consensus_aura::AuraApi`]
53	pub fn new(client: Arc<C>) -> Self {
54		let slot_duration = sc_consensus_aura::slot_duration(&*client)
55			.expect("slot_duration is always present; qed.");
56
57		Self { slot_duration, _phantom: PhantomData }
58	}
59}
60
61impl<B, C, P> ConsensusDataProvider<B> for AuraConsensusDataProvider<B, C, P>
62where
63	B: BlockT,
64	C: AuxStore
65		+ HeaderBackend<B>
66		+ HeaderMetadata<B, Error = sp_blockchain::Error>
67		+ UsageProvider<B>
68		+ ProvideRuntimeApi<B>,
69	C::Api: AuraApi<B, AuthorityId>,
70	P: Send + Sync,
71{
72	type Proof = P;
73
74	fn create_digest(
75		&self,
76		_parent: &B::Header,
77		inherents: &InherentData,
78	) -> Result<Digest, Error> {
79		let timestamp =
80			inherents.timestamp_inherent_data()?.expect("Timestamp is always present; qed");
81
82		// we always calculate the new slot number based on the current time-stamp and the slot
83		// duration.
84		let digest_item = <DigestItem as CompatibleDigestItem<AuthoritySignature>>::aura_pre_digest(
85			Slot::from_timestamp(timestamp, self.slot_duration),
86		);
87
88		Ok(Digest { logs: vec![digest_item] })
89	}
90
91	fn append_block_import(
92		&self,
93		_parent: &B::Header,
94		_params: &mut BlockImportParams<B>,
95		_inherents: &InherentData,
96		_proof: Self::Proof,
97	) -> Result<(), Error> {
98		Ok(())
99	}
100}