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
24#![no_std]
25#![warn(missing_docs)]
26#![doc(html_logo_url = "https://gear-tech.io/logo.png")]
27#![doc(html_favicon_url = "https://gear-tech.io/favicon.ico")]
28#![cfg_attr(docsrs, feature(doc_auto_cfg))]
29
30extern crate alloc;
31
32pub mod buffer;
33pub mod code;
34pub mod costs;
35pub mod env;
36pub mod env_vars;
37pub mod gas;
38pub mod gas_metering;
39pub mod ids;
40pub mod memory;
41pub mod message;
42pub mod pages;
43pub mod percent;
44pub mod program;
45pub mod reservation;
46pub mod rpc;
47pub mod str;
48pub mod tasks;
49pub mod utils {
50 //! Utility functions.
51
52 use blake2::{Blake2b, Digest, digest::typenum::U32};
53
54 /// BLAKE2b-256 hasher state.
55 type Blake2b256 = Blake2b<U32>;
56
57 /// Creates a unique identifier by passing given argument to blake2b hash-function.
58 ///
59 /// # SAFETY: DO NOT ADJUST HASH FUNCTION, BECAUSE MESSAGE ID IS SENSITIVE FOR IT.
60 pub fn hash(data: &[u8]) -> [u8; 32] {
61 let mut ctx = Blake2b256::new();
62 ctx.update(data);
63 ctx.finalize().into()
64 }
65
66 /// Creates a unique identifier by passing given argument to blake2b hash-function.
67 ///
68 /// # SAFETY: DO NOT ADJUST HASH FUNCTION, BECAUSE MESSAGE ID IS SENSITIVE FOR IT.
69 pub fn hash_of_array<T: AsRef<[u8]>, const N: usize>(array: [T; N]) -> [u8; 32] {
70 let mut ctx = Blake2b256::new();
71 for data in array {
72 ctx.update(data);
73 }
74 ctx.finalize().into()
75 }
76}
77
78// This allows all casts from u32 into usize be safe.
79const _: () = assert!(size_of::<u32>() <= size_of::<usize>());