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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Describes the various costs incurred by creating receipts.
//! We use the following abbreviation for readability:
//! * sir -- sender is receiver. Receipts that are directed by an account to itself are guaranteed
//!   to not be cross-shard which is cheaper than cross-shard. Conversely, when sender is not a
//!   receiver it might or might not be a cross-shard communication.
use serde::{Deserialize, Serialize};
pub type Gas = u64;

#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct Fraction {
    pub numerator: u64,
    pub denominator: u64,
}

/// Costs associated with an object that can only be sent over the network (and executed
/// by the receiver).
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct Fee {
    /// Fee for sending an object from the sender to itself, guaranteeing that it does not leave
    /// the shard.
    send_sir: Gas,
    /// Fee for sending an object potentially across the shards.
    send_not_sir: Gas,
    /// Fee for executing the object.
    execution: Gas,
}

impl Fee {
    pub fn send_fee(&self, sir: bool) -> Gas {
        if sir {
            self.send_sir
        } else {
            self.send_not_sir
        }
    }

    pub fn exec_fee(&self) -> Gas {
        self.execution
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct RuntimeFeesConfig {
    /// Costs for runtime externals
    pub ext_costs: ExtCostsConfig,
    /// Describes the cost of creating an action receipt, `ActionReceipt`, excluding the actual cost
    /// of actions.
    pub action_receipt_creation_config: Fee,
    /// Describes the cost of creating a data receipt, `DataReceipt`.
    pub data_receipt_creation_config: DataReceiptCreationConfig,
    /// Describes the cost of creating a certain action, `Action`. Includes all variants.
    pub action_creation_config: ActionCreationConfig,
    /// Describes fees for storage rent
    pub storage_usage_config: StorageUsageConfig,

    /// Fraction of the burnt gas to reward to the contract account for execution.
    pub burnt_gas_reward: Fraction,
}

#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct ExtCostsConfig {
    /// Pay for reading contract input base
    pub input_base: Gas,
    /// Pay for reading contract input per byte
    pub input_per_byte: Gas,
    /// Storage trie read key base cost
    pub storage_read_base: Gas,
    /// Storage trie read key per byte cost
    pub storage_read_key_byte: Gas,
    /// Storage trie read value cost per byte cost
    pub storage_read_value_byte: Gas,
    /// Storage trie write key base cost
    pub storage_write_base: Gas,
    /// Storage trie write key per byte cost
    pub storage_write_key_byte: Gas,
    /// Storage trie write value per byte cost
    pub storage_write_value_byte: Gas,
    /// Storage trie check for key existence cost base
    pub storage_has_key_base: Gas,
    /// Storage trie check for key existence per key byte
    pub storage_has_key_byte: Gas,
    /// Remove key from trie base cost
    pub storage_remove_base: Gas,
    /// Remove key from trie per byte cost
    pub storage_remove_key_byte: Gas,
    /// Remove key from trie ret value byte cost
    pub storage_remove_ret_value_byte: Gas,
    /// Create trie prefix iterator cost base
    pub storage_iter_create_prefix_base: Gas,
    /// Create trie range iterator cost base
    pub storage_iter_create_range_base: Gas,
    /// Create trie iterator per key byte cost
    pub storage_iter_create_key_byte: Gas,
    /// Trie iterator per key base cost
    pub storage_iter_next_base: Gas,
    /// Trie iterator next key byte cost
    pub storage_iter_next_key_byte: Gas,
    /// Trie iterator next key byte cost
    pub storage_iter_next_value_byte: Gas,
    /// Base cost for reading from register
    pub read_register_base: Gas,
    /// Cost for reading byte from register
    pub read_register_byte: Gas,
    /// Base cost for writing into register
    pub write_register_base: Gas,
    /// Cost for writing byte into register
    pub write_register_byte: Gas,
    /// Base cost for guest memory read
    pub read_memory_base: Gas,
    /// Cost for guest memory read
    pub read_memory_byte: Gas,
    /// Base cost for guest memory write
    pub write_memory_base: Gas,
    /// Cost for guest memory write per byte
    pub write_memory_byte: Gas,
    /// Get account balance cost
    pub account_balance: Gas,
    /// Get prepaid gas cost
    pub prepaid_gas: Gas,
    /// Get used gas cost
    pub used_gas: Gas,
    /// Cost of getting random seed
    pub random_seed_base: Gas,
    /// Cost of getting random seed per byte
    pub random_seed_per_byte: Gas,
    /// Cost of getting sha256 base
    pub sha256: Gas,
    /// Cost of getting sha256 per byte
    pub sha256_byte: Gas,
    /// Get account attached_deposit base cost
    pub attached_deposit: Gas,
    /// Get storage usage cost
    pub storage_usage: Gas,
    /// Get a current block height base cost
    pub block_index: Gas,
    /// Get a current timestamp base cost
    pub block_timestamp: Gas,
    /// Cost for getting a current account base
    pub current_account_id: Gas,
    /// Cost for getting a current account per byte
    pub current_account_id_byte: Gas,
    /// Cost for getting a signer account id base
    pub signer_account_id: Gas,
    /// Cost for getting a signer account per byte
    pub signer_account_id_byte: Gas,
    /// Cost for getting a signer public key
    pub signer_account_pk: Gas,
    /// Cost for getting a signer public key per byte
    pub signer_account_pk_byte: Gas,
    /// Cost for getting a predecessor account
    pub predecessor_account_id: Gas,
    /// Cost for getting a predecessor account per byte
    pub predecessor_account_id_byte: Gas,
    /// Cost for calling promise_and
    pub promise_and_base: Gas,
    /// Cost for calling promise_and for each promise
    pub promise_and_per_promise: Gas,
    /// Cost for calling promise_result
    pub promise_result_base: Gas,
    /// Cost for calling promise_result per result byte
    pub promise_result_byte: Gas,
    /// Cost for calling promise_results_count
    pub promise_results_count: Gas,
    /// Cost for calling promise_return
    pub promise_return: Gas,
    /// Cost for calling logging
    pub log_base: Gas,
    /// Cost for logging per byte
    pub log_per_byte: Gas,
}

/// Describes the cost of creating a data receipt, `DataReceipt`.
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct DataReceiptCreationConfig {
    /// Base cost of creating a data receipt.
    pub base_cost: Fee,
    /// Additional cost per byte sent.
    pub cost_per_byte: Fee,
}

/// Describes the cost of creating a specific action, `Action`. Includes all variants.
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct ActionCreationConfig {
    /// Base cost of creating an account.
    pub create_account_cost: Fee,

    /// Base cost of deploying a contract.
    pub deploy_contract_cost: Fee,
    /// Cost per byte of deploying a contract.
    pub deploy_contract_cost_per_byte: Fee,

    /// Base cost of calling a function.
    pub function_call_cost: Fee,
    /// Cost per byte of method name and arguments of calling a function.
    pub function_call_cost_per_byte: Fee,

    /// Base cost of making a transfer.
    pub transfer_cost: Fee,

    /// Base cost of staking.
    pub stake_cost: Fee,

    /// Base cost of adding a key.
    pub add_key_cost: AccessKeyCreationConfig,

    /// Base cost of deleting a key.
    pub delete_key_cost: Fee,

    /// Base cost of deleting an account.
    pub delete_account_cost: Fee,
}

/// Describes the cost of creating an access key.
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct AccessKeyCreationConfig {
    /// Base cost of creating a full access access-key.
    pub full_access_cost: Fee,
    /// Base cost of creating an access-key restricted to specific functions.
    pub function_call_cost: Fee,
    /// Cost per byte of method_names of creating a restricted access-key.
    pub function_call_cost_per_byte: Fee,
}

#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct StorageUsageConfig {
    /// Base storage usage for an account
    pub account_cost: Gas,
    /// Base cost for a k/v record
    pub data_record_cost: Gas,
    /// Cost per byte of key
    pub key_cost_per_byte: Gas,
    /// Cost per byte of value
    pub value_cost_per_byte: Gas,
    /// Cost per byte of contract code
    pub code_cost_per_byte: Gas,
}

