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
// 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/>.

use super::*;
use crate::storage::MapStorage;
use gear_core::code::{CodeAndId, InstrumentedCode, InstrumentedCodeAndId};

#[derive(Clone, Copy, Debug)]
pub enum Error {
    /// Code already exists in storage.
    DuplicateItem,
}

/// Trait to work with program binary codes in a storage.
pub trait CodeStorage {
    type InstrumentedCodeStorage: MapStorage<Key = CodeId, Value = InstrumentedCode>;
    type InstrumentedLenStorage: MapStorage<Key = CodeId, Value = u32>;
    type OriginalCodeStorage: MapStorage<Key = CodeId, Value = Vec<u8>>;
    type MetadataStorage: MapStorage<Key = CodeId, Value = CodeMetadata>;

    /// Attempt to remove all items from all the associated maps.
    fn reset() {
        Self::MetadataStorage::clear();
        Self::OriginalCodeStorage::clear();
        Self::InstrumentedLenStorage::clear();
        Self::InstrumentedCodeStorage::clear();
    }

    fn add_code(code_and_id: CodeAndId, metadata: CodeMetadata) -> Result<(), Error> {
        let (code, code_id) = code_and_id.into_parts();
        let (code, original_code) = code.into_parts();

        Self::InstrumentedCodeStorage::mutate(code_id, |maybe| {
            if maybe.is_some() {
                return Err(CodeStorageError::DuplicateItem);
            }

            Self::InstrumentedLenStorage::insert(code_id, code.code().len() as u32);
            Self::OriginalCodeStorage::insert(code_id, original_code);
            Self::MetadataStorage::insert(code_id, metadata);

            *maybe = Some(code);
            Ok(())
        })
    }

    /// Update the corresponding code in the storage.
    fn update_code(code_and_id: InstrumentedCodeAndId) {
        let (code, code_id) = code_and_id.into_parts();

        Self::InstrumentedLenStorage::insert(code_id, code.code().len() as u32);
        Self::InstrumentedCodeStorage::insert(code_id, code);
    }

    fn exists(code_id: CodeId) -> bool {
        Self::InstrumentedCodeStorage::contains_key(&code_id)
    }

    /// Returns true if the code associated with given id was removed.
    ///
    /// If there is no code for the given id then false is returned.
    fn remove_code(code_id: CodeId) -> bool {
        Self::InstrumentedCodeStorage::mutate(code_id, |maybe| {
            if maybe.is_none() {
                return false;
            }

            Self::InstrumentedLenStorage::remove(code_id);
            Self::OriginalCodeStorage::remove(code_id);
            Self::MetadataStorage::remove(code_id);

            *maybe = None;
            true
        })
    }

    fn get_code(code_id: CodeId) -> Option<InstrumentedCode> {
        Self::InstrumentedCodeStorage::get(&code_id)
    }

    fn get_code_len(code_id: CodeId) -> Option<u32> {
        Self::InstrumentedLenStorage::get(&code_id)
    }

    fn get_original_code(code_id: CodeId) -> Option<Vec<u8>> {
        Self::OriginalCodeStorage::get(&code_id)
    }

    fn get_metadata(code_id: CodeId) -> Option<CodeMetadata> {
        Self::MetadataStorage::get(&code_id)
    }
}