namada_vm 0.48.3

The Namada VM
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
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
//! Wasm runners

use std::cell::RefCell;
use std::collections::BTreeSet;
use std::error::Error as _;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::num::NonZeroU32;
use std::rc::Rc;

use borsh::BorshDeserialize;
use namada_core::address::Address;
use namada_core::hash::{Error as TxHashError, Hash};
use namada_core::internal::HostEnvResult;
use namada_core::storage::{Key, TxIndex};
use namada_core::validity_predicate::VpError;
use namada_gas::{GasMetering, TxGasMeter, VpGasMeter, WASM_MEMORY_PAGE_GAS};
use namada_state::prefix_iter::PrefixIterators;
use namada_state::{DBIter, State, StateRead, StorageHasher, StorageRead, DB};
use namada_tx::data::{TxSentinel, TxType};
use namada_tx::{BatchedTxRef, Commitment, Section, Tx, TxCommitments};
use namada_vp::vp_host_fns;
use parity_wasm::elements::Instruction::*;
use parity_wasm::elements::{self, SignExtInstruction};
use thiserror::Error;
use wasmer::sys::{BaseTunables, Features};
use wasmer::{Engine, Module, NativeEngineExt, Store, Target};

use super::memory::{Limit, WasmMemory};
use super::TxCache;
use crate::host_env::{TxVmEnv, VpCtx, VpEvaluator, VpVmEnv};
use crate::types::VpInput;
use crate::wasm::host_env::{tx_imports, vp_imports};
use crate::wasm::{memory, Cache, CacheName, VpCache};
use crate::{
    validate_untrusted_wasm, HostRef, RwAccess, WasmCacheAccess,
    WasmValidationError,
};

const TX_ENTRYPOINT: &str = "_apply_tx";
const VP_ENTRYPOINT: &str = "_validate_tx";
const WASM_STACK_LIMIT: u32 = u16::MAX as u32;

/// The error type returned by transactions.
// TODO(namada#2980): move this to `core`, to be shared with the wasm vm,
// and make it an `enum` of different variants
type TxError = String;

#[allow(missing_docs)]
#[derive(Error, Debug)]
pub enum Error {
    #[error("VP error: {0}")]
    VpError(VpError),
    #[error("Transaction error: {0}")]
    TxError(TxError),
    #[error("Missing tx section: {0}")]
    MissingSection(String),
    #[error("Memory error: {0}")]
    MemoryError(memory::Error),
    #[error("Unable to inject stack limiter")]
    StackLimiterInjection,
    #[error("Wasm deserialization error: {0}")]
    DeserializationError(elements::Error),
    #[error("Wasm serialization error: {0}")]
    SerializationError(elements::Error),
    #[error("Unable to inject gas meter")]
    GasMeterInjection,
    #[error("Wasm compilation error: {0}")]
    CompileError(wasmer::CompileError),
    #[error("Missing wasm memory export, failed with: {0}")]
    MissingModuleMemory(wasmer::ExportError),
    #[error("Missing wasm entrypoint: {0}")]
    MissingModuleEntrypoint(wasmer::ExportError),
    #[error("Failed running wasm with: {0}")]
    RuntimeError(wasmer::RuntimeError),
    #[error("Failed instantiating wasm module with: {0}")]
    // Boxed cause it's 128b
    InstantiationError(Box<wasmer::InstantiationError>),
    #[error(
        "Unexpected module entrypoint interface {entrypoint}, failed with: \
         {error}"
    )]
    UnexpectedModuleEntrypointInterface {
        entrypoint: &'static str,
        error: wasmer::RuntimeError,
    },
    #[error("Wasm validation error: {0}")]
    ValidationError(WasmValidationError),
    #[error("Wasm code hash error: {0}")]
    CodeHash(TxHashError),
    #[error("Unable to load wasm code: {0}")]
    LoadWasmCode(String),
    #[error("Unable to find compiled wasm code")]
    NoCompiledWasmCode,
    #[error("Gas error: {0}")]
    GasError(String),
    #[error("Failed type conversion: {0}")]
    ConversionError(String),
    #[error("Storage error: {0}")]
    Error(String),
    #[error("Tx is not allowed in allowlist parameter")]
    DisallowedTx,
    #[error("Invalid transaction section signature: {0}")]
    InvalidSectionSignature(String),
}

/// Result for functions that may fail
pub type Result<T> = std::result::Result<T, Error>;

/// Returns [`Error::DisallowedTx`] when the given tx is a user tx and its code
/// `Hash` is not included in the `tx_allowlist` parameter.
pub fn check_tx_allowed<S>(
    batched_tx: &BatchedTxRef<'_>,
    storage: &S,
) -> Result<()>
where
    S: StorageRead,
{
    let BatchedTxRef { tx, cmt } = batched_tx;
    if let TxType::Wrapper(_) = tx.header().tx_type {
        if let Some(code_sec) = tx
            .get_section(cmt.code_sechash())
            .and_then(|x| Section::code_sec(&x))
        {
            if namada_parameters::is_tx_allowed(storage, &code_sec.code.hash())
                .map_err(|e| Error::Error(e.to_string()))?
            {
                return Ok(());
            }
        }
        return Err(Error::DisallowedTx);
    }
    Ok(())
}

/// Execute a transaction code. Returns the set verifiers addresses requested by
/// the transaction.
#[allow(clippy::too_many_arguments)]
pub fn tx<S, CA>(
    state: &mut S,
    gas_meter: &RefCell<TxGasMeter>,
    tx_index: &TxIndex,
    tx: &Tx,
    cmt: &TxCommitments,
    vp_wasm_cache: &mut VpCache<CA>,
    tx_wasm_cache: &mut TxCache<CA>,
) -> Result<BTreeSet<Address>>
where
    S: StateRead + State + StorageRead,
    CA: 'static + WasmCacheAccess,
{
    let tx_code = tx
        .get_section(cmt.code_sechash())
        .and_then(|x| Section::code_sec(x.as_ref()))
        .ok_or(Error::MissingSection(cmt.code_sechash().to_string()))?;

    // Check if the tx code is allowed (to be done after the check on the code
    // section commitment to let the replay protection mechanism run some
    // optimizations)
    let batched_tx = tx.batch_ref_tx(cmt);
    check_tx_allowed(&batched_tx, state)?;

    // If the transaction code has a tag, ensure that the tag hash equals the
    // transaction code's hash.
    if let Some(tag) = &tx_code.tag {
        // Get the WASM code hash corresponding to the tag from storage
        let hash_key = Key::wasm_hash(tag);
        let hash_value = state
            .read(&hash_key)
            .map_err(|e| {
                Error::LoadWasmCode(format!(
                    "Read wasm code hash failed from storage: key {}, error {}",
                    hash_key, e
                ))
            })?
            .ok_or_else(|| {
                Error::LoadWasmCode(format!(
                    "No wasm code hash in storage: key {}",
                    hash_key
                ))
            })?;
        // Ensure that the queried code hash equals the transaction's code hash
        let tx_code_hash = tx_code.code.hash();
        if tx_code_hash != hash_value {
            return Err(Error::LoadWasmCode(format!(
                "Transaction code hash does not correspond to tag: tx hash \
                 {}, tag {}, tag hash {}",
                tx_code_hash, tag, hash_value,
            )));
        }
    }

    let (module, store) =
        fetch_or_compile(tx_wasm_cache, &tx_code.code, state, gas_meter)?;
    let store = Rc::new(RefCell::new(store));

    let mut iterators: PrefixIterators<'_, <S as StateRead>::D> =
        PrefixIterators::default();
    let mut verifiers = BTreeSet::new();
    let mut result_buffer: Option<Vec<u8>> = None;
    let mut yielded_value: Option<Vec<u8>> = None;

    let sentinel = RefCell::new(TxSentinel::default());
    let (write_log, in_mem, db) = state.split_borrow();
    let mut env = TxVmEnv::new(
        WasmMemory::new(Rc::downgrade(&store)),
        write_log,
        in_mem,
        db,
        &mut iterators,
        gas_meter,
        &sentinel,
        tx,
        cmt,
        tx_index,
        &mut verifiers,
        &mut result_buffer,
        &mut yielded_value,
        vp_wasm_cache,
        tx_wasm_cache,
    );

    // Instantiate the wasm module
    let instance = {
        let mut store = store.borrow_mut();
        let imports = tx_imports(&mut *store, env.clone());
        wasmer::Instance::new(&mut *store, &module, &imports)
            .map_err(|e| Error::InstantiationError(Box::new(e)))?
    };

    // Fetch guest's main memory
    let guest_memory = instance
        .exports
        .get_memory("memory")
        .map_err(Error::MissingModuleMemory)?;

    env.memory.init_from(guest_memory);

    // Write the inputs in the memory exported from the wasm
    // module
    let memory::TxCallInput {
        tx_data_ptr,
        tx_data_len,
    } = {
        let mut store = store.borrow_mut();
        memory::write_tx_inputs(&mut *store, guest_memory, &batched_tx)
            .map_err(Error::MemoryError)?
    };

    // Get the module's entrypoint to be called
    let apply_tx = {
        let store = store.borrow();
        instance
            .exports
            .get_function(TX_ENTRYPOINT)
            .map_err(Error::MissingModuleEntrypoint)?
            .typed::<(u64, u64), u64>(&*store)
            .map_err(|error| Error::UnexpectedModuleEntrypointInterface {
                entrypoint: TX_ENTRYPOINT,
                error,
            })?
    };
    let ok = apply_tx
        .call(
            unsafe { &mut *RefCell::as_ptr(&*store) },
            tx_data_ptr,
            tx_data_len,
        )
        .map_err(|err| {
            tracing::debug!("Tx WASM failed with {}", err);
            match *sentinel.borrow() {
                TxSentinel::None => Error::RuntimeError(err),
                TxSentinel::OutOfGas => Error::GasError(err.to_string()),
                TxSentinel::InvalidCommitment => {
                    Error::MissingSection(err.to_string())
                }
            }
        })?;

    // NB: early drop this data to avoid memory errors
    _ = (instance, env);

    if ok == 1 {
        let store = Rc::into_inner(store)
            .expect("The store must be dropped after execution to avoid leaks");
        let _store = RefCell::into_inner(store);
        Ok(verifiers)
    } else {
        let err = yielded_value.take().map_or_else(
            || Ok("Execution ended abruptly with an unknown error".to_owned()),
            |borsh_encoded_err| {
                let tx_err = TxError::try_from_slice(&borsh_encoded_err)
                    .map_err(|e| Error::ConversionError(e.to_string()))?;
                Ok(tx_err)
            },
        )?;

        Err(match *sentinel.borrow() {
            TxSentinel::None => Error::TxError(err),
            TxSentinel::OutOfGas => Error::GasError(err),
            TxSentinel::InvalidCommitment => Error::MissingSection(err),
        })
    }
}

