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
170
171
172
173
174
// This file is part of Gear.
//
// Copyright (C) 2021-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/>.

//! Storage interfaces
use crate::{
    config::GearConfig,
    metadata::{
        runtime_types::{
            frame_system::pallet::Call,
            gear_common::{ActiveProgram, Program},
            gear_core::code::instrumented::InstrumentedCode,
            pallet_gear_bank::pallet::BankAccount,
        },
        storage::{GearBankStorage, GearGasStorage, GearProgramStorage},
        vara_runtime::RuntimeCall,
    },
    signer::Inner,
    utils::storage_address_bytes,
    Api, BlockNumber, Error, GearGasNode, GearGasNodeId, GearPages, Result,
};
use gear_core::{
    ids::*,
    memory::{PageBuf, PageBufInner},
};
use hex::ToHex;
use parity_scale_codec::Encode;
use sp_runtime::AccountId32;
use std::sync::Arc;
use subxt::{
    blocks::ExtrinsicEvents, dynamic::Value, metadata::EncodeWithMetadata, storage::StorageAddress,
    utils::Static,
};

type EventsResult = Result<ExtrinsicEvents<GearConfig>, Error>;

/// Implementation of storage calls for [`Signer`].
#[derive(Clone)]
pub struct SignerStorage(pub(crate) Arc<Inner>);

// pallet-system
impl SignerStorage {
    /// Sets storage values via calling sudo pallet
    pub async fn set_storage(&self, items: &[(impl StorageAddress, impl Encode)]) -> EventsResult {
        let metadata = self.0.api().metadata();
        let mut items_to_set = Vec::with_capacity(items.len());
        for item in items {
            let item_key = storage_address_bytes(&item.0, &metadata)?;
            let mut item_value_bytes = Vec::new();
            let item_value_type_id = crate::storage::storage_type_id(&metadata, &item.0)?;
            Static(&item.1).encode_with_metadata(
                item_value_type_id,
                &metadata,
                &mut item_value_bytes,
            )?;
            items_to_set.push((item_key, item_value_bytes));
        }

        self.0
            .sudo(RuntimeCall::System(Call::set_storage {
                items: items_to_set,
            }))
            .await
    }
}

// pallet-gas
impl SignerStorage {
    /// Writes gas total issuance into storage.
    pub async fn set_total_issuance(&self, value: u64) -> EventsResult {
        let addr = Api::storage_root(GearGasStorage::TotalIssuance);
        self.set_storage(&[(addr, value)]).await
    }

    /// Writes Gear gas nodes into storage at their ids.
    pub async fn set_gas_nodes(
        &self,
        gas_nodes: &impl AsRef<[(GearGasNodeId, GearGasNode)]>,
    ) -> EventsResult {
        let gas_nodes = gas_nodes.as_ref();
        let mut gas_nodes_to_set = Vec::with_capacity(gas_nodes.len());
        for gas_node in gas_nodes {
            let addr = Api::storage(GearGasStorage::GasNodes, vec![Static(gas_node.0)]);
            gas_nodes_to_set.push((addr, &gas_node.1));
        }
        self.set_storage(&gas_nodes_to_set).await
    }
}

// pallet-gear-bank
impl SignerStorage {
    /// Writes given BankAccount info into storage at `AccountId32`.
    pub async fn set_bank_account_storage(
        &self,
        dest: impl Into<AccountId32>,
        value: BankAccount<u128>,
    ) -> EventsResult {
        let addr = Api::storage(GearBankStorage::Bank, vec![Value::from_bytes(dest.into())]);
        self.set_storage(&[(addr, value)]).await
    }
}

// pallet-gear-program
impl SignerStorage {
    /// Writes `InstrumentedCode` length into storage at `CodeId`
    pub async fn set_code_len_storage(&self, code_id: CodeId, code_len: u32) -> EventsResult {
        let addr = Api::storage(
            GearProgramStorage::CodeLenStorage,
            vec![Value::from_bytes(code_id)],
        );
        self.set_storage(&[(addr, code_len)]).await
    }

    /// Writes `InstrumentedCode` into storage at `CodeId`
    pub async fn set_code_storage(&self, code_id: CodeId, code: &InstrumentedCode) -> EventsResult {
        let addr = Api::storage(
            GearProgramStorage::CodeStorage,
            vec![Value::from_bytes(code_id)],
        );
        self.set_storage(&[(addr, code)]).await
    }

    /// Writes `GearPages` into storage at `program_id`
    pub async fn set_gpages(
        &self,
        program_id: ProgramId,
        memory_infix: u32,
        program_pages: &GearPages,
    ) -> EventsResult {
        let mut program_pages_to_set = Vec::with_capacity(program_pages.len());
        for program_page in program_pages {
            let addr = Api::storage(
                GearProgramStorage::MemoryPages,
                vec![
                    subxt::dynamic::Value::from_bytes(program_id),
                    subxt::dynamic::Value::u128(memory_infix as u128),
                    subxt::dynamic::Value::u128(*program_page.0 as u128),
                ],
            );
            let page_buf_inner = PageBufInner::try_from(program_page.1.clone())
                .map_err(|_| Error::PageInvalid(*program_page.0, program_id.encode_hex()))?;
            let value = PageBuf::from_inner(page_buf_inner);
            program_pages_to_set.push((addr, value));
        }
        self.set_storage(&program_pages_to_set).await
    }

    /// Writes `ActiveProgram` into storage at `program_id`
    pub async fn set_gprog(
        &self,
        program_id: ProgramId,
        program: ActiveProgram<BlockNumber>,
    ) -> EventsResult {
        let addr = Api::storage(
            GearProgramStorage::ProgramStorage,
            vec![Value::from_bytes(program_id)],
        );
        self.set_storage(&[(addr, &Program::Active(program))]).await
    }
}