impl Default for RuntimeFeesConfig {
    fn default() -> Self {
        Self {
            ext_costs: ExtCostsConfig {
                input_base: 1,
                input_per_byte: 1,
                storage_read_base: 1,
                storage_read_key_byte: 1,
                storage_read_value_byte: 1,
                storage_write_base: 1,
                storage_write_key_byte: 1,
                storage_write_value_byte: 1,
                storage_has_key_base: 1,
                storage_has_key_byte: 1,
                storage_remove_base: 1,
                storage_remove_key_byte: 1,
                storage_remove_ret_value_byte: 1,
                storage_iter_create_prefix_base: 1,
                storage_iter_create_range_base: 1,
                storage_iter_create_key_byte: 1,
                storage_iter_next_base: 1,
                storage_iter_next_key_byte: 1,
                storage_iter_next_value_byte: 1,
                read_register_base: 1,
                read_register_byte: 1,
                write_register_base: 1,
                write_register_byte: 1,
                read_memory_base: 1,
                read_memory_byte: 1,
                write_memory_base: 1,
                write_memory_byte: 1,
                account_balance: 1,
                prepaid_gas: 1,
                used_gas: 1,
                random_seed_base: 1,
                random_seed_per_byte: 1,
                sha256: 1,
                sha256_byte: 1,
                attached_deposit: 1,
                storage_usage: 1,
                block_index: 1,
                block_timestamp: 1,
                current_account_id: 1,
                current_account_id_byte: 1,
                signer_account_id: 1,
                signer_account_id_byte: 1,
                signer_account_pk: 1,
                signer_account_pk_byte: 1,
                predecessor_account_id: 1,
                predecessor_account_id_byte: 1,
                promise_and_base: 1,
                promise_and_per_promise: 1,
                promise_result_base: 1,
                promise_result_byte: 1,
                promise_results_count: 1,
                promise_return: 1,
                log_base: 1,
                log_per_byte: 1,
            },
            action_receipt_creation_config: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
            data_receipt_creation_config: DataReceiptCreationConfig {
                base_cost: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
                cost_per_byte: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
            },
            action_creation_config: ActionCreationConfig {
                create_account_cost: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
                deploy_contract_cost: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
                deploy_contract_cost_per_byte: Fee {
                    send_sir: 10,
                    send_not_sir: 10,
                    execution: 10,
                },
                function_call_cost: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
                function_call_cost_per_byte: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
                transfer_cost: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
                stake_cost: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
                add_key_cost: AccessKeyCreationConfig {
                    full_access_cost: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
                    function_call_cost: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
                    function_call_cost_per_byte: Fee {
                        send_sir: 10,
                        send_not_sir: 10,
                        execution: 10,
                    },
                },
                delete_key_cost: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
                delete_account_cost: Fee { send_sir: 10, send_not_sir: 10, execution: 10 },
            },
            storage_usage_config: StorageUsageConfig {
                account_cost: 100,
                data_record_cost: 40,
                key_cost_per_byte: 1,
                value_cost_per_byte: 1,
                code_cost_per_byte: 1,
            },
            burnt_gas_reward: Fraction { numerator: 3, denominator: 10 },
        }
    }
}