/// Execute a validity predicate code. Returns whether the validity
/// predicate accepted storage modifications performed by the transaction
/// that triggered the execution.
#[allow(clippy::too_many_arguments)]
pub fn vp<S, CA>(
    vp_code_hash: Hash,
    batched_tx: &BatchedTxRef<'_>,
    tx_index: &TxIndex,
    address: &Address,
    state: &S,
    gas_meter: &RefCell<VpGasMeter>,
    keys_changed: &BTreeSet<Key>,
    verifiers: &BTreeSet<Address>,
    mut vp_wasm_cache: VpCache<CA>,
) -> Result<()>
where
    S: StateRead,
    CA: 'static + WasmCacheAccess,
{
    // Compile the wasm module
    let (module, store) = fetch_or_compile(
        &mut vp_wasm_cache,
        &Commitment::Hash(vp_code_hash),
        state,
        gas_meter,
    )?;
    let store = Rc::new(RefCell::new(store));

    let mut iterators: PrefixIterators<'_, <S as StateRead>::D> =
        PrefixIterators::default();
    let mut result_buffer: Option<Vec<u8>> = None;
    let mut yielded_value: Option<Vec<u8>> = None;
    let eval_runner =
        VpEvalWasm::<<S as StateRead>::D, <S as StateRead>::H, CA> {
            db: PhantomData,
            hasher: PhantomData,
            cache_access: PhantomData,
        };
    let BatchedTxRef { tx, cmt } = batched_tx;
    let mut env = VpVmEnv::new(
        WasmMemory::new(Rc::downgrade(&store)),
        address,
        state.write_log(),
        state.in_mem(),
        state.db(),
        gas_meter,
        tx,
        cmt,
        tx_index,
        &mut iterators,
        verifiers,
        &mut result_buffer,
        &mut yielded_value,
        keys_changed,
        &eval_runner,
        &mut vp_wasm_cache,
    );

    let yielded_value_borrow = env.ctx.yielded_value;

    let imports = {
        let mut store = store.borrow_mut();
        vp_imports(&mut *store, env.clone())
    };

    run_vp(
        store,
        module,
        imports,
        &vp_code_hash,
        batched_tx,
        address,
        keys_changed,
        verifiers,
        yielded_value_borrow,
        |guest_memory| env.memory.init_from(guest_memory),
    )
}

#[allow(clippy::too_many_arguments)]
fn run_vp<F>(
    store: Rc<RefCell<wasmer::Store>>,
    module: wasmer::Module,
    vp_imports: wasmer::Imports,
    vp_code_hash: &Hash,
    input_data: &BatchedTxRef<'_>,
    address: &Address,
    keys_changed: &BTreeSet<Key>,
    verifiers: &BTreeSet<Address>,
    yielded_value: HostRef<RwAccess, Option<Vec<u8>>>,
    mut init_memory_callback: F,
) -> Result<()>
where
    F: FnMut(&wasmer::Memory),
{
    let input: VpInput<'_> = VpInput {
        addr: address,
        data: input_data,
        keys_changed,
        verifiers,
    };

    // Instantiate the wasm module
    let instance = {
        let mut store = store.borrow_mut();
        wasmer::Instance::new(&mut *store, &module, &vp_imports)
            .map_err(|e| Error::InstantiationError(Box::new(e)))?
    };

    // Fetch guest's main memory
    let guest_memory = instance
        .exports
        .get_memory("memory")
        .map_err(Error::MissingModuleMemory)?;

    init_memory_callback(guest_memory);

    // Write the inputs in the memory exported from the wasm
    // module
    let memory::VpCallInput {
        addr_ptr,
        addr_len,
        data_ptr,
        data_len,
        keys_changed_ptr,
        keys_changed_len,
        verifiers_ptr,
        verifiers_len,
    } = {
        let mut store = store.borrow_mut();
        memory::write_vp_inputs(&mut *store, guest_memory, input)
            .map_err(Error::MemoryError)?
    };

    // Get the module's entrypoint to be called
    let validate_tx = {
        let store = store.borrow();
        instance
            .exports
            .get_function(VP_ENTRYPOINT)
            .map_err(Error::MissingModuleEntrypoint)?
            .typed::<(u64, u64, u64, u64, u64, u64, u64, u64), u64>(&*store)
            .map_err(|error| Error::UnexpectedModuleEntrypointInterface {
                entrypoint: VP_ENTRYPOINT,
                error,
            })?
    };
    let is_valid = validate_tx
        .call(
            unsafe { &mut *RefCell::as_ptr(&*store) },
            addr_ptr,
            addr_len,
            data_ptr,
            data_len,
            keys_changed_ptr,
            keys_changed_len,
            verifiers_ptr,
            verifiers_len,
        )
        .map_err(|rt_error| {
            let downcasted_err = || {
                let source_err = rt_error.source()?;
                let downcasted_vp_err =
                    source_err.downcast_ref::<vp_host_fns::Error>()?;
                let downcasted_vp_rt_err = downcasted_vp_err
                    .downcast_ref::<vp_host_fns::RuntimeError>(
                )?;

                match downcasted_vp_rt_err {
                    vp_host_fns::RuntimeError::OutOfGas(_) => {
                        Some(Error::GasError(rt_error.to_string()))
                    }
                    vp_host_fns::RuntimeError::InvalidSectionSignature(_) => {
                        Some(Error::InvalidSectionSignature(
                            rt_error.to_string(),
                        ))
                    }
                    _ => None,
                }
            };
            downcasted_err().unwrap_or(Error::RuntimeError(rt_error))
        })?;
    tracing::debug!(
        is_valid,
        %vp_code_hash,
        "wasm vp"
    );

    // NB: early drop this data to avoid memory errors
    _ = (instance, vp_imports);

    if is_valid == 1 {
        let store = Rc::into_inner(store)
            .expect("The store must be dropped after execution to avoid leaks");
        let _store = RefCell::into_inner(store);
        Ok(())
    } else {
        unsafe { yielded_value.get_mut() }.take().map_or_else(
            || Err(Error::VpError(VpError::Unspecified)),
            |borsh_encoded_err| {
                let vp_err = VpError::try_from_slice(&borsh_encoded_err)
                    .map_err(|e| Error::ConversionError(e.to_string()))?;
                Err(Error::VpError(vp_err))
            },
        )
    }
}

/// Validity predicate wasm evaluator for `eval` host function calls.
#[derive(Default, Debug)]
pub struct VpEvalWasm<D, H, CA>
where
    D: DB + for<'iter> DBIter<'iter> + 'static,
    H: StorageHasher + 'static,
    CA: WasmCacheAccess + 'static,
{
    /// Phantom type for DB
    pub db: PhantomData<*const D>,
    /// Phantom type for hasher
    pub hasher: PhantomData<*const H>,
    /// Phantom type for WASM compilation cache access
    pub cache_access: PhantomData<*const CA>,
}

impl<'a, S, CA> namada_vp::native_vp::VpEvaluator<'a, S, VpCache<CA>, Self>
    for VpEvalWasm<<S as StateRead>::D, <S as StateRead>::H, CA>
where
    S: 'static + StateRead,
    CA: WasmCacheAccess,
{
    fn eval(
        ctx: &namada_vp::native_vp::Ctx<'a, S, VpCache<CA>, Self>,
        vp_code_hash: Hash,
        input_data: BatchedTxRef<'_>,
    ) -> namada_state::Result<()> {
        use namada_state::ResultExt;

        let eval_runner =
            VpEvalWasm::<<S as StateRead>::D, <S as StateRead>::H, CA> {
                db: PhantomData,
                hasher: PhantomData,
                cache_access: PhantomData,
            };
        let mut iterators: PrefixIterators<'_, <S as StateRead>::D> =
            PrefixIterators::default();
        let mut result_buffer: Option<Vec<u8>> = None;
        let mut yielded_value: Option<Vec<u8>> = None;
        let mut vp_wasm_cache = ctx.vp_wasm_cache.clone();

        let ctx = VpCtx::new(
            ctx.address,
            ctx.state.write_log(),
            ctx.state.in_mem(),
            ctx.state.db(),
            ctx.gas_meter,
            ctx.tx,
            ctx.cmt,
            ctx.tx_index,
            &mut iterators,
            ctx.verifiers,
            &mut result_buffer,
            &mut yielded_value,
            ctx.keys_changed,
            &eval_runner,
            &mut vp_wasm_cache,
        );
        eval_runner
            .eval_native_result(ctx, vp_code_hash, input_data)
            .inspect_err(|err| {
                tracing::warn!("VP eval from a native VP failed with: {err}");
            })
            .into_storage_result()
    }
}

