solid-pod-rs-server 0.5.0-alpha.18

Drop-in Solid Pod server binary. Wraps solid-pod-rs with actix-web and a JSS-compatible layered config loader.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
//! Native mempool.space REST client — the read-side of block-trail anchors.
//!
//! [`MempoolHttpClient`](crate::mempool::MempoolHttpClient) is the server-side concrete implementation of the
//! pure [`solid_pod_rs::mrc20::MempoolLookup`] trait. It speaks the
//! mempool.space-style REST API over the `reqwest` client the crate already
//! pulls in for the CORS proxy and webhook delivery:
//!
//! | Method | Path                              | Returns          |
//! |--------|-----------------------------------|------------------|
//! | GET    | `{base}/api/address/{addr}/utxo`  | `Vec<Utxo>`      |
//! | GET    | `{base}/api/tx/{txid}`            | `TxInfo`         |
//!
//! The wire shapes (`status: {confirmed, block_height}` nested objects) are
//! deserialised into local `*Wire` structs and flattened into the crate's
//! transport-free [`Utxo`](solid_pod_rs::mrc20::Utxo)/[`TxInfo`](solid_pod_rs::mrc20::TxInfo) value types, so the pure verification
//! surface never learns the mempool.space schema.
//!
//! ## wasm boundary
//!
//! This module is native-only (it builds a `reqwest::Client`). It mirrors
//! the JSS `verifyMrc20Anchor` mempool round-trip (`mrc20.js:315-327`,
//! `token.js:176-187`) and is the production [`MempoolLookup`](solid_pod_rs::mrc20::MempoolLookup) the
//! `/pay/.deposit` MRC20 path and `/pay/.address` derivation use. wasm
//! consumers implement [`MempoolLookup`](solid_pod_rs::mrc20::MempoolLookup) over `fetch` instead and never
//! compile this file.
//!
//! ## Configuration
//!
//! The base URL is read from `JSS_PAY_MEMPOOL_URL` (JSS `mempoolUrl`
//! parity), defaulting to the testnet4 explorer
//! `https://mempool.space/testnet4`. The reqwest `json` feature is *not*
//! enabled crate-wide, so responses are read as text and parsed with
//! `serde_json` (matching the proxy handler's manual-parse style).
//!
//! The endpoint choice is not silent: [`select_mempool_endpoint`](crate::mempool::select_mempool_endpoint) resolves the
//! base URL, [`infer_network`](crate::mempool::infer_network) classifies the Bitcoin network it serves, and
//! [`log_mempool_selection`](crate::mempool::log_mempool_selection) records both (plus whether the URL was
//! operator-supplied or defaulted) in the startup log — with a warning when the
//! pod would otherwise be anchoring against an unchosen or unclassifiable
//! chain. [`MempoolSelection::to_manifest_json`](crate::mempool::MempoolSelection::to_manifest_json) renders the same facts for a
//! manifest (ADR-2007).

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::time::Duration;

use solid_pod_rs::bitcoin_tx::{
    anchor_state, gitmark_advance, gitmark_genesis, GitmarkUpdate, MempoolBroadcast, TxoVoucher,
    DEFAULT_FEE_SATS,
};
use solid_pod_rs::blocktrail::{verify_anchor_chain, TrailReport};
use solid_pod_rs::mrc20::{bt_address, MempoolLookup, TxIn, TxInfo, TxOut, Utxo};
use solid_pod_rs::payments::PaymentError;
use solid_pod_rs::provenance::{BlockAnchorer, BlockTrailAnchor, ProvenanceError};

/// Environment variable selecting the mempool REST base URL (JSS parity).
pub const MEMPOOL_URL_ENV: &str = "JSS_PAY_MEMPOOL_URL";

/// Default base URL — the mempool.space **testnet4** explorer. Matches the
/// JSS default (`pay.js:243`, `mrc20.js:282`).
pub const DEFAULT_MEMPOOL_URL: &str = "https://mempool.space/testnet4";

/// Environment override for the mempool client's connect timeout, in seconds.
pub const MEMPOOL_CONNECT_TIMEOUT_ENV: &str = "JSS_PAY_MEMPOOL_CONNECT_TIMEOUT_SECS";

/// Environment override for the mempool client's total request timeout, in
/// seconds.
pub const MEMPOOL_TIMEOUT_ENV: &str = "JSS_PAY_MEMPOOL_TIMEOUT_SECS";

/// Connect timeout when [`MEMPOOL_CONNECT_TIMEOUT_ENV`] is unset or invalid.
const DEFAULT_CONNECT_TIMEOUT_SECS: u64 = 5;

/// Total request timeout when [`MEMPOOL_TIMEOUT_ENV`] is unset or invalid.
const DEFAULT_TIMEOUT_SECS: u64 = 20;

/// A positive whole number of seconds from the environment, else `default`.
fn env_secs(var: &str, default: u64) -> Duration {
    let secs = std::env::var(var)
        .ok()
        .and_then(|v| v.trim().parse::<u64>().ok())
        .filter(|s| *s > 0)
        .unwrap_or(default);
    Duration::from_secs(secs)
}

/// An HTTP client that gives up on a backend that stops answering.
fn http_client(connect: Duration, total: Duration) -> reqwest::Client {
    reqwest::Client::builder()
        .connect_timeout(connect)
        .timeout(total)
        .build()
        .unwrap_or_default()
}

// ---------------------------------------------------------------------------
// Endpoint selection (ADR-2007) — pure, no I/O
// ---------------------------------------------------------------------------
//
// The base URL alone does not tell an operator *why* the pod is pointed where
// it is, nor which Bitcoin network that endpoint serves. A silent fall back to
// the built-in testnet4 default could otherwise have the pod anchoring and
// verifying against a chain nobody chose. The types below make the choice
// explicit, classifiable and loggable without performing any I/O, so the
// startup path can record it in the log and in a manifest.

/// Where the mempool base URL came from.
///
/// [`MempoolConfigSource::Explicit`] means an operator supplied it (via
/// [`MEMPOOL_URL_ENV`] or [`MempoolHttpClient::new`]);
/// [`MempoolConfigSource::Default`] means nothing was configured and the
/// built-in [`DEFAULT_MEMPOOL_URL`] was used. The distinction matters because a
/// defaulted endpoint is an *unchosen* chain.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MempoolConfigSource {
    /// The operator supplied the base URL.
    Explicit,
    /// Nothing was configured; [`DEFAULT_MEMPOOL_URL`] was used.
    Default,
}

impl MempoolConfigSource {
    /// Lower-case wire name (`"explicit"` / `"default"`) used in logs and the
    /// manifest JSON.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Explicit => "explicit",
            Self::Default => "default",
        }
    }
}

impl std::fmt::Display for MempoolConfigSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// The Bitcoin network a mempool endpoint is understood to serve.
///
/// [`BitcoinNetwork::Unknown`] is deliberate: an unrecognised operator-supplied
/// explorer is *not* assumed to be mainnet (or anything else). Guessing here
/// would reintroduce exactly the silent-wrong-chain risk this type exists to
/// remove.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BitcoinNetwork {
    /// Bitcoin mainnet.
    Mainnet,
    /// The legacy testnet3 network (`/testnet` or `/testnet3`).
    Testnet3,
    /// The testnet4 network (the crate default).
    Testnet4,
    /// The signet test network.
    Signet,
    /// A local regtest node (loopback host with no network in the path).
    Regtest,
    /// Not classifiable from the URL — treat as unverified.
    Unknown,
}

impl BitcoinNetwork {
    /// Lower-case wire name used in logs and the manifest JSON.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Mainnet => "mainnet",
            Self::Testnet3 => "testnet3",
            Self::Testnet4 => "testnet4",
            Self::Signet => "signet",
            Self::Regtest => "regtest",
            Self::Unknown => "unknown",
        }
    }
}

impl std::fmt::Display for BitcoinNetwork {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Classify the Bitcoin network a mempool base URL serves, purely from the URL.
///
/// The trailing path segment is matched case-insensitively against the
/// mempool.space layout:
///
/// | Base URL                         | Network    |
/// |----------------------------------|------------|
/// | `https://mempool.space`          | `Mainnet`  |
/// | `https://mempool.space/testnet4` | `Testnet4` |
/// | `https://mempool.space/testnet`  | `Testnet3` |
/// | `https://mempool.space/testnet3` | `Testnet3` |
/// | `https://mempool.space/signet`   | `Signet`   |
/// | `http://127.0.0.1:3006`          | `Regtest`  |
/// | anything else                    | `Unknown`  |
///
/// A loopback host is taken to be a local regtest node *unless* its path names a
/// network explicitly (`http://localhost:3006/signet` is `Signet`). Anything
/// unrecognised is [`BitcoinNetwork::Unknown`] — never a guess.
#[must_use]
pub fn infer_network(base_url: &str) -> BitcoinNetwork {
    let trimmed = base_url.trim().trim_end_matches('/');

    // Split scheme / authority / path by hand: the base may legitimately be
    // scheme-relative in a fixture, and a full URL parser would reject it.
    let after_scheme = match trimmed.find("://") {
        Some(i) => &trimmed[i + 3..],
        None => trimmed,
    };
    let (authority, path) = match after_scheme.find('/') {
        Some(i) => (&after_scheme[..i], &after_scheme[i + 1..]),
        None => (after_scheme, ""),
    };

    // Last non-empty path segment, ignoring any query/fragment tail.
    let path = path
        .split(['?', '#'])
        .next()
        .unwrap_or("")
        .trim_end_matches('/');
    let last_segment = path.rsplit('/').find(|seg| !seg.is_empty()).unwrap_or("");

    match last_segment.to_ascii_lowercase().as_str() {
        "testnet4" => return BitcoinNetwork::Testnet4,
        "testnet" | "testnet3" => return BitcoinNetwork::Testnet3,
        "signet" => return BitcoinNetwork::Signet,
        "regtest" => return BitcoinNetwork::Regtest,
        "mainnet" | "bitcoin" => return BitcoinNetwork::Mainnet,
        _ => {}
    }

    // Host (minus any userinfo / port) drives the remaining cases.
    let host = authority.rsplit('@').next().unwrap_or(authority);
    let host = if let Some(rest) = host.strip_prefix('[') {
        // Bracketed IPv6 literal: keep the address, drop the port.
        rest.split(']').next().unwrap_or(rest)
    } else {
        host.split(':').next().unwrap_or(host)
    };
    let host_lower = host.to_ascii_lowercase();

    let is_loopback = host_lower == "localhost"
        || host_lower == "::1"
        || host_lower.starts_with("127.")
        || host_lower.ends_with(".localhost");
    if is_loopback {
        // A local node with no network in the path is a regtest node.
        return BitcoinNetwork::Regtest;
    }

    // A bare mempool.space-style host with no network path is mainnet.
    if path.is_empty() && (host_lower == "mempool.space" || host_lower.ends_with(".mempool.space"))
    {
        return BitcoinNetwork::Mainnet;
    }

    BitcoinNetwork::Unknown
}

/// The resolved mempool endpoint: which URL, which chain, and why.
///
/// Produced by [`select_mempool_endpoint`] /
/// [`select_mempool_endpoint_from_env`] and carried on [`MempoolHttpClient`] so
/// the choice can be logged at startup ([`log_mempool_selection`]) and surfaced
/// in a manifest ([`MempoolSelection::to_manifest_json`]).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MempoolSelection {
    /// The base URL actually in use, trailing slash trimmed.
    pub base_url: String,
    /// The Bitcoin network inferred from `base_url`.
    pub network: BitcoinNetwork,
    /// Whether `base_url` was operator-supplied or the built-in default.
    pub source: MempoolConfigSource,
}

impl MempoolSelection {
    /// Render the selection as manifest JSON:
    /// `{"base_url": …, "network": …, "source": "explicit"|"default"}`.
    #[must_use]
    pub fn to_manifest_json(&self) -> serde_json::Value {
        serde_json::json!({
            "base_url": self.base_url,
            "network": self.network.as_str(),
            "source": self.source.as_str(),
        })
    }
}

