fuel-vm 0.66.3

FuelVM interpreter.
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
use alloc::{
    borrow::ToOwned,
    vec,
    vec::Vec,
};
use consensus_parameters::gas::GasCostsValuesV5;

use crate::{
    checked_transaction::EstimatePredicates,
    consts::*,
    interpreter::{
        InterpreterParams,
        NotSupportedEcal,
    },
    storage::{
        UploadedBytecode,
        predicate::EmptyStorage,
    },
};
use fuel_asm::{
    GMArgs,
    GTFArgs,
    RegId,
    op,
};
use fuel_crypto::Hasher;
use fuel_tx::{
    ConsensusParameters,
    Finalizable,
    Receipt,
    Script,
    TransactionBuilder,
    field::{
        Inputs,
        Outputs,
        ReceiptsRoot,
        Script as ScriptField,
        Witnesses,
    },
    policies::PoliciesBits,
};
use fuel_types::{
    BlockHeight,
    ChainId,
    bytes,
    canonical::Serialize,
};
use rand::{
    Rng,
    SeedableRng,
    rngs::StdRng,
};

use crate::prelude::{
    GasCosts,
    *,
};

/// Allocates a byte array from heap and initializes it. Then points `reg` to it.
fn alloc_bytearray<const S: usize>(reg: u8, v: [u8; S]) -> Vec<Instruction> {
    let mut ops = vec![op::movi(reg, S as u32), op::aloc(reg)];
    for (i, b) in v.iter().enumerate() {
        if *b != 0 {
            ops.push(op::movi(reg, *b as u32));
            ops.push(op::sb(RegId::HP, reg, i as u16));
        }
    }
    ops.push(op::move_(reg, RegId::HP));
    ops
}

