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
crate::ix!();

/**
  | Use a macro instead of a function for
  | conditional logging to prevent evaluating
  | arguments when logging is not enabled.
  |
  | NOTE: The lambda captures all local variables
  | by value.
  */
macro_rules! enqueue_and_log_event {
    ($event:ident, 
     $fmt:ident, 
     $name:ident, 
     $($arg:ident),*) => {
        /*
        
            do {                                                       
                auto local_name = (name);                              
                LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__);  
                m_internals->m_schedulerClient.AddToProcessQueue([=] { 
                    LOG_EVENT(fmt, local_name, __VA_ARGS__);           
                    event();                                           
                });                                                    
            } while (0)
        */
    }
}

macro_rules! log_event {
    ($fmt:ident, $($arg:ident),*) => {
        /*
        
            LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
        */
    }
}

//-------------------------------------------[.cpp/bitcoin/src/validation.h]

/**
  | Default for -minrelaytxfee, minimum
  | relay fee for transactions
  |
  */
pub const DEFAULT_MIN_RELAY_TX_FEE: usize = 1000;

/**
  | Default for -limitancestorcount,
  | max number of in-mempool ancestors
  |
  */
pub const DEFAULT_ANCESTOR_LIMIT: usize = 25;

/**
  | Default for -limitancestorsize, maximum
  | kilobytes of tx + all in-mempool ancestors
  |
  */
pub const DEFAULT_ANCESTOR_SIZE_LIMIT: usize = 101;

/**
  | Default for -limitdescendantcount,
  | max number of in-mempool descendants
  |
  */
pub const DEFAULT_DESCENDANT_LIMIT: usize = 25;

/**
  | Default for -limitdescendantsize,
  | maximum kilobytes of in-mempool descendants
  |
  */
pub const DEFAULT_DESCENDANT_SIZE_LIMIT: usize = 101;

/**
  | Default for -mempoolexpiry, expiration
  | time for mempool transactions in hours
  |
  */
pub const DEFAULT_MEMPOOL_EXPIRY: usize = 336;

/**
  | Maximum number of dedicated script-checking
  | threads allowed
  |
  */
pub const MAX_SCRIPTCHECK_THREADS: usize = 15;

/**
  | -par default (number of script-checking
  | threads, 0 = auto)
  |
  */
pub const DEFAULT_SCRIPTCHECK_THREADS: usize = 0;

pub const DEFAULT_MAX_TIP_AGE:         i64 = 24 * 60 * 60;

pub const DEFAULT_CHECKPOINTS_ENABLED: bool = true;
pub const DEFAULT_TXINDEX:             bool = false;
pub const DEFAULT_COINSTATSINDEX:      bool = false;

pub const DEFAULT_BLOCKFILTERINDEX: &'static str = "0";

/**
  | Default for -persistmempool
  |
  */
pub const DEFAULT_PERSIST_MEMPOOL: bool = true;

/**
  | Default for -stopatheight
  |
  */
pub const DEFAULT_STOPATHEIGHT: usize = 0;

/**
  | Block files containing a block-height
  | within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip()
  | will not be pruned.
  |
  */
pub const MIN_BLOCKS_TO_KEEP: usize = 288;

pub const DEFAULT_CHECKBLOCKS: usize = 6;
pub const DEFAULT_CHECKLEVEL:  usize = 3;

/**
  | Require that user allocate at least 550 MiB for
  | block & undo files (blk???.dat and rev???.dat)
  |
  | At 1MB per block, 288 blocks = 288MB.
  |
  | Add 15% for Undo data = 331MB
  |
  | Add 20% for Orphan block rate = 397MB
  |
  | We want the low water mark after pruning to be
  | at least 397 MB and since we prune in full
  | block file chunks, we need the high water mark
  | which triggers the prune to be one 128MB block
  | file + added 15% undo data = 147MB greater for
  | a total of 545MB
  |
  | Setting the target to >= 550 MiB will make it
  | likely we can respect the target.
  */
pub const MIN_DISK_SPACE_FOR_BLOCK_FILES: u64 = 550 * 1024 * 1024;


lazy_static!{
    /*
    extern RecursiveMutex cs_main;
    extern Mutex g_best_block_mutex;
    extern std::condition_variable g_best_block_cv;
    /** Used to notify getblocktemplate RPC of new tips. */
    extern uint256 g_best_block;
    /** Whether there are dedicated script-checking threads running.
     * False indicates all script checking is done on the main threadMessageHandler thread.
     */
    extern bool g_parallel_script_checks;
    extern bool fRequireStandard;
    extern bool fCheckBlockIndex;
    extern bool fCheckpointsEnabled;
    /** A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation) */
    extern CFeeRate minRelayTxFee;
    /** If the tip is older than this (in seconds), the node is considered to be in initial block download. */
    extern int64_t nMaxTipAge;

    /** Block hash whose ancestors we will assume to have valid scripts without checking them. */
    extern uint256 hashAssumeValid;

    /** Minimum work we will assume exists on some valid chain. */
    extern arith_uint256 nMinimumChainWork;

    /** Best header we've seen so far (used for getheaders queries' starting points). */
    extern CBlockIndex *pindexBestHeader;

    /** Documentation for argument 'checklevel'. */
    extern const std::vector<std::string> CHECKLEVEL_DOC;
    */
}

/* ------- Transaction validation functions  ------- */

/**
  | Closure representing one script verification
  | 
  | -----------
  | @note
  | 
  | this stores references to the spending
  | transaction
  |
  */
pub struct ScriptCheck {
    tx_out:      TxOut,
    ptx_to:      Arc<Transaction>,
    n_in:        u32,
    n_flags:     u32,
    cache_store: bool,
    error:       ScriptError,
    txdata:      Arc<Mutex<PrecomputedTransactionData>>,
}

impl Default for ScriptCheck {
    
    fn default() -> Self {
        todo!();
        /*
        : ptx_to(nullptr),
        : n_in(0),
        : n_flags(0),
        : cache_store(false),
        : error(SCRIPT_ERR_UNKNOWN_ERROR),
        */
    }
}

impl ScriptCheck {

    pub fn new(
        out_in:     &TxOut,
        tx_to_in:   &Transaction,
        n_in_in:    u32,
        n_flags_in: u32,
        cache_in:   bool,
        txdata_in:  Arc<Mutex<PrecomputedTransactionData>>) -> Self {
    
        todo!();
        /*
            : m_tx_out(outIn), ptxTo(&txToIn), nIn(nInIn), nFlags(nFlagsIn), cacheStore(cacheIn), error(SCRIPT_ERR_UNKNOWN_ERROR), txdata(txdataIn)
        */
    }
    
    pub fn swap(&mut self, check: &mut ScriptCheck)  {
        
        todo!();
        /*
            std::swap(ptxTo, check.ptxTo);
            std::swap(m_tx_out, check.m_tx_out);
            std::swap(nIn, check.nIn);
            std::swap(nFlags, check.nFlags);
            std::swap(cacheStore, check.cacheStore);
            std::swap(error, check.error);
            std::swap(txdata, check.txdata);
        */
    }
    
    pub fn get_script_error(&self) -> ScriptError {
        
        todo!();
        /*
            return error;
        */
    }
}

/*
  | Functions for validating blocks and
  | updating the block tree
  |
  */



pub type FopenFn = unsafe extern "C" fn(*const i8, *const i8) -> *mut libc::FILE ;
//fn(_0: &Path, _1: *const u8) -> *mut libc::FILE;


//-------------------------------------------[.cpp/bitcoin/src/validation.cpp]

pub const MICRO: f32 = 0.000001;
pub const MILLI: f32 = 0.001;

/**
  | An extra transaction can be added to
  | a package, as long as it only has one ancestor
  | and is no larger than this.
  | 
  | Not really any reason to make this configurable
  | as it doesn't materially change DoS
  | parameters.
  |
  */
pub const EXTRA_DESCENDANT_TX_SIZE_LIMIT: usize = 10000;

/**
  | Maximum kilobytes for transactions
  | to store for processing during reorg
  |
  */
pub const MAX_DISCONNECTED_TX_POOL_SIZE: usize = 20000;

/**
  | Time to wait_mut between writing blocks/block
  | index to disk.
  |
  */
pub const DATABASE_WRITE_INTERVAL: Duration = Duration::hours(1);

/**
  | Time to wait between flushing chainstate
  | to disk.
  |
  */
