unc-vm-runner 0.12.2

This crate implements the specification of the interface that unc blockchain exposes to the smart contracts.
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
729
730
731
732
733
734
use crate::logic::tests::helpers::*;
use crate::logic::tests::vm_logic_builder::{TestVMLogic, VMLogicBuilder};
use crate::logic::types::Gas;
use crate::logic::MemSlice;
use crate::logic::{HostError, VMLogicError};
use crate::tests::test_vm_config;
use expect_test::expect;
use unc_parameters::{ActionCosts, ExtCosts, Fee};

#[test]
fn test_dont_burn_gas_when_exceeding_attached_gas_limit() {
    let gas_limit = 10u64.pow(14);

    let mut logic_builder = VMLogicBuilder::default();
    logic_builder.config.limit_config.max_gas_burnt = gas_limit * 2;
    logic_builder.context.prepaid_gas = gas_limit;
    let mut logic = logic_builder.build();

    let index = promise_create(&mut logic, b"rick.test", 0, 0).expect("should create a promise");
    promise_batch_action_function_call(&mut logic, index, 0, gas_limit * 2)
        .expect_err("should fail with gas limit");
    let outcome = logic.compute_outcome();

    // Just avoid hard-coding super-precise amount of gas burnt.
    assert!(outcome.burnt_gas < gas_limit / 2);
    assert_eq!(outcome.used_gas, gas_limit);
}

#[test]
fn test_limit_wasm_gas_after_attaching_gas() {
    let gas_limit = 10u64.pow(14);
    let op_limit = op_limit(gas_limit);

    let mut logic_builder = VMLogicBuilder::default();
    logic_builder.config.limit_config.max_gas_burnt = gas_limit * 2;
    logic_builder.context.prepaid_gas = gas_limit;
    let mut logic = logic_builder.build();

    let index = promise_create(&mut logic, b"rick.test", 0, 0).expect("should create a promise");
    promise_batch_action_function_call(&mut logic, index, 0, gas_limit / 2)
        .expect("should add action to receipt");
    logic.gas_opcodes((op_limit / 2) as u32).expect_err("should fail with gas limit");
    let outcome = logic.compute_outcome();

    assert_eq!(outcome.used_gas, gas_limit);
    assert!(gas_limit / 2 < outcome.burnt_gas);
    assert!(outcome.burnt_gas < gas_limit);
}

#[test]
fn test_cant_burn_more_than_max_gas_burnt_gas() {
    let gas_limit = 10u64.pow(14);
    let op_limit = op_limit(gas_limit);

    let mut logic_builder = VMLogicBuilder::default();
    logic_builder.config.limit_config.max_gas_burnt = gas_limit;
    logic_builder.context.prepaid_gas = gas_limit * 2;
    let mut logic = logic_builder.build();

    logic.gas_opcodes(op_limit * 3).expect_err("should fail with gas limit");
    let outcome = logic.compute_outcome();

    assert_eq!(outcome.burnt_gas, gas_limit);
    assert_eq!(outcome.used_gas, gas_limit * 2);
}

#[test]
fn test_cant_burn_more_than_prepaid_gas() {
    let gas_limit = 10u64.pow(14);
    let op_limit = op_limit(gas_limit);

    let mut logic_builder = VMLogicBuilder::default();
    logic_builder.config.limit_config.max_gas_burnt = gas_limit * 2;
    logic_builder.context.prepaid_gas = gas_limit;
    let mut logic = logic_builder.build();

    logic.gas_opcodes(op_limit * 3).expect_err("should fail with gas limit");
    let outcome = logic.compute_outcome();

    assert_eq!(outcome.burnt_gas, gas_limit);
    assert_eq!(outcome.used_gas, gas_limit);
}

#[test]
fn test_hit_max_gas_burnt_limit() {
    let gas_limit = 10u64.pow(14);
    let op_limit = op_limit(gas_limit);

    let mut logic_builder = VMLogicBuilder::default();
    logic_builder.config.limit_config.max_gas_burnt = gas_limit;
    logic_builder.context.prepaid_gas = gas_limit * 3;
    let mut logic = logic_builder.build();

    promise_create(&mut logic, b"rick.test", 0, gas_limit / 2).expect("should create a promise");
    logic.gas_opcodes(op_limit * 2).expect_err("should fail with gas limit");
    let outcome = logic.compute_outcome();

    assert_eq!(outcome.burnt_gas, gas_limit);
    assert!(outcome.used_gas > gas_limit * 2);
}