#[test]
fn metadata() {
    let rng = &mut StdRng::seed_from_u64(2322u64);

    let mut storage = MemoryStorage::default();

    let gas_price = 0;
    let gas_limit = 1_000_000;
    let maturity = Default::default();
    let height = Default::default();

    let consensus_params = ConsensusParameters::standard();

    #[rustfmt::skip]
    let routine_metadata_is_caller_external = vec![
        op::gm_args(0x10, GMArgs::IsCallerExternal),
        op::gm_args(0x11, GMArgs::GetCaller),
        op::log(0x10, 0x00, 0x00, 0x00),
        op::movi(0x20, ContractId::LEN as Immediate18),
        op::logd(0x00, 0x00, 0x11, 0x20),
        op::ret(RegId::ONE),
    ];

    let salt: Salt = rng.r#gen();
    let program: Witness = routine_metadata_is_caller_external
        .into_iter()
        .collect::<Vec<u8>>()
        .into();

    let contract_root = Contract::root_from_code(program.as_ref());
    let state_root = Contract::default_state_root();
    let contract_metadata = Contract::id(&salt, &contract_root, &state_root);

    let tx = TransactionBuilder::create(program, salt, vec![])
        .maturity(maturity)
        .add_fee_input()
        .add_contract_created()
        .finalize()
        .into_checked(height, &consensus_params)
        .expect("failed to check tx");

    let interpreter_params = InterpreterParams::new(gas_price, &consensus_params);

    // Deploy the contract into the blockchain
    assert!(
        Transactor::<_, _, _>::new(
            MemoryInstance::new(),
            &mut storage,
            interpreter_params.clone()
        )
        .transact(tx)
        .is_success()
    );

    let mut routine_call_metadata_contract = vec![
        op::gm_args(0x10, GMArgs::IsCallerExternal),
        op::log(0x10, 0x00, 0x00, 0x00),
        op::movi(0x10, (Bytes32::LEN + 2 * Bytes8::LEN) as Immediate18),
        op::aloc(0x10),
        op::move_(0x10, RegId::HP),
    ];

    contract_metadata
        .as_ref()
        .iter()
        .enumerate()
        .for_each(|(i, b)| {
            routine_call_metadata_contract.push(op::movi(0x11, *b as Immediate18));
            routine_call_metadata_contract.push(op::sb(0x10, 0x11, i as Immediate12));
        });

    routine_call_metadata_contract.push(op::call(0x10, RegId::ZERO, 0x10, RegId::CGAS));
    routine_call_metadata_contract.push(op::ret(RegId::ONE));

    let salt: Salt = rng.r#gen();
    let program: Witness = routine_call_metadata_contract
        .into_iter()
        .collect::<Vec<u8>>()
        .into();

    let contract_root = Contract::root_from_code(program.as_ref());
    let state_root = Contract::default_state_root();
    let contract_id = Contract::id(&salt, &contract_root, &state_root);
    let tx = TransactionBuilder::create(program, salt, vec![])
        .maturity(maturity)
        .add_fee_input()
        .add_contract_created()
        .finalize()
        .into_checked(height, &consensus_params)
        .expect("failed to check tx");

    assert!(
        Transactor::<_, _, _>::new(
            MemoryInstance::new(),
            &mut storage,
            interpreter_params.clone()
        )
        .transact(tx)
        .is_success()
    );

    let mut inputs = vec![];
    let mut outputs = vec![];

    inputs.push(Input::contract(
        rng.r#gen(),
        rng.r#gen(),
        rng.r#gen(),
        rng.r#gen(),
        contract_id,
    ));
    outputs.push(Output::contract(0, rng.r#gen(), rng.r#gen()));

    inputs.push(Input::contract(
        rng.r#gen(),
        rng.r#gen(),
        rng.r#gen(),
        rng.r#gen(),
        contract_metadata,
    ));
    outputs.push(Output::contract(1, rng.r#gen(), rng.r#gen()));

    let mut script = vec![
        op::movi(0x10, (1 + Bytes32::LEN + 2 * Bytes8::LEN) as Immediate18),
        op::aloc(0x10),
        op::move_(0x10, RegId::HP),
    ];

    contract_id.as_ref().iter().enumerate().for_each(|(i, b)| {
        script.push(op::movi(0x11, *b as Immediate18));
        script.push(op::sb(0x10, 0x11, i as Immediate12));
    });

    script.push(op::call(0x10, RegId::ZERO, 0x10, RegId::CGAS));
    script.push(op::ret(RegId::ONE));

    #[allow(clippy::iter_cloned_collect)]
    // collection is also perfomring a type conversion
    let script = script.iter().copied().collect::<Vec<u8>>();

    let tx = TransactionBuilder::script(script, vec![])
        .script_gas_limit(gas_limit)
        .maturity(maturity)
        .add_input(inputs[0].clone())
        .add_input(inputs[1].clone())
        .add_output(outputs[0])
        .add_output(outputs[1])
        .add_fee_input()
        .finalize()
        .into_checked(height, &consensus_params)
        .expect("failed to check tx");

    let receipts = Transactor::<_, _, _>::new(
        MemoryInstance::new(),
        &mut storage,
        interpreter_params,
    )
    .transact(tx)
    .receipts()
    .expect("Failed to transact")
    .to_owned();

    let ra = receipts[1]
        .ra()
        .expect("IsCallerExternal should set $rA as boolean flag");
    assert_eq!(1, ra);

    let ra = receipts[3]
        .ra()
        .expect("IsCallerExternal should set $rA as boolean flag");
    assert_eq!(0, ra);

    let contract_call = Hasher::hash(contract_id.as_ref());
    let digest = receipts[4]
        .digest()
        .expect("GetCaller should return contract Id");
    assert_eq!(&contract_call, digest);
}

#[test]
fn get_metadata_chain_id() {
    let rng = &mut StdRng::seed_from_u64(2322u64);
    let gas_limit = 1_000_000;
    let height = BlockHeight::default();

    let chain_id: ChainId = rng.r#gen();

    let interpreter_params = InterpreterParams {
        chain_id,
        ..Default::default()
    };

    let mut client = MemoryClient::<_, NotSupportedEcal>::new(
        MemoryInstance::new(),
        Default::default(),
        interpreter_params,
    );

    #[rustfmt::skip]
    let get_chain_id = vec![
        op::gm_args(0x10, GMArgs::GetChainId),
        op::ret(0x10),
    ];

    let consensus_params = ConsensusParameters::standard_with_id(chain_id);

    let script = TransactionBuilder::script(get_chain_id.into_iter().collect(), vec![])
        .script_gas_limit(gas_limit)
        .with_chain_id(chain_id)
        .add_fee_input()
        .finalize()
        .into_checked(height, &consensus_params)
        .unwrap();

    let receipts = client.transact(script);

    if let Receipt::Return { val, .. } = receipts[0].clone() {
        assert_eq!(val, *chain_id);
    } else {
        panic!("expected return receipt, instead of {:?}", receipts[0])
    }
}

#[test]
fn get_metadata_base_asset_id() {
    let gas_limit = 1_000_000;
    let height = BlockHeight::default();
    let mut storage = MemoryStorage::default();

    let mut params = ConsensusParameters::standard();
    params.set_base_asset_id(AssetId::from([5; 32]));

    let script = TransactionBuilder::script(
        vec![
            op::gm_args(0x20, GMArgs::BaseAssetId),
            op::movi(0x21, AssetId::LEN.try_into().unwrap()),
            op::logd(RegId::ZERO, RegId::ZERO, 0x20, 0x21),
            op::ret(RegId::ONE),
        ]
        .into_iter()
        .collect(),
        vec![],
    )
    .script_gas_limit(gas_limit)
    .add_fee_input()
    .finalize()
    .into_checked(height, &params)
    .unwrap();

    let receipts = Transactor::<_, _, _>::new(
        MemoryInstance::new(),
        &mut storage,
        InterpreterParams {
            base_asset_id: *params.base_asset_id(),
            ..Default::default()
        },
    )
    .transact(script)
    .receipts()
    .expect("Failed to transact")
    .to_owned();

    if let Receipt::LogData { data, .. } = receipts[0].clone() {
        assert_eq!(data.unwrap(), params.base_asset_id().to_bytes().into());
    } else {
        panic!("expected LogData receipt, instead of {:?}", receipts[0]);
    }
}

#[test]
fn get_metadata_tx_start() {
    let gas_limit = 1_000_000;
    let height = BlockHeight::default();
    let mut storage = MemoryStorage::default();

    let script = TransactionBuilder::script(
        vec![op::gm_args(0x20, GMArgs::TxStart), op::ret(0x20)]
            .into_iter()
            .collect(),
        vec![],
    )
    .script_gas_limit(gas_limit)
    .add_fee_input()
    .finalize()
    .into_checked(height, &ConsensusParameters::default())
    .unwrap();

    let receipts = Transactor::<_, _, _>::new(
        MemoryInstance::new(),
        &mut storage,
        InterpreterParams::default(),
    )
    .transact(script)
    .receipts()
    .expect("Failed to transact")
    .to_owned();

    if let Receipt::Return { val, .. } = receipts[0].clone() {
        assert_eq!(val, TxParameters::DEFAULT.tx_offset() as Word);
    } else {
        panic!("expected return receipt, instead of {:?}", receipts[0])
    }
}

#[test]
fn get_metadata__gas_price__script() {
    // given
    let gas_limit = 1_000_000;
    let height = BlockHeight::default();
    let mut storage = MemoryStorage::default();

    let gas_price = 123;

    let script = TransactionBuilder::script(
        vec![op::gm_args(0x20, GMArgs::GetGasPrice), op::ret(0x20)]
            .into_iter()
            .collect(),
        vec![],
    )
    .script_gas_limit(gas_limit)
    .add_fee_input()
    .add_max_fee_limit(10)
    .finalize()
    .into_checked(height, &ConsensusParameters::default())
    .unwrap();

    let interpreter_params = InterpreterParams {
        gas_price,
        ..Default::default()
    };

    // when
    let receipts = Transactor::<_, _, _>::new(
        MemoryInstance::new(),
        &mut storage,
        interpreter_params,
    )
    .transact(script)
    .receipts()
    .expect("Failed to transact")
    .to_owned();

    // then
    if let Receipt::Return { val, .. } = receipts[0].clone() {
        assert_eq!(val, gas_price);
    } else {
        panic!("expected return receipt, instead of {:?}", receipts[0])
    }
}

#[test]
fn get_metadata__gas_price__contract() {
    let rng = &mut StdRng::seed_from_u64(2322u64);

    // given
    let mut storage = MemoryStorage::default();

    let gas_price = 123;
    let gas_limit = 1_000_000;
    let maturity = Default::default();
    let height = Default::default();

    let consensus_params = ConsensusParameters::standard();

    // Create a contract that gets the gas price and returns it
    #[rustfmt::skip]
    let contract_bytecode = vec![
        op::gm_args(0x10, GMArgs::GetGasPrice),
        op::ret(0x10),
    ];

    let salt: Salt = rng.r#gen();
    let program: Witness = contract_bytecode.into_iter().collect::<Vec<u8>>().into();

    let contract_root = Contract::root_from_code(program.as_ref());
    let state_root = Contract::default_state_root();
    let contract_id = Contract::id(&salt, &contract_root, &state_root);

    // Deploy the contract
    let tx = TransactionBuilder::create(program, salt, vec![])
        .maturity(maturity)
        .add_fee_input()
        .add_contract_created()
        .add_max_fee_limit(10)
        .finalize()
        .into_checked(height, &consensus_params)
        .expect("failed to check tx");

    let interpreter_params = InterpreterParams {
        gas_price,
        ..Default::default()
    };

    // Deploy the contract into the blockchain
    assert!(
        Transactor::<_, _, _>::new(
            MemoryInstance::new(),
            &mut storage,
            interpreter_params.clone()
        )
        .transact(tx)
        .is_success()
    );

    let mut script = vec![
        op::movi(0x10, (1 + Bytes32::LEN + 2 * Bytes8::LEN) as Immediate18),
        op::aloc(0x10),
        op::move_(0x10, RegId::HP),
    ];

    // Copy contract ID bytes into the allocated memory
    contract_id.as_ref().iter().enumerate().for_each(|(i, b)| {
        script.push(op::movi(0x11, *b as Immediate18));
        script.push(op::sb(0x10, 0x11, i as Immediate12));
    });

    // Call the contract and forward the returned value
    script.push(op::call(0x10, RegId::ZERO, 0x10, RegId::CGAS));
    script.push(op::ret(RegId::ONE));

    let tx = TransactionBuilder::script(script.into_iter().collect(), vec![])
        .script_gas_limit(gas_limit)
        .add_input(Input::contract(
            rng.r#gen(),
            rng.r#gen(),
            rng.r#gen(),
            rng.r#gen(),
            contract_id,
        ))
        .add_output(Output::contract(0, rng.r#gen(), rng.r#gen()))
        .add_fee_input()
        .add_max_fee_limit(10)
        .finalize()
        .into_checked(height, &consensus_params)
        .expect("failed to check tx");

    // when
    let receipts = Transactor::<_, _, _>::new(
        MemoryInstance::new(),
        &mut storage,
        interpreter_params,
    )
    .transact(tx)
    .receipts()
    .expect("Failed to transact")
    .to_owned();

    // then
    let receipt = find_matching_return_receipt_for_contract_id(&receipts, &contract_id)
        .expect("There should be a return receipt for contract");

    if let Receipt::Return { val, .. } = receipt {
        assert_eq!(val, gas_price);
    } else {
        panic!("expected return receipt, instead of {:?}", receipt)
    }
}

fn find_matching_return_receipt_for_contract_id(
    receipts: &[Receipt],
    contract_id: &ContractId,
) -> Option<Receipt> {
    receipts
        .iter()
        .find(|receipt| {
            if let Receipt::Return { id, .. } = receipt {
                contract_id == id
            } else {
                false
            }
        })
        .cloned()
}

#[allow(deprecated)]
#[test]
fn get_transaction_fields() {
    let rng = &mut StdRng::seed_from_u64(2322u64);
    let gas_costs = GasCosts::default();

    let mut client = MemoryClient::default();

    let witness_limit = 1234;
    let max_fee_limit = 4321;
    let tip = 4321;
    let gas_limit = 10_000_000;
    let maturity = 50.into();
    let height = 122.into();
    let expiration = 123.into();
    let owner_idx = 1;
    let input = 10_000_000;

    let tx_params = TxParameters::default();

    let contract: Witness = vec![op::ret(0x01)].into_iter().collect::<Vec<u8>>().into();
    let salt = rng.r#gen();
    let code_root = Contract::root_from_code(contract.as_ref());
    let storage_slots = vec![];
    let state_root = Contract::initial_state_root(storage_slots.iter());
    let contract_id = Contract::id(&salt, &code_root, &state_root);

    let tx = TransactionBuilder::create(contract, salt, storage_slots)
        .add_unsigned_coin_input(
            SecretKey::random(rng),
            rng.r#gen(),
            max_fee_limit,
            AssetId::zeroed(),
            rng.r#gen(),
        )
        .add_contract_created()
        .finalize_checked(height);

    client.deploy(tx).unwrap();

    let predicate = vec![op::ret(RegId::ONE)].into_iter().collect::<Vec<u8>>();
    let mut predicate_data = vec![0u8; 512];

    rng.fill(predicate_data.as_mut_slice());

    let owner = Input::predicate_owner(&predicate);
    let input_coin_predicate = Input::coin_predicate(
        rng.r#gen(),
        owner,
        1_500,
        rng.r#gen(),
        rng.r#gen(),
        gas_costs.ret(),
        predicate.clone(),
        predicate_data.clone(),
    );

    let contract_input_index = 2;

    let message_amount = 5_500;
    let mut message_data = vec![0u8; 256];
    rng.fill(message_data.as_mut_slice());

    let mut m_data = vec![0u8; 64];
    let m_predicate = vec![op::ret(RegId::ONE)].into_iter().collect::<Vec<u8>>();
    let mut m_predicate_data = vec![0u8; 512];

    rng.fill(m_data.as_mut_slice());
    rng.fill(m_predicate_data.as_mut_slice());

    let owner = Input::predicate_owner(&m_predicate);
    let message_predicate = Input::message_data_predicate(
        rng.r#gen(),
        owner,
        7_500,
        rng.r#gen(),
        gas_costs.ret(),
        m_data.clone(),
        m_predicate.clone(),
        m_predicate_data.clone(),
    );

    let asset = rng.r#gen();
    let asset_amt = 27;

    let tx = TransactionBuilder::script(vec![], vec![])
        .maturity(maturity)
        .expiration(expiration)
        .owner(owner_idx)
        .with_gas_costs(gas_costs)
        .script_gas_limit(gas_limit)
        .add_unsigned_coin_input(
            SecretKey::random(rng),
            rng.r#gen(),
            input,
            AssetId::zeroed(),
            rng.r#gen(),
        )
        .add_input(input_coin_predicate)
        .add_input(Input::contract(
            rng.r#gen(),
            rng.r#gen(),
            state_root,
            rng.r#gen(),
            contract_id,
        ))
        .add_output(Output::variable(rng.r#gen(), rng.r#gen(), rng.r#gen()))
        .add_output(Output::contract(
            contract_input_index,
            rng.r#gen(),
            state_root,
        ))
        .add_witness(Witness::from(b"some-data".to_vec()))
        .add_unsigned_message_input(
            SecretKey::random(rng),
            rng.r#gen(),
            rng.r#gen(),
            message_amount,
            message_data.clone(),
        )
        .add_input(message_predicate)
        .add_unsigned_coin_input(
            SecretKey::random(rng),
            rng.r#gen(),
            asset_amt,
            asset,
            rng.r#gen(),
        )
        .add_output(Output::coin(rng.r#gen(), asset_amt, asset))
        .add_output(Output::change(rng.r#gen(), rng.gen_range(10..1000), asset))
        .finalize_checked(height);

    let inputs = tx.as_ref().inputs();
    let outputs = tx.as_ref().outputs();
    let witnesses = tx.as_ref().witnesses();

    let inputs_bytes: Vec<Vec<u8>> = inputs
        .iter()
        .map(|v| {
            let mut v = v.clone();
            v.prepare_sign();
            v.clone().to_bytes()
        })
        .collect();
    let outputs_bytes: Vec<Vec<u8>> = outputs
        .iter()
        .map(|v| {
            let mut v = *v;
            v.prepare_init_execute();
            v.clone().to_bytes()
        })
        .collect();
    let witnesses_bytes: Vec<Vec<u8>> =
        witnesses.iter().map(|w| w.clone().to_bytes()).collect();

    let receipts_root = tx.as_ref().receipts_root();

    let base_asset_id = AssetId::BASE;

    #[rustfmt::skip]
    let cases = vec![
        inputs_bytes[0].clone(), // 0 - ScriptInputAtIndex
        outputs_bytes[0].clone(), // 1 - ScriptOutputAtIndex
        witnesses_bytes[1].clone(), // 2 - ScriptWitnessAtIndex
        receipts_root.to_vec(), // 3 - ScriptReceiptsRoot
        inputs[0].utxo_id().unwrap().clone().to_bytes(), // 4- InputCoinTxId
        inputs[0].input_owner().unwrap().to_vec(), // 5 - InputCoinOwner
        inputs[0].asset_id(&base_asset_id).unwrap().to_vec(), // 6 - InputCoinAssetId
        predicate.clone(), // 7 - InputCoinPredicate
        predicate_data.clone(), // 8 - InputCoinPredicateData
        inputs[2].utxo_id().unwrap().clone().to_bytes(), // 9 - InputContractTxId
        inputs[2].balance_root().unwrap().to_vec(), // 10 - InputContractBalanceRoot
        inputs[2].state_root().unwrap().to_vec(), // 11 - InputContractStateRoot
        inputs[2].contract_id().unwrap().to_vec(), // 12 - InputContractId
        inputs[3].sender().unwrap().to_vec(), // 13 - InputMessageSender
        inputs[3].recipient().unwrap().to_vec(), // 14 - InputMessageRecipient
        inputs[3].nonce().unwrap().to_vec(), // 15 - InputMessageNonce
        m_data.clone(), // 16 - InputMessageData
        m_predicate.clone(), // 17 - InputMessagePredicate
        m_predicate_data.clone(), // 18 - InputMessagePredicateData
        outputs[2].to().unwrap().to_vec(), // 19 - OutputCoinTo
        outputs[2].asset_id().unwrap().to_vec(), // 20 - OutputCoinAssetId
        outputs[1].balance_root().unwrap().to_vec(), // 21 - OutputContractBalanceRoot
        outputs[1].state_root().unwrap().to_vec(), // 22 - OutputContractStateRoot
        witnesses[1].as_ref().to_vec(), // 23 - WitnessData
        outputs[3].asset_id().unwrap().to_vec(), // 24 - OutputCoinAssetId
        outputs[3].to().unwrap().to_vec(), // 25 - OutputCoinTo
        inputs[1].tx_pointer().unwrap().clone().to_bytes(), // 26 - InputCoinTxPointer
        inputs[2].tx_pointer().unwrap().clone().to_bytes() // 27 - InputContractTxPointer
    ];

    // hardcoded metadata of script len so it can be checked at runtime
    let script_reserved_words = 300 * WORD_SIZE;
    let script_offset = tx_params.tx_offset() + Script::script_offset_static();
    let script_data_offset = script_offset.saturating_add(
        bytes::padded_len_usize(script_reserved_words).unwrap_or(usize::MAX),
    );
    let script_data: Vec<u8> = cases.iter().flat_map(|c| c.iter()).copied().collect();

    #[rustfmt::skip]
    let mut script: Vec<u8> = vec![
        op::movi(0x20, 0x01),
        op::gtf_args(0x30, 0x19, GTFArgs::ScriptData),

        op::movi(0x19, 0x00),
        op::movi(0x11, TransactionRepr::Script as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::Type),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::gtf_args(0x10, RegId::ZERO, GTFArgs::TxLength),
        op::movi(0x11, 100), // Tx lenght is too complicated to backpatch
        op::gt(0x10, 0x10, 0x11), // so just make sure it's over some arbitrary number
        op::and(0x20, 0x20, 0x10),
        op::movi(0x11, 10_000), // and
        op::lt(0x10, 0x10, 0x11), // below some arbitrary number
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, tip as Immediate18),
        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::PolicyTip),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, (gas_limit & 0x3ffff) as Immediate18),
        op::movi(0x12, (gas_limit >> 18) as Immediate18),
        op::slli(0x12, 0x12, 18),
        op::or(0x11, 0x11, 0x12),
        op::gtf_args(0x10, 0x19, GTFArgs::ScriptGasLimit),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, *maturity as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::PolicyMaturity),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, *expiration as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::PolicyExpiration),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, max_fee_limit as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::PolicyMaxFee),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, witness_limit as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::PolicyWitnessLimit),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, owner_idx as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::PolicyOwner),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, PoliciesBits::all().bits() as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::PolicyTypes),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, inputs.len() as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::ScriptInputsCount),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, outputs.len() as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::ScriptOutputsCount),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, witnesses.len() as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::ScriptWitnessesCount),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::ScriptInputAtIndex),
        op::movi(0x11, cases[0].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::ScriptOutputAtIndex),
        op::movi(0x11, cases[1].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x01),
        op::gtf_args(0x10, 0x19, GTFArgs::ScriptWitnessAtIndex),
        op::movi(0x11, cases[2].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, script_reserved_words as Immediate18),
        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::ScriptLength),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, script_data.len() as Immediate18),
        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::ScriptDataLength),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        // Skip over ReceiptsRoot which is always zero
        op::addi(0x30, 0x30, cases[3].len().try_into().unwrap()),

        op::movi(0x11, script_offset as Immediate18),
        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::Script),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, script_data_offset as Immediate18),
        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::ScriptData),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, InputRepr::Coin as Immediate18),
        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::InputType),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        // Skip over CoinTxId which is always zero
        op::addi(0x30, 0x30, cases[4].len().try_into().unwrap()),

        op::movi(0x11, inputs[0].utxo_id().unwrap().output_index() as Immediate18),
        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::InputCoinOutputIndex),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::InputCoinOwner),
        op::movi(0x11, cases[5].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, (inputs[0].amount().unwrap() & 0x3ffff) as Immediate18),
        op::movi(0x12, (inputs[0].amount().unwrap() >> 18) as Immediate18),
        op::slli(0x12, 0x12, 18),
        op::or(0x11, 0x11, 0x12),
        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::InputCoinAmount),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::InputCoinAssetId),
        op::movi(0x11, cases[6].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, inputs[0].witness_index().unwrap() as Immediate18),
        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::InputCoinWitnessIndex),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, predicate.len() as Immediate18),
        op::movi(0x19, 0x01),
        op::gtf_args(0x10, 0x19, GTFArgs::InputCoinPredicateLength),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, predicate_data.len() as Immediate18),
        op::movi(0x19, 0x01),
        op::gtf_args(0x10, 0x19, GTFArgs::InputCoinPredicateDataLength),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x01),
        op::gtf_args(0x10, 0x19, GTFArgs::InputCoinPredicate),
        op::movi(0x11, cases[7].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x01),
        op::gtf_args(0x10, 0x19, GTFArgs::InputCoinPredicateData),
        op::movi(0x11, cases[8].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        // Skip over always-zero InputContract fields
        op::addi(0x30, 0x30, cases[9].len().try_into().unwrap()),
        op::addi(0x30, 0x30, cases[10].len().try_into().unwrap()),
        op::addi(0x30, 0x30, cases[11].len().try_into().unwrap()),

        op::movi(0x19, contract_input_index as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::InputContractId),
        op::movi(0x11, cases[12].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x03),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessageSender),
        op::movi(0x11, cases[13].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x03),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessageRecipient),
        op::movi(0x11, cases[14].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, message_amount as Immediate18),
        op::movi(0x19, 0x03),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessageAmount),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x03),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessageNonce),
        op::movi(0x11, cases[15].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, 0x02),
        op::movi(0x19, 0x03),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessageWitnessIndex),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, message_data.len() as Immediate18),
        op::movi(0x19, 0x03),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessageDataLength),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, m_predicate.len() as Immediate18),
        op::movi(0x19, 0x04),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessagePredicateLength),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, m_predicate_data.len() as Immediate18),
        op::movi(0x19, 0x04),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessagePredicateDataLength),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x04),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessageData),
        op::movi(0x11, cases[16].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x04),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessagePredicate),
        op::movi(0x11, cases[17].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x04),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessagePredicateData),
        op::movi(0x11, cases[18].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x04),
        op::gtf_args(0x10, 0x19, GTFArgs::InputMessagePredicateGasUsed),
        op::movi(0x11, 0 as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, OutputRepr::Contract as Immediate18),
        op::movi(0x19, 0x01),
        op::gtf_args(0x10, 0x19, GTFArgs::OutputType),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x02),
        op::gtf_args(0x10, 0x19, GTFArgs::OutputCoinTo),
        op::movi(0x11, cases[19].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, asset_amt as Immediate18),
        op::movi(0x19, 0x02),
        op::gtf_args(0x10, 0x19, GTFArgs::OutputCoinAmount),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x02),
        op::gtf_args(0x10, 0x19, GTFArgs::OutputCoinAssetId),
        op::movi(0x11, cases[20].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, asset_amt as Immediate18),
        op::movi(0x19, 0x02),
        op::gtf_args(0x10, 0x19, GTFArgs::OutputCoinAmount),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x11, contract_input_index as Immediate18),
        op::movi(0x19, 0x01),
        op::gtf_args(0x10, 0x19, GTFArgs::OutputContractInputIndex),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        // Skip over always-zero OutputContract fields
        op::addi(0x30, 0x30, cases[21].len().try_into().unwrap()),
        op::addi(0x30, 0x30, cases[22].len().try_into().unwrap()),

        op::movi(0x11, witnesses[1].as_ref().len() as Immediate18),
        op::movi(0x19, 0x01),
        op::gtf_args(0x10, 0x19, GTFArgs::WitnessDataLength),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x01),
        op::gtf_args(0x10, 0x19, GTFArgs::WitnessData),
        op::movi(0x11, cases[23].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x03),
        op::gtf_args(0x10, 0x19, GTFArgs::OutputCoinAssetId),
        op::movi(0x11, cases[24].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x03),
        op::gtf_args(0x10, 0x19, GTFArgs::OutputCoinTo),
        op::movi(0x11, cases[25].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, inputs.len() as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::TxInputsCount),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, outputs.len() as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::TxOutputsCount),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::movi(0x11, witnesses.len() as Immediate18),
        op::gtf_args(0x10, 0x19, GTFArgs::TxWitnessesCount),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::gtf_args(0x30, 0x19, GTFArgs::ScriptData),
        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::TxInputAtIndex),
        op::movi(0x11, cases[0].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x00),
        op::gtf_args(0x10, 0x19, GTFArgs::TxOutputAtIndex),
        op::movi(0x11, cases[1].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x01),
        op::gtf_args(0x10, 0x19, GTFArgs::TxWitnessAtIndex),
        op::movi(0x11, cases[2].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::movi(0x19, 0x01),
        op::gtf_args(0x10, 0x19, GTFArgs::InputCoinTxPointer),
        op::movi(0x11, cases[26].len() as Immediate18),
        op::meq(0x10, 0x10, 0x30, 0x11),
        op::add(0x30, 0x30, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::log(0x20, 0x00, 0x00, 0x00),
        op::ret(0x00)
    ].into_iter().collect();

    while script.len() < script_reserved_words {
        script.extend(op::noop().to_bytes());
    }

    assert_eq!(script.len(), script_reserved_words);

    let mut builder = TransactionBuilder::script(script, script_data);

    tx.as_ref().inputs().iter().for_each(|i| {
        builder.add_input(i.clone());
    });

    tx.as_ref().outputs().iter().for_each(|o| {
        builder.add_output(*o);
    });

    tx.as_ref().witnesses().iter().for_each(|w| {
        builder.add_witness(w.clone());
    });

    let tx = builder
        .tip(tip)
        .maturity(maturity)
        .expiration(expiration)
        .owner(owner_idx)
        .script_gas_limit(gas_limit)
        .witness_limit(witness_limit)
        .max_fee_limit(max_fee_limit)
        .finalize_checked_basic(height);

    let receipts = client.transact(tx);
    let success = receipts
        .iter()
        .any(|r| matches!(r, Receipt::Log{ ra, .. } if ra == &1));

    assert!(success);
}

