clarity/vm/types/
serialization.rs

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
// Copyright (C) 2013-2020 Blockstack PBC, a public benefit corporation
// Copyright (C) 2020 Stacks Open Internet Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

use std::io::{Read, Write};
use std::{cmp, error, fmt, str};

use hashbrown::HashMap;
use lazy_static::lazy_static;
use serde_json::Value as JSONValue;
use stacks_common::codec::{Error as codec_error, StacksMessageCodec};
use stacks_common::types::StacksEpochId;
use stacks_common::util::hash::{hex_bytes, to_hex};
use stacks_common::util::retry::BoundReader;

use super::{ListTypeData, TupleTypeSignature};
use crate::vm::database::{ClarityDeserializable, ClaritySerializable};
use crate::vm::errors::{
    CheckErrors, Error as ClarityError, IncomparableError, InterpreterError, InterpreterResult,
    RuntimeErrorType,
};
use crate::vm::representations::{ClarityName, ContractName, MAX_STRING_LEN};
use crate::vm::types::signatures::CallableSubtype;
use crate::vm::types::{
    byte_len_of_serialization, BufferLength, CallableData, CharType, OptionalData, PrincipalData,
    QualifiedContractIdentifier, ResponseData, SequenceData, SequenceSubtype,
    StandardPrincipalData, StringSubtype, StringUTF8Length, TupleData, TypeSignature, Value,
    BOUND_VALUE_SERIALIZATION_BYTES, MAX_TYPE_DEPTH, MAX_VALUE_SIZE,
};

/// Errors that may occur in serialization or deserialization
/// If deserialization failed because the described type is a bad type and
///   a CheckError is thrown, it gets wrapped in BadTypeError.
/// Any IOErrrors from the supplied buffer will manifest as IOError variants,
///   except for EOF -- if the deserialization code experiences an EOF, it is caught
///   and rethrown as DeserializationError
#[derive(Debug, PartialEq)]
pub enum SerializationError {
    IOError(IncomparableError<std::io::Error>),
    BadTypeError(CheckErrors),
    DeserializationError(String),
    DeserializeExpected(TypeSignature),
    LeftoverBytesInDeserialization,
    SerializationError(String),
}

lazy_static! {
    pub static ref NONE_SERIALIZATION_LEN: u64 = {
        #[allow(clippy::unwrap_used)]
        u64::try_from(Value::none().serialize_to_vec().unwrap().len()).unwrap()
    };
}

/// Deserialization uses a specific epoch for passing to the type signature checks
/// The reason this is pinned to Epoch21 is so that values stored before epoch-2.4
///  can still be read from the database.
const DESERIALIZATION_TYPE_CHECK_EPOCH: StacksEpochId = StacksEpochId::Epoch21;

/// Pre-sanitization values could end up being larger than the deserializer originally
///  supported, so we increase the bound to a higher level limit imposed by the cost checker.
const SANITIZATION_READ_BOUND: u64 = 15_000_000;

/// Before epoch-2.4, this is the deserialization depth limit.
/// After epoch-2.4, with type sanitization support, the full
///  clarity depth limit is supported.
const UNSANITIZED_DEPTH_CHECK: usize = 16;

impl std::fmt::Display for SerializationError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            SerializationError::IOError(e) => {
                write!(f, "Serialization error caused by IO: {}", e.err)
            }
            SerializationError::BadTypeError(e) => {
                write!(f, "Deserialization error, bad type, caused by: {}", e)
            }
            SerializationError::DeserializationError(e) => {
                write!(f, "Deserialization error: {}", e)
            }
            SerializationError::SerializationError(e) => {
                write!(f, "Serialization error: {}", e)
            }
            SerializationError::DeserializeExpected(e) => write!(
                f,
                "Deserialization expected the type of the input to be: {}",
                e
            ),
            SerializationError::LeftoverBytesInDeserialization => {
                write!(f, "Deserialization error: bytes left over in buffer")
            }
        }
    }
}

impl error::Error for SerializationError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            SerializationError::IOError(e) => Some(&e.err),
            SerializationError::BadTypeError(e) => Some(e),
            _ => None,
        }
    }
}

// Note: a byte stream that describes a longer type than
//   there are available bytes to read will result in an IOError(UnexpectedEOF)
impl From<std::io::Error> for SerializationError {
    fn from(err: std::io::Error) -> Self {
        SerializationError::IOError(IncomparableError { err })
    }
}

impl From<&str> for SerializationError {
    fn from(e: &str) -> Self {
        SerializationError::DeserializationError(e.into())
    }
}

impl From<CheckErrors> for SerializationError {
    fn from(e: CheckErrors) -> Self {
        SerializationError::BadTypeError(e)
    }
}

define_u8_enum!(TypePrefix {
    Int = 0,
    UInt = 1,
    Buffer = 2,
    BoolTrue = 3,
    BoolFalse = 4,
    PrincipalStandard = 5,
    PrincipalContract = 6,
    ResponseOk = 7,
    ResponseErr = 8,
    OptionalNone = 9,
    OptionalSome = 10,
    List = 11,
    Tuple = 12,
    StringASCII = 13,
    StringUTF8 = 14
});

impl From<&PrincipalData> for TypePrefix {
    fn from(v: &PrincipalData) -> TypePrefix {
        use super::PrincipalData::*;
        match v {
            Standard(_) => TypePrefix::PrincipalStandard,
            Contract(_) => TypePrefix::PrincipalContract,
        }
    }
}

impl From<&Value> for TypePrefix {
    fn from(v: &Value) -> TypePrefix {
        use super::CharType;
        use super::SequenceData::*;
        use super::Value::*;

        match v {
            Int(_) => TypePrefix::Int,
            UInt(_) => TypePrefix::UInt,
            Bool(value) => {
                if *value {
                    TypePrefix::BoolTrue
                } else {
                    TypePrefix::BoolFalse
                }
            }
            Principal(p) => TypePrefix::from(p),
            Response(response) => {
                if response.committed {
                    TypePrefix::ResponseOk
                } else {
                    TypePrefix::ResponseErr
                }
            }
            Optional(OptionalData { data: None }) => TypePrefix::OptionalNone,
            Optional(OptionalData { data: Some(_) }) => TypePrefix::OptionalSome,
            Tuple(_) => TypePrefix::Tuple,
            Sequence(Buffer(_)) => TypePrefix::Buffer,
            Sequence(List(_)) => TypePrefix::List,
            Sequence(String(CharType::ASCII(_))) => TypePrefix::StringASCII,
            Sequence(String(CharType::UTF8(_))) => TypePrefix::StringUTF8,
            &CallableContract(_) => TypePrefix::PrincipalContract,
        }
    }
}

/// Not a public trait,
///   this is just used to simplify serializing some types that
///   are repeatedly serialized or deserialized.
trait ClarityValueSerializable<T: std::marker::Sized> {
    fn serialize_write<W: Write>(&self, w: &mut W) -> std::io::Result<()>;
    fn deserialize_read<R: Read>(r: &mut R) -> Result<T, SerializationError>;
}

impl ClarityValueSerializable<StandardPrincipalData> for StandardPrincipalData {
    fn serialize_write<W: Write>(&self, w: &mut W) -> std::io::Result<()> {
        w.write_all(&[self.0])?;
        w.write_all(&self.1)
    }

    fn deserialize_read<R: Read>(r: &mut R) -> Result<Self, SerializationError> {
        let mut version = [0; 1];
        let mut data = [0; 20];
        r.read_exact(&mut version)?;
        r.read_exact(&mut data)?;
        Ok(StandardPrincipalData(version[0], data))
    }
}

macro_rules! serialize_guarded_string {
    ($Name:ident) => {
        impl ClarityValueSerializable<$Name> for $Name {
            fn serialize_write<W: Write>(&self, w: &mut W) -> std::io::Result<()> {
                w.write_all(&self.len().to_be_bytes())?;
                // self.as_bytes() is always len bytes, because this is only used for GuardedStrings
                //   which are a subset of ASCII
                w.write_all(self.as_str().as_bytes())
            }

            fn deserialize_read<R: Read>(r: &mut R) -> Result<Self, SerializationError> {
                let mut len = [0; 1];
                r.read_exact(&mut len)?;
                let len = u8::from_be_bytes(len);
                if len > MAX_STRING_LEN {
                    return Err(SerializationError::DeserializationError(
                        "String too long".to_string(),
                    ));
                }

                let mut data = vec![0; len as usize];
                r.read_exact(&mut data)?;

                String::from_utf8(data)
                    .map_err(|_| "Non-UTF8 string data".into())
                    .and_then(|x| $Name::try_from(x).map_err(|_| "Illegal Clarity string".into()))
            }
        }
    };
}

serialize_guarded_string!(ClarityName);
serialize_guarded_string!(ContractName);

impl PrincipalData {
    fn inner_consensus_serialize<W: Write>(&self, w: &mut W) -> std::io::Result<()> {
        w.write_all(&[TypePrefix::from(self) as u8])?;
        match self {
            PrincipalData::Standard(p) => p.serialize_write(w),
            PrincipalData::Contract(contract_identifier) => {
                contract_identifier.issuer.serialize_write(w)?;
                contract_identifier.name.serialize_write(w)
            }
        }
    }

    fn inner_consensus_deserialize<R: Read>(
        r: &mut R,
    ) -> Result<PrincipalData, SerializationError> {
        let mut header = [0];
        r.read_exact(&mut header)?;

        let prefix = TypePrefix::from_u8(header[0]).ok_or("Bad principal prefix")?;

        match prefix {
            TypePrefix::PrincipalStandard => {
                StandardPrincipalData::deserialize_read(r).map(PrincipalData::from)
            }
            TypePrefix::PrincipalContract => {
                let issuer = StandardPrincipalData::deserialize_read(r)?;
                let name = ContractName::deserialize_read(r)?;
                Ok(PrincipalData::from(QualifiedContractIdentifier {
                    issuer,
                    name,
                }))
            }
            _ => Err("Bad principal prefix".into()),
        }
    }
}

