gear_core/ids.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
// This file is part of Gear.
// Copyright (C) 2022-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Base identifiers for messaging primitives.
use blake2::{digest::typenum::U32, Blake2b, Digest};
pub use gprimitives::{ActorId, CodeId, MessageId, ReservationId};
/// BLAKE2b-256 hasher state.
type Blake2b256 = Blake2b<U32>;
/// Creates a unique identifier by passing given argument to blake2b hash-function.
///
/// # SAFETY: DO NOT ADJUST HASH FUNCTION, BECAUSE MESSAGE ID IS SENSITIVE FOR IT.
pub fn hash(data: &[u8]) -> [u8; 32] {
let mut ctx = Blake2b256::new();
ctx.update(data);
ctx.finalize().into()
}
/// Creates a unique identifier by passing given argument to blake2b hash-function.
///
/// # SAFETY: DO NOT ADJUST HASH FUNCTION, BECAUSE MESSAGE ID IS SENSITIVE FOR IT.
pub fn hash_of_array<T: AsRef<[u8]>, const N: usize>(array: [T; N]) -> [u8; 32] {
let mut ctx = Blake2b256::new();
for data in array {
ctx.update(data);
}
ctx.finalize().into()
}
/// Program identifier.
pub type ProgramId = ActorId;
pub mod prelude {
//! The purpose of this module is to make it easier to import `gprimitives` extensions.
use super::*;
mod private {
use super::*;
pub trait Sealed {}
impl Sealed for ActorId {}
impl Sealed for CodeId {}
impl Sealed for MessageId {}
impl Sealed for ReservationId {}
}
/// Program (actor) identifier extension.
pub trait ActorIdExt: private::Sealed {
/// System program identifier.
const SYSTEM: Self;
/// Generates `ActorId` from given `CodeId` and `salt`.
fn generate_from_user(code_id: CodeId, salt: &[u8]) -> Self;
/// Generates `ActorId` from given `MessageId`, `CodeId` and `salt`.
fn generate_from_program(message_id: MessageId, code_id: CodeId, salt: &[u8]) -> Self;
}
impl ActorIdExt for ActorId {
const SYSTEM: Self = Self::new(*b"geargeargeargeargeargeargeargear");
fn generate_from_user(code_id: CodeId, salt: &[u8]) -> Self {
const SALT: &[u8] = b"program_from_user";
hash_of_array([SALT, code_id.as_ref(), salt]).into()
}
fn generate_from_program(message_id: MessageId, code_id: CodeId, salt: &[u8]) -> Self {
const SALT: &[u8] = b"program_from_wasm";
hash_of_array([SALT, message_id.as_ref(), code_id.as_ref(), salt]).into()
}
}
/// Message identifier extension.
pub trait MessageIdExt: private::Sealed {
/// Generates `MessageId` for non-program outgoing message.
fn generate_from_user(
block_number: u32,
user_id: ProgramId,
local_nonce: u128,
) -> MessageId;
/// Generates `MessageId` for program outgoing message.
fn generate_outgoing(origin_msg_id: MessageId, local_nonce: u32) -> MessageId;
/// Generates `MessageId` for reply message depend on status code.
///
/// # SAFETY: DO NOT ADJUST REPLY MESSAGE ID GENERATION,
/// BECAUSE AUTO-REPLY LOGIC DEPENDS ON PRE-DEFINED REPLY ID.
fn generate_reply(origin_msg_id: MessageId) -> MessageId;
/// Generates `MessageId` for signal message depend on status code.
fn generate_signal(origin_msg_id: MessageId) -> MessageId;
}
impl MessageIdExt for MessageId {
fn generate_from_user(
block_number: u32,
user_id: ProgramId,
local_nonce: u128,
) -> MessageId {
const SALT: &[u8] = b"external";
hash_of_array([
SALT,
&block_number.to_le_bytes(),
user_id.as_ref(),
&local_nonce.to_le_bytes(),
])
.into()
}
fn generate_outgoing(origin_msg_id: MessageId, local_nonce: u32) -> MessageId {
const SALT: &[u8] = b"outgoing";
hash_of_array([SALT, origin_msg_id.as_ref(), &local_nonce.to_le_bytes()]).into()
}
fn generate_reply(origin_msg_id: MessageId) -> MessageId {
const SALT: &[u8] = b"reply";
hash_of_array([SALT, origin_msg_id.as_ref()]).into()
}
fn generate_signal(origin_msg_id: MessageId) -> MessageId {
const SALT: &[u8] = b"signal";
hash_of_array([SALT, origin_msg_id.as_ref()]).into()
}
}
/// Code identifier extension.
pub trait CodeIdExt: private::Sealed {
/// Generates `CodeId` from given code.
fn generate(code: &[u8]) -> Self;
}
impl CodeIdExt for CodeId {
fn generate(code: &[u8]) -> Self {
hash(code).into()
}
}
/// Reservation identifier extension.
pub trait ReservationIdExt: private::Sealed {
/// Generates `ReservationId` from given message and nonce.
fn generate(msg_id: MessageId, nonce: u64) -> Self;
}
impl ReservationIdExt for ReservationId {
fn generate(msg_id: MessageId, nonce: u64) -> Self {
const SALT: &[u8] = b"reservation";
hash_of_array([SALT, msg_id.as_ref(), &nonce.to_le_bytes()]).into()
}
}
}