#[test]
fn get_owner_metadata__two_different_owners__policy_set() {
    let rng = &mut StdRng::seed_from_u64(2322u64);
    let gas_costs = GasCosts::default();

    let mut client = MemoryClient::default();

    let predicate_1 = vec![op::ret(RegId::ONE)].into_iter().collect::<Vec<u8>>();

    let owner_1 = Input::predicate_owner(&predicate_1);
    let input_coin_predicate_1 = Input::coin_predicate(
        rng.r#gen(),
        owner_1,
        100_000_000,
        AssetId::BASE,
        rng.r#gen(),
        gas_costs.ret(),
        predicate_1.clone(),
        vec![],
    );

    let predicate_2 = vec![op::ret(RegId::ONE), op::ret(RegId::ONE)]
        .into_iter()
        .collect::<Vec<u8>>();

    let owner_2 = Input::predicate_owner(&predicate_2);
    let input_coin_predicate_2 = Input::coin_predicate(
        rng.r#gen(),
        owner_2,
        100_000_000,
        AssetId::BASE,
        rng.r#gen(),
        gas_costs.ret(),
        predicate_2.clone(),
        vec![],
    );
    // Set second predicate as an owner
    let owner_idx = 1;

    assert_ne!(predicate_2, predicate_1);

    #[rustfmt::skip]
    let script: Vec<u8> = vec![
        op::movi(0x20, 0x01),
        op::gm_args(0x10, GMArgs::GetOwner),

        op::movi(0x19, owner_idx as u32),
        op::gtf_args(0x11, 0x19, GTFArgs::InputCoinOwner),

        op::movi(0x13, 32),
        op::meq(0x12, 0x10, 0x11, 0x13),

        op::log(0x12, 0x00, 0x00, 0x00),
        op::ret(0x00)
    ].into_iter().collect();

    let tx = TransactionBuilder::script(script, vec![])
        // Given
        .owner(owner_idx)
        .max_fee_limit(1_000_000)
        .script_gas_limit(1_000_000)
        .with_gas_costs(gas_costs)
        .add_input(input_coin_predicate_1)
        .add_input(input_coin_predicate_2)
        .finalize_checked(1u32.into());

    // When
    let receipts = client.transact(tx);

    // Then
    let success = receipts
        .iter()
        .any(|r| matches!(r, Receipt::Log{ ra, .. } if ra == &1));
    assert!(success);
}

