referrerpolicy=no-referrer-when-downgrade

test_parachain_undying/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot 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// Polkadot 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 Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Basic parachain that adds a number as part of its state.
18
19#![no_std]
20
21extern crate alloc;
22
23use alloc::vec::Vec;
24use codec::{Decode, Encode};
25use polkadot_parachain_primitives::primitives::UpwardMessages;
26use polkadot_primitives::{
27	ClaimQueueOffset, CoreSelector, UMPSignal, DEFAULT_CLAIM_QUEUE_OFFSET, UMP_SEPARATOR,
28};
29use tiny_keccak::{Hasher as _, Keccak};
30
31#[cfg(not(feature = "std"))]
32mod wasm_validation;
33
34#[cfg(not(feature = "std"))]
35#[global_allocator]
36static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc;
37const LOG_TARGET: &str = "runtime::undying";
38
39// Make the WASM binary available.
40#[cfg(feature = "std")]
41include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
42
43fn keccak256(input: &[u8]) -> [u8; 32] {
44	let mut out = [0u8; 32];
45	let mut keccak256 = Keccak::v256();
46	keccak256.update(input);
47	keccak256.finalize(&mut out);
48	out
49}
50
51/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics.
52#[cfg(feature = "std")]
53pub fn wasm_binary_unwrap() -> &'static [u8] {
54	WASM_BINARY.expect(
55		"Development wasm binary is not available. Testing is only \
56						supported with the flag disabled.",
57	)
58}
59
60/// Head data for this parachain.
61#[derive(Default, Clone, Hash, Eq, PartialEq, Encode, Decode, Debug)]
62pub struct HeadData {
63	/// Block number
64	pub number: u64,
65	/// parent block keccak256
66	pub parent_hash: [u8; 32],
67	/// hash of post-execution state.
68	pub post_state: [u8; 32],
69}
70
71impl HeadData {
72	pub fn hash(&self) -> [u8; 32] {
73		keccak256(&self.encode())
74	}
75}
76
77/// Block data for this parachain.
78#[derive(Default, Clone, Encode, Decode, Debug)]
79pub struct GraveyardState {
80	/// The grave index of the last placed tombstone.
81	pub index: u64,
82	/// We use a matrix where each element represents a grave.
83	/// The unsigned integer tracks the number of tombstones raised on
84	/// each grave.
85	pub graveyard: Vec<u8>,
86	// TODO: Add zombies. All of the graves produce zombies at a regular interval
87	// defined in blocks. The number of zombies produced scales with the tombstones.
88	// This would allow us to have a configurable and reproducible PVF execution time.
89	// However, PVF preparation time will likely rely on prebuild wasm binaries.
90	pub zombies: u64,
91	// Grave seal.
92	pub seal: [u8; 32],
93	// Increasing sequence number for core selector.
94	pub core_selector_number: u8,
95}
96
97/// Block data for this parachain.
98#[derive(Default, Clone, Encode, Decode, Debug)]
99pub struct BlockData {
100	/// The state
101	pub state: GraveyardState,
102	/// The number of tombstones to erect per iteration. For each tombstone placed
103	/// a hash operation is performed as CPU burn.
104	pub tombstones: u64,
105	/// The number of iterations to perform.
106	pub iterations: u32,
107	/// Whether or not to emit the experimental ApprovedPeer UMP signal.
108	pub experimental_send_approved_peer: bool,
109}
110
111pub fn hash_state(state: &GraveyardState) -> [u8; 32] {
112	keccak256(state.encode().as_slice())
113}
114
115/// Executes all graveyard transactions in the block.
116pub fn execute_transaction(mut block_data: BlockData) -> GraveyardState {
117	let graveyard_size = block_data.state.graveyard.len();
118
119	for _ in 0..block_data.iterations {
120		for _ in 0..block_data.tombstones {
121			block_data.state.graveyard[block_data.state.index as usize] =
122				block_data.state.graveyard[block_data.state.index as usize].wrapping_add(1);
123
124			block_data.state.index =
125				((block_data.state.index.saturating_add(1)) as usize % graveyard_size) as u64;
126		}
127		// Chain hash the seals and burn CPU.
128		block_data.state.seal = hash_state(&block_data.state);
129	}
130	block_data.state.core_selector_number = block_data.state.core_selector_number.wrapping_add(1);
131
132	block_data.state
133}
134
135/// Start state mismatched with parent header's state hash.
136#[derive(Debug)]
137pub struct StateMismatch;
138
139/// Execute a block body on top of given parent head, producing new parent head
140/// and new state if valid.
141pub fn execute(
142	parent_hash: [u8; 32],
143	parent_head: HeadData,
144	block_data: BlockData,
145) -> Result<(HeadData, GraveyardState, UpwardMessages), StateMismatch> {
146	assert_eq!(parent_hash, parent_head.hash());
147
148	if hash_state(&block_data.state) != parent_head.post_state {
149		log::debug!(
150			target: LOG_TARGET,
151			"state has diff vs head: {:?} vs {:?}",
152			hash_state(&block_data.state),
153			parent_head.post_state,
154		);
155		return Err(StateMismatch)
156	}
157
158	let mut upward_messages: UpwardMessages = Default::default();
159	upward_messages.force_push(UMP_SEPARATOR);
160	upward_messages.force_push(
161		UMPSignal::SelectCore(
162			CoreSelector(block_data.state.core_selector_number),
163			ClaimQueueOffset(DEFAULT_CLAIM_QUEUE_OFFSET),
164		)
165		.encode(),
166	);
167
168	if block_data.experimental_send_approved_peer {
169		upward_messages
170			.force_push(UMPSignal::ApprovedPeer(alloc::vec![1, 2, 3].try_into().unwrap()).encode());
171	}
172
173	// We need to clone the block data as the fn will mutate it's state.
174	let new_state = execute_transaction(block_data.clone());
175
176	Ok((
177		HeadData {
178			number: parent_head.number + 1,
179			parent_hash,
180			post_state: hash_state(&new_state),
181		},
182		new_state,
183		upward_messages,
184	))
185}