pub const DATABASE_FLUSH_INTERVAL: Duration = Duration::hours(24);

/**
  | Maximum age of our tip for us to be considered
  | current for fee estimation
  |
  */
pub const MAX_FEE_ESTIMATION_TIP_AGE: Duration = Duration::hours(3);

lazy_static!{
    static ref CHECKLEVEL_DOC: Vec<&'static str> = vec!{
        "level 0 reads the blocks from disk",
        "level 1 verifies block validity",
        "level 2 verifies undo data",
        "level 3 checks disconnection of tip blocks",
        "level 4 tries to reconnect the blocks",
        "each level includes the checks of the previous levels",
    };
}

/**
  | Mutex to guard access to validation
  | specific variables, such as reading
  | or changing the chainstate.
  | 
  | This may also need to be locked when updating
  | the transaction pool, e.g. on
  | 
  | AcceptToMemoryPool. See CTxMemPool::cs
  | comment for details.
  | 
  | The transaction pool has a separate
  | lock to allow reading from it and the
  | chainstate at the same time.
  |
  */
lazy_static!{

    pub static ref CS_MAIN:              Arc<Mutex<()>>  = Default::default();
    pub static ref G_BEST_BLOCK_MUTEX:   Arc<Mutex<()>>  = Default::default();
    pub static ref G_BEST_BLOCK_CV:      Condvar         = Condvar::default();
    pub static ref G_BEST_BLOCK:         u256            = u256::default();
    pub static ref HASH_ASSUME_VALID:    u256            = u256::default();
    pub static ref N_MINIMUM_CHAIN_WORK: ArithU256       = ArithU256::default();

    pub static ref PINDEX_BEST_HEADER:       Arc<Mutex<Option<Arc<BlockIndex>>>> = Arc::new(Mutex::new(None));
    pub static ref G_PARALLEL_SCRIPT_CHECKS: AtomicBool = AtomicBool::new(false);
    pub static ref REQUIRE_STANDARD:         AtomicBool = AtomicBool::new(true);
    pub static ref CHECK_BLOCK_INDEX:        AtomicBool = AtomicBool::new(false);
    pub static ref CHECKPOINTS_ENABLED:      AtomicBool = AtomicBool::new(DEFAULT_CHECKPOINTS_ENABLED);
    pub static ref N_MAX_TIP_AGE:            AtomicI64  = AtomicI64::new(DEFAULT_MAX_TIP_AGE);
    pub static ref MIN_RELAY_TX_FEE:         FeeRate    = FeeRate::new(DEFAULT_MIN_RELAY_TX_FEE);
    pub static ref PINDEX_BEST_INVALID:      Arc<Mutex<BlockIndex>> = Default::default();
}

/**
  | Internal stuff from blockstorage ...
  |
  */
lazy_static!{
    /*
    extern RecursiveMutex cs_LastBlockFile;
    extern std::vector<CBlockFileInfo> vinfoBlockFile;
    extern int nLastBlockFile;
    extern bool fCheckForPruning;
    extern std::set<CBlockIndex*> setDirtyBlockIndex;
    extern std::set<int> setDirtyFileInfo;
    */
}

/**
  | ... TODO move fully to blockstorage
  |
  */
pub fn flush_block_file(
    finalize:      Option<bool>,
    finalize_undo: Option<bool>) {

    let finalize:      bool = finalize.unwrap_or(false);
    let finalize_undo: bool = finalize_undo.unwrap_or(false);

    todo!();
        /*
        
        */
}

/**
  | Check if transaction will be final in
  | the next block to be created.
  | 
  | Calls IsFinalTx() with current block
  | height and appropriate block time.
  | 
  | See consensus/consensus.h for flag
  | definitions.
  |
  */
#[EXCLUSIVE_LOCKS_REQUIRED(cs_main)]
pub fn check_final_tx(
        active_chain_tip: Arc<BlockIndex>,
        tx:               &Transaction,
        flags:            Option<i32>) -> bool {

    let flags: i32 = flags.unwrap_or(-1);
    
    todo!();
        /*
        AssertLockHeld(cs_main);
        assert(active_chain_tip); // TODO: Make active_chain_tip a reference

        // By convention a negative value for flags indicates that the
        // current network-enforced consensus rules should be used. In
        // a future soft-fork scenario that would mean checking which
        // rules would be enforced for the next block and setting the
        // appropriate flags. At the present time no soft-forks are
        // scheduled, so no flags are set.
        flags = std::max(flags, 0);

        // CheckFinalTx() uses active_chain_tip.Height()+1 to evaluate
        // nLockTime because when IsFinalTx() is called within
        // AcceptBlock(), the height of the block *being*
        // evaluated is what is used. Thus if we want to know if a
        // transaction can be part of the *next* block, we need to call
        // IsFinalTx() with one more than active_chain_tip.Height().
        const int nBlockHeight = active_chain_tip->nHeight + 1;

        // BIP113 requires that time-locked transactions have nLockTime set to
        // less than the median time of the previous block they're contained in.
        // When the next block is created its previous block will be the current
        // chain tip, so we use that to calculate the median time passed to
        // IsFinalTx() if LOCKTIME_MEDIAN_TIME_PAST is set.
        const int64_t nBlockTime = (flags & LOCKTIME_MEDIAN_TIME_PAST)
                                 ? active_chain_tip->GetMedianTimePast()
                                 : GetAdjustedTime();

        return IsFinalTx(tx, nBlockHeight, nBlockTime);
        */
}

/**
  | Check if transaction will be BIP68 final
  | in the next block to be created on top
  | of tip.
  | 
  | -----------
  | @param[in] tip
  | 
  | Chain tip to check tx sequence locks
  | against. For example, the tip of the
  | current active chain.
  | ----------
  | @param[in] coins_view
  | 
  | Any CCoinsView that provides access
  | to the relevant coins for checking sequence
  | locks. For example, it can be a CCoinsViewCache
  | that isn't connected to anything but
  | contains all the relevant coins, or
  | a CCoinsViewMemPool that is connected
  | to the mempool and chainstate UTXO set.
  | In the latter case, the caller is responsible
  | for holding the appropriate locks to
  | ensure that calls to GetCoin() return
  | correct coins.
  | 
  | Simulates calling SequenceLocks()
  | with data from the tip passed in.
  | 
  | Optionally stores in LockPoints the
  | resulting height and time calculated
  | and the hash of the block needed for calculation
  | or skips the calculation and uses the
  | LockPoints passed in for evaluation.
  | 
  | The LockPoints should not be considered
  | valid if CheckSequenceLocks returns
  | false.
  | 
  | See consensus/consensus.h for flag
  | definitions.
  |
  */
pub fn check_sequence_locks(
        tip:                      Arc<Mutex<BlockIndex>>,
        coins_view:               &dyn CoinsView,
        tx:                       &Transaction,
        flags:                    i32,
        lp:                       Amo<LockPoints>,
        use_existing_lock_points: Option<bool>) -> bool {
    
    let use_existing_lock_points: bool = use_existing_lock_points.unwrap_or(false);

    todo!();
        /*
            assert(tip != nullptr);

        CBlockIndex index;
        index.pprev = tip;
        // CheckSequenceLocks() uses active_chainstate.m_chain.Height()+1 to evaluate
        // height based locks because when SequenceLocks() is called within
        // ConnectBlock(), the height of the block *being*
        // evaluated is what is used.
        // Thus if we want to know if a transaction can be part of the
        // *next* block, we need to use one more than active_chainstate.m_chain.Height()
        index.nHeight = tip->nHeight + 1;

        std::pair<int, int64_t> lockPair;
        if (useExistingLockPoints) {
            assert(lp);
            lockPair.first = lp->height;
            lockPair.second = lp->time;
        }
        else {
            std::vector<int> prevheights;
            prevheights.resize(tx.vin.size());
            for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
                const CTxIn& txin = tx.vin[txinIndex];
                Coin coin;
                if (!coins_view.GetCoin(txin.prevout, coin)) {
                    return error("%s: Missing input", __func__);
                }
                if (coin.nHeight == MEMPOOL_HEIGHT) {
                    // Assume all mempool transaction confirm in the next block
                    prevheights[txinIndex] = tip->nHeight + 1;
                } else {
                    prevheights[txinIndex] = coin.nHeight;
                }
            }
            lockPair = CalculateSequenceLocks(tx, flags, prevheights, index);
            if (lp) {
                lp->height = lockPair.first;
                lp->time = lockPair.second;
                // Also store the hash of the block with the highest height of
                // all the blocks which have sequence locked prevouts.
                // This hash needs to still be on the chain
                // for these LockPoint calculations to be valid
                // Note: It is impossible to correctly calculate a maxInputBlock
                // if any of the sequence locked inputs depend on unconfirmed txs,
                // except in the special case where the relative lock time/height
                // is 0, which is equivalent to no sequence lock. Since we assume
                // input height of tip+1 for mempool txs and test the resulting
                // lockPair from CalculateSequenceLocks against tip+1.  We know
                // EvaluateSequenceLocks will fail if there was a non-zero sequence
                // lock on a mempool input, so we can use the return value of
                // CheckSequenceLocks to indicate the LockPoints validity
                int maxInputHeight = 0;
                for (const int height : prevheights) {
                    // Can ignore mempool inputs since we'll fail if they had non-zero locks
                    if (height != tip->nHeight+1) {
                        maxInputHeight = std::max(maxInputHeight, height);
                    }
                }
                lp->maxInputBlock = tip->GetAncestor(maxInputHeight);
            }
        }
        return EvaluateSequenceLocks(index, lockPair);
        */
}