/// Resolve the mempool endpoint from an optional configured value.
///
/// A `configured` value that is `None`, empty or whitespace-only is treated as
/// absent (matching the historical `JSS_PAY_MEMPOOL_URL` filter), yielding
/// [`DEFAULT_MEMPOOL_URL`] with [`MempoolConfigSource::Default`]. Any trailing
/// `/` is trimmed so `{base}/api/...` joins cleanly. Pure — no environment or
/// network access.
#[must_use]
pub fn select_mempool_endpoint(configured: Option<&str>) -> MempoolSelection {
    let (raw, source) = match configured.map(str::trim).filter(|v| !v.is_empty()) {
        Some(v) => (v, MempoolConfigSource::Explicit),
        None => (DEFAULT_MEMPOOL_URL, MempoolConfigSource::Default),
    };
    let base_url = raw.trim_end_matches('/').to_string();
    let network = infer_network(&base_url);
    MempoolSelection {
        base_url,
        network,
        source,
    }
}

/// Resolve the mempool endpoint from [`MEMPOOL_URL_ENV`], delegating the (pure)
/// decision to [`select_mempool_endpoint`].
#[must_use]
pub fn select_mempool_endpoint_from_env() -> MempoolSelection {
    let configured = std::env::var(MEMPOOL_URL_ENV).ok();
    select_mempool_endpoint(configured.as_deref())
}

/// Record the selected endpoint in the startup log.
///
/// Emits exactly one `INFO` on target `solid_pod_rs_server::mempool` carrying
/// the structured fields `base_url`, `network` and `source`, so an operator can
/// tell from the log which chain the pod anchors and verifies against. When the
/// network could not be classified, or the endpoint was defaulted rather than
/// chosen, an additional `WARN` names [`MEMPOOL_URL_ENV`] as the fix.
pub fn log_mempool_selection(sel: &MempoolSelection) {
    tracing::info!(
        target: "solid_pod_rs_server::mempool",
        base_url = %sel.base_url,
        network = sel.network.as_str(),
        source = sel.source.as_str(),
        "mempool endpoint selected"
    );
    if sel.network == BitcoinNetwork::Unknown || sel.source == MempoolConfigSource::Default {
        tracing::warn!(
            target: "solid_pod_rs_server::mempool",
            base_url = %sel.base_url,
            network = sel.network.as_str(),
            source = sel.source.as_str(),
            env_var = MEMPOOL_URL_ENV,
            "pod is using an unverified or defaulted Bitcoin endpoint; anchors may be \
             written to or verified against an unintended chain — set {} to pin the \
             explorer, and therefore the network, explicitly",
            MEMPOOL_URL_ENV
        );
    }
}

/// Emit [`log_mempool_selection`] at most once for the lifetime of the
/// process.
///
/// `MempoolHttpClient::from_env` is called per request on the payment and
/// provenance routes, so logging unconditionally there would repeat the
/// startup record on every request. The selection is process-global (it comes
/// from an environment variable), so one record is exactly the right number:
/// it lands in the startup log the first time the endpoint is resolved, and
/// `solid-pod-rs-server`'s `main` calls this explicitly at boot so the record
/// appears even on a pod that never serves a payment route.
///
/// Callers that want the value rather than the log should read
/// [`MempoolHttpClient::selection`] or call [`select_mempool_endpoint`].
pub fn log_mempool_selection_once(sel: &MempoolSelection) {
    static ONCE: std::sync::Once = std::sync::Once::new();
    ONCE.call_once(|| log_mempool_selection(sel));
}

/// A [`MempoolLookup`] backed by the mempool.space REST API over `reqwest`.
///
/// Cheap to clone (holds an `Arc`-internal `reqwest::Client` and the base
/// URL). Construct with [`MempoolHttpClient::from_env`] to honour
/// `JSS_PAY_MEMPOOL_URL`, or [`MempoolHttpClient::new`] for an explicit base.
#[derive(Debug, Clone)]
pub struct MempoolHttpClient {
    client: reqwest::Client,
    /// Base URL with any trailing slash trimmed (so `{base}/api/...` joins
    /// cleanly regardless of how the operator wrote the env value).
    base: String,
    /// The resolved endpoint choice — URL, inferred network, and where the URL
    /// came from — so the selection is recordable rather than an invisible
    /// default (ADR-2007).
    selection: MempoolSelection,
}

impl MempoolHttpClient {
    /// Construct a client against an explicit base URL (e.g.
    /// `https://mempool.space/testnet4`). A trailing `/` is trimmed.
    #[must_use]
    pub fn new(base_url: impl Into<String>) -> Self {
        let raw = base_url.into();
        let base = raw.trim_end_matches('/').to_string();
        // An explicit constructor argument is operator-chosen by definition,
        // even when it happens to equal DEFAULT_MEMPOOL_URL.
        let selection = MempoolSelection {
            network: infer_network(&base),
            base_url: base,
            source: MempoolConfigSource::Explicit,
        };
        Self::from_selection(selection)
    }

    /// Construct from an already-resolved [`MempoolSelection`]. Does not log —
    /// the caller owns whether this choice is recorded (see
    /// [`log_mempool_selection`]).
    #[must_use]
    pub fn from_selection(selection: MempoolSelection) -> Self {
        Self {
            client: http_client(
                env_secs(MEMPOOL_CONNECT_TIMEOUT_ENV, DEFAULT_CONNECT_TIMEOUT_SECS),
                env_secs(MEMPOOL_TIMEOUT_ENV, DEFAULT_TIMEOUT_SECS),
            ),
            base: selection.base_url.clone(),
            selection,
        }
    }

    /// Replace the connect and total request timeouts (the defaults come from
    /// [`MEMPOOL_CONNECT_TIMEOUT_ENV`] / [`MEMPOOL_TIMEOUT_ENV`]). The
    /// anchoring locks are held across these calls, so a backend that never
    /// answers must be cut off.
    #[must_use]
    pub fn with_timeouts(mut self, connect: Duration, total: Duration) -> Self {
        self.client = http_client(connect, total);
        self
    }

    /// Construct from `JSS_PAY_MEMPOOL_URL`, falling back to
    /// [`DEFAULT_MEMPOOL_URL`] (testnet4).
    ///
    /// The resolved endpoint is logged exactly once via
    /// [`log_mempool_selection`], so the startup log always records which
    /// explorer — and therefore which Bitcoin network — the pod is using, with
    /// a warning when that endpoint was defaulted or is unclassifiable.
    #[must_use]
    pub fn from_env() -> Self {
        let selection = select_mempool_endpoint_from_env();
        log_mempool_selection_once(&selection);
        Self::from_selection(selection)
    }

    /// The configured base URL (trailing slash trimmed).
    #[must_use]
    pub fn base_url(&self) -> &str {
        &self.base
    }

    /// The resolved endpoint selection: base URL, inferred Bitcoin network, and
    /// whether the URL was operator-supplied or defaulted.
    #[must_use]
    pub fn selection(&self) -> &MempoolSelection {
        &self.selection
    }

    /// Check whether a transaction is known without collapsing an HTTP 404
    /// into the same result as an ambiguous transport/server failure. Payment
    /// intent recovery may compensate a debit only for `Ok(false)`.
    pub async fn transaction_exists(&self, txid: &str) -> Result<bool, PaymentError> {
        let url = format!("{}/api/tx/{txid}", self.base);
        let resp = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| PaymentError::InvalidState(format!("mempool request failed: {e}")))?;
        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            return Ok(false);
        }
        if !resp.status().is_success() {
            return Err(PaymentError::InvalidState(format!(
                "mempool API error: {} for {url}",
                resp.status().as_u16()
            )));
        }
        Ok(true)
    }

    /// GET `url`, returning the body text on a 2xx, or a fail-closed
    /// [`PaymentError::InvalidState`] describing the transport/status error.
    async fn get_text(&self, url: &str) -> Result<String, PaymentError> {
        let resp = self
            .client
            .get(url)
            .send()
            .await
            .map_err(|e| PaymentError::InvalidState(format!("mempool request failed: {e}")))?;
        let status = resp.status();
        if !status.is_success() {
            return Err(PaymentError::InvalidState(format!(
                "mempool API error: {} for {url}",
                status.as_u16()
            )));
        }
        resp.text()
            .await
            .map_err(|e| PaymentError::InvalidState(format!("mempool body read failed: {e}")))
    }

    /// POST `body` as `text/plain` to `url`, returning the response body on a
    /// 2xx (the txid, for `/api/tx`) or a fail-closed
    /// [`PaymentError::InvalidState`]. Mirrors JSS `broadcastTx`
    /// (`token.js:176-187`).
    async fn post_text(&self, url: &str, body: &str) -> Result<String, PaymentError> {
        let resp = self
            .client
            .post(url)
            .header("Content-Type", "text/plain")
            .body(body.to_string())
            .send()
            .await
            .map_err(|e| PaymentError::InvalidState(format!("mempool broadcast failed: {e}")))?;
        let status = resp.status();
        let text = resp
            .text()
            .await
            .map_err(|e| PaymentError::InvalidState(format!("mempool body read failed: {e}")))?;
        if !status.is_success() {
            return Err(PaymentError::InvalidState(format!(
                "broadcast rejected ({}): {text}",
                status.as_u16()
            )));
        }
        Ok(text.trim().to_string())
    }
}

// ── Wire shapes (mempool.space schema) ──────────────────────────────────

/// Nested `status` object on UTXO/tx responses.
#[derive(Debug, Deserialize, Default)]
struct StatusWire {
    #[serde(default)]
    confirmed: bool,
    #[serde(default)]
    block_height: Option<u64>,
}

/// One element of `GET /api/address/{addr}/utxo`.
#[derive(Debug, Deserialize)]
struct UtxoWire {
    txid: String,
    vout: u32,
    #[serde(default)]
    value: u64,
    #[serde(default)]
    status: StatusWire,
}

impl From<UtxoWire> for Utxo {
    fn from(w: UtxoWire) -> Self {
        Utxo {
            txid: w.txid,
            vout: w.vout,
            value: w.value,
            confirmed: w.status.confirmed,
            block_height: w.status.block_height,
        }
    }
}

/// One element of a tx's `vout` array.
#[derive(Debug, Deserialize, Default)]
struct TxOutWire {
    #[serde(default)]
    value: u64,
    #[serde(default)]
    scriptpubkey: Option<String>,
    #[serde(default)]
    scriptpubkey_address: Option<String>,
}

impl From<TxOutWire> for TxOut {
    fn from(w: TxOutWire) -> Self {
        TxOut {
            value: w.value,
            scriptpubkey: w.scriptpubkey,
            scriptpubkey_address: w.scriptpubkey_address,
        }
    }
}

/// One element of a tx's `vin` array: the outpoint it spends (a coinbase
/// input carries the null outpoint).
#[derive(Debug, Deserialize)]
struct TxInWire {
    #[serde(default)]
    txid: String,
    #[serde(default)]
    vout: u32,
}

/// Shape of `GET /api/tx/{txid}`.
#[derive(Debug, Deserialize)]
struct TxWire {
    txid: String,
    #[serde(default)]
    vin: Vec<TxInWire>,
    #[serde(default)]
    vout: Vec<TxOutWire>,
    #[serde(default)]
    status: StatusWire,
}

impl From<TxWire> for TxInfo {
    fn from(w: TxWire) -> Self {
        TxInfo {
            txid: w.txid,
            vin: w
                .vin
                .into_iter()
                .map(|i| TxIn {
                    txid: i.txid,
                    vout: i.vout,
                })
                .collect(),
            vout: w.vout.into_iter().map(TxOut::from).collect(),
            confirmed: w.status.confirmed,
            block_height: w.status.block_height,
        }
    }
}

#[async_trait(?Send)]
impl MempoolLookup for MempoolHttpClient {
    async fn address_utxos(&self, address: &str) -> Result<Vec<Utxo>, PaymentError> {
        let url = format!("{}/api/address/{address}/utxo", self.base);
        let body = self.get_text(&url).await?;
        let wire: Vec<UtxoWire> = serde_json::from_str(&body)
            .map_err(|e| PaymentError::InvalidState(format!("malformed utxo JSON: {e}")))?;
        Ok(wire.into_iter().map(Utxo::from).collect())
    }

    async fn tx(&self, txid: &str) -> Result<TxInfo, PaymentError> {
        let url = format!("{}/api/tx/{txid}", self.base);
        let body = self.get_text(&url).await?;
        let wire: TxWire = serde_json::from_str(&body)
            .map_err(|e| PaymentError::InvalidState(format!("malformed tx JSON: {e}")))?;
        Ok(TxInfo::from(wire))
    }
}