#[test]
fn get_owner_metadata__two_different_owners__policy_not_set_causes_panic() {
    let rng = &mut StdRng::seed_from_u64(2322u64);
    let gas_costs = GasCosts::default();

    let mut client = MemoryClient::default();

    let predicate_1 = vec![op::ret(RegId::ONE)].into_iter().collect::<Vec<u8>>();

    let owner_1 = Input::predicate_owner(&predicate_1);
    let input_coin_predicate_1 = Input::coin_predicate(
        rng.r#gen(),
        owner_1,
        100_000_000,
        AssetId::BASE,
        rng.r#gen(),
        gas_costs.ret(),
        predicate_1.clone(),
        vec![],
    );

    let predicate_2 = vec![op::ret(RegId::ONE), op::ret(RegId::ONE)]
        .into_iter()
        .collect::<Vec<u8>>();

    let owner_2 = Input::predicate_owner(&predicate_2);
    let input_coin_predicate_2 = Input::coin_predicate(
        rng.r#gen(),
        owner_2,
        100_000_000,
        AssetId::BASE,
        rng.r#gen(),
        gas_costs.ret(),
        predicate_2.clone(),
        vec![],
    );
    // Set second predicate as an owner
    let owner_idx = 1u32;

    assert_ne!(predicate_2, predicate_1);

    #[rustfmt::skip]
    let script: Vec<u8> = vec![
        op::movi(0x20, 0x01),
        op::gm_args(0x10, GMArgs::GetOwner),

        op::movi(0x19, owner_idx),
        op::gtf_args(0x11, 0x19, GTFArgs::InputCoinOwner),

        op::movi(0x13, 32),
        op::meq(0x12, 0x10, 0x11, 0x13),

        op::log(0x12, 0x00, 0x00, 0x00),
        op::ret(0x00)
    ].into_iter().collect();

    // Given
    let tx = TransactionBuilder::script(script, vec![])
        .max_fee_limit(1_000_000)
        .script_gas_limit(1_000_000)
        .with_gas_costs(gas_costs)
        .add_input(input_coin_predicate_1)
        .add_input(input_coin_predicate_2)
        .finalize_checked(1u32.into());

    // When
    let receipts = client.transact(tx);

    // Then
    let panic = receipts.iter().any(|r| matches!(r, Receipt::Panic { .. }));
    assert!(panic);
}

