canton-token 0.3.4

Canton Network token-standard workflows (CIP-56 and CIP-0112): registry client, choice contexts, transfers and allocations, and a Splice Scan client.
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
//! Splice Scan's HTTP API, asked directly or through a validator's scan-proxy.
//!
//! Scan is the service each Super Validator runs to publish what the DSO
//! decided and what happened on the network: the DSO party, the Amulet rules,
//! the mining rounds, traffic balances, the update history, snapshots of the
//! active contracts. The paths and payloads here come from Splice's OpenAPI
//! documents, `scan.yaml` and `scan-proxy.yaml` (vendored under
//! `testdata/openapi/`), and the methods are named after what they return.
//!
//! # Two ways in
//!
//! - [`ScanClient::new`] asks a Scan itself, at `<scan>/api/scan`. It serves
//!   everything here.
//! - [`ScanClient::through_validator`] asks a validator's scan-proxy, at
//!   `<validator>/api/validator`. The validator reads from several Super
//!   Validators' Scans and answers when enough of them agree, and it
//!   authenticates every request, so this route needs a token
//!   ([`with_token`](ScanClient::with_token),
//!   [`with_token_source`](ScanClient::with_token_source)). It serves the
//!   part of the API a wallet or an application backend needs, not the
//!   traffic, history and snapshot-page reads, which are refused before
//!   anything is sent; of the snapshot reads it serves only
//!   [`holdings_summary`](ScanClient::holdings_summary).
//!
//! A Scan asked directly is one Super Validator's word. This client does not
//! compare several Scans; the scan-proxy is the route that does.
//!
//! # Why an application needs it
//!
//! Some choices take contracts the DSO owns: the `AmuletRules`, the open
//! mining round, a provider's `FeaturedAppRight`. Creating a
//! `TransferPreapproval` is one such choice. The submitting party does not
//! see those contracts, so they are passed as disclosed contracts, and Scan
//! is where they are published. [`ScanClient::amulet_rules`] and
//! [`ScanClient::latest_open_mining_round`] return them with
//! [`disclosed`](ContractWithState::disclosed), which is what a submission
//! attaches. [`ScanClient::featured_app_right`] returns its contract without
//! a synchronizer: disclose it on the rules' one, with
//! [`ScanContract::disclosed`].
//!
//! # Splice versions
//!
//! Written against the documents of Splice 0.10.3. On an older Scan or
//! validator: [`dso`](ScanClient::dso) falls back to the `/v0/dso` every
//! release serves; the two snapshot page reads
//! ([`acs_snapshot`](ScanClient::acs_snapshot),
//! [`holdings_state`](ScanClient::holdings_state)) need Splice 0.7.5, which
//! is where their `/v2` paths appeared;
//! [`holdings_summary`](ScanClient::holdings_summary) needs 0.6.3. Everything
//! else is served by Splice 0.6.0 and later.
//!
//! # Stability
//!
//! Splice tags each Scan endpoint `external` or `internal`. The external ones
//! are its public API. The internal ones may change in any Splice release,
//! and two of them are the only source of a contract an application may
//! need: [`featured_app_right`](ScanClient::featured_app_right) and
//! [`transfer_preapproval_by_party`](ScanClient::transfer_preapproval_by_party).
//! Asked of a Scan directly, these are refused unless the client was built
//! with [`with_internal_endpoints`](ScanClient::with_internal_endpoints),
//! which is the caller accepting that. Through a validator's scan-proxy they
//! are part of the proxy's own API and need no opt-in.
//!
//! [`amulet_rules`](ScanClient::amulet_rules) needs no opt-in either way:
//! the external API publishes the rules with the rest of the DSO's contracts,
//! and that is where a Scan is asked for them. With the opt-in it asks the
//! internal endpoint, which returns the one contract and nothing else.
//!
//! Contract payloads, choice arguments and results stay JSON
//! ([`serde_json::Value`]): they are Daml values of whatever template the
//! contract has, in the compact encoding the Ledger JSON API uses.

use std::collections::BTreeMap;
use std::time::Duration;

use base64::Engine as _;
use canton_core::{Auth, Error, Result, TokenSource};
use canton_daml::Timestamp;
use canton_proto::com::daml::ledger::api::v2 as pb;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::registry::{
    ErrorResponse, MAX_RESPONSE_BYTES, check_segments, parse_base_url, read_body_from,
    read_error_body, transport_error, truncate,
};

/// How Scan is named in an error.
const SCAN: &str = "Scan";

/// How long to wait on a Scan that accepts the connection and then says
/// nothing. `reqwest`'s own default is unbounded.
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

/// The most contracts a history or snapshot page may ask for.
const MAX_PAGE_SIZE: u32 = 1000;

/// Where the client's base URL points.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Route {
    /// A Scan: `<scan>/api/scan`, paths `/<version>/…`.
    Scan,
    /// A validator: `<validator>/api/validator`, paths
    /// `/<version>/scan-proxy/…`.
    ScanProxy,
}

/// A client for one Scan, or for one validator's scan-proxy.
#[derive(Clone, Debug)]
pub struct ScanClient {
    base: reqwest::Url,
    http: reqwest::Client,
    auth: Auth,
    route: Route,
    internal_endpoints: bool,
}

/// A contract as Scan publishes it.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ScanContract {
    /// `<package id>:<Module>:<Entity>`.
    pub template_id: String,
    /// The contract id.
    pub contract_id: String,
    /// The contract's arguments, as Daml JSON.
    pub payload: Value,
    /// The created-event blob, base64 as JSON requires. This is what a
    /// participant needs to accept the contract as disclosed.
    pub created_event_blob: String,
    /// When the contract was created.
    pub created_at: String,
}

/// A contract and the synchronizer it is assigned to.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ContractWithState {
    /// The contract.
    pub contract: ScanContract,
    /// The synchronizer the contract is assigned to. `None` while it is
    /// being reassigned from one synchronizer to another.
    #[serde(rename = "domain_id", default)]
    pub synchronizer_id: Option<String>,
}

impl ScanContract {
    /// The contract as a disclosed contract for a gRPC submission, on
    /// `synchronizer_id`.
    ///
    /// # Errors
    /// [`Error::UnexpectedResponse`] if the template id is not
    /// `<package>:<Module>:<Entity>` or the blob is not base64.
    pub fn disclosed(&self, synchronizer_id: &str) -> Result<pb::DisclosedContract> {
        let created_event_blob = base64::engine::general_purpose::STANDARD
            .decode(&self.created_event_blob)
            .map_err(|e| {
                Error::UnexpectedResponse(format!(
                    "Scan published contract `{}` with a created-event blob that is not \
                     base64: {e}",
                    self.contract_id
                ))
            })?;
        Ok(pb::DisclosedContract {
            template_id: Some(identifier(&self.template_id)?),
            contract_id: self.contract_id.clone(),
            created_event_blob,
            synchronizer_id: synchronizer_id.to_string(),
        })
    }

    /// The contract as a disclosed contract for the JSON Ledger API, which
    /// takes the blob as the base64 Scan already sent.
    #[must_use]
    pub fn disclosed_json(&self, synchronizer_id: &str) -> Value {
        serde_json::json!({
            "templateId": self.template_id,
            "contractId": self.contract_id,
            "createdEventBlob": self.created_event_blob,
            "synchronizerId": synchronizer_id,
        })
    }
}

impl ContractWithState {
    /// The contract as a disclosed contract for a gRPC submission, on the
    /// synchronizer it is assigned to.
    ///
    /// # Errors
    /// As [`ScanContract::disclosed`], plus [`Error::Connection`], which is
    /// retriable, if the contract is between synchronizers and so has none to
    /// be disclosed on. That passes in seconds.
    pub fn disclosed(&self) -> Result<pb::DisclosedContract> {
        self.contract.disclosed(self.assigned()?)
    }

    /// The contract as a disclosed contract for the JSON Ledger API.
    ///
    /// # Errors
    /// [`Error::Connection`] if the contract is between synchronizers.
    pub fn disclosed_json(&self) -> Result<Value> {
        Ok(self.contract.disclosed_json(self.assigned()?))
    }

    fn assigned(&self) -> Result<&str> {
        self.synchronizer_id.as_deref().ok_or_else(|| {
            Error::Connection(format!(
                "contract `{}` is being reassigned between synchronizers, so there is none to \
                 disclose it on yet; retry shortly",
                self.contract.contract_id
            ))
        })
    }
}

