referrerpolicy=no-referrer-when-downgrade
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Cumulus.

// Cumulus 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.

// Cumulus 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 Cumulus.  If not, see <http://www.gnu.org/licenses/>.

//! A Cumulus test client.

mod block_builder;
pub use block_builder::*;
use codec::{Decode, Encode};
pub use cumulus_test_runtime as runtime;
use cumulus_test_runtime::AuraId;
pub use polkadot_parachain_primitives::primitives::{
	BlockData, HeadData, ValidationParams, ValidationResult,
};
use runtime::{
	Balance, Block, BlockHashCount, Runtime, RuntimeCall, Signature, SignedPayload, TxExtension,
	UncheckedExtrinsic, VERSION,
};
use sc_consensus_aura::standalone::{seal, slot_author};
pub use sc_executor::error::Result as ExecutorResult;
use sc_executor::HeapAllocStrategy;
use sc_executor_common::runtime_blob::RuntimeBlob;
use sp_api::ProvideRuntimeApi;
use sp_application_crypto::AppCrypto;
use sp_blockchain::HeaderBackend;
use sp_consensus_aura::{AuraApi, Slot};
use sp_core::Pair;
use sp_io::TestExternalities;
use sp_keystore::testing::MemoryKeystore;
use sp_runtime::{generic::Era, traits::Header, BuildStorage, MultiAddress, SaturatedConversion};
use std::sync::Arc;
pub use substrate_test_client::*;

pub type ParachainBlockData = cumulus_primitives_core::ParachainBlockData<Block>;

/// Test client database backend.
pub type Backend = substrate_test_client::Backend<Block>;

/// Test client executor.
pub type Executor = client::LocalCallExecutor<
	Block,
	Backend,
	WasmExecutor<(
		sp_io::SubstrateHostFunctions,
		cumulus_primitives_proof_size_hostfunction::storage_proof_size::HostFunctions,
	)>,
>;

/// Test client builder for Cumulus
pub type TestClientBuilder =
	substrate_test_client::TestClientBuilder<Block, Executor, Backend, GenesisParameters>;

/// LongestChain type for the test runtime/client.
pub type LongestChain = sc_consensus::LongestChain<Backend, Block>;

/// Test client type with `LocalExecutor` and generic Backend.
pub type Client = client::Client<Backend, Executor, Block, runtime::RuntimeApi>;

/// Parameters of test-client builder with test-runtime.
#[derive(Default)]
pub struct GenesisParameters {
	pub endowed_accounts: Vec<cumulus_test_runtime::AccountId>,
}

impl substrate_test_client::GenesisInit for GenesisParameters {
	fn genesis_storage(&self) -> Storage {
		cumulus_test_service::chain_spec::get_chain_spec_with_extra_endowed(
			None,
			self.endowed_accounts.clone(),
			cumulus_test_runtime::WASM_BINARY.expect("WASM binary not compiled!"),
		)
		.build_storage()
		.expect("Builds test runtime genesis storage")
	}
}

/// A `test-runtime` extensions to [`TestClientBuilder`].
pub trait TestClientBuilderExt: Sized {
	/// Build the test client.
	fn build(self) -> Client {
		self.build_with_longest_chain().0
	}

	/// Build the test client and longest chain selector.
	fn build_with_longest_chain(self) -> (Client, LongestChain);
}

impl TestClientBuilderExt for TestClientBuilder {
	fn build_with_longest_chain(self) -> (Client, LongestChain) {
		self.build_with_native_executor(None)
	}
}

/// A `TestClientBuilder` with default backend and executor.
pub trait DefaultTestClientBuilderExt: Sized {
	/// Create new `TestClientBuilder`
	fn new() -> Self;
}

impl DefaultTestClientBuilderExt for TestClientBuilder {
	fn new() -> Self {
		Self::with_default_backend()
	}
}

/// Create an unsigned extrinsic from a runtime call.
pub fn generate_unsigned(function: impl Into<RuntimeCall>) -> UncheckedExtrinsic {
	UncheckedExtrinsic::new_bare(function.into())
}