#[test]
fn get_owner_metadata__two_same_owners__policy_not_set_returns_owner() {
    let rng = &mut StdRng::seed_from_u64(2322u64);
    let gas_costs = GasCosts::default();

    let mut client = MemoryClient::default();

    let predicate_1 = vec![op::ret(RegId::ONE)].into_iter().collect::<Vec<u8>>();

    let owner_1 = Input::predicate_owner(&predicate_1);
    let input_coin_predicate_1 = Input::coin_predicate(
        rng.r#gen(),
        owner_1,
        100_000_000,
        AssetId::BASE,
        rng.r#gen(),
        gas_costs.ret(),
        predicate_1.clone(),
        vec![],
    );

    let input_coin_predicate_2 = Input::coin_predicate(
        rng.r#gen(),
        owner_1,
        100_000_000,
        AssetId::BASE,
        rng.r#gen(),
        gas_costs.ret(),
        predicate_1.clone(),
        vec![],
    );
    let owner_idx = 0u32;

    #[rustfmt::skip]
    let script: Vec<u8> = vec![
        op::movi(0x20, 0x01),
        op::gm_args(0x10, GMArgs::GetOwner),

        op::movi(0x19, owner_idx),
        op::gtf_args(0x11, 0x19, GTFArgs::InputCoinOwner),

        op::movi(0x13, 32),
        op::meq(0x12, 0x10, 0x11, 0x13),

        op::log(0x12, 0x00, 0x00, 0x00),
        op::ret(0x00)
    ].into_iter().collect();

    // Given
    let tx = TransactionBuilder::script(script, vec![])
        .max_fee_limit(1_000_000)
        .script_gas_limit(1_000_000)
        .with_gas_costs(gas_costs)
        .add_input(input_coin_predicate_1)
        .add_input(input_coin_predicate_2)
        .finalize_checked(1u32.into());

    // When
    let receipts = client.transact(tx);

    // Then
    let success = receipts
        .iter()
        .any(|r| matches!(r, Receipt::Log{ ra, .. } if ra == &1));
    assert!(success);
}