impl StacksMessageCodec for PrincipalData {
    fn consensus_serialize<W: Write>(&self, fd: &mut W) -> Result<(), codec_error> {
        self.inner_consensus_serialize(fd)
            .map_err(codec_error::WriteError)
    }

    fn consensus_deserialize<R: Read>(fd: &mut R) -> Result<PrincipalData, codec_error> {
        PrincipalData::inner_consensus_deserialize(fd)
            .map_err(|e| codec_error::DeserializeError(e.to_string()))
    }
}

macro_rules! check_match {
    ($item:expr, $Pattern:pat) => {
        match $item {
            None => Ok(()),
            Some($Pattern) => Ok(()),
            Some(x) => Err(SerializationError::DeserializeExpected(x.clone())),
        }
    };
}

/// `DeserializeStackItem` objects are used by the deserializer to indicate
///  how the deserialization loop's current object is to be handled once it is
///  deserialized: i.e., is the object the top-level object for the serialization
///  or is it an entry in a composite type (e.g., a list or tuple)?
enum DeserializeStackItem {
    List {
        items: Vec<Value>,
        expected_len: u32,
        expected_type: Option<ListTypeData>,
    },
    Tuple {
        items: Vec<(ClarityName, Value)>,
        expected_len: u64,
        processed_entries: u64,
        expected_type: Option<TupleTypeSignature>,
        next_name: ClarityName,
        next_sanitize: bool,
    },
    OptionSome {
        inner_expected_type: Option<TypeSignature>,
    },
    ResponseOk {
        inner_expected_type: Option<TypeSignature>,
    },
    ResponseErr {
        inner_expected_type: Option<TypeSignature>,
    },
    TopLevel {
        expected_type: Option<TypeSignature>,
    },
}

impl DeserializeStackItem {
    /// What is the expected type for the child of this deserialization stack item?
    ///
    /// Returns `None` if this stack item either doesn't have an expected type, or the
    ///   next child is going to be sanitized/elided.
    fn next_expected_type(&self) -> Result<Option<TypeSignature>, SerializationError> {
        match self {
            DeserializeStackItem::List { expected_type, .. } => Ok(expected_type
                .as_ref()
                .map(|lt| lt.get_list_item_type())
                .cloned()),
            DeserializeStackItem::Tuple {
                expected_type,
                next_name,
                next_sanitize,
                ..
            } => match expected_type {
                None => Ok(None),
                Some(some_tuple) => {
                    // if we're sanitizing this tuple, and the `next_name` field is to be
                    //  removed, don't return an expected type.
                    if *next_sanitize {
                        return Ok(None);
                    }
                    let field_type = some_tuple.field_type(next_name).ok_or_else(|| {
                        SerializationError::DeserializeExpected(TypeSignature::TupleType(
                            some_tuple.clone(),
                        ))
                    })?;
                    Ok(Some(field_type.clone()))
                }
            },
            DeserializeStackItem::OptionSome {
                inner_expected_type,
            } => Ok(inner_expected_type.clone()),
            DeserializeStackItem::ResponseOk {
                inner_expected_type,
            } => Ok(inner_expected_type.clone()),
            DeserializeStackItem::ResponseErr {
                inner_expected_type,
            } => Ok(inner_expected_type.clone()),
            DeserializeStackItem::TopLevel { expected_type } => Ok(expected_type.clone()),
        }
    }
}

impl TypeSignature {
    /// Return the maximum length of the consensus serialization of a
    /// Clarity value of this type. The returned length *may* not fit
    /// in a Clarity buffer! For example, the maximum serialized
    /// size of a `(buff 1024*1024)` is `1+1024*1024` because of the
    /// type prefix byte. However, that is 1 byte larger than the maximum
    /// buffer size in Clarity.
    pub fn max_serialized_size(&self) -> Result<u32, CheckErrors> {
        let type_prefix_size = 1;

        let max_output_size = match self {
            TypeSignature::NoType => {
                // A `NoType` should *never* actually be evaluated
                // (`NoType` corresponds to the Some branch of a
                // `none` that is never matched with a corresponding
                // `some` or similar with `result` types).  So, when
                // serializing an object with a `NoType`, the other
                // branch should always be used.
                return Err(CheckErrors::CouldNotDetermineSerializationType);
            }
            TypeSignature::IntType => 16,
            TypeSignature::UIntType => 16,
            TypeSignature::BoolType => 0,
            TypeSignature::SequenceType(SequenceSubtype::ListType(list_type)) => {
                // u32 length as big-endian bytes
                let list_length_encode = 4;
                list_type
                    .get_max_len()
                    .checked_mul(list_type.get_list_item_type().max_serialized_size()?)
                    .and_then(|x| x.checked_add(list_length_encode))
                    .ok_or_else(|| CheckErrors::ValueTooLarge)?
            }
            TypeSignature::SequenceType(SequenceSubtype::BufferType(buff_length)) => {
                // u32 length as big-endian bytes
                let buff_length_encode = 4;
                u32::from(buff_length)
                    .checked_add(buff_length_encode)
                    .ok_or_else(|| CheckErrors::ValueTooLarge)?
            }
            TypeSignature::SequenceType(SequenceSubtype::StringType(StringSubtype::ASCII(
                length,
            ))) => {
                // u32 length as big-endian bytes
                let str_length_encode = 4;
                // ascii is 1-byte per character
                u32::from(length)
                    .checked_add(str_length_encode)
                    .ok_or_else(|| CheckErrors::ValueTooLarge)?
            }
            TypeSignature::SequenceType(SequenceSubtype::StringType(StringSubtype::UTF8(
                length,
            ))) => {
                // u32 length as big-endian bytes
                let str_length_encode = 4;
                // utf-8 is maximum 4 bytes per codepoint (which is the length)
                u32::from(length)
                    .checked_mul(4)
                    .and_then(|x| x.checked_add(str_length_encode))
                    .ok_or_else(|| CheckErrors::ValueTooLarge)?
            }
            TypeSignature::PrincipalType
            | TypeSignature::CallableType(_)
            | TypeSignature::TraitReferenceType(_) => {
                // version byte + 20 byte hash160
                let maximum_issuer_size = 21;
                let contract_name_length_encode = 1;
                // contract name maximum length is `MAX_STRING_LEN` (128), and ASCII
                let maximum_contract_name = MAX_STRING_LEN as u32;
                maximum_contract_name + maximum_issuer_size + contract_name_length_encode
            }
            TypeSignature::TupleType(tuple_type) => {
                let type_map = tuple_type.get_type_map();
                // u32 length as big-endian bytes
                let tuple_length_encode: u32 = 4;
                let mut total_size = tuple_length_encode;
                for (key, value) in type_map.iter() {
                    let value_size = value.max_serialized_size()?;
                    total_size = total_size
                        .checked_add(1) // length of key-name
                        .and_then(|x| x.checked_add(key.len() as u32)) // ClarityName is ascii-only, so 1 byte per length
                        .and_then(|x| x.checked_add(value_size))
                        .ok_or_else(|| CheckErrors::ValueTooLarge)?;
                }
                total_size
            }
            TypeSignature::OptionalType(ref some_type) => {
                match some_type.max_serialized_size() {
                    Ok(size) => size,
                    // if NoType, then this is just serializing a none
                    // value, which is only the type prefix
                    Err(CheckErrors::CouldNotDetermineSerializationType) => 0,
                    Err(e) => return Err(e),
                }
            }
            TypeSignature::ResponseType(ref response_types) => {
                let (ok_type, err_type) = response_types.as_ref();
                let (ok_type_max_size, no_ok_type) = match ok_type.max_serialized_size() {
                    Ok(size) => (size, false),
                    Err(CheckErrors::CouldNotDetermineSerializationType) => (0, true),
                    Err(e) => return Err(e),
                };
                let err_type_max_size = match err_type.max_serialized_size() {
                    Ok(size) => size,
                    Err(CheckErrors::CouldNotDetermineSerializationType) => {
                        if no_ok_type {
                            // if both the ok type and the error type are NoType,
                            //  throw a CheckError. This should not be possible, but the check
                            //  is done out of caution.
                            return Err(CheckErrors::CouldNotDetermineSerializationType);
                        } else {
                            0
                        }
                    }
                    Err(e) => return Err(e),
                };
                cmp::max(ok_type_max_size, err_type_max_size)
            }
            TypeSignature::ListUnionType(_) => {
                return Err(CheckErrors::CouldNotDetermineSerializationType)
            }
        };

        max_output_size
            .checked_add(type_prefix_size)
            .ok_or_else(|| CheckErrors::ValueTooLarge)
    }
}

impl Value {
    pub fn deserialize_read<R: Read>(
        r: &mut R,
        expected_type: Option<&TypeSignature>,
        sanitize: bool,
    ) -> Result<Value, SerializationError> {
        Self::deserialize_read_count(r, expected_type, sanitize).map(|(value, _)| value)
    }

    /// Deserialize just like `deserialize_read` but also
    ///  return the bytes read.
    /// If `sanitize` argument is set to true and `expected_type` is supplied,
    ///  this method will remove any extraneous tuple fields which may have been
    ///  allowed by `least_super_type`.
    pub fn deserialize_read_count<R: Read>(
        r: &mut R,
        expected_type: Option<&TypeSignature>,
        sanitize: bool,
    ) -> Result<(Value, u64), SerializationError> {
        let bound_value_serialization_bytes = if sanitize && expected_type.is_some() {
            SANITIZATION_READ_BOUND
        } else {
            BOUND_VALUE_SERIALIZATION_BYTES as u64
        };
        let mut bound_reader = BoundReader::from_reader(r, bound_value_serialization_bytes);
        let value = Value::inner_deserialize_read(&mut bound_reader, expected_type, sanitize)?;
        let bytes_read = bound_reader.num_read();
        if let Some(expected_type) = expected_type {
            let expect_size = match expected_type.max_serialized_size() {
                Ok(x) => x,
                Err(e) => {
                    debug!(
                        "Failed to determine max serialized size when checking expected_type argument";
                        "err" => ?e
                    );
                    return Ok((value, bytes_read));
                }
            };

            if expect_size as u64 > bytes_read {
                // this can happen due to sanitization, so its no longer indicative of a *problem* with the node.
                debug!(
                    "Deserialized more bytes than expected size during deserialization. Expected size = {}, bytes read = {}, type = {}",
                    expect_size,
                    bytes_read,
                    expected_type,
                );
            }
        }

        Ok((value, bytes_read))
    }