impl<D, H, CA> VpEvaluator for VpEvalWasm<D, H, CA>
where
    D: DB + for<'iter> DBIter<'iter> + 'static,
    H: StorageHasher + 'static,
    CA: WasmCacheAccess + 'static,
{
    type CA = CA;
    type Db = D;
    type Eval = Self;
    type H = H;

    fn eval(
        &self,
        ctx: VpCtx<D, H, Self, CA>,
        vp_code_hash: Hash,
        input_data: BatchedTxRef<'_>,
    ) -> HostEnvResult {
        self.eval_native_result(ctx, vp_code_hash, input_data)
            .map_or_else(
                |err| {
                    tracing::warn!("VP eval error {err}");
                    HostEnvResult::Fail
                },
                |()| HostEnvResult::Success,
            )
    }
}

impl<D, H, CA> VpEvalWasm<D, H, CA>
where
    D: DB + for<'iter> DBIter<'iter> + 'static,
    H: StorageHasher + 'static,
    CA: WasmCacheAccess + 'static,
{
    /// Evaluate the given VP.
    pub fn eval_native_result(
        &self,
        ctx: VpCtx<D, H, Self, CA>,
        vp_code_hash: Hash,
        input_data: BatchedTxRef<'_>,
    ) -> Result<()> {
        let address = unsafe { ctx.address.get() };
        let keys_changed = unsafe { ctx.keys_changed.get() };
        let verifiers = unsafe { ctx.verifiers.get() };
        let vp_wasm_cache = unsafe { ctx.vp_wasm_cache.get_mut() };
        let gas_meter = unsafe { ctx.gas_meter.get() };

        // Compile the wasm module
        let (module, store) = fetch_or_compile(
            vp_wasm_cache,
            &Commitment::Hash(vp_code_hash),
            &ctx.state(),
            gas_meter,
        )?;
        let store = Rc::new(RefCell::new(store));

        let mut env = VpVmEnv {
            memory: WasmMemory::new(Rc::downgrade(&store)),
            ctx,
        };
        let yielded_value_borrow = env.ctx.yielded_value;
        let imports = {
            let mut store = store.borrow_mut();
            vp_imports(&mut *store, env.clone())
        };

        run_vp(
            store,
            module,
            imports,
            &vp_code_hash,
            &input_data,
            address,
            keys_changed,
            verifiers,
            yielded_value_borrow,
            |guest_memory| env.memory.init_from(guest_memory),
        )
    }
}

/// Prepare a wasm store for untrusted code.
pub fn untrusted_wasm_store(limit: Limit<BaseTunables>) -> wasmer::Store {
    // Use Singlepass compiler with the default settings
    let compiler = wasmer_compiler_singlepass::Singlepass::default();
    let mut engine = <Engine as NativeEngineExt>::new(
        Box::new(compiler),
        // NB: The default target corresponds to the host's triplet
        Target::default(),
        // NB: WASM features are validated via `validate_untrusted_wasm`,
        // so we can use the default features here
        Features::default(),
    );
    engine.set_tunables(limit);
    wasmer::Store::new(engine)
}

/// Inject gas counter and stack-height limiter into the given wasm code
pub fn prepare_wasm_code<T: AsRef<[u8]>>(code: T) -> Result<Vec<u8>> {
    let module: elements::Module = elements::deserialize_buffer(code.as_ref())
        .map_err(Error::DeserializationError)?;
    let module = wasm_instrument::gas_metering::inject(
        module,
        wasm_instrument::gas_metering::host_function::Injector::new(
            "env", "gas",
        ),
        &GasRules,
    )
    .map_err(|_original_module| Error::GasMeterInjection)?;
    let module =
        wasm_instrument::inject_stack_limiter(module, WASM_STACK_LIMIT)
            .map_err(|_original_module| Error::StackLimiterInjection)?;
    elements::serialize(module).map_err(Error::SerializationError)
}

// Fetch or compile a WASM code from the cache or storage. Account for the
// loading and code compilation gas costs.
fn fetch_or_compile<S, CN, CA>(
    wasm_cache: &mut Cache<CN, CA>,
    code_or_hash: &Commitment,
    state: &S,
    gas_meter: &RefCell<impl GasMetering>,
) -> Result<(Module, Store)>
where
    S: StateRead,
    CN: 'static + CacheName,
    CA: 'static + WasmCacheAccess,
{
    match code_or_hash {
        Commitment::Hash(code_hash) => {
            let code_len_key = Key::wasm_code_len(code_hash);
            let tx_len = state
                .read::<u64>(&code_len_key)
                .map_err(|e| {
                    Error::LoadWasmCode(format!(
                        "Read wasm code length failed: key {code_len_key}, \
                         error {e}"
                    ))
                })?
                .ok_or_else(|| {
                    Error::LoadWasmCode(format!(
                        "No wasm code length in storage: key {code_len_key}"
                    ))
                })?;

            // Gas accounting in any case, even if the compiled module is in
            // cache
            gas_meter
                .borrow_mut()
                .add_wasm_load_from_storage_gas(tx_len)
                .map_err(|e| Error::GasError(e.to_string()))?;
            gas_meter
                .borrow_mut()
                .add_compiling_gas(tx_len)
                .map_err(|e| Error::GasError(e.to_string()))?;

            let (module, store) = match wasm_cache.fetch(code_hash)? {
                Some((module, store)) => (module, store),
                None => {
                    let key = Key::wasm_code(code_hash);
                    let code = state
                        .read::<Vec<u8>>(&key)
                        .map_err(|e| {
                            Error::LoadWasmCode(format!(
                                "Read wasm code failed: key {key}, error {e}"
                            ))
                        })?
                        .ok_or_else(|| {
                            Error::LoadWasmCode(format!(
                                "No wasm code in storage: key {key}"
                            ))
                        })?;

                    match wasm_cache.compile_or_fetch(code)? {
                        Some((module, store)) => (module, store),
                        None => return Err(Error::NoCompiledWasmCode),
                    }
                }
            };

            Ok((module, store))
        }
        Commitment::Id(code) => {
            let tx_len = code.len() as u64;
            gas_meter
                .borrow_mut()
                .add_wasm_validation_gas(tx_len)
                .map_err(|e| Error::GasError(e.to_string()))?;
            // Validation is only needed for governance proposals. The other
            // transactions are subject to the allowlist and are guaranteed to
            // not contain invalid opcodes.
            validate_untrusted_wasm(code).map_err(Error::ValidationError)?;

            gas_meter
                .borrow_mut()
                .add_compiling_gas(tx_len)
                .map_err(|e| Error::GasError(e.to_string()))?;
            match wasm_cache.compile_or_fetch(code)? {
                Some((module, store)) => Ok((module, store)),
                None => Err(Error::NoCompiledWasmCode),
            }
        }
    }
}

struct GasRules;