#[test]
fn get__create_specific_transaction_fields__success() {
    const PREDICATE_COUNT: u64 = 1;
    const MAX_PREDICATE_GAS: u64 = 1_000_000;
    const MAX_TX_GAS: u64 = MAX_PREDICATE_GAS * PREDICATE_COUNT;
    let rng = &mut StdRng::seed_from_u64(8586);
    let mut client = MemoryClient::default();

    // Given
    let salt = Salt::new([1; 32]);
    let storage_slots = vec![
        StorageSlot::new(Bytes32::new([0; 32]), Bytes32::new([0; 32])),
        StorageSlot::new(Bytes32::new([2; 32]), Bytes32::new([3; 32])),
    ];
    // Write the elements we want to check into the bytecode
    // so that they are available in the predicate memory
    // doesn't work for the contract_id because changing the bytecode would change the
    // contract_id
    let mut bytecode = Vec::new();
    bytecode.extend(salt.to_bytes());
    bytecode.extend(storage_slots[1].to_bytes());
    bytecode.extend(Contract::initial_state_root(storage_slots.iter()).iter());
    let mut tx = TransactionBuilder::create(bytecode.into(), salt, storage_slots);
    tx.add_fee_input();
    tx.add_contract_created();

    let instructions_contract_id = alloc_bytearray::<32>(
        0x11,
        hex::decode("54dcc5dbe7dc3ff267b6a9147c358a38f8b7ac61160769458fdcf53f751be37f")
            .unwrap()
            .try_into()
            .unwrap(),
    );
    // When
    #[rustfmt::skip]
    let mut predicate_code = vec![
        op::gtf_args(0x10, 0x00, GTFArgs::CreateBytecodeWitnessIndex),
        op::movi(0x11, 0x00),
        op::eq(0x20, 0x10, 0x11),

        // Store the bytecode pointer for later use
        op::gtf_args(0x13, 0x10, GTFArgs::TxWitnessAtIndex),
        // Skip the length of the bytecode
        op::addi(0x13, 0x13, 0x08),

        op::gtf_args(0x10, 0x00, GTFArgs::CreateStorageSlotsCount),
        op::movi(0x11, 0x02),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::gtf_args(0x10, 0x00, GTFArgs::CreateSalt),
        op::movi(0x11, 0x20),
        // Salt is at the start of the bytecode which start at value stored in 0x13
        op::meq(0x10, 0x10, 0x13, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::gtf_args(0x10, 0x01, GTFArgs::CreateStorageSlotAtIndex),
        op::movi(0x11, StorageSlot::SLOT_SIZE as Immediate18),
        // Increase bytecode pointer by 32 bytes to pass the salt
        op::addi(0x13, 0x13, 0x20),
        op::meq(0x10, 0x10, 0x13, 0x11),
        op::and(0x20, 0x20, 0x10),

        op::gtf_args(0x10, 0x00, GTFArgs::OutputContractCreatedStateRoot),
        op::movi(0x11, 0x20),
        // Increase bytecode pointer by SLOT_SIZE bytes to pass the storage slot written
        // in bytecode
        op::addi(0x13, 0x13, StorageSlot::SLOT_SIZE as Immediate12),
        op::meq(0x10, 0x10, 0x13, 0x11),
        op::and(0x20, 0x20, 0x10),

    ];

    predicate_code.extend(instructions_contract_id);

    predicate_code.extend(vec![
        op::gtf_args(0x10, 0x00, GTFArgs::OutputContractCreatedContractId),
        op::movi(0x12, 0x20),
        // instructions_contract_id saved the value in a memory starting at value of
        // `0x11`
        op::meq(0x10, 0x10, 0x11, 0x12),
        op::and(0x20, 0x20, 0x10),
        op::ret(0x20),
    ]);

    let predicate_code = predicate_code.into_iter().collect();

    let predicate_owner: Address = Input::predicate_owner(&predicate_code);
    tx.add_input(Input::coin_predicate(
        rng.r#gen(),
        predicate_owner,
        rng.r#gen(),
        *tx.get_params().base_asset_id(),
        rng.r#gen(),
        0,
        predicate_code,
        vec![],
    ));
    let tx_param = TxParameters::default().with_max_gas_per_tx(MAX_TX_GAS);
    let mut tx = tx.finalize();
    let gas_costs = GasCosts::new(GasCostsValuesV5::free().into());
    let predicate_param =
        PredicateParameters::default().with_max_gas_per_predicate(MAX_PREDICATE_GAS);
    let fee_param = FeeParameters::default().with_gas_per_byte(0);

    let mut consensus_params = ConsensusParameters::standard();
    consensus_params.set_gas_costs(gas_costs.clone());
    consensus_params.set_predicate_params(predicate_param);
    consensus_params.set_tx_params(tx_param);
    consensus_params.set_fee_params(fee_param);
    tx.estimate_predicates(
        &consensus_params.clone().into(),
        MemoryInstance::new(),
        &EmptyStorage,
    )
    .unwrap();

    let tx = tx
        .into_checked(BlockHeight::new(0), &consensus_params)
        .unwrap();

    // Then
    client.deploy(tx).unwrap();
}

#[test]
fn get__upload_specific_transaction_fields__success() {
    const PREDICATE_COUNT: u64 = 1;
    const MAX_PREDICATE_GAS: u64 = 1_000_000;
    const MAX_TX_GAS: u64 = MAX_PREDICATE_GAS * PREDICATE_COUNT;
    let rng = &mut StdRng::seed_from_u64(8586);
    let mut client = MemoryClient::default();

    // Given
    let subsection = UploadSubsection::split_bytecode(&[1; 10], 1).unwrap()[0].clone();
    let mut tx = TransactionBuilder::upload(UploadBody {
        root: subsection.root,
        witness_index: 0,
        subsection_index: subsection.subsection_index,
        subsections_number: subsection.subsections_number,
        proof_set: subsection.proof_set.clone(),
    });
    tx.add_witness(subsection.subsection.into());
    tx.add_fee_input();

    let instructions_root = alloc_bytearray(0x11, subsection.root.into());
    let instructions_proof = alloc_bytearray(0x11, subsection.proof_set[1].into());
    // When
    #[rustfmt::skip]
    let mut predicate_code = vec![
        op::gtf_args(0x10, 0x00, GTFArgs::UploadRoot),
    ];
    predicate_code.extend(instructions_root);
    predicate_code.extend(vec![
        op::meq(0x20, 0x10, 0x11, 0x20),
        op::gtf_args(0x10, 0x00, GTFArgs::UploadWitnessIndex),
        op::movi(0x11, 0x00),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),
        op::gtf_args(0x10, 0x00, GTFArgs::UploadSubsectionIndex),
        op::movi(0x11, subsection.subsection_index as u32),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),
        op::gtf_args(0x10, 0x00, GTFArgs::UploadSubsectionsCount),
        op::movi(0x11, subsection.subsections_number as u32),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),
        op::gtf_args(0x10, 0x00, GTFArgs::UploadProofSetCount),
        op::movi(0x11, subsection.proof_set.len() as u32),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),
        op::gtf_args(0x10, 0x01, GTFArgs::UploadProofSetAtIndex),
    ]);
    predicate_code.extend(instructions_proof);
    predicate_code.extend(vec![op::meq(0x20, 0x10, 0x11, 0x20), op::ret(0x20)]);

    let predicate_code = predicate_code.into_iter().collect();

    let predicate_owner: Address = Input::predicate_owner(&predicate_code);
    tx.add_input(Input::coin_predicate(
        rng.r#gen(),
        predicate_owner,
        rng.r#gen(),
        *tx.get_params().base_asset_id(),
        rng.r#gen(),
        0,
        predicate_code,
        vec![],
    ));
    let tx_param = TxParameters::default().with_max_gas_per_tx(MAX_TX_GAS);
    let mut tx = tx.finalize();
    let gas_costs = GasCosts::new(GasCostsValuesV5::free().into());
    let predicate_param =
        PredicateParameters::default().with_max_gas_per_predicate(MAX_PREDICATE_GAS);
    let fee_param = FeeParameters::default().with_gas_per_byte(0);

    let mut consensus_params = ConsensusParameters::standard();
    consensus_params.set_gas_costs(gas_costs.clone());
    consensus_params.set_predicate_params(predicate_param);
    consensus_params.set_tx_params(tx_param);
    consensus_params.set_fee_params(fee_param);
    tx.estimate_predicates(
        &consensus_params.clone().into(),
        MemoryInstance::new(),
        &EmptyStorage,
    )
    .unwrap();

    let tx = tx
        .into_checked(BlockHeight::new(0), &consensus_params)
        .unwrap();

    // Then
    client.upload(tx).unwrap();
}