    fn inner_deserialize_read<R: Read>(
        r: &mut R,
        top_expected_type: Option<&TypeSignature>,
        sanitize: bool,
    ) -> Result<Value, SerializationError> {
        use super::PrincipalData::*;
        use super::Value::*;

        let mut stack = vec![DeserializeStackItem::TopLevel {
            expected_type: top_expected_type.cloned(),
        }];

        while !stack.is_empty() {
            let depth_check = if sanitize {
                MAX_TYPE_DEPTH as usize
            } else {
                UNSANITIZED_DEPTH_CHECK
            };
            if stack.len() > depth_check {
                return Err(CheckErrors::TypeSignatureTooDeep.into());
            }

            #[allow(clippy::expect_used)]
            let expected_type = stack
                .last()
                .expect("FATAL: stack.last() should always be some() because of loop condition")
                .next_expected_type()?;

            let mut header = [0];
            r.read_exact(&mut header)?;
            let prefix = TypePrefix::from_u8(header[0]).ok_or("Bad type prefix")?;

            let item = match prefix {
                TypePrefix::Int => {
                    check_match!(expected_type, TypeSignature::IntType)?;
                    let mut buffer = [0; 16];
                    r.read_exact(&mut buffer)?;
                    Ok(Int(i128::from_be_bytes(buffer)))
                }
                TypePrefix::UInt => {
                    check_match!(expected_type, TypeSignature::UIntType)?;
                    let mut buffer = [0; 16];
                    r.read_exact(&mut buffer)?;
                    Ok(UInt(u128::from_be_bytes(buffer)))
                }
                TypePrefix::Buffer => {
                    let mut buffer_len = [0; 4];
                    r.read_exact(&mut buffer_len)?;
                    let buffer_len = BufferLength::try_from(u32::from_be_bytes(buffer_len))?;

                    if let Some(x) = &expected_type {
                        let passed_test = match x {
                            TypeSignature::SequenceType(SequenceSubtype::BufferType(
                                expected_len,
                            )) => u32::from(&buffer_len) <= u32::from(expected_len),
                            _ => false,
                        };
                        if !passed_test {
                            return Err(SerializationError::DeserializeExpected(x.clone()));
                        }
                    }

                    let mut data = vec![0; u32::from(buffer_len) as usize];

                    r.read_exact(&mut data[..])?;

                    Value::buff_from(data).map_err(|_| "Bad buffer".into())
                }
                TypePrefix::BoolTrue => {
                    check_match!(expected_type, TypeSignature::BoolType)?;
                    Ok(Bool(true))
                }
                TypePrefix::BoolFalse => {
                    check_match!(expected_type, TypeSignature::BoolType)?;
                    Ok(Bool(false))
                }
                TypePrefix::PrincipalStandard => {
                    check_match!(expected_type, TypeSignature::PrincipalType)?;
                    StandardPrincipalData::deserialize_read(r).map(Value::from)
                }
                TypePrefix::PrincipalContract => {
                    check_match!(expected_type, TypeSignature::PrincipalType)?;
                    let issuer = StandardPrincipalData::deserialize_read(r)?;
                    let name = ContractName::deserialize_read(r)?;
                    Ok(Value::from(QualifiedContractIdentifier { issuer, name }))
                }
                TypePrefix::ResponseOk | TypePrefix::ResponseErr => {
                    let committed = prefix == TypePrefix::ResponseOk;

                    let expect_contained_type = match &expected_type {
                        None => None,
                        Some(x) => {
                            let contained_type = match (committed, x) {
                                (true, TypeSignature::ResponseType(types)) => Ok(&types.0),
                                (false, TypeSignature::ResponseType(types)) => Ok(&types.1),
                                _ => Err(SerializationError::DeserializeExpected(x.clone())),
                            }?;
                            Some(contained_type)
                        }
                    };

                    let stack_item = if committed {
                        DeserializeStackItem::ResponseOk {
                            inner_expected_type: expect_contained_type.cloned(),
                        }
                    } else {
                        DeserializeStackItem::ResponseErr {
                            inner_expected_type: expect_contained_type.cloned(),
                        }
                    };

                    stack.push(stack_item);
                    continue;
                }
                TypePrefix::OptionalNone => {
                    check_match!(expected_type, TypeSignature::OptionalType(_))?;
                    Ok(Value::none())
                }
                TypePrefix::OptionalSome => {
                    let expect_contained_type = match &expected_type {
                        None => None,
                        Some(x) => {
                            let contained_type = match x {
                                TypeSignature::OptionalType(some_type) => Ok(some_type.as_ref()),
                                _ => Err(SerializationError::DeserializeExpected(x.clone())),
                            }?;
                            Some(contained_type)
                        }
                    };

                    let stack_item = DeserializeStackItem::OptionSome {
                        inner_expected_type: expect_contained_type.cloned(),
                    };

                    stack.push(stack_item);
                    continue;
                }
                TypePrefix::List => {
                    let mut len = [0; 4];
                    r.read_exact(&mut len)?;
                    let len = u32::from_be_bytes(len);

                    if len > MAX_VALUE_SIZE {
                        return Err("Illegal list type".into());
                    }

                    let (list_type, _entry_type) = match expected_type.as_ref() {
                        None => (None, None),
                        Some(TypeSignature::SequenceType(SequenceSubtype::ListType(list_type))) => {
                            if len > list_type.get_max_len() {
                                // unwrap is safe because of the match condition
                                #[allow(clippy::unwrap_used)]
                                return Err(SerializationError::DeserializeExpected(
                                    expected_type.unwrap(),
                                ));
                            }
                            (Some(list_type), Some(list_type.get_list_item_type()))
                        }
                        Some(x) => return Err(SerializationError::DeserializeExpected(x.clone())),
                    };

                    if len > 0 {
                        let items = Vec::with_capacity(len as usize);
                        let stack_item = DeserializeStackItem::List {
                            items,
                            expected_len: len,
                            expected_type: list_type.cloned(),
                        };

                        stack.push(stack_item);
                        continue;
                    } else {
                        let finished_list = if let Some(list_type) = list_type {
                            Value::list_with_type(
                                &DESERIALIZATION_TYPE_CHECK_EPOCH,
                                vec![],
                                list_type.clone(),
                            )
                            .map_err(|_| "Illegal list type")?
                        } else {
                            Value::cons_list_unsanitized(vec![]).map_err(|_| "Illegal list type")?
                        };

                        Ok(finished_list)
                    }
                }
                TypePrefix::Tuple => {
                    let mut len = [0; 4];
                    r.read_exact(&mut len)?;
                    let len = u32::from_be_bytes(len);
                    let expected_len = u64::from(len);

                    if len > MAX_VALUE_SIZE {
                        return Err(SerializationError::DeserializationError(
                            "Illegal tuple type".to_string(),
                        ));
                    }

                    let tuple_type = match expected_type.as_ref() {
                        None => None,
                        Some(TypeSignature::TupleType(tuple_type)) => {
                            if sanitize {
                                if u64::from(len) < tuple_type.len() {
                                    // unwrap is safe because of the match condition
                                    #[allow(clippy::unwrap_used)]
                                    return Err(SerializationError::DeserializeExpected(
                                        expected_type.unwrap(),
                                    ));
                                }
                            } else {
                                if len as u64 != tuple_type.len() {
                                    // unwrap is safe because of the match condition
                                    #[allow(clippy::unwrap_used)]
                                    return Err(SerializationError::DeserializeExpected(
                                        expected_type.unwrap(),
                                    ));
                                }
                            }
                            Some(tuple_type)
                        }
                        Some(x) => return Err(SerializationError::DeserializeExpected(x.clone())),
                    };

                    if len > 0 {
                        let items = Vec::with_capacity(expected_len as usize);
                        let first_key = ClarityName::deserialize_read(r)?;
                        // figure out if the next (key, value) pair for this
                        //  tuple will be elided (or sanitized) from the tuple.
                        // the logic here is that the next pair should be elided if:
                        //    * `sanitize` parameter is true
                        //    * `tuple_type` is some (i.e., there is an expected type for the
                        //       tuple)
                        //    * `tuple_type` does not contain an entry for `key`
                        let next_sanitize = sanitize
                            && tuple_type
                                .map(|tt| tt.field_type(&first_key).is_none())
                                .unwrap_or(false);
                        let stack_item = DeserializeStackItem::Tuple {
                            items,
                            expected_len,
                            processed_entries: 0,
                            expected_type: tuple_type.cloned(),
                            next_name: first_key,
                            next_sanitize,
                        };

                        stack.push(stack_item);
                        continue;
                    } else {
                        let finished_tuple = if let Some(tuple_type) = tuple_type {
                            TupleData::from_data_typed(
                                &DESERIALIZATION_TYPE_CHECK_EPOCH,
                                vec![],
                                tuple_type,
                            )
                            .map_err(|_| "Illegal tuple type")
                            .map(Value::from)?
                        } else {
                            TupleData::from_data(vec![])
                                .map_err(|_| "Illegal tuple type")
                                .map(Value::from)?
                        };
                        Ok(finished_tuple)
                    }
                }
                TypePrefix::StringASCII => {
                    let mut buffer_len = [0; 4];
                    r.read_exact(&mut buffer_len)?;
                    let buffer_len = BufferLength::try_from(u32::from_be_bytes(buffer_len))?;

                    if let Some(x) = &expected_type {
                        let passed_test = match x {
                            TypeSignature::SequenceType(SequenceSubtype::StringType(
                                StringSubtype::ASCII(expected_len),
                            )) => u32::from(&buffer_len) <= u32::from(expected_len),
                            _ => false,
                        };
                        if !passed_test {
                            return Err(SerializationError::DeserializeExpected(x.clone()));
                        }
                    }

                    let mut data = vec![0; u32::from(buffer_len) as usize];

                    r.read_exact(&mut data[..])?;

                    Value::string_ascii_from_bytes(data).map_err(|_| "Bad string".into())
                }
                TypePrefix::StringUTF8 => {
                    let mut total_len = [0; 4];
                    r.read_exact(&mut total_len)?;
                    let total_len = BufferLength::try_from(u32::from_be_bytes(total_len))?;

                    let mut data: Vec<u8> = vec![0; u32::from(total_len) as usize];

                    r.read_exact(&mut data[..])?;

                    let value = Value::string_utf8_from_bytes(data)
                        .map_err(|_| "Illegal string_utf8 type".into());

                    if let Some(x) = &expected_type {
                        let passed_test = match (x, &value) {
                            (
                                TypeSignature::SequenceType(SequenceSubtype::StringType(
                                    StringSubtype::UTF8(expected_len),
                                )),
                                Ok(Value::Sequence(SequenceData::String(CharType::UTF8(utf8)))),
                            ) => utf8.data.len() as u32 <= u32::from(expected_len),
                            _ => false,
                        };
                        if !passed_test {
                            return Err(SerializationError::DeserializeExpected(x.clone()));
                        }
                    }

                    value
                }
            }?;

            let mut finished_item = Some(item);
            while let Some(item) = finished_item.take() {
                let stack_bottom = if let Some(stack_item) = stack.pop() {
                    stack_item
                } else {
                    // this should be unreachable!
                    warn!(
                        "Deserializer reached unexpected path: item processed, but deserializer stack does not expect another value";
                        "item" => %item,
                    );
                    return Err("Deserializer processed item, but deserializer stack does not expect another value".into());
                };
                match stack_bottom {
                    DeserializeStackItem::TopLevel { .. } => return Ok(item),
                    DeserializeStackItem::List {
                        mut items,
                        expected_len,
                        expected_type,
                    } => {
                        items.push(item);
                        if expected_len as usize <= items.len() {
                            // list is finished!
                            let finished_list = if let Some(list_type) = expected_type {
                                Value::list_with_type(
                                    &DESERIALIZATION_TYPE_CHECK_EPOCH,
                                    items,
                                    list_type.clone(),
                                )
                                .map_err(|_| "Illegal list type")?
                            } else {
                                Value::cons_list_unsanitized(items)
                                    .map_err(|_| "Illegal list type")?
                            };

                            finished_item.replace(finished_list);
                        } else {
                            // list is not finished, reinsert on stack
                            stack.push(DeserializeStackItem::List {
                                items,
                                expected_len,
                                expected_type,
                            });
                        }
                    }
                    DeserializeStackItem::Tuple {
                        mut items,
                        expected_len,
                        expected_type,
                        next_name,
                        next_sanitize,
                        mut processed_entries,
                    } => {
                        let push_entry = if sanitize {
                            if expected_type.is_some() {
                                // if performing tuple sanitization, don't include a field
                                //  if it was sanitized
                                !next_sanitize
                            } else {
                                // always push the entry if there's no type expectation
                                true
                            }
                        } else {
                            true
                        };
                        let tuple_entry = (next_name, item);
                        if push_entry {
                            items.push(tuple_entry);
                        }
                        processed_entries += 1;
                        if expected_len <= processed_entries {
                            // tuple is finished!
                            let finished_tuple = if let Some(tuple_type) = expected_type {
                                if items.len() != tuple_type.len() as usize {
                                    return Err(SerializationError::DeserializeExpected(
                                        TypeSignature::TupleType(tuple_type),
                                    ));
                                }
                                TupleData::from_data_typed(
                                    &DESERIALIZATION_TYPE_CHECK_EPOCH,
                                    items,
                                    &tuple_type,
                                )
                                .map_err(|_| "Illegal tuple type")
                                .map(Value::from)?
                            } else {
                                TupleData::from_data(items)
                                    .map_err(|_| "Illegal tuple type")
                                    .map(Value::from)?
                            };

                            finished_item.replace(finished_tuple);
                        } else {
                            // tuple is not finished, read the next key name and reinsert on stack
                            let key = ClarityName::deserialize_read(r)?;
                            // figure out if the next (key, value) pair for this
                            //  tuple will be elided (or sanitized) from the tuple.
                            // the logic here is that the next pair should be elided if:
                            //    * `sanitize` parameter is true
                            //    * `tuple_type` is some (i.e., there is an expected type for the
                            //       tuple)
                            //    * `tuple_type` does not contain an entry for `key`
                            let next_sanitize = sanitize
                                && expected_type
                                    .as_ref()
                                    .map(|tt| tt.field_type(&key).is_none())
                                    .unwrap_or(false);
                            stack.push(DeserializeStackItem::Tuple {
                                items,
                                expected_type,
                                expected_len,
                                next_name: key,
                                next_sanitize,
                                processed_entries,
                            });
                        }
                    }
                    DeserializeStackItem::OptionSome { .. } => {
                        let finished_some = Value::some(item).map_err(|_x| "Value too large")?;
                        finished_item.replace(finished_some);
                    }
                    DeserializeStackItem::ResponseOk { .. } => {
                        let finished_some = Value::okay(item).map_err(|_x| "Value too large")?;
                        finished_item.replace(finished_some);
                    }
                    DeserializeStackItem::ResponseErr { .. } => {
                        let finished_some = Value::error(item).map_err(|_x| "Value too large")?;
                        finished_item.replace(finished_some);
                    }
                };
            }
        }

        Err(SerializationError::DeserializationError(
            "Invalid data: stack ran out before finishing parsing".into(),
        ))
    }