#[test]
fn test_hit_prepaid_gas_limit() {
    let gas_limit = 10u64.pow(14);
    let op_limit = op_limit(gas_limit);

    let mut logic_builder = VMLogicBuilder::default();
    logic_builder.config.limit_config.max_gas_burnt = gas_limit * 3;
    logic_builder.context.prepaid_gas = gas_limit;
    let mut logic = logic_builder.build();

    promise_create(&mut logic, b"rick.test", 0, gas_limit / 2).expect("should create a promise");
    logic.gas_opcodes(op_limit * 2).expect_err("should fail with gas limit");
    let outcome = logic.compute_outcome();

    assert_eq!(outcome.burnt_gas, gas_limit);
    assert_eq!(outcome.used_gas, gas_limit);
}

#[test]
fn function_call_no_weight_refund() {
    let gas_limit = 10u64.pow(14);

    let mut logic_builder = VMLogicBuilder::default();
    logic_builder.config.limit_config.max_gas_burnt = gas_limit;
    logic_builder.context.prepaid_gas = gas_limit;
    let mut logic = logic_builder.build();

    let index = promise_batch_create(&mut logic, "rick.test").expect("should create a promise");
    promise_batch_action_function_call_weight(&mut logic, index, 0, 1000, 0)
        .expect("batch action function call should succeed");

    let outcome = logic.compute_outcome();

    // Verify that unused gas was not allocated to function call
    assert!(outcome.used_gas < gas_limit);
}

#[test]
fn test_overflowing_burn_gas_with_promises_gas() {
    let gas_limit = 3 * 10u64.pow(14);
    let mut logic_builder = VMLogicBuilder::default();
    logic_builder.config.limit_config.max_gas_burnt = gas_limit;
    logic_builder.context.prepaid_gas = gas_limit;
    let mut logic = logic_builder.build();

    let account_id = logic.internal_mem_write(b"rick.test");
    let args = logic.internal_mem_write(b"");
    let num_100u128 = logic.internal_mem_write(&100u128.to_le_bytes());
    let num_10u128 = logic.internal_mem_write(&10u128.to_le_bytes());

    let index = promise_batch_create(&mut logic, "rick.test").expect("should create a promise");
    logic.promise_batch_action_transfer(index, num_100u128.ptr).unwrap();
    let call_id = logic.promise_batch_then(index, account_id.len, account_id.ptr).unwrap();

    let needed_gas_charge = u64::max_value() - logic.gas_counter().used_gas() - 1;
    let function_name_len =
        needed_gas_charge / logic.config().ext_costs.gas_cost(ExtCosts::read_memory_byte);
    let result = logic.promise_batch_action_function_call(
        call_id,
        function_name_len,
        /* function_name_ptr: */ 0,
        args.len,
        args.ptr,
        num_10u128.ptr,
        10000,
    );
    assert!(matches!(
        result,
        Err(crate::logic::VMLogicError::HostError(crate::logic::HostError::GasLimitExceeded))
    ));
    assert_eq!(logic.gas_counter().used_gas(), gas_limit);
}

#[test]
fn test_overflowing_burn_gas_with_promises_gas_2() {
    let gas_limit = 3 * 10u64.pow(14);
    let mut logic_builder = VMLogicBuilder::default();
    logic_builder.config.limit_config.max_gas_burnt = gas_limit;
    logic_builder.context.prepaid_gas = gas_limit / 2;
    let mut logic = logic_builder.build();

    let account_id = logic.internal_mem_write(b"rick.test");
    let args = logic.internal_mem_write(b"");
    let num_100u128 = logic.internal_mem_write(&100u128.to_le_bytes());

    let index = promise_batch_create(&mut logic, "rick.test").expect("should create a promise");
    logic.promise_batch_action_transfer(index, num_100u128.ptr).unwrap();
    logic.promise_batch_then(index, account_id.len, account_id.ptr).unwrap();
    let minimum_prepay = logic.gas_counter().used_gas();
    logic_builder.context.prepaid_gas = minimum_prepay;
    let mut logic = logic_builder.build();
    let index = promise_batch_create(&mut logic, "rick.test").expect("should create a promise");
    logic.promise_batch_action_transfer(index, num_100u128.ptr).unwrap();
    let call_id = logic.promise_batch_then(index, account_id.len, account_id.ptr).unwrap();
    let needed_gas_charge = u64::max_value() - logic.gas_counter().used_gas() - 1;
    let function_name_len =
        needed_gas_charge / logic.config().ext_costs.gas_cost(ExtCosts::read_memory_byte);
    let result = logic.promise_batch_action_function_call(
        call_id,
        function_name_len,
        /* function_name_ptr: */ 0,
        args.len,
        args.ptr,
        10u128.to_le_bytes().as_ptr() as _,
        10000,
    );
    assert!(matches!(
        result,
        Err(crate::logic::VMLogicError::HostError(crate::logic::HostError::GasExceeded))
    ));
    assert_eq!(logic.gas_counter().used_gas(), minimum_prepay);
}