#[async_trait(?Send)]
impl MempoolBroadcast for MempoolHttpClient {
    async fn broadcast_tx(&self, raw_hex: &str) -> Result<String, PaymentError> {
        let url = format!("{}/api/tx", self.base);
        self.post_text(&url, raw_hex).await
    }
}

// ---------------------------------------------------------------------------
// BlockAnchorer::verify — the portable-proof read-side (provenance §2.2)
// ---------------------------------------------------------------------------

/// A [`BlockAnchorer`] implementing **both** sides over a transport that can
/// look up UTXOs ([`MempoolLookup`]) and broadcast transactions
/// ([`MempoolBroadcast`]). Generic over that transport so a fixture drives it
/// in tests and [`MempoolHttpClient`] drives it in production — without
/// changing the logic.
///
/// - `verify` re-derives the expected taproot address from the anchor's
///   *portable proof* (`pubkey` + `state_strings`) via [`bt_address`], rejects
///   a forged `address`, then walks the trail back from the anchor's outpoint
///   ([`verify_anchor_report`]): every mark must exist, carry the key its
///   prefix of states derives and spend the mark before it. No pod trust
///   required, and an anchor stays verifiable after later marks spend it.
///
/// This anchorer serves the MRC20 trails a pod has already issued (their
/// marks and states stay as they are); new trails use [`GitmarkAnchorer`].
/// - `anchor` (Phase 4) loads the named trail from storage, appends an MRC20
///   state notarising `state_hash` (via
///   [`anchor_state`], broadcasts the
///   anchoring tx, persists the updated trail, and returns the
///   [`BlockTrailAnchor`] (txid/vout/address/state_strings/pubkey). It requires
///   a `storage` handle (set via [`MempoolBlockAnchorer::with_storage`]); the
///   verify-only constructor [`MempoolBlockAnchorer::new`] leaves it `None` and
///   `anchor()` then errors with a clear message.
#[derive(Clone)]
pub struct MempoolBlockAnchorer<M: MempoolLookup + MempoolBroadcast + Send + Sync> {
    lookup: M,
    storage: Option<std::sync::Arc<dyn solid_pod_rs::storage::Storage>>,
}

impl<M: MempoolLookup + MempoolBroadcast + Send + Sync> MempoolBlockAnchorer<M> {
    /// Wrap a transport as a **verify-capable** [`BlockAnchorer`]. `anchor()`
    /// is unavailable (no storage) and returns an error explaining that
    /// [`with_storage`](Self::with_storage) is required.
    pub fn new(lookup: M) -> Self {
        Self {
            lookup,
            storage: None,
        }
    }

    /// Wrap a transport + pod storage as a **fully-capable** [`BlockAnchorer`]
    /// (both `verify` and `anchor`). The `storage` backs the trail load/save at
    /// `/.well-known/token/{ticker}.json`.
    pub fn with_storage(
        lookup: M,
        storage: std::sync::Arc<dyn solid_pod_rs::storage::Storage>,
    ) -> Self {
        Self {
            lookup,
            storage: Some(storage),
        }
    }

    /// Borrow the underlying transport (e.g. for a one-off `address_utxos`).
    pub fn lookup(&self) -> &M {
        &self.lookup
    }
}

/// Marks an anchor whose broadcast outcome is unknown. Once the intent is
/// recorded the transaction may be on-chain, and the next advance settles it
/// from the chain, so the caller has been served and must not be refunded.
const PENDING_ANCHOR_PREFIX: &str = "anchor pending: ";

/// The error for an anchor that failed after its intent was recorded.
fn pending_anchor(detail: String) -> ProvenanceError {
    ProvenanceError::Anchor(format!("{PENDING_ANCHOR_PREFIX}{detail}"))
}

/// Whether `e` is an anchor left pending (see [`pending_anchor`]) rather than
/// one that definitely did not go out.
#[cfg(any(feature = "git", test))]
pub(crate) fn is_pending_anchor(e: &ProvenanceError) -> bool {
    matches!(e, ProvenanceError::Anchor(m) if m.starts_with(PENDING_ANCHOR_PREFIX))
}

#[async_trait(?Send)]
impl<M: MempoolLookup + MempoolBroadcast + Send + Sync> BlockAnchorer for MempoolBlockAnchorer<M> {
    /// Append one MRC20 state anchoring `state_hash` under `ticker`, build +
    /// broadcast the anchoring tx, persist the updated trail, and return the
    /// produced [`BlockTrailAnchor`]. This is the expensive-tier write the
    /// provenance design hinges on (ADR-059 §2.2, master-plan Phase 4).
    ///
    /// `network` is honoured as a guard: it must match the trail's own network
    /// (the trail's chained-key addresses are network-bound). The returned
    /// anchor's `vout` is `0` (the anchoring tx pays the next chained-key UTXO
    /// at output 0); `blockheight` is `None` until the tx confirms.
    async fn anchor(
        &self,
        ticker: &str,
        state_hash: &str,
        network: &str,
    ) -> Result<BlockTrailAnchor, ProvenanceError> {
        use crate::trail_store::{load_trail, save_trail};

        let storage = self.storage.as_ref().ok_or_else(|| {
            ProvenanceError::Anchor(
                "anchor() requires storage; construct with MempoolBlockAnchorer::with_storage"
                    .into(),
            )
        })?;

        // One advance of this trail at a time (anchorers are per-request).
        let _guard = crate::trail_store::lock_path(&crate::trail_store::trail_path(ticker)).await;

        settle_trail_intent(&self.lookup, storage, ticker).await?;

        // Load the trail that will carry the anchor (JSS `loadTrail`).
        let mut stored = load_trail(storage, ticker)
            .await
            .map_err(|e| ProvenanceError::Anchor(format!("load trail {ticker}: {e}")))?
            .ok_or_else(|| {
                ProvenanceError::Anchor(format!("trail {ticker} not minted on this pod"))
            })?;

        if stored.network != network {
            return Err(ProvenanceError::Anchor(format!(
                "network mismatch: trail is {}, requested {network}",
                stored.network
            )));
        }

        // Build the anchoring tx (appends a state notarising `state_hash`).
        let public = stored.to_public();
        let update = anchor_state(
            &public,
            &stored.privkey,
            state_hash,
            DEFAULT_FEE_SATS,
            &self.lookup,
        )
        .await
        .map_err(|e| ProvenanceError::Anchor(format!("build anchoring tx: {e}")))?;

        // The trail once the tx is in, with the txid the signed tx has as its
        // new currentTxid (so the next anchor/transfer spends this output).
        let expected = solid_pod_rs::bitcoin_tx::decode_tx_info(&update.tx.raw_hex)
            .map_err(|e| ProvenanceError::Anchor(format!("decode anchoring tx: {e}")))?
            .txid;
        let mut appended = update.trail.clone();
        appended.current_txid = expected.clone();
        stored.merge_public(&appended);
        stored.current_txid = expected.clone();
        stored.current_vout = 0;

        // Record the broadcast first: a lost response or a failed save is
        // then settled from the chain by the next advance, not left as a
        // stored head that points at a spent output.
        let intent_path = anchor_intent_path(ticker);
        let body = serde_json::to_vec(&PendingAnchor {
            txid: expected.clone(),
            raw_hex: update.tx.raw_hex.clone(),
            stored: stored.clone(),
        })
        .map_err(|e| ProvenanceError::Anchor(format!("serialise pending anchor: {e}")))?;
        storage
            .put(&intent_path, bytes::Bytes::from(body), "application/json")
            .await
            .map_err(|e| ProvenanceError::Anchor(format!("record pending anchor: {e}")))?;

        // Broadcast (JSS `broadcastTx`). The returned txid IS the anchoring tx.
        let txid = self
            .lookup
            .broadcast_tx(&update.tx.raw_hex)
            .await
            .map_err(|e| pending_anchor(format!("broadcast anchoring tx: {e}")))?;
        if txid != expected {
            return Err(pending_anchor(format!(
                "mempool returned txid {txid} for an anchor that is {expected}"
            )));
        }

        // Once the broadcast is accepted the anchor is on-chain, so a failed
        // save is not a failed anchor: the intent stays and the next advance
        // settles it. The caller must not be refunded for it.
        match save_trail(storage, &stored).await {
            Ok(()) => {
                if let Err(e) = storage.delete(&intent_path).await {
                    // Settling it later is a no-op: the trail is already saved.
                    tracing::warn!(%ticker, "anchor intent not cleared: {e}");
                }
            }
            Err(e) => tracing::error!(
                %ticker, %txid,
                "anchor broadcast accepted but trail save failed; the next advance settles it: {e}"
            ),
        }

        Ok(BlockTrailAnchor {
            ticker: ticker.to_string(),
            state_hash: state_hash.to_string(),
            txid,
            vout: 0,
            address: update.address,
            network: network.to_string(),
            blockheight: None,
            state_strings: appended.state_strings,
            pubkey: Some(stored.pubkey_base),
        })
    }

    async fn verify(&self, anchor: &BlockTrailAnchor) -> Result<bool, ProvenanceError> {
        Ok(verify_anchor_report(&self.lookup, anchor)
            .await?
            .is_some_and(|r| r.is_intact()))
    }
}

impl<M: MempoolLookup + MempoolBroadcast + Send + Sync> MempoolBlockAnchorer<M> {
    /// The per-link report behind [`BlockAnchorer::verify`]: `None` when the
    /// anchor carries no portable proof or its recorded address is not the
    /// derivation; otherwise every mark, walked back from the anchor's
    /// outpoint, with the [`TrailVerdict`](solid_pod_rs::blocktrail::TrailVerdict)
    /// (`verified` once every mark is confirmed).
    pub async fn verify_report(
        &self,
        anchor: &BlockTrailAnchor,
    ) -> Result<Option<TrailReport>, ProvenanceError> {
        verify_anchor_report(&self.lookup, anchor).await
    }
}

/// Check an anchor's portable proof and walk its trail back from the anchor's
/// outpoint: the read side both anchorers share.
///
/// `Ok(None)` when there is nothing to check against (no `pubkey`, no
/// `state_strings`) or the recorded `address` is not the one the proof
/// derives (a forged anchor). Otherwise the
/// [`verify_anchor_chain`]
/// report: every mark recomputed from the proof and compared with its output
/// on-chain, each spending the one before it. A head still in the mempool is
/// intact but not yet verified.
///
/// # Errors
///
/// [`ProvenanceError::Anchor`] when the proof cannot be derived at all (a
/// malformed key, a zero tweak).
pub async fn verify_anchor_report(
    lookup: &dyn MempoolLookup,
    anchor: &BlockTrailAnchor,
) -> Result<Option<TrailReport>, ProvenanceError> {
    // The portable proof requires both the issuer pubkey and the state
    // strings. Absent either, there is nothing to independently re-derive
    // against: not verifiable (None, not an error).
    let Some(pubkey) = anchor.pubkey.as_deref() else {
        return Ok(None);
    };
    if anchor.state_strings.is_empty() {
        return Ok(None);
    }

    // Re-derive the taproot address from the proof and reject a forged
    // `address` field (the recorded address must equal the derivation).
    let derived = bt_address(pubkey, &anchor.state_strings, &anchor.network)
        .map_err(|e| ProvenanceError::Anchor(format!("address re-derivation failed: {e}")))?;
    if derived != anchor.address {
        return Ok(None);
    }

    // Every link: the anchor's own mark, then each mark it spends back to
    // genesis, each the key its prefix of states derives.
    verify_anchor_chain(
        pubkey,
        &anchor.state_strings,
        &anchor.txid,
        anchor.vout,
        lookup,
    )
    .await
    .map(Some)
    .map_err(|e| ProvenanceError::Anchor(format!("anchor chain walk failed: {e}")))
}

// ---------------------------------------------------------------------------
// Pending broadcasts: kept until the trail that carries them is saved
// ---------------------------------------------------------------------------

/// Whether a broadcast error says the node already has this very transaction
/// (in its mempool, in a block, or its outputs in the UTXO set). Anything else
/// is not a statement about the transaction and is never read as one.
fn already_known(e: &PaymentError) -> bool {
    let msg = e.to_string().to_lowercase().replace('-', " ");
    msg.contains("already in mempool")
        || msg.contains("already in block chain")
        || msg.contains("already known")
        || msg.contains("already in utxo set")
        || msg.contains("outputs already in")
}