    pub fn serialize_write<W: Write>(&self, w: &mut W) -> Result<(), SerializationError> {
        use super::CharType::*;
        use super::PrincipalData::*;
        use super::SequenceData::{self, *};
        use super::Value::*;

        w.write_all(&[TypePrefix::from(self) as u8])?;
        match self {
            Int(value) => w.write_all(&value.to_be_bytes())?,
            UInt(value) => w.write_all(&value.to_be_bytes())?,
            Principal(Standard(data)) => data.serialize_write(w)?,
            Principal(Contract(contract_identifier))
            | CallableContract(CallableData {
                contract_identifier,
                trait_identifier: _,
            }) => {
                contract_identifier.issuer.serialize_write(w)?;
                contract_identifier.name.serialize_write(w)?;
            }
            Response(response) => response.data.serialize_write(w)?,
            // Bool types don't need any more data.
            Bool(_) => {}
            // None types don't need any more data.
            Optional(OptionalData { data: None }) => {}
            Optional(OptionalData { data: Some(value) }) => {
                value.serialize_write(w)?;
            }
            Sequence(List(data)) => {
                let len_bytes = data
                    .len()
                    .map_err(|e| SerializationError::SerializationError(e.to_string()))?
                    .to_be_bytes();
                w.write_all(&len_bytes)?;
                for item in data.data.iter() {
                    item.serialize_write(w)?;
                }
            }
            Sequence(Buffer(value)) => {
                let len_bytes = u32::from(
                    value
                        .len()
                        .map_err(|e| SerializationError::SerializationError(e.to_string()))?,
                )
                .to_be_bytes();
                w.write_all(&len_bytes)?;
                w.write_all(&value.data)?
            }
            Sequence(SequenceData::String(UTF8(value))) => {
                let total_len: u32 = value.data.iter().fold(0u32, |len, c| len + c.len() as u32);
                w.write_all(&(total_len.to_be_bytes()))?;
                for bytes in value.data.iter() {
                    w.write_all(bytes)?
                }
            }
            Sequence(SequenceData::String(ASCII(value))) => {
                let len_bytes = u32::from(
                    value
                        .len()
                        .map_err(|e| SerializationError::SerializationError(e.to_string()))?,
                )
                .to_be_bytes();
                w.write_all(&len_bytes)?;
                w.write_all(&value.data)?
            }
            Tuple(data) => {
                let len_bytes = u32::try_from(data.data_map.len())
                    .map_err(|e| SerializationError::SerializationError(e.to_string()))?
                    .to_be_bytes();
                w.write_all(&len_bytes)?;
                for (key, value) in data.data_map.iter() {
                    key.serialize_write(w)?;
                    value.serialize_write(w)?;
                }
            }
        };

        Ok(())
    }

    /// This function attempts to deserialize a byte buffer into a Clarity Value.
    /// The `expected_type` parameter tells the deserializer to expect (and enforce)
    /// a particular type. `ClarityDB` uses this to ensure that lists, tuples, etc. loaded from the database
    /// have their max-length and other type information set by the type declarations in the contract.
    pub fn try_deserialize_bytes(
        bytes: &Vec<u8>,
        expected: &TypeSignature,
        sanitize: bool,
    ) -> Result<Value, SerializationError> {
        Value::deserialize_read(&mut bytes.as_slice(), Some(expected), sanitize)
    }

    /// This function attempts to deserialize a hex string into a Clarity Value.
    /// The `expected_type` parameter tells the deserializer to expect (and enforce)
    /// a particular type. `ClarityDB` uses this to ensure that lists, tuples, etc. loaded from the database
    /// have their max-length and other type information set by the type declarations in the contract.
    pub fn try_deserialize_hex(
        hex: &str,
        expected: &TypeSignature,
        sanitize: bool,
    ) -> Result<Value, SerializationError> {
        let data = hex_bytes(hex).map_err(|_| "Bad hex string")?;
        Value::try_deserialize_bytes(&data, expected, sanitize)
    }