#[EXCLUSIVE_LOCKS_REQUIRED(pool.cs, ::cs_main)]
pub fn limit_mempool_size(
        pool:        &mut TxMemPool,
        coins_cache: &mut CoinsViewCache,
        limit:       usize,
        age:         Duration /* seconds */)  {
    
    todo!();
        /*
            int expired = pool.Expire(GetTime<seconds>() - age);
        if (expired != 0) {
            LogPrint(BCLog::MEMPOOL, "Expired %i transactions from the memory pool\n", expired);
        }

        std::vector<OutPoint> vNoSpendsRemaining;
        pool.TrimToSize(limit, &vNoSpendsRemaining);
        for (const OutPoint& removed : vNoSpendsRemaining)
            coins_cache.Uncache(removed);
        */
}

/**
  | Checks to avoid mempool polluting consensus
  | critical paths since cached signature
  | and script validity results will be
  | reused if we validate this transaction
  | again during block validation.
  |
  */
#[EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)]
pub fn check_inputs_from_mempool_and_cache(
        tx:        &Transaction,
        state:     &mut TxValidationState,
        view:      &CoinsViewCache,
        pool:      &TxMemPool,
        flags:     u32,
        txdata:    &mut PrecomputedTransactionData,
        coins_tip: &mut CoinsViewCache) -> bool {
    
    todo!();
        /*
            AssertLockHeld(cs_main);
        AssertLockHeld(pool.cs);

        assert(!tx.IsCoinBase());
        for (const CTxIn& txin : tx.vin) {
            const Coin& coin = view.AccessCoin(txin.prevout);

            // This coin was checked in PreChecks and MemPoolAccept
            // has been holding cs_main since then.
            Assume(!coin.IsSpent());
            if (coin.IsSpent()) return false;

            // If the Coin is available, there are 2 possibilities:
            // it is available in our current ChainstateActive UTXO set,
            // or it's a UTXO provided by a transaction in our mempool.
            // Ensure the scriptPubKeys in Coins from CoinsView are correct.
            const CTransactionRef& txFrom = pool.get(txin.prevout.hash);
            if (txFrom) {
                assert(txFrom->GetHash() == txin.prevout.hash);
                assert(txFrom->vout.size() > txin.prevout.n);
                assert(txFrom->vout[txin.prevout.n] == coin.out);
            } else {
                const Coin& coinFromUTXOSet = coins_tip.AccessCoin(txin.prevout);
                assert(!coinFromUTXOSet.IsSpent());
                assert(coinFromUTXOSet.out == coin.out);
            }
        }

        // Call CheckInputScripts() to cache signature and script validity against current tip consensus rules.
        return CheckInputScripts(tx, state, view, flags, /* cacheSigStore= */ true, /* cacheFullScriptStore= */ true, txdata);
        */
}

pub fn get_block_subsidy(
        n_height:         i32,
        consensus_params: &ChainConsensusParams) -> Amount {
    
    todo!();
        /*
            int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
        // Force block reward to zero when right shift is undefined.
        if (halvings >= 64)
            return 0;

        CAmount nSubsidy = 50 * COIN;
        // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
        nSubsidy >>= halvings;
        return nSubsidy;
        */
}

impl ScriptCheck {
    
    pub fn invoke(&mut self) -> bool {
        
        todo!();
        /*
            const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
        const CScriptWitness *witness = &ptxTo->vin[nIn].scriptWitness;
        return VerifyScript(scriptSig, m_tx_out.scriptPubKey, witness, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, m_tx_out.nValue, cacheStore, *txdata), &error);
        */
    }
}


lazy_static!{
    /*
    static CuckooCache::cache<uint256, SignatureCacheHasher> g_scriptExecutionCache;
    static CSHA256 g_scriptExecutionCacheHasher;
    */
}

/**
  | Initializes the script-execution
  | cache
  |
  */
pub fn init_script_execution_cache()  {
    
    todo!();
        /*
            // Setup the salted hasher
        uint256 nonce = GetRandHash();
        // We want the nonce to be 64 bytes long to force the hasher to process
        // this chunk, which makes later hash computations more efficient. We
        // just write our 32-byte entropy twice to fill the 64 bytes.
        g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
        g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
        // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
        // setup_bytes creates the minimum possible cache (2 elements).
        size_t nMaxCacheSize = std::min(std::max((int64_t)0, gArgs.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2), MAX_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
        size_t nElems = g_scriptExecutionCache.setup_bytes(nMaxCacheSize);
        LogPrintf("Using %zu MiB out of %zu/2 requested for script execution cache, able to store %zu elements\n",
                (nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
        */
}

/**
  | Check whether all of this transaction's
  | input scripts succeed.
  | 
  | This involves ECDSA signature checks
  | so can be computationally intensive.
  | This function should only be called
  | after the cheap sanity checks in
  | 
  | CheckTxInputs passed.
  | 
  | If pvChecks is not nullptr, script checks
  | are pushed onto it instead of being performed
  | inline. Any script checks which are
  | not necessary (eg due to script execution
  | cache hits) are, obviously, not pushed
  | onto pvChecks/run.
  | 
  | Setting cacheSigStore/cacheFullScriptStore
  | to false will remove elements from the
  | corresponding cache which are matched.
  | This is useful for checking blocks where
  | we will likely never need the cache entry
  | again.
  | 
  | -----------
  | @note
  | 
  | we may set state.reason to
  | 
  | NOT_STANDARD for extra soft-fork flags
  | in flags, block-checking callers should
  | probably reset it to CONSENSUS in such
  | cases.
  | 
  | Non-static (and re-declared) in src/test/txvalidationcache_tests.cpp
  |
  */
