Skip to main content

gear_core/
lib.rs

1// Copyright (C) Gear Technologies Inc.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4//! Gear core.
5//!
6//! This library provides a runner for dealing with multiple little programs exchanging messages in a deterministic manner.
7//! To be used primary in Gear Substrate node implementation, but it is not limited to that.
8
9#![no_std]
10#![warn(missing_docs)]
11#![doc(html_logo_url = "https://gear-tech.io/logo.png")]
12#![doc(html_favicon_url = "https://gear-tech.io/favicon.ico")]
13#![cfg_attr(docsrs, feature(doc_cfg))]
14
15extern crate alloc;
16
17pub mod buffer;
18pub mod code;
19pub mod costs;
20pub mod env;
21pub mod env_vars;
22pub mod gas;
23pub mod gas_metering;
24pub mod ids;
25pub mod limited;
26pub mod memory;
27pub mod message;
28pub mod pages;
29pub mod percent;
30pub mod program;
31pub mod reservation;
32pub mod rpc;
33pub mod tasks;
34pub mod utils {
35    //! Utility functions.
36
37    use blake2::{Blake2b, Digest, digest::typenum::U32};
38
39    /// BLAKE2b-256 hasher state.
40    type Blake2b256 = Blake2b<U32>;
41
42    /// Creates a unique identifier by passing given argument to blake2b hash-function.
43    ///
44    /// # SAFETY: DO NOT ADJUST HASH FUNCTION, BECAUSE MESSAGE ID IS SENSITIVE FOR IT.
45    pub fn hash(data: &[u8]) -> [u8; 32] {
46        let mut ctx = Blake2b256::new();
47        ctx.update(data);
48        ctx.finalize().into()
49    }
50
51    /// Creates a unique identifier by passing given argument to blake2b hash-function.
52    ///
53    /// # SAFETY: DO NOT ADJUST HASH FUNCTION, BECAUSE MESSAGE ID IS SENSITIVE FOR IT.
54    pub fn hash_of_array<T: AsRef<[u8]>, const N: usize>(array: [T; N]) -> [u8; 32] {
55        let mut ctx = Blake2b256::new();
56        for data in array {
57            ctx.update(data);
58        }
59        ctx.finalize().into()
60    }
61}
62
63// This allows all casts from u32 into usize be safe.
64const _: () = assert!(size_of::<u32>() <= size_of::<usize>());