    /// This function attempts to deserialize a byte buffer into a
    /// Clarity Value, while ensuring that the whole byte buffer is
    /// consumed by the deserialization, erroring if it is not. The
    /// `expected_type` parameter tells the deserializer to expect
    /// (and enforce) a particular type. `ClarityDB` uses this to
    /// ensure that lists, tuples, etc. loaded from the database have
    /// their max-length and other type information set by the type
    /// declarations in the contract.
    pub fn try_deserialize_bytes_exact(
        bytes: &Vec<u8>,
        expected: &TypeSignature,
        sanitize: bool,
    ) -> Result<Value, SerializationError> {
        let input_length = bytes.len();
        let (value, read_count) =
            Value::deserialize_read_count(&mut bytes.as_slice(), Some(expected), sanitize)?;
        if read_count != (input_length as u64) {
            Err(SerializationError::LeftoverBytesInDeserialization)
        } else {
            Ok(value)
        }
    }

    /// Try to deserialize a value without type information. This *does not* perform sanitization
    ///  so it should not be used when decoding clarity database values.
    fn try_deserialize_bytes_untyped(bytes: &Vec<u8>) -> Result<Value, SerializationError> {
        Value::deserialize_read(&mut bytes.as_slice(), None, false)
    }

    /// Try to deserialize a value from a hex string without type information. This *does not*
    /// perform sanitization.
    pub fn try_deserialize_hex_untyped(hex: &str) -> Result<Value, SerializationError> {
        let hex = hex.strip_prefix("0x").unwrap_or(hex);
        let data = hex_bytes(hex).map_err(|_| "Bad hex string")?;
        Value::try_deserialize_bytes_untyped(&data)
    }

    pub fn serialized_size(&self) -> Result<u32, SerializationError> {
        let mut counter = WriteCounter { count: 0 };
        self.serialize_write(&mut counter).map_err(|_| {
            SerializationError::DeserializationError(
                "Error: Failed to count serialization length of Clarity value".into(),
            )
        })?;
        Ok(counter.count)
    }
}

/// A writer that just counts the bytes written
struct WriteCounter {
    count: u32,
}

impl Write for WriteCounter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let input: u32 = buf.len().try_into().map_err(|_e| {
            std::io::Error::new(
                std::io::ErrorKind::Other,
                "Serialization size would overflow u32",
            )
        })?;
        self.count = self.count.checked_add(input).ok_or_else(|| {
            std::io::Error::new(
                std::io::ErrorKind::Other,
                "Serialization size would overflow u32",
            )
        })?;
        Ok(input as usize)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

impl Value {
    pub fn serialize_to_vec(&self) -> Result<Vec<u8>, InterpreterError> {
        let mut byte_serialization = Vec::new();
        self.serialize_write(&mut byte_serialization)
            .map_err(|_| InterpreterError::Expect("IOError filling byte buffer.".into()))?;
        Ok(byte_serialization)
    }

    /// This does *not* perform any data sanitization
    pub fn serialize_to_hex(&self) -> Result<String, InterpreterError> {
        let byte_serialization = self.serialize_to_vec()?;
        Ok(to_hex(byte_serialization.as_slice()))
    }

    /// Sanitize `value` against pre-2.4 serialization
    ///
    /// Returns Some if the sanitization is successful, or was not necessary.
    /// Returns None if the sanitization failed.
    ///
    /// Returns the sanitized value _and_ whether or not sanitization was required.
    pub fn sanitize_value(
        epoch: &StacksEpochId,
        expected: &TypeSignature,
        value: Value,
    ) -> Option<(Value, bool)> {
        // in epochs before 2.4, perform no sanitization
        if !epoch.value_sanitizing() {
            return Some((value, false));
        }
        let (output, did_sanitize) = match value {
            Value::Sequence(SequenceData::List(l)) => {
                let lt = match expected {
                    TypeSignature::SequenceType(SequenceSubtype::ListType(lt)) => lt,
                    _ => return None,
                };
                // if cannot compute l.len(), sanitization fails, so use ? operator can short return
                if l.len().ok()? > lt.get_max_len() {
                    return None;
                }
                let mut sanitized_items = Vec::with_capacity(l.data.len());
                let mut did_sanitize_children = false;
                for item in l.data.into_iter() {
                    let (sanitized_item, did_sanitize) =
                        Self::sanitize_value(epoch, lt.get_list_item_type(), item)?;
                    sanitized_items.push(sanitized_item);
                    did_sanitize_children = did_sanitize_children || did_sanitize;
                }
                // do not sanitize list before construction here, because we're already sanitizing
                let output_list = Value::cons_list_unsanitized(sanitized_items).ok()?;
                (output_list, did_sanitize_children)
            }
            Value::Tuple(tuple_data) => {
                let tt = match expected {
                    TypeSignature::TupleType(tt) => tt,
                    _ => return None,
                };
                let type_map = tt.get_type_map();
                let mut sanitized_tuple_entries = Vec::with_capacity(type_map.len());
                let original_tuple_len = tuple_data.len();
                let mut tuple_data_map = tuple_data.data_map;
                let mut did_sanitize_children = false;
                for (key, expect_key_type) in type_map.iter() {
                    let field_data = tuple_data_map.remove(key)?;
                    let (sanitized_field, did_sanitize) =
                        Self::sanitize_value(epoch, expect_key_type, field_data)?;
                    sanitized_tuple_entries.push((key.clone(), sanitized_field));
                    did_sanitize_children = did_sanitize_children || did_sanitize;
                }
                if sanitized_tuple_entries.len() as u64 != tt.len() {
                    // this code should be unreachable, because I think any case that
                    //    could trigger this would have returned None earlier
                    warn!("Sanitizer handled path that should have errored earlier, skipping sanitization");
                    return None;
                }
                let did_sanitize_tuple = did_sanitize_children || (tt.len() != original_tuple_len);
                (
                    Value::Tuple(TupleData::from_data(sanitized_tuple_entries).ok()?),
                    did_sanitize_tuple,
                )
            }
            Value::Optional(opt_data) => {
                let inner_type = match expected {
                    TypeSignature::OptionalType(inner_type) => inner_type,
                    _ => return None,
                };
                let some_data = match opt_data.data {
                    Some(data) => *data,
                    None => return Some((Value::none(), false)),
                };
                let (sanitized_data, did_sanitize_child) =
                    Self::sanitize_value(epoch, inner_type, some_data)?;
                (Value::some(sanitized_data).ok()?, did_sanitize_child)
            }
            Value::Response(response) => {
                let rt = match expected {
                    TypeSignature::ResponseType(rt) => rt,
                    _ => return None,
                };

                let response_ok = response.committed;
                let response_data = *response.data;
                let inner_type = if response_ok { &rt.0 } else { &rt.1 };
                let (sanitized_inner, did_sanitize_child) =
                    Self::sanitize_value(epoch, inner_type, response_data)?;
                let sanitized_resp = if response_ok {
                    Value::okay(sanitized_inner)
                } else {
                    Value::error(sanitized_inner)
                };
                (sanitized_resp.ok()?, did_sanitize_child)
            }
            value => {
                if expected.admits(epoch, &value).ok()? {
                    return Some((value, false));
                } else {
                    return None;
                }
            }
        };

        if expected.admits(epoch, &output).ok()? {
            Some((output, did_sanitize))
        } else {
            None
        }
    }
}

impl ClaritySerializable for u32 {
    fn serialize(&self) -> String {
        to_hex(&self.to_be_bytes())
    }
}

impl ClarityDeserializable<u32> for u32 {
    fn deserialize(input: &str) -> Result<Self, ClarityError> {
        let bytes = hex_bytes(&input).map_err(|_| {
            InterpreterError::Expect("u32 deserialization: failed decoding bytes.".into())
        })?;
        assert_eq!(bytes.len(), 4);
        Ok(u32::from_be_bytes(bytes[0..4].try_into().map_err(
            |_| InterpreterError::Expect("u32 deserialization: failed reading.".into()),
        )?))
    }
}

/// Note: the StacksMessageCodec implementation for Clarity values *does not*
///       sanitize its serialization or deserialization.
impl StacksMessageCodec for Value {
    fn consensus_serialize<W: Write>(&self, fd: &mut W) -> Result<(), codec_error> {
        self.serialize_write(fd).map_err(|e| match e {
            SerializationError::IOError(io_e) => codec_error::WriteError(io_e.err),
            other => codec_error::SerializeError(other.to_string()),
        })
    }

    fn consensus_deserialize<R: Read>(fd: &mut R) -> Result<Value, codec_error> {
        Value::deserialize_read(fd, None, false).map_err(|e| match e {
            SerializationError::IOError(e) => codec_error::ReadError(e.err),
            _ => codec_error::DeserializeError(format!("Failed to decode clarity value: {:?}", &e)),
        })
    }
}

#[cfg(test)]
pub mod tests {
    use std::io::Write;

    use rstest::rstest;
    use rstest_reuse::{self, *};
    use stacks_common::types::StacksEpochId;

    use super::super::*;
    use super::SerializationError;
    use crate::vm::database::{ClarityDeserializable, ClaritySerializable, RollbackWrapper};
    use crate::vm::errors::Error;
    use crate::vm::tests::test_clarity_versions;
    use crate::vm::types::TypeSignature::{BoolType, IntType};
    use crate::vm::ClarityVersion;

    fn buff_type(size: u32) -> TypeSignature {
        TypeSignature::SequenceType(SequenceSubtype::BufferType(size.try_into().unwrap()))
    }

