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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use mirai_annotations::*;
use serde::{Deserialize, Serialize};
use std::{
ops::{Add, Div, Mul, Sub},
u64,
};
pub type GasCarrier = u64;
pub trait GasAlgebra<GasCarrier>: Sized
where
GasCarrier: Add<Output = GasCarrier>
+ Sub<Output = GasCarrier>
+ Div<Output = GasCarrier>
+ Mul<Output = GasCarrier>
+ Copy,
{
fn new(carrier: GasCarrier) -> Self;
fn get(&self) -> GasCarrier;
fn map<F: Fn(GasCarrier) -> GasCarrier>(self, f: F) -> Self {
Self::new(f(self.get()))
}
fn map2<F: Fn(GasCarrier, GasCarrier) -> GasCarrier>(
self,
other: impl GasAlgebra<GasCarrier>,
f: F,
) -> Self {
Self::new(f(self.get(), other.get()))
}
fn app<T, F: Fn(GasCarrier, GasCarrier) -> T>(
&self,
other: &impl GasAlgebra<GasCarrier>,
f: F,
) -> T {
f(self.get(), other.get())
}
fn unitary_cast<T: GasAlgebra<GasCarrier>>(self) -> T {
T::new(self.get())
}
fn add(self, right: impl GasAlgebra<GasCarrier>) -> Self {
self.map2(right, Add::add)
}
fn sub(self, right: impl GasAlgebra<GasCarrier>) -> Self {
self.map2(right, Sub::sub)
}
fn mul(self, right: impl GasAlgebra<GasCarrier>) -> Self {
self.map2(right, Mul::mul)
}
fn div(self, right: impl GasAlgebra<GasCarrier>) -> Self {
self.map2(right, Div::div)
}
}
macro_rules! define_gas_unit {
{
name: $name: ident,
carrier: $carrier: ty,
doc: $comment: literal
} => {
#[derive(Debug, Hash, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)]
#[doc=$comment]
pub struct $name<GasCarrier>(GasCarrier);
impl GasAlgebra<$carrier> for $name<$carrier> {
fn new(c: GasCarrier) -> Self {
Self(c)
}
fn get(&self) -> GasCarrier {
self.0
}
}
}
}
define_gas_unit! {
name: AbstractMemorySize,
carrier: GasCarrier,
doc: "A newtype wrapper that represents the (abstract) memory size that the instruction will take up."
}
define_gas_unit! {
name: GasUnits,
carrier: GasCarrier,
doc: "Units of gas as seen by clients of the Move VM."
}
define_gas_unit! {
name: InternalGasUnits,
carrier: GasCarrier,
doc: "Units of gas used within the Move VM, scaled for fine-grained accounting."
}
define_gas_unit! {
name: GasPrice,
carrier: GasCarrier,
doc: "A newtype wrapper around the gas price for each unit of gas consumed."
}
pub const ONE_GAS_UNIT: InternalGasUnits<GasCarrier> = InternalGasUnits(1);
pub const MAX_ABSTRACT_MEMORY_SIZE: AbstractMemorySize<GasCarrier> =
AbstractMemorySize(std::u64::MAX);
pub const CONST_SIZE: AbstractMemorySize<GasCarrier> = AbstractMemorySize(16);
pub const REFERENCE_SIZE: AbstractMemorySize<GasCarrier> = AbstractMemorySize(8);
pub const STRUCT_SIZE: AbstractMemorySize<GasCarrier> = AbstractMemorySize(2);
pub const DEFAULT_ACCOUNT_SIZE: AbstractMemorySize<GasCarrier> = AbstractMemorySize(800);
pub const LARGE_TRANSACTION_CUTOFF: AbstractMemorySize<GasCarrier> = AbstractMemorySize(600);
pub const MIN_EXISTS_DATA_SIZE: AbstractMemorySize<GasCarrier> = AbstractMemorySize(100);
pub const MAX_TRANSACTION_SIZE_IN_BYTES: GasCarrier = 4096;
#[derive(Clone, Debug, Serialize, PartialEq, Deserialize)]
pub struct GasConstants {
pub global_memory_per_byte_cost: InternalGasUnits<GasCarrier>,
pub global_memory_per_byte_write_cost: InternalGasUnits<GasCarrier>,
pub min_transaction_gas_units: InternalGasUnits<GasCarrier>,
pub large_transaction_cutoff: AbstractMemorySize<GasCarrier>,
pub intrinsic_gas_per_byte: InternalGasUnits<GasCarrier>,
pub maximum_number_of_gas_units: GasUnits<GasCarrier>,
pub min_price_per_gas_unit: GasPrice<GasCarrier>,
pub max_price_per_gas_unit: GasPrice<GasCarrier>,
pub max_transaction_size_in_bytes: GasCarrier,
pub gas_unit_scaling_factor: GasCarrier,
pub default_account_size: AbstractMemorySize<GasCarrier>,
}
impl GasConstants {
pub fn to_internal_units(&self, units: GasUnits<GasCarrier>) -> InternalGasUnits<GasCarrier> {
InternalGasUnits::new(units.get() * self.gas_unit_scaling_factor)
}
pub fn to_external_units(&self, units: InternalGasUnits<GasCarrier>) -> GasUnits<GasCarrier> {
GasUnits::new(units.get() / self.gas_unit_scaling_factor)
}
}
impl Default for GasConstants {
fn default() -> Self {
Self {
global_memory_per_byte_cost: InternalGasUnits(4),
global_memory_per_byte_write_cost: InternalGasUnits(9),
min_transaction_gas_units: InternalGasUnits(600),
large_transaction_cutoff: LARGE_TRANSACTION_CUTOFF,
intrinsic_gas_per_byte: InternalGasUnits(8),
maximum_number_of_gas_units: GasUnits(4_000_000),
min_price_per_gas_unit: GasPrice(0),
max_price_per_gas_unit: GasPrice(10_000),
max_transaction_size_in_bytes: MAX_TRANSACTION_SIZE_IN_BYTES,
gas_unit_scaling_factor: 1000,
default_account_size: DEFAULT_ACCOUNT_SIZE,
}
}
}
#[derive(Clone, Debug, Serialize, PartialEq, Deserialize)]
pub struct CostTable {
pub instruction_table: Vec<GasCost>,
pub native_table: Vec<GasCost>,
pub gas_constants: GasConstants,
}
impl CostTable {
#[inline]
pub fn instruction_cost(&self, instr_index: u8) -> &GasCost {
precondition!(instr_index > 0 && instr_index <= (self.instruction_table.len() as u8));
&self.instruction_table[(instr_index - 1) as usize]
}
#[inline]
pub fn native_cost(&self, native_index: u8) -> &GasCost {
precondition!(native_index < (self.native_table.len() as u8));
&self.native_table[native_index as usize]
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct GasCost {
pub instruction_gas: InternalGasUnits<GasCarrier>,
pub memory_gas: InternalGasUnits<GasCarrier>,
}
impl GasCost {
pub fn new(instr_gas: GasCarrier, mem_gas: GasCarrier) -> Self {
Self {
instruction_gas: InternalGasUnits::new(instr_gas),
memory_gas: InternalGasUnits::new(mem_gas),
}
}
#[inline]
pub fn total(&self) -> InternalGasUnits<GasCarrier> {
self.instruction_gas.add(self.memory_gas)
}
}