/// Create a signed extrinsic from a runtime call and sign
/// with the given key pair.
pub fn generate_extrinsic_with_pair(
	client: &Client,
	origin: sp_core::sr25519::Pair,
	function: impl Into<RuntimeCall>,
	nonce: Option<u32>,
) -> UncheckedExtrinsic {
	let current_block_hash = client.info().best_hash;
	let current_block = client.info().best_number.saturated_into();
	let genesis_block = client.hash(0).unwrap().unwrap();
	let nonce = nonce.unwrap_or_default();
	let period =
		BlockHashCount::get().checked_next_power_of_two().map(|c| c / 2).unwrap_or(2) as u64;
	let tip = 0;
	let tx_ext: TxExtension = (
		frame_system::CheckNonZeroSender::<Runtime>::new(),
		frame_system::CheckSpecVersion::<Runtime>::new(),
		frame_system::CheckGenesis::<Runtime>::new(),
		frame_system::CheckEra::<Runtime>::from(Era::mortal(period, current_block)),
		frame_system::CheckNonce::<Runtime>::from(nonce),
		frame_system::CheckWeight::<Runtime>::new(),
		pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
	)
		.into();

	let function = function.into();

	let raw_payload = SignedPayload::from_raw(
		function.clone(),
		tx_ext.clone(),
		((), VERSION.spec_version, genesis_block, current_block_hash, (), (), ()),
	);
	let signature = raw_payload.using_encoded(|e| origin.sign(e));

	UncheckedExtrinsic::new_signed(
		function,
		MultiAddress::Id(origin.public().into()),
		Signature::Sr25519(signature),
		tx_ext,
	)
}

/// Generate an extrinsic from the provided function call, origin and [`Client`].
pub fn generate_extrinsic(
	client: &Client,
	origin: sp_keyring::Sr25519Keyring,
	function: impl Into<RuntimeCall>,
) -> UncheckedExtrinsic {
	generate_extrinsic_with_pair(client, origin.into(), function, None)
}

/// Transfer some token from one account to another using a provided test [`Client`].
pub fn transfer(
	client: &Client,
	origin: sp_keyring::Sr25519Keyring,
	dest: sp_keyring::Sr25519Keyring,
	value: Balance,
) -> UncheckedExtrinsic {
	let function = RuntimeCall::Balances(pallet_balances::Call::transfer_allow_death {
		dest: MultiAddress::Id(dest.public().into()),
		value,
	});

	generate_extrinsic(client, origin, function)
}

/// Call `validate_block` in the given `wasm_blob`.
pub fn validate_block(
	validation_params: ValidationParams,
	wasm_blob: &[u8],
) -> ExecutorResult<ValidationResult> {
	let mut ext = TestExternalities::default();
	let mut ext_ext = ext.ext();

	let heap_pages = HeapAllocStrategy::Static { extra_pages: 1024 };
	let executor = WasmExecutor::<(
		sp_io::SubstrateHostFunctions,
		cumulus_primitives_proof_size_hostfunction::storage_proof_size::HostFunctions,
	)>::builder()
	.with_execution_method(WasmExecutionMethod::default())
	.with_max_runtime_instances(1)
	.with_runtime_cache_size(2)
	.with_onchain_heap_alloc_strategy(heap_pages)
	.with_offchain_heap_alloc_strategy(heap_pages)
	.build();

	executor
		.uncached_call(
			RuntimeBlob::uncompress_if_needed(wasm_blob).expect("RuntimeBlob uncompress & parse"),
			&mut ext_ext,
			false,
			"validate_block",
			&validation_params.encode(),
		)
		.map(|v| ValidationResult::decode(&mut &v[..]).expect("Decode `ValidationResult`."))
}

fn get_keystore() -> sp_keystore::KeystorePtr {
	let keystore = MemoryKeystore::new();
	sp_keyring::Sr25519Keyring::iter().for_each(|key| {
		keystore
			.sr25519_generate_new(
				sp_consensus_aura::sr25519::AuthorityPair::ID,
				Some(&key.to_seed()),
			)
			.expect("Key should be created");
	});
	Arc::new(keystore)
}

/// Given parachain block data and a slot, seal the block with an aura seal. Assumes that the
/// authorities of the test runtime are present in the keyring.
pub fn seal_block(
	block: ParachainBlockData,
	parachain_slot: Slot,
	client: &Client,
) -> ParachainBlockData {
	let parent_hash = block.header().parent_hash;
	let authorities = client.runtime_api().authorities(parent_hash).unwrap();
	let expected_author = slot_author::<<AuraId as AppCrypto>::Pair>(parachain_slot, &authorities)
		.expect("Should be able to find author");

	let (mut header, extrinsics, proof) = block.deconstruct();
	let keystore = get_keystore();
	let seal_digest = seal::<_, sp_consensus_aura::sr25519::AuthorityPair>(
		&header.hash(),
		expected_author,
		&keystore,
	)
	.expect("Should be able to create seal");
	header.digest_mut().push(seal_digest);
	ParachainBlockData::new(header, extrinsics, proof)
}