    fn test_deser_ser(v: Value) {
        assert_eq!(
            &v,
            &Value::try_deserialize_hex(
                &v.serialize_to_hex().unwrap(),
                &TypeSignature::type_of(&v).unwrap(),
                false
            )
            .unwrap()
        );
        assert_eq!(
            &v,
            &Value::try_deserialize_hex_untyped(&v.serialize_to_hex().unwrap()).unwrap()
        );
        // test the serialized_size implementation
        assert_eq!(
            v.serialized_size().unwrap(),
            v.serialize_to_hex().unwrap().len() as u32 / 2,
            "serialized_size() should return the byte length of the serialization (half the length of the hex encoding)",
        );
    }

    fn test_deser_u32_helper(num: u32) {
        assert_eq!(num, u32::deserialize(&num.serialize()).unwrap());
    }

    fn test_bad_expectation(v: Value, e: TypeSignature) {
        assert!(
            match Value::try_deserialize_hex(&v.serialize_to_hex().unwrap(), &e, false).unwrap_err()
            {
                SerializationError::DeserializeExpected(_) => true,
                _ => false,
            }
        )
    }

    #[test]
    fn test_deser_u32() {
        test_deser_u32_helper(0);
        test_deser_u32_helper(10);
        test_deser_u32_helper(42);
        test_deser_u32_helper(10992);
        test_deser_u32_helper(10992);
        test_deser_u32_helper(262144);
        test_deser_u32_helper(134217728);
    }

    #[apply(test_clarity_versions)]
    fn test_lists(#[case] version: ClarityVersion, #[case] epoch: StacksEpochId) {
        let list_list_int = Value::list_from(vec![Value::list_from(vec![
            Value::Int(1),
            Value::Int(2),
            Value::Int(3),
        ])
        .unwrap()])
        .unwrap();

        // Should be legal!
        Value::try_deserialize_hex(
            &Value::list_from(vec![])
                .unwrap()
                .serialize_to_hex()
                .unwrap(),
            &TypeSignature::from_string("(list 2 (list 3 int))", version, epoch),
            false,
        )
        .unwrap();
        Value::try_deserialize_hex(
            &list_list_int.serialize_to_hex().unwrap(),
            &TypeSignature::from_string("(list 2 (list 3 int))", version, epoch),
            false,
        )
        .unwrap();
        Value::try_deserialize_hex(
            &list_list_int.serialize_to_hex().unwrap(),
            &TypeSignature::from_string("(list 1 (list 4 int))", version, epoch),
            false,
        )
        .unwrap();

        test_deser_ser(list_list_int.clone());
        test_deser_ser(Value::list_from(vec![]).unwrap());
        test_bad_expectation(list_list_int.clone(), TypeSignature::BoolType);
        // inner type isn't expected
        test_bad_expectation(
            list_list_int.clone(),
            TypeSignature::from_string("(list 1 (list 4 uint))", version, epoch),
        );
        // child list longer than expected
        test_bad_expectation(
            list_list_int.clone(),
            TypeSignature::from_string("(list 1 (list 2 uint))", version, epoch),
        );
        // parent list longer than expected
        test_bad_expectation(
            list_list_int,
            TypeSignature::from_string("(list 0 (list 2 uint))", version, epoch),
        );

        // make a list too large for the type itself!
        //   this describes a list of size 1+MAX_VALUE_SIZE of Value::Bool(true)'s
        let mut too_big = vec![3u8; 6 + MAX_VALUE_SIZE as usize];
        // list prefix
        too_big[0] = 11;
        // list length
        Write::write_all(
            &mut too_big.get_mut(1..5).unwrap(),
            &(1 + MAX_VALUE_SIZE).to_be_bytes(),
        )
        .unwrap();

        assert_eq!(
            Value::deserialize_read(&mut too_big.as_slice(), None, false).unwrap_err(),
            "Illegal list type".into()
        );

        // make a list that says it is longer than it is!
        //   this describes a list of size MAX_VALUE_SIZE of Value::Bool(true)'s, but is actually only 59 bools.
        let mut eof = vec![3u8; 64_usize];
        // list prefix
        eof[0] = 11;
        // list length
        Write::write_all(
            &mut eof.get_mut(1..5).unwrap(),
            &(MAX_VALUE_SIZE).to_be_bytes(),
        )
        .unwrap();

        /*
         * jude -- this should return an IOError
        assert_eq!(
            Value::deserialize_read(&mut eof.as_slice(), None).unwrap_err(),
            "Unexpected end of byte stream".into());
        */

        match Value::deserialize_read(&mut eof.as_slice(), None, false) {
            Ok(_) => panic!("Accidentally parsed truncated slice"),
            Err(eres) => match eres {
                SerializationError::IOError(ioe) => match ioe.err.kind() {
                    std::io::ErrorKind::UnexpectedEof => {}
                    _ => panic!("Invalid I/O error: {:?}", &ioe),
                },
                _ => panic!("Invalid deserialize error: {:?}", &eres),
            },
        }
    }

    #[test]
    fn test_bools() {
        test_deser_ser(Value::Bool(false));
        test_deser_ser(Value::Bool(true));

        test_bad_expectation(Value::Bool(false), TypeSignature::IntType);
        test_bad_expectation(Value::Bool(true), TypeSignature::IntType);
    }

    #[test]
    fn test_ints() {
        test_deser_ser(Value::Int(0));
        test_deser_ser(Value::Int(1));
        test_deser_ser(Value::Int(-1));
        test_deser_ser(Value::Int(i128::MAX));
        test_deser_ser(Value::Int(i128::MIN));

        test_bad_expectation(Value::Int(1), TypeSignature::UIntType);
    }

    #[test]
    fn test_uints() {
        test_deser_ser(Value::UInt(0));
        test_deser_ser(Value::UInt(1));
        test_deser_ser(Value::UInt(u128::MAX));
        test_deser_ser(Value::UInt(u128::MIN));

        test_bad_expectation(Value::UInt(1), TypeSignature::IntType);
    }

    #[apply(test_clarity_versions)]
    fn test_opts(#[case] version: ClarityVersion, #[case] epoch: StacksEpochId) {
        test_deser_ser(Value::none());
        test_deser_ser(Value::some(Value::Int(15)).unwrap());

        test_bad_expectation(Value::none(), TypeSignature::IntType);
        test_bad_expectation(Value::some(Value::Int(15)).unwrap(), TypeSignature::IntType);
        // bad expected _contained_ type
        test_bad_expectation(
            Value::some(Value::Int(15)).unwrap(),
            TypeSignature::from_string("(optional uint)", version, epoch),
        );
    }

    #[apply(test_clarity_versions)]
    fn test_resp(#[case] version: ClarityVersion, #[case] epoch: StacksEpochId) {
        test_deser_ser(Value::okay(Value::Int(15)).unwrap());
        test_deser_ser(Value::error(Value::Int(15)).unwrap());

        // Bad expected types.
        test_bad_expectation(Value::okay(Value::Int(15)).unwrap(), TypeSignature::IntType);
        test_bad_expectation(
            Value::okay(Value::Int(15)).unwrap(),
            TypeSignature::from_string("(response uint int)", version, epoch),
        );
        test_bad_expectation(
            Value::error(Value::Int(15)).unwrap(),
            TypeSignature::from_string("(response int uint)", version, epoch),
        );
    }

    #[apply(test_clarity_versions)]
    fn test_buffs(#[case] version: ClarityVersion, #[case] epoch: StacksEpochId) {
        test_deser_ser(Value::buff_from(vec![0, 0, 0, 0]).unwrap());
        test_deser_ser(Value::buff_from(vec![0xde, 0xad, 0xbe, 0xef]).unwrap());
        test_deser_ser(Value::buff_from(vec![0, 0xde, 0xad, 0xbe, 0xef, 0]).unwrap());

        test_bad_expectation(
            Value::buff_from(vec![0, 0xde, 0xad, 0xbe, 0xef, 0]).unwrap(),
            TypeSignature::BoolType,
        );

        // fail because we expect a shorter buffer
        test_bad_expectation(
            Value::buff_from(vec![0, 0xde, 0xad, 0xbe, 0xef, 0]).unwrap(),
            TypeSignature::from_string("(buff 2)", version, epoch),
        );
    }

    #[apply(test_clarity_versions)]
    fn test_string_ascii(#[case] version: ClarityVersion, #[case] epoch: StacksEpochId) {
        test_deser_ser(Value::string_ascii_from_bytes(vec![61, 62, 63, 64]).unwrap());

        // fail because we expect a shorter string
        test_bad_expectation(
            Value::string_ascii_from_bytes(vec![61, 62, 63, 64]).unwrap(),
            TypeSignature::from_string("(string-ascii 3)", version, epoch),
        );
    }

    #[apply(test_clarity_versions)]
    fn test_string_utf8(#[case] version: ClarityVersion, #[case] epoch: StacksEpochId) {
        test_deser_ser(Value::string_utf8_from_bytes(vec![61, 62, 63, 64]).unwrap());
        test_deser_ser(
            Value::string_utf8_from_bytes(vec![61, 62, 63, 240, 159, 164, 151]).unwrap(),
        );

        // fail because we expect a shorter string
        test_bad_expectation(
            Value::string_utf8_from_bytes(vec![61, 62, 63, 64]).unwrap(),
            TypeSignature::from_string("(string-utf8 3)", version, epoch),
        );

        test_bad_expectation(
            Value::string_utf8_from_bytes(vec![61, 62, 63, 240, 159, 164, 151]).unwrap(),
            TypeSignature::from_string("(string-utf8 3)", version, epoch),
        );
    }