#[EXCLUSIVE_LOCKS_REQUIRED(cs_main)]
pub fn check_input_scripts(
        tx:                      &Transaction,
        state:                   &mut TxValidationState,
        inputs:                  &CoinsViewCache,
        flags:                   u32,
        cache_sig_store:         bool,
        cache_full_script_store: bool,
        txdata:                  &mut PrecomputedTransactionData,
        pv_checks:               Amo<Vec<ScriptCheck>>) -> bool {
    
    todo!();
        /*
            if (tx.IsCoinBase()) return true;

        if (pvChecks) {
            pvChecks->reserve(tx.vin.size());
        }

        // First check if script executions have been cached with the same
        // flags. Note that this assumes that the inputs provided are
        // correct (ie that the transaction hash which is in tx's prevouts
        // properly commits to the scriptPubKey in the inputs view of that
        // transaction).
        uint256 hashCacheEntry;
        CSHA256 hasher = g_scriptExecutionCacheHasher;
        hasher.Write(tx.GetWitnessHash().begin(), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin());
        AssertLockHeld(cs_main); //TODO: Remove this requirement by making CuckooCache not require external locks
        if (g_scriptExecutionCache.contains(hashCacheEntry, !cacheFullScriptStore)) {
            return true;
        }

        if (!txdata.m_spent_outputs_ready) {
            std::vector<CTxOut> spent_outputs;
            spent_outputs.reserve(tx.vin.size());

            for (const auto& txin : tx.vin) {
                const OutPoint& prevout = txin.prevout;
                const Coin& coin = inputs.AccessCoin(prevout);
                assert(!coin.IsSpent());
                spent_outputs.emplace_back(coin.out);
            }
            txdata.Init(tx, std::move(spent_outputs));
        }
        assert(txdata.m_spent_outputs.size() == tx.vin.size());

        for (unsigned int i = 0; i < tx.vin.size(); i++) {

            // We very carefully only pass in things to CScriptCheck which
            // are clearly committed to by tx' witness hash. This provides
            // a sanity check that our caching is not introducing consensus
            // failures through additional data in, eg, the coins being
            // spent being checked as a part of CScriptCheck.

            // Verify signature
            CScriptCheck check(txdata.m_spent_outputs[i], tx, i, flags, cacheSigStore, &txdata);
            if (pvChecks) {
                pvChecks->push_back(CScriptCheck());
                check.swap(pvChecks->back());
            } else if (!check()) {
                if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
                    // Check whether the failure was caused by a
                    // non-mandatory script verification check, such as
                    // non-standard DER encodings or non-null dummy
                    // arguments; if so, ensure we return NOT_STANDARD
                    // instead of CONSENSUS to avoid downstream users
                    // splitting the network between upgraded and
                    // non-upgraded nodes by banning CONSENSUS-failing
                    // data providers.
                    CScriptCheck check2(txdata.m_spent_outputs[i], tx, i,
                            flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheSigStore, &txdata);
                    if (check2())
                        return state.Invalid(TxValidationResult::TX_NOT_STANDARD, strprintf("non-mandatory-script-verify-flag (%s)", ScriptErrorString(check.GetScriptError())));
                }
                // MANDATORY flag failures correspond to
                // TxValidationResult::TX_CONSENSUS. Because CONSENSUS
                // failures are the most serious case of validation
                // failures, we may need to consider using
                // RECENT_CONSENSUS_CHANGE for any script failure that
                // could be due to non-upgraded nodes which we may want to
                // support, to avoid splitting the network (but this
                // depends on the details of how net_processing handles
                // such errors).
                return state.Invalid(TxValidationResult::TX_CONSENSUS, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(check.GetScriptError())));
            }
        }

        if (cacheFullScriptStore && !pvChecks) {
            // We executed all of the provided scripts, and were told to
            // cache the result. Do so now.
            g_scriptExecutionCache.insert(hashCacheEntry);
        }

        return true;
        */
}

pub fn abort_node(
        state:        &mut BlockValidationState,
        str_message:  &String,
        user_message: Option<&BilingualStr>) -> bool {

    let df = BilingualStr::default();
    let user_message: &BilingualStr = user_message.unwrap_or(&df);
    
    todo!();
        /*
            AbortNode(strMessage, userMessage);
        return state.Error(strMessage);
        */
}

/**
  | Restore the UTXO in a Coin at a given OutPoint
  | 
  | -----------
  | @param undo
  | 
  | The Coin to be restored.
  | ----------
  | @param view
  | 
  | The coins view to which to apply the changes.
  | ----------
  | @param out
  | 
  | The out point that corresponds to the
  | tx input.
  | 
  | -----------
  | @return
  | 
  | A DisconnectResult as an int
  |
  */
pub fn apply_tx_in_undo(
        undo: Coin,
        view: &mut CoinsViewCache,
        out:  &OutPoint) -> i32 {
    
    todo!();
        /*
            bool fClean = true;

        if (view.HaveCoin(out)) fClean = false; // overwriting transaction output

        if (undo.nHeight == 0) {
            // Missing undo metadata (height and coinbase). Older versions included this
            // information only in undo records for the last spend of a transactions'
            // outputs. This implies that it must be present for some other output of the same tx.
            const Coin& alternate = AccessByTxid(view, out.hash);
            if (!alternate.IsSpent()) {
                undo.nHeight = alternate.nHeight;
                undo.fCoinBase = alternate.fCoinBase;
            } else {
                return DISCONNECT_FAILED; // adding output for transaction without known metadata
            }
        }
        // If the coin already exists as an unspent coin in the cache, then the
        // possible_overwrite parameter to AddCoin must be set to true. We have
        // already checked whether an unspent coin exists above using HaveCoin, so
        // we don't need to guess. When fClean is false, an unspent coin already
        // existed and it is an overwrite.
        view.AddCoin(out, std::move(undo), !fClean);

        return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
        */
}

lazy_static!{
    /*
    static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
    */
}

/**
  | Run instances of script checking worker
  | threads
  |
  */
pub fn start_script_check_worker_threads(threads_num: i32)  {
    
    todo!();
        /*
            scriptcheckqueue.StartWorkerThreads(threads_num);
        */
}

/**
  | Stop all of the script checking worker
  | threads
  |
  */
pub fn stop_script_check_worker_threads()  {
    
    todo!();
        /*
            scriptcheckqueue.StopWorkerThreads();
        */
}

/**
  | Threshold condition checker that triggers
  | when unknown versionbits are seen on
  | the network.
  |
  */
pub struct WarningBitsConditionChecker {
    bit:  i32,
}

impl AbstractThresholdConditionChecker for WarningBitsConditionChecker {

}

impl abstract_threshold_condition_checker::Interface for WarningBitsConditionChecker { }

impl abstract_threshold_condition_checker::MinActivationHeight for WarningBitsConditionChecker { 

}

impl abstract_threshold_condition_checker::BeginTime for WarningBitsConditionChecker { 

    fn begin_time(&self, params: &ChainConsensusParams) -> i64 {
        
        todo!();
        /*
            return 0;
        */
    }
}

impl abstract_threshold_condition_checker::EndTime for WarningBitsConditionChecker { 

    fn end_time(&self, params: &ChainConsensusParams) -> i64 {
        
        todo!();
        /*
            return std::numeric_limits<int64_t>::max();
        */
    }
}

impl abstract_threshold_condition_checker::Condition for WarningBitsConditionChecker { 

    fn condition(&self, 
        pindex: *const BlockIndex,
        params: &ChainConsensusParams) -> bool {
        
        todo!();
        /*
            return pindex->nHeight >= params.MinBIP9WarningHeight &&
                   ((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
                   ((pindex->nVersion >> bit) & 1) != 0 &&
                   ((g_versionbitscache.ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
        */
    }
}

impl abstract_threshold_condition_checker::Threshold for WarningBitsConditionChecker { 

    fn threshold(&self, params: &ChainConsensusParams) -> i32 {
        
        todo!();
        /*
            return params.nRuleChangeActivationThreshold;
        */
    }
}

impl abstract_threshold_condition_checker::Period for WarningBitsConditionChecker { 

    fn period(&self, params: &ChainConsensusParams) -> i32 {
        
        todo!();
        /*
            return params.nMinerConfirmationWindow;
        */
    }
}

impl WarningBitsConditionChecker {

    pub fn new(bit_in: i32) -> Self {
    
        todo!();
        /*
        : bit(bitIn),
        */
    }
}

lazy_static!{
    /*
    static ThresholdConditionCache warningcache[VERSIONBITS_NUM_BITS] GUARDED_BY(cs_main);
    */
}

/**
  | Returns the script flags which should
  | be checked for a given block
  |
  */
pub fn get_block_script_flags(
        pindex:          Arc<BlockIndex>,
        consensusparams: &ChainConsensusParams) -> u32 {
    
    todo!();
        /*
            unsigned int flags = SCRIPT_VERIFY_NONE;

        // BIP16 didn't become active until Apr 1 2012 (on mainnet, and
        // retroactively applied to testnet)
        // However, only one historical block violated the P2SH rules (on both
        // mainnet and testnet), so for simplicity, always leave P2SH
        // on except for the one violating block.
        if (consensusparams.BIP16Exception.IsNull() || // no bip16 exception on this chain
            pindex->phashBlock == nullptr || // this is a new candidate block, eg from TestBlockValidity()
            *pindex->phashBlock != consensusparams.BIP16Exception) // this block isn't the historical exception
        {
            // Enforce WITNESS rules whenever P2SH is in effect
            flags |= SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS;
        }

        // Enforce the DERSIG (BIP66) rule
        if (DeploymentActiveAt(*pindex, consensusparams, consensus::DEPLOYMENT_DERSIG)) {
            flags |= SCRIPT_VERIFY_DERSIG;
        }

        // Enforce CHECKLOCKTIMEVERIFY (BIP65)
        if (DeploymentActiveAt(*pindex, consensusparams, consensus::DEPLOYMENT_CLTV)) {
            flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
        }

        // Enforce CHECKSEQUENCEVERIFY (BIP112)
        if (DeploymentActiveAt(*pindex, consensusparams, consensus::DEPLOYMENT_CSV)) {
            flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
        }

        // Enforce Taproot (BIP340-BIP342)
        if (DeploymentActiveAt(*pindex, consensusparams, consensus::DEPLOYMENT_TAPROOT)) {
            flags |= SCRIPT_VERIFY_TAPROOT;
        }

        // Enforce BIP147 NULLDUMMY (activated simultaneously with segwit)
        if (DeploymentActiveAt(*pindex, consensusparams, consensus::DEPLOYMENT_SEGWIT)) {
            flags |= SCRIPT_VERIFY_NULLDUMMY;
        }

        return flags;
        */
}