impl RuntimeFeesConfig {
    pub fn free() -> Self {
        let free = Fee { send_sir: 0, send_not_sir: 0, execution: 0 };
        RuntimeFeesConfig {
            ext_costs: ExtCostsConfig {
                input_base: 0,
                input_per_byte: 0,
                storage_read_base: 0,
                storage_read_key_byte: 0,
                storage_read_value_byte: 0,
                storage_write_base: 0,
                storage_write_key_byte: 0,
                storage_write_value_byte: 0,
                storage_has_key_base: 0,
                storage_has_key_byte: 0,
                storage_remove_base: 0,
                storage_remove_key_byte: 0,
                storage_remove_ret_value_byte: 0,
                storage_iter_create_prefix_base: 0,
                storage_iter_create_range_base: 0,
                storage_iter_create_key_byte: 0,
                storage_iter_next_base: 0,
                storage_iter_next_key_byte: 0,
                storage_iter_next_value_byte: 0,
                read_register_base: 0,
                read_register_byte: 0,
                write_register_base: 0,
                write_register_byte: 0,
                read_memory_base: 0,
                read_memory_byte: 0,
                write_memory_base: 0,
                write_memory_byte: 0,
                account_balance: 0,
                prepaid_gas: 0,
                used_gas: 0,
                random_seed_base: 0,
                random_seed_per_byte: 0,
                sha256: 0,
                sha256_byte: 0,
                attached_deposit: 0,
                storage_usage: 0,
                block_index: 0,
                block_timestamp: 0,
                current_account_id: 0,
                current_account_id_byte: 0,
                signer_account_id: 0,
                signer_account_id_byte: 0,
                signer_account_pk: 0,
                signer_account_pk_byte: 0,
                predecessor_account_id: 0,
                predecessor_account_id_byte: 0,
                promise_and_base: 0,
                promise_and_per_promise: 0,
                promise_result_base: 0,
                promise_result_byte: 0,
                promise_results_count: 0,
                promise_return: 0,
                log_base: 0,
                log_per_byte: 0,
            },
            action_receipt_creation_config: free.clone(),
            data_receipt_creation_config: DataReceiptCreationConfig {
                base_cost: free.clone(),
                cost_per_byte: free.clone(),
            },
            action_creation_config: ActionCreationConfig {
                create_account_cost: free.clone(),
                deploy_contract_cost: free.clone(),
                deploy_contract_cost_per_byte: free.clone(),
                function_call_cost: free.clone(),
                function_call_cost_per_byte: free.clone(),
                transfer_cost: free.clone(),
                stake_cost: free.clone(),
                add_key_cost: AccessKeyCreationConfig {
                    full_access_cost: free.clone(),
                    function_call_cost: free.clone(),
                    function_call_cost_per_byte: free.clone(),
                },
                delete_key_cost: free.clone(),
                delete_account_cost: free.clone(),
            },
            storage_usage_config: StorageUsageConfig {
                account_cost: 0,
                data_record_cost: 0,
                key_cost_per_byte: 0,
                value_cost_per_byte: 0,
                code_cost_per_byte: 0,
            },
            burnt_gas_reward: Fraction { numerator: 0, denominator: 1 },
        }
    }
}