    #[test]
    fn test_tuples() {
        let t_1 = Value::from(
            TupleData::from_data(vec![
                ("a".into(), Value::Int(1)),
                ("b".into(), Value::Int(1)),
            ])
            .unwrap(),
        );
        let t_0 = Value::from(
            TupleData::from_data(vec![
                ("b".into(), Value::Int(1)),
                ("a".into(), Value::Int(1)),
            ])
            .unwrap(),
        );
        let t_2 = Value::from(
            TupleData::from_data(vec![
                ("a".into(), Value::Int(1)),
                ("b".into(), Value::Bool(true)),
            ])
            .unwrap(),
        );
        let t_3 = Value::from(TupleData::from_data(vec![("a".into(), Value::Int(1))]).unwrap());
        let t_4 = Value::from(
            TupleData::from_data(vec![
                ("a".into(), Value::Int(1)),
                ("c".into(), Value::Bool(true)),
            ])
            .unwrap(),
        );

        test_deser_ser(t_0.clone());
        test_deser_ser(t_1.clone());
        test_deser_ser(t_2.clone());
        test_deser_ser(t_3.clone());

        test_bad_expectation(t_0.clone(), TypeSignature::BoolType);

        // t_0 and t_1 are actually the same
        assert_eq!(
            Value::try_deserialize_hex(
                &t_1.serialize_to_hex().unwrap(),
                &TypeSignature::type_of(&t_0).unwrap(),
                false
            )
            .unwrap(),
            Value::try_deserialize_hex(
                &t_0.serialize_to_hex().unwrap(),
                &TypeSignature::type_of(&t_0).unwrap(),
                false
            )
            .unwrap()
        );

        // field number not equal to expectations
        assert!(match Value::try_deserialize_hex(
            &t_3.serialize_to_hex().unwrap(),
            &TypeSignature::type_of(&t_1).unwrap(),
            false
        )
        .unwrap_err()
        {
            SerializationError::DeserializeExpected(_) => true,
            _ => false,
        });

        // field type mismatch
        assert!(match Value::try_deserialize_hex(
            &t_2.serialize_to_hex().unwrap(),
            &TypeSignature::type_of(&t_1).unwrap(),
            false
        )
        .unwrap_err()
        {
            SerializationError::DeserializeExpected(_) => true,
            _ => false,
        });

        // field not-present in expected
        assert!(match Value::try_deserialize_hex(
            &t_1.serialize_to_hex().unwrap(),
            &TypeSignature::type_of(&t_4).unwrap(),
            false
        )
        .unwrap_err()
        {
            SerializationError::DeserializeExpected(_) => true,
            _ => false,
        });
    }

    #[apply(test_clarity_versions)]
    fn test_sanitization(#[case] version: ClarityVersion, #[case] epoch: StacksEpochId) {
        let v_1 = Value::list_from(vec![
            TupleData::from_data(vec![("b".into(), Value::Int(2))])
                .unwrap()
                .into(),
            TupleData::from_data(vec![
                ("a".into(), Value::Int(1)),
                ("b".into(), Value::Int(4)),
                ("c".into(), Value::Int(3)),
            ])
            .unwrap()
            .into(),
        ])
        .unwrap();
        let v_1_good = Value::list_from(vec![
            TupleData::from_data(vec![("b".into(), Value::Int(2))])
                .unwrap()
                .into(),
            TupleData::from_data(vec![("b".into(), Value::Int(4))])
                .unwrap()
                .into(),
        ])
        .unwrap();

        let t_1_good = TypeSignature::from_string("(list 5 (tuple (b int)))", version, epoch);
        let t_1_bad_0 =
            TypeSignature::from_string("(list 5 (tuple (b int) (a int)))", version, epoch);
        let t_1_bad_1 = TypeSignature::from_string("(list 5 (tuple (b uint)))", version, epoch);

        let v_2 = TupleData::from_data(vec![
            (
                "list-1".into(),
                Value::list_from(vec![
                    TupleData::from_data(vec![("b".into(), Value::Int(2))])
                        .unwrap()
                        .into(),
                    TupleData::from_data(vec![
                        ("a".into(), Value::Int(1)),
                        ("b".into(), Value::Int(4)),
                        ("c".into(), Value::Int(3)),
                    ])
                    .unwrap()
                    .into(),
                ])
                .unwrap(),
            ),
            (
                "list-2".into(),
                Value::list_from(vec![
                    TupleData::from_data(vec![("c".into(), Value::Int(2))])
                        .unwrap()
                        .into(),
                    TupleData::from_data(vec![
                        ("a".into(), Value::Int(1)),
                        ("b".into(), Value::Int(4)),
                        ("c".into(), Value::Int(3)),
                    ])
                    .unwrap()
                    .into(),
                ])
                .unwrap(),
            ),
        ])
        .unwrap()
        .into();

        let v_2_good = TupleData::from_data(vec![
            (
                "list-1".into(),
                Value::list_from(vec![
                    TupleData::from_data(vec![("b".into(), Value::Int(2))])
                        .unwrap()
                        .into(),
                    TupleData::from_data(vec![("b".into(), Value::Int(4))])
                        .unwrap()
                        .into(),
                ])
                .unwrap(),
            ),
            (
                "list-2".into(),
                Value::list_from(vec![
                    TupleData::from_data(vec![("c".into(), Value::Int(2))])
                        .unwrap()
                        .into(),
                    TupleData::from_data(vec![("c".into(), Value::Int(3))])
                        .unwrap()
                        .into(),
                ])
                .unwrap(),
            ),
        ])
        .unwrap()
        .into();

        let t_2_good = TypeSignature::from_string(
            "(tuple (list-2 (list 2 (tuple (c int)))) (list-1 (list 5 (tuple (b int)))))",
            version,
            epoch,
        );
        let t_2_bad_0 = TypeSignature::from_string(
            "(tuple (list-2 (list 2 (tuple (c int)))) (list-1 (list 5 (tuple (a int)))))",
            version,
            epoch,
        );
        let t_2_bad_1 = TypeSignature::from_string(
            "(tuple (list-2 (list 1 (tuple (c int)))) (list-1 (list 5 (tuple (b int)))))",
            version,
            epoch,
        );

        let v_3 = Value::some(
            TupleData::from_data(vec![
                ("a".into(), Value::Int(1)),
                ("b".into(), Value::Int(4)),
                ("c".into(), Value::Int(3)),
            ])
            .unwrap()
            .into(),
        )
        .unwrap();

        let v_3_good = Value::some(
            TupleData::from_data(vec![
                ("a".into(), Value::Int(1)),
                ("b".into(), Value::Int(4)),
            ])
            .unwrap()
            .into(),
        )
        .unwrap();

        let t_3_good =
            TypeSignature::from_string("(optional (tuple (a int) (b int)))", version, epoch);
        let t_3_bad_0 =
            TypeSignature::from_string("(optional (tuple (a uint) (b int)))", version, epoch);
        let t_3_bad_1 =
            TypeSignature::from_string("(optional (tuple (d int) (b int)))", version, epoch);

        let v_4 = Value::list_from(vec![
            TupleData::from_data(vec![("b".into(), Value::some(Value::Int(2)).unwrap())])
                .unwrap()
                .into(),
            TupleData::from_data(vec![
                ("a".into(), Value::some(Value::Int(1)).unwrap()),
                ("b".into(), Value::none()),
                ("c".into(), Value::some(Value::Int(3)).unwrap()),
            ])
            .unwrap()
            .into(),
        ])
        .unwrap();
        let v_4_good = Value::list_from(vec![
            TupleData::from_data(vec![("b".into(), Value::some(Value::Int(2)).unwrap())])
                .unwrap()
                .into(),
            TupleData::from_data(vec![("b".into(), Value::none())])
                .unwrap()
                .into(),
        ])
        .unwrap();

        let t_4_good =
            TypeSignature::from_string("(list 5 (tuple (b (optional int))))", version, epoch);
        let t_4_bad_0 = TypeSignature::from_string(
            "(list 5 (tuple (b (optional int)) (a (optional int))))",
            version,
            epoch,
        );
        let t_4_bad_1 =
            TypeSignature::from_string("(list 5 (tuple (b (optional uint))))", version, epoch);

        let v_5 = Value::okay(
            Value::list_from(vec![
                TupleData::from_data(vec![("b".into(), Value::some(Value::Int(2)).unwrap())])
                    .unwrap()
                    .into(),
                TupleData::from_data(vec![
                    ("a".into(), Value::some(Value::Int(1)).unwrap()),
                    ("b".into(), Value::none()),
                    ("c".into(), Value::some(Value::Int(3)).unwrap()),
                ])
                .unwrap()
                .into(),
            ])
            .unwrap(),
        )
        .unwrap();
        let v_5_good = Value::okay(
            Value::list_from(vec![
                TupleData::from_data(vec![("b".into(), Value::some(Value::Int(2)).unwrap())])
                    .unwrap()
                    .into(),
                TupleData::from_data(vec![("b".into(), Value::none())])
                    .unwrap()
                    .into(),
            ])
            .unwrap(),
        )
        .unwrap();

        let t_5_good_0 = TypeSignature::from_string(
            "(response (list 5 (tuple (b (optional int)))) int)",
            version,
            epoch,
        );
        let t_5_good_1 = TypeSignature::from_string(
            "(response (list 2 (tuple (b (optional int)))) int)",
            version,
            epoch,
        );
        let t_5_good_2 = TypeSignature::from_string(
            "(response (list 2 (tuple (b (optional int)))) bool)",
            version,
            epoch,
        );
        let t_5_bad_0 = TypeSignature::from_string(
            "(response (list 5 (tuple (b (optional int)) (a (optional int)))) uint)",
            version,
            epoch,
        );
        let t_5_bad_1 = TypeSignature::from_string(
            "(response (list 5 (tuple (b (optional uint)))) int)",
            version,
            epoch,
        );
        let t_5_bad_2 = TypeSignature::from_string(
            "(response int (list 5 (tuple (b (optional int)))))",
            version,
            epoch,
        );
        let t_5_bad_3 = TypeSignature::from_string(
            "(list 5 (tuple (b (optional int)) (a (optional int))))",
            version,
            epoch,
        );

        let v_6 = Value::error(
            Value::list_from(vec![
                TupleData::from_data(vec![("b".into(), Value::some(Value::Int(2)).unwrap())])
                    .unwrap()
                    .into(),
                TupleData::from_data(vec![
                    ("a".into(), Value::some(Value::Int(1)).unwrap()),
                    ("b".into(), Value::none()),
                    ("c".into(), Value::some(Value::Int(3)).unwrap()),
                ])
                .unwrap()
                .into(),
            ])
            .unwrap(),
        )
        .unwrap();
        let v_6_good = Value::error(
            Value::list_from(vec![
                TupleData::from_data(vec![("b".into(), Value::some(Value::Int(2)).unwrap())])
                    .unwrap()
                    .into(),
                TupleData::from_data(vec![("b".into(), Value::none())])
                    .unwrap()
                    .into(),
            ])
            .unwrap(),
        )
        .unwrap();

        let t_6_good_0 = TypeSignature::from_string(
            "(response int (list 5 (tuple (b (optional int)))))",
            version,
            epoch,
        );
        let t_6_good_1 = TypeSignature::from_string(
            "(response int (list 2 (tuple (b (optional int)))))",
            version,
            epoch,
        );
        let t_6_good_2 = TypeSignature::from_string(
            "(response bool (list 2 (tuple (b (optional int)))))",
            version,
            epoch,
        );
        let t_6_bad_0 = TypeSignature::from_string(
            "(response uint (list 5 (tuple (b (optional int)) (a (optional int)))))",
            version,
            epoch,
        );
        let t_6_bad_1 = TypeSignature::from_string(
            "(response int (list 5 (tuple (b (optional uint)))))",
            version,
            epoch,
        );
        let t_6_bad_2 = TypeSignature::from_string(
            "(response (list 5 (tuple (b (optional int)))) int)",
            version,
            epoch,
        );
        let t_6_bad_3 = TypeSignature::from_string(
            "(list 5 (tuple (b (optional int)) (a (optional int))))",
            version,
            epoch,
        );

        let test_cases = [
            (v_1, v_1_good, t_1_good, vec![t_1_bad_0, t_1_bad_1]),
            (v_2, v_2_good, t_2_good, vec![t_2_bad_0, t_2_bad_1]),
            (v_3, v_3_good, t_3_good, vec![t_3_bad_0, t_3_bad_1]),
            (v_4, v_4_good, t_4_good, vec![t_4_bad_0, t_4_bad_1]),
            (
                v_5.clone(),
                v_5_good.clone(),
                t_5_good_0,
                vec![t_5_bad_0, t_5_bad_1, t_5_bad_2, t_5_bad_3],
            ),
            (v_5.clone(), v_5_good.clone(), t_5_good_1, vec![]),
            (v_5, v_5_good, t_5_good_2, vec![]),
            (
                v_6.clone(),
                v_6_good.clone(),
                t_6_good_0,
                vec![t_6_bad_0, t_6_bad_1, t_6_bad_2, t_6_bad_3],
            ),
            (v_6.clone(), v_6_good.clone(), t_6_good_1, vec![]),
            (v_6, v_6_good, t_6_good_2, vec![]),
        ];

        for (input_val, expected_out, good_type, bad_types) in test_cases.iter() {
            eprintln!(
                "Testing {}. Expected sanitization = {}",
                input_val, expected_out
            );
            let serialized = input_val.serialize_to_hex().unwrap();

            let result =
                RollbackWrapper::deserialize_value(&serialized, good_type, &epoch).map(|x| x.value);
            if epoch < StacksEpochId::Epoch24 {
                let error = result.unwrap_err();
                assert!(matches!(error, SerializationError::DeserializeExpected(_)));
            } else {
                let value = result.unwrap();
                assert_eq!(&value, expected_out);
            }

            for bad_type in bad_types.iter() {
                eprintln!("Testing bad type: {}", bad_type);
                let result = RollbackWrapper::deserialize_value(&serialized, bad_type, &epoch);
                let error = result.unwrap_err();
                assert!(matches!(error, SerializationError::DeserializeExpected(_)));
            }

            // now test the value::sanitize routine
            let result = Value::sanitize_value(&epoch, good_type, input_val.clone());
            if epoch < StacksEpochId::Epoch24 {
                let (value, did_sanitize) = result.unwrap();
                assert_eq!(&value, input_val);
                assert!(!did_sanitize, "Should not sanitize before epoch-2.4");
            } else {
                let (value, did_sanitize) = result.unwrap();
                assert_eq!(&value, expected_out);
                assert!(did_sanitize, "Should have sanitized");
            }

            for bad_type in bad_types.iter() {
                eprintln!("Testing bad type: {}", bad_type);
                let result = Value::sanitize_value(&epoch, bad_type, input_val.clone());
                if epoch < StacksEpochId::Epoch24 {
                    let (value, did_sanitize) = result.unwrap();
                    assert_eq!(&value, input_val);
                    assert!(!did_sanitize, "Should not sanitize before epoch-2.4");
                } else {
                    assert!(result.is_none());
                }
            }
        }
    }

