radix-engine 1.3.1

Reference implementation of Radix Engine, from the Radix DLT project.
Documentation
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
use crate::internal_prelude::*;
use crate::kernel::kernel_callback_api::{
    CheckReferenceEvent, CloseSubstateEvent, CreateNodeEvent, DrainSubstatesEvent, DropNodeEvent,
    MoveModuleEvent, OpenSubstateEvent, ReadSubstateEvent, RemoveSubstateEvent, ScanKeysEvent,
    ScanSortedSubstatesEvent, SetSubstateEvent, WriteSubstateEvent,
};
use crate::kernel::substate_io::SubstateDevice;
use crate::system::actor::Actor;
use crate::system::system_callback::SystemVersion;
use crate::system::system_modules::transaction_runtime::Event;
use crate::{
    blueprints::package::*,
    track::interface::{IOAccess, StoreCommit},
};
use lazy_static::lazy_static;

// Reference EC2 instance c5.4xlarge has CPU clock 3.4 GHz which means in 1 µs it executes 3400 instructions
// (1 core, single-threaded operation, skipping CPU cache influence).
// Basing on above assumptions converting CPU instructions count to cost units requires divistion CPU instructions
// by 3400 and multiplication by 100 (1 µs = 100 cost units), so it is enough to divide by 34.
const CPU_INSTRUCTIONS_TO_COST_UNIT: u32 = 34;

lazy_static! {
    pub static ref NATIVE_FUNCTION_BASE_COSTS: IndexMap<PackageAddress, IndexMap<&'static str, u32>> = {
        let mut costs: IndexMap<PackageAddress, IndexMap<&'static str, u32>> = index_map_new();
        include_str!("../../../../assets/native_function_base_costs.csv")
            .split("\n")
            .filter(|x| !x.is_empty())
            .for_each(|x| {
                let mut tokens = x.split(",");
                let package_address =
                    PackageAddress::try_from_hex(tokens.next().unwrap().trim()).unwrap();
                let export_name = tokens.next().unwrap().trim();
                let cost = u32::from_str(tokens.next().unwrap().trim()).unwrap();
                costs
                    .entry(package_address)
                    .or_default()
                    .insert(export_name, cost);
            });
        costs
    };
    pub static ref NATIVE_FUNCTION_BASE_COSTS_SIZE_DEPENDENT: IndexMap<PackageAddress, IndexMap<&'static str, (u32, u32)>> = {
        let mut costs: IndexMap<PackageAddress, IndexMap<&'static str, (u32, u32)>> =
            index_map_new();
        costs
            .entry(PACKAGE_PACKAGE)
            .or_default()
            .insert(PACKAGE_PUBLISH_NATIVE_IDENT, (875, 11477486));
        costs
            .entry(PACKAGE_PACKAGE)
            .or_default()
            // TODO: publish_wasm_advanced is too expensive, dividing by 6 to let large package (1MiB) to be published, consider using cubic approximation
            .insert(PACKAGE_PUBLISH_WASM_ADVANCED_IDENT, (9063 / 6, 11072798));
    costs
    };
}

/// Fee table specifies how each costing entry should be costed.
///
/// ## High Level Guideline
/// - Max execution cost unit limit: 100,000,000
/// - Max execution cost unit limit: 50,000,000
/// - Transaction fee = Network Execution + Network Finalization + Tip + Network Storage + Royalties
/// - Execution time for 100,000,000 cost units' worth of computation: <= 1 second
/// - Baseline: 1 microsecond = 100 cost units
///
#[derive(Debug, Clone, ScryptoSbor)]
pub struct FeeTable {
    wasm_execution_units_divider: u32,
}

impl FeeTable {
    pub fn new(version: SystemVersion) -> Self {
        let wasm_execution_units_divider = if version <= SystemVersion::V1 {
            // From `costing::spin_loop`, it takes 5.5391 ms for 1918122691 wasm execution units.
            // Therefore, cost for single unit: 5.5391 *  1000 / 1918122691 * 100 = 0.00028877714
            // 1 / 0.00028877714 = 3462 rounded down gives 3000
            3000
        } else {
            // W - WASM execution units
            // C - cost units
            // c - single cost unit
            // T - execution time (1 µs = 100 c => 1 ms = 100,000 c)
            //
            // Cost units might be expressed as
            //  C = T * c
            //
            // To convert W to C, we need a d.
            //   C = W / divider
            //   divider = W / C
            //   divider = W / (T * c)
            //
            // From `costing::spin_loop_v2` it consumes W=438,729,340,586 wasm execution
            // units and it should never take more than T = 1s.
            //   T = 1s = 1000 ms = 1 * 100,000
            //   W = 438,729,340,586
            // Therefore
            //   divider = 438,729,340,586 / (1000 * 100,000) = 4387.293 ~= 4500
            //
            // With divider set to 4500 it takes 543 ms (measured at GH benchmark, git rev c591c4003a,
            // EC2 instance type c6a.4xlarge) which is fine.
            4500
        };

        Self {
            wasm_execution_units_divider,
        }
    }