lazy_static!{
    /*
    static int64_t nTimeCheck = 0;
    static int64_t nTimeForks = 0;
    static int64_t nTimeVerify = 0;
    static int64_t nTimeConnect = 0;
    static int64_t nTimeIndex = 0;
    static int64_t nTimeTotal = 0;
    static int64_t nBlocksTotal = 0;
    */
}

pub fn do_warning(warning: &BilingualStr)  {
    
    todo!();
        /*
            static bool fWarned = false;
        SetMiscWarning(warning);
        if (!fWarned) {
            AlertNotify(warning.original);
            fWarned = true;
        }
        */
}

/**
  | Private helper function that concatenates
  | warning messages.
  |
  */
pub fn append_warning(
        res:  &mut BilingualStr,
        warn: &BilingualStr)  {
    
    todo!();
        /*
            if (!res.empty()) res += Untranslated(", ");
        res += warn;
        */
}

lazy_static!{
    /*
    static int64_t nTimeReadFromDisk = 0;
    static int64_t nTimeConnectTotal = 0;
    static int64_t nTimeFlush = 0;
    static int64_t nTimeChainState = 0;
    static int64_t nTimePostConnect = 0;
    */
}

pub fn check_block_header(
        block:            &BlockHeader,
        state:            &mut BlockValidationState,
        consensus_params: &ChainConsensusParams,
        checkpow:         Option<bool>) -> bool {
    let checkpow: bool = checkpow.unwrap_or(true);

    todo!();
        /*
            // Check proof of work matches claimed amount
        if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
            return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "high-hash", "proof of work failed");

        return true;
        */
}

/**
  | Context-independent validity checks
  |
  */
pub fn check_block(
        block:             &Block,
        state:             &mut BlockValidationState,
        consensus_params:  &ChainConsensusParams,
        checkpow:          Option<bool>,
        check_merkle_root: Option<bool>) -> bool {
    
    let checkpow:          bool = checkpow.unwrap_or(true);
    let check_merkle_root: bool = check_merkle_root.unwrap_or(true);

    todo!();
        /*
            // These are checks that are independent of context.

        if (block.fChecked)
            return true;

        // Check that the header is valid (particularly PoW).  This is mostly
        // redundant with the call in AcceptBlockHeader.
        if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
            return false;

        // Signet only: check block solution
        if (consensusParams.signet_blocks && fCheckPOW && !CheckSignetBlockSolution(block, consensusParams)) {
            return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-signet-blksig", "signet block signature validation failure");
        }

        // Check the merkle root.
        if (fCheckMerkleRoot) {
            bool mutated;
            uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
            if (block.hashMerkleRoot != hashMerkleRoot2)
                return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-txnmrklroot", "hashMerkleRoot mismatch");

            // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
            // of transactions in a block without affecting the merkle root of a block,
            // while still invalidating it.
            if (mutated)
                return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-txns-duplicate", "duplicate transaction");
        }

        // All potential-corruption validation must be done before we do any
        // transaction validation, as otherwise we may mark the header as invalid
        // because we receive the wrong transactions for it.
        // Note that witness malleability is checked in ContextualCheckBlock, so no
        // checks that use witness data may be performed here.

        // Size limits
        if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
            return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-length", "size limits failed");

        // First transaction must be coinbase, the rest must not be
        if (block.vtx.empty() || !block.vtx[0]->IsCoinBase())
            return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-missing", "first tx is not coinbase");
        for (unsigned int i = 1; i < block.vtx.size(); i++)
            if (block.vtx[i]->IsCoinBase())
                return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-multiple", "more than one coinbase");

        // Check transactions
        // Must check for duplicate inputs (see CVE-2018-17144)
        for (const auto& tx : block.vtx) {
            TxValidationState tx_state;
            if (!CheckTransaction(*tx, tx_state)) {
                // CheckBlock() does context-free validation checks. The only
                // possible failures are consensus failures.
                assert(tx_state.GetResult() == TxValidationResult::TX_CONSENSUS);
                return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, tx_state.GetRejectReason(),
                                     strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), tx_state.GetDebugMessage()));
            }
        }
        unsigned int nSigOps = 0;
        for (const auto& tx : block.vtx)
        {
            nSigOps += GetLegacySigOpCount(*tx);
        }
        if (nSigOps * WITNESS_SCALE_FACTOR > MAX_BLOCK_SIGOPS_COST)
            return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-sigops", "out-of-bounds SigOpCount");

        if (fCheckPOW && fCheckMerkleRoot)
            block.fChecked = true;

        return true;
        */
}

/**
  | Update uncommitted block structures
  | (currently: only the witness reserved
  | value). This is safe for submitted blocks.
  |
  */
pub fn update_uncommitted_block_structures(
        block:            &mut Block,
        pindex_prev:      Arc<BlockIndex>,
        consensus_params: &ChainConsensusParams)  {
    
    todo!();
        /*
            int commitpos = GetWitnessCommitmentIndex(block);
        static const std::vector<unsigned char> nonce(32, 0x00);
        if (commitpos != NO_WITNESS_COMMITMENT && DeploymentActiveAfter(pindexPrev, consensusParams, consensus::DEPLOYMENT_SEGWIT) && !block.vtx[0]->HasWitness()) {
            CMutableTransaction tx(*block.vtx[0]);
            tx.vin[0].scriptWitness.stack.resize(1);
            tx.vin[0].scriptWitness.stack[0] = nonce;
            block.vtx[0] = MakeTransactionRef(std::move(tx));
        }
        */
}

/**
  | Produce the necessary coinbase commitment
  | for a block (modifies the hash, don't
  | call for mined blocks).
  |
  */
pub fn generate_coinbase_commitment(
        block:            &mut Block,
        pindex_prev:      Arc<BlockIndex>,
        consensus_params: &ChainConsensusParams) -> Vec<u8> {
    
    todo!();
        /*
            std::vector<unsigned char> commitment;
        int commitpos = GetWitnessCommitmentIndex(block);
        std::vector<unsigned char> ret(32, 0x00);
        if (commitpos == NO_WITNESS_COMMITMENT) {
            uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
            CHash256().Write(witnessroot).Write(ret).Finalize(witnessroot);
            CTxOut out;
            out.nValue = 0;
            out.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT);
            out.scriptPubKey[0] = OP_RETURN;
            out.scriptPubKey[1] = 0x24;
            out.scriptPubKey[2] = 0xaa;
            out.scriptPubKey[3] = 0x21;
            out.scriptPubKey[4] = 0xa9;
            out.scriptPubKey[5] = 0xed;
            memcpy(&out.scriptPubKey[6], witnessroot.begin(), 32);
            commitment = std::vector<unsigned char>(out.scriptPubKey.begin(), out.scriptPubKey.end());
            CMutableTransaction tx(*block.vtx[0]);
            tx.vout.push_back(out);
            block.vtx[0] = MakeTransactionRef(std::move(tx));
        }
        UpdateUncommittedBlockStructures(block, pindexPrev, consensusParams);
        return commitment;
        */
}


/**
  | @note
  | 
  | This function is not currently invoked
  | by ConnectBlock(), so we should consider
  | upgrade issues if we change which consensus
  | rules are enforced in this function
  | (eg by adding a new consensus rule).
  | See comment in ConnectBlock().
  | ----------
  | @note
  | 
  | -reindex-chainstate skips the validation
  | that happens here!
  |
  */
