referrerpolicy=no-referrer-when-downgrade

cumulus_test_runtime/
test_pallet.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3
4// Cumulus is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Cumulus is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Cumulus.  If not, see <http://www.gnu.org/licenses/>.
16
17/// A special pallet that exposes dispatchables that are only useful for testing.
18pub use pallet::*;
19
20/// Some key that we set in genesis and only read in [`TestOnRuntimeUpgrade`] to ensure that
21/// [`OnRuntimeUpgrade`] works as expected.
22pub const TEST_RUNTIME_UPGRADE_KEY: &[u8] = b"+test_runtime_upgrade_key+";
23
24#[frame_support::pallet(dev_mode)]
25pub mod pallet {
26	use crate::test_pallet::TEST_RUNTIME_UPGRADE_KEY;
27	use alloc::vec;
28	use frame_support::pallet_prelude::*;
29	use frame_system::pallet_prelude::*;
30
31	#[pallet::pallet]
32	pub struct Pallet<T>(_);
33
34	#[pallet::config]
35	pub trait Config: frame_system::Config + cumulus_pallet_parachain_system::Config {}
36
37	/// A simple storage map for testing purposes.
38	#[pallet::storage]
39	pub type TestMap<T: Config> = StorageMap<_, Twox64Concat, u32, (), ValueQuery>;
40
41	#[pallet::hooks]
42	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
43
44	#[pallet::call]
45	impl<T: Config> Pallet<T> {
46		/// A test dispatchable for setting a custom head data in `validate_block`.
47		#[pallet::weight(0)]
48		pub fn set_custom_validation_head_data(
49			_: OriginFor<T>,
50			custom_header: alloc::vec::Vec<u8>,
51		) -> DispatchResult {
52			cumulus_pallet_parachain_system::Pallet::<T>::set_custom_validation_head_data(
53				custom_header,
54			);
55			Ok(())
56		}
57
58		/// A dispatchable that first reads two values from two different child tries, asserts they
59		/// are the expected values (if the values exist in the state) and then writes two different
60		/// values to these child tries.
61		#[pallet::weight(0)]
62		pub fn read_and_write_child_tries(_: OriginFor<T>) -> DispatchResult {
63			let key = &b"hello"[..];
64			let first_trie = &b"first"[..];
65			let second_trie = &b"second"[..];
66			let first_value = "world1".encode();
67			let second_value = "world2".encode();
68
69			if let Some(res) = sp_io::default_child_storage::get(first_trie, key) {
70				assert_eq!(first_value, res);
71			}
72			if let Some(res) = sp_io::default_child_storage::get(second_trie, key) {
73				assert_eq!(second_value, res);
74			}
75
76			sp_io::default_child_storage::set(first_trie, key, &first_value);
77			sp_io::default_child_storage::set(second_trie, key, &second_value);
78
79			Ok(())
80		}
81
82		/// Reads a key and writes a big value under this key.
83		///
84		/// At genesis this `key` is empty and thus, will only be set in consequent blocks.
85		pub fn read_and_write_big_value(_: OriginFor<T>) -> DispatchResult {
86			let key = &b"really_huge_value"[..];
87			sp_io::storage::get(key);
88			sp_io::storage::set(key, &vec![0u8; 1024 * 1024 * 5]);
89
90			Ok(())
91		}
92
93		/// Stores `()` in `TestMap` for keys from 0 up to `max_key`.
94		#[pallet::weight(0)]
95		pub fn store_values_in_map(_: OriginFor<T>, max_key: u32) -> DispatchResult {
96			for i in 0..=max_key {
97				TestMap::<T>::insert(i, ());
98			}
99			Ok(())
100		}
101
102		/// Removes the value associated with `key` from `TestMap`.
103		#[pallet::weight(0)]
104		pub fn remove_value_from_map(_: OriginFor<T>, key: u32) -> DispatchResult {
105			TestMap::<T>::remove(key);
106			Ok(())
107		}
108	}
109
110	#[derive(frame_support::DefaultNoBound)]
111	#[pallet::genesis_config]
112	pub struct GenesisConfig<T: Config> {
113		#[serde(skip)]
114		pub _config: core::marker::PhantomData<T>,
115	}
116
117	#[pallet::genesis_build]
118	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
119		fn build(&self) {
120			sp_io::storage::set(TEST_RUNTIME_UPGRADE_KEY, &[1, 2, 3, 4]);
121		}
122	}
123}