gear_core/lib.rs
1// This file is part of Gear.
2
3// Copyright (C) 2021-2025 Gear Technologies Inc.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Gear core.
20//!
21//! This library provides a runner for dealing with multiple little programs exchanging messages in a deterministic manner.
22//! To be used primary in Gear Substrate node implementation, but it is not limited to that.
23#![no_std]
24#![warn(missing_docs)]
25#![doc(html_logo_url = "https://docs.gear.rs/logo.svg")]
26#![doc(html_favicon_url = "https://gear-tech.io/favicons/favicon.ico")]
27
28extern crate alloc;
29
30pub mod buffer;
31pub mod code;
32pub mod costs;
33pub mod env;
34pub mod env_vars;
35pub mod gas;
36pub mod gas_metering;
37pub mod ids;
38pub mod memory;
39pub mod message;
40pub mod pages;
41pub mod percent;
42pub mod program;
43pub mod reservation;
44pub mod rpc;
45pub mod str;
46pub mod tasks;
47pub mod utils {
48 //! Utility functions.
49
50 use blake2::{Blake2b, Digest, digest::typenum::U32};
51
52 /// BLAKE2b-256 hasher state.
53 type Blake2b256 = Blake2b<U32>;
54
55 /// Creates a unique identifier by passing given argument to blake2b hash-function.
56 ///
57 /// # SAFETY: DO NOT ADJUST HASH FUNCTION, BECAUSE MESSAGE ID IS SENSITIVE FOR IT.
58 pub fn hash(data: &[u8]) -> [u8; 32] {
59 let mut ctx = Blake2b256::new();
60 ctx.update(data);
61 ctx.finalize().into()
62 }
63
64 /// Creates a unique identifier by passing given argument to blake2b hash-function.
65 ///
66 /// # SAFETY: DO NOT ADJUST HASH FUNCTION, BECAUSE MESSAGE ID IS SENSITIVE FOR IT.
67 pub fn hash_of_array<T: AsRef<[u8]>, const N: usize>(array: [T; N]) -> [u8; 32] {
68 let mut ctx = Blake2b256::new();
69 for data in array {
70 ctx.update(data);
71 }
72 ctx.finalize().into()
73 }
74}
75
76// This allows all casts from u32 into usize be safe.
77const _: () = assert!(size_of::<u32>() <= size_of::<usize>());