pub fn contextual_check_block(
        block:            &Block,
        state:            &mut BlockValidationState,
        consensus_params: &ChainConsensusParams,
        pindex_prev:      Arc<BlockIndex>) -> bool {
    
    todo!();
        /*
            const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;

        // Enforce BIP113 (Median Time Past).
        int nLockTimeFlags = 0;
        if (DeploymentActiveAfter(pindexPrev, consensusParams, consensus::DEPLOYMENT_CSV)) {
            assert(pindexPrev != nullptr);
            nLockTimeFlags |= LOCKTIME_MEDIAN_TIME_PAST;
        }

        int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST)
                                  ? pindexPrev->GetMedianTimePast()
                                  : block.GetBlockTime();

        // Check that all transactions are finalized
        for (const auto& tx : block.vtx) {
            if (!IsFinalTx(*tx, nHeight, nLockTimeCutoff)) {
                return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-nonfinal", "non-final transaction");
            }
        }

        // Enforce rule that the coinbase starts with serialized block height
        if (DeploymentActiveAfter(pindexPrev, consensusParams, consensus::DEPLOYMENT_HEIGHTINCB))
        {
            CScript expect = CScript() << nHeight;
            if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
                !std::equal(expect.begin(), expect.end(), block.vtx[0]->vin[0].scriptSig.begin())) {
                return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-height", "block height mismatch in coinbase");
            }
        }

        // Validation for witness commitments.
        // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
        //   coinbase (where 0x0000....0000 is used instead).
        // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness reserved value (unconstrained).
        // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
        // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
        //   {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness reserved value). In case there are
        //   multiple, the last one is used.
        bool fHaveWitness = false;
        if (DeploymentActiveAfter(pindexPrev, consensusParams, consensus::DEPLOYMENT_SEGWIT)) {
            int commitpos = GetWitnessCommitmentIndex(block);
            if (commitpos != NO_WITNESS_COMMITMENT) {
                bool malleated = false;
                uint256 hashWitness = BlockWitnessMerkleRoot(block, &malleated);
                // The malleation check is ignored; as the transaction tree itself
                // already does not permit it, it is impossible to trigger in the
                // witness tree.
                if (block.vtx[0]->vin[0].scriptWitness.stack.size() != 1 || block.vtx[0]->vin[0].scriptWitness.stack[0].size() != 32) {
                    return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-witness-nonce-size", strprintf("%s : invalid witness reserved value size", __func__));
                }
                CHash256().Write(hashWitness).Write(block.vtx[0]->vin[0].scriptWitness.stack[0]).Finalize(hashWitness);
                if (memcmp(hashWitness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
                    return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-witness-merkle-match", strprintf("%s : witness merkle commitment mismatch", __func__));
                }
                fHaveWitness = true;
            }
        }

        // No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
        if (!fHaveWitness) {
          for (const auto& tx : block.vtx) {
                if (tx->HasWitness()) {
                    return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "unexpected-witness", strprintf("%s : unexpected witness data found", __func__));
                }
            }
        }

        // After the coinbase witness reserved value and commitment are verified,
        // we can check if the block weight passes (before we've checked the
        // coinbase witness, it would be possible for the weight to be too
        // large by filling up the coinbase witness, which doesn't change
        // the block hash, so we couldn't mark the block as permanently
        // failed).
        if (GetBlockWeight(block) > MAX_BLOCK_WEIGHT) {
            return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-weight", strprintf("%s : weight limit failed", __func__));
        }

        return true;
        */
}

/* -------------- BLOCK PRUNING CODE  -------------- */

pub const MEMPOOL_DUMP_VERSION: u64 = 1;


/**
  | Dump the mempool to disk.
  |
  */
pub fn dump_mempool(
        pool:                    &TxMemPool,
        mockable_fopen_function: Option<FopenFn>,
        skip_file_commit:        Option<bool>) -> bool {

    let mockable_fopen_function: FopenFn = mockable_fopen_function.unwrap_or(libc::fopen);
    let skip_file_commit:           bool = skip_file_commit.unwrap_or(false);
    
    todo!();
        /*
            int64_t start = GetTimeMicros();

        std::map<uint256, CAmount> mapDeltas;
        std::vector<TxMemPoolInfo> vinfo;
        std::set<uint256> unbroadcast_txids;

        static Mutex dump_mutex;
        LOCK(dump_mutex);

        {
            LOCK(pool.cs);
            for (const auto &i : pool.mapDeltas) {
                mapDeltas[i.first] = i.second;
            }
            vinfo = pool.infoAll();
            unbroadcast_txids = pool.GetUnbroadcastTxs();
        }

        int64_t mid = GetTimeMicros();

        try {
            FILE* filestr{mockable_fopen_function(gArgs.GetDataDirNet() / "mempool.dat.new", "wb")};
            if (!filestr) {
                return false;
            }

            CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);

            uint64_t version = MEMPOOL_DUMP_VERSION;
            file << version;

            file << (uint64_t)vinfo.size();
            for (const auto& i : vinfo) {
                file << *(i.tx);
                file << int64_t{count_seconds(i.m_time)};
                file << int64_t{i.nFeeDelta};
                mapDeltas.erase(i.tx->GetHash());
            }

            file << mapDeltas;

            LogPrintf("Writing %d unbroadcast transactions to disk.\n", unbroadcast_txids.size());
            file << unbroadcast_txids;

            if (!skip_file_commit && !FileCommit(file.Get()))
                throw std::runtime_error("FileCommit failed");
            file.fclose();
            if (!RenameOver(gArgs.GetDataDirNet() / "mempool.dat.new", gArgs.GetDataDirNet() / "mempool.dat")) {
                throw std::runtime_error("Rename failed");
            }
            int64_t last = GetTimeMicros();
            LogPrintf("Dumped mempool: %gs to copy, %gs to dump\n", (mid-start)*MICRO, (last-mid)*MICRO);
        } catch (const std::exception& e) {
            LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
            return false;
        }
        return true;
        */
}

pub fn alert_notify(str_message: &String)  {
    
    todo!();
        /*
            uiInterface.NotifyAlertChanged();
    #if HAVE_SYSTEM
        std::string strCmd = gArgs.GetArg("-alertnotify", "");
        if (strCmd.empty()) return;

        // Alert text should be plain ascii coming from a trusted source, but to
        // be safe we first strip anything not in safeChars, then add single quotes around
        // the whole string before passing it to the shell:
        std::string singleQuote("'");
        std::string safeStatus = SanitizeString(strMessage);
        safeStatus = singleQuote+safeStatus+singleQuote;
        boost::replace_all(strCmd, "%s", safeStatus);

        std::thread t(runCommand, strCmd);
        t.detach(); // thread runs free
    #endif
        */
}

pub fn update_coins(
        tx:       &Transaction,
        inputs:   &mut CoinsViewCache,
        txundo:   &mut TxUndo,
        n_height: i32)  {
    
    todo!();
        /*
            // mark inputs spent
        if (!tx.IsCoinBase()) {
            txundo.vprevout.reserve(tx.vin.size());
            for (const CTxIn &txin : tx.vin) {
                txundo.vprevout.emplace_back();
                bool is_spent = inputs.SpendCoin(txin.prevout, &txundo.vprevout.back());
                assert(is_spent);
            }
        }
        // add outputs
        AddCoins(inputs, tx, nHeight);
        */
}

#[LOCKS_EXCLUDED(cs_main)]
pub fn limit_validation_interface_queue()  {
    
    todo!();
        /*
            AssertLockNotHeld(cs_main);

        if (GetMainSignals().CallbacksPending() > 10) {
            SyncWithValidationInterfaceQueue();
        }
        */
}

//-------------------------------------------[.cpp/bitcoin/src/consensus/validation.h]

/**
  | Index marker for when no witness commitment
  | is present in a coinbase transaction.
  |
  */
pub const NO_WITNESS_COMMITMENT: i32 = -1;

/**
  | Minimum size of a witness commitment
  | structure. Defined in BIP 141. *
  |
  */
pub const MINIMUM_WITNESS_COMMITMENT: usize = 38;

/**
  | A "reason" why a transaction was invalid,
  | suitable for determining whether the
  | provider of the transaction should
  | be banned/ignored/disconnected/etc.
  |
  */
#[derive(PartialEq,Eq,Clone,Debug)]
pub enum TxValidationResult {

    /**
      | initial value. Tx has not yet been rejected
      |
      */
    TX_RESULT_UNSET = 0,     