/// `<package>:<Module>:<Entity>` as the Ledger API's three fields. A module
/// name has dots and never colons, so exactly three parts is the only
/// well-formed shape.
fn identifier(template_id: &str) -> Result<pb::Identifier> {
    let mut parts = template_id.split(':');
    match (parts.next(), parts.next(), parts.next(), parts.next()) {
        (Some(package_id), Some(module_name), Some(entity_name), None)
            if !package_id.is_empty() && !module_name.is_empty() && !entity_name.is_empty() =>
        {
            Ok(pb::Identifier {
                package_id: package_id.to_string(),
                module_name: module_name.to_string(),
                entity_name: entity_name.to_string(),
            })
        }
        _ => Err(Error::UnexpectedResponse(format!(
            "Scan published a contract with template id `{template_id}`, which is not \
             `<package>:<Module>:<Entity>`"
        ))),
    }
}

/// What the DSO is and the contracts that say how it runs.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct DsoInfo {
    /// The party that represents the DSO as a whole.
    pub dso_party_id: String,
    /// How many Super Validators must agree for a vote to pass.
    pub voting_threshold: u32,
    /// The `OpenMiningRound` with the highest round number. It may not have
    /// opened yet: see [`MiningRounds::latest_open`] for the one to use.
    pub latest_mining_round: ContractWithState,
    /// The `AmuletRules` contract.
    pub amulet_rules: ContractWithState,
    /// The `DsoRules` contract.
    pub dso_rules: ContractWithState,
    /// One `SvNodeState` contract per Super Validator in the DSO rules.
    pub sv_node_states: Vec<ContractWithState>,
    /// The round the network started from, where Scan reports it.
    #[serde(default)]
    pub initial_round: Option<String>,
}

/// The Scans the Super Validators run for one synchronizer.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct SynchronizerScans {
    /// The synchronizer.
    #[serde(rename = "domainId")]
    pub synchronizer_id: String,
    /// Its Scans.
    pub scans: Vec<ScanInfo>,
}

/// One Super Validator's Scan.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ScanInfo {
    /// The Scan's public URL, without `/api/scan`.
    pub public_url: String,
    /// The Super Validator that runs it.
    pub sv_name: String,
}

/// The mining rounds that rewards can be registered in, and the ones whose
/// rewards are being computed.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct MiningRounds {
    /// `OpenMiningRound` contracts. Several are open at once, and the newest
    /// may not have reached its opening time.
    pub open: Vec<ContractWithState>,
    /// `IssuingMiningRound` contracts.
    pub issuing: Vec<ContractWithState>,
}

/// The two fields of an `OpenMiningRound` that decide whether it is the one
/// to use.
#[derive(Deserialize)]
struct OpenRound {
    round: Round,
    #[serde(rename = "opensAt")]
    opens_at: Timestamp,
}

#[derive(Deserialize)]
struct Round {
    #[serde(deserialize_with = "int64")]
    number: i64,
}

/// A Daml `Int` in JSON: a string, since a JSON number cannot hold every
/// `i64`, though a number is accepted too.
fn int64<'de, D: serde::Deserializer<'de>>(deserializer: D) -> std::result::Result<i64, D::Error> {
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum Int {
        Text(String),
        Number(i64),
    }
    match Int::deserialize(deserializer)? {
        Int::Text(text) => text.parse().map_err(serde::de::Error::custom),
        Int::Number(number) => Ok(number),
    }
}

impl MiningRounds {
    /// The open round to pass to a choice at `now`: of the rounds whose
    /// opening time has come, the one with the highest number.
    ///
    /// This is the round Splice's own applications pick
    /// (`selectLatestOpenMiningRound`). The round with the lowest number is
    /// open too, but it is the next to be archived, and a transaction built
    /// on it fails if the archival lands first.
    ///
    /// # Errors
    /// [`Error::UnexpectedResponse`] if a round's payload has no `round` or
    /// `opensAt`, and [`Error::Connection`], which is retriable, if no round
    /// has opened yet.
    pub fn latest_open(&self, now: Timestamp) -> Result<&ContractWithState> {
        let mut latest: Option<(i64, &ContractWithState)> = None;
        for round in &self.open {
            let fields: OpenRound = serde_json::from_value(round.contract.payload.clone())
                .map_err(|e| {
                    Error::UnexpectedResponse(format!(
                        "Scan published open mining round `{}` with a payload that is not an \
                         OpenMiningRound: {e}",
                        round.contract.contract_id
                    ))
                })?;
            if fields.opens_at > now {
                continue;
            }
            if latest.is_none_or(|(number, _)| fields.round.number > number) {
                latest = Some((fields.round.number, round));
            }
        }
        latest.map(|(_, round)| round).ok_or_else(|| {
            Error::Connection(format!(
                "none of the {} open mining rounds Scan published has reached its opening time; \
                 retry shortly",
                self.open.len()
            ))
        })
    }
}

/// How much traffic a member has bought and used on a synchronizer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TrafficStatus {
    /// Bytes of extra traffic the member has consumed, as the sequencers
    /// account it.
    pub total_consumed: i64,
    /// Bytes of extra traffic the member may consume in total, as the
    /// sequencers account it.
    pub total_limit: i64,
    /// Bytes of extra traffic the member has purchased, as the DSO records
    /// it. The limit catches up with this once the purchase is processed.
    pub total_purchased: i64,
}

/// An entry in the Amulet Name Service.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct AnsEntry {
    /// The entry's contract, where it has one. Entries the DSO defines for
    /// itself and the Super Validators have none.
    #[serde(default)]
    pub contract_id: Option<String>,
    /// The party the name belongs to.
    pub user: String,
    /// The name.
    pub name: String,
    /// The URL the owner gave.
    pub url: String,
    /// The description the owner gave.
    pub description: String,
    /// When the entry expires, where it does.
    #[serde(default)]
    pub expires_at: Option<String>,
}

/// A point in Scan's history: everything up to and including it has been
/// read. Pass it back to read on from there.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct HistoryPosition {
    /// The migration the record time belongs to. Record times start over
    /// after a hard synchronizer migration, so one alone does not order
    /// history.
    #[serde(rename = "after_migration_id")]
    pub migration_id: i64,
    /// The record time.
    #[serde(rename = "after_record_time")]
    pub record_time: String,
}

/// One entry of the update history.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ScanUpdate {
    /// A transaction.
    Transaction(ScanTransaction),
    /// A contract leaving one synchronizer or arriving on another.
    Reassignment(ScanReassignment),
}

impl ScanUpdate {
    /// Where this update is in history, to read on from.
    #[must_use]
    pub fn position(&self) -> HistoryPosition {
        match self {
            Self::Transaction(transaction) => HistoryPosition {
                migration_id: transaction.migration_id,
                record_time: transaction.record_time.clone(),
            },
            Self::Reassignment(reassignment) => HistoryPosition {
                migration_id: match &reassignment.event {
                    ReassignmentEvent::Assignment(assignment) => assignment.migration_id,
                    ReassignmentEvent::Unassignment(unassignment) => unassignment.migration_id,
                },
                record_time: reassignment.record_time.clone(),
            },
        }
    }

    /// The update id.
    #[must_use]
    pub fn update_id(&self) -> &str {
        match self {
            Self::Transaction(transaction) => &transaction.update_id,
            Self::Reassignment(reassignment) => &reassignment.update_id,
        }
    }
}

/// The API gives the two kinds of update no tag to tell them by: a
/// transaction is the one with events, a reassignment the one with an event.
impl<'de> Deserialize<'de> for ScanUpdate {
    fn deserialize<D: serde::Deserializer<'de>>(
        deserializer: D,
    ) -> std::result::Result<Self, D::Error> {
        use serde::de::Error as _;
        let value = Value::deserialize(deserializer)?;
        if value.get("events_by_id").is_some() {
            serde_json::from_value(value)
                .map(Self::Transaction)
                .map_err(D::Error::custom)
        } else if value.get("event").is_some() {
            serde_json::from_value(value)
                .map(Self::Reassignment)
                .map_err(D::Error::custom)
        } else {
            Err(D::Error::custom(
                "an update with neither `events_by_id` (a transaction) nor `event` (a \
                 reassignment)",
            ))
        }
    }
}

/// A transaction in the update history.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ScanTransaction {
    /// The update id.
    pub update_id: String,
    /// The migration the transaction was recorded in.
    pub migration_id: i64,
    /// The workflow id the submitter gave, empty when it gave none.
    pub workflow_id: String,
    /// When the sequencer recorded it.
    pub record_time: String,
    /// The synchronizer it was sequenced on.
    pub synchronizer_id: String,
    /// The ledger effective time.
    pub effective_at: String,
    /// The ids of the root events, in execution order.
    pub root_event_ids: Vec<String>,
    /// Every event of the transaction by its id.
    pub events_by_id: BTreeMap<String, ScanEvent>,
    /// The hash an external party signed, for an externally signed
    /// transaction.
    #[serde(default)]
    pub external_transaction_hash: Option<String>,
}