    pub fn latest() -> Self {
        Self::cuttlefish()
    }

    pub fn cuttlefish() -> Self {
        Self::new(SystemVersion::V2)
    }

    pub fn bottlenose() -> Self {
        Self::new(SystemVersion::V1)
    }

    pub fn anemone() -> Self {
        Self::new(SystemVersion::V1)
    }

    pub fn babylon() -> Self {
        Self::new(SystemVersion::V1)
    }

    //======================
    // Execution costs
    //======================

    fn data_processing_cost(size: usize) -> u32 {
        // Based on benchmark `bench_decode_sbor`
        // Time for processing a byte: 10.244 µs / 1068 = 0.00959176029

        // Based on benchmark `bench_validate_sbor_payload`
        // Time for processing a byte: 10.075 µs / 1169 = 0.00861847733

        mul(cast(size), 2)
    }

    fn io_access_cost(&self, io_access: &IOAccess) -> u32 {
        match io_access {
            IOAccess::ReadFromDb(_, size) => {
                // Execution time (µs): 0.0009622109 * size + 389.5155
                // Execution cost: (0.0009622109 * size + 389.5155) * 100 = 0.1 * size + 40,000
                // See: https://radixdlt.atlassian.net/wiki/spaces/S/pages/3091562563/RocksDB+metrics
                add(cast(*size) / 10, 40_000)
            }
            IOAccess::ReadFromDbNotFound(_) => {
                // Execution time (µs): varies, using max 1,600
                // Execution cost: 1,600 * 100
                // See: https://radixdlt.atlassian.net/wiki/spaces/S/pages/3091562563/RocksDB+metrics
                160_000
            }
            IOAccess::HeapSubstateUpdated { .. } | IOAccess::TrackSubstateUpdated { .. } => {
                // Heap/track substate total size is limited by limits module.
                0
            }
        }
    }

    #[inline]
    pub fn verify_tx_signatures_cost(&self, n: usize) -> u32 {
        // Based on benchmark `bench_validate_secp256k1`
        // The cost for validating a single signature is: 67.522 µs * 100 units/µs = 7,000 cost units
        mul(cast(n), 7_000)
    }

    #[inline]
    pub fn validate_tx_payload_cost(&self, size: usize) -> u32 {
        // Rational:
        // Transaction payload is propagated over a P2P network.
        // Larger size may slows down the network performance.
        // The size of a typical transfer transaction is 400 bytes, and the cost will be 400 * 40 = 16,000 cost units
        // The max size of a transaction is 1 MiB, and the cost will be 1,048,576 * 40 = 41,943,040 cost units
        // This is roughly 1/24 of storing data in substate store per current setup.
        mul(cast(size), 40)
    }

    #[inline]
    pub fn check_reference(&self, event: &CheckReferenceEvent) -> u32 {
        match event {
            CheckReferenceEvent::IOAccess(io_access) => self.io_access_cost(io_access),
        }
    }

    #[inline]
    pub fn check_intent_validity(&self) -> u32 {
        // Equivalent to an `IOAccess::ReadFromDbNotFound`
        160000
    }

    #[inline]
    pub fn check_timestamp(&self) -> u32 {
        // Equivalent to an `IOAccess::ReadFromDb`
        40_000
    }