#[test]
fn get__blob_specific_transaction_fields__success() {
    const PREDICATE_COUNT: u64 = 1;
    const MAX_PREDICATE_GAS: u64 = 1_000_000;
    const MAX_TX_GAS: u64 = MAX_PREDICATE_GAS * PREDICATE_COUNT;
    let rng = &mut StdRng::seed_from_u64(8586);
    let mut client = MemoryClient::default();

    // Given
    let id = BlobId::compute(&[1; 100]);
    let mut tx = TransactionBuilder::blob(BlobBody {
        id,
        witness_index: 0,
    });
    tx.add_witness(vec![1; 100].into());
    tx.add_fee_input();

    // When
    let blob_instructions = alloc_bytearray(0x11, id.into());
    #[rustfmt::skip]
    let mut predicate_code = vec![
        op::gtf_args(0x10, 0x00, GTFArgs::BlobId),
    ];
    predicate_code.extend(blob_instructions);
    predicate_code.extend(vec![
        op::meq(0x20, 0x10, 0x11, 0x20),
        op::gtf_args(0x10, 0x00, GTFArgs::BlobWitnessIndex),
        op::movi(0x11, 0x00),
        op::eq(0x10, 0x10, 0x11),
        op::and(0x20, 0x20, 0x10),
        op::ret(0x20),
    ]);

    let predicate_code = predicate_code.into_iter().collect();

    let predicate_owner: Address = Input::predicate_owner(&predicate_code);
    tx.add_input(Input::coin_predicate(
        rng.r#gen(),
        predicate_owner,
        rng.r#gen(),
        *tx.get_params().base_asset_id(),
        rng.r#gen(),
        0,
        predicate_code,
        vec![],
    ));
    let tx_param = TxParameters::default().with_max_gas_per_tx(MAX_TX_GAS);
    let mut tx = tx.finalize();
    let gas_costs = GasCosts::new(GasCostsValuesV5::free().into());
    let predicate_param =
        PredicateParameters::default().with_max_gas_per_predicate(MAX_PREDICATE_GAS);
    let fee_param = FeeParameters::default().with_gas_per_byte(0);

    let mut consensus_params = ConsensusParameters::standard();
    consensus_params.set_gas_costs(gas_costs.clone());
    consensus_params.set_predicate_params(predicate_param);
    consensus_params.set_tx_params(tx_param);
    consensus_params.set_fee_params(fee_param);
    tx.estimate_predicates(
        &consensus_params.clone().into(),
        MemoryInstance::new(),
        &EmptyStorage,
    )
    .unwrap();

    let tx = tx
        .into_checked(BlockHeight::new(0), &consensus_params)
        .unwrap();

    // Then
    client.blob(tx).unwrap();
}