/// An event of a transaction.
#[derive(Clone, Debug, Deserialize)]
#[serde(tag = "event_type")]
#[non_exhaustive]
pub enum ScanEvent {
    /// A contract was created.
    #[serde(rename = "created_event")]
    Created(ScanCreatedEvent),
    /// A choice was exercised.
    #[serde(rename = "exercised_event")]
    Exercised(ScanExercisedEvent),
}

/// A contract's creation.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ScanCreatedEvent {
    /// The event id.
    pub event_id: String,
    /// The contract id.
    pub contract_id: String,
    /// `<package id>:<Module>:<Entity>`.
    pub template_id: String,
    /// The name of the package the template is in.
    pub package_name: String,
    /// The contract's arguments, as Daml JSON.
    pub create_arguments: Value,
    /// When the contract was created.
    pub created_at: String,
    /// The contract's signatories.
    pub signatories: Vec<String>,
    /// The contract's observers.
    pub observers: Vec<String>,
}

/// A choice exercised on a contract.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ScanExercisedEvent {
    /// The event id.
    pub event_id: String,
    /// The contract the choice was exercised on.
    pub contract_id: String,
    /// `<package id>:<Module>:<Entity>`.
    pub template_id: String,
    /// The name of the package the template is in.
    pub package_name: String,
    /// The choice.
    pub choice: String,
    /// The choice's argument, as Daml JSON.
    pub choice_argument: Value,
    /// The ids of the events the choice caused, in execution order.
    pub child_event_ids: Vec<String>,
    /// What the choice returned, as Daml JSON.
    pub exercise_result: Value,
    /// Whether the choice archived the contract.
    pub consuming: bool,
    /// The parties that exercised it.
    pub acting_parties: Vec<String>,
    /// The interface the choice was exercised through, where it was.
    #[serde(default)]
    pub interface_id: Option<String>,
}

/// A contract leaving one synchronizer or arriving on another.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ScanReassignment {
    /// The update id.
    pub update_id: String,
    /// The offset on the Scan's own participant. It means nothing to another
    /// participant.
    pub offset: String,
    /// When the sequencer recorded it.
    pub record_time: String,
    /// Which half of the reassignment this is.
    pub event: ReassignmentEvent,
}

/// The two halves of a reassignment.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ReassignmentEvent {
    /// The contract arrived on the target synchronizer.
    Assignment(ScanAssignment),
    /// The contract left the source synchronizer.
    Unassignment(ScanUnassignment),
}

/// As [`ScanUpdate`]: untagged in the API, and told apart by the one field
/// each has that the other does not.
impl<'de> Deserialize<'de> for ReassignmentEvent {
    fn deserialize<D: serde::Deserializer<'de>>(
        deserializer: D,
    ) -> std::result::Result<Self, D::Error> {
        use serde::de::Error as _;
        let value = Value::deserialize(deserializer)?;
        if value.get("created_event").is_some() {
            serde_json::from_value(value)
                .map(Self::Assignment)
                .map_err(D::Error::custom)
        } else if value.get("contract_id").is_some() {
            serde_json::from_value(value)
                .map(Self::Unassignment)
                .map_err(D::Error::custom)
        } else {
            Err(D::Error::custom(
                "a reassignment event with neither `created_event` (an assignment) nor \
                 `contract_id` (an unassignment)",
            ))
        }
    }
}

/// A contract arriving on a synchronizer.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ScanAssignment {
    /// The party that submitted the reassignment.
    pub submitter: String,
    /// The synchronizer the contract left.
    pub source_synchronizer: String,
    /// The synchronizer it arrived on.
    pub target_synchronizer: String,
    /// The migration it was recorded in.
    pub migration_id: i64,
    /// The id that ties this half to the unassignment.
    pub unassign_id: String,
    /// The contract, as created on the target synchronizer.
    pub created_event: ScanCreatedEvent,
    /// How many times the contract has been reassigned.
    pub reassignment_counter: i64,
}

/// A contract leaving a synchronizer.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ScanUnassignment {
    /// The party that submitted the reassignment.
    pub submitter: String,
    /// The synchronizer the contract left.
    pub source_synchronizer: String,
    /// The migration it was recorded in.
    pub migration_id: i64,
    /// The synchronizer it is going to.
    pub target_synchronizer: String,
    /// The id that ties this half to the assignment.
    pub unassign_id: String,
    /// How many times the contract has been reassigned.
    pub reassignment_counter: i64,
    /// The contract.
    pub contract_id: String,
}

/// One entry of the event history: an update, the mediator's verdict on it,
/// or both.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ScanEventItem {
    /// The update, where Scan's participant saw it. A transaction the
    /// mediator rejected has a verdict and no update.
    #[serde(default)]
    pub update: Option<ScanUpdate>,
    /// The mediator's verdict, as Scan publishes it.
    #[serde(default)]
    pub verdict: Option<Value>,
    /// The traffic the update cost, where Scan reports it.
    #[serde(default)]
    pub traffic_summary: Option<Value>,
    /// The application activity records the update produced, where Scan
    /// reports them.
    #[serde(default)]
    pub app_activity_records: Option<Value>,
    /// The asset activity records the update produced, where Scan reports
    /// them.
    #[serde(default)]
    pub asset_activity_records: Option<Value>,
    /// The application provider the submission was made through, where Scan
    /// reports one.
    #[serde(default)]
    pub submitting_app_provider: Option<String>,
}

impl ScanEventItem {
    /// Where this entry is in history, to read on from: the update's
    /// position, or the verdict's where there is no update, as for a
    /// transaction the mediator rejected. `None` only for an entry with
    /// neither, or a verdict without its migration and record time.
    #[must_use]
    pub fn position(&self) -> Option<HistoryPosition> {
        if let Some(update) = &self.update {
            return Some(update.position());
        }
        let verdict = self.verdict.as_ref()?;
        Some(HistoryPosition {
            migration_id: verdict.get("migration_id")?.as_i64()?,
            record_time: verdict.get("record_time")?.as_str()?.to_string(),
        })
    }
}

/// Which snapshot of the active contracts to read, and which part of it.
///
/// Scan takes a snapshot every few hours.
/// [`ScanClient::acs_snapshot_timestamp`] gives the record time of the one
/// before a moment; [`at_or_before`](Self::at_or_before) lets Scan find it
/// instead.
#[derive(Clone, Debug)]
pub struct SnapshotRequest {
    migration_id: i64,
    record_time: String,
    at_or_before: bool,
    after: Option<String>,
    page_size: u32,
    parties: Vec<String>,
    templates: Vec<String>,
}

impl SnapshotRequest {
    /// The snapshot taken at exactly `record_time` in `migration_id`.
    pub fn at(migration_id: i64, record_time: impl Into<String>) -> Self {
        Self {
            migration_id,
            record_time: record_time.into(),
            at_or_before: false,
            after: None,
            page_size: 100,
            parties: Vec::new(),
            templates: Vec::new(),
        }
    }

    /// Take the most recent snapshot at or before the record time, where the
    /// default is to require one at exactly that time.
    #[must_use]
    pub fn at_or_before(mut self) -> Self {
        self.at_or_before = true;
        self
    }

    /// How many contracts a page holds. Defaults to 100; Scan allows 1000.
    #[must_use]
    pub fn with_page_size(mut self, page_size: u32) -> Self {
        self.page_size = page_size;
        self
    }

    /// Read the page after the one that returned this token.
    #[must_use]
    pub fn after(mut self, next_page_token: impl Into<String>) -> Self {
        self.after = Some(next_page_token.into());
        self
    }

    /// Only contracts these parties are stakeholders of. For the holdings
    /// reads these are the owners, and at least one is required.
    #[must_use]
    pub fn for_parties<I, S>(mut self, parties: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.parties = parties.into_iter().map(Into::into).collect();
        self
    }

    /// Only contracts of these templates, each as
    /// `<package name>:<Module>:<Entity>`. Ignored by the holdings reads.
    #[must_use]
    pub fn for_templates<I, S>(mut self, templates: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.templates = templates.into_iter().map(Into::into).collect();
        self
    }

    fn record_time_match(&self) -> &'static str {
        if self.at_or_before {
            "at_or_before"
        } else {
            "exact"
        }
    }

    fn owners(&self, read: &str) -> Result<&[String]> {
        if self.parties.is_empty() {
            return Err(Error::InvalidRequest(format!(
                "{read} needs at least one owner party (SnapshotRequest::for_parties)"
            )));
        }
        Ok(&self.parties)
    }
}

