test_parachain_adder/
lib.rs1#![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#[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#[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#[derive(Default, Clone, Hash, Eq, PartialEq, Encode, Decode, Debug)]
56pub struct HeadData {
57 pub number: u64,
59 pub parent_hash: [u8; 32],
61 pub post_state: [u8; 32],
63}
64
65impl HeadData {
66 pub fn hash(&self) -> [u8; 32] {
67 keccak256(&self.encode())
68 }
69}
70
71#[derive(Default, Clone, Encode, Decode, Debug)]
73pub struct BlockData {
74 pub state: u64,
76 pub add: u64,
78}
79
80pub fn hash_state(state: u64) -> [u8; 32] {
81 keccak256(state.encode().as_slice())
82}
83
84#[derive(Debug)]
86pub struct StateMismatch;
87
88pub 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}