    #[inline]
    pub fn run_native_code_cost(
        &self,
        package_address: &PackageAddress,
        export_name: &str,
        input_size: &usize,
    ) -> u32 {
        let native_execution_units = NATIVE_FUNCTION_BASE_COSTS
            .get(package_address)
            .and_then(|x| x.get(export_name).cloned())
            .unwrap_or_else(|| {
                NATIVE_FUNCTION_BASE_COSTS_SIZE_DEPENDENT
                    .get(package_address)
                    .and_then(|x| x.get(export_name))
                    .map(|value| add(value.1, mul(value.0, cast(*input_size))))
                    .unwrap_or_else(|| {
                        panic!(
                            "Native function not found: {:?}::{}. ",
                            package_address, export_name
                        )
                    })
            });

        native_execution_units / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn run_wasm_code_cost(
        &self,
        _package_address: &PackageAddress,
        _export_name: &str,
        wasm_execution_units: u32,
    ) -> u32 {
        wasm_execution_units / self.wasm_execution_units_divider
    }

    #[inline]
    pub fn instantiate_wasm_code_cost(&self, size: usize) -> u32 {
        // From `costing::instantiate_radiswap`, it takes 3.3271 ms to instantiate WASM of length 288406.
        // Therefore, cost for byte: 3.3271 *  1000 / 203950 * 100 = 1.63133120863

        mul(cast(size), 2)
    }

    #[inline]
    pub fn before_invoke_cost(&self, _actor: &Actor, input_size: usize) -> u32 {
        Self::data_processing_cost(input_size)
    }

    #[inline]
    pub fn after_invoke_cost(&self, input_size: usize) -> u32 {
        Self::data_processing_cost(input_size)
    }

    #[inline]
    pub fn allocate_node_id_cost(&self) -> u32 {
        3312 / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn create_node_cost(&self, event: &CreateNodeEvent) -> u32 {
        match event {
            CreateNodeEvent::Start(_node_id, node_substates) => {
                let total_substate_size = node_substates
                    .values()
                    .map(|x| x.values().map(|x| x.len()).sum::<usize>())
                    .sum::<usize>();
                add(
                    15510 / CPU_INSTRUCTIONS_TO_COST_UNIT,
                    Self::data_processing_cost(total_substate_size),
                )
            }
            CreateNodeEvent::IOAccess(io_access) => self.io_access_cost(io_access),
            CreateNodeEvent::End(..) => 0,
        }
    }

    #[inline]
    pub fn pin_node_cost(&self, _node_id: &NodeId) -> u32 {
        424 / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn drop_node_cost(&self, event: &DropNodeEvent) -> u32 {
        match event {
            DropNodeEvent::Start(..) => 0,
            DropNodeEvent::IOAccess(io_access) => self.io_access_cost(io_access),
            DropNodeEvent::End(_node_id, node_substates) => {
                let total_substate_size = node_substates
                    .values()
                    .map(|x| x.values().map(|x| x.len()).sum::<usize>())
                    .sum::<usize>();
                add(
                    38883 / CPU_INSTRUCTIONS_TO_COST_UNIT,
                    Self::data_processing_cost(total_substate_size),
                )
            }
        }
    }

    #[inline]
    pub fn move_module_cost(&self, event: &MoveModuleEvent) -> u32 {
        match event {
            MoveModuleEvent::IOAccess(io_access) => add(
                4791 / CPU_INSTRUCTIONS_TO_COST_UNIT,
                self.io_access_cost(io_access),
            ),
        }
    }

    #[inline]
    pub fn open_substate_cost(&self, event: &OpenSubstateEvent) -> u32 {
        match event {
            OpenSubstateEvent::Start { .. } => 0,
            OpenSubstateEvent::IOAccess(io_access) => self.io_access_cost(io_access),
            OpenSubstateEvent::End { size, .. } => add(
                10318 / CPU_INSTRUCTIONS_TO_COST_UNIT,
                Self::data_processing_cost(*size),
            ),
        }
    }

    #[inline]
    pub fn read_substate_cost(&self, event: &ReadSubstateEvent) -> u32 {
        match event {
            ReadSubstateEvent::OnRead { value, device, .. } => {
                let base_cost: u32 = match device {
                    SubstateDevice::Heap => 2234,
                    SubstateDevice::Store => 3868,
                };

                add(
                    base_cost / CPU_INSTRUCTIONS_TO_COST_UNIT,
                    Self::data_processing_cost(value.len()),
                )
            }
            ReadSubstateEvent::IOAccess(io_access) => self.io_access_cost(io_access),
        }
    }

    #[inline]
    pub fn write_substate_cost(&self, event: &WriteSubstateEvent) -> u32 {
        match event {
            WriteSubstateEvent::IOAccess(io_access) => self.io_access_cost(io_access),
            WriteSubstateEvent::Start { value, .. } => add(
                7441 / CPU_INSTRUCTIONS_TO_COST_UNIT,
                Self::data_processing_cost(value.len()),
            ),
        }
    }

    #[inline]
    pub fn close_substate_cost(&self, event: &CloseSubstateEvent) -> u32 {
        match event {
            CloseSubstateEvent::Start(..) => 4390 / CPU_INSTRUCTIONS_TO_COST_UNIT,
        }
    }

    #[inline]
    pub fn set_substate_cost(&self, event: &SetSubstateEvent) -> u32 {
        match event {
            SetSubstateEvent::Start(.., value) => add(
                4530 / CPU_INSTRUCTIONS_TO_COST_UNIT,
                Self::data_processing_cost(value.len()),
            ),
            SetSubstateEvent::IOAccess(io_access) => self.io_access_cost(io_access),
        }
    }

    #[inline]
    pub fn remove_substate_cost(&self, event: &RemoveSubstateEvent) -> u32 {
        match event {
            RemoveSubstateEvent::Start(..) => 24389 / CPU_INSTRUCTIONS_TO_COST_UNIT,
            RemoveSubstateEvent::IOAccess(io_access) => self.io_access_cost(io_access),
        }
    }

    #[inline]
    pub fn mark_substate_as_transient_cost(
        &self,
        _node_id: &NodeId,
        _partition_number: &PartitionNumber,
        _substate_key: &SubstateKey,
    ) -> u32 {
        1896 / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn scan_keys_cost(&self, event: &ScanKeysEvent) -> u32 {
        match event {
            ScanKeysEvent::Start => 16938 / CPU_INSTRUCTIONS_TO_COST_UNIT,
            ScanKeysEvent::IOAccess(io_access) => self.io_access_cost(io_access),
        }
    }

    #[inline]
    pub fn drain_substates_cost(&self, event: &DrainSubstatesEvent) -> u32 {
        match event {
            DrainSubstatesEvent::Start(count) => {
                let cpu_instructions = add(9262, mul(9286, *count));
                cpu_instructions / CPU_INSTRUCTIONS_TO_COST_UNIT
            }
            DrainSubstatesEvent::IOAccess(io_access) => self.io_access_cost(io_access),
        }
    }

    #[inline]
    pub fn scan_sorted_substates_cost(&self, event: &ScanSortedSubstatesEvent) -> u32 {
        match event {
            ScanSortedSubstatesEvent::Start => 6369 / CPU_INSTRUCTIONS_TO_COST_UNIT,
            ScanSortedSubstatesEvent::IOAccess(io_access) => self.io_access_cost(io_access),
        }
    }

    #[inline]
    pub fn get_stack_id(&self) -> u32 {
        500
    }

    #[inline]
    pub fn get_owned_nodes(&self) -> u32 {
        500
    }

    #[inline]
    pub fn switch_stack(&self) -> u32 {
        500
    }

    #[inline]
    pub fn send_to_stack(&self, data_len: usize) -> u32 {
        500 + Self::data_processing_cost(data_len)
    }

    #[inline]
    pub fn set_call_frame_data(&self, data_len: usize) -> u32 {
        500 + Self::data_processing_cost(data_len)
    }

    #[inline]
    pub fn lock_fee_cost(&self) -> u32 {
        500
    }

    #[inline]
    pub fn query_fee_reserve_cost(&self) -> u32 {
        500
    }

    #[inline]
    pub fn query_costing_module(&self) -> u32 {
        500
    }

    #[inline]
    pub fn query_actor_cost(&self) -> u32 {
        500
    }

    #[inline]
    pub fn query_transaction_hash_cost(&self) -> u32 {
        500
    }

    #[inline]
    pub fn generate_ruid_cost(&self) -> u32 {
        500
    }

    #[inline]
    pub fn emit_event_cost(&self, size: usize) -> u32 {
        500 + Self::data_processing_cost(size)
    }

    #[inline]
    pub fn emit_log_cost(&self, size: usize) -> u32 {
        500 + Self::data_processing_cost(size)
    }

    #[inline]
    pub fn encode_bech32_address_cost(&self) -> u32 {
        500
    }

    #[inline]
    pub fn panic_cost(&self, size: usize) -> u32 {
        500 + Self::data_processing_cost(size)
    }

    #[inline]
    pub fn bls12381_v1_verify_cost(&self, size: usize) -> u32 {
        // Based on  `test_crypto_scrypto_verify_bls12381_v1_costing`
        // - For sizes less than 1024, instruction count remains the same.
        // - For greater sizes following linear equation might be applied:
        //   (used: https://www.socscistatistics.com/tests/regression/default.aspx)
        //   instructions_cnt = 35.83223 * size + 15563087.39
        //   Lets round:
        //    35.83223       -> 36
        //    15563087.39    -> 15650000 (increased slightly to get the positive difference between
        //             calculated and measured number of instructions)
        let size = if size < 1024 { 1024 } else { cast(size) };
        let instructions_cnt = add(mul(size, 36), 15650000);
        // Convert to cost units
        instructions_cnt / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn bls12381_v1_aggregate_verify_cost(&self, sizes: &[usize]) -> u32 {
        // Observed that aggregated verify might be broken down into:
        // - steps depending on message size
        //   - aggregation of pairings of each corresponding key and message pair
        //   - commit each above aggregation
        // - steps that do not depend on message size
        //   - read signature from bytes: 281125 instructions
        //   - signature validation: 583573 instructions
        //   - aggregated pairing of signature to verify and initialization point: 3027639 instructions
        //   - final verification: 4280077 instructions
        //
        // more details and data in https://docs.google.com/spreadsheets/d/1rV0KyB7UQrg2tOenbh2MQ1fo9MXwrwPFW4l0_CVO-6o/edit?usp=sharing

        // Pairing aggregate
        // Following linerar equation might be applied:
        //   (used: https://www.socscistatistics.com/tests/regression/default.aspx)
        //   instructions_cnt = 34.42199 * size + 2620295.64271
        //   Lets round:
        //    34.42199      -> 35
        //    2620295.64271 -> 2620296
        //
        // Also observed that additional 16850000 instructions are performed
        // every multiple of 8
        let mut instructions_cnt = 0u32;

        for s in sizes {
            instructions_cnt = add(add(instructions_cnt, mul(35, cast(*s))), 2620296);
        }
        let multiplier = cast(sizes.len() / 8);
        instructions_cnt = add(instructions_cnt, mul(multiplier, 16850000));

        // Pairing commit
        // Observed that number commit instructions repeats every multiple of 8
        instructions_cnt = add(
            instructions_cnt,
            match sizes.len() % 8 {
                0 => 0,
                1 => 3051556,
                2 => 5020768,
                3 => 6990111,
                4 => 8959454,
                5 => 10928798,
                6 => 12898141,
                7 => 14867484,
                _ => unreachable!(),
            },
        );

        // Instructions that do not depend on size
        instructions_cnt = add(instructions_cnt, 281125 + 583573 + 3027639 + 4280077);

        // Observed that threaded takes ~1.21 more instructions than no threaded
        instructions_cnt = mul(instructions_cnt / 100, 121);
        // Convert to cost units
        instructions_cnt / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn bls12381_v1_fast_aggregate_verify_cost(&self, size: usize, keys_cnt: usize) -> u32 {
        // Based on  `test_crypto_scrypto_bls12381_v1_fast_aggregate_verify_costing`
        // - For sizes less than 1024, instruction count remains the same.
        // - For greater sizes following linear equation might be applied:
        //   instructions_cnt = 35.008 * size + 626055.4801 * keys_cnt + 15125588.5419
        //   (used: https://www.socscistatistics.com/tests/multipleregression/default.aspx)
        //   Lets round:
        //    35.008        -> 36
        //    626055.4801   -> 626056
        //    15125588.5419 -> 15200000  (increased slightly to get the positive difference between
        //             calculated and measured number of instructions)
        let size = if size < 1024 { 1024 } else { cast(size) };
        let instructions_cnt = add(add(mul(size, 36), mul(cast(keys_cnt), 626056)), 15200000);
        // Convert to cost units
        instructions_cnt / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn bls12381_g2_signature_aggregate_cost(&self, signatures_cnt: usize) -> u32 {
        // Based on  `test_crypto_scrypto_bls12381_g2_signature_aggregate_costing`
        // Following linear equation might be applied:
        //   instructions_cnt = 879553.91557 * signatures_cnt - 567872.58948
        //   (used: https://www.socscistatistics.com/tests/regression/default.aspx)
        //   Lets round:
        //    879553.91557 -> 879554
        //    567872.5895  -> 500000 (decreased to get more accurate difference between calculated
        //           and measured instructions)
        let instructions_cnt = sub(mul(cast(signatures_cnt), 879554), 500000);
        // Convert to cost units
        instructions_cnt / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn keccak256_hash_cost(&self, size: usize) -> u32 {
        // Based on  `test_crypto_scrypto_keccak256_costing`
        // - For sizes less than 100, instruction count remains the same.
        // - For greater sizes following linear equation might be applied:
        //   instructions_cnt = 46.41919 * size + 2641.66077
        //   (used: https://www.socscistatistics.com/tests/regression/default.aspx)
        //   Lets round:
        //     46.41919  -> 47
        //     2641.66077 -> 2642
        let size = if size < 100 { 100 } else { cast(size) };
        let instructions_cnt = add(mul(size, 47), 2642);
        // Convert to cost units
        instructions_cnt / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn blake2b256_hash_cost(&self, size: usize) -> u32 {
        // Based on  `test_crypto_scrypto_blake2b_256_costing`
        // - For sizes less than 100, instruction count remains the same.
        // - For greater sizes following linear equation might be applied:
        //   instructions_cnt = 14.79642 * size + 1111.02264
        //   (used: https://www.socscistatistics.com/tests/regression/default.aspx)
        //   Lets round:
        //     14.79642  -> 15
        //     1111.02264 -> 1600 (increased to get more accurate difference between calculated
        //          and measured instruction)
        let size = if size < 100 { 100 } else { cast(size) };
        let instructions_cnt = add(mul(size, 15), 1600);
        // Convert to cost units
        instructions_cnt / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn ed25519_verify_cost(&self, size: usize) -> u32 {
        // Based on  `test_crypto_scrypto_verify_ed25519_costing`
        //   instructions_cnt = 33.08798 * size + 444420.94242
        //   (used: https://www.socscistatistics.com/tests/regression/default.aspx)
        //   Lets round:
        //     33.08798 -> 34
        //     444420.94242 -> 500000 (increased slightly make sure we get the positive difference between
        //             calculated and measured number of instructions)
        let instructions_cnt = add(mul(cast(size), 34), 500000);
        // Convert to cost units
        instructions_cnt / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn secp256k1_ecdsa_verify_cost(&self) -> u32 {
        // Based on  `test_crypto_scrypto_verify_secp256k1_ecdsa_costing`
        //   instructions_cnt = 464236 (input is always 32 bytes long)
        //   Lets round:
        //     464236 -> 500000
        let instructions_cnt = 500000;
        // Convert to cost units
        instructions_cnt / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    #[inline]
    pub fn secp256k1_ecdsa_verify_and_key_recover_cost(&self) -> u32 {
        // Based on  `test_crypto_scrypto_key_recover_secp256k1_ecdsa`
        //   instructions_cnt = 464236 (input is always 32 bytes long)
        //   Lets round:
        //     463506 -> 500000
        let instructions_cnt = 500000;
        // Convert to cost units
        instructions_cnt / CPU_INSTRUCTIONS_TO_COST_UNIT
    }

    //======================
    // Finalization costs
    // This is primarily to account for the additional work on the Node side
    //======================

    #[inline]
    pub fn commit_state_updates_cost(&self, store_commit: &StoreCommit) -> u32 {
        // Committing state time (µs): 0.0025 * size + 1000
        // Finalization cost: (0.0025 * size + 1000) * 100 = 0.25 * size + 100,000
        // See: https://radixdlt.atlassian.net/wiki/spaces/S/pages/3091562563/RocksDB+metrics
        match store_commit {
            StoreCommit::Insert { size, .. } => add(cast(*size) / 4, 100_000),
            StoreCommit::Update { size, .. } => add(cast(*size) / 4, 100_000),
            StoreCommit::Delete { .. } => 100_000,
        }
    }

    #[inline]
    pub fn commit_events_cost(&self, events: &Vec<Event>) -> u32 {
        let mut sum = 0;
        for event in events {
            sum += add(cast(event.payload.len()) / 4, 5_000)
        }
        sum
    }

    #[inline]
    pub fn commit_logs_cost(&self, logs: &Vec<(Level, String)>) -> u32 {
        let mut sum = 0;
        for log in logs {
            sum += add(cast(log.1.len()) / 4, 1_000)
        }
        sum
    }

    #[inline]
    pub fn commit_intent_status(&self, num_of_intent_statuses: usize) -> u32 {
        // Equivalent to a substate insertion
        mul(cast(num_of_intent_statuses), 100_000)
    }
}

#[inline]
fn cast(a: usize) -> u32 {
    u32::try_from(a).unwrap_or(u32::MAX)
}

#[inline]
fn add(a: u32, b: u32) -> u32 {
    a.saturating_add(b)
}

#[inline]
fn sub(a: u32, b: u32) -> u32 {
    a.checked_sub(b).unwrap_or(u32::MAX)
}

#[inline]
fn mul(a: u32, b: u32) -> u32 {
    a.saturating_mul(b)
}