/// A page of a snapshot of the active contracts.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct AcsSnapshot {
    /// The record time of the snapshot that was read.
    pub record_time: String,
    /// The migration it belongs to.
    pub migration_id: i64,
    /// The contracts on this page.
    pub created_events: Vec<ActiveContract>,
    /// The token for the next page, absent on the last one.
    #[serde(default)]
    pub next_page_token: Option<String>,
}

/// A contract in a snapshot.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ActiveContract {
    /// The update that created it, where Scan reports it.
    #[serde(default)]
    pub created_in_update_id: Option<String>,
    /// The contract id.
    pub contract_id: String,
    /// `<package id>:<Module>:<Entity>`.
    pub template_id: String,
    /// The name of the package the template is in.
    pub package_name: String,
    /// The contract's arguments, as Daml JSON.
    pub create_arguments: Value,
    /// When the contract was created.
    pub created_at: String,
    /// The contract's signatories.
    pub signatories: Vec<String>,
    /// The contract's observers.
    pub observers: Vec<String>,
}

/// What some parties held at a snapshot.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct HoldingsSummaries {
    /// The record time of the snapshot that was read.
    pub record_time: String,
    /// The migration it belongs to.
    pub migration_id: i64,
    /// One summary per owner that held anything.
    pub summaries: Vec<HoldingsSummary>,
}

/// What one party held at a snapshot. Amounts are decimal strings, as Daml
/// JSON writes a `Decimal`.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct HoldingsSummary {
    /// The owner.
    pub party_id: String,
    /// The Amulet the party could spend.
    pub total_unlocked_coin: String,
    /// The Amulet locked in its name.
    pub total_locked_coin: String,
    /// Both together.
    pub total_coin_holdings: String,
}

impl ScanClient {
    /// A client for the Scan served at `base_url`, which ends in `/api/scan`.
    ///
    /// # Errors
    /// [`Error::InvalidRequest`] if `base_url` is not a URL, or an HTTP client
    /// cannot be built.
    pub fn new(base_url: &str) -> Result<Self> {
        Self::with_http_client(base_url, default_http_client()?)
    }

    /// [`new`](Self::new) with an HTTP client the caller has configured:
    /// timeouts, proxies, a custom TLS root store. Configure it with
    /// `reqwest::redirect::Policy::none()`, as [`new`](Self::new) does: what
    /// Scan answers goes into commands the caller signs, so it must come from
    /// the Scan the caller named and not from wherever a 3xx points.
    ///
    /// # Errors
    /// [`Error::InvalidRequest`] if `base_url` is not a URL.
    pub fn with_http_client(base_url: &str, http: reqwest::Client) -> Result<Self> {
        Self::build(base_url, http, Route::Scan)
    }

    /// A client for the scan-proxy of the validator served at `base_url`,
    /// which ends in `/api/validator`.
    ///
    /// The proxy authenticates every request: give the client a token with
    /// [`with_token`](Self::with_token) or
    /// [`with_token_source`](Self::with_token_source).
    ///
    /// # Errors
    /// [`Error::InvalidRequest`] if `base_url` is not a URL, or an HTTP client
    /// cannot be built.
    pub fn through_validator(base_url: &str) -> Result<Self> {
        Self::through_validator_with_http_client(base_url, default_http_client()?)
    }

    /// [`through_validator`](Self::through_validator) with an HTTP client the
    /// caller has configured. See [`with_http_client`](Self::with_http_client).
    ///
    /// # Errors
    /// [`Error::InvalidRequest`] if `base_url` is not a URL.
    pub fn through_validator_with_http_client(
        base_url: &str,
        http: reqwest::Client,
    ) -> Result<Self> {
        Self::build(base_url, http, Route::ScanProxy)
    }

    fn build(base_url: &str, http: reqwest::Client, route: Route) -> Result<Self> {
        let base = parse_base_url(base_url, "Scan")?;
        Ok(Self {
            base,
            http,
            auth: Auth::None,
            route,
            internal_endpoints: false,
        })
    }

    /// Send a bearer token with every request. A validator's scan-proxy
    /// requires one; a Scan does not.
    #[must_use]
    pub fn with_token(mut self, token: impl Into<String>) -> Self {
        self.auth = Auth::Static(token.into());
        self
    }

    /// Send a bearer token from a source that refreshes it, such as
    /// `canton_auth::TokenProvider`.
    #[must_use]
    pub fn with_token_source(mut self, source: impl TokenSource + 'static) -> Self {
        self.auth = Auth::Dynamic(std::sync::Arc::new(source));
        self
    }

    /// Allow the calls that reach endpoints Splice tags `internal` when the
    /// client asks a Scan directly:
    /// [`featured_app_right`](Self::featured_app_right) and
    /// [`transfer_preapproval_by_party`](Self::transfer_preapproval_by_party),
    /// and the lighter of the two ways to read
    /// [`amulet_rules`](Self::amulet_rules).
    ///
    /// Splice may change an internal endpoint in any release, and these
    /// methods change with it. Through a validator's scan-proxy the same
    /// calls are part of the proxy's API, and this is not needed.
    #[must_use]
    pub fn with_internal_endpoints(mut self) -> Self {
        self.internal_endpoints = true;
        self
    }

    /// Build a URL from an API version and path segments, under the
    /// scan-proxy prefix where the client goes through a validator. Every
    /// segment is percent-encoded, so a party or a name cannot change which
    /// resource is addressed, and a segment that is empty, `.` or `..` is
    /// refused: URL parsing resolves those away, so `featured_app_right("..")`
    /// asked for the list of every right and read it as "not featured".
    fn url(&self, version: &str, segments: &[&str]) -> Result<reqwest::Url> {
        check_segments(&[version])?;
        check_segments(segments)?;
        let mut url = self.base.clone();
        {
            let mut path = url.path_segments_mut().map_err(|()| {
                Error::InvalidRequest("the Scan URL cannot have a path".to_string())
            })?;
            path.pop_if_empty().push(version);
            if self.route == Route::ScanProxy {
                path.push("scan-proxy");
            }
            path.extend(segments);
        }
        Ok(url)
    }

    /// Refuse a read the validator's scan-proxy does not serve.
    fn scan_only(&self, read: &str) -> Result<()> {
        if self.route == Route::ScanProxy {
            return Err(Error::InvalidRequest(format!(
                "a validator's scan-proxy does not serve {read}; ask a Scan for it \
                 (ScanClient::new)"
            )));
        }
        Ok(())
    }

    /// Refuse an internal endpoint of a Scan the caller did not opt into.
    fn internal(&self, read: &str) -> Result<()> {
        if self.route == Route::Scan && !self.internal_endpoints {
            return Err(Error::InvalidRequest(format!(
                "{read} is an endpoint Scan tags internal, which Splice may change in any \
                 release; opt in with ScanClient::with_internal_endpoints(), or go through a \
                 validator's scan-proxy (ScanClient::through_validator), which serves it as \
                 part of its API"
            )));
        }
        Ok(())
    }

    /// The party that represents the DSO.
    ///
    /// # Errors
    /// As any HTTP call, plus [`Error::UnexpectedResponse`] on a body that is
    /// not what Splice specifies.
    pub async fn dso_party_id(&self) -> Result<String> {
        #[derive(Deserialize)]
        struct Reply {
            dso_party_id: String,
        }
        let reply: Reply = self.get(self.url("v0", &["dso-party-id"])?).await?;
        Ok(reply.dso_party_id)
    }

    /// The DSO: its party, its voting threshold, and the contracts that hold
    /// its rules.
    ///
    /// Asked at `/v1/dso`, which Splice 0.10 added. A Scan or a validator
    /// older than that answers `404` there and serves `/v0/dso`, which
    /// carries the same contracts, so that is asked next.
    ///
    /// # Errors
    /// As [`dso_party_id`](Self::dso_party_id).
    pub async fn dso(&self) -> Result<DsoInfo> {
        match self.get(self.url("v1", &["dso"])?).await {
            Err(Error::Http { status: 404, .. }) => self.get(self.url("v0", &["dso"])?).await,
            answer => answer,
        }
    }

    /// Every Super Validator's Scan, per synchronizer.
    ///
    /// # Errors
    /// As [`dso_party_id`](Self::dso_party_id), and
    /// [`Error::InvalidRequest`] through a validator's scan-proxy, which does
    /// not serve it.
    pub async fn scans(&self) -> Result<Vec<SynchronizerScans>> {
        #[derive(Deserialize)]
        struct Reply {
            scans: Vec<SynchronizerScans>,
        }
        self.scan_only("the list of Scans")?;
        let reply: Reply = self.get(self.url("v0", &["scans"])?).await?;
        Ok(reply.scans)
    }