impl wasm_instrument::gas_metering::Rules for GasRules {
    fn instruction_cost(
        &self,
        instruction: &wasm_instrument::parity_wasm::elements::Instruction,
    ) -> Option<u32> {
        // NOTE: costs set to 0 don't actually trigger the injection of a call
        // to the gas host function (no useless instructions are
        // injected)
        // NOTE: these costs are taken from the benchmarks crate. None of them
        // should be zero
        let gas = match instruction {
            // NOTE: the real cost of this operation is 57_330 but because of
            // the behavior of the instrumentaiton tools which doesn't account
            // for traps in called functions we need to reduce it to 1 otherwise
            // the gas costs explode
            Unreachable => 1,
            // Just a label, aribitrary cost of 1
            End => 1,
            // Just a label, aribitrary cost of 1
            Else => 1,
            Nop => 1,
            // Just a label, cost of 1
            Block(_) => 1,
            // Just a label, cost of 1
            Loop(_) => 1,
            If(_) => 5,
            Br(_) => 14,
            BrIf(_) => 14,
            BrTable(_) => 56,
            Return => 4,
            Call(_) => 16,
            CallIndirect(_, _) => 28,
            Drop => 1,
            Select => 11,
            GetLocal(_) => 1,
            SetLocal(_) => 2,
            TeeLocal(_) => 2,
            GetGlobal(_) => 4,
            SetGlobal(_) => 5,
            I32Load(_, _) => 8,
            I64Load(_, _) => 8,
            F32Load(_, _) => 9,
            F64Load(_, _) => 9,
            I32Load8S(_, _) => 8,
            I32Load8U(_, _) => 8,
            I32Load16S(_, _) => 8,
            I32Load16U(_, _) => 8,
            I64Load8S(_, _) => 8,
            I64Load8U(_, _) => 8,
            I64Load16S(_, _) => 8,
            I64Load16U(_, _) => 8,
            I64Load32S(_, _) => 7,
            I64Load32U(_, _) => 7,
            I32Store(_, _) => 8,
            I64Store(_, _) => 9,
            F32Store(_, _) => 8,
            F64Store(_, _) => 9,
            I32Store8(_, _) => 7,
            I32Store16(_, _) => 13,
            I64Store8(_, _) => 7,
            I64Store16(_, _) => 12,
            I64Store32(_, _) => 8,
            CurrentMemory(_) => 110,
            GrowMemory(_) => 194,
            I32Const(_) => 1,
            I64Const(_) => 1,
            F32Const(_) => 1,
            F64Const(_) => 1,
            I32Eqz => 6,
            I32Eq => 6,
            I32Ne => 6,
            I32LtS => 6,
            I32LtU => 6,
            I32GtS => 6,
            I32GtU => 6,
            I32LeS => 6,
            I32LeU => 6,
            I32GeS => 6,
            I32GeU => 6,
            I64Eqz => 8,
            I64Eq => 8,
            I64Ne => 8,
            I64LtS => 8,
            I64LtU => 8,
            I64GtS => 8,
            I64GtU => 8,
            I64LeS => 8,
            I64LeU => 8,
            I64GeS => 8,
            I64GeU => 8,
            F32Eq => 10,
            F32Ne => 10,
            F32Lt => 10,
            F32Gt => 9,
            F32Le => 10,
            F32Ge => 10,
            F64Eq => 11,
            F64Ne => 11,
            F64Lt => 11,
            F64Gt => 12,
            F64Le => 11,
            F64Ge => 11,
            I32Clz => 3,
            I32Ctz => 3,
            I32Popcnt => 3,
            I32Add => 4,
            I32Sub => 4,
            I32Mul => 6,
            I32DivS => 18,
            I32DivU => 18,
            I32RemS => 18,
            I32RemU => 18,
            I32And => 4,
            I32Or => 4,
            I32Xor => 4,
            I32Shl => 4,
            I32ShrS => 4,
            I32ShrU => 4,
            I32Rotl => 4,
            I32Rotr => 4,
            I64Clz => 4,
            I64Ctz => 4,
            I64Popcnt => 4,
            I64Add => 7,
            I64Sub => 7,
            I64Mul => 8,
            I64DivS => 30,
            I64DivU => 30,
            I64RemS => 31,
            I64RemU => 30,
            I64And => 7,
            I64Or => 7,
            I64Xor => 7,
            I64Shl => 6,
            I64ShrS => 6,
            I64ShrU => 6,
            I64Rotl => 6,
            I64Rotr => 6,
            F32Abs => 5,
            F32Neg => 4,
            F32Ceil => 7,
            F32Floor => 7,
            F32Trunc => 7,
            F32Nearest => 7,
            F32Sqrt => 10,
            F32Add => 7,
            F32Sub => 7,
            F32Mul => 7,
            F32Div => 10,
            F32Min => 21,
            F32Max => 19,
            F32Copysign => 9,
            F64Abs => 7,
            F64Neg => 5,
            F64Ceil => 9,
            F64Floor => 9,
            F64Trunc => 9,
            F64Nearest => 9,
            F64Sqrt => 19,
            F64Add => 9,
            F64Sub => 9,
            F64Mul => 9,
            F64Div => 12,
            F64Min => 24,
            F64Max => 31,
            F64Copysign => 13,
            I32WrapI64 => 2,
            I32TruncSF32 => 24,
            I32TruncUF32 => 25,
            I32TruncSF64 => 28,
            I32TruncUF64 => 27,
            I64ExtendSI32 => 3,
            I64ExtendUI32 => 2,
            I64TruncSF32 => 24,
            I64TruncUF32 => 39,
            I64TruncSF64 => 27,
            I64TruncUF64 => 46,
            F32ConvertSI32 => 12,
            F32ConvertUI32 => 6,
            F32ConvertSI64 => 6,
            F32ConvertUI64 => 12,
            F32DemoteF64 => 9,
            F64ConvertSI32 => 12,
            F64ConvertUI32 => 12,
            F64ConvertSI64 => 12,
            F64ConvertUI64 => 12,
            F64PromoteF32 => 9,
            I32ReinterpretF32 => 2,
            I64ReinterpretF64 => 3,
            F32ReinterpretI32 => 3,
            F64ReinterpretI64 => 4,
            SignExt(SignExtInstruction::I32Extend8S) => 1,
            SignExt(SignExtInstruction::I32Extend16S) => 1,
            SignExt(SignExtInstruction::I64Extend8S) => 1,
            SignExt(SignExtInstruction::I64Extend16S) => 1,
            SignExt(SignExtInstruction::I64Extend32S) => 1,
        };

        // We always return a cost, forbidden instructions should be rejected at
        // validation time not here
        Some(gas)
    }

    fn memory_grow_cost(
        &self,
    ) -> wasm_instrument::gas_metering::MemoryGrowCost {
        wasm_instrument::gas_metering::MemoryGrowCost::Linear(
            NonZeroU32::new(WASM_MEMORY_PAGE_GAS)
                .expect("Memory grow gas cost should be non-zero"),
        )
    }