    /**
      | invalid by consensus rules
      |
      */
    TX_CONSENSUS,            

    /**
      | Invalid by a change to consensus rules
      | more recent than SegWit.
      | 
      | Currently unused as there are no such
      | consensus rule changes, and any download
      | sources realistically need to support
      | SegWit in order to provide useful data,
      | so differentiating between always-invalid
      | and invalid-by-pre-SegWit-soft-fork
      | is uninteresting.
      |
      */
    TX_RECENT_CONSENSUS_CHANGE,

    /**
      | inputs (covered by txid) failed policy
      | rules
      |
      */
    TX_INPUTS_NOT_STANDARD,   

    /**
      | otherwise didn't meet our local policy
      | rules
      |
      */
    TX_NOT_STANDARD,          

    /**
      | transaction was missing some of its
      | inputs
      |
      */
    TX_MISSING_INPUTS,        

    /**
      | transaction spends a coinbase too early,
      | or violates locktime/sequence locks
      |
      */
    TX_PREMATURE_SPEND,       

    /**
      | Transaction might have a witness prior
      | to SegWit activation, or witness may
      | have been malleated (which includes
      | non-standard witnesses).
      |
      */
    TX_WITNESS_MUTATED,

    /**
      | Transaction is missing a witness.
      |
      */
    TX_WITNESS_STRIPPED,

    /**
      | Tx already in mempool or conflicts with
      | a tx in the chain (if it conflicts with
      | another tx in mempool, we use MEMPOOL_POLICY
      | as it failed to reach the RBF threshold)
      | 
      | Currently this is only used if the transaction
      | already exists in the mempool or on chain.
      |
      */
    TX_CONFLICT,

    /**
      | violated mempool's fee/size/descendant/RBF/etc
      | limits
      |
      */
    TX_MEMPOOL_POLICY,        
}

pub struct TxValidationState {
    base: ValidationState<TxValidationResult>,
}

impl TxValidationState {

    delegate! {
        to self.base {

            pub fn invalid(&mut self, 
                result:        TxValidationResult,
                reject_reason: Option<&str>,
                debug_message: Option<&str>) -> bool;
            
            pub fn error(&mut self, reject_reason: &String) -> bool;
            
            pub fn is_valid(&self) -> bool;
            
            pub fn is_invalid(&self) -> bool;
            
            pub fn is_error(&self) -> bool;
            
            pub fn get_result(&self) -> TxValidationResult;
            
            pub fn get_reject_reason(&self) -> String;
            
            pub fn get_debug_message(&self) -> String;
            
            pub fn to_string(&self) -> String;
        }
    }
}

/**
  | These implement the weight = (stripped_size
  | * 4) + witness_size formula, using only
  | serialization with and without witness data. As
  | witness_size is equal to total_size
  | - stripped_size, this formula is identical to:
  | weight = (stripped_size * 3) + total_size.
  */
#[inline] pub fn get_transaction_weight(tx: &Transaction) -> i64 {
    
    todo!();
        /*
            return ::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(tx, PROTOCOL_VERSION);
        */
}

#[inline] pub fn get_block_weight(block: &Block) -> i64 {
    
    todo!();
        /*
            return ::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(block, PROTOCOL_VERSION);
        */
}

#[inline] pub fn get_transaction_input_weight(txin: &TxIn) -> i64 {
    
    todo!();
        /*
            // scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
        return ::GetSerializeSize(txin, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(txin, PROTOCOL_VERSION) + ::GetSerializeSize(txin.scriptWitness.stack, PROTOCOL_VERSION);
        */
}

/**
  | Compute at which vout of the block's
  | coinbase transaction the witness commitment
  | occurs, or -1 if not found
  |
  */
#[inline] pub fn get_witness_commitment_index(block: &Block) -> i32 {
    
    todo!();
        /*
            int commitpos = NO_WITNESS_COMMITMENT;
        if (!block.vtx.empty()) {
            for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
                const CTxOut& vout = block.vtx[0]->vout[o];
                if (vout.scriptPubKey.size() >= MINIMUM_WITNESS_COMMITMENT &&
                    vout.scriptPubKey[0] == OP_RETURN &&
                    vout.scriptPubKey[1] == 0x24 &&
                    vout.scriptPubKey[2] == 0xaa &&
                    vout.scriptPubKey[3] == 0x21 &&
                    vout.scriptPubKey[4] == 0xa9 &&
                    vout.scriptPubKey[5] == 0xed) {
                    commitpos = o;
                }
            }
        }
        return commitpos;
        */
}

//-------------------------------------------[.cpp/bitcoin/src/consensus/tx_verify.h]

/* ------- Transaction validation functions  ------- */

/*
  | Auxiliary functions for transaction
  | validation (ideally should not be exposed)
  |
  */

//-------------------------------------------[.cpp/bitcoin/src/consensus/tx_verify.cpp]

/**
  | Check if transaction is final and can
  | be included in a block with the specified
  | height and time. Consensus critical.
  |
  */
pub fn is_final_tx(
        tx:             &Transaction,
        n_block_height: i32,
        n_block_time:   i64) -> bool {
    
    todo!();
        /*
            if (tx.nLockTime == 0)
            return true;
        if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
            return true;

        // Even if tx.nLockTime isn't satisfied by nBlockHeight/nBlockTime, a
        // transaction is still considered final if all inputs' nSequence ==
        // SEQUENCE_FINAL (0xffffffff), in which case nLockTime is ignored.
        //
        // Because of this behavior OP_CHECKLOCKTIMEVERIFY/CheckLockTime() will
        // also check that the spending input's nSequence != SEQUENCE_FINAL,
        // ensuring that an unsatisfied nLockTime value will actually cause
        // IsFinalTx() to return false here:
        for (const auto& txin : tx.vin) {
            if (!(txin.nSequence == CTxIn::SEQUENCE_FINAL))
                return false;
        }
        return true;
        */
}

/**
  | Calculates the block height and previous
  | block's median time past at which the
  | transaction will be considered final
  | in the context of BIP 68.
  | 
  | Also removes from the vector of input
  | heights any entries which did not correspond
  | to sequence locked inputs as they do
  | not affect the calculation.
  |
  */
pub fn calculate_sequence_locks(
        tx:           &Transaction,
        flags:        i32,
        prev_heights: &mut Vec<i32>,
        block:        &BlockIndex) -> (i32,i64) {
    
    todo!();
        /*
            assert(prevHeights.size() == tx.vin.size());

        // Will be set to the equivalent height- and time-based nLockTime
        // values that would be necessary to satisfy all relative lock-
        // time constraints given our view of block chain history.
        // The semantics of nLockTime are the last invalid height/time, so
        // use -1 to have the effect of any height or time being valid.
        int nMinHeight = -1;
        int64_t nMinTime = -1;

        // tx.nVersion is signed integer so requires cast to unsigned otherwise
        // we would be doing a signed comparison and half the range of nVersion
        // wouldn't support BIP 68.
        bool fEnforceBIP68 = static_cast<uint32_t>(tx.nVersion) >= 2
                          && flags & LOCKTIME_VERIFY_SEQUENCE;

        // Do not enforce sequence numbers as a relative lock time
        // unless we have been instructed to
        if (!fEnforceBIP68) {
            return std::make_pair(nMinHeight, nMinTime);
        }

        for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
            const CTxIn& txin = tx.vin[txinIndex];

            // Sequence numbers with the most significant bit set are not
            // treated as relative lock-times, nor are they given any
            // consensus-enforced meaning at this point.
            if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) {
                // The height of this input is not relevant for sequence locks
                prevHeights[txinIndex] = 0;
                continue;
            }

            int nCoinHeight = prevHeights[txinIndex];

            if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) {
                int64_t nCoinTime = block.GetAncestor(std::max(nCoinHeight-1, 0))->GetMedianTimePast();
                // NOTE: Subtract 1 to maintain nLockTime semantics
                // BIP 68 relative lock times have the semantics of calculating
                // the first block or time at which the transaction would be
                // valid. When calculating the effective block time or height
                // for the entire transaction, we switch to using the
                // semantics of nLockTime which is the last invalid block
                // time or height.  Thus we subtract 1 from the calculated
                // time or height.

                // Time-based relative lock-times are measured from the
                // smallest allowed timestamp of the block containing the
                // txout being spent, which is the median time past of the
                // block prior.
                nMinTime = std::max(nMinTime, nCoinTime + (int64_t)((txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) << CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) - 1);
            } else {
                nMinHeight = std::max(nMinHeight, nCoinHeight + (int)(txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) - 1);
            }
        }

        return std::make_pair(nMinHeight, nMinTime);
        */
}