    /// The open and the issuing mining rounds.
    ///
    /// # Errors
    /// As [`dso_party_id`](Self::dso_party_id).
    pub async fn open_and_issuing_mining_rounds(&self) -> Result<MiningRounds> {
        // A Scan answers with only the rounds the caller does not say it has
        // cached, keyed by contract id; the proxy answers with them all.
        #[derive(Serialize)]
        struct Cached {
            cached_open_mining_round_contract_ids: [String; 0],
            cached_issuing_round_contract_ids: [String; 0],
        }
        #[derive(Deserialize)]
        struct MaybeCached {
            #[serde(default)]
            contract: Option<ScanContract>,
            #[serde(default)]
            domain_id: Option<String>,
        }
        #[derive(Deserialize)]
        struct FromScan {
            open_mining_rounds: BTreeMap<String, MaybeCached>,
            issuing_mining_rounds: BTreeMap<String, MaybeCached>,
        }
        #[derive(Deserialize)]
        struct FromProxy {
            open_mining_rounds: Vec<ContractWithState>,
            issuing_mining_rounds: Vec<ContractWithState>,
        }
        fn all(rounds: BTreeMap<String, MaybeCached>) -> Result<Vec<ContractWithState>> {
            rounds
                .into_iter()
                .map(|(contract_id, round)| {
                    let contract = round.contract.ok_or_else(|| {
                        Error::UnexpectedResponse(format!(
                            "Scan left out mining round `{contract_id}` as cached, where this \
                             client told it nothing was"
                        ))
                    })?;
                    Ok(ContractWithState {
                        contract,
                        synchronizer_id: round.domain_id,
                    })
                })
                .collect()
        }

        let url = self.url("v0", &["open-and-issuing-mining-rounds"])?;
        match self.route {
            Route::Scan => {
                let reply: FromScan = self
                    .post(
                        url,
                        &Cached {
                            cached_open_mining_round_contract_ids: [],
                            cached_issuing_round_contract_ids: [],
                        },
                    )
                    .await?;
                Ok(MiningRounds {
                    open: all(reply.open_mining_rounds)?,
                    issuing: all(reply.issuing_mining_rounds)?,
                })
            }
            Route::ScanProxy => {
                let reply: FromProxy = self.get(url).await?;
                Ok(MiningRounds {
                    open: reply.open_mining_rounds,
                    issuing: reply.issuing_mining_rounds,
                })
            }
        }
    }

    /// The open mining round to pass to a choice now: see
    /// [`MiningRounds::latest_open`].
    ///
    /// # Errors
    /// As [`open_and_issuing_mining_rounds`](Self::open_and_issuing_mining_rounds)
    /// and [`MiningRounds::latest_open`].
    pub async fn latest_open_mining_round(&self) -> Result<ContractWithState> {
        let rounds = self.open_and_issuing_mining_rounds().await?;
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .ok()
            .and_then(|since| i64::try_from(since.as_micros()).ok())
            .ok_or_else(|| {
                Error::InvalidRequest("the system clock is not a time after 1970".to_string())
            })?;
        rounds.latest_open(Timestamp(now)).cloned()
    }

    /// The closed mining rounds that have not been archived yet.
    ///
    /// # Errors
    /// As [`scans`](Self::scans).
    pub async fn closed_rounds(&self) -> Result<Vec<ScanContract>> {
        #[derive(Deserialize)]
        struct Reply {
            rounds: Vec<ScanContract>,
        }
        self.scan_only("the closed mining rounds")?;
        let reply: Reply = self.get(self.url("v0", &["closed-rounds"])?).await?;
        Ok(reply.rounds)
    }

    /// The `AmuletRules` contract.
    ///
    /// A Scan publishes it twice: with the rest of the DSO's contracts in the
    /// external API ([`dso`](Self::dso)), and alone at an internal endpoint.
    /// The external one is asked, unless the client opted into the internal
    /// endpoints ([`with_internal_endpoints`](Self::with_internal_endpoints)),
    /// whose reply is the one contract. A validator's scan-proxy serves it as
    /// part of its own API.
    ///
    /// # Errors
    /// As [`dso_party_id`](Self::dso_party_id).
    pub async fn amulet_rules(&self) -> Result<ContractWithState> {
        #[derive(Deserialize)]
        struct MaybeCached {
            #[serde(default)]
            contract: Option<ScanContract>,
            #[serde(default)]
            domain_id: Option<String>,
        }
        #[derive(Deserialize)]
        struct FromScan {
            amulet_rules_update: MaybeCached,
        }
        #[derive(Deserialize)]
        struct FromProxy {
            amulet_rules: ContractWithState,
        }
        if self.route == Route::Scan && !self.internal_endpoints {
            return Ok(self.dso().await?.amulet_rules);
        }
        let url = self.url("v0", &["amulet-rules"])?;
        match self.route {
            Route::Scan => {
                // The body names the contract the caller has cached, so that
                // Scan can leave it out. None is named here.
                let reply: FromScan = self.post(url, &serde_json::json!({})).await?;
                let contract = reply.amulet_rules_update.contract.ok_or_else(|| {
                    Error::UnexpectedResponse(
                        "Scan left out the Amulet rules as cached, where this client told it \
                         nothing was"
                            .to_string(),
                    )
                })?;
                Ok(ContractWithState {
                    contract,
                    synchronizer_id: reply.amulet_rules_update.domain_id,
                })
            }
            Route::ScanProxy => {
                let reply: FromProxy = self.get(url).await?;
                Ok(reply.amulet_rules)
            }
        }
    }

    /// A provider's `FeaturedAppRight` contract, or `None` if the provider is
    /// not a featured application.
    ///
    /// The contract comes without a synchronizer: disclose it on the one the
    /// Amulet rules are on ([`ScanContract::disclosed`]).
    ///
    /// On a Scan this is an internal endpoint: see
    /// [`with_internal_endpoints`](Self::with_internal_endpoints).
    ///
    /// # Errors
    /// As [`dso_party_id`](Self::dso_party_id), and
    /// [`Error::InvalidRequest`] on a Scan without the opt-in.
    pub async fn featured_app_right(&self, provider: &str) -> Result<Option<ScanContract>> {
        #[derive(Deserialize)]
        struct Reply {
            #[serde(default)]
            featured_app_right: Option<ScanContract>,
        }
        self.internal("a featured application right")?;
        let reply: Reply = self
            .get(self.url("v0", &["featured-apps", provider])?)
            .await?;
        Ok(reply.featured_app_right)
    }

    /// The `TransferPreapproval` contract that lets `party` receive Amulet
    /// without accepting each transfer, or `None` if it has none.
    ///
    /// On a Scan this is an internal endpoint: see
    /// [`with_internal_endpoints`](Self::with_internal_endpoints).
    ///
    /// # Errors
    /// As [`featured_app_right`](Self::featured_app_right).
    pub async fn transfer_preapproval_by_party(
        &self,
        party: &str,
    ) -> Result<Option<ContractWithState>> {
        #[derive(Deserialize)]
        struct Reply {
            transfer_preapproval: ContractWithState,
        }
        self.internal("a transfer preapproval")?;
        let url = self.url("v0", &["transfer-preapprovals", "by-party", party])?;
        let reply: Option<Reply> = self.get_optional(url).await?;
        Ok(reply.map(|reply| reply.transfer_preapproval))
    }

    /// How much traffic `member` has bought and used on a synchronizer. A
    /// member is a participant or a mediator, by its id (`PAR::…`, `MED::…`).
    ///
    /// # Errors
    /// As [`scans`](Self::scans).
    pub async fn member_traffic_status(
        &self,
        synchronizer_id: &str,
        member_id: &str,
    ) -> Result<TrafficStatus> {
        #[derive(Deserialize)]
        struct Reply {
            traffic_status: Status,
        }
        #[derive(Deserialize)]
        struct Status {
            actual: Actual,
            target: Target,
        }
        #[derive(Deserialize)]
        struct Actual {
            total_consumed: i64,
            total_limit: i64,
        }
        #[derive(Deserialize)]
        struct Target {
            total_purchased: i64,
        }
        self.scan_only("a member's traffic status")?;
        let url = self.url(
            "v0",
            &[
                "domains",
                synchronizer_id,
                "members",
                member_id,
                "traffic-status",
            ],
        )?;
        let reply: Reply = self.get(url).await?;
        Ok(TrafficStatus {
            total_consumed: reply.traffic_status.actual.total_consumed,
            total_limit: reply.traffic_status.actual.total_limit,
            total_purchased: reply.traffic_status.target.total_purchased,
        })
    }

    /// The Amulet Name Service entry of `party`, or `None` if it has none.
    ///
    /// # Errors
    /// As [`dso_party_id`](Self::dso_party_id).
    pub async fn ans_entry_by_party(&self, party: &str) -> Result<Option<AnsEntry>> {
        self.ans_entry(&["ans-entries", "by-party", party]).await
    }