fn valid_storage(hash: Bytes32, bytecode: Vec<u8>) -> MemoryStorage {
    let mut storage = MemoryStorage::default();
    storage.set_state_transition_version(123);
    storage
        .state_transition_bytecodes_mut()
        .insert(hash, UploadedBytecode::Completed(bytecode));

    storage
}

#[test]
fn get__upgrade_specific_transaction_fields__success() {
    const PREDICATE_COUNT: u64 = 1;
    const MAX_PREDICATE_GAS: u64 = 1_000_000;
    const MAX_TX_GAS: u64 = MAX_PREDICATE_GAS * PREDICATE_COUNT;
    let rng = &mut StdRng::seed_from_u64(8586);

    // Given
    let root = Bytes32::from([1; 32]);
    let mut client: MemoryClient<MemoryInstance> = MemoryClient::new(
        MemoryInstance::default(),
        valid_storage(root, vec![]),
        InterpreterParams::default(),
    );
    let mut tx = TransactionBuilder::upgrade(UpgradePurpose::StateTransition { root });
    tx.add_fee_input();

    // When
    let state_transition_instructions = alloc_bytearray(0x11, root.into());
    #[rustfmt::skip]
    let mut predicate_code = vec![
        op::gtf_args(0x10, 0x00, GTFArgs::UpgradePurpose),
    ];
    predicate_code.extend(state_transition_instructions);
    predicate_code.extend(vec![op::meq(0x20, 0x10, 0x11, 0x20), op::ret(0x20)]);

    let predicate_code = predicate_code.into_iter().collect();

    let predicate_owner: Address = Input::predicate_owner(&predicate_code);
    tx.add_input(Input::coin_predicate(
        rng.r#gen(),
        predicate_owner,
        rng.r#gen(),
        *tx.get_params().base_asset_id(),
        rng.r#gen(),
        0,
        predicate_code,
        vec![],
    ));
    let tx_param = TxParameters::default().with_max_gas_per_tx(MAX_TX_GAS);
    let mut tx = tx.finalize();
    let gas_costs = GasCosts::new(GasCostsValuesV5::free().into());
    let predicate_param =
        PredicateParameters::default().with_max_gas_per_predicate(MAX_PREDICATE_GAS);
    let fee_param = FeeParameters::default().with_gas_per_byte(0);

    let mut consensus_params = ConsensusParameters::standard();
    consensus_params.set_gas_costs(gas_costs.clone());
    consensus_params.set_predicate_params(predicate_param);
    consensus_params.set_tx_params(tx_param);
    consensus_params.set_fee_params(fee_param);
    consensus_params.set_privileged_address(predicate_owner);
    tx.estimate_predicates(
        &consensus_params.clone().into(),
        MemoryInstance::new(),
        &EmptyStorage,
    )
    .unwrap();

    let tx = tx
        .into_checked(BlockHeight::new(0), &consensus_params)
        .unwrap();

    // Then
    client.upgrade(tx).unwrap();
}