block_builder.rs 5.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

use crate::{Client, FullBackend};
use polkadot_test_runtime::{GetLastTimestamp, UncheckedExtrinsic};
use polkadot_primitives::v1::Block;
20
use sp_runtime::{generic::BlockId, Digest, DigestItem};
21
use sp_api::ProvideRuntimeApi;
22
use sp_consensus_babe::{BABE_ENGINE_ID, digests::{PreDigest, SecondaryPlainPreDigest}};
23
24
use sc_block_builder::{BlockBuilderProvider, BlockBuilder};
use sp_state_machine::BasicExternalities;
25
use parity_scale_codec::{Encode, Decode};
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

/// An extension for the test client to init a Polkadot specific block builder.
pub trait InitPolkadotBlockBuilder {
	/// Init a Polkadot specific block builder that works for the test runtime.
	///
	/// This will automatically create and push the inherents for you to make the block valid for the test runtime.
	fn init_polkadot_block_builder(&self) -> sc_block_builder::BlockBuilder<Block, Client, FullBackend>;

	/// Init a Polkadot specific block builder at a specific block that works for the test runtime.
	///
	/// Same as [`InitPolkadotBlockBuilder::init_polkadot_block_builder`] besides that it takes a [`BlockId`] to say
	/// which should be the parent block of the block that is being build.
	fn init_polkadot_block_builder_at(
		&self,
		at: &BlockId<Block>,
	) -> sc_block_builder::BlockBuilder<Block, Client, FullBackend>;
}

impl InitPolkadotBlockBuilder for Client {
	fn init_polkadot_block_builder(
		&self,
	) -> BlockBuilder<Block, Client, FullBackend> {
		let chain_info = self.chain_info();
		self.init_polkadot_block_builder_at(&BlockId::Hash(chain_info.best_hash))
	}

	fn init_polkadot_block_builder_at(
		&self,
		at: &BlockId<Block>,
	) -> BlockBuilder<Block, Client, FullBackend> {
		let last_timestamp = self
			.runtime_api()
			.get_last_timestamp(&at)
			.expect("Get last timestamp");

		// `MinimumPeriod` is a storage parameter type that requires externalities to access the value.
		let minimum_period = BasicExternalities::new_empty()
			.execute_with(|| polkadot_test_runtime::MinimumPeriod::get());

65
66
67
68
69
70
71
		let timestamp = if last_timestamp == 0 {
			std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH)
				.expect("Time is always after UNIX_EPOCH; qed")
				.as_millis() as u64
		} else {
			last_timestamp + minimum_period
		};
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
		// `SlotDuration` is a storage parameter type that requires externalities to access the value.
		let slot_duration = BasicExternalities::new_empty()
			.execute_with(|| polkadot_test_runtime::SlotDuration::get());

		let slot = (timestamp / slot_duration).into();

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

		let mut block_builder = self.new_block_at(at, digest, false)
			.expect("Creates new block builder for test runtime");

		let mut inherent_data = sp_inherents::InherentData::new();

96
97
		inherent_data
			.put_data(sp_timestamp::INHERENT_IDENTIFIER, &timestamp)
98
99
			.expect("Put timestamp inherent data");

100
101
102
103
104
105
106
107
108
		let parent_header = self.header(at)
			.expect("Get the parent block header")
			.expect("The target block header must exist");
		let provisioner_data = polkadot_node_subsystem::messages::ProvisionerInherentData::default();
		let inclusion_inherent_data = (
			provisioner_data.0,
			provisioner_data.1,
			parent_header,
		);
109
110
111
		inherent_data
			.put_data(
				polkadot_primitives::v1::INCLUSION_INHERENT_IDENTIFIER,
112
				&inclusion_inherent_data,
113
114
			)
			.expect("Put inclusion inherent data");
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

		let inherents = block_builder.create_inherents(inherent_data).expect("Creates inherents");

		inherents.into_iter().for_each(|ext| block_builder.push(ext).expect("Pushes inherent"));

		block_builder
	}
}

/// Polkadot specific extensions for the [`BlockBuilder`].
pub trait BlockBuilderExt {
	/// Push a Polkadot test runtime specific extrinsic to the block.
	///
	/// This will internally use the [`BlockBuilder::push`] method, but this method expects a opaque extrinsic. So,
	/// we provide this wrapper which converts a test runtime specific extrinsic to a opaque extrinsic and pushes it to
	/// the block.
	///
	/// Returns the result of the application of the extrinsic.
	fn push_polkadot_extrinsic(&mut self, ext: UncheckedExtrinsic) -> Result<(), sp_blockchain::Error>;
}

impl BlockBuilderExt for BlockBuilder<'_, Block, Client, FullBackend> {
	fn push_polkadot_extrinsic(&mut self, ext: UncheckedExtrinsic) -> Result<(), sp_blockchain::Error> {
		let encoded = ext.encode();
		self.push(
			Decode::decode(&mut &encoded[..])
				.expect("The runtime specific extrinsic always decodes to an opaque extrinsic; qed"),
		)
	}
}