/// Settle a recorded broadcast that the backend does not report. "Not found"
/// is not "never sent": a backend that took the transaction but has not
/// indexed it says the same. So send it again; the same bytes are harmless.
///
/// - `Ok(true)`: the node has the transaction (sent now, or already known).
/// - `Ok(false)`: the node refused it (a 4xx that is not "already known") **and**
///   it cannot land: the output it spends is shown unspent, or `head` (the
///   outpoint the trail's newest mark now is) is no longer that output, so
///   something else replaced it. The caller may drop its record.
/// - `Err`: nothing was learned (transport, 5xx, a refusal with no proof the
///   input is free); the caller keeps its record and refuses to advance.
async fn settle_broadcast<M: MempoolLookup + MempoolBroadcast + ?Sized>(
    lookup: &M,
    txid: &str,
    raw_hex: &str,
    head: Option<(&str, u32)>,
) -> Result<bool, ProvenanceError> {
    let unresolved = |why: &dyn std::fmt::Display| {
        ProvenanceError::Anchor(format!(
            "a previous mark ({txid}) is unresolved, refusing to advance: {why}"
        ))
    };
    let refusal = match lookup.broadcast_tx(raw_hex).await {
        Ok(t) if t == txid => return Ok(true),
        Ok(t) => {
            return Err(ProvenanceError::Anchor(format!(
                "mempool returned txid {t} for a mark that is {txid}"
            )))
        }
        Err(e) if already_known(&e) => return Ok(true),
        Err(e) if e.to_string().contains("broadcast rejected (4") => e,
        Err(e) => return Err(unresolved(&e)),
    };
    let spent = solid_pod_rs::bitcoin_tx::decode_tx_info(raw_hex)
        .map_err(|e| unresolved(&e))?
        .vin
        .into_iter()
        .next()
        .ok_or_else(|| unresolved(&"it spends nothing"))?;
    // Replaced: the trail already moved off the output this mark spends
    // (and is not this mark), so this mark can never confirm.
    if head.is_some_and(|(t, v)| (t, v) != (spent.txid.as_str(), spent.vout) && t != txid) {
        return Ok(false);
    }
    // Unspent: the node has just refused the mark, so a lagging index cannot
    // be hiding it; the output it spends is still there to be spent again.
    let prev = lookup.tx(&spent.txid).await.map_err(|e| unresolved(&e))?;
    let address = prev
        .vout
        .get(spent.vout as usize)
        .and_then(|o| o.scriptpubkey_address.clone())
        .ok_or_else(|| unresolved(&refusal))?;
    let utxos = lookup
        .address_utxos(&address)
        .await
        .map_err(|e| unresolved(&e))?;
    if utxos
        .iter()
        .any(|u| u.txid == spent.txid && u.vout == spent.vout)
    {
        Ok(false)
    } else {
        Err(unresolved(&refusal))
    }
}

/// An intent is only acted on if its signed transaction really is the one it
/// names and, when it would move the stored trail forward (`behind`), spends
/// the trail's stored head. A tampered or mismatched record is refused rather
/// than written over the trail.
fn verify_intent_tx(
    txid: &str,
    raw_hex: &str,
    head: Option<(&str, u32)>,
    behind: bool,
) -> Result<(), ProvenanceError> {
    let bad =
        |why: String| ProvenanceError::Anchor(format!("pending intent {txid} refused: {why}"));
    let info = solid_pod_rs::bitcoin_tx::decode_tx_info(raw_hex)
        .map_err(|e| bad(format!("undecodable transaction: {e}")))?;
    if info.txid != txid {
        return Err(bad(format!("its transaction is {}", info.txid)));
    }
    if let (true, Some((head_txid, head_vout))) = (behind, head) {
        let spends_head = info
            .vin
            .first()
            .is_some_and(|i| i.txid == head_txid && i.vout == head_vout);
        if !spends_head {
            return Err(bad("it does not spend the stored head".into()));
        }
    }
    Ok(())
}

/// A git-mark intent as stored: [`GitmarkIntent`](crate::trail_store::GitmarkIntent)
/// plus the signed transaction, so an unresolved mark can be sent again.
/// Intents written before `rawHex` existed read back with `None`.
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PendingMark {
    #[serde(flatten)]
    intent: crate::trail_store::GitmarkIntent,
    #[serde(default)]
    raw_hex: Option<String>,
}

/// Read the pending mark of git-mark trail `name`, if any.
async fn load_pending_mark(
    storage: &std::sync::Arc<dyn solid_pod_rs::storage::Storage>,
    name: &str,
) -> Result<Option<PendingMark>, ProvenanceError> {
    use solid_pod_rs::PodError;
    match storage
        .get(&crate::trail_store::gitmark_intent_path(name))
        .await
    {
        Ok((bytes, _)) => serde_json::from_slice(&bytes).map(Some).map_err(|e| {
            ProvenanceError::Anchor(format!("read pending mark: malformed git-mark intent: {e}"))
        }),
        Err(PodError::NotFound(_)) => Ok(None),
        Err(e) => Err(ProvenanceError::Anchor(format!("read pending mark: {e}"))),
    }
}

/// Write the pending mark of a git-mark trail.
async fn save_pending_mark(
    storage: &std::sync::Arc<dyn solid_pod_rs::storage::Storage>,
    pending: &PendingMark,
) -> Result<(), String> {
    let body = serde_json::to_vec(pending).map_err(|e| e.to_string())?;
    storage
        .put(
            &crate::trail_store::gitmark_intent_path(&pending.intent.stored.name),
            bytes::Bytes::from(body),
            "application/json",
        )
        .await
        .map(|_| ())
        .map_err(|e| e.to_string())
}

/// An MRC20 anchor about to be broadcast: the signed transaction and the
/// trail as it stands once that transaction is in.
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PendingAnchor {
    txid: String,
    raw_hex: String,
    stored: crate::trail_store::StoredTrail,
}

/// Pod-storage path of an MRC20 trail's pending anchor. It holds the trail's
/// secret, so it sits under `/.well-known/token/` beside the trail file.
fn anchor_intent_path(ticker: &str) -> String {
    format!("/.well-known/token/{}/intent.json", ticker.to_lowercase())
}

/// Settle an interrupted advance of the MRC20 trail `ticker`: the previous
/// anchor wrote its intent and did not clear it. The transaction is looked up
/// and, if the backend does not know it, sent again; once it is in, the
/// intent's trail becomes the stored one (unless a later write already did).
/// Caller holds the trail's [`lock_path`](crate::trail_store::lock_path).
///
/// # Errors
///
/// [`ProvenanceError::Anchor`] when the pending anchor cannot be resolved; the
/// intent is kept and no advance may be built on the old head.
pub(crate) async fn settle_trail_intent<M: MempoolLookup + MempoolBroadcast + ?Sized>(
    lookup: &M,
    storage: &std::sync::Arc<dyn solid_pod_rs::storage::Storage>,
    ticker: &str,
) -> Result<(), ProvenanceError> {
    use crate::trail_store::{load_trail, save_trail};
    use solid_pod_rs::PodError;
    let err =
        |what: &str, e: &dyn std::fmt::Display| ProvenanceError::Anchor(format!("{what}: {e}"));
    let path = anchor_intent_path(ticker);
    let pending: PendingAnchor = match storage.get(&path).await {
        Ok((bytes, _)) => {
            serde_json::from_slice(&bytes).map_err(|e| err("malformed pending anchor", &e))?
        }
        Err(PodError::NotFound(_)) => return Ok(()),
        Err(e) => return Err(err("read pending anchor", &e)),
    };
    let stored = load_trail(storage, ticker)
        .await
        .map_err(|e| err("load trail", &e))?;
    verify_intent_tx(
        &pending.txid,
        &pending.raw_hex,
        stored
            .as_ref()
            .map(|t| (t.current_txid.as_str(), t.current_vout)),
        stored.as_ref().map_or(0, |t| t.states.len()) < pending.stored.states.len(),
    )?;
    let landed = lookup.tx(&pending.txid).await.is_ok()
        || settle_broadcast(
            lookup,
            &pending.txid,
            &pending.raw_hex,
            stored
                .as_ref()
                .map(|t| (t.current_txid.as_str(), t.current_vout)),
        )
        .await?;
    if landed && stored.map_or(0, |t| t.states.len()) < pending.stored.states.len() {
        save_trail(storage, &pending.stored)
            .await
            .map_err(|e| err("save recovered trail", &e))?;
    }
    match storage.delete(&path).await {
        Ok(()) | Err(PodError::NotFound(_)) => Ok(()),
        Err(e) => Err(err("clear pending anchor", &e)),
    }
}

// ---------------------------------------------------------------------------
// GitmarkAnchorer — new trails: the git-mark profile
// ---------------------------------------------------------------------------

/// Whether the mempool knows a transaction, with a definite "no" kept apart
/// from a transport failure ([`MempoolLookup::tx`] cannot tell them apart).
/// [`GitmarkAnchorer`] resolves an interrupted broadcast with it.
#[async_trait(?Send)]
pub trait MempoolTxPresence {
    /// `Ok(true)` when `txid` is known, `Ok(false)` only on a definite
    /// not-found, `Err` for anything ambiguous.
    async fn transaction_exists(&self, txid: &str) -> Result<bool, PaymentError>;
}

#[async_trait(?Send)]
impl MempoolTxPresence for MempoolHttpClient {
    async fn transaction_exists(&self, txid: &str) -> Result<bool, PaymentError> {
        MempoolHttpClient::transaction_exists(self, txid).await
    }
}

/// A [`BlockAnchorer`] over a git-mark trail: each anchored state is the
/// commit hash itself, as text (blocktrails/git-mark b852d7d), with no MRC20
/// wrapper.
///
/// This is the profile new trails use. The MRC20 trails a pod has already
/// issued keep [`MempoolBlockAnchorer`] and its `urn:mono:op:anchor` states:
/// their marks are on-chain and their keys follow from those states, so they
/// are never moved to another profile.
///
/// - [`genesis`](Self::genesis) starts a trail: a funding voucher is spent to
///   the first mark, the voucher key's point tweaked by the first commit; that
///   key is the trail's base key.
/// - `anchor` spends the newest mark to the next, tweaked by the commit
///   ([`gitmark_advance`]), broadcasts it and saves the trail.
/// - `verify` walks the trail back from the anchor's outpoint, as
///   [`MempoolBlockAnchorer`] does.
///
/// The trail is kept at `/.well-known/gitmark/{name}.json` with its base
/// secret (as MRC20 trails keep theirs, see [`crate::trail_store`]), and its
/// public `blocktrails.json` (no secret) is written beside it at
/// `/.well-known/gitmark/{name}/blocktrails.json`, where a verifier such as
/// blocktrails/verify reads it.
#[derive(Clone)]
pub struct GitmarkAnchorer<M: MempoolLookup + MempoolBroadcast + MempoolTxPresence + Send + Sync> {
    lookup: M,
    storage: Option<std::sync::Arc<dyn solid_pod_rs::storage::Storage>>,
}

impl<M: MempoolLookup + MempoolBroadcast + MempoolTxPresence + Send + Sync> GitmarkAnchorer<M> {
    /// Wrap a transport as a **verify-capable** anchorer; `anchor()` and
    /// [`genesis`](Self::genesis) need [`with_storage`](Self::with_storage).
    pub fn new(lookup: M) -> Self {
        Self {
            lookup,
            storage: None,
        }
    }

    /// Wrap a transport and pod storage as a fully-capable anchorer.
    pub fn with_storage(
        lookup: M,
        storage: std::sync::Arc<dyn solid_pod_rs::storage::Storage>,
    ) -> Self {
        Self {
            lookup,
            storage: Some(storage),
        }
    }

    /// Borrow the underlying transport.
    pub fn lookup(&self) -> &M {
        &self.lookup
    }

    fn storage(
        &self,
    ) -> Result<&std::sync::Arc<dyn solid_pod_rs::storage::Storage>, ProvenanceError> {
        self.storage.as_ref().ok_or_else(|| {
            ProvenanceError::Anchor(
                "a git-mark trail needs storage; construct with GitmarkAnchorer::with_storage"
                    .into(),
            )
        })
    }

