1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::BlockT;
use parity_scale_codec::Encode;
use sc_cli::Result;
use sp_consensus_aura::{Slot, SlotDuration, AURA_ENGINE_ID};
use sp_consensus_babe::{
	digests::{PreDigest, SecondaryPlainPreDigest},
	BABE_ENGINE_ID,
};
use sp_inherents::{InherentData, InherentDataProvider};
use sp_runtime::{Digest, DigestItem};
use sp_timestamp::TimestampInherentData;

/// Something that can create inherent data providers and pre-runtime digest.
///
/// It is possible for the caller to provide custom arguments to the callee by setting the
/// `ExtraArgs` generic parameter.
///
/// This module already provides some convenience implementation of this trait for closures. So, it
/// should not be required to implement it directly.
#[async_trait::async_trait]
pub trait BlockBuildingInfoProvider<Block: BlockT, ExtraArgs = ()> {
	type InherentDataProviders: InherentDataProvider;

	async fn get_inherent_providers_and_pre_digest(
		&self,
		parent_hash: Block::Hash,
		extra_args: ExtraArgs,
	) -> Result<(Self::InherentDataProviders, Vec<DigestItem>)>;
}

#[async_trait::async_trait]
impl<F, Block, IDP, ExtraArgs, Fut> BlockBuildingInfoProvider<Block, ExtraArgs> for F
where
	Block: BlockT,
	F: Fn(Block::Hash, ExtraArgs) -> Fut + Sync + Send,
	Fut: std::future::Future<Output = Result<(IDP, Vec<DigestItem>)>> + Send + 'static,
	IDP: InherentDataProvider + 'static,
	ExtraArgs: Send + 'static,
{
	type InherentDataProviders = IDP;

	async fn get_inherent_providers_and_pre_digest(
		&self,
		parent: Block::Hash,
		extra_args: ExtraArgs,
	) -> Result<(Self::InherentDataProviders, Vec<DigestItem>)> {
		(*self)(parent, extra_args).await
	}
}

/// Provides [`BlockBuildingInfoProvider`] implementation for chains that include timestamp inherent
/// and use Aura for a block production.
///
/// It depends only on the expected block production frequency, i.e. `blocktime_millis`.
pub fn timestamp_with_aura_info<Block: BlockT>(
	blocktime_millis: u64,
) -> impl BlockBuildingInfoProvider<Block, Option<(InherentData, Digest)>> {
	move |_, maybe_prev_info: Option<(InherentData, Digest)>| async move {
		let timestamp_idp = match maybe_prev_info {
			Some((inherent_data, _)) => sp_timestamp::InherentDataProvider::new(
				inherent_data.timestamp_inherent_data().unwrap().unwrap() + blocktime_millis,
			),
			None => sp_timestamp::InherentDataProvider::from_system_time(),
		};

		let slot =
			Slot::from_timestamp(*timestamp_idp, SlotDuration::from_millis(blocktime_millis));
		let digest = vec![DigestItem::PreRuntime(AURA_ENGINE_ID, slot.encode())];

		Ok((timestamp_idp, digest))
	}
}

/// Provides [`BlockBuildingInfoProvider`] implementation for chains that include timestamp inherent
/// and use Babe for a block production.
///
/// It depends only on the expected block production frequency, i.e. `blocktime_millis`.
pub fn timestamp_with_babe_info<Block: BlockT>(
	blocktime_millis: u64,
) -> impl BlockBuildingInfoProvider<Block, Option<(InherentData, Digest)>> {
	move |_, maybe_prev_info: Option<(InherentData, Digest)>| async move {
		let timestamp_idp = match maybe_prev_info {
			Some((inherent_data, _)) => sp_timestamp::InherentDataProvider::new(
				inherent_data.timestamp_inherent_data().unwrap().unwrap() + blocktime_millis,
			),
			None => sp_timestamp::InherentDataProvider::from_system_time(),
		};

		let slot =
			Slot::from_timestamp(*timestamp_idp, SlotDuration::from_millis(blocktime_millis));
		let slot_idp = sp_consensus_babe::inherents::InherentDataProvider::new(slot);

		let digest = vec![DigestItem::PreRuntime(
			BABE_ENGINE_ID,
			PreDigest::SecondaryPlain(SecondaryPlainPreDigest { slot, authority_index: 0 })
				.encode(),
		)];

		Ok(((slot_idp, timestamp_idp), digest))
	}
}

/// Provides [`BlockBuildingInfoProvider`] implementation for chains that use:
///  - timestamp inherent,
///  - Babe for a block production (inherent + digest),
///  - uncles inherent,
///  - storage proof inherent
///
/// It depends only on the expected block production frequency, i.e. `blocktime_millis`.
pub fn substrate_info<Block: BlockT>(
	blocktime_millis: u64,
) -> impl BlockBuildingInfoProvider<Block, Option<(InherentData, Digest)>> {
	move |_, maybe_prev_info: Option<(InherentData, Digest)>| async move {
		let timestamp_idp = match maybe_prev_info {
			Some((inherent_data, _)) => sp_timestamp::InherentDataProvider::new(
				inherent_data.timestamp_inherent_data().unwrap().unwrap() + blocktime_millis,
			),
			None => sp_timestamp::InherentDataProvider::from_system_time(),
		};

		let slot =
			Slot::from_timestamp(*timestamp_idp, SlotDuration::from_millis(blocktime_millis));
		let slot_idp = sp_consensus_babe::inherents::InherentDataProvider::new(slot);

		let storage_proof_idp = sp_transaction_storage_proof::InherentDataProvider::new(None);

		let digest = vec![DigestItem::PreRuntime(
			BABE_ENGINE_ID,
			PreDigest::SecondaryPlain(SecondaryPlainPreDigest { slot, authority_index: 0 })
				.encode(),
		)];

		Ok(((slot_idp, timestamp_idp, storage_proof_idp), digest))
	}
}