    /// The Amulet Name Service entry for `name`, or `None` if there is none.
    ///
    /// # Errors
    /// As [`dso_party_id`](Self::dso_party_id).
    pub async fn ans_entry_by_name(&self, name: &str) -> Result<Option<AnsEntry>> {
        self.ans_entry(&["ans-entries", "by-name", name]).await
    }

    async fn ans_entry(&self, segments: &[&str]) -> Result<Option<AnsEntry>> {
        #[derive(Deserialize)]
        struct Reply {
            entry: AnsEntry,
        }
        let reply: Option<Reply> = self.get_optional(self.url("v0", segments)?).await?;
        Ok(reply.map(|reply| reply.entry))
    }

    /// Up to `page_size` unexpired Amulet Name Service entries, those whose
    /// name starts with `name_prefix` where one is given.
    ///
    /// # Errors
    /// As [`dso_party_id`](Self::dso_party_id).
    pub async fn ans_entries(
        &self,
        name_prefix: Option<&str>,
        page_size: u32,
    ) -> Result<Vec<AnsEntry>> {
        #[derive(Deserialize)]
        struct Reply {
            entries: Vec<AnsEntry>,
        }
        let mut url = self.url("v0", &["ans-entries"])?;
        {
            let mut query = url.query_pairs_mut();
            if let Some(name_prefix) = name_prefix {
                query.append_pair("name_prefix", name_prefix);
            }
            query.append_pair("page_size", &page_size.to_string());
        }
        let reply: Reply = self.get(url).await?;
        Ok(reply.entries)
    }

    /// Up to `page_size` updates after `after`, oldest first, or from the
    /// beginning of history where `after` is `None`.
    ///
    /// The last update's [`position`](ScanUpdate::position) is the `after` of
    /// the next page, and fewer than `page_size` updates means the end of
    /// what Scan has so far. Every Scan returns the same updates in the same
    /// order.
    ///
    /// # Errors
    /// As [`scans`](Self::scans).
    pub async fn updates(
        &self,
        after: Option<&HistoryPosition>,
        page_size: u32,
    ) -> Result<Vec<ScanUpdate>> {
        #[derive(Deserialize)]
        struct Reply {
            transactions: Vec<ScanUpdate>,
        }
        self.scan_only("the update history")?;
        let reply: Reply = self
            .post(
                self.url("v2", &["updates"])?,
                &history_request(after, page_size)?,
            )
            .await?;
        Ok(reply.transactions)
    }

    /// The update with this id, or `None` if Scan has none.
    ///
    /// # Errors
    /// As [`scans`](Self::scans).
    pub async fn update(&self, update_id: &str) -> Result<Option<ScanUpdate>> {
        self.scan_only("an update")?;
        self.get_optional(self.url("v2", &["updates", update_id])?)
            .await
    }

    /// Up to `page_size` entries of the event history after `after`: each
    /// update with the mediator's verdict on it. Paged as
    /// [`updates`](Self::updates), from the last entry's
    /// [`position`](ScanEventItem::position), which an entry has also when it
    /// is a verdict with no update.
    ///
    /// # Errors
    /// As [`scans`](Self::scans).
    pub async fn events(
        &self,
        after: Option<&HistoryPosition>,
        page_size: u32,
    ) -> Result<Vec<ScanEventItem>> {
        #[derive(Deserialize)]
        struct Reply {
            events: Vec<ScanEventItem>,
        }
        self.scan_only("the event history")?;
        let reply: Reply = self
            .post(
                self.url("v0", &["events"])?,
                &history_request(after, page_size)?,
            )
            .await?;
        Ok(reply.events)
    }

    /// The record time of the most recent snapshot of the active contracts
    /// taken before `before` (an RFC 3339 time) in `migration_id`, or `None`
    /// if there is none.
    ///
    /// # Errors
    /// As [`scans`](Self::scans).
    pub async fn acs_snapshot_timestamp(
        &self,
        before: &str,
        migration_id: i64,
    ) -> Result<Option<String>> {
        #[derive(Deserialize)]
        struct Reply {
            record_time: String,
        }
        self.scan_only("the snapshot times")?;
        let mut url = self.url("v0", &["state", "acs", "snapshot-timestamp"])?;
        url.query_pairs_mut()
            .append_pair("before", before)
            .append_pair("migration_id", &migration_id.to_string());
        let reply: Option<Reply> = self.get_optional(url).await?;
        Ok(reply.map(|reply| reply.record_time))
    }

    /// A page of a snapshot of the active contracts.
    ///
    /// Splice 0.7.5 and later: an older Scan has no `/v2/state/acs` and
    /// answers `404`.
    ///
    /// # Errors
    /// As [`scans`](Self::scans), and [`Error::InvalidRequest`] for a page
    /// size Scan does not allow.
    pub async fn acs_snapshot(&self, request: &SnapshotRequest) -> Result<AcsSnapshot> {
        self.scan_only("the snapshots of the active contracts")?;
        let mut body = serde_json::json!({
            "migration_id": request.migration_id,
            "record_time": request.record_time,
            "record_time_match": request.record_time_match(),
            "page_size": page_size(request.page_size)?,
        });
        if let Some(after) = &request.after {
            set(&mut body, "after", Value::from(after.as_str()));
        }
        if !request.parties.is_empty() {
            set(&mut body, "party_ids", Value::from(request.parties.clone()));
        }
        if !request.templates.is_empty() {
            set(
                &mut body,
                "templates",
                Value::from(request.templates.clone()),
            );
        }
        self.post(self.url("v2", &["state", "acs"])?, &body).await
    }

    /// A page of the Amulet and locked Amulet contracts the request's parties
    /// owned at a snapshot. Splice 0.7.5 and later, as
    /// [`acs_snapshot`](Self::acs_snapshot).
    ///
    /// # Errors
    /// As [`acs_snapshot`](Self::acs_snapshot), and
    /// [`Error::InvalidRequest`] if the request names no party.
    pub async fn holdings_state(&self, request: &SnapshotRequest) -> Result<AcsSnapshot> {
        self.scan_only("the holdings at a snapshot")?;
        let mut body = serde_json::json!({
            "migration_id": request.migration_id,
            "record_time": request.record_time,
            "record_time_match": request.record_time_match(),
            "page_size": page_size(request.page_size)?,
            "owner_party_ids": request.owners("a holdings read")?,
        });
        if let Some(after) = &request.after {
            set(&mut body, "after", Value::from(after.as_str()));
        }
        self.post(self.url("v2", &["holdings", "state"])?, &body)
            .await
    }

    /// The Amulet totals the request's parties held at a snapshot. The page
    /// size, page token and templates of the request are not used. Splice
    /// 0.6.3 and later.
    ///
    /// # Errors
    /// As [`dso_party_id`](Self::dso_party_id), and
    /// [`Error::InvalidRequest`] if the request names no party.
    pub async fn holdings_summary(&self, request: &SnapshotRequest) -> Result<HoldingsSummaries> {
        let body = serde_json::json!({
            "migration_id": request.migration_id,
            "record_time": request.record_time,
            "record_time_match": request.record_time_match(),
            "owner_party_ids": request.owners("a holdings summary")?,
        });
        self.post(self.url("v1", &["holdings", "summary"])?, &body)
            .await
    }

    /// `GET` a path this client has no method for, and return the JSON as it
    /// came. `version` is the first path segment (`v0`, `v1`, …).
    ///
    /// For the rest of Splice's API: the same transport as every other call
    /// here (the token, no redirects, a bounded reply, encoded segments) and
    /// nothing checked about what comes back. Through a validator the path is
    /// taken under its `scan-proxy` prefix.
    ///
    /// # Errors
    /// As any HTTP call.
    pub async fn get_json(
        &self,
        version: &str,
        segments: &[&str],
        query: &[(&str, &str)],
    ) -> Result<Value> {
        let mut url = self.url(version, segments)?;
        if !query.is_empty() {
            url.query_pairs_mut().extend_pairs(query);
        }
        self.get(url).await
    }

    /// `POST` a JSON body to a path this client has no method for, and return
    /// the JSON as it came. See [`get_json`](Self::get_json).
    ///
    /// # Errors
    /// As any HTTP call.
    pub async fn post_json(&self, version: &str, segments: &[&str], body: &Value) -> Result<Value> {
        self.post(self.url(version, segments)?, body).await
    }

    /// Send a request with the bearer token, when one is configured.
    async fn send(&self, request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
        let request = match self.auth.bearer().await? {
            Some(token) => request.bearer_auth(token),
            None => request,
        };
        request.send().await.map_err(|e| transport_error(e, SCAN))
    }

