referrerpolicy=no-referrer-when-downgrade

test_parachain_adder/
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 codec::{Decode, Encode};
24use tiny_keccak::{Hasher as _, Keccak};
25
26#[cfg(not(feature = "std"))]
27mod wasm_validation;
28
29#[cfg(not(feature = "std"))]
30#[global_allocator]
31static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc;
32
33// Make the WASM binary available.
34#[cfg(feature = "std")]
35include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
36
37fn keccak256(input: &[u8]) -> [u8; 32] {
38	let mut out = [0u8; 32];
39	let mut keccak256 = Keccak::v256();
40	keccak256.update(input);
41	keccak256.finalize(&mut out);
42	out
43}
44
45/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics.
46#[cfg(feature = "std")]
47pub fn wasm_binary_unwrap() -> &'static [u8] {
48	WASM_BINARY.expect(
49		"Development wasm binary is not available. Testing is only \
50						supported with the flag disabled.",
51	)
52}
53
54/// Head data for this parachain.
55#[derive(Default, Clone, Hash, Eq, PartialEq, Encode, Decode, Debug)]
56pub struct HeadData {
57	/// Block number
58	pub number: u64,
59	/// parent block keccak256
60	pub parent_hash: [u8; 32],
61	/// hash of post-execution state.
62	pub post_state: [u8; 32],
63}
64
65impl HeadData {
66	pub fn hash(&self) -> [u8; 32] {
67		keccak256(&self.encode())
68	}
69}
70
71/// Block data for this parachain.
72#[derive(Default, Clone, Encode, Decode, Debug)]
73pub struct BlockData {
74	/// State to begin from.
75	pub state: u64,
76	/// Amount to add (wrapping)
77	pub add: u64,
78}
79
80pub fn hash_state(state: u64) -> [u8; 32] {
81	keccak256(state.encode().as_slice())
82}
83
84/// Start state mismatched with parent header's state hash.
85#[derive(Debug)]
86pub struct StateMismatch;
87
88/// Execute a block body on top of given parent head, producing new parent head
89/// if valid.
90pub fn execute(
91	parent_hash: [u8; 32],
92	parent_head: HeadData,
93	block_data: &BlockData,
94) -> Result<HeadData, StateMismatch> {
95	assert_eq!(parent_hash, parent_head.hash());
96
97	if hash_state(block_data.state) != parent_head.post_state {
98		return Err(StateMismatch)
99	}
100
101	let new_state = block_data.state.wrapping_add(block_data.add);
102
103	Ok(HeadData { number: parent_head.number + 1, parent_hash, post_state: hash_state(new_state) })
104}