pub fn evaluate_sequence_locks(
        block:     &BlockIndex,
        lock_pair: (i32,i64)) -> bool {
    
    todo!();
        /*
            assert(block.pprev);
        int64_t nBlockTime = block.pprev->GetMedianTimePast();
        if (lockPair.first >= block.nHeight || lockPair.second >= nBlockTime)
            return false;

        return true;
        */
}

/**
  | Check if transaction is final per BIP
  | 68 sequence numbers and can be included
  | in a block.
  | 
  | Consensus critical. Takes as input
  | a list of heights at which tx's inputs
  | (in order) confirmed.
  |
  */
pub fn sequence_locks(
        tx:           &Transaction,
        flags:        i32,
        prev_heights: &mut Vec<i32>,
        block:        &BlockIndex) -> bool {
    
    todo!();
        /*
            return EvaluateSequenceLocks(block, CalculateSequenceLocks(tx, flags, prevHeights, block));
        */
}

/**
  | Count ECDSA signature operations the
  | old-fashioned (pre-0.6) way
  | 
  | 
  | -----------
  | @return
  | 
  | number of sigops this transaction's
  | outputs will produce when spent @see
  | CTransaction::FetchInputs
  |
  */
pub fn get_legacy_sig_op_count(tx: &Transaction) -> u32 {
    
    todo!();
        /*
            unsigned int nSigOps = 0;
        for (const auto& txin : tx.vin)
        {
            nSigOps += txin.scriptSig.GetSigOpCount(false);
        }
        for (const auto& txout : tx.vout)
        {
            nSigOps += txout.scriptPubKey.GetSigOpCount(false);
        }
        return nSigOps;
        */
}

/**
  | Count ECDSA signature operations in
  | pay-to-script-hash inputs.
  | 
  | -----------
  | @param[in] mapInputs
  | 
  | Map of previous transactions that have
  | outputs we're spending
  | 
  | -----------
  | @return
  | 
  | maximum number of sigops required to
  | validate this transaction's inputs
  | @see CTransaction::FetchInputs
  |
  */
pub fn get_p2sh_sig_op_count(
        tx:     &Transaction,
        inputs: &CoinsViewCache) -> u32 {
    
    todo!();
        /*
            if (tx.IsCoinBase())
            return 0;

        unsigned int nSigOps = 0;
        for (unsigned int i = 0; i < tx.vin.size(); i++)
        {
            const Coin& coin = inputs.AccessCoin(tx.vin[i].prevout);
            assert(!coin.IsSpent());
            const CTxOut &prevout = coin.out;
            if (prevout.scriptPubKey.IsPayToScriptHash())
                nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
        }
        return nSigOps;
        */
}

/**
  | Compute total signature operation
  | cost of a transaction.
  | 
  | -----------
  | @param[in] tx
  | 
  | Transaction for which we are computing
  | the cost
  | ----------
  | @param[in] inputs
  | 
  | Map of previous transactions that have
  | outputs we're spending
  | ----------
  | @param[in] flags
  | 
  | Script verification flags
  | 
  | -----------
  | @return
  | 
  | Total signature operation cost of tx
  |
  */
pub fn get_transaction_sig_op_cost(
        tx:     &Transaction,
        inputs: &CoinsViewCache,
        flags:  u32) -> i64 {
    
    todo!();
        /*
            int64_t nSigOps = GetLegacySigOpCount(tx) * WITNESS_SCALE_FACTOR;

        if (tx.IsCoinBase())
            return nSigOps;

        if (flags & SCRIPT_VERIFY_P2SH) {
            nSigOps += GetP2SHSigOpCount(tx, inputs) * WITNESS_SCALE_FACTOR;
        }

        for (unsigned int i = 0; i < tx.vin.size(); i++)
        {
            const Coin& coin = inputs.AccessCoin(tx.vin[i].prevout);
            assert(!coin.IsSpent());
            const CTxOut &prevout = coin.out;
            nSigOps += CountWitnessSigOps(tx.vin[i].scriptSig, prevout.scriptPubKey, &tx.vin[i].scriptWitness, flags);
        }
        return nSigOps;
        */
}

/**
  | Check whether all inputs of this transaction
  | are valid (no double spends and amounts)
  | 
  | This does not modify the UTXO set. This
  | does not check scripts and sigs.
  | 
  | -----------
  | @param[out] txfee
  | 
  | Set to the transaction fee if successful.
  | 
  | Preconditions: tx.IsCoinBase() is
  | false.
  |
  */
pub fn check_tx_inputs(
    tx:             &Transaction,
    state:          &mut TxValidationState,
    inputs:         &CoinsViewCache,
    n_spend_height: i32,
    txfee:          &mut Amount) -> bool {
    
    todo!();
    /*
        // are the actual inputs available?
    if (!inputs.HaveInputs(tx)) {
        return state.Invalid(TxValidationResult::TX_MISSING_INPUTS, "bad-txns-inputs-missingorspent",
                         strprintf("%s: inputs missing/spent", __func__));
    }

    CAmount nValueIn = 0;
    for (unsigned int i = 0; i < tx.vin.size(); ++i) {
        const OutPoint &prevout = tx.vin[i].prevout;
        const Coin& coin = inputs.AccessCoin(prevout);
        assert(!coin.IsSpent());

        // If prev is coinbase, check that it's matured
        if (coin.IsCoinBase() && nSpendHeight - coin.nHeight < COINBASE_MATURITY) {
            return state.Invalid(TxValidationResult::TX_PREMATURE_SPEND, "bad-txns-premature-spend-of-coinbase",
                strprintf("tried to spend coinbase at depth %d", nSpendHeight - coin.nHeight));
        }

        // Check for negative or overflow input values
        nValueIn += coin.out.nValue;
        if (!MoneyRange(coin.out.nValue) || !MoneyRange(nValueIn)) {
            return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputvalues-outofrange");
        }
    }

    const CAmount value_out = tx.GetValueOut();
    if (nValueIn < value_out) {
        return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-in-belowout",
            strprintf("value in (%s) < value out (%s)", FormatMoney(nValueIn), FormatMoney(value_out)));
    }

    // Tally transaction fees
    const CAmount txfee_aux = nValueIn - value_out;
    if (!MoneyRange(txfee_aux)) {
        return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-fee-outofrange");
    }

    txfee = txfee_aux;
    return true;
    */
}

//-------------------------------------------[.cpp/bitcoin/src/consensus/tx_check.h]
//-------------------------------------------[.cpp/bitcoin/src/consensus/tx_check.cpp]

/**
  | Context-independent transaction
  | checking code that can be called outside
  | the bitcoin server and doesn't depend
  | on chain or mempool state. Transaction
  | verification code that does call server
  | functions or depend on server state
  | belongs in tx_verify.h/cpp instead.
  |
  */
pub fn check_transaction(
        tx:    &Transaction,
        state: &mut TxValidationState) -> bool {
    
    todo!();
        /*
            // Basic checks that don't depend on any context
        if (tx.vin.empty())
            return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty");
        if (tx.vout.empty())
            return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty");
        // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
        if (::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
            return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize");

        // Check for negative or overflow output values (see CVE-2010-5139)
        CAmount nValueOut = 0;
        for (const auto& txout : tx.vout)
        {
            if (txout.nValue < 0)
                return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative");
            if (txout.nValue > MAX_MONEY)
                return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge");
            nValueOut += txout.nValue;
            if (!MoneyRange(nValueOut))
                return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge");
        }

        // Check for duplicate inputs (see CVE-2018-17144)
        // While consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs
        // of a tx as spent, it does not check if the tx has duplicate inputs.
        // Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
        // the underlying coins database.
        std::set<OutPoint> vInOutPoints;
        for (const auto& txin : tx.vin) {
            if (!vInOutPoints.insert(txin.prevout).second)
                return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
        }

        if (tx.IsCoinBase())
        {
            if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
                return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
        }
        else
        {
            for (const auto& txin : tx.vin)
                if (txin.prevout.IsNull())
                    return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");
        }

        return true;
        */
}