|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) 2021-2022 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 | +use crate::BlockT; |
| 19 | +use parity_scale_codec::Encode; |
| 20 | +use sc_cli::Result; |
| 21 | +use sp_consensus_aura::{Slot, SlotDuration, AURA_ENGINE_ID}; |
| 22 | +use sp_consensus_babe::{ |
| 23 | + digests::{PreDigest, SecondaryPlainPreDigest}, |
| 24 | + BABE_ENGINE_ID, |
| 25 | +}; |
| 26 | +use sp_inherents::{InherentData, InherentDataProvider}; |
| 27 | +use sp_runtime::{Digest, DigestItem}; |
| 28 | +use sp_timestamp::TimestampInherentData; |
| 29 | + |
| 30 | +/// Something that can create inherent data providers and pre-runtime digest. |
| 31 | +/// |
| 32 | +/// It is possible for the caller to provide custom arguments to the callee by setting the |
| 33 | +/// `ExtraArgs` generic parameter. |
| 34 | +/// |
| 35 | +/// This module already provides some convenience implementation of this trait for closures. So, it |
| 36 | +/// should not be required to implement it directly. |
| 37 | +#[async_trait::async_trait] |
| 38 | +pub trait BlockBuildingInfoProvider<Block: BlockT, ExtraArgs = ()> { |
| 39 | + type InherentDataProviders: InherentDataProvider; |
| 40 | + |
| 41 | + async fn get_inherent_providers_and_pre_digest( |
| 42 | + &self, |
| 43 | + parent_hash: Block::Hash, |
| 44 | + extra_args: ExtraArgs, |
| 45 | + ) -> Result<(Self::InherentDataProviders, Vec<DigestItem>)>; |
| 46 | +} |
| 47 | + |
| 48 | +#[async_trait::async_trait] |
| 49 | +impl<F, Block, IDP, ExtraArgs, Fut> BlockBuildingInfoProvider<Block, ExtraArgs> for F |
| 50 | +where |
| 51 | + Block: BlockT, |
| 52 | + F: Fn(Block::Hash, ExtraArgs) -> Fut + Sync + Send, |
| 53 | + Fut: std::future::Future<Output = Result<(IDP, Vec<DigestItem>)>> + Send + 'static, |
| 54 | + IDP: InherentDataProvider + 'static, |
| 55 | + ExtraArgs: Send + 'static, |
| 56 | +{ |
| 57 | + type InherentDataProviders = IDP; |
| 58 | + |
| 59 | + async fn get_inherent_providers_and_pre_digest( |
| 60 | + &self, |
| 61 | + parent: Block::Hash, |
| 62 | + extra_args: ExtraArgs, |
| 63 | + ) -> Result<(Self::InherentDataProviders, Vec<DigestItem>)> { |
| 64 | + (*self)(parent, extra_args).await |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// Provides [`BlockBuildingInfoProvider`] implementation for chains that include timestamp inherent |
| 69 | +/// and use Aura for a block production. |
| 70 | +/// |
| 71 | +/// It depends only on the expected block production frequency, i.e. `blocktime_millis`. |
| 72 | +pub fn timestamp_with_aura_info<Block: BlockT>( |
| 73 | + blocktime_millis: u64, |
| 74 | +) -> impl BlockBuildingInfoProvider<Block, Option<(InherentData, Digest)>> { |
| 75 | + move |_, maybe_prev_info: Option<(InherentData, Digest)>| async move { |
| 76 | + let timestamp_idp = match maybe_prev_info { |
| 77 | + Some((inherent_data, _)) => sp_timestamp::InherentDataProvider::new( |
| 78 | + inherent_data.timestamp_inherent_data().unwrap().unwrap() + blocktime_millis, |
| 79 | + ), |
| 80 | + None => sp_timestamp::InherentDataProvider::from_system_time(), |
| 81 | + }; |
| 82 | + |
| 83 | + let slot = |
| 84 | + Slot::from_timestamp(*timestamp_idp, SlotDuration::from_millis(blocktime_millis)); |
| 85 | + let digest = vec![DigestItem::PreRuntime(AURA_ENGINE_ID, slot.encode())]; |
| 86 | + |
| 87 | + Ok((timestamp_idp, digest)) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/// Provides [`BlockBuildingInfoProvider`] implementation for chains that include timestamp inherent |
| 92 | +/// and use Babe for a block production. |
| 93 | +/// |
| 94 | +/// It depends only on the expected block production frequency, i.e. `blocktime_millis`. |
| 95 | +pub fn timestamp_with_babe_info<Block: BlockT>( |
| 96 | + blocktime_millis: u64, |
| 97 | +) -> impl BlockBuildingInfoProvider<Block, Option<(InherentData, Digest)>> { |
| 98 | + move |_, maybe_prev_info: Option<(InherentData, Digest)>| async move { |
| 99 | + let timestamp_idp = match maybe_prev_info { |
| 100 | + Some((inherent_data, _)) => sp_timestamp::InherentDataProvider::new( |
| 101 | + inherent_data.timestamp_inherent_data().unwrap().unwrap() + blocktime_millis, |
| 102 | + ), |
| 103 | + None => sp_timestamp::InherentDataProvider::from_system_time(), |
| 104 | + }; |
| 105 | + |
| 106 | + let slot = |
| 107 | + Slot::from_timestamp(*timestamp_idp, SlotDuration::from_millis(blocktime_millis)); |
| 108 | + let slot_idp = sp_consensus_babe::inherents::InherentDataProvider::new(slot); |
| 109 | + |
| 110 | + let digest = vec![DigestItem::PreRuntime( |
| 111 | + BABE_ENGINE_ID, |
| 112 | + PreDigest::SecondaryPlain(SecondaryPlainPreDigest { slot, authority_index: 0 }) |
| 113 | + .encode(), |
| 114 | + )]; |
| 115 | + |
| 116 | + Ok(((slot_idp, timestamp_idp), digest)) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/// Provides [`BlockBuildingInfoProvider`] implementation for chains that use: |
| 121 | +/// - timestamp inherent, |
| 122 | +/// - Babe for a block production (inherent + digest), |
| 123 | +/// - uncles inherent, |
| 124 | +/// - storage proof inherent |
| 125 | +/// |
| 126 | +/// It depends only on the expected block production frequency, i.e. `blocktime_millis`. |
| 127 | +pub fn substrate_info<Block: BlockT>( |
| 128 | + blocktime_millis: u64, |
| 129 | +) -> impl BlockBuildingInfoProvider<Block, Option<(InherentData, Digest)>> { |
| 130 | + move |_, maybe_prev_info: Option<(InherentData, Digest)>| async move { |
| 131 | + let timestamp_idp = match maybe_prev_info { |
| 132 | + Some((inherent_data, _)) => sp_timestamp::InherentDataProvider::new( |
| 133 | + inherent_data.timestamp_inherent_data().unwrap().unwrap() + blocktime_millis, |
| 134 | + ), |
| 135 | + None => sp_timestamp::InherentDataProvider::from_system_time(), |
| 136 | + }; |
| 137 | + |
| 138 | + let slot = |
| 139 | + Slot::from_timestamp(*timestamp_idp, SlotDuration::from_millis(blocktime_millis)); |
| 140 | + let slot_idp = sp_consensus_babe::inherents::InherentDataProvider::new(slot); |
| 141 | + |
| 142 | + let storage_proof_idp = sp_transaction_storage_proof::InherentDataProvider::new(None); |
| 143 | + |
| 144 | + let digest = vec![DigestItem::PreRuntime( |
| 145 | + BABE_ENGINE_ID, |
| 146 | + PreDigest::SecondaryPlain(SecondaryPlainPreDigest { slot, authority_index: 0 }) |
| 147 | + .encode(), |
| 148 | + )]; |
| 149 | + |
| 150 | + Ok(((slot_idp, timestamp_idp, storage_proof_idp), digest)) |
| 151 | + } |
| 152 | +} |
0 commit comments