    #[test]
    fn test_vectors() {
        let tests = [
            ("1010", Err("Bad type prefix".into())),
            ("0000000000000000000000000000000001", Ok(Value::Int(1))),
            ("00ffffffffffffffffffffffffffffffff", Ok(Value::Int(-1))),
            ("0100000000000000000000000000000001", Ok(Value::UInt(1))),
            ("0200000004deadbeef", Ok(Value::buff_from(vec![0xde, 0xad, 0xbe, 0xef])
                                      .unwrap())),
            ("03", Ok(Value::Bool(true))),
            ("04", Ok(Value::Bool(false))),
            ("050011deadbeef11ababffff11deadbeef11ababffff", Ok(
                StandardPrincipalData(
                    0x00,
                    [0x11, 0xde, 0xad, 0xbe, 0xef, 0x11, 0xab, 0xab, 0xff, 0xff,
                     0x11, 0xde, 0xad, 0xbe, 0xef, 0x11, 0xab, 0xab, 0xff, 0xff]).into())),
            ("060011deadbeef11ababffff11deadbeef11ababffff0461626364", Ok(
                QualifiedContractIdentifier::new(
                    StandardPrincipalData(
                        0x00,
                        [0x11, 0xde, 0xad, 0xbe, 0xef, 0x11, 0xab, 0xab, 0xff, 0xff,
                         0x11, 0xde, 0xad, 0xbe, 0xef, 0x11, 0xab, 0xab, 0xff, 0xff]),
                    "abcd".into()).into())),
            ("0700ffffffffffffffffffffffffffffffff", Ok(Value::okay(Value::Int(-1)).unwrap())),
            ("0800ffffffffffffffffffffffffffffffff", Ok(Value::error(Value::Int(-1)).unwrap())),
            ("09", Ok(Value::none())),
            ("0a00ffffffffffffffffffffffffffffffff", Ok(Value::some(Value::Int(-1)).unwrap())),
            ("0b0000000400000000000000000000000000000000010000000000000000000000000000000002000000000000000000000000000000000300fffffffffffffffffffffffffffffffc",
             Ok(Value::list_from(vec![
                 Value::Int(1), Value::Int(2), Value::Int(3), Value::Int(-4)]).unwrap())),
            ("0c000000020362617a0906666f6f62617203",
             Ok(Value::from(TupleData::from_data(vec![
                 ("baz".into(), Value::none()), ("foobar".into(), Value::Bool(true))]).unwrap())))
        ];

        for (test, expected) in tests.iter() {
            if let Ok(x) = expected {
                assert_eq!(test, &x.serialize_to_hex().unwrap());
            }
            assert_eq!(expected, &Value::try_deserialize_hex_untyped(test));
            assert_eq!(
                expected,
                &Value::try_deserialize_hex_untyped(&format!("0x{}", test))
            );
        }

        // test the serialized_size implementation
        for (test, expected) in tests.iter() {
            if let Ok(value) = expected {
                assert_eq!(
                    value.serialized_size().unwrap(),
                    test.len() as u32 / 2,
                    "serialized_size() should return the byte length of the serialization (half the length of the hex encoding)",
                );
            }
        }
    }

    #[test]
    fn try_deser_large_list() {
        let buff = vec![
            11, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        ];

        assert_eq!(
            Value::try_deserialize_bytes_untyped(&buff).unwrap_err(),
            SerializationError::DeserializationError("Illegal list type".to_string())
        );
    }

    #[test]
    fn try_deser_large_tuple() {
        let buff = vec![
            12, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        ];

        assert_eq!(
            Value::try_deserialize_bytes_untyped(&buff).unwrap_err(),
            SerializationError::DeserializationError("Illegal tuple type".to_string())
        );
    }

    #[test]
    fn try_overflow_stack() {
        let input = "08080808080808080808070707080807080808080808080708080808080708080707080707080807080808080808080708080808080708080707080708070807080808080808080708080808080708080708080808080808080807070807080808080808070808070707080807070808070808080808070808070708070807080808080808080707080708070807080708080808080808070808080808070808070808080808080808080707080708080808080807080807070708080707080807080808080807080807070807080708080808080808070708070808080808080708080707070808070708080807080807070708";
        assert_eq!(
            Err(CheckErrors::TypeSignatureTooDeep.into()),
            Value::try_deserialize_hex_untyped(input)
        );
    }

    #[test]
    fn test_principals() {
        let issuer =
            PrincipalData::parse_standard_principal("SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G")
                .unwrap();
        let standard_p = Value::from(issuer.clone());

        let contract_identifier = QualifiedContractIdentifier::new(issuer, "foo".into());
        let contract_p2 = Value::from(PrincipalData::Contract(contract_identifier));

        test_deser_ser(contract_p2.clone());
        test_deser_ser(standard_p.clone());

        test_bad_expectation(contract_p2, TypeSignature::BoolType);
        test_bad_expectation(standard_p, TypeSignature::BoolType);
    }
}