    /// Settle an interrupted advance of trail `name`: the previous call wrote
    /// its intent (the txid it would broadcast and the trail with it) and did
    /// not get to clear it. Caller holds the trail lock.
    ///
    /// The transaction on-chain ⇒ the intent's trail becomes the stored one
    /// (unless a later advance already did); definitely not on-chain ⇒ the
    /// intent is dropped; ambiguous ⇒ the advance is refused (fail closed).
    async fn settle_intent(&self, name: &str) -> Result<(), ProvenanceError> {
        use crate::trail_store::{clear_gitmark_intent, load_gitmark_trail, save_gitmark_trail};
        let storage = self.storage()?;
        let err = |what: &str, e: PaymentError| ProvenanceError::Anchor(format!("{what}: {e}"));
        let Some(pending) = load_pending_mark(storage, name).await? else {
            return Ok(());
        };
        let intent = &pending.intent;
        let exists = self
            .lookup
            .transaction_exists(&intent.txid)
            .await
            .map_err(|e| {
                ProvenanceError::Anchor(format!(
                    "a previous mark ({}) is unresolved, refusing to advance: {e}",
                    intent.txid
                ))
            })?;
        // "Not found" is not "not broadcast": a backend that took the mark
        // but has not indexed it says the same. Send it again; the intent is
        // dropped only for a mark that predates `rawHex` and cannot be.
        let stored = load_gitmark_trail(storage, name)
            .await
            .map_err(|e| err("load trail", e))?;
        if let Some(raw_hex) = &pending.raw_hex {
            verify_intent_tx(
                &intent.txid,
                raw_hex,
                stored
                    .as_ref()
                    .and_then(|t| t.trail.txo.last())
                    .map(|m| (m.txid.as_str(), m.vout)),
                stored.as_ref().map_or(0, |t| t.trail.txo.len()) < intent.stored.trail.txo.len(),
            )?;
        }
        let landed = if exists {
            true
        } else if let Some(raw_hex) = &pending.raw_hex {
            let head = stored
                .as_ref()
                .and_then(|t| t.trail.txo.last())
                .map(|m| (m.txid.as_str(), m.vout));
            settle_broadcast(&self.lookup, &intent.txid, raw_hex, head).await?
        } else {
            tracing::warn!(trail = %name, txid = %intent.txid, "pending mark has no raw transaction; dropped as not on-chain");
            false
        };
        if landed && stored.map_or(0, |t| t.trail.txo.len()) < intent.stored.trail.txo.len() {
            save_gitmark_trail(storage, &intent.stored)
                .await
                .map_err(|e| err("save recovered trail", e))?;
        }
        clear_gitmark_intent(storage, name)
            .await
            .map_err(|e| err("clear pending mark", e))
    }

    /// Write the intent, broadcast, save the trail, clear the intent. Caller
    /// holds the trail lock and has settled any earlier intent.
    ///
    /// Once the broadcast is accepted the mark is on-chain, so a failed save
    /// is not a failed anchor: the intent stays and the next advance settles
    /// it. The caller must not be refunded for it.
    async fn broadcast_and_save(
        &self,
        name: &str,
        privkey: String,
        update: GitmarkUpdate,
        network: &str,
        date_created: String,
    ) -> Result<BlockTrailAnchor, ProvenanceError> {
        use crate::trail_store::{
            clear_gitmark_intent, save_gitmark_trail, GitmarkIntent, StoredGitmarkTrail,
        };
        let storage = self.storage()?;
        let expected = solid_pod_rs::bitcoin_tx::decode_tx_info(&update.tx.raw_hex)
            .map_err(|e| ProvenanceError::Anchor(format!("decode mark: {e}")))?
            .txid;
        let mut trail = update.trail;
        if let Some(last) = trail.txo.last_mut() {
            last.txid = expected.clone();
        }
        let state_strings = trail
            .state_strings()
            .map_err(|e| ProvenanceError::Anchor(format!("trail states: {e}")))?;
        let pubkey = trail.pubkey_base.clone();
        let stored = StoredGitmarkTrail {
            name: name.to_string(),
            privkey,
            trail,
            date_created,
        };
        save_pending_mark(
            storage,
            &PendingMark {
                intent: GitmarkIntent {
                    txid: expected.clone(),
                    stored: stored.clone(),
                },
                raw_hex: Some(update.tx.raw_hex.clone()),
            },
        )
        .await
        .map_err(|e| ProvenanceError::Anchor(format!("record pending mark: {e}")))?;

        let txid = self
            .lookup
            .broadcast_tx(&update.tx.raw_hex)
            .await
            .map_err(|e| pending_anchor(format!("broadcast mark: {e}")))?;
        if txid != expected {
            return Err(pending_anchor(format!(
                "mempool returned txid {txid} for a mark that is {expected}"
            )));
        }
        match save_gitmark_trail(storage, &stored).await {
            Ok(()) => {
                if let Err(e) = clear_gitmark_intent(storage, name).await {
                    // Settling it later is a no-op: the trail is already saved.
                    tracing::warn!(trail = %name, "git-mark intent not cleared: {e}");
                }
            }
            Err(e) => tracing::error!(
                trail = %name, txid = %txid,
                "git-mark broadcast accepted but trail save failed; the next advance settles it: {e}"
            ),
        }
        Ok(BlockTrailAnchor {
            ticker: name.to_string(),
            state_hash: update.txo.commit.clone().unwrap_or_default(),
            txid,
            vout: update.txo.vout,
            address: update.address,
            network: network.to_string(),
            blockheight: None,
            state_strings,
            pubkey,
        })
    }

    /// Start the git-mark trail `name`: spend `voucher` to its first mark,
    /// committing to `commit` ([`gitmark_genesis`]), broadcast, and save the
    /// trail with the voucher key as its base secret.
    ///
    /// # Errors
    ///
    /// [`ProvenanceError::Anchor`] when there is no storage, the trail already
    /// exists (a trail is never restarted over one that has marks), or the
    /// build, broadcast or save fails.
    pub async fn genesis(
        &self,
        name: &str,
        voucher: &TxoVoucher,
        commit: &str,
        network: &str,
        date_created: &str,
    ) -> Result<BlockTrailAnchor, ProvenanceError> {
        use crate::trail_store::{gitmark_trail_path, load_gitmark_trail};
        let storage = self.storage()?;
        let _guard = crate::trail_store::lock_path(&gitmark_trail_path(name)).await;
        self.settle_intent(name).await?;
        if load_gitmark_trail(storage, name)
            .await
            .map_err(|e| ProvenanceError::Anchor(format!("load trail {name}: {e}")))?
            .is_some()
        {
            return Err(ProvenanceError::Anchor(format!(
                "git-mark trail {name} already exists"
            )));
        }
        let update = gitmark_genesis(voucher, commit, network, DEFAULT_FEE_SATS, &self.lookup)
            .await
            .map_err(|e| ProvenanceError::Anchor(format!("build genesis mark: {e}")))?;
        self.broadcast_and_save(
            name,
            voucher.privkey.clone(),
            update,
            network,
            date_created.to_string(),
        )
        .await
    }

    /// The per-link report behind [`BlockAnchorer::verify`] (see
    /// [`verify_anchor_report`]).
    pub async fn verify_report(
        &self,
        anchor: &BlockTrailAnchor,
    ) -> Result<Option<TrailReport>, ProvenanceError> {
        verify_anchor_report(&self.lookup, anchor).await
    }
}

#[async_trait(?Send)]
impl<M: MempoolLookup + MempoolBroadcast + MempoolTxPresence + Send + Sync> BlockAnchorer
    for GitmarkAnchorer<M>
{
    /// Advance the git-mark trail `ticker` by the commit `state_hash`: spend
    /// its newest mark to the next ([`gitmark_advance`]), broadcast, save, and
    /// return the new mark as a [`BlockTrailAnchor`] (`state_strings` are the
    /// commits so far, `pubkey` the trail's base key).
    ///
    /// `network` must be the trail's own (its chain token read as a network
    /// name, so `testnet4` for a `tbtc4` trail).
    async fn anchor(
        &self,
        ticker: &str,
        state_hash: &str,
        network: &str,
    ) -> Result<BlockTrailAnchor, ProvenanceError> {
        use crate::trail_store::{gitmark_trail_path, load_gitmark_trail};
        let _guard = crate::trail_store::lock_path(&gitmark_trail_path(ticker)).await;
        self.settle_intent(ticker).await?;
        let stored = load_gitmark_trail(self.storage()?, ticker)
            .await
            .map_err(|e| ProvenanceError::Anchor(format!("load trail {ticker}: {e}")))?
            .ok_or_else(|| {
                ProvenanceError::Anchor(format!(
                    "git-mark trail {ticker} has no genesis on this pod"
                ))
            })?;
        let trail_network = stored.network();
        if trail_network != network {
            return Err(ProvenanceError::Anchor(format!(
                "network mismatch: trail is {trail_network}, requested {network}"
            )));
        }
        let update = gitmark_advance(
            &stored.trail,
            &stored.privkey,
            state_hash,
            DEFAULT_FEE_SATS,
            &self.lookup,
        )
        .await
        .map_err(|e| ProvenanceError::Anchor(format!("build mark: {e}")))?;
        self.broadcast_and_save(ticker, stored.privkey, update, network, stored.date_created)
            .await
    }

    async fn verify(&self, anchor: &BlockTrailAnchor) -> Result<bool, ProvenanceError> {
        Ok(verify_anchor_report(&self.lookup, anchor)
            .await?
            .is_some_and(|r| r.is_intact()))
    }
}

