gear_common/
code_storage.rs

1// This file is part of Gear.
2
3// Copyright (C) 2022-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
19use super::*;
20use crate::storage::MapStorage;
21use gear_core::code::{CodeAndId, InstrumentedCode, InstrumentedCodeAndId};
22
23#[derive(Clone, Copy, Debug)]
24pub enum Error {
25    /// Code already exists in storage.
26    DuplicateItem,
27}
28
29/// Trait to work with program binary codes in a storage.
30pub trait CodeStorage {
31    type InstrumentedCodeStorage: MapStorage<Key = CodeId, Value = InstrumentedCode>;
32    type InstrumentedLenStorage: MapStorage<Key = CodeId, Value = u32>;
33    type OriginalCodeStorage: MapStorage<Key = CodeId, Value = Vec<u8>>;
34    type MetadataStorage: MapStorage<Key = CodeId, Value = CodeMetadata>;
35
36    /// Attempt to remove all items from all the associated maps.
37    fn reset() {
38        Self::MetadataStorage::clear();
39        Self::OriginalCodeStorage::clear();
40        Self::InstrumentedLenStorage::clear();
41        Self::InstrumentedCodeStorage::clear();
42    }
43
44    fn add_code(code_and_id: CodeAndId, metadata: CodeMetadata) -> Result<(), Error> {
45        let (code, code_id) = code_and_id.into_parts();
46        let (code, original_code) = code.into_parts();
47
48        Self::InstrumentedCodeStorage::mutate(code_id, |maybe| {
49            if maybe.is_some() {
50                return Err(CodeStorageError::DuplicateItem);
51            }
52
53            Self::InstrumentedLenStorage::insert(code_id, code.code().len() as u32);
54            Self::OriginalCodeStorage::insert(code_id, original_code);
55            Self::MetadataStorage::insert(code_id, metadata);
56
57            *maybe = Some(code);
58            Ok(())
59        })
60    }
61
62    /// Update the corresponding code in the storage.
63    fn update_code(code_and_id: InstrumentedCodeAndId) {
64        let (code, code_id) = code_and_id.into_parts();
65
66        Self::InstrumentedLenStorage::insert(code_id, code.code().len() as u32);
67        Self::InstrumentedCodeStorage::insert(code_id, code);
68    }
69
70    fn exists(code_id: CodeId) -> bool {
71        Self::InstrumentedCodeStorage::contains_key(&code_id)
72    }
73
74    /// Returns true if the code associated with given id was removed.
75    ///
76    /// If there is no code for the given id then false is returned.
77    fn remove_code(code_id: CodeId) -> bool {
78        Self::InstrumentedCodeStorage::mutate(code_id, |maybe| {
79            if maybe.is_none() {
80                return false;
81            }
82
83            Self::InstrumentedLenStorage::remove(code_id);
84            Self::OriginalCodeStorage::remove(code_id);
85            Self::MetadataStorage::remove(code_id);
86
87            *maybe = None;
88            true
89        })
90    }
91
92    fn get_code(code_id: CodeId) -> Option<InstrumentedCode> {
93        Self::InstrumentedCodeStorage::get(&code_id)
94    }
95
96    fn get_code_len(code_id: CodeId) -> Option<u32> {
97        Self::InstrumentedLenStorage::get(&code_id)
98    }
99
100    fn get_original_code(code_id: CodeId) -> Option<Vec<u8>> {
101        Self::OriginalCodeStorage::get(&code_id)
102    }
103
104    fn get_metadata(code_id: CodeId) -> Option<CodeMetadata> {
105        Self::MetadataStorage::get(&code_id)
106    }
107}