/// Check consistent result when exceeding gas limit on a specific action gas parameter.
///
/// Increases an action cost to a high value and then watch an execution run out
/// of gas. Then make sure the exact result is still the same. This prevents
/// accidental protocol changes where gas is deducted in different order.
///
/// The `exercise_action` function must be a function or closure that operates
/// on a `VMLogic` and triggers gas costs associated with the action parameter
/// under test.
///
/// `num_action_paid` specifies how often the cost is charged in
/// `exercise_action`. We aim to make it `num_action_paid` = 1 in the typical
/// case but for cots per byte this is usually a higher value.
///
/// `num_action_paid` is required to calculate by how much exactly gas prices
/// must be increased so that it will just trigger the gas limit.
#[track_caller]
fn check_action_gas_exceeds_limit(
    cost: ActionCosts,
    num_action_paid: u64,
    exercise_action: impl FnOnce(&mut TestVMLogic) -> Result<(), VMLogicError>,
) {
    // Create a logic parametrized such that it will fail with out-of-gas when specified action is deducted.
    let gas_limit = 10u64.pow(13);
    let gas_attached = gas_limit;
    let fee = Fee {
        send_sir: gas_limit / num_action_paid + 1,
        send_not_sir: gas_limit / num_action_paid + 10,
        execution: 1, // exec part is `used`, make it small
    };
    let mut logic_builder = VMLogicBuilder::default();
    logic_builder.config.limit_config.max_gas_burnt = gas_limit;
    logic_builder.fees_config.action_fees[cost] = fee;
    logic_builder.context.prepaid_gas = gas_attached;
    logic_builder.context.output_data_receivers = vec!["alice.test".parse().unwrap()];
    let mut logic = logic_builder.build();

    let result = exercise_action(&mut logic);
    assert!(result.is_err(), "expected out-of-gas error for {cost:?} but was ok");
    assert_eq!(result.unwrap_err(), VMLogicError::HostError(HostError::GasLimitExceeded));

    // When gas limit is exceeded, we always set burnt_gas := prepaid and then promise_gas := 0.
    assert_eq!(
        gas_attached,
        logic.gas_counter().burnt_gas(),
        "burnt gas should be all attached gas",
    );
    assert_eq!(
        gas_attached,
        logic.gas_counter().used_gas(),
        "used gas should be no more than burnt gas",
    );
}

/// Check consistent result when exceeding attached gas on a specific action gas
/// parameter.
///
/// Very similar to `check_action_gas_exceeds_limit` but we hit a different
/// limit and return a different error. See that comment for an explanation on
/// the arguments.
///
/// This case is more interesting because the burnt gas can be below used gas,
/// when the prepaid gas was exceeded by burnt burnt + promised gas but not by
/// burnt gas alone.
///
/// Consequently, `num_action_paid` here is even more important to calculate
/// exactly what the gas costs should be to trigger the limits.
#[track_caller]
fn check_action_gas_exceeds_attached(
    cost: ActionCosts,
    num_action_paid: u64,
    expected: expect_test::Expect,
    exercise_action: impl FnOnce(&mut TestVMLogic) -> Result<(), VMLogicError>,
) {
    // Create a logic parametrized such that it will fail with out-of-gas when specified action is deducted.
    let gas_limit = 10u64.pow(14);
    let gas_attached = 10u64.pow(13);
    let fee = Fee {
        send_sir: 1,      // make burnt gas small
        send_not_sir: 10, // make it easy to distinguish `sir` / `not_sir`
        execution: gas_attached / num_action_paid + 1,
    };
    let mut logic_builder = VMLogicBuilder::default();
    logic_builder.config.limit_config.max_gas_burnt = gas_limit;
    logic_builder.fees_config.action_fees[cost] = fee;
    logic_builder.context.prepaid_gas = gas_attached;
    logic_builder.context.output_data_receivers = vec!["alice.test".parse().unwrap()];
    let mut logic = logic_builder.build();

    let result = exercise_action(&mut logic);
    assert!(result.is_err(), "expected out-of-gas error for {cost:?} but was ok");
    assert_eq!(result.unwrap_err(), VMLogicError::HostError(HostError::GasExceeded));

    let actual = format!(
        "{} burnt {} used",
        logic.gas_counter().burnt_gas(),
        logic.gas_counter().used_gas()
    );
    expected.assert_eq(&actual);
}