// ---------------------------------------------------------------------------
// Tests — fixture parsing only (NO live mempool.space access).
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    /// A captured mempool.space `GET /api/address/{addr}/utxo` payload:
    /// one confirmed UTXO. Deserialises into the flat [`Utxo`].
    const UTXO_JSON: &str = include_str!("../tests/fixtures/mempool/address_utxos.json");
    /// A captured `GET /api/tx/{txid}` payload (one confirmed tx, 2 outputs).
    const TX_JSON: &str = include_str!("../tests/fixtures/mempool/tx.json");
    /// An empty UTXO set (`[]`) — the "no deposit yet" response.
    const EMPTY_UTXO_JSON: &str = "[]";

    #[test]
    fn utxo_wire_flattens_status() {
        let wire: Vec<UtxoWire> = serde_json::from_str(UTXO_JSON).unwrap();
        let utxos: Vec<Utxo> = wire.into_iter().map(Utxo::from).collect();
        assert_eq!(utxos.len(), 1);
        assert_eq!(utxos[0].vout, 0);
        assert_eq!(utxos[0].value, 9700);
        assert!(
            utxos[0].confirmed,
            "status.confirmed must flatten onto Utxo"
        );
        assert_eq!(utxos[0].block_height, Some(42_000));
    }

    #[test]
    fn empty_utxo_set_parses_to_empty_vec() {
        let wire: Vec<UtxoWire> = serde_json::from_str(EMPTY_UTXO_JSON).unwrap();
        assert!(wire.is_empty());
    }

    #[test]
    fn tx_wire_flattens_outputs_and_status() {
        let wire: TxWire = serde_json::from_str(TX_JSON).unwrap();
        let tx = TxInfo::from(wire);
        assert_eq!(tx.vout.len(), 2);
        assert_eq!(tx.vout[0].value, 9700);
        assert_eq!(
            tx.vout[0].scriptpubkey.as_deref(),
            Some("5120aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899")
        );
        assert_eq!(
            tx.vout[0].scriptpubkey_address.as_deref(),
            Some("tb1pexampleaddress")
        );
        assert!(tx.confirmed);
        assert_eq!(tx.block_height, Some(42_000));
    }

    #[test]
    fn from_env_defaults_to_testnet4() {
        // Snapshot/restore so a parallel test or the host env can't perturb it.
        let prev = std::env::var(MEMPOOL_URL_ENV).ok();
        std::env::remove_var(MEMPOOL_URL_ENV);
        let c = MempoolHttpClient::from_env();
        assert_eq!(c.base_url(), DEFAULT_MEMPOOL_URL);
        if let Some(v) = prev {
            std::env::set_var(MEMPOOL_URL_ENV, v);
        }
    }

    #[test]
    fn new_trims_trailing_slash() {
        let c = MempoolHttpClient::new("https://mempool.space/testnet4/");
        assert_eq!(c.base_url(), "https://mempool.space/testnet4");
    }

    /// Live smoke test — disabled by default (no live chain in CI). Run with
    /// `cargo test -p solid-pod-rs-server --features git -- --ignored live_`.
    #[ignore = "hits live mempool.space; opt-in only"]
    #[tokio::test]
    async fn live_address_utxos_smoke() {
        let c = MempoolHttpClient::from_env();
        // A well-known testnet4 faucet-ish address may have UTXOs; the test
        // only asserts the call shape succeeds (empty is acceptable).
        let _ = c
            .address_utxos("tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c")
            .await;
    }

    // ── BlockAnchorer::verify over a FIXTURE MempoolLookup (no network) ──

    use std::collections::HashMap;

    /// In-memory [`MempoolLookup`] + [`MempoolBroadcast`]: address→UTXO and
    /// txid→transaction maps, no HTTP. A broadcast is decoded and kept under
    /// its real txid (confirmed), so the per-link walk can follow every mark
    /// back to the one it spends. Interior mutability so clones share state.
    #[derive(Clone, Default)]
    struct FixtureMempool {
        utxos: std::sync::Arc<std::sync::Mutex<HashMap<String, Vec<Utxo>>>>,
        txs: std::sync::Arc<std::sync::Mutex<HashMap<String, TxInfo>>>,
        broadcasts: std::sync::Arc<std::sync::Mutex<Vec<String>>>,
    }
    impl FixtureMempool {
        fn empty() -> Self {
            Self::default()
        }
        /// Register an output by hand (a funding voucher).
        fn add_output(&self, txid: &str, vout: u32, spk_hex: &str) {
            let mut txs = self.txs.lock().unwrap();
            let tx = txs.entry(txid.to_string()).or_insert_with(|| TxInfo {
                txid: txid.to_string(),
                vin: vec![],
                vout: vec![],
                confirmed: true,
                block_height: Some(41_000),
            });
            while tx.vout.len() <= vout as usize {
                tx.vout.push(TxOut {
                    value: 0,
                    scriptpubkey: None,
                    scriptpubkey_address: None,
                });
            }
            tx.vout[vout as usize].scriptpubkey = Some(spk_hex.to_string());
        }
        /// A whole trail on-chain for `state_strings`: one transaction per
        /// state, each paying the key its prefix derives and spending the one
        /// before. Returns the head txid.
        fn add_chain(&self, pubkey: &str, state_strings: &[String]) -> String {
            let outputs = solid_pod_rs::mrc20::bt_trail_outputs(pubkey, state_strings).unwrap();
            let mut prev = "ff".repeat(32);
            for (i, x) in outputs.iter().enumerate() {
                let txid = solid_pod_rs::mrc20::sha256_hex(&format!("{pubkey} mark {i}"));
                self.txs.lock().unwrap().insert(
                    txid.clone(),
                    TxInfo {
                        txid: txid.clone(),
                        vin: vec![TxIn {
                            txid: prev,
                            vout: 0,
                        }],
                        vout: vec![TxOut {
                            value: 9_700,
                            scriptpubkey: Some(format!("5120{}", hex::encode(x))),
                            scriptpubkey_address: None,
                        }],
                        confirmed: true,
                        block_height: Some(42_000 + i as u64),
                    },
                );
                prev = txid;
            }
            prev
        }
    }
    #[async_trait(?Send)]
    impl MempoolLookup for FixtureMempool {
        async fn address_utxos(&self, address: &str) -> Result<Vec<Utxo>, PaymentError> {
            Ok(self
                .utxos
                .lock()
                .unwrap()
                .get(address)
                .cloned()
                .unwrap_or_default())
        }
        async fn tx(&self, txid: &str) -> Result<TxInfo, PaymentError> {
            self.txs
                .lock()
                .unwrap()
                .get(txid)
                .cloned()
                .ok_or_else(|| PaymentError::InvalidState(format!("tx {txid} not found")))
        }
    }
    #[async_trait(?Send)]
    impl MempoolBroadcast for FixtureMempool {
        async fn broadcast_tx(&self, raw_hex: &str) -> Result<String, PaymentError> {
            let mut info = solid_pod_rs::bitcoin_tx::decode_tx_info(raw_hex)?;
            info.confirmed = true;
            info.block_height = Some(43_000);
            let txid = info.txid.clone();
            self.txs.lock().unwrap().insert(txid.clone(), info);
            self.broadcasts.lock().unwrap().push(raw_hex.to_string());
            Ok(txid)
        }
    }

    #[async_trait(?Send)]
    impl MempoolTxPresence for FixtureMempool {
        async fn transaction_exists(&self, txid: &str) -> Result<bool, PaymentError> {
            Ok(self.txs.lock().unwrap().contains_key(txid))
        }
    }

    // Issuer keypair (arbitrary) for deriving real anchor addresses.
    const ISSUER_PRIVKEY: &str = "0000000000000000000000000000000000000000000000000000000000000001";
    fn issuer_pubkey() -> String {
        let sk = k256::SecretKey::from_slice(&hex::decode(ISSUER_PRIVKEY).unwrap()).unwrap();
        hex::encode(sk.public_key().to_sec1_bytes())
    }

    /// Build a `BlockTrailAnchor` whose `address`/`state_strings`/`pubkey`
    /// are internally consistent (the `address` is the genuine derivation),
    /// with its head at `txid`.
    fn consistent_anchor(txid: &str) -> BlockTrailAnchor {
        let pubkey = issuer_pubkey();
        let state_strings = vec!["{\"seq\":0}".to_string(), "{\"seq\":1}".to_string()];
        let address = bt_address(&pubkey, &state_strings, "testnet4").unwrap();
        BlockTrailAnchor {
            ticker: "PROV".into(),
            state_hash: "ff".repeat(32),
            txid: txid.to_string(),
            vout: 0,
            address,
            network: "testnet4".into(),
            blockheight: Some(42_000),
            state_strings,
            pubkey: Some(pubkey),
        }
    }

    /// A fixture holding the whole trail of [`consistent_anchor`], and the
    /// anchor at its head.
    fn anchored_chain() -> (FixtureMempool, BlockTrailAnchor) {
        let mempool = FixtureMempool::empty();
        let probe = consistent_anchor("");
        let head = mempool.add_chain(&issuer_pubkey(), &probe.state_strings);
        (mempool, consistent_anchor(&head))
    }

    #[test]
    fn tx_wire_reads_inputs() {
        let wire: TxWire = serde_json::from_str(
            r#"{"txid":"aa","vin":[{"txid":"bb","vout":3,"prevout":{}},{"is_coinbase":true}],"vout":[],"status":{"confirmed":false}}"#,
        )
        .unwrap();
        let tx = TxInfo::from(wire);
        assert_eq!(tx.vin.len(), 2);
        assert_eq!((tx.vin[0].txid.as_str(), tx.vin[0].vout), ("bb", 3));
        assert!(!tx.confirmed);
    }

    #[tokio::test]
    async fn block_anchorer_verify_true_when_every_link_is_on_chain() {
        let (mempool, anchor) = anchored_chain();
        let anchorer = MempoolBlockAnchorer::new(mempool);
        assert!(anchorer.verify(&anchor).await.unwrap());
        let report = anchorer.verify_report(&anchor).await.unwrap().unwrap();
        assert!(report.is_verified());
        assert_eq!(report.marks.len(), 2);
    }

    #[tokio::test]
    async fn block_anchorer_verify_false_when_the_anchor_tx_is_absent() {
        let anchor = consistent_anchor(&"ab".repeat(32));
        let anchorer = MempoolBlockAnchorer::new(FixtureMempool::empty());
        assert!(
            !anchorer.verify(&anchor).await.unwrap(),
            "no transaction behind the anchor ⇒ verify false"
        );
    }

    #[tokio::test]
    async fn block_anchorer_verify_false_when_an_earlier_mark_does_not_commit() {
        // The head is the right key, but the genesis mark behind it is not:
        // the head-only check accepted this; the walk does not.
        let (mempool, anchor) = anchored_chain();
        let genesis = solid_pod_rs::mrc20::sha256_hex(&format!("{} mark 0", issuer_pubkey()));
        mempool.txs.lock().unwrap().get_mut(&genesis).unwrap().vout[0].scriptpubkey =
            Some(format!("5120{}", "22".repeat(32)));
        let anchorer = MempoolBlockAnchorer::new(mempool);
        assert!(!anchorer.verify(&anchor).await.unwrap());
        let report = anchorer.verify_report(&anchor).await.unwrap().unwrap();
        assert_eq!(
            report.marks[0].status,
            solid_pod_rs::blocktrail::MarkStatus::WrongKey
        );
    }

    #[tokio::test]
    async fn block_anchorer_verify_false_when_address_forged() {
        // The chain is real, but the anchor *claims* a different (forged)
        // address → the re-derivation mismatch fails it.
        let (mempool, mut anchor) = anchored_chain();
        anchor.address = "tb1pforged000000000000000000000000000000".into();
        let anchorer = MempoolBlockAnchorer::new(mempool);
        assert!(
            !anchorer.verify(&anchor).await.unwrap(),
            "forged address must not verify even with a real chain behind it"
        );
        assert!(anchorer.verify_report(&anchor).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn block_anchorer_verify_false_without_pubkey() {
        // No pubkey ⇒ nothing to re-derive against ⇒ not verifiable.
        let (mempool, mut anchor) = anchored_chain();
        anchor.pubkey = None;
        let anchorer = MempoolBlockAnchorer::new(mempool);
        assert!(!anchorer.verify(&anchor).await.unwrap());
    }

    #[tokio::test]
    async fn block_anchorer_anchor_requires_storage() {
        // The verify-only constructor leaves storage None ⇒ anchor() errors
        // with a clear message rather than panicking.
        let anchorer = MempoolBlockAnchorer::new(FixtureMempool::empty());
        let err = anchorer
            .anchor("PROV", "deadbeef", "testnet4")
            .await
            .unwrap_err();
        match err {
            ProvenanceError::Anchor(m) => assert!(m.contains("with_storage")),
            other => panic!("expected Anchor(requires storage), got {other:?}"),
        }
    }

    // ── Phase 4: full anchor() round-trip (mint → store → anchor → verify) ──

    use crate::trail_store::{load_gitmark_trail, load_trail, save_trail, StoredTrail};
    use solid_pod_rs::bitcoin_tx::mint_token;
    use solid_pod_rs::storage::memory::MemoryBackend;
    use solid_pod_rs::storage::Storage;

    /// The issuer key's own (untweaked) output: a voucher it can spend.
    fn issuer_voucher(mempool: &FixtureMempool, txid: &str) -> TxoVoucher {
        let sk = k256::SecretKey::from_slice(&hex::decode(ISSUER_PRIVKEY).unwrap()).unwrap();
        let compressed = sk.public_key().to_sec1_bytes();
        mempool.add_output(txid, 0, &format!("5120{}", hex::encode(&compressed[1..])));
        TxoVoucher {
            txid: txid.to_string(),
            vout: 0,
            amount: 100_000,
            privkey: ISSUER_PRIVKEY.to_string(),
        }
    }

    /// Mint a genesis trail through the write-side, broadcast it, and persist
    /// it (with the issuer secret). Returns `(storage, mempool, ticker)`.
    async fn mint_and_store(ticker: &str) -> (std::sync::Arc<dyn Storage>, FixtureMempool, String) {
        let mempool = FixtureMempool::empty();
        let storage: std::sync::Arc<dyn Storage> = std::sync::Arc::new(MemoryBackend::new());
        let voucher = issuer_voucher(&mempool, &"11".repeat(32));
        let mint = mint_token(ticker, None, 1_000, &voucher, "testnet4", 300, &mempool)
            .await
            .unwrap();
        let mint_txid = mempool.broadcast_tx(&mint.tx.raw_hex).await.unwrap();

        // Persist the trail with the issuer secret + the broadcast txid.
        let stored = StoredTrail {
            ticker: mint.trail.ticker.clone(),
            name: mint.trail.name.clone(),
            supply: mint.trail.supply,
            privkey: ISSUER_PRIVKEY.to_string(),
            pubkey_base: mint.trail.pubkey_base.clone(),
            states: mint.trail.states.clone(),
            state_strings: mint.trail.state_strings.clone(),
            current_txid: mint_txid,
            current_vout: 0,
            current_amount: mint.trail.current_amount,
            network: mint.trail.network.clone(),
            date_created: "2026-06-13T00:00:00Z".into(),
        };
        save_trail(&storage, &stored).await.unwrap();
        (storage, mempool, ticker.to_string())
    }

    #[tokio::test]
    async fn block_anchorer_anchor_round_trip_and_self_verifies() {
        let (storage, mempool, ticker) = mint_and_store("ANCH").await;
        let anchorer = MempoolBlockAnchorer::with_storage(mempool.clone(), storage.clone());

        // Anchor a git commit SHA (the provenance write).
        let commit_sha = "a1b2c3d4e5f60718293a4b5c6d7e8f9001122334";
        let anchor = anchorer
            .anchor(&ticker, commit_sha, "testnet4")
            .await
            .expect("anchor() must build + broadcast + persist");

        assert_eq!(anchor.ticker, "ANCH");
        assert_eq!(anchor.state_hash, commit_sha);
        assert_eq!(anchor.vout, 0);
        assert!(anchor.blockheight.is_none());
        assert_eq!(anchor.network, "testnet4");
        assert!(anchor.pubkey.is_some());
        // The portable proof carries genesis + anchor state strings.
        assert_eq!(anchor.state_strings.len(), 2);
        // The recorded address is the genuine derivation from the proof.
        let derived = bt_address(
            anchor.pubkey.as_deref().unwrap(),
            &anchor.state_strings,
            "testnet4",
        )
        .unwrap();
        assert_eq!(anchor.address, derived);

        // The trail was persisted with the new state appended + new txid; the
        // issued trail keeps its MRC20 anchor state.
        let reloaded = load_trail(&storage, "ANCH").await.unwrap().unwrap();
        assert_eq!(reloaded.states.len(), 2);
        assert_eq!(reloaded.current_txid, anchor.txid);
        assert_eq!(reloaded.states[1].anchor.as_deref(), Some(commit_sha));
        assert_eq!(reloaded.states[1].ops[0].op, "urn:mono:op:anchor");

        // verify() walks the produced anchor back to the genesis mark.
        assert!(
            anchorer.verify(&anchor).await.unwrap(),
            "the anchor we just produced must verify, every link"
        );

        // A second anchor spends the first; the first still verifies (the
        // head-only check needed it to be unspent).
        let second = anchorer
            .anchor(&ticker, &"b".repeat(40), "testnet4")
            .await
            .unwrap();
        assert_eq!(second.state_strings.len(), 3);
        assert!(anchorer.verify(&second).await.unwrap());
        assert!(anchorer.verify(&anchor).await.unwrap());
    }

    #[tokio::test]
    async fn block_anchorer_anchor_rejects_unminted_ticker() {
        let storage: std::sync::Arc<dyn Storage> = std::sync::Arc::new(MemoryBackend::new());
        let anchorer = MempoolBlockAnchorer::with_storage(FixtureMempool::empty(), storage);
        let err = anchorer
            .anchor("GHOST", "deadbeef", "testnet4")
            .await
            .unwrap_err();
        match err {
            ProvenanceError::Anchor(m) => assert!(m.contains("not minted")),
            other => panic!("expected not-minted error, got {other:?}"),
        }
    }

    // ── GitmarkAnchorer: new trails, the commit as the state ──

    const COMMITS: [&str; 3] = [
        "0123456789abcdef0123456789abcdef01234567",
        "cf97baba489e88c1ffbe6758c0fe8c18ff83d17d",
        "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
    ];

    #[tokio::test]
    async fn gitmark_anchorer_genesis_anchor_verify() {
        let mempool = FixtureMempool::empty();
        let storage: std::sync::Arc<dyn Storage> = std::sync::Arc::new(MemoryBackend::new());
        let anchorer = GitmarkAnchorer::with_storage(mempool.clone(), storage.clone());
        let voucher = issuer_voucher(&mempool, &"33".repeat(32));

        let genesis = anchorer
            .genesis(
                "prov",
                &voucher,
                COMMITS[0],
                "testnet4",
                "2026-10-02T00:00:00Z",
            )
            .await
            .unwrap();
        assert_eq!(genesis.state_strings, vec![COMMITS[0].to_string()]);
        assert_eq!(genesis.pubkey.as_deref(), Some(issuer_pubkey().as_str()));
        assert!(anchorer.verify(&genesis).await.unwrap());
        // never restarted over a trail that has marks
        let again = anchorer
            .genesis("prov", &voucher, COMMITS[0], "testnet4", "")
            .await
            .unwrap_err();
        assert!(again.to_string().contains("already exists"));

        let mut last = genesis.clone();
        for commit in &COMMITS[1..] {
            last = anchorer.anchor("prov", commit, "testnet4").await.unwrap();
            assert_eq!(last.state_hash, *commit);
        }
        assert_eq!(
            last.state_strings,
            COMMITS.iter().map(|c| c.to_string()).collect::<Vec<_>>()
        );
        let report = anchorer.verify_report(&last).await.unwrap().unwrap();
        assert!(report.is_verified(), "{report:?}");
        assert_eq!(report.marks.len(), 3);
        // older marks stay verifiable after later ones spend them
        assert!(anchorer.verify(&genesis).await.unwrap());

        // the stored trail is the §5.2 shape, and its published copy verifies
        // with blocktrails/verify's walk
        let stored = load_gitmark_trail(&storage, "prov").await.unwrap().unwrap();
        assert_eq!(stored.trail.txo.len(), 3);
        assert_eq!(stored.trail.txo[2].txid, last.txid);
        let report = solid_pod_rs::blocktrail::verify_blocktrail(&stored.trail, &mempool).await;
        assert!(report.is_verified(), "{report:?}");
        let (published, _) = storage
            .get(&crate::trail_store::gitmark_blocktrails_path("prov"))
            .await
            .unwrap();
        let published: solid_pod_rs::blocktrail::Blocktrail =
            serde_json::from_slice(&published).unwrap();
        assert_eq!(published, stored.trail);
    }

    #[tokio::test]
    async fn gitmark_anchorer_refusals() {
        let mempool = FixtureMempool::empty();
        let storage: std::sync::Arc<dyn Storage> = std::sync::Arc::new(MemoryBackend::new());
        let anchorer = GitmarkAnchorer::with_storage(mempool.clone(), storage);
        let err = anchorer
            .anchor("prov", COMMITS[0], "testnet4")
            .await
            .unwrap_err();
        assert!(err.to_string().contains("no genesis"), "{err}");

        let voucher = issuer_voucher(&mempool, &"34".repeat(32));
        anchorer
            .genesis("prov", &voucher, COMMITS[0], "testnet4", "")
            .await
            .unwrap();
        let err = anchorer
            .anchor("prov", COMMITS[1], "mainnet")
            .await
            .unwrap_err();
        assert!(err.to_string().contains("network mismatch"), "{err}");
        let err = anchorer
            .anchor("prov", "not-a-commit", "testnet4")
            .await
            .unwrap_err();
        assert!(err.to_string().contains("commit hash"), "{err}");

        let verify_only = GitmarkAnchorer::new(mempool);
        let err = verify_only
            .anchor("prov", COMMITS[1], "testnet4")
            .await
            .unwrap_err();
        assert!(err.to_string().contains("with_storage"), "{err}");
    }

    // ── GitmarkAnchorer: serialised advances, durable broadcast intent ──

    /// A mempool that yields on every call (so concurrent tasks interleave),
    /// rejects a double spend like a node would, and can drop a broadcast.
    #[derive(Clone)]
    struct RacyMempool {
        inner: FixtureMempool,
        spent: std::sync::Arc<std::sync::Mutex<std::collections::HashSet<(String, u32)>>>,
        drop_next_broadcast: std::sync::Arc<std::sync::atomic::AtomicBool>,
        /// The backend accepted a broadcast but has not indexed it yet:
        /// presence checks answer "not found".
        index_lag: std::sync::Arc<std::sync::atomic::AtomicBool>,
        /// The next broadcast is refused with this node message.
        reject_next: std::sync::Arc<std::sync::Mutex<Option<String>>>,
    }
    impl RacyMempool {
        fn new(inner: FixtureMempool) -> Self {
            Self {
                inner,
                spent: Default::default(),
                drop_next_broadcast: Default::default(),
                index_lag: Default::default(),
                reject_next: Default::default(),
            }
        }
        fn reject_next(&self, msg: &str) {
            *self.reject_next.lock().unwrap() = Some(msg.into());
        }
        /// Show `txid:vout` as an unspent output of a known address, as an
        /// esplora backend does.
        fn show_unspent(&self, txid: &str, vout: u32) {
            let address = format!("tb1q{txid}{vout}");
            self.inner.txs.lock().unwrap().get_mut(txid).unwrap().vout[vout as usize]
                .scriptpubkey_address = Some(address.clone());
            self.inner.utxos.lock().unwrap().insert(
                address,
                vec![Utxo {
                    txid: txid.into(),
                    vout,
                    value: 9_700,
                    confirmed: true,
                    block_height: Some(1),
                }],
            );
        }
    }
    #[async_trait(?Send)]
    impl MempoolLookup for RacyMempool {
        async fn address_utxos(&self, address: &str) -> Result<Vec<Utxo>, PaymentError> {
            tokio::task::yield_now().await;
            self.inner.address_utxos(address).await
        }
        async fn tx(&self, txid: &str) -> Result<TxInfo, PaymentError> {
            tokio::task::yield_now().await;
            self.inner.tx(txid).await
        }
    }
    #[async_trait(?Send)]
    impl MempoolBroadcast for RacyMempool {
        async fn broadcast_tx(&self, raw_hex: &str) -> Result<String, PaymentError> {
            tokio::task::yield_now().await;
            if self
                .drop_next_broadcast
                .swap(false, std::sync::atomic::Ordering::SeqCst)
            {
                return Err(PaymentError::InvalidState("connection reset".into()));
            }
            let info = solid_pod_rs::bitcoin_tx::decode_tx_info(raw_hex)?;
            if let Some(msg) = self.reject_next.lock().unwrap().take() {
                return Err(PaymentError::InvalidState(msg));
            }
            // A node refuses the identical transaction a second time.
            if self.inner.txs.lock().unwrap().contains_key(&info.txid) {
                return Err(PaymentError::InvalidState(
                    "broadcast rejected (400): txn-already-in-mempool".into(),
                ));
            }
            {
                let mut spent = self.spent.lock().unwrap();
                if info
                    .vin
                    .iter()
                    .any(|i| spent.contains(&(i.txid.clone(), i.vout)))
                {
                    return Err(PaymentError::InvalidState("txn-mempool-conflict".into()));
                }
                for i in &info.vin {
                    spent.insert((i.txid.clone(), i.vout));
                }
            }
            self.inner.broadcast_tx(raw_hex).await
        }
    }

    #[async_trait(?Send)]
    impl MempoolTxPresence for RacyMempool {
        async fn transaction_exists(&self, txid: &str) -> Result<bool, PaymentError> {
            tokio::task::yield_now().await;
            if self.index_lag.load(std::sync::atomic::Ordering::SeqCst) {
                return Ok(false);
            }
            self.inner.transaction_exists(txid).await
        }
    }

    /// Two separate anchorer instances (as `build_anchorer` makes per request)
    /// over one pod, with genesis already on-chain.
    async fn racy_trail() -> (
        RacyMempool,
        std::sync::Arc<crate::trail_store::fault_storage::FaultStorage>,
        GitmarkAnchorer<RacyMempool>,
        GitmarkAnchorer<RacyMempool>,
    ) {
        let fixture = FixtureMempool::empty();
        let voucher = issuer_voucher(&fixture, &"35".repeat(32));
        let mempool = RacyMempool::new(fixture);
        let inner: std::sync::Arc<dyn Storage> = std::sync::Arc::new(MemoryBackend::new());
        let storage = crate::trail_store::fault_storage::FaultStorage::new(inner, true);
        let a = GitmarkAnchorer::with_storage(mempool.clone(), storage.clone());
        let b = GitmarkAnchorer::with_storage(mempool.clone(), storage.clone());
        a.genesis("prov", &voucher, COMMITS[0], "testnet4", "")
            .await
            .unwrap();
        (mempool, storage, a, b)
    }

    #[tokio::test]
    async fn concurrent_anchors_build_on_each_others_head() {
        let (mempool, storage, a, b) = racy_trail().await;
        let (ra, rb) = tokio::join!(
            a.anchor("prov", COMMITS[1], "testnet4"),
            b.anchor("prov", COMMITS[2], "testnet4"),
        );
        let (ra, rb) = (ra.unwrap(), rb.unwrap());
        let dyn_storage: std::sync::Arc<dyn Storage> = storage.clone();
        let stored = load_gitmark_trail(&dyn_storage, "prov")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(stored.trail.txo.len(), 3, "both anchors must land");
        let heads = [ra.txid.clone(), rb.txid.clone()];
        assert_eq!(
            heads
                .iter()
                .filter(|t| **t == stored.trail.txo[2].txid)
                .count(),
            1
        );
        assert!(heads.contains(&stored.trail.txo[1].txid));
        let report = solid_pod_rs::blocktrail::verify_blocktrail(&stored.trail, &mempool).await;
        assert!(report.is_verified(), "{report:?}");
    }

    #[tokio::test]
    async fn broadcast_then_failed_save_is_ok_and_recovered_by_the_next_anchor() {
        let (mempool, storage, a, _b) = racy_trail().await;
        storage.fail_put(&crate::trail_store::gitmark_trail_path("prov"));
        let landed = a
            .anchor("prov", COMMITS[1], "testnet4")
            .await
            .expect("the tx is on-chain: a failed save must not read as a failed anchor");
        storage.heal();
        let next = a.anchor("prov", COMMITS[2], "testnet4").await.unwrap();
        let dyn_storage: std::sync::Arc<dyn Storage> = storage.clone();
        let stored = load_gitmark_trail(&dyn_storage, "prov")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(stored.trail.txo.len(), 3);
        assert_eq!(stored.trail.txo[1].txid, landed.txid);
        assert_eq!(stored.trail.txo[2].txid, next.txid);
        let report = solid_pod_rs::blocktrail::verify_blocktrail(&stored.trail, &mempool).await;
        assert!(report.is_verified(), "{report:?}");
    }

    #[test]
    fn already_known_reads_only_node_statements_about_the_same_tx() {
        let e = |m: &str| PaymentError::InvalidState(m.into());
        assert!(already_known(&e(
            "broadcast rejected (400): txn-already-in-mempool"
        )));
        assert!(already_known(&e(
            "broadcast rejected (400): Transaction already in block chain"
        )));
        assert!(already_known(&e(
            "broadcast rejected (400): txn-already-known"
        )));
        assert!(!already_known(&e(
            "broadcast rejected (400): txn-mempool-conflict"
        )));
        assert!(!already_known(&e(
            "mempool broadcast failed: connection reset"
        )));
    }

    #[tokio::test]
    async fn dropped_broadcast_is_resent_by_the_next_anchor() {
        let (mempool, storage, a, _b) = racy_trail().await;
        mempool
            .drop_next_broadcast
            .store(true, std::sync::atomic::Ordering::SeqCst);
        let lost = a.anchor("prov", COMMITS[1], "testnet4").await;
        assert!(
            is_pending_anchor(&lost.unwrap_err()),
            "a recorded but unconfirmed broadcast is pending, not failed"
        );
        // the intent survives the failed call
        let dyn_storage: std::sync::Arc<dyn Storage> = storage.clone();
        assert!(
            crate::trail_store::load_gitmark_intent(&dyn_storage, "prov")
                .await
                .unwrap()
                .is_some()
        );
        // the next advance re-sends the same mark (it is not on-chain), then
        // builds on it
        let next = a.anchor("prov", COMMITS[2], "testnet4").await.unwrap();
        let stored = load_gitmark_trail(&dyn_storage, "prov")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(stored.trail.txo.len(), 3, "the re-sent mark landed");
        assert_eq!(stored.trail.txo[2].txid, next.txid);
        assert!(
            crate::trail_store::load_gitmark_intent(&dyn_storage, "prov")
                .await
                .unwrap()
                .is_none()
        );
        let report = solid_pod_rs::blocktrail::verify_blocktrail(&stored.trail, &mempool).await;
        assert!(report.is_verified(), "{report:?}");
    }

    #[tokio::test]
    async fn unresolved_resend_keeps_the_intent_and_refuses_to_advance() {
        let (mempool, storage, a, _b) = racy_trail().await;
        let drop_next = || {
            mempool
                .drop_next_broadcast
                .store(true, std::sync::atomic::Ordering::SeqCst)
        };
        drop_next();
        a.anchor("prov", COMMITS[1], "testnet4").await.unwrap_err();
        // the re-send fails too: nothing is known, so nothing is dropped
        drop_next();
        a.anchor("prov", COMMITS[2], "testnet4").await.unwrap_err();
        let dyn_storage: std::sync::Arc<dyn Storage> = storage.clone();
        assert!(
            crate::trail_store::load_gitmark_intent(&dyn_storage, "prov")
                .await
                .unwrap()
                .is_some(),
            "an unresolved mark must keep its intent"
        );
        let stored = load_gitmark_trail(&dyn_storage, "prov")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(stored.trail.txo.len(), 1);
    }

    #[tokio::test]
    async fn rejected_resend_with_an_unspent_head_drops_the_intent() {
        let (mempool, storage, a, _b) = racy_trail().await;
        let dyn_storage: std::sync::Arc<dyn Storage> = storage.clone();
        let head = load_gitmark_trail(&dyn_storage, "prov")
            .await
            .unwrap()
            .unwrap()
            .trail
            .txo[0]
            .txid
            .clone();
        mempool
            .drop_next_broadcast
            .store(true, std::sync::atomic::Ordering::SeqCst);
        a.anchor("prov", COMMITS[1], "testnet4").await.unwrap_err();
        // the node refuses the mark outright, and the head it spends is unspent
        mempool.reject_next("broadcast rejected (400): min relay fee not met");
        mempool.show_unspent(&head, 0);
        a.anchor("prov", COMMITS[2], "testnet4")
            .await
            .expect("a refused mark must not wedge the trail");
        let stored = load_gitmark_trail(&dyn_storage, "prov")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(stored.trail.txo.len(), 2, "the refused mark never landed");
        assert!(
            crate::trail_store::load_gitmark_intent(&dyn_storage, "prov")
                .await
                .unwrap()
                .is_none()
        );
    }

    #[tokio::test]
    async fn rejected_resend_without_proof_of_an_unspent_head_keeps_refusing() {
        let (mempool, storage, a, _b) = racy_trail().await;
        mempool
            .drop_next_broadcast
            .store(true, std::sync::atomic::Ordering::SeqCst);
        a.anchor("prov", COMMITS[1], "testnet4").await.unwrap_err();
        // refused, but nothing shows the head is still unspent
        mempool.reject_next("broadcast rejected (400): bad-txns-inputs-missingorspent");
        a.anchor("prov", COMMITS[2], "testnet4").await.unwrap_err();
        let dyn_storage: std::sync::Arc<dyn Storage> = storage.clone();
        assert!(
            crate::trail_store::load_gitmark_intent(&dyn_storage, "prov")
                .await
                .unwrap()
                .is_some()
        );
    }

    #[tokio::test]
    async fn block_anchorer_rejected_resend_with_an_unspent_head_drops_the_intent() {
        let (inner, fixture, ticker) = mint_and_store("DRP").await;
        let mempool = RacyMempool::new(fixture);
        let anchorer = MempoolBlockAnchorer::with_storage(mempool.clone(), inner.clone());
        let head = load_trail(&inner, &ticker)
            .await
            .unwrap()
            .unwrap()
            .current_txid;
        mempool
            .drop_next_broadcast
            .store(true, std::sync::atomic::Ordering::SeqCst);
        anchorer
            .anchor(&ticker, &"a".repeat(40), "testnet4")
            .await
            .unwrap_err();
        mempool.reject_next("broadcast rejected (400): min relay fee not met");
        mempool.show_unspent(&head, 0);
        anchorer
            .anchor(&ticker, &"b".repeat(40), "testnet4")
            .await
            .expect("a refused anchor must not wedge the trail");
        let stored = load_trail(&inner, &ticker).await.unwrap().unwrap();
        assert_eq!(stored.states.len(), 2);
        assert!(!inner.exists(&anchor_intent_path(&ticker)).await.unwrap());
    }

    #[tokio::test]
    async fn block_anchorer_refuses_an_intent_whose_txid_is_not_its_transaction() {
        let (inner, fixture, ticker) = mint_and_store("TMP").await;
        let mempool = RacyMempool::new(fixture);
        let anchorer = MempoolBlockAnchorer::with_storage(mempool.clone(), inner.clone());
        mempool
            .drop_next_broadcast
            .store(true, std::sync::atomic::Ordering::SeqCst);
        anchorer
            .anchor(&ticker, &"a".repeat(40), "testnet4")
            .await
            .unwrap_err();
        // Tamper with the recorded txid, as a writer of the intent file could,
        // naming a transaction the mempool does know: the stored head. Taken
        // at face value it reads as "landed", and the trail would be advanced
        // over an anchor that never went out.
        let path = anchor_intent_path(&ticker);
        let head = load_trail(&inner, &ticker)
            .await
            .unwrap()
            .unwrap()
            .current_txid;
        let (bytes, _) = inner.get(&path).await.unwrap();
        let mut v: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
        v["txid"] = serde_json::json!(head);
        inner
            .put(
                &path,
                bytes::Bytes::from(serde_json::to_vec(&v).unwrap()),
                "application/json",
            )
            .await
            .unwrap();
        let before = load_trail(&inner, &ticker).await.unwrap().unwrap();
        let err = anchorer
            .anchor(&ticker, &"b".repeat(40), "testnet4")
            .await
            .unwrap_err();
        assert!(err.to_string().contains("refused"), "{err}");
        let after = load_trail(&inner, &ticker).await.unwrap().unwrap();
        assert_eq!(after.states.len(), before.states.len());
        assert!(inner.exists(&path).await.unwrap(), "the intent is kept");
    }

    #[tokio::test]
    async fn accepted_but_unindexed_mark_is_not_orphaned_by_the_next_anchor() {
        let (mempool, storage, a, _b) = racy_trail().await;
        // the broadcast is accepted, the trail save is lost
        storage.fail_put(&crate::trail_store::gitmark_trail_path("prov"));
        let landed = a.anchor("prov", COMMITS[1], "testnet4").await.unwrap();
        storage.heal();
        // the backend has not indexed it yet: presence answers 404
        mempool
            .index_lag
            .store(true, std::sync::atomic::Ordering::SeqCst);
        let next = a
            .anchor("prov", COMMITS[2], "testnet4")
            .await
            .expect("the next advance must build on the accepted mark, not on the old head");
        let dyn_storage: std::sync::Arc<dyn Storage> = storage.clone();
        let stored = load_gitmark_trail(&dyn_storage, "prov")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(stored.trail.txo.len(), 3);
        assert_eq!(stored.trail.txo[1].txid, landed.txid);
        assert_eq!(stored.trail.txo[2].txid, next.txid);
        let report = solid_pod_rs::blocktrail::verify_blocktrail(&stored.trail, &mempool).await;
        assert!(report.is_verified(), "{report:?}");
    }

    #[tokio::test]
    async fn block_anchorer_accepted_broadcast_with_failed_save_is_not_a_failed_anchor() {
        use crate::trail_store::fault_storage::FaultStorage;
        let (inner, mempool, ticker) = mint_and_store("FLT").await;
        let storage = FaultStorage::new(inner, false);
        let anchorer = MempoolBlockAnchorer::with_storage(mempool.clone(), storage.clone());
        storage.fail_put(&crate::trail_store::trail_path(&ticker));
        let first = anchorer
            .anchor(&ticker, &"a".repeat(40), "testnet4")
            .await
            .expect("the tx is on-chain: a failed save must not read as a failed anchor");
        storage.heal();
        // the next advance settles the first, then builds on it
        let second = anchorer
            .anchor(&ticker, &"b".repeat(40), "testnet4")
            .await
            .unwrap();
        let dyn_storage: std::sync::Arc<dyn Storage> = storage.clone();
        let stored = load_trail(&dyn_storage, &ticker).await.unwrap().unwrap();
        assert_eq!(stored.states.len(), 3);
        assert_eq!(stored.current_txid, second.txid);
        let second_tx = mempool
            .txs
            .lock()
            .unwrap()
            .get(&second.txid)
            .cloned()
            .unwrap();
        assert_eq!(
            second_tx.vin[0].txid, first.txid,
            "the second anchor must spend the first, not the old head"
        );
    }

    #[tokio::test]
    async fn http_client_presence_check_reaches_the_inherent_method() {
        // A closed port: the call must come back as an ambiguous Err (not a
        // definite `false`, not a recursion into the trait impl).
        let client = MempoolHttpClient::new("http://127.0.0.1:1");
        let out =
            <MempoolHttpClient as MempoolTxPresence>::transaction_exists(&client, &"ab".repeat(32))
                .await;
        assert!(out.is_err(), "{out:?}");
    }
}