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

//! Configurations.

use alloc::{collections::BTreeSet, vec::Vec};
use gear_core::{
    costs::{BlocksAmount, BytesAmount, CallsAmount, CostOf, SyscallCosts},
    pages::WasmPagesAmount,
};
use gear_lazy_pages_common::LazyPagesCosts;
use gear_wasm_instrument::syscalls::SyscallName;

/// Number of max pages number to use it in tests.
pub const TESTS_MAX_PAGES_NUMBER: u16 = 512;

/// Contextual block information.
#[derive(Clone, Copy, Debug, Default)]
pub struct BlockInfo {
    /// Height.
    pub height: u32,
    /// Timestamp.
    pub timestamp: u64,
}

/// Holding in storages rent costs.
#[derive(Debug, Default, Clone)]
pub struct RentCosts {
    /// Holding message in waitlist cost per block.
    pub waitlist: CostOf<BlocksAmount>,
    /// Holding message in dispatch stash cost per block.
    pub dispatch_stash: CostOf<BlocksAmount>,
    /// Holding reservation cost per block.
    pub reservation: CostOf<BlocksAmount>,
}

/// Execution externalities costs.
#[derive(Debug, Default, Clone)]
pub struct ExtCosts {
    /// Syscalls costs.
    pub syscalls: SyscallCosts,
    /// Rent costs.
    pub rent: RentCosts,
    /// Memory grow cost per page.
    pub mem_grow: CostOf<WasmPagesAmount>,
}

/// Costs for message processing
#[derive(Clone, Debug, Default)]
pub struct ProcessCosts {
    /// Execution externalities costs.
    pub ext: ExtCosts,
    /// Lazy pages costs.
    pub lazy_pages: LazyPagesCosts,
    /// Storage read cost.
    pub read: CostOf<CallsAmount>,
    /// Storage read per byte cost.
    pub read_per_byte: CostOf<BytesAmount>,
    /// Storage write cost.
    pub write: CostOf<CallsAmount>,
    /// Code instrumentation cost.
    pub instrumentation: CostOf<CallsAmount>,
    /// Code instrumentation per byte cost.
    pub instrumentation_per_byte: CostOf<BytesAmount>,
    /// Static page cost.
    pub static_page: CostOf<WasmPagesAmount>,
    /// WASM module instantiation per byte cost.
    pub module_instantiation_per_byte: CostOf<BytesAmount>,
}

/// Execution settings for handling messages.
pub(crate) struct ExecutionSettings {
    /// Contextual block information.
    pub block_info: BlockInfo,
    /// Performance multiplier.
    pub performance_multiplier: gsys::Percent,
    /// Execution externalities costs.
    pub ext_costs: ExtCosts,
    /// Lazy pages costs.
    pub lazy_pages_costs: LazyPagesCosts,
    /// Existential deposit.
    pub existential_deposit: u128,
    /// Mailbox threshold.
    pub mailbox_threshold: u64,
    /// Max allowed memory size.
    pub max_pages: WasmPagesAmount,
    /// Forbidden functions.
    pub forbidden_funcs: BTreeSet<SyscallName>,
    /// Reserve for parameter of scheduling.
    pub reserve_for: u32,
    /// Most recently determined random seed, along with the time in the past since when it was determinable by chain observers.
    // TODO: find a way to put a random seed inside block config.
    pub random_data: (Vec<u8>, u32),
    /// Gas multiplier.
    pub gas_multiplier: gsys::GasMultiplier,
}

/// Stable parameters for the whole block across processing runs.
#[derive(Clone)]
pub struct BlockConfig {
    /// Block info.
    pub block_info: BlockInfo,
    /// Performance multiplier.
    pub performance_multiplier: gsys::Percent,
    /// Forbidden functions.
    pub forbidden_funcs: BTreeSet<SyscallName>,
    /// Reserve for parameter of scheduling.
    pub reserve_for: u32,
    /// Gas multiplier.
    pub gas_multiplier: gsys::GasMultiplier,
    /// Program processing costs.
    pub costs: ProcessCosts,
    /// Existential deposit.
    pub existential_deposit: u128,
    /// Mailbox threshold.
    pub mailbox_threshold: u64,
    /// Amount of reservations can exist for 1 program.
    pub max_reservations: u64,
    /// Max allowed page numbers for wasm program.
    pub max_pages: WasmPagesAmount,
    /// Outgoing limit.
    pub outgoing_limit: u32,
    /// Outgoing bytes limit.
    pub outgoing_bytes_limit: u32,
}