// Below are a bunch of `out_of_gas_*` tests. These test that when we run out of
// gas while charging a specific action gas cost, we burn a consistent amount of
// gas. This is to prevent accidental changes in how we charge gas. It cannot
// cover all cases but it can detect things like a changed order of gas charging
// or splitting pay_gas(A+B) to pay_gas(A), pay_gas(B), which went through to
// master unnoticed before.
//
// The setup for these tests is as follows:
// - 1 test per action cost
// - each test checks for 2 types of out of gas errors, gas limit exceeded and
//   gas attached exceeded
// - common code to create a test VMLogic setup is in checker functions
//   `check_action_gas_exceeds_limit` and `check_action_gas_exceeds_attached`
//   which are called from every test
// - each action cost must be triggered in a different way, so we define a small
//   function that does something which charges the tested action cost, then we
//   give this function to the checker functions
// - if an action cost is charged through different paths, the test defines
//   multiple functions that trigger the cost and the checker functions are
//   called once for each of them
// - these action cost triggering functions are defined in the test's inner
//   scope, unless they are shared between multiple tests

/// see longer comment above for how this test works
#[test]
fn out_of_gas_new_action_receipt() {
    // two different ways to create an action receipts, first check exceeding the burnt limit
    check_action_gas_exceeds_limit(ActionCosts::new_action_receipt, 1, create_action_receipt);
    check_action_gas_exceeds_limit(ActionCosts::new_action_receipt, 2, create_promise_dependency);

    // the same again, but for prepaid gas
    check_action_gas_exceeds_attached(
        ActionCosts::new_action_receipt,
        1,
        expect!["8644846690 burnt 10000000000000 used"],
        create_action_receipt,
    );

    check_action_gas_exceeds_attached(
        ActionCosts::new_action_receipt,
        2,
        expect!["9411968532130 burnt 10000000000000 used"],
        create_promise_dependency,
    );

    /// function to trigger action receipt action cost
    fn create_action_receipt(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
        promise_batch_create(logic, "rick.test")?;
        Ok(())
    }
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_new_data_receipt() {
    check_action_gas_exceeds_limit(
        ActionCosts::new_data_receipt_base,
        1,
        create_promise_dependency,
    );

    check_action_gas_exceeds_attached(
        ActionCosts::new_data_receipt_base,
        1,
        expect!["10000000000000 burnt 10000000000000 used"],
        create_promise_dependency,
    );
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_new_data_receipt_byte() {
    check_action_gas_exceeds_limit(ActionCosts::new_data_receipt_byte, 11, value_return);

    // expect to burn it all because send + exec fees are fully paid upfront
    check_action_gas_exceeds_attached(
        ActionCosts::new_data_receipt_byte,
        11,
        expect!["10000000000000 burnt 10000000000000 used"],
        value_return,
    );

    // value return will pay for the cost of returned data dependency bytes, if there are any.
    fn value_return(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
        // 11 characters long string
        let value = logic.internal_mem_write(b"lorem ipsum");
        logic.value_return(11, value.ptr)?;
        Ok(())
    }
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_create_account() {
    check_action_gas_exceeds_limit(ActionCosts::create_account, 1, create_account);

    check_action_gas_exceeds_attached(
        ActionCosts::create_account,
        1,
        expect!["116969114801 burnt 10000000000000 used"],
        create_account,
    );

    fn create_account(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
        let account_id = "rick.test";
        let idx = promise_batch_create(logic, account_id)?;
        logic.promise_batch_action_create_account(idx)?;
        Ok(())
    }
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_delete_account() {
    check_action_gas_exceeds_limit(ActionCosts::delete_account, 1, delete_account);

    check_action_gas_exceeds_attached(
        ActionCosts::delete_account,
        1,
        expect!["125349193370 burnt 10000000000000 used"],
        delete_account,
    );

    fn delete_account(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
        let beneficiary_account_id = "alice.test";
        let deleted_account_id = "bob.test";
        let idx = promise_batch_create(logic, deleted_account_id)?;
        let beneficiary = logic.internal_mem_write(beneficiary_account_id.as_bytes());
        logic.promise_batch_action_delete_account(idx, beneficiary.len, beneficiary.ptr)?;
        Ok(())
    }
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_deploy_contract_base() {
    check_action_gas_exceeds_limit(ActionCosts::deploy_contract_base, 1, deploy_contract);

    check_action_gas_exceeds_attached(
        ActionCosts::deploy_contract_base,
        1,
        expect!["119677812659 burnt 10000000000000 used"],
        deploy_contract,
    );
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_deploy_contract_byte() {
    check_action_gas_exceeds_limit(ActionCosts::deploy_contract_byte, 26, deploy_contract);

    check_action_gas_exceeds_attached(
        ActionCosts::deploy_contract_byte,
        26,
        expect!["304443562909 burnt 10000000000000 used"],
        deploy_contract,
    );
}

/// function to trigger base + 26 bytes deployment costs (26 is arbitrary)
fn deploy_contract(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
    let account_id = "rick.test";
    let idx = promise_batch_create(logic, account_id)?;
    let code = logic.internal_mem_write(b"lorem ipsum with length 26");
    logic.promise_batch_action_deploy_contract(idx, code.len, code.ptr)?;
    Ok(())
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_function_call_base() {
    check_action_gas_exceeds_limit(ActionCosts::function_call_base, 1, cross_contract_call);
    check_action_gas_exceeds_limit(
        ActionCosts::function_call_base,
        1,
        cross_contract_call_gas_weight,
    );

    check_action_gas_exceeds_attached(
        ActionCosts::function_call_base,
        1,
        expect!["125011579049 burnt 10000000000000 used"],
        cross_contract_call,
    );
    check_action_gas_exceeds_attached(
        ActionCosts::function_call_base,
        1,
        expect!["125011579049 burnt 10000000000000 used"],
        cross_contract_call_gas_weight,
    );
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_function_call_byte() {
    check_action_gas_exceeds_limit(ActionCosts::function_call_byte, 40, cross_contract_call);
    check_action_gas_exceeds_limit(
        ActionCosts::function_call_byte,
        40,
        cross_contract_call_gas_weight,
    );

    check_action_gas_exceeds_attached(
        ActionCosts::function_call_byte,
        40,
        expect!["2444873079439 burnt 10000000000000 used"],
        cross_contract_call,
    );
    check_action_gas_exceeds_attached(
        ActionCosts::function_call_byte,
        40,
        expect!["2444873079439 burnt 10000000000000 used"],
        cross_contract_call_gas_weight,
    );
}

/// function to trigger base + 40 bytes function call action costs (40 is 26 +
/// 14 which are arbitrary)
fn cross_contract_call(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
    let account_id = "rick.test";
    let idx = promise_batch_create(logic, account_id)?;
    let arg = b"lorem ipsum with length 26";
    let name = b"fn_with_len_14";
    let attached_balance = 1u128;
    let gas = 1; // attaching very little gas so it doesn't cause gas exceeded on its own
    promise_batch_action_function_call_ext(logic, idx, name, arg, attached_balance, gas)?;
    Ok(())
}

/// same as `cross_contract_call` but splits gas remainder among outgoing calls
fn cross_contract_call_gas_weight(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
    let account_id = "rick.test";
    let idx = promise_batch_create(logic, account_id)?;
    let arg = b"lorem ipsum with length 26";
    let name = b"fn_with_len_14";
    let attached_balance = 1u128;
    let gas = 1; // attaching very little gas so it doesn't cause gas exceeded on its own
    let gas_weight = 1;
    promise_batch_action_function_call_weight_ext(
        logic,
        idx,
        name,
        arg,
        attached_balance,
        gas,
        gas_weight,
    )?;
    Ok(())
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_transfer() {
    check_action_gas_exceeds_limit(ActionCosts::transfer, 1, promise_transfer);

    check_action_gas_exceeds_attached(
        ActionCosts::transfer,
        1,
        expect!["119935181141 burnt 10000000000000 used"],
        promise_transfer,
    );

    fn promise_transfer(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
        let account_id = "alice.test";
        let idx = promise_batch_create(logic, account_id)?;
        let attached_balance = logic.internal_mem_write(&1u128.to_be_bytes());
        logic.promise_batch_action_transfer(idx, attached_balance.ptr)?;
        Ok(())
    }
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_pledge() {
    check_action_gas_exceeds_limit(ActionCosts::pledge, 1, promise_pledge);

    check_action_gas_exceeds_attached(
        ActionCosts::pledge,
        1,
        expect!["122375106518 burnt 10000000000000 used"],
        promise_pledge,
    );

    fn promise_pledge(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
        let account_id = "pool.test";
        let idx = promise_batch_create(logic, account_id)?;
        let attached_balance = logic.internal_mem_write(&1u128.to_be_bytes());
        let pk = write_test_pk(logic);
        logic.promise_batch_action_stake(idx, attached_balance.ptr, pk.len, pk.ptr)?;
        Ok(())
    }
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_add_full_access_key() {
    check_action_gas_exceeds_limit(ActionCosts::add_full_access_key, 1, promise_full_access_key);

    check_action_gas_exceeds_attached(
        ActionCosts::add_full_access_key,
        1,
        expect!["119999803802 burnt 10000000000000 used"],
        promise_full_access_key,
    );

    fn promise_full_access_key(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
        let account_id = "alice.test";
        let idx = promise_batch_create(logic, account_id)?;
        let pk = test_pk();
        let nonce = 0;
        promise_batch_action_add_key_with_full_access(logic, idx, &pk, nonce)?;
        Ok(())
    }
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_add_function_call_key_base() {
    check_action_gas_exceeds_limit(
        ActionCosts::add_function_call_key_base,
        1,
        promise_function_key,
    );

    check_action_gas_exceeds_attached(
        ActionCosts::add_function_call_key_base,
        1,
        expect!["133982421242 burnt 10000000000000 used"],
        promise_function_key,
    );
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_add_function_call_key_byte() {
    check_action_gas_exceeds_limit(
        ActionCosts::add_function_call_key_byte,
        7,
        promise_function_key,
    );

    check_action_gas_exceeds_attached(
        ActionCosts::add_function_call_key_byte,
        7,
        expect!["236200046312 burnt 10000000000000 used"],
        promise_function_key,
    );
}

/// function to trigger base + 7 bytes action costs for adding a new function
/// call access key to an account (7 is arbitrary)
fn promise_function_key(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
    let account_id = "alice.test";
    let idx = promise_batch_create(logic, account_id)?;
    let allowance = 1u128;
    let pk = test_pk();
    let nonce = 0;
    let methods = b"foo,baz";
    promise_batch_action_add_key_with_function_call(
        logic,
        idx,
        &pk,
        nonce,
        allowance,
        account_id.as_bytes(),
        methods,
    )?;
    Ok(())
}

/// see longer comment above for how this test works
#[test]
fn out_of_gas_delete_key() {
    check_action_gas_exceeds_limit(ActionCosts::delete_key, 1, promise_delete_key);

    check_action_gas_exceeds_attached(
        ActionCosts::delete_key,
        1,
        expect!["119999803802 burnt 10000000000000 used"],
        promise_delete_key,
    );

    fn promise_delete_key(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
        let account_id = "alice.test";
        let idx = promise_batch_create(logic, account_id)?;
        let pk = write_test_pk(logic);
        logic.promise_batch_action_delete_key(idx, pk.len, pk.ptr)?;
        Ok(())
    }
}

/// function to trigger action + data receipt action costs
fn create_promise_dependency(logic: &mut TestVMLogic) -> Result<(), VMLogicError> {
    let account_id = "rick.test";
    let idx = promise_batch_create(logic, account_id)?;
    let account_id = logic.internal_mem_write(account_id.as_bytes());
    logic.promise_batch_then(idx, account_id.len, account_id.ptr)?;
    Ok(())
}

/// Given the limit in gas, compute the corresponding limit in wasm ops for use
/// with [`VMLogic::gas`] function.
fn op_limit(gas_limit: Gas) -> u32 {
    (gas_limit / (test_vm_config().regular_op_cost as u64)) as u32
}

fn test_pk() -> Vec<u8> {
    let pk = borsh::to_vec(
        &"ed25519:22W5rKuvbMRphnDoCj6nfrWhRKvh9Xf9SWXfGHaeXGde"
            .parse::<unc_crypto::PublicKey>()
            .unwrap(),
    )
    .unwrap();
    pk
}

fn write_test_pk(logic: &mut TestVMLogic) -> MemSlice {
    logic.internal_mem_write(&test_pk())
}