    async fn get<T: serde::de::DeserializeOwned>(&self, url: reqwest::Url) -> Result<T> {
        let response = self.send(self.http.get(url.clone())).await?;
        decode(response, url.as_str()).await
    }

    async fn post<T: serde::de::DeserializeOwned>(
        &self,
        url: reqwest::Url,
        body: &(impl Serialize + ?Sized),
    ) -> Result<T> {
        let response = self.send(self.http.post(url.clone()).json(body)).await?;
        decode(response, url.as_str()).await
    }

    /// A `GET` whose `404` is an answer: there is no such entry.
    ///
    /// Only when Scan is the one answering. A `404` whose body is not JSON did
    /// not come from a Scan handler: it is a base URL one path component off,
    /// or a gateway in front of one, and reporting that as "no such entry"
    /// would be a steady, quiet `None` where every other call fails loudly.
    async fn get_optional<T: serde::de::DeserializeOwned>(
        &self,
        url: reqwest::Url,
    ) -> Result<Option<T>> {
        let response = self.send(self.http.get(url.clone())).await?;
        if response.status() == reqwest::StatusCode::NOT_FOUND {
            let body = read_error_body(response, url.as_str(), SCAN).await?;
            let trimmed = body.trim();
            if serde_json::from_str::<ErrorResponse>(trimmed).is_ok() {
                return Ok(None);
            }
            tracing::warn!(%url, "a 404 that is not Scan's own — check the base URL");
            return Err(Error::http_at(404, truncate(trimmed), url.as_str()));
        }
        Ok(Some(decode(response, url.as_str()).await?))
    }
}

/// The HTTP client every [`ScanClient`] starts from: a deadline, and no
/// redirects.
fn default_http_client() -> Result<reqwest::Client> {
    reqwest::Client::builder()
        .timeout(DEFAULT_TIMEOUT)
        .redirect(reqwest::redirect::Policy::none())
        .build()
        .map_err(|e| Error::InvalidRequest(format!("cannot build an HTTP client: {e}")))
}

fn page_size(page_size: u32) -> Result<u32> {
    if page_size == 0 || page_size > MAX_PAGE_SIZE {
        return Err(Error::InvalidRequest(format!(
            "a page of {page_size} is not one Scan serves: it takes 1 to {MAX_PAGE_SIZE}"
        )));
    }
    Ok(page_size)
}

/// Set a field of a JSON object built on the line above. `object[key] = …`
/// does the same and panics when `object` is not an object.
fn set(object: &mut Value, key: &str, value: Value) {
    if let Some(fields) = object.as_object_mut() {
        fields.insert(key.to_string(), value);
    }
}

/// The body of a history read: where to read on from, and how much.
fn history_request(after: Option<&HistoryPosition>, size: u32) -> Result<Value> {
    let mut body = serde_json::json!({ "page_size": page_size(size)? });
    if let Some(after) = after {
        let after = serde_json::to_value(after).map_err(|e| {
            Error::InvalidRequest(format!("a history position that is not JSON: {e}"))
        })?;
        set(&mut body, "after", after);
    }
    Ok(body)
}

