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
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
//! Unified hosting and subscription management.
//!
//! # Architecture Overview
//!
//! This module manages contract hosting (which contracts a peer keeps available) and
//! subscription state (which contracts a peer is actively interested in).
//!
//! ## Key Design (2026-01 Unified Hosting Refactor)
//!
//! This module unifies the previously separate "hosting" and "GET subscription" caches
//! into a single `HostingCache` that serves as the source of truth for which contracts
//! this peer is hosting.
//!
//! 1. **Hosting ≠automatic subscription renewal**: Hosted contracts are cached
//! locally but only contracts with active client subscriptions, downstream
//! subscribers, OR the `local_client_access` flag (#3769) get their
//! subscriptions renewed. Relay-cached contracts (no local interest) serve
//! as a recovery mechanism (last-resort data source) only.
//!
//! 2. **Subscriptions are lease-based**: Active subscriptions have a lease that expires
//! unless renewed. Clients must re-subscribe periodically (every ~2 minutes).
//!
//! 3. **Single cache**: One `HostingCache` with byte-budget LRU and TTL protection.
//!
//! ## Data Flow
//!
//! - GET/PUT/SUBSCRIBE operations add contracts to the hosting cache
//! - Only locally-accessed or client-subscribed contracts get subscription renewal via `contracts_needing_renewal()`
//! - Active subscriptions prevent eviction from the hosting cache
//! - TTL protects recently accessed contracts from premature eviction
mod cache;
use crate::util::backoff::{ExponentialBackoff, TrackedBackoff};
use crate::util::time_source::{InstantTimeSrc, TimeSource};
pub use cache::{AccessType, RecordAccessResult};
use cache::{DEFAULT_HOSTING_BUDGET_BYTES, DEFAULT_MIN_TTL, HostingCache};
use dashmap::{DashMap, DashSet};
use freenet_stdlib::prelude::{ContractInstanceId, ContractKey};
use parking_lot::RwLock;
use std::collections::{HashMap, HashSet};
use std::time::Duration;
use tokio::time::Instant;
use tracing::{debug, info};
use super::interest::PeerKey;
// =============================================================================
// Constants
// =============================================================================
/// Renewal interval for subscriptions.
/// Clients should renew subscriptions at this interval to prevent expiry.
pub const SUBSCRIPTION_RENEWAL_INTERVAL: Duration = Duration::from_secs(120); // 2 minutes
/// Multiplier for lease duration relative to renewal interval.
/// Gives this many renewal attempts before subscription expires.
pub const LEASE_RENEWAL_MULTIPLIER: u32 = 4;
/// Subscription lease duration.
/// Subscriptions automatically expire after this duration unless renewed.
/// Computed as LEASE_RENEWAL_MULTIPLIER × SUBSCRIPTION_RENEWAL_INTERVAL.
pub const SUBSCRIPTION_LEASE_DURATION: Duration =
Duration::from_secs(SUBSCRIPTION_RENEWAL_INTERVAL.as_secs() * LEASE_RENEWAL_MULTIPLIER as u64); // 8 minutes
/// Initial backoff duration for subscription retries.
const INITIAL_SUBSCRIPTION_BACKOFF: Duration = Duration::from_secs(15);
/// Maximum backoff duration for subscription retries.
///
/// Computed as 1/4 of SUBSCRIPTION_LEASE_DURATION so that a contract in
/// max-backoff always retries well before its subscription expires.
const MAX_SUBSCRIPTION_BACKOFF: Duration =
Duration::from_secs(SUBSCRIPTION_LEASE_DURATION.as_secs() / 4); // 2 minutes
/// Maximum number of tracked subscription backoff entries.
const MAX_SUBSCRIPTION_BACKOFF_ENTRIES: usize = 4096;
/// Maximum number of downstream peer subscribers per contract.
/// Prevents network-level subscription amplification attacks.
const MAX_DOWNSTREAM_SUBSCRIBERS_PER_CONTRACT: usize = 512;
// =============================================================================
// Result Types
// =============================================================================
/// Result of adding a client subscription.
#[derive(Debug)]
pub struct AddClientSubscriptionResult {
/// Whether this was the first client for this contract.
pub is_first_client: bool,
}
/// Result of removing all subscriptions for a disconnected client.
#[derive(Debug)]
pub struct ClientDisconnectResult {
/// All contracts where this client had a subscription (for cleanup).
pub affected_contracts: Vec<ContractKey>,
}
/// Result of subscribing to a contract.
#[derive(Debug)]
#[allow(dead_code)] // Fields available for future telemetry/diagnostics
pub struct SubscribeResult {
/// Whether this is a new subscription (vs renewal).
pub is_new: bool,
/// When the subscription will expire.
pub expires_at: Instant,
}
// =============================================================================
// HostingManager
// =============================================================================
/// Manages contract hosting and subscription state.
///
/// # Subscription Model
///
/// Subscriptions are lease-based with automatic expiry:
/// - `subscribe()` creates or renews a subscription with a lease
/// - Subscriptions expire after `SUBSCRIPTION_LEASE_DURATION` (8 minutes)
/// - Clients must call `renew_subscription()` every `SUBSCRIPTION_RENEWAL_INTERVAL` (2 minutes)
/// - Expired subscriptions are removed by `expire_stale_subscriptions()`
///
/// # Hosting Model
///
/// Contracts are hosted based on access patterns:
/// - GET, PUT, SUBSCRIBE operations add contracts to the hosting cache
/// - Contracts with client or active subscriptions get renewal
/// - Active subscriptions and client subscriptions prevent eviction
/// - TTL protects recently accessed contracts from premature eviction
pub(crate) struct HostingManager {
/// Active subscriptions with lease expiry timestamps.
/// Key: ContractKey, Value: expiry timestamp
active_subscriptions: DashMap<ContractKey, Instant>,
/// Contracts where a local client (WebSocket) is actively subscribed.
/// Prevents hosting cache eviction while client subscriptions exist.
client_subscriptions: DashMap<ContractInstanceId, HashSet<crate::client_events::ClientId>>,
/// Unified hosting cache with byte-budget LRU and TTL protection.
/// This is the single source of truth for which contracts we're hosting.
hosting_cache: RwLock<HostingCache<InstantTimeSrc>>,
/// Downstream peers subscribed to contracts we host, with lease timestamps.
/// Drives `should_unsubscribe_upstream()` decisions.
///
/// Must be kept in sync with `InterestManager::interested_peers`
/// (see `InterestManager` docs for the dual-tracking relationship).
downstream_subscribers: DashMap<ContractKey, HashMap<PeerKey, Instant>>,
/// Time source for downstream subscriber lease tracking.
time_source: InstantTimeSrc,
/// Contracts with subscription requests currently in-flight.
pending_subscription_requests: DashSet<ContractKey>,
/// Exponential backoff state for subscription retries.
subscription_backoff: RwLock<TrackedBackoff<ContractKey>>,
/// Storage reference for persisting/removing hosting metadata.
/// Set after executor creation via `set_storage()`.
#[cfg(feature = "redb")]
storage: RwLock<Option<crate::contract::storages::Storage>>,
#[cfg(all(feature = "sqlite", not(feature = "redb")))]
storage: RwLock<Option<crate::contract::storages::Storage>>,
}
impl HostingManager {
pub fn new() -> Self {
let backoff_config =
ExponentialBackoff::new(INITIAL_SUBSCRIPTION_BACKOFF, MAX_SUBSCRIPTION_BACKOFF);
Self {
active_subscriptions: DashMap::new(),
client_subscriptions: DashMap::new(),
hosting_cache: RwLock::new(HostingCache::new(
DEFAULT_HOSTING_BUDGET_BYTES,
DEFAULT_MIN_TTL,
InstantTimeSrc::new(),
)),
downstream_subscribers: DashMap::new(),
time_source: InstantTimeSrc::new(),
pending_subscription_requests: DashSet::new(),
subscription_backoff: RwLock::new(TrackedBackoff::new(
backoff_config,
MAX_SUBSCRIPTION_BACKOFF_ENTRIES,
)),
storage: RwLock::new(None),
}
}
/// Set the storage reference for persisting hosting metadata.
/// Must be called after executor creation.
pub fn set_storage(&self, storage: crate::contract::storages::Storage) {
*self.storage.write() = Some(storage);
}
// =========================================================================
// Subscription Management (Lease-Based)
// =========================================================================
/// Subscribe to a contract with a lease.
///
/// Creates a new subscription or renews an existing one. The subscription
/// will expire after `SUBSCRIPTION_LEASE_DURATION` unless renewed.
pub fn subscribe(&self, contract: ContractKey) -> SubscribeResult {
let expires_at = self.time_source.now() + SUBSCRIPTION_LEASE_DURATION;
let is_new = self
.active_subscriptions
.insert(contract, expires_at)
.is_none();
debug!(
%contract,
is_new,
expires_in_secs = SUBSCRIPTION_LEASE_DURATION.as_secs(),
"subscribe: {} subscription",
if is_new { "created" } else { "renewed" }
);
SubscribeResult { is_new, expires_at }
}
/// Renew an existing subscription.
///
/// Extends the lease by `SUBSCRIPTION_LEASE_DURATION` from now.
/// Returns `true` if the subscription existed and was renewed.
#[allow(dead_code)] // Used in tests, may be used for explicit renewal in future
pub fn renew_subscription(&self, contract: &ContractKey) -> bool {
if let Some(mut entry) = self.active_subscriptions.get_mut(contract) {
*entry = self.time_source.now() + SUBSCRIPTION_LEASE_DURATION;
debug!(%contract, "renew_subscription: lease extended");
true
} else {
debug!(%contract, "renew_subscription: no active subscription to renew");
false
}
}
/// Unsubscribe from a contract.
///
/// Removes the active subscription. The contract may still be hosted
/// (in the hosting cache) until evicted by LRU.
pub fn unsubscribe(&self, contract: &ContractKey) {
if self.active_subscriptions.remove(contract).is_some() {
crate::node::network_status::record_subscription_removed(&format!("{contract}"));
debug!(%contract, "unsubscribe: removed active subscription");
}
}
/// Check if we have an active (non-expired) subscription to a contract.
pub fn is_subscribed(&self, contract: &ContractKey) -> bool {
self.active_subscriptions
.get(contract)
.map(|expires_at| *expires_at > self.time_source.now())
.unwrap_or(false)
}
/// Get all contracts with active subscriptions.
pub fn get_subscribed_contracts(&self) -> Vec<ContractKey> {
let now = self.time_source.now();
let mut contracts: Vec<ContractKey> = self
.active_subscriptions
.iter()
.filter(|entry| *entry.value() > now)
.map(|entry| *entry.key())
.collect();
// Sort for deterministic ordering (critical for simulation tests)
contracts.sort_by(|a, b| a.id().as_bytes().cmp(b.id().as_bytes()));
contracts
}
/// Expire stale subscriptions and return the contracts that were expired.
///
/// Should be called periodically by a background task.
/// Force-expire a contract's subscription so it gets renewed through the
/// current best route on the next recovery cycle. Used when a new closer
/// connection has been established (not just initiated).
pub fn force_subscription_renewal(&self, contract: &ContractKey) {
if self.active_subscriptions.remove(contract).is_some() {
tracing::info!(
%contract,
"force_subscription_renewal: expired subscription to trigger re-route"
);
}
}
pub fn expire_stale_subscriptions(&self) -> Vec<ContractKey> {
let now = self.time_source.now();
let mut expired = Vec::new();
// Collect expired subscriptions
self.active_subscriptions.retain(|contract, expires_at| {
if *expires_at <= now {
expired.push(*contract);
false
} else {
true
}
});
if !expired.is_empty() {
for contract in &expired {
crate::node::network_status::record_subscription_removed(&format!("{contract}"));
}
info!(
expired_count = expired.len(),
"expire_stale_subscriptions: expired stale subscriptions"
);
}
expired
}
/// Get the number of active subscriptions.
#[allow(dead_code)] // Used in tests, may be used for metrics in future
pub fn active_subscription_count(&self) -> usize {
let now = self.time_source.now();
self.active_subscriptions
.iter()
.filter(|entry| *entry.value() > now)
.count()
}
// =========================================================================
// Client Subscription Management
// =========================================================================
/// Register a client subscription for a contract (WebSocket client subscribed).
pub fn add_client_subscription(
&self,
instance_id: &ContractInstanceId,
client_id: crate::client_events::ClientId,
) -> AddClientSubscriptionResult {
let mut entry = self.client_subscriptions.entry(*instance_id).or_default();
let is_first_client = entry.is_empty();
entry.insert(client_id);
debug!(
contract = %instance_id,
%client_id,
is_first_client,
"add_client_subscription: registered"
);
AddClientSubscriptionResult { is_first_client }
}
/// Remove a client subscription.
/// Returns true if this was the last client subscription for this contract.
pub fn remove_client_subscription(
&self,
instance_id: &ContractInstanceId,
client_id: crate::client_events::ClientId,
) -> bool {
let mut no_more_subscriptions = false;
if let Some(mut clients) = self.client_subscriptions.get_mut(instance_id) {
clients.remove(&client_id);
if clients.is_empty() {
no_more_subscriptions = true;
}
}
if no_more_subscriptions {
self.client_subscriptions.remove(instance_id);
}
debug!(
contract = %instance_id,
%client_id,
no_more_subscriptions,
"remove_client_subscription: removed"
);
no_more_subscriptions
}
/// Check if there are any client subscriptions for a contract.
pub fn has_client_subscriptions(&self, instance_id: &ContractInstanceId) -> bool {
self.client_subscriptions
.get(instance_id)
.map(|clients| !clients.is_empty())
.unwrap_or(false)
}
/// Remove a client from ALL its subscriptions (used when client disconnects).
pub fn remove_client_from_all_subscriptions(
&self,
client_id: crate::client_events::ClientId,
) -> ClientDisconnectResult {
let mut affected_contracts = Vec::new();
// Find all contracts where this client is subscribed
// Sort for deterministic iteration order
let mut instance_ids_with_client: Vec<ContractInstanceId> = self
.client_subscriptions
.iter()
.filter(|entry| entry.value().contains(&client_id))
.map(|entry| *entry.key())
.collect();
instance_ids_with_client.sort_by(|a, b| a.as_bytes().cmp(b.as_bytes()));
for instance_id in instance_ids_with_client {
self.remove_client_subscription(&instance_id, client_id);
// Find matching ContractKey in active_subscriptions
if let Some(contract) = self
.active_subscriptions
.iter()
.find(|entry| *entry.key().id() == instance_id)
.map(|entry| *entry.key())
{
affected_contracts.push(contract);
}
}
debug!(
%client_id,
affected_count = affected_contracts.len(),
"remove_client_from_all_subscriptions: removed"
);
ClientDisconnectResult { affected_contracts }
}
// =========================================================================
// Downstream Subscriber Tracking
// =========================================================================
/// Record that a downstream peer is subscribed to a contract we host.
/// Returns false if the downstream subscriber limit for this contract has been reached
/// and the peer is not already tracked (existing peers can always renew).
pub fn add_downstream_subscriber(&self, contract: &ContractKey, peer: PeerKey) -> bool {
let mut entry = self.downstream_subscribers.entry(*contract).or_default();
if !entry.contains_key(&peer) && entry.len() >= MAX_DOWNSTREAM_SUBSCRIBERS_PER_CONTRACT {
tracing::warn!(
contract = %contract,
limit = MAX_DOWNSTREAM_SUBSCRIBERS_PER_CONTRACT,
"Downstream subscriber limit reached, rejecting peer"
);
return false;
}
entry.insert(peer, self.time_source.now());
true
}
/// Renew a downstream peer's subscription lease.
/// Returns false if the peer is not currently tracked.
#[allow(dead_code)] // Only used in tests
pub fn renew_downstream_subscriber(&self, contract: &ContractKey, peer: &PeerKey) -> bool {
if let Some(mut peers) = self.downstream_subscribers.get_mut(contract) {
if peers.contains_key(peer) {
peers.insert(peer.clone(), self.time_source.now());
return true;
}
}
false
}
/// Remove a downstream peer's subscription for a contract.
/// Returns true if the peer was found and removed.
pub fn remove_downstream_subscriber(&self, contract: &ContractKey, peer: &PeerKey) -> bool {
let mut removed = false;
if let Some(mut peers) = self.downstream_subscribers.get_mut(contract) {
removed = peers.remove(peer).is_some();
}
if removed {
// Remove the map entry if no peers remain
self.downstream_subscribers
.remove_if(contract, |_, peers| peers.is_empty());
}
removed
}
/// Check whether any downstream peers are subscribed to this contract.
pub fn has_downstream_subscribers(&self, contract: &ContractKey) -> bool {
self.downstream_subscribers
.get(contract)
.is_some_and(|peers| !peers.is_empty())
}
/// Remove downstream subscribers whose leases have expired.
/// Returns each affected contract paired with the number of expired peers.
pub fn expire_stale_downstream_subscribers(&self) -> Vec<(ContractKey, usize)> {
let now = self.time_source.now();
let mut expired_counts = Vec::new();
let keys: Vec<ContractKey> = self
.downstream_subscribers
.iter()
.map(|entry| *entry.key())
.collect();
for key in keys {
if let Some(mut peers) = self.downstream_subscribers.get_mut(&key) {
let before = peers.len();
peers.retain(|_, last_renewed| {
now.duration_since(*last_renewed) < SUBSCRIPTION_LEASE_DURATION
});
let expired = before - peers.len();
if expired > 0 {
expired_counts.push((key, expired));
}
if peers.is_empty() {
drop(peers);
self.downstream_subscribers
.remove_if(&key, |_, peers| peers.is_empty());
}
}
}
expired_counts
}
/// Check if a contract has no local clients and no downstream subscribers,
/// meaning we can safely unsubscribe upstream.
pub fn should_unsubscribe_upstream(&self, contract: &ContractKey) -> bool {
if self.has_client_subscriptions(contract.id()) {
return false;
}
!self.has_downstream_subscribers(contract)
}
// =========================================================================
// Hosting Cache Management
// =========================================================================
/// Record a contract access in the hosting cache.
///
/// This is the main entry point for adding contracts to the hosting cache.
/// Cached contracts are retained for durability (stale fallback) but only
/// those with active interest (client subscriptions or downstream subscribers)
/// will have their subscriptions renewed.
///
/// Returns a `RecordAccessResult` containing:
/// - `is_new`: Whether this contract was newly added (vs. refreshed existing)
/// - `evicted`: Contracts that were evicted to make room
///
/// Automatically persists hosting metadata for the accessed contract and
/// removes persisted metadata for evicted contracts.
pub fn record_contract_access(
&self,
key: ContractKey,
size_bytes: u64,
access_type: AccessType,
) -> RecordAccessResult {
let result = self
.hosting_cache
.write()
.record_access(key, size_bytes, access_type);
// Persist hosting metadata for the accessed contract
if let Some(storage) = self.storage.read().as_ref() {
#[cfg(feature = "redb")]
{
use crate::contract::storages::HostingMetadata;
let now_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
let access_type_u8 = match access_type {
AccessType::Get => 0,
AccessType::Put => 1,
AccessType::Subscribe => 2,
};
let code_hash: [u8; 32] = **key.code_hash();
let local_client = self.hosting_cache.read().has_local_client_access(&key);
let metadata = HostingMetadata::new(
now_ms,
access_type_u8,
size_bytes,
code_hash,
local_client,
);
if let Err(e) = storage.store_hosting_metadata(&key, metadata) {
tracing::warn!(
contract = %key,
error = %e,
"Failed to persist hosting metadata for accessed contract"
);
}
}
#[cfg(all(feature = "sqlite", not(feature = "redb")))]
{
// For sqlite, we can't easily run async from a sync context
// The metadata is persisted via StateStorage::store() when state is stored
tracing::trace!(
contract = %key,
"Sqlite hosting metadata update deferred to state store"
);
}
// Clean up persisted metadata for evicted contracts
for evicted_key in &result.evicted {
#[cfg(feature = "redb")]
{
if let Err(e) = storage.remove_hosting_metadata(evicted_key) {
tracing::warn!(
contract = %evicted_key,
error = %e,
"Failed to remove persisted hosting metadata for evicted contract"
);
}
}
#[cfg(all(feature = "sqlite", not(feature = "redb")))]
{
tracing::debug!(
contract = %evicted_key,
"Evicted contract - sqlite metadata cleanup deferred"
);
}
}
}
result
}
/// Check if a contract is in the hosting cache.
pub fn is_hosting_contract(&self, key: &ContractKey) -> bool {
self.hosting_cache.read().contains(key)
}
/// Get all hosted contract keys.
pub fn hosting_contract_keys(&self) -> Vec<ContractKey> {
self.hosting_cache.read().iter().collect()
}
/// Get the cached state size in bytes for a hosted contract.
pub fn hosting_contract_size(&self, key: &ContractKey) -> u64 {
self.hosting_cache
.read()
.get(key)
.map(|c| c.size_bytes)
.unwrap_or(0)
}
/// Get the number of contracts in the hosting cache.
pub fn hosting_contracts_count(&self) -> usize {
self.hosting_cache.read().len()
}
/// Check if we should continue hosting a contract.
///
/// Returns true if:
/// - We have an active subscription, OR
/// - We have client subscriptions, OR
/// - The contract is in our hosting cache
#[cfg(test)]
pub fn should_host(&self, contract: &ContractKey) -> bool {
self.is_subscribed(contract)
|| self.has_client_subscriptions(contract.id())
|| self.is_hosting_contract(contract)
}
/// Check if this node is actively receiving updates for a contract.
///
/// Returns true only if we have an active network subscription or local
/// client subscriptions — conditions that guarantee our cached state is
/// kept fresh. Unlike [`should_host()`](Self::should_host), this excludes
/// the hosting LRU cache, which can retain contracts after their
/// subscriptions expire (leaving stale state).
pub fn is_receiving_updates(&self, contract: &ContractKey) -> bool {
self.is_subscribed(contract) || self.has_client_subscriptions(contract.id())
}
/// Mark a contract as accessed by a local client (HTTP/WebSocket).
///
/// Only contracts with this flag get subscription renewal and trusted
/// local-cache serving. Persists to disk so it survives restarts.
pub fn mark_local_client_access(&self, key: &ContractKey) {
let already_set = self.hosting_cache.read().has_local_client_access(key);
// Always refresh the timestamp (keeps the age gate alive) even if
// the flag is already set. Only skip disk persistence for the flag.
self.hosting_cache.write().mark_local_client_access(key);
if already_set {
return;
}
// Persist the updated flag to disk
if let Some(storage) = self.storage.read().as_ref() {
#[cfg(feature = "redb")]
{
if let Ok(Some(mut metadata)) = storage.get_hosting_metadata(key) {
metadata.local_client_access = true;
if let Err(e) = storage.store_hosting_metadata(key, metadata) {
tracing::warn!(
contract = %key,
error = %e,
"Failed to persist local_client_access flag"
);
}
}
}
#[cfg(all(feature = "sqlite", not(feature = "redb")))]
{
// Sqlite persistence is deferred to the next state store call,
// which uses MAX() to preserve the flag (see store_hosting_metadata).
tracing::trace!(
contract = %key,
"Sqlite local_client_access persistence deferred to state store"
);
}
}
debug!(%key, "Marked contract as locally accessed by client");
}
/// Check if a contract was accessed by a local client.
pub fn has_local_client_access(&self, key: &ContractKey) -> bool {
self.hosting_cache.read().has_local_client_access(key)
}
/// Touch a contract in the hosting cache (refresh TTL without adding).
///
/// Called when a user GET serves a hosted contract from local cache.
pub fn touch_hosting(&self, key: &ContractKey) {
self.hosting_cache.write().touch(key);
}
/// Sweep for expired entries in the hosting cache.
///
/// Contracts are protected from eviction if they have client subscriptions
/// OR downstream subscribers (other peers relying on us for updates).
/// The downstream subscriber exemption is time-bounded: stale entries are
/// removed by `expire_stale_downstream_subscribers()` (called periodically)
/// after `SUBSCRIPTION_LEASE_DURATION` without renewal.
/// Automatically removes persisted metadata for expired contracts.
pub fn sweep_expired_hosting(&self) -> Vec<ContractKey> {
let expired = self.hosting_cache.write().sweep_expired(|key| {
self.has_client_subscriptions(key.id()) || self.has_downstream_subscribers(key)
});
// Clean up persisted metadata for expired contracts
if !expired.is_empty() {
if let Some(storage) = self.storage.read().as_ref() {
for expired_key in &expired {
#[cfg(feature = "redb")]
{
if let Err(e) = storage.remove_hosting_metadata(expired_key) {
tracing::warn!(
contract = %expired_key,
error = %e,
"Failed to remove persisted hosting metadata for expired contract"
);
}
}
#[cfg(all(feature = "sqlite", not(feature = "redb")))]
{
tracing::debug!(
contract = %expired_key,
"Expired contract - sqlite metadata cleanup deferred"
);
}
}
}
}
expired
}
// =========================================================================
// Subscription Retry Management (Backoff)
// =========================================================================
/// Check if a subscription request can be made for a contract.
/// Returns false if request is already pending or in backoff period.
pub fn can_request_subscription(&self, contract: &ContractKey) -> bool {
if self.pending_subscription_requests.contains(contract) {
return false;
}
!self.subscription_backoff.read().is_in_backoff(contract)
}
/// Mark a subscription request as in-flight.
/// Returns false if already pending.
pub fn mark_subscription_pending(&self, contract: ContractKey) -> bool {
if self.pending_subscription_requests.contains(&contract) {
return false;
}
self.pending_subscription_requests.insert(contract);
true
}
/// Mark a subscription request as completed.
/// If success is false, applies exponential backoff.
pub fn complete_subscription_request(&self, contract: &ContractKey, success: bool) {
self.pending_subscription_requests.remove(contract);
if success {
self.subscription_backoff.write().record_success(contract);
} else {
self.subscription_backoff.write().record_failure(*contract);
}
}
// =========================================================================
// Introspection / Telemetry
// =========================================================================
/// Get subscription state for all contracts (for telemetry).
///
/// Returns: (contract, has_client_subscription, is_active_subscription, expires_at)
pub fn get_subscription_states(&self) -> Vec<(ContractKey, bool, bool, Option<Instant>)> {
let now = self.time_source.now();
let mut states: Vec<_> = self
.active_subscriptions
.iter()
.map(|entry| {
let contract = *entry.key();
let expires_at = *entry.value();
let is_active = expires_at > now;
let has_client = self.has_client_subscriptions(contract.id());
(contract, has_client, is_active, Some(expires_at))
})
.collect();
// Sort by contract key for deterministic ordering (critical for simulation tests)
states.sort_by(|(a, _, _, _), (b, _, _, _)| a.id().as_bytes().cmp(b.id().as_bytes()));
states
}
/// Get contracts that need subscription renewal.
///
/// Returns contracts where:
/// - We have an active subscription that will expire soon, OR
/// - We have client subscriptions but no active network subscription
///
/// Hosted contracts without active interest (no client subscriptions,
/// no downstream subscribers) are intentionally NOT renewed. Contracts
/// persisted to disk are kept as a recovery mechanism (last-resort PUT
/// if the contract is lost from the network) but are not actively
/// subscribed to avoid subscription accumulation.
pub fn contracts_needing_renewal(&self) -> Vec<ContractKey> {
let now = self.time_source.now();
let renewal_threshold = now + SUBSCRIPTION_RENEWAL_INTERVAL;
// Use HashSet for O(1) deduplication instead of O(n) Vec::contains
let mut needs_renewal_set = HashSet::new();
// 1. Contracts with soon-to-expire subscriptions
// Collect and sort for deterministic iteration order
let mut active_subs: Vec<_> = self
.active_subscriptions
.iter()
.map(|entry| (*entry.key(), *entry.value()))
.collect();
active_subs.sort_by(|(a, _), (b, _)| a.id().as_bytes().cmp(b.id().as_bytes()));
for (key, expires_at) in active_subs {
if expires_at <= renewal_threshold && expires_at > now {
needs_renewal_set.insert(key);
}
}
// 2. Contracts with client subscriptions but no active network subscription
// Collect and sort for deterministic iteration order
let mut client_instance_ids: Vec<_> =
self.client_subscriptions.iter().map(|e| *e.key()).collect();
client_instance_ids.sort_by(|a, b| a.as_bytes().cmp(b.as_bytes()));
for instance_id in client_instance_ids {
// Find if we have an active subscription for this contract
let has_active = self
.active_subscriptions
.iter()
.any(|sub| sub.key().id() == &instance_id && *sub.value() > now);
if !has_active {
// Need to find the ContractKey - check hosting cache
if let Some(contract) = self
.hosting_cache
.read()
.iter()
.find(|k| k.id() == &instance_id)
{
needs_renewal_set.insert(contract);
}
}
}
// 3. Locally-accessed hosted contracts without active subscription.
// Only contracts recently marked by local clients are renewed (#3769);
// relay-cached contracts are excluded to prevent storms (#3763).
// The age gate (SUBSCRIPTION_LEASE_DURATION) ensures contracts stop
// being renewed if the local user hasn't accessed them recently,
// satisfying the cleanup exemption rule (AGENTS.md).
{
let cache = self.hosting_cache.read();
let now = self.time_source.now();
for key in cache.iter() {
if cache.has_recent_local_client_access(&key, SUBSCRIPTION_LEASE_DURATION)
&& !self
.active_subscriptions
.get(&key)
.map(|e| *e > now)
.unwrap_or(false)
{
needs_renewal_set.insert(key);
}
}
}
// Convert set to vec and sort for deterministic return order
let mut result: Vec<ContractKey> = needs_renewal_set.into_iter().collect();
result.sort_by(|a, b| a.id().as_bytes().cmp(b.id().as_bytes()));
result
}
// =========================================================================
// Topology Snapshot (for telemetry/visualization)
// =========================================================================
/// Generate a topology snapshot for this peer.
///
/// In the simplified lease-based model (2026-01 refactor), we don't track
/// upstream/downstream relationships. The snapshot shows which contracts
/// we're hosting and which have client subscriptions.
#[allow(dead_code)] // Called from Ring methods that may be behind feature gates
pub fn generate_topology_snapshot(
&self,
peer_addr: std::net::SocketAddr,
location: f64,
) -> super::topology_registry::TopologySnapshot {
use super::topology_registry::{ContractSubscription, TopologySnapshot};
let mut snapshot = TopologySnapshot::new(peer_addr, location);
let now = self.time_source.now();
// Record the raw set of keys that are in `active_subscriptions` right
// now. This is used by regression tests to detect whether a peer
// installed a subscription lease — e.g. the relay-pollution bug fixed
// alongside this field where every forwarder on a SUBSCRIBE response
// path was unconditionally adding itself to active_subscriptions,
// causing feedback-loop renewal. Must be populated BEFORE the merged
// `contracts` map below, which hides active_subscriptions entries
// behind hosting cache presence when both exist.
for entry in self.active_subscriptions.iter() {
if *entry.value() > now {
snapshot.active_subscription_keys.insert(*entry.key().id());
}
}
// Add all hosted contracts
// Collect and sort for deterministic iteration order
let hosting_cache = self.hosting_cache.read();
let mut hosted_contracts: Vec<_> = hosting_cache.iter().collect();
hosted_contracts.sort_by(|a, b| a.id().as_bytes().cmp(b.id().as_bytes()));
for contract_key in hosted_contracts {
let has_client_subscriptions =
self.client_subscriptions.contains_key(contract_key.id());
snapshot.set_contract(
*contract_key.id(),
ContractSubscription {
contract_key,
upstream: None, // No upstream tracking in lease-based model
downstream: vec![], // No downstream tracking in lease-based model
is_hosting: true,
has_client_subscriptions,
},
);
}
// Add subscribed contracts that might not be in hosting cache yet
// Collect and sort for deterministic iteration order
let mut active_subs: Vec<_> = self
.active_subscriptions
.iter()
.map(|entry| (*entry.key(), *entry.value()))
.collect();
active_subs.sort_by(|(a, _), (b, _)| a.id().as_bytes().cmp(b.id().as_bytes()));
for (contract_key, expires_at) in active_subs {
if expires_at > now && !hosting_cache.contains(&contract_key) {
let has_client_subscriptions =
self.client_subscriptions.contains_key(contract_key.id());
snapshot.set_contract(
*contract_key.id(),
ContractSubscription {
contract_key,
upstream: None,
downstream: vec![],
is_hosting: false,
has_client_subscriptions,
},
);
}
}
// Use GlobalSimulationTime for deterministic timestamps in simulation tests
snapshot.timestamp_nanos =
crate::config::GlobalSimulationTime::current_time_ms() * 1_000_000;
snapshot
}
}
// =============================================================================
// Persistence Methods
// =============================================================================
impl HostingManager {
/// Load hosting metadata from storage during startup.
///
/// This restores the hosting cache from persisted data, allowing the peer
/// to continue hosting contracts after a restart without losing LRU state.
///
/// Also migrates legacy contracts that have state but no hosting metadata.
/// This is critical for network upgrades - without migration, all peers would
/// "forget" legacy contracts after upgrading.
///
/// # Arguments
/// * `storage` - The storage backend (ReDb or SqlitePool)
/// * `code_hash_lookup` - Function to look up CodeHash from ContractInstanceId.
/// Uses ContractStore which has the id->code_hash mapping.
///
/// # Returns
/// The number of contracts loaded from storage (including migrated legacy contracts).
#[cfg(feature = "redb")]
pub fn load_from_storage<F>(
&self,
storage: &crate::contract::storages::Storage,
code_hash_lookup: F,
) -> Result<usize, redb::Error>
where
F: Fn(&ContractInstanceId) -> Option<freenet_stdlib::prelude::CodeHash>,
{
use freenet_stdlib::prelude::{CodeHash, ContractInstanceId, ContractKey};
use std::collections::HashSet;
let metadata_entries = storage.load_all_hosting_metadata()?;
let now_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
let mut cache = self.hosting_cache.write();
let mut loaded = 0;
// Track which instance IDs we've loaded (for legacy detection)
let mut loaded_instance_ids: HashSet<[u8; 32]> = HashSet::new();
for (key_bytes, metadata) in metadata_entries {
// Reconstruct ContractKey from instance ID bytes and code hash from metadata
// key_bytes contains the ContractInstanceId (32 bytes)
// metadata.code_hash contains the CodeHash (32 bytes)
if key_bytes.len() == 32 {
let mut instance_id_bytes = [0u8; 32];
instance_id_bytes.copy_from_slice(&key_bytes);
loaded_instance_ids.insert(instance_id_bytes);
let instance_id = ContractInstanceId::new(instance_id_bytes);
let code_hash = CodeHash::new(metadata.code_hash);
let key = ContractKey::from_id_and_code(instance_id, code_hash);
let access_type = match metadata.access_type {
1 => cache::AccessType::Put,
2 => cache::AccessType::Subscribe,
_ => cache::AccessType::Get,
};
// Calculate age from persisted timestamp
let age_ms = now_ms.saturating_sub(metadata.last_access_ms);
let age = std::time::Duration::from_millis(age_ms);
cache.load_persisted_entry(
key,
metadata.size_bytes,
access_type,
age,
metadata.local_client_access,
);
loaded += 1;
}
}
// Migrate legacy contracts: contracts in states table but without hosting metadata
// This ensures the network doesn't "forget" contracts after upgrading
let all_state_keys = storage.iter_all_state_keys().unwrap_or_default();
let mut migrated = 0;
let mut migration_failures = 0;
for key_bytes in all_state_keys {
if key_bytes.len() != 32 {
continue;
}
let mut instance_id_bytes = [0u8; 32];
instance_id_bytes.copy_from_slice(&key_bytes);
// Skip if already loaded with metadata
if loaded_instance_ids.contains(&instance_id_bytes) {
continue;
}
// Legacy contract: has state but no hosting metadata
let instance_id = ContractInstanceId::new(instance_id_bytes);
// Look up code_hash from ContractStore
if let Some(code_hash) = code_hash_lookup(&instance_id) {
let key = ContractKey::from_id_and_code(instance_id, code_hash);
// Get state size for the hosting cache
let size_bytes = storage.get_state_size(&key).unwrap_or(Some(0)).unwrap_or(0);
// Legacy contracts don't have local_client_access info
cache.load_persisted_entry(
key,
size_bytes,
cache::AccessType::Get,
std::time::Duration::ZERO,
false,
);
// Persist hosting metadata so future restarts don't need migration
let code_hash_bytes: [u8; 32] = *code_hash;
let metadata = crate::contract::storages::HostingMetadata::new(
now_ms,
0, // GET access type
size_bytes,
code_hash_bytes,
false,
);
if let Err(e) = storage.store_hosting_metadata(&key, metadata) {
tracing::warn!(
contract = %key,
error = %e,
"Failed to persist hosting metadata for migrated legacy contract"
);
}
migrated += 1;
} else {
// ContractStore doesn't know about this contract
// This shouldn't happen normally - means WASM code is missing
migration_failures += 1;
tracing::warn!(
instance_id = %instance_id,
"Legacy contract has state but no WASM code - cannot migrate"
);
}
}
// Sort LRU order by last_accessed time
cache.finalize_loading();
let total_loaded = loaded + migrated;
if migrated > 0 || migration_failures > 0 {
tracing::info!(
loaded_with_metadata = loaded,
migrated_legacy = migrated,
migration_failures = migration_failures,
total_contracts = total_loaded,
total_bytes = cache.current_bytes(),
"Loaded hosting cache from storage (with legacy migration)"
);
} else {
tracing::info!(
loaded_contracts = total_loaded,
total_bytes = cache.current_bytes(),
"Loaded hosting cache from storage"
);
}
Ok(total_loaded)
}
/// Load hosting metadata from storage during startup (sqlite version).
///
/// Also migrates legacy contracts that have state but no hosting metadata.
#[cfg(all(feature = "sqlite", not(feature = "redb")))]
pub async fn load_from_storage<F>(
&self,
storage: &crate::contract::storages::Storage,
code_hash_lookup: F,
) -> Result<usize, crate::contract::storages::sqlite::SqlDbError>
where
F: Fn(&ContractInstanceId) -> Option<freenet_stdlib::prelude::CodeHash>,
{
use freenet_stdlib::prelude::{CodeHash, ContractInstanceId, ContractKey};
use std::collections::HashSet;
let metadata_entries = storage.load_all_hosting_metadata().await?;
let now_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
let mut cache = self.hosting_cache.write();
let mut loaded = 0;
// Track which instance IDs we've loaded (for legacy detection)
let mut loaded_instance_ids: HashSet<[u8; 32]> = HashSet::new();
for (key_bytes, metadata) in metadata_entries {
// Reconstruct ContractKey from instance ID bytes and code hash from metadata
// key_bytes contains the ContractInstanceId (32 bytes)
// metadata.code_hash contains the CodeHash (32 bytes)
if key_bytes.len() == 32 {
let mut instance_id_bytes = [0u8; 32];
instance_id_bytes.copy_from_slice(&key_bytes);
loaded_instance_ids.insert(instance_id_bytes);
let instance_id = ContractInstanceId::new(instance_id_bytes);
let code_hash = CodeHash::new(metadata.code_hash);
let key = ContractKey::from_id_and_code(instance_id, code_hash);
let access_type = match metadata.access_type {
1 => cache::AccessType::Put,
2 => cache::AccessType::Subscribe,
_ => cache::AccessType::Get,
};
// Calculate age from persisted timestamp
let age_ms = now_ms.saturating_sub(metadata.last_access_ms);
let age = std::time::Duration::from_millis(age_ms);
cache.load_persisted_entry(
key,
metadata.size_bytes,
access_type,
age,
metadata.local_client_access,
);
loaded += 1;
}
}
// Migrate legacy contracts: contracts in states table but without hosting metadata
let all_state_keys = storage.iter_all_state_keys().await.unwrap_or_default();
let mut migrated = 0;
let mut migration_failures = 0;
for key_bytes in all_state_keys {
if key_bytes.len() != 32 {
continue;
}
let mut instance_id_bytes = [0u8; 32];
instance_id_bytes.copy_from_slice(&key_bytes);
// Skip if already loaded with metadata
if loaded_instance_ids.contains(&instance_id_bytes) {
continue;
}
// Legacy contract: has state but no hosting metadata
let instance_id = ContractInstanceId::new(instance_id_bytes);
// Look up code_hash from ContractStore
if let Some(code_hash) = code_hash_lookup(&instance_id) {
let key = ContractKey::from_id_and_code(instance_id, code_hash);
// Get state size for the hosting cache
let size_bytes = storage
.get_state_size(&key)
.await
.unwrap_or(Some(0))
.unwrap_or(0);
cache.load_persisted_entry(
key,
size_bytes,
cache::AccessType::Get,
std::time::Duration::ZERO,
false,
);
// Persist hosting metadata so future restarts don't need migration
let code_hash_bytes: [u8; 32] = *code_hash;
let metadata = crate::contract::storages::sqlite::HostingMetadata::new(
now_ms,
0, // GET access type
size_bytes,
code_hash_bytes,
false,
);
if let Err(e) = storage.store_hosting_metadata(&key, metadata).await {
tracing::warn!(
contract = %key,
error = %e,
"Failed to persist hosting metadata for migrated legacy contract"
);
}
migrated += 1;
} else {
migration_failures += 1;
tracing::warn!(
instance_id = %instance_id,
"Legacy contract has state but no WASM code - cannot migrate"
);
}
}
// Sort LRU order by last_accessed time
cache.finalize_loading();
let total_loaded = loaded + migrated;
if migrated > 0 || migration_failures > 0 {
tracing::info!(
loaded_with_metadata = loaded,
migrated_legacy = migrated,
migration_failures = migration_failures,
total_contracts = total_loaded,
total_bytes = cache.current_bytes(),
"Loaded hosting cache from storage (with legacy migration)"
);
} else {
tracing::info!(
loaded_contracts = total_loaded,
total_bytes = cache.current_bytes(),
"Loaded hosting cache from storage"
);
}
Ok(total_loaded)
}
}
impl Default for HostingManager {
fn default() -> Self {
Self::new()
}
}
// =============================================================================
// Tests
// =============================================================================
#[cfg(test)]
mod tests {
use super::*;
use freenet_stdlib::prelude::CodeHash;
fn make_contract_key(seed: u8) -> ContractKey {
ContractKey::from_id_and_code(
ContractInstanceId::new([seed; 32]),
CodeHash::new([seed.wrapping_add(1); 32]),
)
}
#[tokio::test]
async fn test_subscribe_creates_new_subscription() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
let result = manager.subscribe(contract);
assert!(result.is_new);
assert!(manager.is_subscribed(&contract));
}
#[tokio::test]
async fn test_subscribe_renews_existing() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
let first = manager.subscribe(contract);
let second = manager.subscribe(contract);
assert!(first.is_new);
assert!(!second.is_new);
assert!(second.expires_at >= first.expires_at);
}
#[tokio::test]
async fn test_unsubscribe_removes_subscription() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
manager.subscribe(contract);
assert!(manager.is_subscribed(&contract));
manager.unsubscribe(&contract);
assert!(!manager.is_subscribed(&contract));
}
#[tokio::test]
async fn test_renew_subscription() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
// Renew non-existent subscription fails
assert!(!manager.renew_subscription(&contract));
// Subscribe then renew succeeds
manager.subscribe(contract);
assert!(manager.renew_subscription(&contract));
}
#[tokio::test]
async fn test_get_subscribed_contracts() {
let manager = HostingManager::new();
let c1 = make_contract_key(1);
let c2 = make_contract_key(2);
let c3 = make_contract_key(3);
manager.subscribe(c1);
manager.subscribe(c2);
manager.subscribe(c3);
manager.unsubscribe(&c2);
let subscribed = manager.get_subscribed_contracts();
assert_eq!(subscribed.len(), 2);
assert!(subscribed.contains(&c1));
assert!(!subscribed.contains(&c2));
assert!(subscribed.contains(&c3));
}
#[tokio::test]
async fn test_active_subscription_count() {
let manager = HostingManager::new();
assert_eq!(manager.active_subscription_count(), 0);
manager.subscribe(make_contract_key(1));
manager.subscribe(make_contract_key(2));
assert_eq!(manager.active_subscription_count(), 2);
manager.unsubscribe(&make_contract_key(1));
assert_eq!(manager.active_subscription_count(), 1);
}
#[test]
fn test_client_subscription_basic() {
let manager = HostingManager::new();
let instance_id = ContractInstanceId::new([1; 32]);
let client_id = crate::client_events::ClientId::next();
let result = manager.add_client_subscription(&instance_id, client_id);
assert!(result.is_first_client);
assert!(manager.has_client_subscriptions(&instance_id));
let is_last = manager.remove_client_subscription(&instance_id, client_id);
assert!(is_last);
assert!(!manager.has_client_subscriptions(&instance_id));
}
#[test]
fn test_client_subscription_multiple_clients() {
let manager = HostingManager::new();
let instance_id = ContractInstanceId::new([1; 32]);
let client1 = crate::client_events::ClientId::next();
let client2 = crate::client_events::ClientId::next();
let r1 = manager.add_client_subscription(&instance_id, client1);
let r2 = manager.add_client_subscription(&instance_id, client2);
assert!(r1.is_first_client);
assert!(!r2.is_first_client);
let is_last1 = manager.remove_client_subscription(&instance_id, client1);
assert!(!is_last1); // client2 still subscribed
let is_last2 = manager.remove_client_subscription(&instance_id, client2);
assert!(is_last2);
}
#[test]
fn test_hosting_cache_basic() {
let manager = HostingManager::new();
let key = make_contract_key(1);
assert!(!manager.is_hosting_contract(&key));
assert_eq!(manager.hosting_contracts_count(), 0);
manager.record_contract_access(key, 1000, AccessType::Put);
assert!(manager.is_hosting_contract(&key));
assert_eq!(manager.hosting_contracts_count(), 1);
}
#[test]
fn test_subscription_backoff() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
// Initially can request
assert!(manager.can_request_subscription(&contract));
// Mark pending
assert!(manager.mark_subscription_pending(contract));
// Can't request while pending
assert!(!manager.can_request_subscription(&contract));
// Complete with failure
manager.complete_subscription_request(&contract, false);
// Now in backoff - can't request immediately
assert!(!manager.can_request_subscription(&contract));
}
#[test]
fn test_should_host() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
// Not hosting initially
assert!(!manager.should_host(&contract));
// Add to hosting cache
manager.record_contract_access(contract, 1000, AccessType::Put);
assert!(manager.should_host(&contract));
}
/// Regression test for #3546: hosted-only contracts must NOT be in the
/// renewal list. Including them caused subscription storms (#3763 incident).
#[test]
fn test_hosted_contract_not_in_renewal_after_restart() {
let manager = HostingManager::new();
let contract = make_contract_key(42);
manager.record_contract_access(contract, 1000, AccessType::Get);
assert!(manager.is_hosting_contract(&contract));
assert!(
manager.contracts_needing_renewal().is_empty(),
"Hosted-only contract must NOT be in renewal list"
);
}
/// Regression test for #3340: is_receiving_updates must return false when
/// a contract is only in the hosting LRU cache (no active subscription).
#[test]
fn test_is_receiving_updates_excludes_hosting_cache_only() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
// Not receiving updates initially
assert!(!manager.is_receiving_updates(&contract));
// Add to hosting cache only — should_host true, is_receiving_updates false
manager.record_contract_access(contract, 1000, AccessType::Put);
assert!(manager.should_host(&contract));
assert!(
!manager.is_receiving_updates(&contract),
"Hosting cache alone should NOT count as receiving updates"
);
// Add active subscription — now is_receiving_updates should be true
manager.subscribe(contract);
assert!(manager.is_receiving_updates(&contract));
}
/// Regression test for #3340: is_receiving_updates with client subscriptions.
#[test]
fn test_is_receiving_updates_with_client_subscription() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
let client_id = crate::client_events::ClientId::next();
assert!(!manager.is_receiving_updates(&contract));
manager.add_client_subscription(contract.id(), client_id);
assert!(manager.is_receiving_updates(&contract));
}
#[test]
fn test_contracts_needing_renewal_excludes_hosted_only() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
// Add to hosting cache (simulating GET operation)
manager.record_contract_access(contract, 1000, AccessType::Get);
// Hosted-only contracts should NOT be renewed -- subscribing to all
// hosted contracts causes subscription storms (#3546). The local
// cache shortcut (#3761) handles same-session freshness, and the
// subscription piggyback (#3762) handles post-GET subscription.
let needs_renewal = manager.contracts_needing_renewal();
assert!(
!needs_renewal.contains(&contract),
"Hosted-only contract should NOT be in renewal list"
);
}
// Removed: test_contracts_needing_renewal_includes_hosted was added in #3763
// but caused subscription storms. Hosted-only contracts must NOT be renewed.
// The exclusion test (test_contracts_needing_renewal_excludes_hosted_only)
// covers the correct behavior.
/// Regression: a node that merely relays a SUBSCRIBE response for some
/// other peer must NOT end up with the contract in its own
/// `active_subscriptions`, and consequently must NOT appear in
/// `contracts_needing_renewal()`.
///
/// Before the fix to `operations::subscribe::SubscribeMsgResult::Subscribed`,
/// every relay on a SUBSCRIBE response path called `ring.subscribe(*key)`
/// unconditionally. That installed a lease in `active_subscriptions`,
/// which `contracts_needing_renewal()` path #1 would then pick up every
/// ~2 minutes and spawn a fresh subscribe for — routing through new
/// relays that *also* installed leases, compounding with each cycle.
/// The feedback loop shows up as the 85+ phantom contracts observed on
/// the `technic` peer's local dashboard (see commit message).
///
/// This test models the post-fix relay state as "contract has a
/// downstream subscriber registered, but no `subscribe()` lease", which
/// is what the SUBSCRIBE Response relay branch now does. The assertion
/// is that such a relay does not get recruited into the renewal cycle.
#[test]
fn test_relay_downstream_only_not_in_renewal() {
let manager = HostingManager::new();
let contract = make_contract_key(77);
let downstream = make_peer_key(42);
// Relay state: we've accepted a downstream subscriber for the
// contract, but we have not called `subscribe()` on our own behalf
// (we're just forwarding Updates for someone else) and we have no
// local client expressing interest.
assert!(manager.add_downstream_subscriber(&contract, downstream.clone()));
// Invariant 1: we did not install a self-subscription lease.
assert!(
!manager.is_subscribed(&contract),
"Relay must not have an active subscription lease just from \
registering a downstream subscriber"
);
assert!(
manager.get_subscribed_contracts().is_empty(),
"active_subscriptions must be empty on a pure-relay peer"
);
// Invariant 2: the contract is not in the renewal set. This is the
// load-bearing property: if the relay were in `active_subscriptions`,
// `contracts_needing_renewal()` path #1 (expiring active leases)
// would pick it up and spawn a new subscribe, recruiting more
// relays. Pure downstream registration must NOT trigger renewal.
let needs_renewal = manager.contracts_needing_renewal();
assert!(
!needs_renewal.contains(&contract),
"Pure-relay peer must not appear in contracts_needing_renewal \
(relay-subscription feedback loop regression, see \
subscribe.rs::SubscribeMsgResult::Subscribed)"
);
// Invariant 3: downstream registration still works as intended —
// the relay holds the downstream peer so UPDATE broadcasts can be
// forwarded. This is the *correct* mechanism for a relay to receive
// and propagate updates, without inflating subscription trees.
assert!(manager.has_downstream_subscribers(&contract));
}
// Superseded: startup revalidation window removed in #3546 to prevent
// subscription accumulation storms. Hosted-only contracts are no longer
// proactively renewed at startup. Replaced by test_hosted_contracts_not_renewed_at_scale.
#[ignore]
#[test]
fn test_hosted_contract_renewed_despite_no_interest() {
let manager = HostingManager::new();
let contract = make_contract_key(42);
manager.record_contract_access(contract, 1000, AccessType::Get);
assert!(manager.is_hosting_contract(&contract));
// Before #3546: contracts_needing_renewal() included this during startup window
// After #3546: hosted-only contracts are never included
let renewals = manager.contracts_needing_renewal();
assert!(
!renewals.contains(&contract),
"Hosted contract should NOT be in renewal list (startup window removed in #3546)"
);
}
// Superseded: startup revalidation window removed in #3546.
// Hosted contracts loaded from disk are no longer auto-subscribed on startup.
#[ignore]
#[test]
fn test_startup_revalidation_includes_hosted_contracts() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
manager.record_contract_access(contract, 1000, AccessType::Get);
// Before #3546: during startup window, this would be in renewal list
// After #3546: hosted-only contracts are never renewed
let needs_renewal = manager.contracts_needing_renewal();
assert!(
!needs_renewal.contains(&contract),
"Hosted contract should NOT be in renewal list (startup window removed in #3546)"
);
}
// Superseded: startup revalidation window removed in #3546.
#[ignore]
#[test]
fn test_startup_revalidation_skips_already_subscribed() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
manager.record_contract_access(contract, 1000, AccessType::Get);
manager.subscribe(contract);
let needs_renewal = manager.contracts_needing_renewal();
assert!(
!needs_renewal.contains(&contract),
"Already-subscribed contract should not be in renewal list"
);
}
// Superseded: startup revalidation window removed in #3546.
#[ignore]
#[test]
fn test_startup_revalidation_window_expires() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
manager.record_contract_access(contract, 1000, AccessType::Get);
let needs_renewal = manager.contracts_needing_renewal();
assert!(
!needs_renewal.contains(&contract),
"Hosted-only contract should NOT be in renewal list"
);
}
// Superseded: startup revalidation window removed in #3546.
#[ignore]
#[test]
fn test_startup_revalidation_multiple_contracts() {
let manager = HostingManager::new();
let contract_a = make_contract_key(1);
let contract_b = make_contract_key(2);
let contract_c = make_contract_key(3);
manager.record_contract_access(contract_a, 1000, AccessType::Get);
manager.record_contract_access(contract_b, 1000, AccessType::Get);
manager.record_contract_access(contract_c, 1000, AccessType::Get);
manager.subscribe(contract_b);
let client_id = crate::client_events::ClientId::next();
manager.add_client_subscription(contract_c.id(), client_id);
let needs_renewal = manager.contracts_needing_renewal();
// Before #3546: contract_a would be included by startup window
// After #3546: only contract_c (client subscription) is included
assert!(
!needs_renewal.contains(&contract_a),
"Hosted-only contract_a should NOT be included (startup window removed)"
);
assert!(
!needs_renewal.contains(&contract_b),
"Subscribed contract_b should be excluded (not expiring soon)"
);
assert!(
needs_renewal.contains(&contract_c),
"Client-subscribed contract_c should be included"
);
}
/// Verify that hosted contracts are included in renewal and the renewal
/// system handles scale (200 hosted contracts). The batch limit in
/// renew_subscriptions_task (MAX_RECOVERY_ATTEMPTS_PER_INTERVAL = 10)
/// prevents subscription storms by processing at most 10 per cycle.
#[test]
fn test_hosted_contracts_not_renewed_at_scale() {
let manager = HostingManager::new();
// Simulate 200 relay-cached contracts loaded from disk
for i in 0..200u8 {
let contract = make_contract_key(i);
manager.record_contract_access(contract, 1000, AccessType::Get);
}
assert_eq!(manager.hosting_contracts_count(), 200);
// None should appear in renewal list -- subscribing to all hosted
// contracts causes subscription storms (#3546, confirmed in #3763).
let needs_renewal = manager.contracts_needing_renewal();
assert!(
needs_renewal.is_empty(),
"200 hosted-only contracts should NOT be in renewal list, found {}",
needs_renewal.len()
);
// Subscribe to exactly 2 (simulating River client)
let client_id = crate::client_events::ClientId::next();
let contract_a = make_contract_key(42);
let contract_b = make_contract_key(99);
manager.add_client_subscription(contract_a.id(), client_id);
manager.add_client_subscription(contract_b.id(), client_id);
// Only those 2 should need renewal
let needs_renewal = manager.contracts_needing_renewal();
assert_eq!(
needs_renewal.len(),
2,
"Only 2 client-subscribed contracts should need renewal, found {}",
needs_renewal.len()
);
assert!(needs_renewal.contains(&contract_a));
assert!(needs_renewal.contains(&contract_b));
}
/// Validates that backoff constants are internally consistent.
///
/// MAX_SUBSCRIPTION_BACKOFF must be shorter than SUBSCRIPTION_LEASE_DURATION,
/// otherwise a contract at maximum backoff will have its subscription expire
/// before the next retry — causing permanent subscription loss that only
/// recovers when the orphan recovery sweep picks it up (up to 30s later).
///
/// This test would have caught the original bug where MAX_SUBSCRIPTION_BACKOFF
/// was 600s (10 min) but SUBSCRIPTION_LEASE_DURATION was only 480s (8 min).
#[test]
fn test_backoff_shorter_than_lease() {
assert!(
MAX_SUBSCRIPTION_BACKOFF < SUBSCRIPTION_LEASE_DURATION,
"MAX_SUBSCRIPTION_BACKOFF ({:?}) must be shorter than \
SUBSCRIPTION_LEASE_DURATION ({:?}), otherwise subscriptions \
expire before retry",
MAX_SUBSCRIPTION_BACKOFF,
SUBSCRIPTION_LEASE_DURATION
);
}
/// Validates that the full backoff sequence never exceeds the lease duration.
/// Even after many consecutive failures, no single backoff delay should be
/// long enough to let the subscription expire.
#[test]
fn test_backoff_sequence_within_lease() {
let backoff =
ExponentialBackoff::new(INITIAL_SUBSCRIPTION_BACKOFF, MAX_SUBSCRIPTION_BACKOFF);
// Check delays for up to 10 consecutive failures
for failures in 1..=10 {
let delay = backoff.delay_for_failures(failures);
assert!(
delay < SUBSCRIPTION_LEASE_DURATION,
"Backoff delay after {} failures ({:?}) exceeds lease ({:?})",
failures,
delay,
SUBSCRIPTION_LEASE_DURATION
);
}
}
fn make_peer_key(seed: u8) -> PeerKey {
PeerKey(crate::transport::TransportPublicKey::from_bytes([seed; 32]))
}
/// Test that should_unsubscribe_upstream returns true when contract is not
/// tracked (simulates "contract not found" early return in the Unsubscribe handler).
#[test]
fn test_should_unsubscribe_upstream_unknown_contract() {
let manager = HostingManager::new();
let unknown_contract = make_contract_key(99);
// Contract never added to any tracking structure
assert!(
manager.should_unsubscribe_upstream(&unknown_contract),
"Unknown contract with no clients and no downstream should return true"
);
assert!(!manager.has_downstream_subscribers(&unknown_contract));
assert!(!manager.has_client_subscriptions(unknown_contract.id()));
}
#[test]
fn test_should_unsubscribe_upstream() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
let peer = make_peer_key(10);
let client_id = crate::client_events::ClientId::next();
// No clients, no downstream -> should unsubscribe
assert!(manager.should_unsubscribe_upstream(&contract));
// Add downstream subscriber -> should NOT unsubscribe
manager.add_downstream_subscriber(&contract, peer.clone());
assert!(!manager.should_unsubscribe_upstream(&contract));
// Remove downstream -> should unsubscribe again
manager.remove_downstream_subscriber(&contract, &peer);
assert!(manager.should_unsubscribe_upstream(&contract));
// Add client subscription -> should NOT unsubscribe
manager.add_client_subscription(contract.id(), client_id);
assert!(!manager.should_unsubscribe_upstream(&contract));
}
// =========================================================================
// Upstream Unsubscribe Decision Logic Tests
// =========================================================================
/// Simulate chain propagation: downstream peer unsubscribes, node checks
/// whether it should propagate the unsubscribe upstream.
///
/// Scenario: A -> B -> C (subscription tree). C unsubscribes from B.
/// B has no other downstream subscribers and no local clients, so B
/// should propagate the unsubscribe to A.
#[test]
fn test_chain_propagation_single_downstream() {
let manager = HostingManager::new();
let contract = make_contract_key(10);
let downstream_c = make_peer_key(30);
// B is hosting the contract with C as the only downstream subscriber
manager.subscribe(contract);
manager.add_downstream_subscriber(&contract, downstream_c.clone());
// C unsubscribes from B
assert!(manager.remove_downstream_subscriber(&contract, &downstream_c));
// B has no local clients and no remaining downstream -> should propagate
assert!(
manager.should_unsubscribe_upstream(&contract),
"Node with no clients and no downstream should propagate unsubscribe upstream"
);
}
/// Scenario: A -> B, C -> B. C unsubscribes, but A is still subscribed.
/// B should NOT propagate upstream because A remains as a downstream subscriber.
#[test]
fn test_no_propagation_with_remaining_downstream() {
let manager = HostingManager::new();
let contract = make_contract_key(10);
let downstream_a = make_peer_key(10);
let downstream_c = make_peer_key(30);
// B hosts contract with both A and C as downstream subscribers
manager.subscribe(contract);
manager.add_downstream_subscriber(&contract, downstream_a.clone());
manager.add_downstream_subscriber(&contract, downstream_c.clone());
// C unsubscribes
assert!(manager.remove_downstream_subscriber(&contract, &downstream_c));
// A is still subscribed -> should NOT propagate
assert!(
!manager.should_unsubscribe_upstream(&contract),
"Node with remaining downstream should NOT propagate unsubscribe"
);
}
/// Scenario: Local client still interested even after all downstream peers leave.
/// Node should NOT propagate upstream because a local WebSocket client is subscribed.
#[test]
fn test_no_propagation_with_local_client() {
let manager = HostingManager::new();
let contract = make_contract_key(10);
let downstream_peer = make_peer_key(10);
let client_id = crate::client_events::ClientId::next();
// Node has both a downstream subscriber and a local client
manager.subscribe(contract);
manager.add_downstream_subscriber(&contract, downstream_peer.clone());
manager.add_client_subscription(contract.id(), client_id);
// Downstream peer unsubscribes
assert!(manager.remove_downstream_subscriber(&contract, &downstream_peer));
// Local client still subscribed -> should NOT propagate
assert!(
!manager.should_unsubscribe_upstream(&contract),
"Node with local client should NOT propagate unsubscribe even if downstream is empty"
);
}
/// Simulate client disconnect: when a WebSocket client disconnects, check
/// that affected contracts can be identified and the unsubscribe decision
/// is correct.
#[test]
fn test_client_disconnect_triggers_unsubscribe_decision() {
let manager = HostingManager::new();
let contract = make_contract_key(10);
let client_id = crate::client_events::ClientId::next();
// Client subscribes to a contract (no downstream peers)
manager.subscribe(contract);
manager.add_client_subscription(contract.id(), client_id);
// Client should prevent unsubscribe
assert!(!manager.should_unsubscribe_upstream(&contract));
// Client disconnects
let result = manager.remove_client_from_all_subscriptions(client_id);
assert_eq!(
result.affected_contracts.len(),
1,
"Disconnect should report the affected contract"
);
assert_eq!(result.affected_contracts[0], contract);
// Now with no client and no downstream -> should unsubscribe
assert!(
manager.should_unsubscribe_upstream(&contract),
"After client disconnect with no downstream, should propagate unsubscribe"
);
}
/// Simulate client disconnect with multiple contracts: only contracts with
/// no remaining interest should trigger the unsubscribe decision.
#[test]
fn test_client_disconnect_partial_unsubscribe() {
let manager = HostingManager::new();
let contract_a = make_contract_key(10);
let contract_b = make_contract_key(20);
let client_id = crate::client_events::ClientId::next();
let downstream_peer = make_peer_key(50);
// Client subscribes to both contracts
manager.subscribe(contract_a);
manager.subscribe(contract_b);
manager.add_client_subscription(contract_a.id(), client_id);
manager.add_client_subscription(contract_b.id(), client_id);
// contract_b also has a downstream subscriber
manager.add_downstream_subscriber(&contract_b, downstream_peer.clone());
// Client disconnects
let result = manager.remove_client_from_all_subscriptions(client_id);
assert_eq!(result.affected_contracts.len(), 2);
// contract_a: no client, no downstream -> should unsubscribe
assert!(
manager.should_unsubscribe_upstream(&contract_a),
"Contract with no remaining interest should trigger unsubscribe"
);
// contract_b: no client, but has downstream -> should NOT unsubscribe
assert!(
!manager.should_unsubscribe_upstream(&contract_b),
"Contract with downstream subscribers should NOT trigger unsubscribe"
);
}
/// Simulate downstream subscriber expiry triggering unsubscribe decisions.
/// Uses manual timestamp manipulation via DashMap to simulate time passing.
#[test]
fn test_expire_downstream_triggers_unsubscribe_decision() {
let manager = HostingManager::new();
let contract = make_contract_key(10);
let peer = make_peer_key(10);
// Add a downstream subscriber
manager.subscribe(contract);
manager.add_downstream_subscriber(&contract, peer.clone());
// Not expired yet -> should NOT unsubscribe
assert!(!manager.should_unsubscribe_upstream(&contract));
// Manually set the subscriber's lease to the past
if let Some(mut peers) = manager.downstream_subscribers.get_mut(&contract) {
peers.insert(
peer.clone(),
Instant::now() - SUBSCRIPTION_LEASE_DURATION - Duration::from_secs(1),
);
}
// Run expiry sweep
let expired = manager.expire_stale_downstream_subscribers();
assert_eq!(
expired.len(),
1,
"Should detect one contract with expired downstream"
);
assert_eq!(expired[0].0, contract);
assert_eq!(expired[0].1, 1, "One peer should have expired");
// Now should unsubscribe (no client, no downstream)
assert!(
manager.should_unsubscribe_upstream(&contract),
"After all downstream subscribers expire, should propagate unsubscribe"
);
}
/// Partial expiry: some downstream subscribers expire but others remain.
/// Should NOT trigger unsubscribe.
#[test]
fn test_partial_downstream_expiry_no_unsubscribe() {
let manager = HostingManager::new();
let contract = make_contract_key(10);
let stale_peer = make_peer_key(10);
let fresh_peer = make_peer_key(20);
// Add two downstream subscribers
manager.subscribe(contract);
manager.add_downstream_subscriber(&contract, stale_peer.clone());
manager.add_downstream_subscriber(&contract, fresh_peer.clone());
// Make one subscriber stale
if let Some(mut peers) = manager.downstream_subscribers.get_mut(&contract) {
peers.insert(
stale_peer,
Instant::now() - SUBSCRIPTION_LEASE_DURATION - Duration::from_secs(1),
);
}
// Run expiry sweep - one stale peer expired but fresh peer remains
let expired = manager.expire_stale_downstream_subscribers();
assert_eq!(expired.len(), 1, "One contract had expired peers");
assert_eq!(expired[0].0, contract);
assert_eq!(expired[0].1, 1, "One peer should have expired");
// fresh_peer still present -> should NOT unsubscribe
assert!(
!manager.should_unsubscribe_upstream(&contract),
"Contract with remaining downstream should NOT trigger unsubscribe"
);
}
// =========================================================================
// Unsubscribe Handler Logic Tests
// =========================================================================
fn make_interest_manager() -> crate::ring::interest::InterestManager<InstantTimeSrc> {
crate::ring::interest::InterestManager::new(InstantTimeSrc::new())
}
/// Contract found + peer resolved → removes both tracking structures,
/// triggers upstream unsubscribe propagation.
#[test]
fn test_unsubscribe_handler_contract_found_peer_resolved() {
let manager = HostingManager::new();
let interest = make_interest_manager();
let contract = make_contract_key(1);
let peer = make_peer_key(10);
manager.add_downstream_subscriber(&contract, peer.clone());
interest.register_peer_interest(&contract, peer.clone(), None, true);
assert!(!manager.should_unsubscribe_upstream(&contract));
manager.remove_downstream_subscriber(&contract, &peer);
interest.remove_peer_interest(&contract, &peer);
assert!(!manager.has_downstream_subscribers(&contract));
assert!(manager.should_unsubscribe_upstream(&contract));
}
/// Removing an unknown peer is a noop; existing entries remain intact.
#[test]
fn test_unsubscribe_handler_unknown_peer_is_noop() {
let manager = HostingManager::new();
let contract = make_contract_key(2);
let known_peer = make_peer_key(20);
let unknown_peer = make_peer_key(99);
manager.add_downstream_subscriber(&contract, known_peer.clone());
assert!(!manager.remove_downstream_subscriber(&contract, &unknown_peer));
assert!(manager.has_downstream_subscribers(&contract));
assert!(!manager.should_unsubscribe_upstream(&contract));
}
/// Removing from an untracked contract is a noop; other contracts unaffected.
#[test]
fn test_unsubscribe_handler_unknown_contract_is_noop() {
let manager = HostingManager::new();
let known_contract = make_contract_key(3);
let unknown_contract = make_contract_key(99);
let peer = make_peer_key(30);
manager.add_downstream_subscriber(&known_contract, peer.clone());
assert!(!manager.remove_downstream_subscriber(&unknown_contract, &peer));
assert!(manager.has_downstream_subscribers(&known_contract));
assert!(!manager.has_downstream_subscribers(&unknown_contract));
}
/// `downstream_subscribers` is authoritative for unsubscribe decisions,
/// independent of `InterestManager` state.
#[test]
fn test_unsubscribe_dual_tracking_authority() {
let manager = HostingManager::new();
let interest = make_interest_manager();
let contract = make_contract_key(4);
let peer = make_peer_key(40);
manager.add_downstream_subscriber(&contract, peer.clone());
interest.register_peer_interest(&contract, peer.clone(), None, true);
manager.remove_downstream_subscriber(&contract, &peer);
assert!(manager.should_unsubscribe_upstream(&contract));
// InterestManager still tracks the peer — independent of unsubscribe decision
assert!(interest.remove_peer_interest(&contract, &peer));
}
/// A hosted contract with downstream subscribers must NOT be evicted
/// from the hosting cache even after TTL expires and cache is over budget.
/// Without this, interior peers would drop hosting → stop renewal → lose
/// upstream subscription → downstream subscribers lose their feed.
///
/// This test operates at the HostingCache level with MockTimeSrc so we
/// can actually advance time past TTL and verify the retain predicate.
#[test]
fn test_downstream_subscribers_protect_from_eviction() {
use crate::ring::hosting::cache::HostingCache;
use crate::util::time_source::SharedMockTimeSource;
let time = SharedMockTimeSource::new();
let min_ttl = Duration::from_secs(60);
// Budget of 150 bytes with 2x100-byte entries = over budget
let mut cache = HostingCache::new(150, min_ttl, time.clone());
let protected = make_contract_key(1);
let unprotected = make_contract_key(2);
cache.record_access(protected, 100, AccessType::Get);
cache.record_access(unprotected, 100, AccessType::Get);
assert_eq!(cache.current_bytes(), 200); // over budget
// Advance past TTL
time.advance_time(Duration::from_secs(61));
// Sweep with predicate that protects the first contract
// (simulates has_downstream_subscribers returning true)
let evicted = cache.sweep_expired(|k| *k == protected);
assert!(
!evicted.contains(&protected),
"Contract with downstream subscribers must not be evicted"
);
assert!(
evicted.contains(&unprotected),
"Unprotected contract should be evicted when over budget + past TTL"
);
assert!(cache.contains(&protected));
}
/// A hosted contract with NO subscribers and NO clients SHOULD be
/// evictable after TTL expires when the cache is over budget.
///
/// Uses HostingCache with MockTimeSrc for time advancement.
#[test]
fn test_no_subscribers_allows_eviction() {
use crate::ring::hosting::cache::HostingCache;
use crate::util::time_source::SharedMockTimeSource;
let time = SharedMockTimeSource::new();
let min_ttl = Duration::from_secs(60);
// Budget of 80 bytes, entry is 100 → over budget immediately
let mut cache = HostingCache::new(80, min_ttl, time.clone());
let contract = make_contract_key(100);
cache.record_access(contract, 100, AccessType::Get);
assert!(cache.contains(&contract));
// Under TTL: should not be evicted even though over budget
let evicted = cache.sweep_expired(|_| false);
assert!(
evicted.is_empty(),
"Contract within TTL should not be evicted"
);
// Advance past TTL
time.advance_time(Duration::from_secs(61));
// Now should be evicted (over budget + past TTL + no retain predicate)
let evicted = cache.sweep_expired(|_| false);
assert!(
evicted.contains(&contract),
"Contract past TTL with no subscribers should be evicted"
);
assert!(!cache.contains(&contract));
}
// =========================================================================
// Downstream Subscriber Limit Tests
// =========================================================================
#[test]
fn test_downstream_subscriber_limit_enforced() {
let manager = HostingManager::new();
let contract = make_contract_key(50);
// Use a small limit for testing to avoid issues with peer key generation.
// We test the limit logic by adding peers up to the constant and verifying rejection.
let limit = MAX_DOWNSTREAM_SUBSCRIBERS_PER_CONTRACT;
// Add `limit` peers — all should succeed
let mut peers = Vec::with_capacity(limit);
for i in 0..limit {
let peer = PeerKey(crate::transport::TransportPublicKey::from_bytes({
let mut bytes = [0u8; 32];
// Encode index across 3 bytes for safety
bytes[0] = (i & 0xFF) as u8;
bytes[1] = ((i >> 8) & 0xFF) as u8;
bytes[2] = ((i >> 16) & 0xFF) as u8;
bytes
}));
peers.push(peer.clone());
let result = manager.add_downstream_subscriber(&contract, peer);
assert!(
result,
"Downstream subscriber {i} should succeed within limit (count before: {i})"
);
}
// Verify the actual count
let actual_count = manager
.downstream_subscribers
.get(&contract)
.map(|e| e.len())
.unwrap_or(0);
assert_eq!(
actual_count, limit,
"Should have exactly {limit} entries, got {actual_count}"
);
// The next new peer (with completely different bytes) should be rejected
let extra_peer = PeerKey(crate::transport::TransportPublicKey::from_bytes([0xAA; 32]));
// Verify it's not in the set
let is_new = !manager
.downstream_subscribers
.get(&contract)
.map(|e| e.contains_key(&extra_peer))
.unwrap_or(false);
assert!(is_new, "Extra peer should not already be in the set");
let rejected = !manager.add_downstream_subscriber(&contract, extra_peer);
assert!(
rejected,
"Downstream subscriber beyond limit should be rejected (count was {actual_count})"
);
}
#[test]
fn test_downstream_subscriber_existing_peer_can_renew_at_limit() {
let manager = HostingManager::new();
let contract = make_contract_key(51);
// Fill up to the limit
let first_peer = make_peer_key(1);
manager.add_downstream_subscriber(&contract, first_peer.clone());
for i in 1..MAX_DOWNSTREAM_SUBSCRIBERS_PER_CONTRACT {
let peer = PeerKey(crate::transport::TransportPublicKey::from_bytes({
let mut bytes = [0u8; 32];
bytes[0] = (i & 0xFF) as u8;
bytes[1] = ((i >> 8) & 0xFF) as u8;
bytes
}));
manager.add_downstream_subscriber(&contract, peer);
}
// Existing peer can still renew (re-insert updates the timestamp)
assert!(
manager.add_downstream_subscriber(&contract, first_peer),
"Existing peer should be able to renew at limit"
);
}
// =========================================================================
// Regression tests for #3469: downstream_subscriber_count leak
// =========================================================================
/// Regression test: expire_stale_downstream_subscribers must return the
/// count of expired peers so the interest manager can be decremented.
#[test]
fn test_expire_returns_expired_count_for_interest_sync() {
let manager = HostingManager::new();
let interest = make_interest_manager();
let contract = make_contract_key(90);
let peer_a = make_peer_key(90);
let peer_b = make_peer_key(91);
// Register two downstream subscribers in both managers
manager.add_downstream_subscriber(&contract, peer_a.clone());
interest.add_downstream_subscriber(&contract);
manager.add_downstream_subscriber(&contract, peer_b.clone());
interest.add_downstream_subscriber(&contract);
// Verify interest manager tracks 2 downstream
let count = interest.with_local_interest(&contract, |li| li.downstream_subscriber_count);
assert_eq!(count, 2);
// Make both stale
if let Some(mut peers) = manager.downstream_subscribers.get_mut(&contract) {
let stale = Instant::now() - SUBSCRIPTION_LEASE_DURATION - Duration::from_secs(1);
peers.insert(peer_a, stale);
peers.insert(peer_b, stale);
}
// Expire and sync interest manager (mimics ring.rs TTL expiry path)
let expired = manager.expire_stale_downstream_subscribers();
assert_eq!(expired.len(), 1);
let (expired_contract, expired_count) = &expired[0];
assert_eq!(*expired_contract, contract);
assert_eq!(*expired_count, 2);
for _ in 0..*expired_count {
interest.remove_downstream_subscriber(expired_contract);
}
// Interest manager should now show 0 downstream
assert!(
!interest.has_local_interest(&contract),
"downstream_subscriber_count should be 0 after syncing with TTL expiry"
);
}
// =========================================================================
// Local Client Access Tests (#3769)
// =========================================================================
/// Core test for #3769: locally-accessed contracts should be included in
/// renewal, but relay-cached contracts should NOT.
#[test]
fn test_local_client_access_enables_renewal() {
let manager = HostingManager::new();
let local_contract = make_contract_key(1);
let relay_contract = make_contract_key(2);
// Both contracts get hosted via GET
manager.record_contract_access(local_contract, 1000, AccessType::Get);
manager.record_contract_access(relay_contract, 1000, AccessType::Get);
// Only the local one gets marked as locally accessed
manager.mark_local_client_access(&local_contract);
let needs_renewal = manager.contracts_needing_renewal();
assert!(
needs_renewal.contains(&local_contract),
"Locally-accessed contract should be in renewal list"
);
assert!(
!needs_renewal.contains(&relay_contract),
"Relay-cached contract should NOT be in renewal list"
);
}
/// Relay-only contracts at scale should not cause subscription storms.
/// Regression test for #3763/#3765 (the subscription storm incident).
#[test]
fn test_relay_cached_contracts_not_renewed_at_scale() {
let manager = HostingManager::new();
// Simulate 200 relay-cached contracts (no local_client_access)
for i in 0..200u8 {
let contract = make_contract_key(i);
manager.record_contract_access(contract, 1000, AccessType::Get);
}
// Mark only 2 as locally accessed (simulating River user)
let local_a = make_contract_key(42);
let local_b = make_contract_key(99);
manager.mark_local_client_access(&local_a);
manager.mark_local_client_access(&local_b);
let needs_renewal = manager.contracts_needing_renewal();
assert_eq!(
needs_renewal.len(),
2,
"Only 2 locally-accessed contracts should need renewal, found {}",
needs_renewal.len()
);
assert!(needs_renewal.contains(&local_a));
assert!(needs_renewal.contains(&local_b));
}
/// Locally-accessed contracts with active subscriptions should not be
/// double-counted in the renewal list.
#[test]
fn test_local_client_access_with_active_subscription_no_duplicate() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
manager.record_contract_access(contract, 1000, AccessType::Get);
manager.mark_local_client_access(&contract);
manager.subscribe(contract);
let needs_renewal = manager.contracts_needing_renewal();
// The contract has an active subscription that isn't expiring yet,
// and local_client_access. It should not appear (subscription is fresh).
assert!(
!needs_renewal.contains(&contract),
"Contract with fresh active subscription should not need renewal"
);
}
/// Marking and querying unknown contracts should be no-ops (no panic).
#[test]
fn test_local_client_access_unknown_contract() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
assert!(!manager.has_local_client_access(&contract));
manager.mark_local_client_access(&contract); // no-op, not in cache
assert!(!manager.has_local_client_access(&contract));
}
/// The local_client_access flag should be sticky -- once set, it should
/// persist even after the contract's access type changes.
#[test]
fn test_local_client_access_sticky_across_access_type_changes() {
let manager = HostingManager::new();
let contract = make_contract_key(1);
manager.record_contract_access(contract, 1000, AccessType::Get);
manager.mark_local_client_access(&contract);
assert!(manager.has_local_client_access(&contract));
// Refresh via a relay PUT -- should NOT clear the local flag
manager.record_contract_access(contract, 1000, AccessType::Put);
assert!(
manager.has_local_client_access(&contract),
"local_client_access should persist across access type changes"
);
}
/// Simulate restart: contracts loaded from disk with local_client_access
/// should appear in contracts_needing_renewal().
#[test]
fn test_local_client_access_survives_restart_via_load() {
let manager = HostingManager::new();
// Simulate loading from disk with local_client_access=true
{
let mut cache = manager.hosting_cache.write();
cache.load_persisted_entry(
make_contract_key(1),
1000,
cache::AccessType::Get,
std::time::Duration::from_secs(10),
true, // locally accessed before restart
);
cache.load_persisted_entry(
make_contract_key(2),
1000,
cache::AccessType::Get,
std::time::Duration::from_secs(10),
false, // relay-cached
);
cache.finalize_loading();
}
let needs_renewal = manager.contracts_needing_renewal();
assert!(
needs_renewal.contains(&make_contract_key(1)),
"Locally-accessed contract loaded from disk should be renewed"
);
assert!(
!needs_renewal.contains(&make_contract_key(2)),
"Relay-cached contract loaded from disk should NOT be renewed"
);
}
/// When a locally-accessed contract is evicted and re-added via relay,
/// the local_client_access flag should be cleared (relay doesn't set it).
#[test]
fn test_eviction_clears_local_client_access() {
// Small budget to force eviction
let manager = HostingManager::new();
// Override with a tiny cache
{
let mut cache = manager.hosting_cache.write();
*cache = cache::HostingCache::new(
200, // tiny budget: room for ~2 contracts at 100 bytes
std::time::Duration::ZERO, // no TTL protection
crate::util::time_source::InstantTimeSrc::new(),
);
}
let contract_a = make_contract_key(1);
let contract_b = make_contract_key(2);
let contract_c = make_contract_key(3);
// Add A (locally accessed) and B
manager.record_contract_access(contract_a, 100, AccessType::Get);
manager.mark_local_client_access(&contract_a);
manager.record_contract_access(contract_b, 100, AccessType::Get);
assert!(manager.has_local_client_access(&contract_a));
// Add C -- should evict A (oldest in LRU)
manager.record_contract_access(contract_c, 100, AccessType::Get);
assert!(
!manager.is_hosting_contract(&contract_a),
"contract_a should have been evicted"
);
// Re-add A via relay (no mark_local_client_access)
manager.record_contract_access(contract_a, 100, AccessType::Get);
assert!(
!manager.has_local_client_access(&contract_a),
"Re-added via relay should NOT have local_client_access"
);
// After local client re-accesses, flag is restored
manager.mark_local_client_access(&contract_a);
assert!(manager.has_local_client_access(&contract_a));
}
}