    fn call_per_local_cost(&self) -> u32 {
        0
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::error::Error as StdErrorTrait;

    use assert_matches::assert_matches;
    use itertools::Either;
    use namada_core::arith::checked;
    use namada_core::borsh::BorshSerializeExt;
    use namada_state::testing::TestState;
    use namada_state::StorageWrite;
    use namada_test_utils::TestWasms;
    use namada_token::DenominatedAmount;
    use namada_tx::data::eval_vp::EvalVp;
    use namada_tx::data::{Fee, TxType};
    use namada_tx::{Code, Data};
    use test_log::test;
    use wasmer::WASM_PAGE_SIZE;
    use wasmer_vm::TrapCode;

    use super::memory::{TX_MEMORY_INIT_PAGES, VP_MEMORY_INIT_PAGES};
    use super::*;
    use crate::host_env::{self, TxRuntimeError};
    use crate::wasm;

    const TX_GAS_LIMIT: u64 = 10_000_000_000_000;
    const OUT_OF_GAS_LIMIT: u64 = 10_000;
    const GAS_SCALE: u64 = 1;

    /// Test that we sanitize accesses to invalid addresses in wasm memory.
    #[test]
    fn test_tx_sanitize_invalid_addrs() {
        let tx_code = wasmer::wat2wasm(
            r#"
            (module
                (import "env" "namada_tx_read" (func (param i64 i64) (result i64)))
                (func (param i64 i64) (result i64)
                    i64.const 18446744073709551615
                    i64.const 1
                    (call 0)
                )
                (memory 16)
                (export "memory" (memory 0))
                (export "_apply_tx" (func 1))
            )
            "#
            .as_bytes(),
        )
        .expect("unexpected error converting wat2wasm")
        .into_owned();

        const PANIC_MSG: &str =
            "Test should have failed with a wasm runtime memory error";

        let error = execute_tx_with_code(&tx_code).expect_err(PANIC_MSG);
        assert!(
            matches!(
                assert_tx_rt_mem_error(&error, PANIC_MSG),
                memory::Error::OverflowingOffset(18446744073709551615, 1),
            ),
            "{PANIC_MSG}"
        );
    }

    /// Extract a tx wasm runtime memory error from some [`Error`].
    fn assert_tx_rt_mem_error<'err>(
        error: &'err Error,
        assert_msg: &str,
    ) -> &'err memory::Error {
        let Error::RuntimeError(rt_error) = error else {
            panic!("{assert_msg}: {error}");
        };
        let source_err =
            rt_error.source().expect("No runtime error source found");
        let downcasted_tx_err: &host_env::Error = source_err
            .downcast_ref()
            .unwrap_or_else(|| panic!("{assert_msg}: {source_err}"));
        let downcasted_tx_rt_err: &TxRuntimeError = downcasted_tx_err
            .downcast_ref()
            .unwrap_or_else(|| panic!("{assert_msg}: {source_err}"));
        let TxRuntimeError::MemoryError(tx_mem_err) = downcasted_tx_rt_err
        else {
            panic!("{assert_msg}: {downcasted_tx_rt_err}");
        };
        tx_mem_err
            .downcast_ref()
            .unwrap_or_else(|| panic!("{assert_msg}: {tx_mem_err}"))
    }

    /// Extract a vp wasm runtime memory error from some [`Error`].
    fn assert_vp_rt_mem_error<'err>(
        error: &'err Error,
        assert_msg: &str,
    ) -> &'err memory::Error {
        let Error::RuntimeError(rt_error) = error else {
            panic!("{assert_msg}: {error}");
        };
        let source_err =
            rt_error.source().expect("No runtime error source found");
        let downcasted_vp_err: &host_env::Error = source_err
            .downcast_ref()
            .unwrap_or_else(|| panic!("{assert_msg}: {source_err}"));
        let downcasted_err: &wasm::memory::Error = downcasted_vp_err
            .downcast_ref()
            .unwrap_or_else(|| panic!("{assert_msg}: {downcasted_vp_err}"));
        downcasted_err
    }

    /// Test that when a transaction wasm goes over the stack-height limit, the
    /// execution is aborted.
    #[test]
    // NB: Disabled on aarch64 macOS since a fix for
    // https://github.com/wasmerio/wasmer/issues/4072
    // reduced the available stack space on mac
    #[cfg_attr(all(target_arch = "aarch64", target_os = "macos"), ignore)]
    fn test_tx_stack_limiter() {
        // Because each call into `$loop` inside the wasm consumes 5 stack
        // heights except for the terminal call, this should hit the stack
        // limit.
        let loops = WASM_STACK_LIMIT / 5 - 1;

        let error = loop_in_tx_wasm(loops).expect_err(&format!(
            "Expecting runtime error \"unreachable\" caused by stack-height \
             overflow, loops {}. Got",
            loops,
        ));
        assert_stack_overflow(&error);

        // one less loop shouldn't go over the limit
        let result = loop_in_tx_wasm(loops - 1);
        assert!(result.is_ok(), "Expected success. Got {:?}", result);
    }

    /// Test that when a VP wasm goes over the stack-height limit, the execution
    /// is aborted.
    #[test]
    // NB: Disabled on aarch64 macOS since a fix for
    // https://github.com/wasmerio/wasmer/issues/4072
    // reduced the available stack space on mac
    #[cfg_attr(all(target_arch = "aarch64", target_os = "macos"), ignore)]
    fn test_vp_stack_limiter() {
        // Because each call into `$loop` inside the wasm consumes 5 stack
        // heights except for the terminal call, this should hit the stack
        // limit.
        let loops = WASM_STACK_LIMIT / 5 - 1;

        let error = loop_in_vp_wasm(loops).expect_err(
            "Expecting runtime error caused by stack-height overflow. Got",
        );
        assert_stack_overflow(&error);

        // one less loop shouldn't go over the limit
        let result = loop_in_vp_wasm(loops - 1);
        assert!(result.is_ok(), "Expected success. Got {:?}", result);
    }

    /// Test that when a transaction wasm goes over the memory limit inside the
    /// wasm execution, the execution is aborted.
    #[test]
    fn test_tx_memory_limiter_in_guest() {
        let mut state = TestState::default();
        let gas_meter = RefCell::new(TxGasMeter::new(TX_GAS_LIMIT, GAS_SCALE));
        let tx_index = TxIndex::default();

        // This code will allocate memory of the given size
        let tx_code = TestWasms::TxMemoryLimit.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&tx_code);
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        let code_len = (tx_code.len() as u64).serialize_to_vec();
        let _ = state.write_log_mut().write(&key, tx_code.clone()).unwrap();
        let _ = state.write_log_mut().write(&len_key, code_len).unwrap();

        // Assuming 200 pages, 12.8 MiB limit
        assert_eq!(memory::TX_MEMORY_MAX_PAGES, 200);

        // Allocating `2^23` (8 MiB) should be below the memory limit and
        // shouldn't fail
        let tx_data = 2_usize.pow(23).serialize_to_vec();
        let (mut vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        let (mut tx_cache, _) =
            wasm::compilation_cache::common::testing::tx_cache();
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.set_code(Code::new(tx_code.clone(), None));
        outer_tx.set_data(Data::new(tx_data));
        let batched_tx = outer_tx.batch_ref_first_tx().unwrap();
        let result = tx(
            &mut state,
            &gas_meter,
            &tx_index,
            batched_tx.tx,
            batched_tx.cmt,
            &mut vp_cache,
            &mut tx_cache,
        );
        assert!(result.is_ok(), "Expected success, got {:?}", result);

        // Allocating `2^24` (16 MiB) should be above the memory limit and
        // should fail
        let tx_data = 2_usize.pow(24).serialize_to_vec();
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.set_code(Code::new(tx_code, None));
        outer_tx.set_data(Data::new(tx_data));
        let batched_tx = outer_tx.batch_ref_first_tx().unwrap();
        let error = tx(
            &mut state,
            &gas_meter,
            &tx_index,
            batched_tx.tx,
            batched_tx.cmt,
            &mut vp_cache,
            &mut tx_cache,
        )
        .expect_err("Expected to run out of memory");

        assert_stack_overflow(&error);
    }

    /// Test that when a validity predicate wasm goes over the memory limit
    /// inside the wasm execution when calling `eval` host function, the `eval`
    /// fails and hence returns `false`.
    #[test]
    fn test_vp_memory_limiter_in_guest_calling_eval() {
        let mut state = TestState::default();
        let addr = state.in_mem_mut().address_gen.generate_address("rng seed");
        let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
            &TxGasMeter::new(TX_GAS_LIMIT, GAS_SCALE),
        ));
        let keys_changed = BTreeSet::new();
        let verifiers = BTreeSet::new();
        let tx_index = TxIndex::default();

        // This code will call `eval` with the other VP below
        let vp_eval = TestWasms::VpEval.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&vp_eval);
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        let code_len = vp_eval.len() as u64;
        state.write(&key, vp_eval).unwrap();
        state.write(&len_key, code_len).unwrap();
        // This code will allocate memory of the given size
        let vp_memory_limit = TestWasms::VpMemoryLimit.read_bytes();
        // store the wasm code
        let limit_code_hash = Hash::sha256(&vp_memory_limit);
        let key = Key::wasm_code(&limit_code_hash);
        let len_key = Key::wasm_code_len(&limit_code_hash);
        let code_len = vp_memory_limit.len() as u64;
        state.write(&key, vp_memory_limit).unwrap();
        state.write(&len_key, code_len).unwrap();

        // Assuming 200 pages, 12.8 MiB limit
        assert_eq!(memory::VP_MEMORY_MAX_PAGES, 200);

        // Allocating `2^23` (8 MiB) should be below the memory limit and
        // shouldn't fail
        let input = 2_usize.pow(23).serialize_to_vec();

        let mut tx = Tx::new(state.in_mem().chain_id.clone(), None);
        tx.add_code(vec![], None).add_serialized_data(input);

        let eval_vp = EvalVp {
            vp_code_hash: limit_code_hash,
            input: tx.batch_first_tx(),
        };

        let mut outer_tx = Tx::new(state.in_mem().chain_id.clone(), None);
        outer_tx.add_code(vec![], None).add_data(eval_vp);

        let (vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        // When the `eval`ed VP doesn't run out of memory, it should return
        // `true`
        assert!(
            vp(
                code_hash,
                &outer_tx.batch_ref_first_tx().unwrap(),
                &tx_index,
                &addr,
                &state,
                &gas_meter,
                &keys_changed,
                &verifiers,
                vp_cache.clone(),
            )
            .is_ok()
        );

        // Allocating `2^24` (16 MiB) should be above the memory limit and
        // should fail
        let input = 2_usize.pow(24).serialize_to_vec();
        let mut tx = Tx::new(state.in_mem().chain_id.clone(), None);
        tx.add_code(vec![], None).add_data(input);

        let eval_vp = EvalVp {
            vp_code_hash: limit_code_hash,
            input: tx.batch_first_tx(),
        };

        let mut outer_tx = Tx::new(state.in_mem().chain_id.clone(), None);
        outer_tx.add_code(vec![], None).add_data(eval_vp);

        // When the `eval`ed VP runs out of memory, its result should be
        // `false`, hence we should also get back `false` from the VP that
        // called `eval`.
        assert!(
            vp(
                code_hash,
                &outer_tx.batch_ref_first_tx().unwrap(),
                &tx_index,
                &addr,
                &state,
                &gas_meter,
                &keys_changed,
                &verifiers,
                vp_cache,
            )
            .is_err()
        );
    }

    /// Test that when a validity predicate wasm goes over the memory limit
    /// inside the wasm execution, the execution is aborted.
    #[test]
    fn test_vp_memory_limiter_in_guest() {
        let mut state = TestState::default();
        let addr = state.in_mem_mut().address_gen.generate_address("rng seed");
        let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
            &TxGasMeter::new(TX_GAS_LIMIT, GAS_SCALE),
        ));
        let keys_changed = BTreeSet::new();
        let verifiers = BTreeSet::new();
        let tx_index = TxIndex::default();

        // This code will allocate memory of the given size
        let vp_code = TestWasms::VpMemoryLimit.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&vp_code);
        let code_len = vp_code.len() as u64;
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        state.write(&key, vp_code).unwrap();
        state.write(&len_key, code_len).unwrap();

        // Assuming 200 pages, 12.8 MiB limit
        assert_eq!(memory::VP_MEMORY_MAX_PAGES, 200);

        // Allocating `2^23` (8 MiB) should be below the memory limit and
        // shouldn't fail
        let tx_data = 2_usize.pow(23).serialize_to_vec();
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.header.chain_id = state.in_mem().chain_id.clone();
        outer_tx.set_data(Data::new(tx_data));
        outer_tx.set_code(Code::new(vec![], None));
        let (vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        let result = vp(
            code_hash,
            &outer_tx.batch_ref_first_tx().unwrap(),
            &tx_index,
            &addr,
            &state,
            &gas_meter,
            &keys_changed,
            &verifiers,
            vp_cache.clone(),
        );
        assert!(result.is_ok(), "Expected success, got {:?}", result);

        // Allocating `2^24` (16 MiB) should be above the memory limit and
        // should fail
        let tx_data = 2_usize.pow(24).serialize_to_vec();
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.header.chain_id = state.in_mem().chain_id.clone();
        outer_tx.set_data(Data::new(tx_data));
        let error = vp(
            code_hash,
            &outer_tx.batch_ref_first_tx().unwrap(),
            &tx_index,
            &addr,
            &state,
            &gas_meter,
            &keys_changed,
            &verifiers,
            vp_cache,
        )
        .expect_err("Expected to run out of memory");

        assert_stack_overflow(&error);
    }

    /// Test that when a transaction wasm goes over the wasm memory limit in the
    /// host input, the execution fails.
    #[test]
    fn test_tx_memory_limiter_in_host_input() {
        let mut state = TestState::default();
        let gas_meter = RefCell::new(TxGasMeter::new(TX_GAS_LIMIT, GAS_SCALE));
        let tx_index = TxIndex::default();

        let tx_no_op = TestWasms::TxNoOp.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&tx_no_op);
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        let code_len = (tx_no_op.len() as u64).serialize_to_vec();
        let _ = state
            .write_log_mut()
            .write(&key, tx_no_op.serialize_to_vec())
            .unwrap();
        let _ = state.write_log_mut().write(&len_key, code_len).unwrap();

        // Assuming 200 pages, 12.8 MiB limit
        assert_eq!(memory::TX_MEMORY_MAX_PAGES, 200);

        // Allocating `2^24` (16 MiB) for the input should be above the memory
        // limit and should fail
        let len = 2_usize.pow(24);
        let tx_data: Vec<u8> = vec![6_u8; len];
        let (mut vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        let (mut tx_cache, _) =
            wasm::compilation_cache::common::testing::tx_cache();
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.set_code(Code::new(tx_no_op, None));
        outer_tx.set_data(Data::new(tx_data));
        let batched_tx = outer_tx.batch_ref_first_tx().unwrap();
        let result = tx(
            &mut state,
            &gas_meter,
            &tx_index,
            batched_tx.tx,
            batched_tx.cmt,
            &mut vp_cache,
            &mut tx_cache,
        );
        // Depending on platform, we get a different error from the running out
        // of memory
        match result {
            // Dylib engine error (used anywhere except mac)
            Err(Error::MemoryError(memory::Error::Grow(
                wasmer::MemoryError::CouldNotGrow { .. },
            ))) => {}
            Err(error) => {
                let trap_code = get_trap_code(&error);
                // Universal engine error (currently used on mac)
                assert_eq!(
                    trap_code,
                    Either::Left(wasmer_vm::TrapCode::HeapAccessOutOfBounds)
                );
            }
            _ => panic!("Expected to run out of memory, got {:?}", result),
        }
    }

    /// Test that when a validity predicate wasm goes over the wasm memory limit
    /// in the host input, the execution fails.
    #[test]
    fn test_vp_memory_limiter_in_host_input() {
        let mut state = TestState::default();
        let addr = state.in_mem_mut().address_gen.generate_address("rng seed");
        let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
            &TxGasMeter::new(TX_GAS_LIMIT, GAS_SCALE),
        ));
        let keys_changed = BTreeSet::new();
        let verifiers = BTreeSet::new();
        let tx_index = TxIndex::default();

        let vp_code = TestWasms::VpAlwaysTrue.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&vp_code);
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        let code_len = vp_code.len() as u64;
        state.write(&key, vp_code).unwrap();
        state.write(&len_key, code_len).unwrap();

        // Assuming 200 pages, 12.8 MiB limit
        assert_eq!(memory::VP_MEMORY_MAX_PAGES, 200);

        // Allocating `2^24` (16 MiB) for the input should be above the memory
        // limit and should fail
        let len = 2_usize.pow(24);
        let tx_data: Vec<u8> = vec![6_u8; len];
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.header.chain_id = state.in_mem().chain_id.clone();
        outer_tx.set_data(Data::new(tx_data));
        outer_tx.set_code(Code::new(vec![], None));
        let (vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        let result = vp(
            code_hash,
            &outer_tx.batch_ref_first_tx().unwrap(),
            &tx_index,
            &addr,
            &state,
            &gas_meter,
            &keys_changed,
            &verifiers,
            vp_cache,
        );
        // Depending on platform, we get a different error from the running out
        // of memory
        match result {
            // Dylib engine error (used anywhere except mac)
            Err(Error::MemoryError(memory::Error::Grow(
                wasmer::MemoryError::CouldNotGrow { .. },
            ))) => {
                // as expected
            }
            Err(error) => {
                let trap_code = get_trap_code(&error);
                // Universal engine error (currently used on mac)
                assert_eq!(
                    trap_code,
                    Either::Left(wasmer_vm::TrapCode::HeapAccessOutOfBounds)
                );
            }
            _ => panic!("Expected to run out of memory, got {:?}", result),
        }
    }

    /// Test that when a transaction wasm goes over the wasm memory limit in the
    /// value returned from host environment call during wasm execution, the
    /// execution is aborted.
    #[test]
    fn test_tx_memory_limiter_in_host_env() {
        let mut state = TestState::default();
        let gas_meter = RefCell::new(TxGasMeter::new(TX_GAS_LIMIT, GAS_SCALE));
        let tx_index = TxIndex::default();

        let tx_read_key = TestWasms::TxReadStorageKey.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&tx_read_key);
        let code_len = (tx_read_key.len() as u64).serialize_to_vec();
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        let _ = state
            .write_log_mut()
            .write(&key, tx_read_key.clone())
            .unwrap();
        let _ = state.write_log_mut().write(&len_key, code_len).unwrap();

        // Allocating `2^24` (16 MiB) for a value in storage that the tx
        // attempts to read should be above the memory limit and should
        // fail
        let len = 2_usize.pow(24);
        let value: Vec<u8> = vec![6_u8; len];
        let key_raw = "key";
        let key = Key::parse(key_raw).unwrap();
        // Write the value that should be read by the tx into the storage. When
        // writing directly to storage, the value has to be encoded with
        // Borsh.
        state.write(&key, value).unwrap();
        let tx_data = key.serialize_to_vec();
        let (mut vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        let (mut tx_cache, _) =
            wasm::compilation_cache::common::testing::tx_cache();
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.set_code(Code::new(tx_read_key, None));
        outer_tx.set_data(Data::new(tx_data));
        let batched_tx = outer_tx.batch_ref_first_tx().unwrap();
        let error = tx(
            &mut state,
            &gas_meter,
            &tx_index,
            batched_tx.tx,
            batched_tx.cmt,
            &mut vp_cache,
            &mut tx_cache,
        )
        .expect_err("Expected to run out of memory");

        assert_stack_overflow(&error);
    }

    /// Test that when a validity predicate wasm goes over the wasm memory limit
    /// in the value returned from host environment call during wasm
    /// execution, the execution is aborted.
    #[test]
    fn test_vp_memory_limiter_in_host_env() {
        let mut state = TestState::default();
        let addr = state.in_mem_mut().address_gen.generate_address("rng seed");
        let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
            &TxGasMeter::new(TX_GAS_LIMIT, GAS_SCALE),
        ));
        let keys_changed = BTreeSet::new();
        let verifiers = BTreeSet::new();
        let tx_index = TxIndex::default();

        let vp_read_key = TestWasms::VpReadStorageKey.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&vp_read_key);
        let code_len = vp_read_key.len() as u64;
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        state.write(&key, vp_read_key).unwrap();
        state.write(&len_key, code_len).unwrap();

        // Allocating `2^24` (16 MiB) for a value in storage that the tx
        // attempts to read should be above the memory limit and should
        // fail
        let len = 2_usize.pow(24);
        let value: Vec<u8> = vec![6_u8; len];
        let key_raw = "key";
        let key = Key::parse(key_raw).unwrap();
        // Write the value that should be read by the tx into the storage. When
        // writing directly to storage, the value has to be encoded with
        // Borsh.
        state.write(&key, value).unwrap();
        let tx_data = key.serialize_to_vec();
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.header.chain_id = state.in_mem().chain_id.clone();
        outer_tx.set_data(Data::new(tx_data));
        outer_tx.set_code(Code::new(vec![], None));
        let (vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        let error = vp(
            code_hash,
            &outer_tx.batch_ref_first_tx().unwrap(),
            &tx_index,
            &addr,
            &state,
            &gas_meter,
            &keys_changed,
            &verifiers,
            vp_cache,
        )
        .expect_err("Expected to run out of memory");

        assert_stack_overflow(&error);
    }

    /// Test that when a validity predicate wasm goes over the wasm memory limit
    /// in the value returned from host environment call during wasm execution,
    /// inside the wasm execution calling `eval` host function, the `eval` fails
    /// and hence returns `false`.
    #[test]
    fn test_vp_memory_limiter_in_host_env_inside_guest_calling_eval() {
        let mut state = TestState::default();
        let addr = state.in_mem_mut().address_gen.generate_address("rng seed");
        let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
            &TxGasMeter::new(TX_GAS_LIMIT, GAS_SCALE),
        ));
        let keys_changed = BTreeSet::new();
        let verifiers = BTreeSet::new();
        let tx_index = TxIndex::default();

        // This code will call `eval` with the other VP below
        let vp_eval = TestWasms::VpEval.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&vp_eval);
        let code_len = (vp_eval.len() as u64).serialize_to_vec();
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        state.write(&key, vp_eval).unwrap();
        state.write(&len_key, code_len).unwrap();
        // This code will read value from the storage
        let vp_read_key = TestWasms::VpReadStorageKey.read_bytes();
        // store the wasm code
        let read_code_hash = Hash::sha256(&vp_read_key);
        let code_len = (vp_read_key.len() as u64).serialize_to_vec();
        let key = Key::wasm_code(&read_code_hash);
        let len_key = Key::wasm_code_len(&read_code_hash);
        state.write(&key, vp_read_key).unwrap();
        state.write(&len_key, code_len).unwrap();

        // Allocating `2^24` (16 MiB) for a value in storage that the tx
        // attempts to read should be above the memory limit and should
        // fail
        let len = 2_usize.pow(24);
        let value: Vec<u8> = vec![6_u8; len];
        let key_raw = "key";
        let key = Key::parse(key_raw).unwrap();
        // Write the value that should be read by the tx into the storage. When
        // writing directly to storage, the value has to be encoded with
        // Borsh.
        state.write(&key, value).unwrap();
        let input = 2_usize.pow(23).serialize_to_vec();

        let mut tx = Tx::new(state.in_mem().chain_id.clone(), None);
        tx.add_code(vec![], None).add_serialized_data(input);

        let eval_vp = EvalVp {
            vp_code_hash: read_code_hash,
            input: tx.batch_first_tx(),
        };

        let mut outer_tx = Tx::new(state.in_mem().chain_id.clone(), None);
        outer_tx.add_code(vec![], None).add_data(eval_vp);

        let (vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        assert!(
            vp(
                code_hash,
                &outer_tx.batch_ref_first_tx().unwrap(),
                &tx_index,
                &addr,
                &state,
                &gas_meter,
                &keys_changed,
                &verifiers,
                vp_cache,
            )
            .is_err()
        );
    }

    #[test]
    fn test_apply_wasm_tx_allowlist() {
        let mut state = TestState::default();

        let tx_read_key = TestWasms::TxReadStorageKey.read_bytes();
        // store the wasm code
        let read_code_hash = Hash::sha256(&tx_read_key);
        let code_len = (tx_read_key.len() as u64).serialize_to_vec();
        let key = Key::wasm_code(&read_code_hash);
        let len_key = Key::wasm_code_len(&read_code_hash);
        state.write(&key, tx_read_key).unwrap();
        state.write(&len_key, code_len).unwrap();

        let mut tx = Tx::new(state.in_mem().chain_id.clone(), None);
        let mut wrapper_tx = Tx::from_type(TxType::Wrapper(Box::new(
            namada_tx::data::WrapperTx::new(
                Fee {
                    amount_per_gas_unit: DenominatedAmount::native(1.into()),
                    token: state.in_mem().native_token.clone(),
                },
                namada_core::key::testing::common_sk_from_simple_seed(0)
                    .to_public(),
                0.into(),
            ),
        )));
        tx.add_code_from_hash(read_code_hash, None);
        wrapper_tx.add_code_from_hash(read_code_hash, None);
        tx.add_serialized_data(vec![]);
        wrapper_tx.add_serialized_data(vec![]);
        let mut raw_tx = wrapper_tx.clone();
        raw_tx.update_header(TxType::Raw);
        let batched_tx = wrapper_tx.batch_ref_first_tx().unwrap();

        // Check that using a disallowed wrapper tx leads to an error, but a raw
        // tx is ok even if not allowlisted
        {
            let allowlist = vec![format!("{}-bad", read_code_hash)];
            namada_parameters::update_tx_allowlist_parameter(
                &mut state, allowlist,
            )
            .unwrap();
            state.commit_tx_batch();

            let result = check_tx_allowed(&batched_tx, &state);
            assert_matches!(result.unwrap_err(), Error::DisallowedTx);
            let batched_raw_tx = raw_tx.batch_ref_first_tx().unwrap();
            let result = check_tx_allowed(&batched_raw_tx, &state);
            if let Err(result) = result {
                assert!(!matches!(result, Error::DisallowedTx));
            }
        }

        // Check that using an allowed wrapper tx doesn't lead to
        // `Error::DisallowedTx`
        {
            let allowlist = vec![read_code_hash.to_string()];
            namada_parameters::update_tx_allowlist_parameter(
                &mut state, allowlist,
            )
            .unwrap();
            state.commit_tx_batch();

            let result = check_tx_allowed(&batched_tx, &state);
            if let Err(result) = result {
                assert!(!matches!(result, Error::DisallowedTx));
            }
        }
    }

    /// Test that when a function runs out of gas in guest, the execution is
    /// aborted
    #[test]
    fn test_tx_out_of_gas_in_guest() {
        let mut state = TestState::default();
        let gas_meter =
            RefCell::new(TxGasMeter::new(OUT_OF_GAS_LIMIT, GAS_SCALE));
        let tx_index = TxIndex::default();

        // This code will charge gas in a host function indefinetely
        let tx_code = TestWasms::TxInfiniteGuestGas.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&tx_code);
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        let code_len = (tx_code.len() as u64).serialize_to_vec();
        let _ = state.write_log_mut().write(&key, tx_code.clone()).unwrap();
        let _ = state.write_log_mut().write(&len_key, code_len).unwrap();

        let (mut vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        let (mut tx_cache, _) =
            wasm::compilation_cache::common::testing::tx_cache();
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.set_code(Code::new(tx_code.clone(), None));
        outer_tx.set_data(Data::new(vec![]));
        let batched_tx = outer_tx.batch_ref_first_tx().unwrap();
        let result = tx(
            &mut state,
            &gas_meter,
            &tx_index,
            batched_tx.tx,
            batched_tx.cmt,
            &mut vp_cache,
            &mut tx_cache,
        );

        assert!(matches!(result.unwrap_err(), Error::GasError(_)));
    }

    /// Test that when a function runs out of gas in host, the execution is
    /// aborted from the host env (no cooperation required by the guest).
    #[test]
    fn test_tx_out_of_gas_in_host() {
        let mut state = TestState::default();
        let gas_meter =
            RefCell::new(TxGasMeter::new(OUT_OF_GAS_LIMIT, GAS_SCALE));
        let tx_index = TxIndex::default();

        // This code will charge gas in a host function indefinetely
        let tx_code = TestWasms::TxInfiniteHostGas.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&tx_code);
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        let code_len = (tx_code.len() as u64).serialize_to_vec();
        let _ = state.write_log_mut().write(&key, tx_code.clone()).unwrap();
        let _ = state.write_log_mut().write(&len_key, code_len).unwrap();

        let (mut vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        let (mut tx_cache, _) =
            wasm::compilation_cache::common::testing::tx_cache();
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.set_code(Code::new(tx_code.clone(), None));
        outer_tx.set_data(Data::new(vec![]));
        let batched_tx = outer_tx.batch_ref_first_tx().unwrap();
        let result = tx(
            &mut state,
            &gas_meter,
            &tx_index,
            batched_tx.tx,
            batched_tx.cmt,
            &mut vp_cache,
            &mut tx_cache,
        );

        assert!(matches!(result.unwrap_err(), Error::GasError(_)));
    }

    /// Test that when a vp runs out of gas in guest, the execution is aborted
    #[test]
    fn test_vp_out_of_gas_in_guest() {
        let mut state = TestState::default();
        let tx_index = TxIndex::default();

        let addr = state.in_mem_mut().address_gen.generate_address("rng seed");
        let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
            &TxGasMeter::new(OUT_OF_GAS_LIMIT, GAS_SCALE),
        ));
        let keys_changed = BTreeSet::new();
        let verifiers = BTreeSet::new();

        // This code will charge gas in a host function indefinetely
        let tx_code = TestWasms::VpInfiniteGuestGas.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&tx_code);
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        let code_len = (tx_code.len() as u64).serialize_to_vec();
        let _ = state.write_log_mut().write(&key, tx_code.clone()).unwrap();
        let _ = state.write_log_mut().write(&len_key, code_len).unwrap();

        let (vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.set_code(Code::new(tx_code.clone(), None));
        outer_tx.set_data(Data::new(vec![]));
        let result = vp(
            code_hash,
            &outer_tx.batch_ref_first_tx().unwrap(),
            &tx_index,
            &addr,
            &state,
            &gas_meter,
            &keys_changed,
            &verifiers,
            vp_cache.clone(),
        );

        assert!(matches!(result.unwrap_err(), Error::GasError(_)));
    }

    /// Test that when a vp runs out of gas in host, the execution is aborted
    /// from the host env (no cooperation required by the guest).
    #[test]
    fn test_vp_out_of_gas_in_host() {
        let mut state = TestState::default();
        let tx_index = TxIndex::default();

        let addr = state.in_mem_mut().address_gen.generate_address("rng seed");
        let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
            &TxGasMeter::new(OUT_OF_GAS_LIMIT, GAS_SCALE),
        ));
        let keys_changed = BTreeSet::new();
        let verifiers = BTreeSet::new();

        // This code will charge gas in a host function indefinetely
        let tx_code = TestWasms::VpInfiniteHostGas.read_bytes();
        // store the wasm code
        let code_hash = Hash::sha256(&tx_code);
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        let code_len = (tx_code.len() as u64).serialize_to_vec();
        let _ = state.write_log_mut().write(&key, tx_code.clone()).unwrap();
        let _ = state.write_log_mut().write(&len_key, code_len).unwrap();

        let (vp_cache, _) =
            wasm::compilation_cache::common::testing::vp_cache();
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.set_code(Code::new(tx_code.clone(), None));
        outer_tx.set_data(Data::new(vec![]));
        let result = vp(
            code_hash,
            &outer_tx.batch_ref_first_tx().unwrap(),
            &tx_index,
            &addr,
            &state,
            &gas_meter,
            &keys_changed,
            &verifiers,
            vp_cache.clone(),
        );

        assert!(matches!(result.unwrap_err(), Error::GasError(_)));
    }

    #[test]
    fn test_tx_ro_memory_wont_grow() {
        // a transaction that accesses memory out of bounds
        let out_of_bounds_index =
            checked!(2usize * TX_MEMORY_INIT_PAGES as usize * WASM_PAGE_SIZE)
                .unwrap();
        let tx_code = wasmer::wat2wasm(format!(
            r#"
            (module
                (import "env" "namada_tx_read" (func (param i64 i64) (result i64)))
                (func (param i64 i64) (result i64)
                    i64.const {out_of_bounds_index}
                    i64.const 1
                    (call 0)
                )
                (memory 16)
                (export "memory" (memory 0))
                (export "_apply_tx" (func 1))
            )
            "#
        ).as_bytes())
        .expect("unexpected error converting wat2wasm")
        .into_owned();

        const PANIC_MSG: &str =
            "Test should have failed with a wasm runtime memory error";

        let error = execute_tx_with_code(&tx_code).expect_err(PANIC_MSG);
        assert!(
            matches!(
                assert_tx_rt_mem_error(&error, PANIC_MSG),
                memory::Error::ReadOnly,
            ),
            "{PANIC_MSG}"
        );
    }

    #[test]
    fn test_vp_ro_memory_wont_grow() {
        // vp code that accesses memory out of bounds
        let out_of_bounds_index =
            checked!(2usize * VP_MEMORY_INIT_PAGES as usize * WASM_PAGE_SIZE)
                .unwrap();
        let vp_code = wasmer::wat2wasm(format!(
            r#"
            (module
                (type (;0;) (func (param i64 i64 i64 i64 i64 i64 i64 i64) (result i64)))
                (import "env" "namada_vp_read_pre" (func (param i64 i64) (result i64)))

                (func $_validate_tx (type 0) (param i64 i64 i64 i64 i64 i64 i64 i64) (result i64)
                    i64.const {out_of_bounds_index}
                    i64.const 1
                    (call 0)
                )

                (table (;0;) 1 1 funcref)
                (memory (;0;) 16)
                (global (;0;) (mut i32) (i32.const 1048576))
                (export "memory" (memory 0))
                (export "_validate_tx" (func $_validate_tx)))
            "#).as_bytes(),
        )
        .expect("unexpected error converting wat2wasm").into_owned();

        const PANIC_MSG: &str =
            "Test should have failed with a wasm runtime memory error";

        let error = execute_vp_with_code(&vp_code).expect_err(PANIC_MSG);
        assert!(
            matches!(
                assert_vp_rt_mem_error(&error, PANIC_MSG),
                memory::Error::ReadOnly,
            ),
            "{PANIC_MSG}"
        );
    }

    #[test]
    fn test_tx_leak() {
        let tx_code = TestWasms::TxNoOp.read_bytes();
        let (mut vp_cache, _) =
            wasm::compilation_cache::common::testing::cache();
        let (mut tx_cache, _) =
            wasm::compilation_cache::common::testing::cache();
        let mut last_cache_size: Option<usize> = None;
        for _ in 0..3 {
            let _verifiers = execute_tx_with_code_and_cache(
                &tx_code,
                &mut tx_cache,
                &mut vp_cache,
            )
            .unwrap();

            let info = &wasmer_compiler::FRAME_INFO.read().unwrap();
            let info: &GlobalFrameInfo = unsafe { std::mem::transmute(info) };
            if let Some(last_cache_size) = last_cache_size {
                assert_eq!(
                    last_cache_size,
                    info.ranges.len(),
                    "The frame info must not be growing - we're using the \
                     same WASM in each loop"
                );
            } else {
                last_cache_size = Some(info.ranges.len());
            }
        }
    }

    #[test]
    fn test_vp_leak() {
        let vp_code = TestWasms::VpAlwaysTrue.read_bytes();
        let (mut vp_cache, _) =
            wasm::compilation_cache::common::testing::cache();
        let mut last_cache_size: Option<usize> = None;
        for _ in 0..3 {
            execute_vp_with_code_and_cache(&vp_code, &mut vp_cache).unwrap();

            let info = &wasmer_compiler::FRAME_INFO.read().unwrap();
            let info: &GlobalFrameInfo = unsafe { std::mem::transmute(info) };
            if let Some(last_cache_size) = last_cache_size {
                assert_eq!(
                    last_cache_size,
                    info.ranges.len(),
                    "The frame info must not be growing - we're using the \
                     same WASM in each loop"
                );
            } else {
                last_cache_size = Some(info.ranges.len());
            }
        }
    }

    fn execute_vp_with_code(vp_code: &[u8]) -> Result<()> {
        let (mut vp_cache, _) =
            wasm::compilation_cache::common::testing::cache();
        execute_vp_with_code_and_cache(vp_code, &mut vp_cache)
    }

    fn execute_vp_with_code_and_cache<CA: 'static + WasmCacheAccess>(
        vp_code: &[u8],
        vp_cache: &mut VpCache<CA>,
    ) -> Result<()> {
        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.push_default_inner_tx();
        let tx_index = TxIndex::default();
        let mut state = TestState::default();
        let addr = state.in_mem_mut().address_gen.generate_address("rng seed");
        let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
            &TxGasMeter::new(TX_GAS_LIMIT, GAS_SCALE),
        ));
        let keys_changed = BTreeSet::new();
        let verifiers = BTreeSet::new();
        // store the vp code
        let code_hash = Hash::sha256(vp_code);
        let code_len = vp_code.len() as u64;
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        state.write(&key, vp_code).unwrap();
        state.write(&len_key, code_len).unwrap();

        vp(
            code_hash,
            &outer_tx.batch_ref_first_tx().unwrap(),
            &tx_index,
            &addr,
            &state,
            &gas_meter,
            &keys_changed,
            &verifiers,
            vp_cache.clone(),
        )
    }

    fn execute_tx_with_code(tx_code: &[u8]) -> Result<BTreeSet<Address>> {
        let (mut tx_cache, _) =
            wasm::compilation_cache::common::testing::cache();
        let (mut vp_cache, _) =
            wasm::compilation_cache::common::testing::cache();
        execute_tx_with_code_and_cache(tx_code, &mut tx_cache, &mut vp_cache)
    }

    fn execute_tx_with_code_and_cache<CA: 'static + WasmCacheAccess>(
        tx_code: &[u8],
        tx_cache: &mut TxCache<CA>,
        vp_cache: &mut VpCache<CA>,
    ) -> Result<BTreeSet<Address>> {
        let tx_data = vec![];
        let tx_index = TxIndex::default();
        let mut state = TestState::default();
        let gas_meter = RefCell::new(TxGasMeter::new(TX_GAS_LIMIT, GAS_SCALE));

        // store the tx code
        let code_hash = Hash::sha256(tx_code);
        let code_len = (tx_code.len() as u64).serialize_to_vec();
        let key = Key::wasm_code(&code_hash);
        let len_key = Key::wasm_code_len(&code_hash);
        let _ = state
            .write_log_mut()
            .write(&key, tx_code.serialize_to_vec())
            .unwrap();
        let _ = state.write_log_mut().write(&len_key, code_len).unwrap();

        let mut outer_tx = Tx::from_type(TxType::Raw);
        outer_tx.set_code(Code::from_hash(code_hash, None));
        outer_tx.set_data(Data::new(tx_data));
        let batched_tx = outer_tx.batch_ref_first_tx().unwrap();

        tx(
            &mut state,
            &gas_meter,
            &tx_index,
            batched_tx.tx,
            batched_tx.cmt,
            vp_cache,
            tx_cache,
        )
    }

    fn loop_in_tx_wasm(loops: u32) -> Result<BTreeSet<Address>> {
        // A transaction with a recursive loop.
        // The boilerplate code is generated from tx_template.wasm using
        // `wasm2wat` and the loop code is hand-written.
        let tx_code = wasmer::wat2wasm(
            format!(
                r#"
            (module
                (type (;0;) (func (param i64 i64) (result i64)))

                ;; recursive loop, the param is the number of loops
                (func $loop (param i64) (result i64)
                (if
                (result i64)
                (i64.eqz (get_local 0))
                (then (i64.const 1))
                (else (call $loop (i64.sub (get_local 0) (i64.const 1))))))

                (func $_apply_tx (type 0) (param i64 i64) (result i64)
                (call $loop (i64.const {loops})))

                (table (;0;) 1 1 funcref)
                (memory (;0;) 16)
                (global (;0;) (mut i32) (i32.const 1048576))
                (export "memory" (memory 0))
                (export "_apply_tx" (func $_apply_tx)))
            "#
            )
            .as_bytes(),
        )
        .expect("unexpected error converting wat2wasm")
        .into_owned();

        execute_tx_with_code(&tx_code)
    }

    fn loop_in_vp_wasm(loops: u32) -> Result<()> {
        // A validity predicate with a recursive loop.
        // The boilerplate code is generated from vp_template.wasm using
        // `wasm2wat` and the loop code is hand-written.
        let vp_code = wasmer::wat2wasm(format!(
            r#"
            (module
                (type (;0;) (func (param i64 i64 i64 i64 i64 i64 i64 i64) (result i64)))

                ;; recursive loop, the param is the number of loops
                (func $loop (param i64) (result i64)
                (if
                (result i64)
                (i64.eqz (get_local 0))
                (then (i64.const 1))
                (else (call $loop (i64.sub (get_local 0) (i64.const 1))))))

                (func $_validate_tx (type 0) (param i64 i64 i64 i64 i64 i64 i64 i64) (result i64)
                (call $loop (i64.const {})))

                (table (;0;) 1 1 funcref)
                (memory (;0;) 16)
                (global (;0;) (mut i32) (i32.const 1048576))
                (export "memory" (memory 0))
                (export "_validate_tx" (func $_validate_tx)))
            "#, loops).as_bytes(),
        )
            .expect("unexpected error converting wat2wasm").into_owned();

        execute_vp_with_code(&vp_code)
    }

    fn get_trap_code(error: &Error) -> Either<TrapCode, String> {
        if let Error::RuntimeError(err) = error {
            if let Some(trap_code) = err.clone().to_trap() {
                Either::Left(trap_code)
            } else {
                Either::Right(format!("Missing trap code {}", err))
            }
        } else {
            Either::Right(format!("Unexpected error {}", error))
        }
    }

    fn assert_stack_overflow(error: &Error) {
        let trap_code = get_trap_code(error);
        // Depending on platform, we get a different error from the overflow
        assert!(
            // Universal engine error (currently used on mac)
            trap_code ==
                Either::Left(wasmer_vm::TrapCode::UnreachableCodeReached) ||
            // Dylib engine error (used elsewhere)
                trap_code ==
                Either::Left(wasmer_vm::TrapCode::StackOverflow),
        );
    }

    /// The following definitions are copied from wasmer v4.3.5
    /// `lib/compiler/src/engine/trap/frame_info.rs` to access internal
    /// fields that are otherwise private. This must be carefully maintained
    /// while we workaround the leak before it's fixed in wasmer.
    pub struct GlobalFrameInfo {
        ranges: BTreeMap<usize, ModuleInfoFrameInfo>,
    }
    struct ModuleInfoFrameInfo {
        _start: usize,
        _functions: BTreeMap<usize, FunctionInfo>,
        _module: std::sync::Arc<wasmer_types::ModuleInfo>,
        _frame_infos: wasmer_compiler::FrameInfosVariant,
    }
    struct FunctionInfo {
        _start: usize,
        _local_index: wasmer_types::LocalFunctionIndex,
    }
}