/// One place where a non-success status becomes an error and a body becomes a
/// value, so every call reports failure the same way.
async fn decode<T: serde::de::DeserializeOwned>(
    response: reqwest::Response,
    url: &str,
) -> Result<T> {
    let status = response.status();
    let body = if status.is_success() {
        read_body_from(response, MAX_RESPONSE_BYTES, url, SCAN).await?
    } else {
        read_error_body(response, url, SCAN).await?
    };
    if !status.is_success() {
        // Splice's error body is `{"error": "…"}`; anything else is passed
        // through. The body stays the body, since the shared error model
        // reads it; the URL is in the log line.
        tracing::warn!(%url, status = status.as_u16(), "Scan answered with an error");
        let message = serde_json::from_str::<ErrorResponse>(&body)
            .map(|e| e.error)
            .unwrap_or(body);
        return Err(Error::http_at(status.as_u16(), truncate(&message), url));
    }
    serde_json::from_str(&body).map_err(|e| {
        Error::UnexpectedResponse(format!(
            "{url} returned a body this does not understand: {e}"
        ))
    })
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    fn contract(contract_id: &str, payload: Value) -> ContractWithState {
        ContractWithState {
            contract: ScanContract {
                template_id: "pkg:Splice.Round:OpenMiningRound".to_string(),
                contract_id: contract_id.to_string(),
                payload,
                created_event_blob: base64::engine::general_purpose::STANDARD.encode([1, 2, 3]),
                created_at: "2026-11-26T10:00:00Z".to_string(),
            },
            synchronizer_id: Some("global-domain::1220aa".to_string()),
        }
    }

    fn round(number: i64, opens_at: &str) -> ContractWithState {
        contract(
            &format!("00round{number}"),
            serde_json::json!({
                "round": { "number": number.to_string() },
                "opensAt": opens_at,
                "targetClosesAt": "2026-11-26T12:00:00Z",
            }),
        )
    }

    fn at(time: &str) -> Timestamp {
        serde_json::from_value(Value::from(time)).unwrap()
    }

    /// Several rounds are open at once and the newest has usually not reached
    /// its opening time. The one to use is the highest that has: the lowest
    /// is archived next, and the one above the highest is not usable yet.
    #[test]
    fn the_latest_open_round_is_the_highest_one_whose_opening_time_has_come() {
        let rounds = MiningRounds {
            open: vec![
                round(11, "2026-11-26T10:10:00.000000Z"),
                round(10, "2026-11-26T10:00:00.000000Z"),
                round(12, "2026-11-26T10:20:00.000000Z"),
            ],
            issuing: Vec::new(),
        };
        let pick = |now: &str| {
            rounds
                .latest_open(at(now))
                .map(|round| round.contract.contract_id.clone())
        };
        assert_eq!(pick("2026-11-26T10:15:00Z").unwrap(), "00round11");
        assert_eq!(pick("2026-11-26T10:05:00Z").unwrap(), "00round10");
        // The opening time itself counts as open.
        assert_eq!(pick("2026-11-26T10:20:00Z").unwrap(), "00round12");
        assert_eq!(pick("2026-11-27T00:00:00Z").unwrap(), "00round12");

        // Before any round opens there is none, and that passes by itself.
        let error = pick("2026-11-26T09:00:00Z").unwrap_err();
        assert!(error.is_retriable(), "{error:?}");
        assert!(
            error.to_string().contains("3 open mining rounds"),
            "{error}"
        );
    }

    /// A round number is a Daml `Int`, which JSON writes as a string, and
    /// compares as a number: round 9 is before round 10.
    #[test]
    fn round_numbers_compare_as_numbers_whether_written_as_text_or_not() {
        let mut as_number = round(9, "2026-11-26T09:50:00Z");
        as_number.contract.payload["round"]["number"] = Value::from(9);
        let rounds = MiningRounds {
            open: vec![round(10, "2026-11-26T10:00:00Z"), as_number],
            issuing: Vec::new(),
        };
        let latest = rounds.latest_open(at("2026-11-26T11:00:00Z")).unwrap();
        assert_eq!(latest.contract.contract_id, "00round10");
    }

    #[test]
    fn a_round_that_is_not_an_open_mining_round_is_an_unexpected_response() {
        let rounds = MiningRounds {
            open: vec![contract("00odd", serde_json::json!({ "round": {} }))],
            issuing: Vec::new(),
        };
        let error = rounds.latest_open(at("2026-11-26T11:00:00Z")).unwrap_err();
        assert!(matches!(error, Error::UnexpectedResponse(_)), "{error:?}");
        assert!(error.to_string().contains("00odd"), "{error}");
    }

    /// gRPC takes the blob as bytes and the JSON API as the base64 Scan sent,
    /// and both name the synchronizer the contract is on.
    #[test]
    fn a_contract_is_disclosed_as_bytes_for_grpc_and_base64_for_json() {
        let rules = contract("00rules", serde_json::json!({}));
        let disclosed = rules.disclosed().unwrap();
        assert_eq!(disclosed.contract_id, "00rules");
        assert_eq!(disclosed.created_event_blob, [1, 2, 3]);
        assert_eq!(disclosed.synchronizer_id, "global-domain::1220aa");
        let template = disclosed.template_id.unwrap();
        assert_eq!(
            (
                template.package_id.as_str(),
                template.module_name.as_str(),
                template.entity_name.as_str()
            ),
            ("pkg", "Splice.Round", "OpenMiningRound")
        );

        let json = rules.disclosed_json().unwrap();
        assert_eq!(json["createdEventBlob"], "AQID");
        assert_eq!(json["synchronizerId"], "global-domain::1220aa");
        assert_eq!(json["templateId"], "pkg:Splice.Round:OpenMiningRound");
    }

    #[test]
    fn a_contract_that_cannot_be_disclosed_says_why() {
        let mut in_transit = contract("00moving", serde_json::json!({}));
        in_transit.synchronizer_id = None;
        let error = in_transit.disclosed().unwrap_err();
        assert!(error.is_retriable(), "{error:?}");
        assert!(in_transit.disclosed_json().is_err());

        for template_id in ["pkg:Module", "pkg:Module:Entity:Extra", "::", ""] {
            let mut odd = contract("00odd", serde_json::json!({}));
            odd.contract.template_id = template_id.to_string();
            let error = odd.disclosed().unwrap_err();
            assert!(matches!(error, Error::UnexpectedResponse(_)), "{error:?}");
        }

        let mut odd = contract("00blob", serde_json::json!({}));
        odd.contract.created_event_blob = "not base64!!".to_string();
        let error = odd.disclosed().unwrap_err();
        assert!(error.to_string().contains("00blob"), "{error}");
    }

    /// The two kinds of update carry no tag: a transaction is the one with
    /// events, a reassignment the one with an event.
    #[test]
    fn updates_are_told_apart_by_their_fields() {
        let transaction: ScanUpdate = serde_json::from_value(serde_json::json!({
            "update_id": "1220tx",
            "migration_id": 4,
            "workflow_id": "",
            "record_time": "2026-11-26T10:00:00.000000Z",
            "synchronizer_id": "global-domain::1220aa",
            "effective_at": "2026-11-26T09:59:59.000000Z",
            "root_event_ids": ["1220tx:0"],
            "events_by_id": {
                "1220tx:0": {
                    "event_type": "exercised_event",
                    "event_id": "1220tx:0",
                    "contract_id": "00rules",
                    "template_id": "pkg:Splice.AmuletRules:AmuletRules",
                    "package_name": "splice-amulet",
                    "choice": "AmuletRules_Transfer",
                    "choice_argument": {},
                    "child_event_ids": ["1220tx:1"],
                    "exercise_result": {},
                    "consuming": false,
                    "acting_parties": ["alice::1220"],
                },
                "1220tx:1": {
                    "event_type": "created_event",
                    "event_id": "1220tx:1",
                    "contract_id": "00amulet",
                    "template_id": "pkg:Splice.Amulet:Amulet",
                    "package_name": "splice-amulet",
                    "create_arguments": { "owner": "bob::1220" },
                    "created_at": "2026-11-26T09:59:59.000000Z",
                    "signatories": ["DSO::1220"],
                    "observers": [],
                },
            },
        }))
        .unwrap();
        assert_eq!(transaction.update_id(), "1220tx");
        assert_eq!(
            transaction.position(),
            HistoryPosition {
                migration_id: 4,
                record_time: "2026-11-26T10:00:00.000000Z".to_string(),
            }
        );
        let ScanUpdate::Transaction(transaction) = transaction else {
            panic!("a transaction");
        };
        assert!(matches!(
            transaction.events_by_id["1220tx:0"],
            ScanEvent::Exercised(_)
        ));
        assert!(matches!(
            transaction.events_by_id["1220tx:1"],
            ScanEvent::Created(_)
        ));

        let unassigned: ScanUpdate = serde_json::from_value(serde_json::json!({
            "update_id": "1220out",
            "offset": "000a",
            "record_time": "2026-11-26T10:01:00.000000Z",
            "event": {
                "submitter": "alice::1220",
                "source_synchronizer": "one::1220",
                "target_synchronizer": "two::1220",
                "migration_id": 5,
                "unassign_id": "u1",
                "reassignment_counter": 1,
                "contract_id": "00moving",
            },
        }))
        .unwrap();
        assert_eq!(unassigned.position().migration_id, 5);
        assert!(matches!(
            unassigned,
            ScanUpdate::Reassignment(ScanReassignment {
                event: ReassignmentEvent::Unassignment(_),
                ..
            })
        ));

        let neither = serde_json::from_value::<ScanUpdate>(serde_json::json!({
            "update_id": "1220odd",
        }))
        .unwrap_err();
        assert!(neither.to_string().contains("events_by_id"), "{neither}");
    }

    /// The last entry of an event page is what the next page is read from,
    /// and it can be a verdict with no update: a transaction the mediator
    /// rejected. Its position comes from the verdict then.
    #[test]
    fn an_event_entry_has_a_position_with_or_without_an_update() {
        let rejected: ScanEventItem = serde_json::from_value(serde_json::json!({
            "update": null,
            "verdict": {
                "update_id": "1220rejected",
                "migration_id": 4,
                "domain_id": "global-domain::1220aa",
                "record_time": "2026-11-26T10:02:00.000000Z",
                "finalization_time": "2026-11-26T10:02:01.000000Z",
                "submitting_parties": ["alice::1220"],
                "submitting_participant_uid": "participant::1220",
                "verdict_result": "VERDICT_RESULT_REJECTED",
                "mediator_group": 0,
                "transaction_views": { "views": [], "root_views": [] },
            },
            "asset_activity_records": [],
            "submitting_app_provider": "provider::1220",
        }))
        .unwrap();
        assert!(rejected.update.is_none());
        assert_eq!(
            rejected.position(),
            Some(HistoryPosition {
                migration_id: 4,
                record_time: "2026-11-26T10:02:00.000000Z".to_string(),
            })
        );
        assert_eq!(
            rejected.submitting_app_provider.as_deref(),
            Some("provider::1220")
        );
        assert!(rejected.asset_activity_records.is_some());

        let empty: ScanEventItem = serde_json::from_value(serde_json::json!({})).unwrap();
        assert_eq!(empty.position(), None);
    }

    /// A path argument that URL parsing would resolve away is refused before
    /// anything is sent, on either route.
    #[tokio::test]
    async fn a_dot_or_empty_path_segment_is_refused_before_sending() {
        let direct = ScanClient::new("http://127.0.0.1:1/api/scan")
            .unwrap()
            .with_internal_endpoints();
        let proxy = ScanClient::through_validator("http://127.0.0.1:1/api/validator").unwrap();
        for client in [direct, proxy] {
            // URL parsing drops tab, CR and LF, so `.\t` was `.` and `.\t.`
            // was `..` by the time the request went out.
            for id in ["..", ".", "", ".\t", ".\t.", "..\r", "\n.."] {
                let error = client.featured_app_right(id).await.unwrap_err();
                assert!(
                    matches!(error, Error::InvalidRequest(_)),
                    "{id:?}: {error:?}"
                );
                let error = client.ans_entry_by_party(id).await.unwrap_err();
                assert!(
                    matches!(error, Error::InvalidRequest(_)),
                    "{id:?}: {error:?}"
                );
            }
            // The API version is a segment like the others.
            for version in ["", ".", "..", ".\t."] {
                assert!(
                    matches!(client.url(version, &["dso"]), Err(Error::InvalidRequest(_))),
                    "{version:?}"
                );
            }
        }
    }

    /// A base URL with a query or a fragment is refused on either route, and
    /// a trailing backslash is trimmed like a slash.
    #[test]
    fn the_base_url_has_no_query_or_fragment_and_is_trimmed() {
        for base in [
            "https://s.example/api/scan?apikey=abc",
            "https://s.example/api/scan#x",
        ] {
            assert!(
                matches!(ScanClient::new(base), Err(Error::InvalidRequest(_))),
                "{base}"
            );
            assert!(
                matches!(
                    ScanClient::through_validator(base),
                    Err(Error::InvalidRequest(_))
                ),
                "{base}"
            );
        }
        let client = ScanClient::new(r"https://s.example/api/scan\").unwrap();
        assert_eq!(
            client.url("v0", &["dso"]).unwrap().as_str(),
            "https://s.example/api/scan/v0/dso"
        );
    }

    /// The position serializes as the `after` object the history reads take.
    #[test]
    fn a_history_position_is_the_after_of_the_next_page() {
        let after = HistoryPosition {
            migration_id: 4,
            record_time: "2026-11-26T10:00:00.000000Z".to_string(),
        };
        assert_eq!(
            history_request(Some(&after), 50).unwrap(),
            serde_json::json!({
                "page_size": 50,
                "after": {
                    "after_migration_id": 4,
                    "after_record_time": "2026-11-26T10:00:00.000000Z",
                },
            })
        );
        assert_eq!(
            history_request(None, 1).unwrap(),
            serde_json::json!({ "page_size": 1 })
        );
        for size in [0, 1001] {
            let error = history_request(None, size).unwrap_err();
            assert!(matches!(error, Error::InvalidRequest(_)), "{error:?}");
        }
    }
}