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
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
//! Sync manager and orchestration.
//!
//! **Purpose**: Coordinates periodic syncs, selects peers, and delegates to protocols.
//! **Strategy**: Try delta sync first, fallback to state sync on failure.
use std::collections::HashMap;
use std::pin::pin;
use std::sync::Arc;
use calimero_context_client::client::ContextClient;
use calimero_crypto::{Nonce, SharedKey};
use calimero_network_primitives::client::NetworkClient;
use calimero_network_primitives::stream::Stream;
use calimero_node_primitives::client::{NamespaceJoinParams, NodeClient, OpenSubgroupJoinParams};
use calimero_node_primitives::join_bundle::JoinBundle;
use calimero_node_primitives::sync::{InitPayload, MessagePayload, StreamMessage};
use calimero_primitives::common::DIGEST_SIZE;
use calimero_primitives::context::ContextId;
use calimero_primitives::identity::PublicKey;
use eyre::bail;
use eyre::WrapErr;
use futures_util::stream::{self};
use futures_util::StreamExt;
use libp2p::gossipsub::TopicHash;
use libp2p::PeerId;
use rand::seq::SliceRandom;
use rand::Rng;
use tokio::sync::{mpsc, oneshot};
use tokio::time::{self, Instant, MissedTickBehavior};
use tracing::{debug, error, info, warn};
use crate::sync_session_bridge::{
SyncSessionJob, SyncSessionResult, SyncSessionSendError, SyncSessionSender,
};
use crate::utils::choose_stream;
use super::config::SyncConfig;
use super::tracking::SyncState;
// Internal SyncProtocol for metrics (3 variants)
use super::tracking::SyncProtocol as TrackingSyncProtocol;
// Full SyncProtocol from primitives for protocol selection (7 variants, CIP §2.3)
// Uses shared state machine types for consistent behavior with simulation
use super::hash_comparison_protocol::{HashComparisonConfig, HashComparisonProtocol};
use super::level_sync::{LevelWiseConfig, LevelWiseProtocol};
use calimero_node_primitives::sync::{
build_handshake_from_raw, estimate_entity_count, estimate_max_depth, select_protocol,
SyncHandshake, SyncProtocol, SyncProtocolExecutor,
};
/// Network synchronization manager.
///
/// Orchestrates sync protocols: full resync, delta sync, state sync.
pub struct SyncManager {
pub(crate) sync_config: SyncConfig,
pub(super) node_client: NodeClient,
pub(super) context_client: ContextClient,
pub(crate) network_client: NetworkClient,
pub(super) node_state: crate::NodeState,
pub(super) ctx_sync_rx: Option<mpsc::Receiver<(Option<ContextId>, Option<PeerId>)>>,
pub(super) ns_sync_rx: Option<mpsc::Receiver<[u8; 32]>>,
pub(super) ns_join_rx: Option<
mpsc::Receiver<(
NamespaceJoinParams,
oneshot::Sender<eyre::Result<JoinBundle>>,
)>,
>,
pub(super) open_subgroup_join_rx: Option<
mpsc::Receiver<(
OpenSubgroupJoinParams,
oneshot::Sender<eyre::Result<Vec<u8>>>,
)>,
>,
/// Dispatch handle for the dedicated `SyncSessionActor` (#2316).
/// Set via [`SyncManager::set_session_handles`] after the actor is
/// started; `None` on freshly-cloned instances (which never run
/// the `start` loop) and on the original until wiring completes.
pub(super) session_tx: Option<SyncSessionSender>,
/// Channel the `SyncSessionActor` writes initiator results into so
/// `start` can update per-context tracking state. Consumed once by
/// `start`; `None` on clones.
pub(super) session_result_rx: Option<mpsc::UnboundedReceiver<SyncSessionResult>>,
/// Sync-protocol metrics collector. Installed by `run.rs::start` via
/// [`SyncManager::set_metrics`] after the [`crate::sync::PrometheusSyncMetrics`]
/// instance is registered against the global registry. `None` means
/// recording sites use [`crate::sync::no_op_metrics`] as a silent
/// fallback — never a panic and never a runtime cost beyond a vtable
/// no-op.
///
/// `dyn SyncMetricsCollector` does not implement `Debug`, so we
/// hand-write a `Debug` impl on `SyncManager` (below) that prints
/// only the presence/absence of this field — the inner vtable is
/// opaque anyway.
pub(crate) metrics: Option<Arc<dyn super::metrics::SyncMetricsCollector>>,
}
impl std::fmt::Debug for SyncManager {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SyncManager")
.field("sync_config", &self.sync_config)
.field("metrics_installed", &self.metrics.is_some())
.finish_non_exhaustive()
}
}
impl Clone for SyncManager {
fn clone(&self) -> Self {
Self {
sync_config: self.sync_config,
node_client: self.node_client.clone(),
context_client: self.context_client.clone(),
network_client: self.network_client.clone(),
node_state: self.node_state.clone(),
ctx_sync_rx: None,
ns_sync_rx: None,
ns_join_rx: None,
open_subgroup_join_rx: None,
// Cloned `SyncManager`s never drive the `start` loop, so
// they don't need a session-dispatch handle or a results
// receiver. The bridge holds its own clone of the
// SyncManager for issuing sessions.
session_tx: None,
session_result_rx: None,
// Clones share the same metrics handle — Arc keeps the
// recording surface unified across the original (which runs
// `start`) and every responder/initiator clone.
metrics: self.metrics.clone(),
}
}
}
/// True if `context_id` had a dispatch attempt recorded in `map` less
/// than `interval` ago.
///
/// Used in [`SyncManager::start`] to stop the every-tick re-attempt
/// storm after the `SyncSessionActor` mailbox returns `Full` (#2319): a
/// dropped dispatch records `Instant::now()` here, and we skip the
/// context until `interval` has elapsed. Distinct from `SyncState`'s
/// `last_sync` — touching that on a dropped dispatch is what #2317
/// forbids, since it would leave the context "in progress" forever with
/// no result to clear it.
pub(crate) fn dispatch_recently_attempted(
map: &HashMap<ContextId, time::Instant>,
context_id: &ContextId,
interval: time::Duration,
) -> bool {
map.get(context_id)
.is_some_and(|attempted| attempted.elapsed() < interval)
}
/// True if an initiator dispatched for `context_id` looks wedged: its
/// dispatch was recorded in `dispatched_at` more than `grace` ago and
/// the context's [`SyncState`] still shows it "in progress"
/// (`last_sync == None`), i.e. no `SyncSessionResult` ever cleared it.
///
/// The #2319 watchdog in [`SyncManager::start`] uses this to detect a
/// `SyncSessionActor` whose single arbiter thread is stuck in a
/// synchronous merkle/CRDT-merge loop the per-session
/// `tokio::time::timeout` can't preempt — when it returns true the
/// manager synthesises an `on_failure` so the context is eligible for a
/// fresh dispatch rather than logging "Sync already in progress" forever.
pub(crate) fn session_dispatch_wedged(
dispatched_at: &HashMap<ContextId, time::Instant>,
state: &HashMap<ContextId, SyncState>,
context_id: &ContextId,
grace: time::Duration,
) -> bool {
dispatched_at
.get(context_id)
.is_some_and(|dispatched| dispatched.elapsed() >= grace)
&& state
.get(context_id)
.is_some_and(|s| s.last_sync().is_none())
}
impl SyncManager {
pub(crate) fn new(
sync_config: SyncConfig,
node_client: NodeClient,
context_client: ContextClient,
network_client: NetworkClient,
node_state: crate::NodeState,
ctx_sync_rx: mpsc::Receiver<(Option<ContextId>, Option<PeerId>)>,
ns_sync_rx: mpsc::Receiver<[u8; 32]>,
ns_join_rx: mpsc::Receiver<(
NamespaceJoinParams,
oneshot::Sender<eyre::Result<JoinBundle>>,
)>,
open_subgroup_join_rx: mpsc::Receiver<(
OpenSubgroupJoinParams,
oneshot::Sender<eyre::Result<Vec<u8>>>,
)>,
) -> Self {
Self {
sync_config,
node_client,
context_client,
network_client,
node_state,
ctx_sync_rx: Some(ctx_sync_rx),
ns_sync_rx: Some(ns_sync_rx),
ns_join_rx: Some(ns_join_rx),
open_subgroup_join_rx: Some(open_subgroup_join_rx),
session_tx: None,
session_result_rx: None,
metrics: None,
}
}
/// Wire the `SyncSessionActor` handles onto the original
/// `SyncManager` instance after the actor is started in `run.rs`.
/// Must be called before [`SyncManager::start`]. No-op on cloned
/// instances (those never run the `start` loop).
pub(crate) fn set_session_handles(
&mut self,
session_tx: SyncSessionSender,
session_result_rx: mpsc::UnboundedReceiver<SyncSessionResult>,
) {
self.session_tx = Some(session_tx);
self.session_result_rx = Some(session_result_rx);
}
/// Install the sync-protocol metrics collector. Must be called before
/// any clones are taken; recording sites resolve `self.metrics` via
/// [`SyncManager::metrics`] (which falls back to a no-op collector if
/// this hasn't been called).
pub(crate) fn set_metrics(&mut self, metrics: Arc<dyn super::metrics::SyncMetricsCollector>) {
self.metrics = Some(metrics);
}
/// Resolve the metrics collector. Returns a static no-op handle when
/// no collector was installed so call sites never have to branch on
/// `Option` — `self.metrics().record_*()` is always valid.
pub(crate) fn metrics(&self) -> &dyn super::metrics::SyncMetricsCollector {
// The no-op fallback lives in a static OnceLock so it isn't
// allocated per call. `NoOpMetrics` is a unit struct with
// `Default`, so the init closure is `default()`.
static NOOP: std::sync::OnceLock<super::metrics::NoOpMetrics> = std::sync::OnceLock::new();
match self.metrics.as_deref() {
Some(m) => m,
None => NOOP.get_or_init(super::metrics::NoOpMetrics::default),
}
}
/// Build `SyncHandshake` from local context state for protocol negotiation.
///
/// Queries the real entity count and tree depth from the Merkle tree Index
/// via the storage bridge. Falls back to estimation from DAG heads if the
/// Index is not accessible (e.g., after snapshot sync with format mismatch).
///
/// # Arguments
///
/// * `context` - The context to build a handshake for.
///
/// # Returns
///
/// A `SyncHandshake` containing the context's current state summary.
fn build_local_handshake(
&self,
context: &calimero_primitives::context::Context,
) -> SyncHandshake {
let root_hash = *context.root_hash;
let dag_heads = context.dag_heads.clone();
// Try to get real entity count and depth from the Merkle tree Index.
// This gives accurate protocol selection instead of guessing from dag_heads.
let (entity_count, max_depth) = self.query_tree_stats(&context.id).unwrap_or_else(|| {
// Fallback: estimate from dag_heads if Index is unavailable
let count = estimate_entity_count(root_hash, dag_heads.len());
let depth = estimate_max_depth(count);
(count, depth)
});
build_handshake_from_raw(root_hash, entity_count, max_depth, dag_heads)
}
/// Query real entity count and tree depth from the Merkle tree Index.
///
/// Returns `Some((entity_count, max_depth))` on success, `None` if the
/// Index is unavailable (e.g., fresh node or deserialization mismatch).
fn query_tree_stats(&self, context_id: &ContextId) -> Option<(u64, u32)> {
use calimero_node_primitives::sync::create_runtime_env;
use calimero_storage::address::Id;
use calimero_storage::env::with_runtime_env;
use calimero_storage::index::Index;
use calimero_storage::store::MainStorage;
let store = self.context_client.datastore_handle().into_inner();
// SAFETY: identity is unused for read-only Index queries via RuntimeEnv
let identity = calimero_primitives::identity::PublicKey::from([0u8; 32]);
let env = create_runtime_env(&store, *context_id, identity);
let root_id = Id::new(*context_id.as_ref());
with_runtime_env(env, || {
// Check if root Index exists
let root_index = Index::<MainStorage>::get_index(root_id).ok().flatten()?;
// Count children (leaf entities) under root.
// Minimum 1 when root exists (consistent with fallback estimation).
let children = root_index.children().unwrap_or_default();
let entity_count = (children.len() as u64).max(1);
// Depth: 1 when root has data (consistent with fallback).
// For deeper trees, we'd need recursive traversal — tracked in #2054.
let max_depth = 1;
Some((entity_count, max_depth))
})
}
/// Build `SyncHandshake` from peer state for protocol negotiation.
///
/// Uses shared estimation functions from `calimero_node_primitives::sync::state_machine`
/// to ensure consistent behavior between production (`SyncManager`) and simulation (`SimNode`).
fn build_remote_handshake(
peer_root_hash: calimero_primitives::hash::Hash,
peer_dag_heads: &[[u8; DIGEST_SIZE]],
) -> SyncHandshake {
let root_hash = *peer_root_hash;
// Use shared estimation functions for consistency with simulation
let entity_count = estimate_entity_count(root_hash, peer_dag_heads.len());
let max_depth = estimate_max_depth(entity_count);
build_handshake_from_raw(root_hash, entity_count, max_depth, peer_dag_heads.to_vec())
}
pub async fn start(mut self) {
let mut next_sync = time::interval(self.sync_config.frequency);
next_sync.set_missed_tick_behavior(MissedTickBehavior::Delay);
let mut state = HashMap::<_, SyncState>::new();
// #2319: per-context "last dispatch attempt" instants. When a
// `try_send` to the `SyncSessionActor` returns `Full`/`Closed`
// we record the context here and skip re-dispatching it until
// `sync_config.interval` has elapsed — otherwise every interval
// tick (and every heartbeat-driven re-trigger) re-attempts and
// re-drops, which is the wedge in #2319. A real
// `SyncSessionResult` clears the entry. Distinct from
// `SyncState.last_sync`, which #2317 forbids touching on a
// dropped dispatch.
let mut last_dispatch_attempt = HashMap::<ContextId, time::Instant>::new();
// #2319: watchdog. Once an initiator is dispatched (Phase 3
// below clears the context's `last_sync` to `None`), a
// `SyncSessionResult` is supposed to arrive and call
// `on_success`/`on_failure` to clear it. If the `SyncSessionActor`
// wedges — its single arbiter thread stuck in a synchronous
// merkle/CRDT-merge loop that the per-session `tokio::time::timeout`
// can't preempt, or its mailbox saturated by such sessions — that
// result never comes and the context stays "in progress" forever
// ("Sync already in progress" on every tick, never converging
// again). Record when we dispatched; if no result has cleared
// the entry within `SESSION_WEDGE_GRACE` we synthesise an
// `on_failure` so the context becomes eligible again and the
// periodic loop retries with a fresh dispatch. The entry is
// cleared by the real result on `session_result_rx`.
const SESSION_WEDGE_GRACE_MULTIPLIER: u32 = 2;
let session_wedge_grace =
self.sync_config.session_deadline * SESSION_WEDGE_GRACE_MULTIPLIER;
let mut initiator_dispatched_at = HashMap::<ContextId, time::Instant>::new();
// #2319: rate-limit the "mailbox full" warn to ≤1 per context
// per `MAILBOX_FULL_SUMMARY_WINDOW`; the rest roll up into one
// info line per window so the wedge is still visible without
// drowning the log.
const MAILBOX_FULL_SUMMARY_WINDOW: time::Duration = time::Duration::from_secs(60);
let mut last_full_warn = HashMap::<ContextId, time::Instant>::new();
let mut full_drops_in_window: u64 = 0;
let mut full_window_started = time::Instant::now();
let mut requested_ctx = None;
let mut requested_peer = None;
let Some(mut ctx_sync_rx) = self.ctx_sync_rx.take() else {
error!("SyncManager can only be run once");
return;
};
let mut ns_sync_rx = self.ns_sync_rx.take().unwrap_or_else(|| {
let (_tx, rx) = mpsc::channel(1);
rx
});
let mut ns_join_rx = self.ns_join_rx.take().unwrap_or_else(|| {
let (_tx, rx) = mpsc::channel(1);
rx
});
let mut open_subgroup_join_rx = self.open_subgroup_join_rx.take().unwrap_or_else(|| {
let (_tx, rx) = mpsc::channel(1);
rx
});
let Some(session_tx) = self.session_tx.clone() else {
error!("SyncManager started without a SyncSessionActor handle (#2316)");
return;
};
let Some(mut session_result_rx) = self.session_result_rx.take() else {
error!("SyncManager started without a SyncSessionActor result channel (#2316)");
return;
};
// Apply a session result to per-context tracking state. Mirrors
// the body of the legacy `advance` helper, which read from a
// local `FuturesUnordered<futs>` before #2316 moved session
// execution onto a dedicated arbiter.
fn apply_session_result(
state: &mut HashMap<ContextId, SyncState>,
result: SyncSessionResult,
) {
let SyncSessionResult {
context_id,
peer_id,
took,
result,
} = result;
let _ignored = state.entry(context_id).and_modify(|state| match result {
Ok(Ok(ref protocol)) => {
state.on_success(peer_id, TrackingSyncProtocol::from(protocol));
info!(
%context_id,
?took,
?protocol,
success_count = state.success_count,
"Sync finished successfully"
);
}
Ok(Err(ref err)) => {
state.on_failure(err.to_string());
warn!(
%context_id,
?took,
error = %err,
failure_count = state.failure_count(),
backoff_secs = state.backoff_delay().as_secs(),
"Sync failed, applying exponential backoff"
);
}
Err(ref timeout_err) => {
state.on_failure(timeout_err.to_string());
warn!(
%context_id,
?took,
failure_count = state.failure_count(),
backoff_secs = state.backoff_delay().as_secs(),
"Sync timed out, applying exponential backoff"
);
}
});
}
loop {
tokio::select! {
_ = next_sync.tick() => {
debug!("Performing interval sync");
// #2319: roll up rate-limited mailbox-full drops.
if full_window_started.elapsed() >= MAILBOX_FULL_SUMMARY_WINDOW {
if full_drops_in_window > 0 {
info!(
full_drops_in_window,
contexts_affected = last_full_warn.len(),
"SyncSession mailbox-full drop rollup (last {:?}) (#2319)",
MAILBOX_FULL_SUMMARY_WINDOW
);
}
full_drops_in_window = 0;
full_window_started = time::Instant::now();
last_full_warn.retain(|_, t| t.elapsed() < MAILBOX_FULL_SUMMARY_WINDOW);
}
// #2319 watchdog: any context whose initiator was
// dispatched longer than `session_wedge_grace` ago
// and is still flagged "in progress" — its session
// (or the whole `SyncSessionActor`) is wedged and no
// `SyncSessionResult` is coming. Synthesise a
// failure so the context is eligible again; the
// dispatch loop below will retry it this tick.
let wedged: Vec<ContextId> = initiator_dispatched_at
.keys()
.copied()
.filter(|context_id| {
session_dispatch_wedged(
&initiator_dispatched_at,
&state,
context_id,
session_wedge_grace,
)
})
.collect();
// Drop the dispatch record either way once past grace
// (a still-in-progress entry that's been there >grace
// is what we just failed; a no-longer-in-progress one
// already got a result that removed it, but be tidy).
initiator_dispatched_at
.retain(|_, dispatched_at| dispatched_at.elapsed() < session_wedge_grace);
for context_id in wedged {
warn!(
%context_id,
grace = ?session_wedge_grace,
"SyncSession initiator produced no result within watchdog grace — assuming a wedged session/actor; failing it so periodic-sync retries (#2319)"
);
if let Some(s) = state.get_mut(&context_id) {
s.on_failure(
"sync session wedged — no SyncSessionResult within watchdog grace (#2319)"
.to_owned(),
);
}
}
}
Some(result) = session_result_rx.recv() => {
// #2319: a real result means this context is no
// longer wedged behind a full mailbox — drop the
// dispatch-attempt backoff so it isn't throttled —
// and the watchdog timer for it is satisfied.
let _removed = last_dispatch_attempt.remove(&result.context_id);
let _removed = initiator_dispatched_at.remove(&result.context_id);
apply_session_result(&mut state, result);
continue;
}
Some(namespace_id) = ns_sync_rx.recv() => {
info!(
namespace_id = %hex::encode(namespace_id),
"Performing namespace governance sync"
);
self.sync_namespace_from_peer(namespace_id).await;
continue;
}
Some((params, reply_tx)) = ns_join_rx.recv() => {
info!(
namespace_id = %hex::encode(params.namespace_id),
"Processing namespace join request (initiator side)"
);
let result = self.initiate_namespace_join(params).await;
let _ignored = reply_tx.send(result);
continue;
}
Some((params, reply_tx)) = open_subgroup_join_rx.recv() => {
info!(
namespace_id = %hex::encode(params.namespace_id),
subgroup_id = %hex::encode(params.subgroup_id),
"Processing open-subgroup join request (initiator side)"
);
let result = self.initiate_open_subgroup_join(params).await;
let _ignored = reply_tx.send(result);
continue;
}
Some((ctx, peer)) = ctx_sync_rx.recv() => {
info!(?ctx, ?peer, "Received sync request");
requested_ctx = ctx;
requested_peer = peer;
// CRITICAL FIX: Drain all other pending sync requests in the queue.
// When multiple contexts join rapidly (common in E2E tests), they all
// call sync() which queues requests in ctx_sync_rx. The old code only
// processed ONE request per loop iteration, leaving contexts 2-N queued
// indefinitely. This caused those contexts to never sync and remain
// with dag_heads=[] and Uninitialized errors.
//
// Solution: Use try_recv() to drain all buffered requests immediately,
// then trigger a full sync that will process all contexts.
let mut drained_count = 0;
while ctx_sync_rx.try_recv().is_ok() {
drained_count += 1;
}
if drained_count > 0 {
info!(drained_count, "Drained additional sync requests from queue, will sync all contexts");
// Clear requested_ctx to force syncing ALL contexts
// This ensures newly-joined contexts get synced even if they weren't first in queue
requested_ctx = None;
requested_peer = None;
}
}
}
let requested_ctx = requested_ctx.take();
let requested_peer = requested_peer.take();
let contexts = requested_ctx
.is_none()
.then(|| self.context_client.get_context_ids(None));
let contexts = stream::iter(requested_ctx)
.map(Ok)
.chain(stream::iter(contexts).flatten());
let mut contexts = pin!(contexts);
while let Some(context_id) = contexts.next().await {
let context_id = match context_id {
Ok(context_id) => context_id,
Err(err) => {
error!(%err, "Failed reading context id to sync");
continue;
}
};
// #2319: respect the dispatch-attempt backoff. After a
// `Full` mailbox we wait one `interval` before retrying
// this context rather than re-attempting (and re-dropping)
// on every tick. Explicit requests bypass it, same as the
// recency override below.
if requested_ctx.is_none()
&& dispatch_recently_attempted(
&last_dispatch_attempt,
&context_id,
self.sync_config.interval,
)
{
debug!(%context_id, "Skipping sync — dispatch recently attempted, mailbox was full (#2319)");
continue;
}
// Phase 1: read-only eligibility check. We must not
// mutate `state` here because a failed `try_send`
// below would leave `last_sync = None` with no future
// result to clear it — permanently stalling the
// context (Cursor bugbot #2317).
let is_first_sync = match state.get(&context_id) {
Some(existing) => {
let Some(last_sync) = existing.last_sync() else {
debug!(%context_id, "Sync already in progress");
continue;
};
let minimum = self.sync_config.interval;
let time_since = last_sync.elapsed();
if time_since < minimum {
if requested_ctx.is_none() {
debug!(%context_id, ?time_since, ?minimum, "Skipping sync, last one was too recent");
continue;
}
debug!(%context_id, ?time_since, ?minimum, "Force syncing despite recency, due to explicit request");
}
false
}
None => true,
};
info!(%context_id, "Scheduled sync");
// Phase 2: dispatch BEFORE mutating state — so a
// `Full`/`Closed` outcome leaves the per-context
// tracking state untouched and the next interval
// tick (or heartbeat trigger) just retries.
let dispatched = match session_tx.try_send(SyncSessionJob::Initiator {
context_id,
peer_id: requested_peer,
}) {
Ok(()) => true,
Err(SyncSessionSendError::Full) => {
full_drops_in_window += 1;
let warn_now = last_full_warn
.get(&context_id)
.is_none_or(|t| t.elapsed() >= MAILBOX_FULL_SUMMARY_WINDOW);
if warn_now {
warn!(
%context_id,
"SyncSession actor mailbox full — skipping initiator dispatch; backing off this context for {:?} (#2316/#2319)",
self.sync_config.interval
);
let _prev = last_full_warn.insert(context_id, time::Instant::now());
} else {
debug!(%context_id, "SyncSession actor mailbox full — skipping (rate-limited; see periodic rollup) (#2319)");
}
false
}
Err(SyncSessionSendError::Closed) => {
warn!(
%context_id,
"SyncSession actor closed — skipping initiator dispatch"
);
false
}
};
if !dispatched {
// #2319: record the failed attempt so the next
// interval tick backs off instead of re-dropping.
let _prev = last_dispatch_attempt.insert(context_id, time::Instant::now());
continue;
}
// Phase 3: dispatch succeeded — mark the context as
// in-flight. A `SyncSessionResult` will arrive on
// `session_result_rx` and call `on_success` /
// `on_failure` to clear the flag — or, if it never does,
// the #2319 watchdog above fails it after the grace.
let _prev = initiator_dispatched_at.insert(context_id, time::Instant::now());
if is_first_sync {
info!(%context_id, "Syncing for the first time");
let mut new_state = SyncState::new();
new_state.start();
let _ignored = state.insert(context_id, new_state);
} else if let Some(existing) = state.get_mut(&context_id) {
let _ignored = existing.take_last_sync();
}
}
}
}
pub(crate) async fn perform_interval_sync(
&self,
context_id: ContextId,
peer_id: Option<PeerId>,
) -> eyre::Result<(PeerId, SyncProtocol)> {
if let Some(peer_id) = peer_id {
return self.initiate_sync(context_id, peer_id).await;
}
// Check if we're uninitialized before peer discovery so we can use
// a longer mesh wait window for bootstrap scenarios.
let context = self
.context_client
.get_context(&context_id)?
.ok_or_else(|| eyre::eyre!("Context not found: {}", context_id))?;
let is_uninitialized = *context.root_hash == [0; 32];
// Retry peer discovery if mesh is still forming.
// Uninitialized nodes need a longer wait window (10s vs 1.5s) to avoid
// getting stuck before first snapshot sync. Gossipsub mesh takes 5-10
// heartbeats (~5-10s) to add a new subscriber after topic subscription.
let (max_retries, retry_delay_ms) = if is_uninitialized {
(
super::config::DEFAULT_MESH_RETRIES_UNINITIALIZED,
super::config::DEFAULT_MESH_RETRY_DELAY_MS_UNINITIALIZED,
)
} else {
(
super::config::DEFAULT_MESH_RETRIES_INITIALIZED,
super::config::DEFAULT_MESH_RETRY_DELAY_MS_INITIALIZED,
)
};
let mesh_discovery_start = Instant::now();
let mut peers = Vec::new();
let mut final_attempt = 0u32;
for attempt in 1..=max_retries {
final_attempt = attempt;
peers = self
.network_client
.mesh_peers(TopicHash::from_raw(context_id))
.await;
if !peers.is_empty() {
break;
}
if attempt < max_retries {
debug!(
%context_id,
attempt,
is_uninitialized,
max_retries,
"No peers found yet, mesh may still be forming, retrying..."
);
time::sleep(std::time::Duration::from_millis(retry_delay_ms)).await;
}
}
let mesh_elapsed = mesh_discovery_start.elapsed();
if peers.is_empty() {
// Gossipsub mesh for the context topic hasn't formed yet (takes 5-10
// heartbeats / ~5-10s after subscription). Try namespace mesh peers as
// a fallback: the namespace topic's mesh is established during join
// with a 2-second grace period, so namespace peers are available even
// when the context-specific gossipsub mesh is still being built.
// Direct-stream context sync works over any connected P2P peer.
//
// `get_context_group_id` returns the context's IMMEDIATE owning group,
// which for a context owned by a subgroup is the subgroup id, not the
// namespace root. Only namespace roots ever have `ns/<id>` topics
// subscribed (see `NodeClient::subscribe_namespace`), so we must walk
// up the parent chain to find the root before computing the fallback
// topic. Without this walk, contexts owned by subgroups always get 0
// peers from the fallback and sync fails during the 5-10s cold-start
// window. `resolve_namespace` on a root group is a no-op (returns the
// same id), so behaviour for namespace-root-owned contexts is
// unchanged.
if let Ok(Some(group_id)) = self.context_client.get_context_group_id(&context_id) {
let store = self.context_client.datastore_handle().into_inner();
let ns_id_bytes = calimero_context::group_store::resolve_namespace(
&store,
&calimero_context_config::types::ContextGroupId::from(group_id),
)
.map(|id| id.to_bytes())
.unwrap_or_else(|err| {
// Errors here are rare and always indicate something worth
// investigating: store I/O failure or a circular parent chain
// exceeding MAX_NAMESPACE_DEPTH. Surface them before falling
// back so this debugging-focused code path doesn't hide real
// data-integrity bugs. Falling back to the immediate owning
// group preserves pre-fix behaviour rather than aborting the
// whole sync attempt.
warn!(
%context_id,
%err,
"failed to resolve namespace root for fallback topic; \
using immediate group id as best-effort"
);
group_id
});
let ns_topic = TopicHash::from_raw(format!("ns/{}", hex::encode(ns_id_bytes)));
let ns_peers = self.network_client.mesh_peers(ns_topic).await;
if !ns_peers.is_empty() {
info!(
%context_id,
peer_count = ns_peers.len(),
is_uninitialized,
"context gossipsub mesh not ready; falling back to namespace mesh peers"
);
peers = ns_peers;
}
}
}
if peers.is_empty() {
warn!(
%context_id,
is_uninitialized,
attempts = max_retries,
?mesh_elapsed,
"Mesh peer discovery exhausted all retries"
);
bail!("No peers to sync with for context {}", context_id);
}
info!(
%context_id,
peer_count = peers.len(),
attempts = final_attempt,
?mesh_elapsed,
is_uninitialized,
peers = ?peers,
"Mesh peer discovery succeeded"
);
if is_uninitialized {
// When uninitialized, we need to bootstrap from a peer that HAS data
// Trying random peers can result in querying other uninitialized nodes
info!(
%context_id,
peer_count = peers.len(),
"Node is uninitialized, selecting peer with state for bootstrapping"
);
// Try to find a peer with actual state
match self.find_peer_with_state(context_id, &peers).await {
Ok(peer_id) => {
info!(%context_id, %peer_id, "Found peer with state, syncing from them");
return self.initiate_sync(context_id, peer_id).await;
}
Err(e) => {
warn!(%context_id, error = %e, "Failed to find peer with state, falling back to random selection");
// Fall through to random selection
}
}
}
// Normal sync: try peers serially. Parallelising `initiate_sync` for
// the same context is unsafe — the sync protocol mutates per-context
// state (sync-in-progress marker at snapshot.rs:581, sync sessions at
// state.rs:235, snapshot-page cleanup in `request_and_apply_snapshot_pages`
// which documents "assumes no concurrent writes") and futures cancelled
// mid-flight can leak a sync session into the DashMap, causing
// `should_buffer_delta` to return true permanently. Tail-latency
// benefit is still obtained from the parallel probe above, which
// narrows this loop to "try a known-good peer first".
//
// Peer order: random shuffle, then stable-partition so peers we
// have observed signing applied messages with an
// Owner/Admin/ReadOnlyTee identity come first. Anchors are the
// peers whose canonical view is authoritative — targeting them
// first reduces the chance of pulling from a peer that's
// behind or divergent. Plain members still get tried if all
// anchors fail. Empty cache or context with no observed anchor
// peers degrades to plain random selection.
let mut shuffled: Vec<libp2p::PeerId> = peers
.choose_multiple(&mut rand::thread_rng(), peers.len())
.copied()
.collect();
let anchor_count = partition_peers_anchor_first(
&mut shuffled,
&self.node_state.peer_identities,
&self.anchor_identities_for_context(&context_id),
);
if anchor_count > 0 {
debug!(
%context_id,
anchor_peer_count = anchor_count,
non_anchor_peer_count = shuffled.len() - anchor_count,
"Preferring anchor peers for sync"
);
} else {
debug!(
%context_id,
peer_count = shuffled.len(),
"No anchor peers connected — falling back to random selection"
);
}
for peer_id in &shuffled {
if let Ok(result) = self.initiate_sync(context_id, *peer_id).await {
return Ok(result);
}
}
bail!("Failed to sync with any peer for context {}", context_id)
}
/// Look up the trusted-anchor identity set for the group that owns
/// `context_id` (Owner, Admins, ReadOnlyTee members). Returns an
/// empty set on any failure — context not registered to a group,
/// store read error, or no meta written yet. Callers fall back to
/// plain random peer selection on an empty set.
fn anchor_identities_for_context(
&self,
context_id: &ContextId,
) -> std::collections::BTreeSet<calimero_primitives::identity::PublicKey> {
let store = self.context_client.datastore_handle().into_inner();
let Ok(Some(group_id)) =
calimero_context::group_store::get_group_for_context(&store, context_id)
else {
return std::collections::BTreeSet::new();
};
self.anchor_identities_for_group(&group_id)
}
/// Look up the trusted-anchor identity set for a group directly.
/// Preferred over [`Self::anchor_identities_for_context`] when the
/// caller already knows `group_id` — late-joiner nodes can have a
/// missing context→group mapping, which makes the context-keyed
/// lookup return an empty set even though the group's anchors are
/// well-defined on the local node.
fn anchor_identities_for_group(
&self,
group_id: &calimero_context_config::types::ContextGroupId,
) -> std::collections::BTreeSet<calimero_primitives::identity::PublicKey> {
let store = self.context_client.datastore_handle().into_inner();
calimero_context::group_store::trusted_anchors_for_group(&store, group_id)
.unwrap_or_default()
}
/// Find a peer that has state (non-zero root_hash and non-empty DAG heads)
///
/// This is critical for bootstrapping newly joined nodes. Without this,
/// uninitialized nodes may query other uninitialized nodes, resulting in
/// all nodes remaining uninitialized.
///
/// Peers are probed concurrently so a single slow/unreachable peer no
/// longer stalls the entire discovery. The first peer to report state
/// wins and remaining probes are cancelled when this function returns.
async fn find_peer_with_state(
&self,
context_id: ContextId,
peers: &[PeerId],
) -> eyre::Result<PeerId> {
use calimero_node_primitives::sync::{InitPayload, MessagePayload, StreamMessage};
// Get our identity for handshake
let identities = self
.context_client
.get_context_members(&context_id, Some(true));
let Some((our_identity, _)) = choose_stream(identities, &mut rand::thread_rng())
.await
.transpose()?
else {
bail!("no owned identities found for context: {}", context_id);
};
let timeout_budget = self.sync_config.timeout / 6;
let concurrency = self
.sync_config
.peer_state_probe_concurrency
.min(peers.len())
.max(1);
debug!(
%context_id,
peer_count = peers.len(),
concurrency,
"Probing peers for state in parallel"
);
// Each probe opens a P2P stream, sends one `DagHeadsRequest`, and
// reads the response. When we find a peer with state and return, the
// remaining in-flight probes are dropped without sending a close
// frame; libp2p's idle-timeout handles the cleanup, and the peer may
// log a write-error if it was mid-response. This is an accepted
// trade-off — the probe is read-only on the local node, so there is
// no partial state to unwind, and adding an explicit graceful-close
// path would require async work in `Drop`, which Rust does not
// support cleanly.
let mut probes = stream::iter(peers.iter().copied())
.map(|peer_id| async move {
let outcome = async {
let mut stream = self.network_client.open_stream(peer_id).await?;
let request_msg = StreamMessage::Init {
context_id,
party_id: our_identity,
payload: InitPayload::DagHeadsRequest { context_id },
next_nonce: rand::thread_rng().gen(),
};
self.send(&mut stream, &request_msg, None).await?;
let Some(response) =
super::stream::recv(&mut stream, None, timeout_budget).await?
else {
return Ok::<_, eyre::Error>(None);
};
if let StreamMessage::Message {
payload:
MessagePayload::DagHeadsResponse {
dag_heads,
root_hash,
},
..
} = response
{
// Peer has state if root_hash is not zeros (dag_heads may
// be empty for migrated/legacy contexts).
let has_state = *root_hash != [0; 32];
let heads_count = dag_heads.len();
debug!(
%context_id,
%peer_id,
heads_count,
%root_hash,
has_state,
"Received DAG heads from peer"
);
Ok(Some((has_state, heads_count, root_hash)))
} else {
Ok(None)
}
}
.await;
(peer_id, outcome)
})
.buffer_unordered(concurrency);
while let Some((peer_id, outcome)) = probes.next().await {
match outcome {
Ok(Some((true, heads_count, root_hash))) => {
info!(
%context_id,
%peer_id,
heads_count,
%root_hash,
"Found peer with state for bootstrapping"
);
return Ok(peer_id);
}
Ok(Some((false, _, _))) => {
debug!(%context_id, %peer_id, "peer reported no state");
}
Ok(None) => {
debug!(%context_id, %peer_id, "peer did not return DAG heads");
}
Err(e) => {
debug!(%context_id, %peer_id, error = %e, "peer probe failed");
}
}
}
bail!("No peers with state found for context {}", context_id)
}
async fn initiate_sync(
&self,
context_id: ContextId,
peer_id: PeerId,
) -> eyre::Result<(PeerId, SyncProtocol)> {
let start = Instant::now();
info!(%context_id, %peer_id, "Attempting to sync with peer");
// Metrics: every sync attempt goes through this chokepoint, so
// `sync_start / sync_complete / sync_failure` here covers every
// protocol path. We don't yet know the protocol on entry — pass
// "unknown"; the success arm overwrites with the protocol the
// negotiated path actually chose.
self.metrics()
.record_sync_start(&context_id.to_string(), "unknown", "interval");
let protocol = match self.initiate_sync_inner(context_id, peer_id).await {
Ok(protocol) => protocol,
Err(err) => {
warn!(
%context_id,
%peer_id,
error = %err,
"Sync attempt failed for peer"
);
self.metrics().record_sync_failure(
&context_id.to_string(),
"unknown",
err.to_string().as_str(),
);
return Err(err);
}
};
let took = start.elapsed();
info!(%context_id, %peer_id, ?took, ?protocol, "Sync with peer completed successfully");
// Use the variant-only `SyncProtocolKind` for the protocol label
// so it matches the fixed `KNOWN_PROTOCOLS` set in
// `PrometheusSyncMetrics::sanitize_protocol`. Formatting the
// data-carrying `SyncProtocol` with `{:?}` would yield strings
// like `HashComparison { root_hash: [...], divergent_subtrees: [...] }`
// which never match the sanitiser and would label every sync
// `protocol="unknown"`, breaking the per-protocol slicing on
// `sync_successes_total` and `sync_duration_seconds`.
//
// `entities_transferred` is not threaded back to the sync manager
// today; pass 0. The collector still records the duration histogram
// and a sync_successes increment, which are the two most useful
// signals on a dashboard.
self.metrics().record_sync_complete(
&context_id.to_string(),
&format!("{:?}", protocol.kind()),
took,
0,
);
Ok((peer_id, protocol))
}
/// Sends a message over the stream (delegates to stream module).
pub(super) async fn send(
&self,
stream: &mut Stream,
message: &StreamMessage<'_>,
shared_key: Option<(SharedKey, Nonce)>,
) -> eyre::Result<()> {
super::stream::send(stream, message, shared_key).await
}
/// Receives a message from the stream (delegates to stream module).
pub(super) async fn recv(
&self,
stream: &mut Stream,
shared_key: Option<(SharedKey, Nonce)>,
) -> eyre::Result<Option<StreamMessage<'static>>> {
let budget = self.sync_config.timeout / 3;
super::stream::recv(stream, shared_key, budget).await
}
/// Get blob ID and application config from application or context config
async fn get_blob_info(
&self,
context_id: &ContextId,
application: &Option<calimero_primitives::application::Application>,
) -> eyre::Result<(
calimero_primitives::blobs::BlobId,
Option<calimero_primitives::application::Application>,
)> {
if let Some(ref app) = application {
Ok((app.blob.bytecode, None))
} else {
// Application not found - get blob_id from context config
let app_config = self
.context_client
.get_context_application(context_id)
.await?;
Ok((app_config.blob.bytecode, Some(app_config)))
}
}
/// Get application size from application, cached config, or context config
async fn get_application_size(
&self,
context_id: &ContextId,
application: &Option<calimero_primitives::application::Application>,
app_config_opt: &Option<calimero_primitives::application::Application>,
) -> eyre::Result<u64> {
if let Some(ref app) = application {
Ok(app.size)
} else if let Some(ref app_config) = app_config_opt {
Ok(app_config.size)
} else {
let app_config = self
.context_client
.get_context_application(context_id)
.await?;
Ok(app_config.size)
}
}
/// Get application source from cached config or context config
async fn get_application_source(
&self,
context_id: &ContextId,
app_config_opt: &Option<calimero_primitives::application::Application>,
) -> eyre::Result<calimero_primitives::application::ApplicationSource> {
if let Some(ref app_config) = app_config_opt {
Ok(app_config.source.clone())
} else {
let app_config = self
.context_client
.get_context_application(context_id)
.await?;
Ok(app_config.source.clone())
}
}
/// Install bundle application after blob sharing completes.
///
/// Returns `Some(installed_application)` if a bundle was installed,
/// `None` otherwise. Updates `context.application_id` if the installed
/// ApplicationId differs from the context's ApplicationId.
async fn install_bundle_after_blob_sharing(
&self,
context_id: &ContextId,
blob_id: &calimero_primitives::blobs::BlobId,
app_config_opt: &Option<calimero_primitives::application::Application>,
context: &mut calimero_primitives::context::Context,
application: &mut Option<calimero_primitives::application::Application>,
) -> eyre::Result<()> {
// Only proceed if blob is now available locally
if !self.node_client.has_blob(blob_id)? {
return Ok(());
}
// Check if blob is a bundle
let Some(blob_bytes) = self.node_client.get_blob_bytes(blob_id, None).await? else {
return Ok(());
};
// Wrap blocking I/O in spawn_blocking to avoid blocking async runtime
let blob_bytes_clone = blob_bytes.clone();
let is_bundle =
tokio::task::spawn_blocking(move || NodeClient::is_bundle_blob(&blob_bytes_clone))
.await?;
// Get source from context config (use cached if available, otherwise fetch)
let source = self
.get_application_source(context_id, app_config_opt)
.await?;
let installed_app_id = if is_bundle {
self.node_client
.install_application_from_bundle_blob(blob_id, &source)
.await
.map_err(|e| {
eyre::eyre!(
"Failed to install bundle application from blob {}: {}",
blob_id,
e
)
})?
} else {
// For non-bundle apps, write ApplicationMeta directly under the
// known application_id rather than re-deriving it via
// install_application (which hashes source+metadata and would
// produce a different ID than the original installer used).
let size = blob_bytes.len() as u64;
let mut handle = self.context_client.datastore_handle();
handle.put(
&calimero_store::key::ApplicationMeta::new(context.application_id),
&calimero_store::types::ApplicationMeta::new(
calimero_store::key::BlobMeta::new(*blob_id),
size,
source.to_string().into_boxed_str(),
Box::default(),
calimero_store::key::BlobMeta::new(calimero_primitives::blobs::BlobId::from(
[0u8; 32],
)),
"unknown".to_owned().into_boxed_str(),
"0.0.0".to_owned().into_boxed_str(),
String::new().into_boxed_str(),
),
)?;
context.application_id
};
// Verify installation succeeded by fetching the installed application
let installed_application = self
.node_client
.get_application(&installed_app_id)
.map_err(|e| {
eyre::eyre!(
"Failed to verify bundle installation for application {}: {}",
installed_app_id,
e
)
})?;
let Some(installed_application) = installed_application else {
bail!(
"Bundle installation reported success but application {} is not retrievable",
installed_app_id
);
};
// Check if the installed ApplicationId matches the context's ApplicationId
if installed_app_id != context.application_id {
warn!(
installed_app_id = %installed_app_id,
context_app_id = %context.application_id,
"Installed application ID does not match context application ID, updating to installed ID"
);
// Update context with the installed application ID for consistency
context.application_id = installed_app_id;
// Persist the ApplicationId change to the database
// This is critical: if we don't persist, the old ApplicationId will be
// used on node restart, causing application lookup failures
self.context_client
.update_context_application_id(context_id, installed_app_id)
.map_err(|e| {
eyre::eyre!(
"Failed to persist ApplicationId update for context {}: {}",
context_id,
e
)
})?;
debug!(
%context_id,
installed_app_id = %installed_app_id,
"Persisted ApplicationId update to database"
);
}
// Use the verified installed application
*application = Some(installed_application);
Ok(())
}
/// Handle DAG synchronization for uninitialized nodes or nodes with incomplete DAGs
async fn handle_dag_sync(
&self,
context_id: ContextId,
context: &calimero_primitives::context::Context,
chosen_peer: PeerId,
our_identity: PublicKey,
stream: &mut Stream,
) -> eyre::Result<Option<SyncProtocol>> {
let is_uninitialized = *context.root_hash == [0; 32];
// Check for incomplete sync from a previous run (crash recovery)
let has_incomplete_sync = self.check_sync_in_progress(context_id)?.is_some();
if has_incomplete_sync {
warn!(
%context_id,
"Detected incomplete snapshot sync from previous run, forcing re-sync"
);
}
if is_uninitialized || has_incomplete_sync {
info!(
%context_id,
%chosen_peer,
is_uninitialized,
has_incomplete_sync,
"Node needs snapshot sync, checking if peer has state"
);
// Query peer's state to decide sync strategy
let peer_state = self
.query_peer_dag_state(context_id, chosen_peer, our_identity, stream)
.await?;
match peer_state {
Some((peer_root_hash, _peer_dag_heads)) if *peer_root_hash != [0; 32] => {
// Peer has state - use snapshot sync for efficient bootstrap
info!(
%context_id,
%chosen_peer,
peer_root_hash = %peer_root_hash,
"Peer has state, using snapshot sync for bootstrap"
);
// Note: request_snapshot_sync opens its own stream, existing stream
// will be closed when this function returns
// force=false: This is bootstrap for uninitialized nodes
match self
.request_snapshot_sync(context_id, chosen_peer, false)
.await
.wrap_err("snapshot sync")
{
Ok(result) => {
info!(
%context_id,
%chosen_peer,
applied_records = result.applied_records,
boundary_root_hash = %result.boundary_root_hash,
dag_heads_count = result.dag_heads.len(),
"Snapshot sync completed successfully"
);
// CRITICAL: Add snapshot boundary checkpoints to DAG
// This ensures that when new deltas arrive referencing the
// snapshot boundary heads as parents, the DAG accepts them.
if !result.dag_heads.is_empty() {
let delta_store = self
.node_state
.delta_stores
.entry(context_id)
.or_insert_with(|| {
crate::delta_store::DeltaStore::new(
[0u8; 32],
self.context_client.clone(),
context_id,
our_identity,
)
})
.clone();
let checkpoints_added = delta_store
.add_snapshot_checkpoints(
result.dag_heads.clone(),
*result.boundary_root_hash,
)
.await;
info!(
%context_id,
checkpoints_added,
"Added snapshot boundary checkpoints to DAG"
);
match self.network_client.open_stream(chosen_peer).await {
Ok(mut fine_stream) => {
if let Err(e) = self
.fine_sync_from_boundary(
context_id,
chosen_peer,
our_identity,
&mut fine_stream,
)
.await
{
warn!(
%context_id,
%chosen_peer,
error = %e,
"Fine-sync after snapshot failed, state may be slightly behind"
);
}
}
Err(e) => {
warn!(
%context_id,
%chosen_peer,
error = %e,
"Fine-sync stream open failed, state may be slightly behind"
);
}
}
}
// Replay any buffered deltas (from uninitialized context period)
// This ensures handlers execute for deltas that arrived before sync completed
if let Some(buffered_deltas) =
self.node_state.end_sync_session(&context_id)
{
let buffered_count = buffered_deltas.len();
if buffered_count > 0 {
info!(
%context_id,
buffered_count,
"Replaying buffered deltas after snapshot sync (bootstrap path)"
);
self.replay_buffered_deltas(
context_id,
our_identity,
buffered_deltas,
chosen_peer,
)
.await;
}
}
return Ok(Some(SyncProtocol::Snapshot {
compressed: false,
verified: true,
}));
}
Err(e) => {
warn!(
%context_id,
%chosen_peer,
error = %e,
"Snapshot sync failed, will retry with another peer"
);
bail!("Snapshot sync failed: {}", e);
}
}
}
Some(_) => {
// Peer is also uninitialized, try next peer
info!(%context_id, %chosen_peer, "Peer also has no state, trying next peer");
bail!("Peer has no data for this context");
}
None => {
// Failed to query peer state
bail!("Failed to query peer state for context {}", context_id);
}
}
}
// Check if we have pending deltas (incomplete DAG)
// Even if node has some state, it might be missing parent deltas
if let Some(delta_store) = self.node_state.delta_stores.get(&context_id) {
// NOTE: previously called `load_persisted_deltas()` here to
// catch locally-created deltas from execute.rs that are in
// the DB but not in the in-memory DAG. That rescan was
// ~21% of CPU (pre #2244) and ~6% after. execute.rs and
// create_context.rs now notify the node-side drainer via
// `NodeClient::notify_local_applied_delta`, keeping the
// DAG current without the per-sync full-column scan.
let missing_result = delta_store.get_missing_parents().await;
// Note: Cascaded events from DB loads are handled in state_delta handler
if !missing_result.cascaded_events.is_empty() {
info!(
%context_id,
cascaded_count = missing_result.cascaded_events.len(),
"Cascaded deltas from DB load (handlers executed in state_delta path)"
);
}
if !missing_result.missing_ids.is_empty() {
warn!(
%context_id,
%chosen_peer,
missing_count = missing_result.missing_ids.len(),
"Node has incomplete DAG (pending deltas), requesting DAG heads to catch up"
);
// Request DAG heads just like uninitialized nodes
let result = self
.request_dag_heads_and_sync(context_id, chosen_peer, our_identity, stream)
.await
.wrap_err("request DAG heads and sync")?;
// If peer had no data, return error to try next peer
if matches!(result, SyncProtocol::None) {
bail!("Peer has no data for this context");
}
return Ok(Some(result));
}
}
// Compare our state with peer's state even if we think we're in sync.
// The peer might have new heads we don't know about (e.g., if gossipsub messages were lost).
let peer_state = self
.query_peer_dag_state(context_id, chosen_peer, our_identity, stream)
.await?;
if let Some((peer_root_hash, peer_dag_heads)) = peer_state {
// Build handshakes for protocol selection (CIP §2.3)
// Uses shared functions from calimero_node_primitives::sync::state_machine
let local_hs = self.build_local_handshake(context);
let remote_hs = Self::build_remote_handshake(peer_root_hash, &peer_dag_heads);
// Select optimal sync protocol based on state comparison
let selection = select_protocol(&local_hs, &remote_hs);
info!(
%context_id,
%chosen_peer,
protocol = ?selection.protocol,
reason = %selection.reason,
local_root = %context.root_hash,
remote_root = %peer_root_hash,
local_entities = local_hs.entity_count,
remote_entities = remote_hs.entity_count,
"Protocol selected"
);
// Dispatch based on selected protocol
match selection.protocol {
SyncProtocol::None => {
debug!(
%context_id,
%chosen_peer,
root_hash = %context.root_hash,
reason = %selection.reason,
"No sync needed: {}",
selection.reason
);
return Ok(None);
}
SyncProtocol::Snapshot { compressed, .. } => {
// Snapshot sync - use existing handler
info!(
%context_id,
%chosen_peer,
compressed,
reason = %selection.reason,
"Initiating snapshot sync"
);
let result = self
.fallback_to_snapshot_sync(context_id, our_identity, chosen_peer)
.await
.wrap_err("snapshot sync")?;
return Ok(Some(result));
}
SyncProtocol::DeltaSync { .. } => {
// Delta sync - use existing DAG heads request mechanism
info!(
%context_id,
%chosen_peer,
reason = %selection.reason,
"Initiating delta sync via DAG heads request"
);
let result = self
.request_dag_heads_and_sync(context_id, chosen_peer, our_identity, stream)
.await
.wrap_err("delta sync")?;
if matches!(result, SyncProtocol::None) {
bail!("Peer has no data for this context");
}
return Ok(Some(result));
}
SyncProtocol::HashComparison { root_hash, .. } => {
// Execute HashComparison sync (CIP §4)
info!(
%context_id,
reason = %selection.reason,
"Starting HashComparison sync"
);
// Wrap stream in transport abstraction
let mut transport = super::stream::StreamTransport::new(stream);
// Get store for protocol execution
let store = self.context_client.datastore_handle().into_inner();
let config = HashComparisonConfig {
remote_root_hash: root_hash,
};
match HashComparisonProtocol::run_initiator(
&mut transport,
&store,
context_id,
our_identity,
config,
)
.await
{
Ok(stats) => {
info!(
%context_id,
nodes_compared = stats.nodes_compared,
entities_merged = stats.entities_merged,
nodes_skipped = stats.nodes_skipped,
"HashComparison sync completed successfully"
);
return Ok(Some(SyncProtocol::HashComparison {
root_hash,
divergent_subtrees: vec![],
}));
}
Err(e) => {
warn!(
%context_id,
error = %e,
"HashComparison sync failed, falling back to DAG catchup"
);
// Fall back to DAG heads request
let result = self
.request_dag_heads_and_sync(
context_id,
chosen_peer,
our_identity,
stream,
)
.await
.wrap_err("hash comparison fallback")?;
if matches!(result, SyncProtocol::None) {
// If DAG catchup doesn't work, try snapshot as last resort
info!(
%context_id,
"DAG catchup failed, falling back to snapshot sync"
);
let result = self
.fallback_to_snapshot_sync(
context_id,
our_identity,
chosen_peer,
)
.await
.wrap_err("snapshot fallback")?;
return Ok(Some(result));
}
return Ok(Some(result));
}
}
}
SyncProtocol::BloomFilter { .. } => {
warn!(
%context_id,
reason = %selection.reason,
"BloomFilter not yet implemented, falling back to snapshot"
);
let result = self
.fallback_to_snapshot_sync(context_id, our_identity, chosen_peer)
.await
.wrap_err("bloom filter fallback")?;
return Ok(Some(result));
}
SyncProtocol::SubtreePrefetch { .. } => {
warn!(
%context_id,
reason = %selection.reason,
"SubtreePrefetch not yet implemented, falling back to snapshot"
);
let result = self
.fallback_to_snapshot_sync(context_id, our_identity, chosen_peer)
.await
.wrap_err("subtree prefetch fallback")?;
return Ok(Some(result));
}
SyncProtocol::LevelWise { max_depth } => {
// Execute LevelWise sync (CIP Appendix B)
info!(
%context_id,
max_depth,
reason = %selection.reason,
"Starting LevelWise sync"
);
// Wrap stream in transport abstraction
let mut transport = super::stream::StreamTransport::new(stream);
// Get store for protocol execution
let store = self.context_client.datastore_handle().into_inner();
let config = LevelWiseConfig {
remote_root_hash: *peer_root_hash,
max_depth,
};
match LevelWiseProtocol::run_initiator(
&mut transport,
&store,
context_id,
our_identity,
config,
)
.await
{
Ok(stats) => {
info!(
%context_id,
levels_synced = stats.levels_synced,
nodes_compared = stats.nodes_compared,
entities_merged = stats.entities_merged,
nodes_skipped = stats.nodes_skipped,
"LevelWise sync completed successfully"
);
return Ok(Some(SyncProtocol::LevelWise { max_depth }));
}
Err(e) => {
warn!(
%context_id,
error = %e,
"LevelWise sync failed, falling back to DAG catchup"
);
// Fall back to DAG heads request - open a new stream since the
// LevelWise protocol may have left the peer's responder in a state
// where it expects LevelWiseRequest messages, not DagHeadsRequest.
let mut fallback_stream = self
.network_client
.open_stream(chosen_peer)
.await
.wrap_err("open stream for level-wise fallback")?;
let result = self
.request_dag_heads_and_sync(
context_id,
chosen_peer,
our_identity,
&mut fallback_stream,
)
.await
.wrap_err("level-wise fallback")?;
if matches!(result, SyncProtocol::None) {
// If DAG catchup doesn't work, try snapshot as last resort
info!(
%context_id,
"DAG catchup insufficient, attempting snapshot"
);
// Drop the consumed fallback_stream before opening fresh streams
// in snapshot sync (fallback_stream is in indeterminate state
// after DAG sync exchanges)
drop(fallback_stream);
let snapshot_result = self
.fallback_to_snapshot_sync(
context_id,
our_identity,
chosen_peer,
)
.await
.wrap_err("level-wise snapshot fallback")?;
return Ok(Some(snapshot_result));
}
return Ok(Some(result));
}
}
}
}
}
Ok(None)
}
/// Query peer for their DAG state (root_hash and dag_heads) without triggering full sync.
///
/// Returns `Ok(Some((root_hash, dag_heads)))` if peer responded successfully,
/// `Ok(None)` if peer had no valid response or no state, or `Err` on communication error.
async fn query_peer_dag_state(
&self,
context_id: ContextId,
chosen_peer: PeerId,
our_identity: PublicKey,
stream: &mut Stream,
) -> eyre::Result<Option<(calimero_primitives::hash::Hash, Vec<[u8; DIGEST_SIZE]>)>> {
let request_msg = StreamMessage::Init {
context_id,
party_id: our_identity,
payload: InitPayload::DagHeadsRequest { context_id },
next_nonce: rand::thread_rng().gen(),
};
self.send(stream, &request_msg, None).await?;
let response = self.recv(stream, None).await?;
match response {
Some(StreamMessage::Message {
payload:
MessagePayload::DagHeadsResponse {
dag_heads,
root_hash,
},
..
}) => {
debug!(
%context_id,
%chosen_peer,
heads_count = dag_heads.len(),
peer_root_hash = %root_hash,
"Received peer DAG state for comparison"
);
Ok(Some((root_hash, dag_heads)))
}
_ => {
debug!(%context_id, %chosen_peer, "Failed to get peer DAG state for comparison");
Ok(None)
}
}
}
async fn initiate_sync_inner(
&self,
context_id: ContextId,
chosen_peer: PeerId,
) -> eyre::Result<SyncProtocol> {
let sync_start = Instant::now();
let mut context = self
.context_client
.sync_context_config(context_id, None)
.await?;
let is_uninitialized = *context.root_hash == [0; 32];
info!(
%context_id,
%chosen_peer,
is_uninitialized,
root_hash = %context.root_hash,
dag_heads_count = context.dag_heads.len(),
application_id = %context.application_id,
"Starting sync session"
);
// Get application - if not found, we'll try to install it after blob sharing
let mut application = self.node_client.get_application(&context.application_id)?;
// Get blob_id and app config for later use
let (blob_id, app_config_opt) = self.get_blob_info(&context_id, &application).await?;
let identities = self
.context_client
.get_context_members(&context.id, Some(true));
let Some((our_identity, _)) = choose_stream(identities, &mut rand::thread_rng())
.await
.transpose()?
else {
bail!("no owned identities found for context: {}", context.id);
};
let mut stream = self
.network_client
.open_stream(chosen_peer)
.await
.wrap_err("open stream for sync")?;
// Key share phase removed — group key envelopes handle key distribution.
let key_share_elapsed = std::time::Duration::ZERO;
debug!(
%context_id,
%chosen_peer,
?key_share_elapsed,
"Phase 1/3 complete: key share"
);
// Phase 2: Blob share (if needed)
if !self.node_client.has_blob(&blob_id)? {
let phase_start = Instant::now();
// Get size from application config if we don't have application yet
let size = self
.get_application_size(&context_id, &application, &app_config_opt)
.await?;
self.initiate_blob_share_process(&context, our_identity, blob_id, size, &mut stream)
.await
.wrap_err("blob share")?;
let blob_share_elapsed = phase_start.elapsed();
debug!(
%context_id,
%chosen_peer,
?blob_share_elapsed,
"Phase 2/3 complete: blob share"
);
// After blob sharing, try to install application if it doesn't exist
// or if we only have a stub (size==0 from join_context bootstrap)
let needs_install =
application.is_none() || application.as_ref().is_some_and(|app| app.size == 0);
if needs_install {
self.install_bundle_after_blob_sharing(
&context_id,
&blob_id,
&app_config_opt,
&mut context,
&mut application,
)
.await
.wrap_err("install bundle after blob share")?;
}
}
let Some(_application) = application else {
if context.application_id
== calimero_primitives::application::ApplicationId::from([0u8; 32])
{
bail!("context has placeholder application ID — waiting for governance op to resolve it");
}
bail!("application not found: {}", context.application_id);
};
// Phase 3: DAG synchronization (if needed — uninitialized or incomplete DAG)
let phase_start = Instant::now();
if let Some(result) = self
.handle_dag_sync(context_id, &context, chosen_peer, our_identity, &mut stream)
.await
.wrap_err("DAG sync")?
{
let dag_sync_elapsed = phase_start.elapsed();
let total_elapsed = sync_start.elapsed();
info!(
%context_id,
%chosen_peer,
?key_share_elapsed,
?dag_sync_elapsed,
?total_elapsed,
protocol = ?result,
"Sync session complete (DAG sync performed)"
);
return Ok(result);
}
let total_elapsed = sync_start.elapsed();
// Otherwise, DAG-based sync happens automatically via BroadcastMessage::StateDelta
debug!(
%context_id,
%chosen_peer,
?key_share_elapsed,
?total_elapsed,
"Sync session complete: node is in sync, no active protocol needed"
);
Ok(SyncProtocol::None)
}
/// Request peer's DAG heads and sync all missing deltas
async fn request_dag_heads_and_sync(
&self,
context_id: ContextId,
peer_id: PeerId,
our_identity: PublicKey,
stream: &mut Stream,
) -> eyre::Result<SyncProtocol> {
use calimero_node_primitives::sync::{InitPayload, MessagePayload, StreamMessage};
// Send DAG heads request
let request_msg = StreamMessage::Init {
context_id,
party_id: our_identity,
payload: InitPayload::DagHeadsRequest { context_id },
next_nonce: {
use rand::Rng;
rand::thread_rng().gen()
},
};
self.send(stream, &request_msg, None).await?;
// Receive response
let response = self.recv(stream, None).await?;
match response {
Some(StreamMessage::Message {
payload:
MessagePayload::DagHeadsResponse {
dag_heads,
root_hash,
},
..
}) => {
info!(
%context_id,
heads_count = dag_heads.len(),
peer_root_hash = %root_hash,
"Received DAG heads from peer, requesting deltas"
);
// Check if peer has state even without DAG heads
if dag_heads.is_empty() && *root_hash != [0; 32] {
error!(
%context_id,
peer_root_hash = %root_hash,
"Peer has state but no DAG heads!"
);
bail!(
"Peer has state but no DAG heads (migration issue). \
Clear data directories on both nodes and recreate context."
);
}
if dag_heads.is_empty() {
info!(%context_id, "Peer also has no deltas and no state, will try next peer");
// Return None to signal caller to try next peer
return Ok(SyncProtocol::None);
}
// CRITICAL FIX: Fetch ALL DAG heads first, THEN request missing parents
// This ensures we don't miss sibling heads that might be the missing parents
// Get or create DeltaStore for this context (do this once before the loop)
let (delta_store_ref, is_new) = {
let mut is_new = false;
let delta_store = self
.node_state
.delta_stores
.entry(context_id)
.or_insert_with(|| {
is_new = true;
crate::delta_store::DeltaStore::new(
[0u8; 32],
self.context_client.clone(),
context_id,
our_identity,
)
});
(delta_store.clone(), is_new)
};
// The previous revision ran `load_persisted_deltas`
// unconditionally here on every sync — the rescan
// dominated the hot path. execute.rs now notifies the
// node-side drainer directly, so warm stores don't
// need rehydration. But when *this* path is the first
// to create the DeltaStore for a context (fresh boot,
// sync arrives before the first local execute), the
// in-memory DAG is empty and we still need a one-time
// load so `get_delta` can serve peers and missing-
// parent queries have the right picture.
if is_new {
if let Err(e) = delta_store_ref.load_persisted_deltas().await {
warn!(
?e,
%context_id,
"Failed to hydrate freshly-created DeltaStore from DB"
);
}
}
// Phase 1: Request and add ALL DAG heads
for head_id in &dag_heads {
info!(
%context_id,
head_id = ?head_id,
"Requesting DAG head delta from peer"
);
let delta_request = StreamMessage::Init {
context_id,
party_id: our_identity,
payload: InitPayload::DeltaRequest {
context_id,
delta_id: *head_id,
},
next_nonce: {
use rand::Rng;
rand::thread_rng().gen()
},
};
self.send(stream, &delta_request, None).await?;
let delta_response = self.recv(stream, None).await?;
match delta_response {
Some(StreamMessage::Message {
payload: MessagePayload::DeltaResponse { delta },
..
}) => {
// Deserialize and add to DAG
let storage_delta: calimero_storage::delta::CausalDelta =
borsh::from_slice(&delta)?;
let dag_delta = calimero_dag::CausalDelta {
id: storage_delta.id,
parents: storage_delta.parents,
payload: storage_delta.actions,
hlc: storage_delta.hlc,
expected_root_hash: storage_delta.expected_root_hash,
kind: calimero_dag::DeltaKind::Regular,
};
if let Err(e) = delta_store_ref.add_delta(dag_delta).await {
warn!(
?e,
%context_id,
head_id = ?head_id,
"Failed to add DAG head delta"
);
} else {
info!(
%context_id,
head_id = ?head_id,
"Successfully added DAG head delta"
);
}
}
Some(StreamMessage::Message {
payload:
MessagePayload::SnapshotError {
error:
calimero_node_primitives::sync::SnapshotError::SnapshotRequired,
},
..
}) => {
info!(
%context_id,
head_id = ?head_id,
"Peer's delta history is pruned, falling back to snapshot sync"
);
// Fall back to snapshot sync
return self
.fallback_to_snapshot_sync(context_id, our_identity, peer_id)
.await;
}
Some(StreamMessage::Message {
payload: MessagePayload::DeltaNotFound,
..
}) => {
warn!(
%context_id,
head_id = ?head_id,
"Peer doesn't have requested DAG head delta"
);
// Continue trying other heads
}
_ => {
warn!(%context_id, head_id = ?head_id, "Unexpected response to delta request");
}
}
}
// Phase 2: Now check for missing parents and fetch them recursively
let missing_result = delta_store_ref.get_missing_parents().await;
// Note: Cascaded events from DB loads logged but not executed here (state_delta handler will catch them)
if !missing_result.cascaded_events.is_empty() {
info!(
%context_id,
cascaded_count = missing_result.cascaded_events.len(),
"Cascaded deltas from DB load during DAG head sync"
);
}
// Steady-state: the initial DAG-heads response matched local
// state, so there are no missing parents to chase. Skip the
// entire retry-and-final-check machinery on the common path.
if missing_result.missing_ids.is_empty() {
return Ok(SyncProtocol::DeltaSync {
missing_delta_ids: vec![],
});
}
info!(
%context_id,
missing_count = missing_result.missing_ids.len(),
"DAG heads have missing parents, requesting them recursively"
);
// First attempt: the peer that served DAG heads.
if let Err(e) = self
.request_missing_deltas(
context_id,
missing_result.missing_ids,
peer_id,
delta_store_ref.clone(),
our_identity,
)
.await
{
warn!(
?e,
%context_id,
"Failed to request missing parent deltas from initial peer"
);
}
// Cross-peer fallback for cold-start race (#2198): if the
// initial peer did not resolve every missing parent, iterate
// other mesh peers for this context until the DAG is whole
// or the retry budget is exhausted.
let topic = TopicHash::from_raw(context_id);
let mut budget = super::parent_pull::ParentPullBudget::new(
peer_id,
self.sync_config.parent_pull_additional_peers,
self.sync_config.parent_pull_budget,
);
let mut mesh_peers = self.network_client.mesh_peers(topic.clone()).await;
loop {
let after = delta_store_ref.get_missing_parents().await;
if after.missing_ids.is_empty() {
break; // fully resolved
}
let next_peer = match budget.next(&mesh_peers) {
super::parent_pull::NextPeer::Peer(p) => p,
super::parent_pull::NextPeer::RefetchMesh => {
mesh_peers = self.network_client.mesh_peers(topic.clone()).await;
budget.record_refetch();
match budget.next(&mesh_peers) {
super::parent_pull::NextPeer::Peer(p) => p,
other => {
debug!(
%context_id,
?other,
"no additional mesh peers available for parent pull"
);
break;
}
}
}
super::parent_pull::NextPeer::BudgetExhausted => {
warn!(
%context_id,
"parent-pull budget exhausted"
);
break;
}
super::parent_pull::NextPeer::MaxPeersReached
| super::parent_pull::NextPeer::NoMorePeers => break,
};
budget.record_attempt(next_peer);
info!(
%context_id,
?next_peer,
attempt = budget.attempts(),
still_missing = after.missing_ids.len(),
"retrying missing-parent fetch against additional mesh peer"
);
if let Err(e) = self
.request_missing_deltas(
context_id,
after.missing_ids,
next_peer,
delta_store_ref.clone(),
our_identity,
)
.await
{
warn!(
?e,
%context_id,
?next_peer,
"cross-peer parent-pull attempt failed"
);
}
}
// Final check: if pending parents still remain, the sync did
// NOT fully restore the DAG. Return an error so the caller
// (e.g. join_context) surfaces a real failure instead of
// silent success on a partially-applied DAG.
let final_missing = delta_store_ref.get_missing_parents().await;
if !final_missing.missing_ids.is_empty() {
warn!(
%context_id,
remaining = final_missing.missing_ids.len(),
peer_attempts = budget.total_attempts(),
"DAG sync ended with unresolved missing parents"
);
bail!(
"pending parents unresolved for context {}: {} remaining after {} peer attempt(s)",
context_id,
final_missing.missing_ids.len(),
budget.total_attempts(),
);
}
// Success: DAG is fully resolved.
Ok(SyncProtocol::DeltaSync {
missing_delta_ids: vec![],
})
}
_ => {
warn!(%context_id, "Unexpected response to DAG heads request, trying next peer");
Ok(SyncProtocol::None)
}
}
}
/// Fall back to full snapshot sync when delta sync is not possible.
///
/// Implements Invariant I6: Deltas received during sync are buffered and
/// replayed after sync completes. On error, buffered deltas are discarded
/// via `cancel_sync_session()`.
async fn fallback_to_snapshot_sync(
&self,
context_id: ContextId,
our_identity: PublicKey,
peer_id: PeerId,
) -> eyre::Result<SyncProtocol> {
info!(%context_id, %peer_id, "Initiating snapshot sync");
// Start buffering deltas that arrive during snapshot sync (Invariant I6)
// Use current time as sync start HLC
let sync_start_hlc = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos() as u64)
.unwrap_or(0);
self.node_state
.start_sync_session(context_id, sync_start_hlc);
// force=false: Enforce Invariant I5 - only allow snapshot on fresh nodes.
// If the node has state, this will fail, which is correct - divergence
// or pruned history on initialized nodes cannot be safely resolved via
// snapshot overwrite. CRDT merge must be used instead.
let result = match self.request_snapshot_sync(context_id, peer_id, false).await {
Ok(r) => r,
Err(e) => {
// Cancel sync session on failure - discard buffered deltas
// since the context state is inconsistent
self.node_state.cancel_sync_session(&context_id);
return Err(e);
}
};
info!(%context_id, records = result.applied_records, "Snapshot sync completed");
// End buffering and get any deltas that arrived during sync
let buffered_deltas = self.node_state.end_sync_session(&context_id);
let buffered_count = buffered_deltas.as_ref().map_or(0, Vec::len);
if buffered_count > 0 {
info!(
%context_id,
buffered_count,
"Replaying buffered deltas after snapshot sync"
);
// Replay buffered deltas - now that context is initialized, we can process them
if let Some(deltas) = buffered_deltas {
self.replay_buffered_deltas(context_id, our_identity, deltas, peer_id)
.await;
}
}
// Fine-sync to catch any deltas since the snapshot boundary
if !result.dag_heads.is_empty() {
let mut stream = self.network_client.open_stream(peer_id).await?;
if let Err(e) = self
.fine_sync_from_boundary(context_id, peer_id, our_identity, &mut stream)
.await
{
warn!(?e, %context_id, "Fine-sync failed, state may be slightly behind");
}
}
Ok(SyncProtocol::Snapshot {
compressed: false,
verified: true,
})
}
/// Replay buffered deltas after snapshot sync completes.
///
/// This ensures that:
/// 1. Deltas arriving during sync aren't lost
/// 2. Event handlers execute for buffered deltas
/// 3. Ancestor deltas (whose state is covered by checkpoint) get handlers executed
async fn replay_buffered_deltas(
&self,
context_id: ContextId,
our_identity: PublicKey,
mut deltas: Vec<calimero_node_primitives::delta_buffer::BufferedDelta>,
_fallback_peer: PeerId,
) {
use crate::handlers::state_delta::{replay_buffered_delta, ReplayBufferedDeltaInput};
use std::collections::{HashMap, HashSet};
// #2319 determinism: deltas land in the buffer in gossipsub
// arrival order, which differs node-to-node — replaying them in
// that order makes two nodes apply *concurrent* deltas to storage
// in different sequences, which (for any merge that isn't
// perfectly order-independent) yields a different Merkle root for
// the same delta set. Replay in a canonical, causally-consistent
// order — HLC, then delta id as a tiebreaker — so every node
// applies the same sequence. (The DAG cascade still re-orders for
// genuine causal dependencies; this only pins the order of
// concurrent ones.)
deltas.sort_by(|a, b| a.hlc.cmp(&b.hlc).then_with(|| a.id.cmp(&b.id)));
// Build a set of IDs that are "covered" by the snapshot
// This includes:
// 1. Deltas that match checkpoints directly
// 2. Deltas that are ancestors of checkpoints (their state is included in snapshot)
let mut covered_delta_ids: HashSet<[u8; 32]> = HashSet::new();
// Get the delta store to check for existing checkpoints.
// If this path is the first to create the DeltaStore, hydrate
// from DB once — incremental updates via execute.rs handle the
// warm-store case, but a fresh store here would otherwise miss
// everything on disk and we'd later fail to match checkpoints.
let (delta_store, is_new) = {
let mut is_new = false;
let entry = self
.node_state
.delta_stores
.entry(context_id)
.or_insert_with(|| {
is_new = true;
crate::delta_store::DeltaStore::new(
[0u8; 32],
self.context_client.clone(),
context_id,
our_identity,
)
});
(entry.clone(), is_new)
};
if is_new {
if let Err(e) = delta_store.load_persisted_deltas().await {
warn!(
?e,
%context_id,
"Failed to hydrate freshly-created DeltaStore from DB"
);
}
}
// Build parent -> children map from buffered deltas
let mut parent_to_children: HashMap<[u8; 32], Vec<[u8; 32]>> = HashMap::new();
for buffered in &deltas {
for parent in &buffered.parents {
parent_to_children
.entry(*parent)
.or_default()
.push(buffered.id);
}
}
// Identify which buffered deltas match existing checkpoints
let mut checkpoint_matches: Vec<[u8; 32]> = Vec::new();
for buffered in &deltas {
if delta_store.dag_has_delta_applied(&buffered.id).await {
checkpoint_matches.push(buffered.id);
covered_delta_ids.insert(buffered.id);
}
}
// Propagate "covered" status backwards through the parent chain
// If delta D has a child C that is covered, then D is also covered
// (D's state is included in C's checkpoint)
let delta_ids: HashSet<[u8; 32]> = deltas.iter().map(|d| d.id).collect();
let delta_parents: HashMap<[u8; 32], Vec<[u8; 32]>> =
deltas.iter().map(|d| (d.id, d.parents.clone())).collect();
// BFS backwards from checkpoint matches
let mut queue: std::collections::VecDeque<[u8; 32]> =
checkpoint_matches.iter().copied().collect();
while let Some(child_id) = queue.pop_front() {
// Get parents of this delta (if it's one of our buffered deltas)
if let Some(parents) = delta_parents.get(&child_id) {
for parent_id in parents {
// If parent is also a buffered delta and not yet covered
if delta_ids.contains(parent_id) && !covered_delta_ids.contains(parent_id) {
covered_delta_ids.insert(*parent_id);
queue.push_back(*parent_id);
}
}
}
}
if !covered_delta_ids.is_empty() {
info!(
%context_id,
covered_count = covered_delta_ids.len(),
checkpoint_matches = checkpoint_matches.len(),
total_buffered = deltas.len(),
"Identified buffered deltas covered by snapshot checkpoint"
);
}
for buffered in deltas {
let delta_id = buffered.id;
let has_events = buffered.events.is_some();
let is_covered_by_checkpoint = covered_delta_ids.contains(&delta_id);
match replay_buffered_delta(ReplayBufferedDeltaInput {
context_client: self.context_client.clone(),
node_client: self.node_client.clone(),
node_state: self.node_state.clone(),
context_id,
our_identity,
buffered,
sync_timeout: self.sync_config.timeout,
is_covered_by_checkpoint,
})
.await
{
Ok(applied) => {
if applied {
info!(
%context_id,
delta_id = ?delta_id,
has_events,
"Replayed buffered delta successfully"
);
} else if is_covered_by_checkpoint {
debug!(
%context_id,
delta_id = ?delta_id,
"Buffered delta is ancestor of checkpoint (state covered, handlers executed)"
);
} else {
debug!(
%context_id,
delta_id = ?delta_id,
"Buffered delta went to pending (missing parents)"
);
}
}
Err(e) => {
warn!(
%context_id,
delta_id = ?delta_id,
error = %e,
"Failed to replay buffered delta"
);
}
}
}
}
/// Fine-sync from snapshot boundary to catch up to latest state.
async fn fine_sync_from_boundary(
&self,
context_id: ContextId,
peer_id: PeerId,
our_identity: PublicKey,
stream: &mut Stream,
) -> eyre::Result<()> {
// Fresh DeltaStore created here must be hydrated once from DB;
// warm stores are kept current by execute-side incremental
// notifications.
let (delta_store, is_new) = {
let mut is_new = false;
let entry = self
.node_state
.delta_stores
.entry(context_id)
.or_insert_with(|| {
is_new = true;
crate::delta_store::DeltaStore::new(
[0u8; 32],
self.context_client.clone(),
context_id,
our_identity,
)
});
(entry.clone(), is_new)
};
if is_new {
if let Err(e) = delta_store.load_persisted_deltas().await {
warn!(
?e,
%context_id,
"Failed to hydrate freshly-created DeltaStore from DB"
);
}
}
let request_msg = StreamMessage::Init {
context_id,
party_id: our_identity,
payload: InitPayload::DagHeadsRequest { context_id },
next_nonce: rand::random(),
};
self.send(stream, &request_msg, None).await?;
let response = self.recv(stream, None).await?;
if let Some(StreamMessage::Message {
payload: MessagePayload::DagHeadsResponse { dag_heads, .. },
..
}) = response
{
let mut missing = Vec::new();
for head in &dag_heads {
if !delta_store.has_delta(head).await {
missing.push(*head);
}
}
if !missing.is_empty() {
self.request_missing_deltas(
context_id,
missing,
peer_id,
delta_store,
our_identity,
)
.await?;
}
}
Ok(())
}
pub async fn handle_opened_stream(&self, peer_id: PeerId, mut stream: Box<Stream>) {
loop {
match self
.internal_handle_opened_stream(peer_id, &mut stream)
.await
{
Ok(None) => break,
Ok(Some(())) => {}
Err(err) => {
error!(%err, "Failed to handle stream message");
if let Err(err) = self
.send(&mut stream, &StreamMessage::OpaqueError, None)
.await
{
error!(%err, "Failed to send error message");
}
}
}
}
}
async fn internal_handle_opened_stream(
&self,
peer_id: PeerId,
stream: &mut Stream,
) -> eyre::Result<Option<()>> {
let Some(message) = self.recv(stream, None).await? else {
return Ok(None);
};
let (context_id, their_identity, payload, nonce) = match message {
StreamMessage::Init {
context_id,
party_id,
payload,
next_nonce,
..
} => (context_id, party_id, payload, next_nonce),
unexpected @ (StreamMessage::Message { .. } | StreamMessage::OpaqueError) => {
bail!("expected initialization handshake, got {:?}", unexpected)
}
};
if let InitPayload::NamespaceBackfillRequest {
namespace_id,
delta_ids,
} = &payload
{
self.handle_namespace_backfill_request(*namespace_id, delta_ids, stream, nonce)
.await?;
return Ok(Some(()));
}
if let InitPayload::NamespaceJoinRequest {
namespace_id,
ref invitation_bytes,
joiner_public_key,
} = &payload
{
self.handle_namespace_join_request(
*namespace_id,
invitation_bytes,
*joiner_public_key,
stream,
nonce,
)
.await?;
return Ok(Some(()));
}
if let InitPayload::OpenSubgroupJoinRequest {
namespace_id,
subgroup_id,
joiner_public_key,
} = &payload
{
self.handle_open_subgroup_join_request(
*namespace_id,
*subgroup_id,
*joiner_public_key,
stream,
nonce,
)
.await?;
return Ok(Some(()));
}
let context = match self.context_client.get_context(&context_id)? {
Some(ctx) => ctx,
None => {
// Race window: the dialer can trigger context-level sync as
// a cascade of namespace-topic subscription
// (`subscriptions.rs::handle_subscribed` → `sync_group` /
// `broadcast_group_local_state`) before this node's local
// `join_context` materialises the context entry. If the
// dialer is a member of the namespace this context belongs
// to, the inbound stream is legitimate — just early. Brief
// wait for materialisation, then proceed (or close if it
// really is an unknown context). Same race shape as the
// unknown-member catch-up below (#2237).
let store = self.context_client.datastore();
let dialer_is_namespace_member =
match calimero_context::group_store::get_group_for_context(store, &context_id)?
{
Some(group_id) => calimero_context::group_store::check_group_membership(
store,
&group_id,
&their_identity,
)?,
None => false,
};
if !dialer_is_namespace_member {
// Genuinely unknown context (or cross-namespace stream
// leak per #2198). Close cleanly so unrelated sync
// activity is unaffected.
warn!(
%context_id,
?their_identity,
"inbound stream for unknown context, closing cleanly"
);
if let Err(err) = self.send(stream, &StreamMessage::OpaqueError, None).await {
error!(%err, %context_id, "failed to send OpaqueError for unknown context");
}
return Ok(None);
}
// Bounded wait for local `join_context` to materialise the
// context entry. Poll cadence matches `FALLBACK_POLL` in
// `handlers/join_context.rs`. The 5 s budget comfortably
// covers the ~5 s gap observed between namespace-membership
// application and local context materialisation in the
// `bdc61af` smoke-regression artefact; cold-start cases
// beyond that fall through to the same OpaqueError as
// before — the dialer retries on its next sync interval.
const MATERIALIZATION_WINDOW: time::Duration = time::Duration::from_secs(5);
const MATERIALIZATION_POLL: time::Duration = time::Duration::from_millis(200);
let deadline = Instant::now() + MATERIALIZATION_WINDOW;
let mut materialised = None;
while Instant::now() < deadline {
time::sleep(MATERIALIZATION_POLL).await;
if let Some(ctx) = self.context_client.get_context(&context_id)? {
materialised = Some(ctx);
break;
}
}
match materialised {
Some(ctx) => {
debug!(
%context_id,
?their_identity,
"context materialised during join race window, proceeding with inbound sync"
);
ctx
}
None => {
debug!(
%context_id,
?their_identity,
"context not materialised within join race window, closing stream"
);
if let Err(err) = self.send(stream, &StreamMessage::OpaqueError, None).await
{
error!(
%err,
%context_id,
"failed to send OpaqueError for unknown context"
);
}
return Ok(None);
}
}
}
};
let mut _updated = None;
// Issue #2256: also accept inheritance-eligible parent members
// for sync auth. `has_member` only knows direct context-membership
// and direct group-membership; the parent-walk for `Open` subgroups
// lives in `calimero-context::group_store`, which we have access
// to here at the node layer.
let is_inherited_member = || -> eyre::Result<bool> {
let store = self.context_client.datastore();
let Some(group_id) =
calimero_context::group_store::get_group_for_context(store, &context_id)?
else {
return Ok(false);
};
calimero_context::group_store::check_group_membership(store, &group_id, &their_identity)
};
if !self
.context_client
.has_member(&context_id, &their_identity)?
&& !is_inherited_member()?
{
_updated = Some(
self.context_client
.sync_context_config(context_id, None)
.await?,
);
if !self
.context_client
.has_member(&context_id, &their_identity)?
&& !is_inherited_member()?
{
// The peer may have just published MemberAdded for themselves
// (or their side of the governance DAG is ahead of ours) and
// gossipsub hasn't delivered it yet. Instead of waiting and
// hoping the gossip arrives, ask this peer directly for the
// current namespace governance state on a separate stream —
// it's the fastest path out of the "unknown member" state and
// avoids a 30 s stall waiting for `NamespaceStateHeartbeat`.
//
// Fire-and-forget governance propagation (issue #2237) is the
// underlying bug; this is a narrower mitigation in the
// responder path that converts the terminal close into an
// active catch-up request.
self.request_governance_catchup_from_peer(peer_id, &context_id, &their_identity)
.await;
if !self
.context_client
.has_member(&context_id, &their_identity)?
&& !is_inherited_member()?
{
// Catch-up didn't resolve it (peer returned nothing, peer
// also doesn't know, or the op chain isn't valid locally).
// Close gracefully — the initiator retries on their next
// sync interval. Demoted from warn to debug because this
// is expected during mesh formation and would otherwise
// spam logs on every cold join.
debug!(
%context_id,
%their_identity,
"unknown context member after namespace backfill request, closing stream"
);
return Ok(Some(()));
}
}
}
// Note: Concurrent syncs are already prevented by SyncState tracking
// in the start() loop. When sync starts, last_sync is set to None.
// When complete, it's set to Some(now).
let identities = self
.context_client
.get_context_members(&context.id, Some(true));
let Some((our_identity, _)) = choose_stream(identities, &mut rand::thread_rng())
.await
.transpose()?
else {
bail!("no owned identities found for context: {}", context.id);
};
match payload {
InitPayload::BlobShare { blob_id } => {
self.handle_blob_share_request(
&context,
our_identity,
their_identity,
blob_id,
stream,
)
.await?
}
// Old sync protocols removed - DAG uses gossipsub broadcast instead
// Streams are only used for: KeyShare, BlobShare, DeltaRequest, DagHeadsRequest
InitPayload::DeltaRequest {
context_id: requested_context_id,
delta_id,
} => {
// Handle delta request from peer
self.handle_delta_request(requested_context_id, delta_id, stream)
.await?
}
InitPayload::DagHeadsRequest {
context_id: requested_context_id,
} => {
// Handle DAG heads request from peer
self.handle_dag_heads_request(requested_context_id, stream, nonce)
.await?
}
InitPayload::SnapshotBoundaryRequest {
context_id: requested_context_id,
requested_cutoff_timestamp,
} => {
// Handle snapshot boundary negotiation request from peer
self.handle_snapshot_boundary_request(
requested_context_id,
requested_cutoff_timestamp,
stream,
nonce,
)
.await?
}
InitPayload::SnapshotStreamRequest {
context_id: requested_context_id,
boundary_root_hash,
page_limit,
byte_limit,
resume_cursor,
} => {
// Handle snapshot stream request from peer
self.handle_snapshot_stream_request(
requested_context_id,
boundary_root_hash,
page_limit,
byte_limit,
resume_cursor,
stream,
nonce,
)
.await?
}
InitPayload::TreeNodeRequest {
context_id: requested_context_id,
node_id,
max_depth,
} => {
// Handle tree node request from peer (HashComparison sync)
// Wrap stream in transport abstraction
let mut transport = super::stream::StreamTransport::new(stream);
self.handle_tree_node_request(
requested_context_id,
node_id,
max_depth,
&mut transport,
nonce,
)
.await?
}
InitPayload::LevelWiseRequest {
context_id: requested_context_id,
level: first_level,
parent_ids: first_parent_ids,
} => {
// Handle LevelWise request from peer (LevelWise sync responder)
// Wrap stream in transport abstraction
let mut transport = super::stream::StreamTransport::new(stream);
// Get store for protocol execution
let store = self.context_client.datastore_handle().into_inner();
// Use the already-resolved our_identity from the top of handle_sync_request
// (avoids redundant lookup and ensures consistency with other handlers)
// Build the first request data (already parsed above for routing)
let first_request = super::level_sync::LevelWiseFirstRequest {
level: first_level,
parent_ids: first_parent_ids,
};
// Run the LevelWise responder via the trait method
use calimero_node_primitives::sync::SyncProtocolExecutor;
super::level_sync::LevelWiseProtocol::run_responder(
&mut transport,
&store,
requested_context_id,
our_identity,
first_request,
)
.await?
}
InitPayload::EntityPush { .. } => {
// EntityPush is handled within the HashComparison responder loop,
// not as a top-level stream init. If received here, it means a
// protocol error — the initiator sent EntityPush outside of a
// HashComparison session. Log and ignore.
warn!("Received EntityPush outside of HashComparison session, ignoring");
}
InitPayload::NamespaceBackfillRequest { .. } => {
unreachable!("handled by early return above")
}
InitPayload::NamespaceJoinRequest { .. } => {
unreachable!("handled by early return above")
}
InitPayload::OpenSubgroupJoinRequest { .. } => {
unreachable!("handled by early return above")
}
};
Ok(Some(()))
}
/// Schedule reconcile-via-anchor for every per-context hash
/// mismatch in `report`. Called by the namespace governance op
/// receive handler after `MemberRemoved` / `MemberLeft` apply
/// reports state-hash divergence from the signed claims.
///
/// One sync attempt per divergent context (`hash_differs`):
/// pick a connected anchor peer (via the trusted-anchor set for
/// the op's group + the verified `peer_identities` cache),
/// initiate Snapshot sync against that peer, and after sync
/// completes compare the receiver's new root hash against the
/// signed expected. Mismatch on post-adoption verify is logged
/// loudly — a follow-up will tighten this into pre-adoption
/// rejection with rollback once the store has transactional
/// staging.
///
/// `only_in_expected` and `only_in_actual` entries are NOT
/// reconciled here — those buckets reflect namespace-DAG
/// drift (a registration the receiver hasn't seen yet, or a
/// registration the signer hadn't seen). The cross-DAG
/// membership check on subsequent state deltas catches that
/// via `Unknown { needed }` → buffer; routing them through
/// anchor sync would burn bandwidth on cases the existing
/// catch-up path handles correctly.
pub async fn reconcile_after_divergence(
&self,
report: calimero_context_client::messages::DivergenceReport,
) {
if report.hash_differs.is_empty() {
// Distinguish "no divergence at all" (debug-level
// bookkeeping) from "group-level divergence with no
// per-context mismatch" (operator-visible: a member row
// is missing or extra somewhere, but every context the
// op touched still hashes the same). The latter is rare
// enough that we want it surfaced, not buried at debug.
// Per-context reconcile doesn't apply — there's no
// signed canonical hash for the group-state alone to
// pull state against — so we log and return. Subsequent
// signed ops carry the corrected group-state hash and
// the namespace-DAG buffer + cross-DAG check on later
// state deltas closes the gap.
if report.group_hash_diverges {
tracing::warn!(
group_id = %hex::encode(report.group_id.to_bytes()),
op_kind = report.op_kind,
only_in_expected_count = report.only_in_expected.len(),
only_in_actual_count = report.only_in_actual.len(),
"reconcile-after-divergence: group-state hash diverges from signed expected, \
but no per-context hash mismatch is reconcilable here — convergence relies \
on the cross-DAG check against subsequent signed ops"
);
} else {
tracing::debug!(
group_id = %hex::encode(report.group_id.to_bytes()),
op_kind = report.op_kind,
only_in_expected_count = report.only_in_expected.len(),
only_in_actual_count = report.only_in_actual.len(),
"reconcile-after-divergence: no per-context hash mismatches to reconcile; \
namespace-DAG drift (if any) is handled by the cross-DAG check on \
subsequent state deltas"
);
}
return;
}
for (context_id, expected_root_hash) in &report.hash_differs {
self.reconcile_one_divergent_context(
report.group_id,
*context_id,
*expected_root_hash,
report.op_kind,
)
.await;
}
}
/// Reconcile a single divergent context against a trusted anchor.
///
/// Returns silently after logging — there is no error to bubble
/// up to the caller because reconcile is best-effort: a future
/// arrival of another signed op, or a sync interval tick, will
/// re-attempt convergence. A hard error here would only inflate
/// noise; the warn logs are the operator signal.
///
/// Backoff: prior failed attempts for the same context impose an
/// exponential cooldown (see [`reconcile_cooldown`]). Within that
/// window, this is a no-op — the next signed op or sync tick will
/// re-trigger once cooldown lapses. A successful post-adoption
/// verify clears the backoff state immediately.
///
/// **Convergence is not guaranteed in one shot**: `initiate_sync`
/// negotiates the protocol via the standard handshake (typically
/// `HashComparison` or `DeltaSync` between two initialized peers).
/// Snapshot overwrite is gated by the `force=false` invariant in
/// `fallback_to_snapshot_sync` and won't run on an initialized
/// divergent node — that is by design, because snapshot adoption
/// after the fact requires transactional staging the store layer
/// doesn't yet provide. CRDT merge will sometimes converge two
/// divergent states to the signed expected hash and sometimes
/// won't (e.g. the partition-window case where the receiver holds
/// a write the signer's expected hash excludes). When it doesn't,
/// `verify_post_reconcile_root_hash` flags the mismatch and the
/// backoff records a failure — operator-investigation territory
/// until pre-adoption rejection + rollback lands.
async fn reconcile_one_divergent_context(
&self,
group_id: calimero_context_config::types::ContextGroupId,
context_id: ContextId,
expected_root_hash: [u8; 32],
op_kind: &'static str,
) {
if let Some((remaining, failures)) =
reconcile_remaining_cooldown(&self.node_state.reconcile_attempts, &context_id)
{
tracing::debug!(
%context_id,
op_kind,
consecutive_failures = failures,
cooldown_remaining_secs = remaining.as_secs(),
"reconcile-after-divergence: skipping — prior attempts failed and the \
per-context cooldown is still active; will re-attempt after backoff lapses"
);
return;
}
// Look up anchors by `group_id` directly (carried in the
// divergence report) rather than re-deriving the group from
// `context_id`. A late-joiner can have a missing
// context→group mapping locally even though the group's
// trusted-anchor set is well-defined; the report already
// names the group authoritatively so use it as the source of
// truth.
let anchors = self.anchor_identities_for_group(&group_id);
if anchors.is_empty() {
tracing::warn!(
%context_id,
group_id = %hex::encode(group_id.to_bytes()),
op_kind,
"reconcile-after-divergence: no trusted anchors defined for this group — \
falling back to operator path (no automatic recovery)"
);
return;
}
// Pick an anchor from the gossipsub mesh on the context's
// topic. The mesh is a superset of "peers known to host this
// context" — same source the regular sync path uses.
//
// Randomise the order before filtering so that, when there
// are multiple connected anchors, we don't always pick the
// one gossipsub happens to list first. Matters for two
// reasons: (a) load distribution across honest anchors when
// one is slow; (b) a compromised anchor that consistently
// sorts first in libp2p's mesh order can't monopolise
// reconcile syncs without contention. Post-adoption hash
// verification against the signed expected still defends
// against any anchor serving non-canonical state.
let topic = TopicHash::from_raw(context_id);
let mut mesh_peers = self.network_client.mesh_peers(topic).await;
let mesh_peer_count = mesh_peers.len();
mesh_peers.shuffle(&mut rand::thread_rng());
// Walk mesh peers explicitly so cache-miss skips are visible
// to operators. A peer with no `peer_identities` entry has not
// yet been observed signing a verified message in this group;
// it is invisible to the anchor predicate even if it would be
// an anchor in practice. Counting and logging those skips
// distinguishes "no anchors reachable" from "anchors reachable
// but cache hasn't warmed yet" in the no-anchor warn below.
let mut peers_missing_cache_entry: usize = 0;
let mut peers_known_not_anchor: usize = 0;
let anchor_peer = mesh_peers.iter().copied().find(|peer| {
match self.node_state.peer_identities.get(peer) {
Some(ids) => {
if ids.iter().any(|id| anchors.contains(id)) {
true
} else {
peers_known_not_anchor += 1;
false
}
}
None => {
peers_missing_cache_entry += 1;
tracing::debug!(
%context_id,
%peer,
op_kind,
"reconcile-after-divergence: mesh peer skipped — no peer_identities \
cache entry yet (peer has not been observed signing a verified \
message); cache warms as the peer's signed traffic is processed"
);
false
}
}
});
let Some(anchor_peer) = anchor_peer else {
tracing::warn!(
%context_id,
op_kind,
anchor_count = anchors.len(),
connected_mesh_peers = mesh_peer_count,
peers_missing_cache_entry,
peers_known_not_anchor,
"reconcile-after-divergence: no connected mesh peer matches the anchor set — \
falling back to operator path; reconcile will re-attempt on the next signed \
op or sync tick"
);
return;
};
tracing::info!(
%context_id,
%anchor_peer,
op_kind,
expected_root_hash = %hex::encode(expected_root_hash),
"reconcile-after-divergence: pulling canonical state from trusted anchor"
);
match self.initiate_sync(context_id, anchor_peer).await {
Ok((peer_used, protocol)) => {
tracing::info!(
%context_id,
%peer_used,
?protocol,
"reconcile-after-divergence: anchor sync completed; verifying post-adoption hash"
);
// Use `peer_used` (the peer the sync actually
// resolved against) for verify-time logs rather than
// the originally-picked `anchor_peer`. The two
// normally agree, but `initiate_sync` is the
// authoritative source.
let converged = self.verify_post_reconcile_root_hash(
context_id,
expected_root_hash,
peer_used,
op_kind,
);
if converged {
record_reconcile_success(&self.node_state.reconcile_attempts, &context_id);
} else {
let failures =
record_reconcile_failure(&self.node_state.reconcile_attempts, context_id);
tracing::warn!(
%context_id,
op_kind,
consecutive_failures = failures,
next_cooldown_secs = reconcile_cooldown(failures).as_secs(),
"reconcile-after-divergence: recorded failure; subsequent reconcile \
attempts for this context are gated by the backoff window"
);
}
}
Err(err) => {
let failures =
record_reconcile_failure(&self.node_state.reconcile_attempts, context_id);
tracing::warn!(
%context_id,
%anchor_peer,
op_kind,
%err,
consecutive_failures = failures,
next_cooldown_secs = reconcile_cooldown(failures).as_secs(),
"reconcile-after-divergence: anchor sync failed; reconcile will re-attempt \
after the backoff window lapses"
);
}
}
}
/// Compare the local context's `root_hash` against the signed
/// `expected_root_hash` from the triggering op. On match, log
/// at info level — the reconcile succeeded. On mismatch, log
/// loudly at warn: the anchor served state that does not match
/// the canonical expected, OR the local apply diverged again
/// after sync. Either is operator-investigation territory and
/// a follow-up will replace this post-adoption check with
/// pre-adoption rejection + rollback once the store layer has
/// transactional staging.
fn verify_post_reconcile_root_hash(
&self,
context_id: ContextId,
expected_root_hash: [u8; 32],
anchor_peer: PeerId,
op_kind: &'static str,
) -> bool {
let Ok(Some(context)) = self.context_client.get_context(&context_id) else {
tracing::warn!(
%context_id,
%anchor_peer,
op_kind,
"reconcile-after-divergence: context not found locally after anchor sync — \
cannot verify root hash"
);
return false;
};
let actual_root_hash: [u8; 32] = *AsRef::<[u8; 32]>::as_ref(&context.root_hash);
if actual_root_hash == expected_root_hash {
tracing::info!(
%context_id,
%anchor_peer,
op_kind,
root_hash = %hex::encode(actual_root_hash),
"reconcile-after-divergence: post-adoption hash matches signed expected — converged"
);
true
} else {
tracing::warn!(
%context_id,
%anchor_peer,
op_kind,
expected_root_hash = %hex::encode(expected_root_hash),
actual_root_hash = %hex::encode(actual_root_hash),
"reconcile-after-divergence: post-adoption hash does NOT match signed expected — \
either the anchor served non-canonical state or local apply diverged again; \
operator-investigation territory until pre-adoption rejection lands"
);
false
}
}
}
/// Exponential cooldown for the reconcile-after-divergence backoff,
/// capped at 30 min. `consecutive_failures == 0` is illegal (the
/// caller only invokes this when at least one failure has been
/// recorded); we treat it the same as `1` to avoid an arithmetic
/// surprise. Schedule:
///
/// - 1 failure → 30s
/// - 2 failures → 60s
/// - 3 failures → 2m
/// - 4 failures → 4m
/// - 5 failures → 8m
/// - 6 failures → 16m
/// - 7+ failures → 30m (cap)
///
/// Free function so backoff math can be unit-tested independently.
fn reconcile_cooldown(consecutive_failures: u32) -> std::time::Duration {
const BASE_SECS: u64 = 30;
const MAX: std::time::Duration = std::time::Duration::from_secs(30 * 60);
let exp = consecutive_failures.saturating_sub(1).min(8);
let secs = BASE_SECS.saturating_mul(1u64 << u64::from(exp));
std::time::Duration::from_secs(secs).min(MAX)
}
/// If `context_id` has a recorded prior failure that is still within
/// its cooldown window, return `Some((remaining_cooldown,
/// consecutive_failures))`. Otherwise — no entry, or the cooldown has
/// elapsed — return `None`.
fn reconcile_remaining_cooldown(
attempts: &dashmap::DashMap<ContextId, crate::state::ReconcileAttempt>,
context_id: &ContextId,
) -> Option<(std::time::Duration, u32)> {
let entry = attempts.get(context_id)?;
let cooldown = reconcile_cooldown(entry.consecutive_failures);
let elapsed = entry.last_attempt_at.elapsed();
let remaining = cooldown.checked_sub(elapsed)?;
if remaining.is_zero() {
None
} else {
Some((remaining, entry.consecutive_failures))
}
}
/// Record a reconcile failure for `context_id`: bump
/// `consecutive_failures` and stamp `last_attempt_at = now`. Returns
/// the new failure count so the caller can log the next cooldown
/// directly.
fn record_reconcile_failure(
attempts: &dashmap::DashMap<ContextId, crate::state::ReconcileAttempt>,
context_id: ContextId,
) -> u32 {
let mut entry = attempts
.entry(context_id)
.or_insert_with(|| crate::state::ReconcileAttempt {
last_attempt_at: std::time::Instant::now(),
consecutive_failures: 0,
});
entry.consecutive_failures = entry.consecutive_failures.saturating_add(1);
entry.last_attempt_at = std::time::Instant::now();
entry.consecutive_failures
}
/// Clear backoff state for `context_id` after a successful reconcile.
/// Subsequent divergences are treated as fresh — no inherited cooldown.
fn record_reconcile_success(
attempts: &dashmap::DashMap<ContextId, crate::state::ReconcileAttempt>,
context_id: &ContextId,
) {
let _ = attempts.remove(context_id);
}
/// Stable-partition `peers` so peers with an observed trusted-anchor
/// identity come first while preserving the relative order within each
/// partition. Returns the index at which non-anchor peers start (i.e.
/// the count of anchor peers).
///
/// A peer is an anchor if at least one identity recorded in
/// `peer_identities` for that peer appears in `anchors`. An empty
/// `anchors` set returns 0 immediately — no point sorting if every
/// peer is going to be non-anchor.
///
/// The anchor predicate is materialized into a `Vec<bool>` keyed by
/// the peer's original index before sorting. This avoids reacquiring
/// the `DashMap` shard lock O(n log n) times during `sort_by_key`'s
/// comparisons, and prevents a concurrent cache mutation from causing
/// the post-sort anchor count to disagree with the actual partition
/// boundary — both `sort_by_key` and the count read from the same
/// snapshot.
///
/// Free function (not a method) so it can be unit-tested against
/// synthetic inputs without spinning up a sync manager.
fn partition_peers_anchor_first(
peers: &mut [libp2p::PeerId],
peer_identities: &dashmap::DashMap<
libp2p::PeerId,
std::collections::BTreeSet<calimero_primitives::identity::PublicKey>,
>,
anchors: &std::collections::BTreeSet<calimero_primitives::identity::PublicKey>,
) -> usize {
if anchors.is_empty() {
return 0;
}
let anchor_flags: Vec<bool> = peers
.iter()
.map(|peer| {
peer_identities
.get(peer)
.map(|ids| ids.iter().any(|id| anchors.contains(id)))
.unwrap_or(false)
})
.collect();
// sort_by_key over a pre-indexed flag table — stable, so the
// caller's random shuffle order is preserved within each partition.
let mut indices: Vec<usize> = (0..peers.len()).collect();
indices.sort_by_key(|&i| !anchor_flags[i]);
let anchor_count = anchor_flags.iter().filter(|&&f| f).count();
let reordered: Vec<libp2p::PeerId> = indices.iter().map(|&i| peers[i]).collect();
peers.copy_from_slice(&reordered);
anchor_count
}
impl SyncManager {
/// Actively request governance catch-up from a specific peer whose
/// identity we don't yet recognize as a context member.
///
/// Scenario: a peer opens a sync stream to us, but their identity isn't
/// in our local governance DAG yet because fire-and-forget `MemberAdded`
/// gossip (issue #2237) hasn't reached us. The legacy path waited 2 s
/// for gossip and then closed the stream, stalling the initiator for
/// up to 30 s (`NamespaceStateHeartbeat` cadence). Instead, open a
/// separate stream back to the peer with `NamespaceBackfillRequest`
/// (empty `delta_ids` = "send everything you have for this namespace"),
/// apply every op they return, and let the caller re-check membership.
///
/// Best-effort: any failure (no group resolved, stream open fails,
/// peer returns no ops, ops fail to apply) is logged at debug and the
/// caller proceeds to close the stream as before. The real fix is the
/// three-phase contract in #2237; this is a responder-side bandaid
/// that turns a 30 s stall into at worst a second round-trip.
async fn request_governance_catchup_from_peer(
&self,
peer_id: PeerId,
context_id: &ContextId,
their_identity: &PublicKey,
) {
let store = self.context_client.datastore();
let namespace_id =
match calimero_context::group_store::get_group_for_context(store, context_id) {
Ok(Some(group_id)) => {
match calimero_context::group_store::resolve_namespace(store, &group_id) {
Ok(ns) => ns.to_bytes(),
Err(err) => {
debug!(
%context_id,
%their_identity,
%err,
"failed to resolve namespace for governance catch-up"
);
return;
}
}
}
Ok(None) => {
debug!(
%context_id,
%their_identity,
"context not in a group — no namespace to request catch-up from"
);
return;
}
Err(err) => {
debug!(
%context_id,
%their_identity,
%err,
"failed to resolve group for governance catch-up"
);
return;
}
};
let mut stream = match self.network_client.open_stream(peer_id).await {
Ok(s) => s,
Err(err) => {
debug!(
%context_id,
%their_identity,
%peer_id,
%err,
"failed to open catch-up stream to peer"
);
return;
}
};
let msg = StreamMessage::Init {
context_id: ContextId::from([0u8; 32]),
party_id: PublicKey::from([0u8; 32]),
payload: InitPayload::NamespaceBackfillRequest {
namespace_id,
delta_ids: Vec::new(),
},
next_nonce: rand::thread_rng().gen(),
};
if let Err(err) = super::stream::send(&mut stream, &msg, None).await {
debug!(
%context_id,
%their_identity,
%peer_id,
%err,
"failed to send NamespaceBackfillRequest during catch-up"
);
return;
}
let response = match super::stream::recv(&mut stream, None, self.sync_config.timeout).await
{
Ok(Some(StreamMessage::Message {
payload: MessagePayload::NamespaceBackfillResponse { deltas },
..
})) => deltas,
Ok(_) => {
debug!(
%context_id,
%their_identity,
%peer_id,
"unexpected response to NamespaceBackfillRequest during catch-up"
);
return;
}
Err(err) => {
debug!(
%context_id,
%their_identity,
%peer_id,
%err,
"catch-up NamespaceBackfillRequest timed out or failed"
);
return;
}
};
if response.is_empty() {
debug!(
%context_id,
%their_identity,
%peer_id,
"peer returned no namespace ops for catch-up"
);
return;
}
use calimero_context_client::messages::NamespaceApplyOutcome;
let ops_count = response.len();
let mut applied = 0usize;
let mut newly_applied = 0usize;
for (_delta_id, op_bytes) in response {
let op = match borsh::from_slice::<
calimero_context_client::local_governance::SignedNamespaceOp,
>(&op_bytes)
{
Ok(o) => o,
Err(err) => {
debug!(
%context_id,
%their_identity,
%err,
"failed to decode catch-up op"
);
continue;
}
};
match self.context_client.apply_signed_namespace_op(op).await {
Ok(NamespaceApplyOutcome::Applied { .. }) => {
applied += 1;
newly_applied += 1;
}
Ok(_) => {
applied += 1;
}
Err(err) => {
debug!(
%context_id,
%their_identity,
%err,
"failed to apply catch-up op"
);
continue;
}
}
}
// Single FSM notification after the batch when we actually
// advanced the local applied_through. `Pending` (parents missing)
// and `Duplicate` outcomes are no-progress from the FSM's POV,
// so we skip the mailbox hop in those cases. Mirrors the gate
// used at `network_event/namespace.rs:120`.
if newly_applied > 0 {
self.node_client.notify_namespace_op_applied(namespace_id);
}
debug!(
%context_id,
%their_identity,
%peer_id,
ops_received = ops_count,
ops_applied = applied,
"governance catch-up complete"
);
}
/// Handle a namespace backfill request: look up full `SignedNamespaceOp`
/// payloads for the requested delta IDs and send them back.
///
/// We scan the namespace governance op store for matching delta IDs.
/// For each requested delta, if we have the full op (stored when we were
/// a member at apply time), we include it in the response.
async fn handle_namespace_backfill_request(
&self,
namespace_id: [u8; 32],
delta_ids: &[[u8; 32]],
stream: &mut Stream,
nonce: Nonce,
) -> eyre::Result<()> {
let store = self.context_client.datastore_handle().into_inner();
let handle = store.handle();
let mut found = Vec::new();
/// Maximum ops returned in a single backfill response to prevent
/// memory exhaustion from large namespace governance DAGs.
const MAX_BACKFILL_OPS: usize = 500;
if delta_ids.is_empty() {
// Empty request = "give me everything for this namespace".
let start = calimero_store::key::NamespaceGovOp::new(namespace_id, [0u8; 32]);
let mut iter = handle.iter::<calimero_store::key::NamespaceGovOp>()?;
let first = iter.seek(start).transpose();
for entry in first.into_iter().chain(iter.keys()) {
let key = match entry {
Ok(k) => k,
Err(_) => break,
};
if key.namespace_id() != namespace_id {
break;
}
if let Ok(Some(value)) = handle.get(&key) {
if let Some(signed_bytes) =
crate::sync::helpers::extract_signed_op_bytes(&value.skeleton_bytes)
{
found.push((key.delta_id(), signed_bytes));
if found.len() >= MAX_BACKFILL_OPS {
break;
}
}
}
}
} else {
for delta_id in delta_ids.iter().take(MAX_BACKFILL_OPS) {
let key = calimero_store::key::NamespaceGovOp::new(namespace_id, *delta_id);
if let Ok(Some(value)) = handle.get(&key) {
if let Some(signed_bytes) =
crate::sync::helpers::extract_signed_op_bytes(&value.skeleton_bytes)
{
found.push((*delta_id, signed_bytes));
}
}
}
}
let msg = StreamMessage::Message {
sequence_id: 0,
payload: MessagePayload::NamespaceBackfillResponse { deltas: found },
next_nonce: nonce,
};
super::stream::send(stream, &msg, None).await?;
Ok(())
}
/// Handle an incoming NamespaceJoinRequest on the responder side.
///
/// Validates the invitation, wraps the group key for the joiner,
/// enumerates contexts, and collects governance ops.
async fn handle_namespace_join_request(
&self,
namespace_id: [u8; 32],
invitation_bytes: &[u8],
joiner_public_key: PublicKey,
stream: &mut Stream,
nonce: Nonce,
) -> eyre::Result<()> {
use calimero_context::group_store::{
enumerate_group_contexts, get_default_capabilities, load_current_group_key,
load_group_meta, wrap_group_key_for_member,
};
use calimero_context_config::types::ContextGroupId;
use calimero_context_config::types::SignedGroupOpenInvitation;
let _invitation: SignedGroupOpenInvitation = match borsh::from_slice(invitation_bytes) {
Ok(inv) => inv,
Err(err) => {
let msg = StreamMessage::Message {
sequence_id: 0,
payload: MessagePayload::NamespaceJoinRejected {
reason: format!("invalid invitation: {err}"),
},
next_nonce: nonce,
};
super::stream::send(stream, &msg, None).await?;
return Ok(());
}
};
let group_id = ContextGroupId::from(namespace_id);
let store = self.context_client.datastore_handle().into_inner();
let meta = match load_group_meta(&store, &group_id)? {
Some(m) => m,
None => {
let msg = StreamMessage::Message {
sequence_id: 0,
payload: MessagePayload::NamespaceJoinRejected {
reason: "group not found".to_owned(),
},
next_nonce: nonce,
};
super::stream::send(stream, &msg, None).await?;
return Ok(());
}
};
let key_envelope_bytes = match load_current_group_key(&store, &group_id)? {
Some((_key_id, group_key)) => {
let ns_identity = calimero_context::group_store::resolve_namespace_identity_record(
&store, &group_id,
)?;
match ns_identity {
Some(record) => {
let sender_sk =
calimero_primitives::identity::PrivateKey::from(record.private_key);
match wrap_group_key_for_member(&sender_sk, &joiner_public_key, &group_key)
{
Ok(envelope) => borsh::to_vec(&envelope).unwrap_or_default(),
Err(err) => {
warn!(
namespace_id = %hex::encode(namespace_id),
%err,
"failed to wrap group key for joiner"
);
Vec::new()
}
}
}
None => {
warn!(
namespace_id = %hex::encode(namespace_id),
"no namespace identity found, cannot wrap key"
);
Vec::new()
}
}
}
None => Vec::new(),
};
// Pre-register the joiner as a group member and write ContextIdentity
// entries so that when the joiner opens a sync stream, this node's
// membership check (has_member) passes immediately.
if let Err(e) = calimero_context::group_store::add_group_member(
&store,
&group_id,
&joiner_public_key,
calimero_primitives::context::GroupMemberRole::Member,
) {
warn!(%e, "failed to pre-register joiner as group member");
}
let context_ids = enumerate_group_contexts(&store, &group_id, 0, usize::MAX)?;
let application_id: [u8; 32] = *meta.target_application_id.as_ref();
for ctx_id in &context_ids {
let ci_key = calimero_store::key::ContextIdentity::new(*ctx_id, joiner_public_key);
let mut handle = store.handle();
if !handle.has(&ci_key).unwrap_or(false) {
let _ = handle.put(
&ci_key,
&calimero_store::types::ContextIdentity {
private_key: None,
sender_key: None,
},
);
}
}
let governance_ops = self.collect_namespace_governance_ops(namespace_id)?;
// Issue #2256: the namespace's default-capabilities value travels
// with the bundle so the joiner doesn't need to fall back to a
// hard-coded constant. Read whatever the responder currently
// believes (already reflects any admin-issued
// `DefaultCapabilitiesSet` ops because the local store is
// updated as those ops apply). `unwrap_or(0)` matches the
// pre-existing semantics for "default key absent."
let default_capabilities = get_default_capabilities(&store, &group_id)?.unwrap_or(0);
debug!(
namespace_id = %hex::encode(namespace_id),
has_key = !key_envelope_bytes.is_empty(),
context_count = context_ids.len(),
app_id = %hex::encode(application_id),
governance_ops_count = governance_ops.len(),
default_capabilities,
"Sending NamespaceJoinResponse"
);
let msg = StreamMessage::Message {
sequence_id: 0,
payload: MessagePayload::NamespaceJoinResponse {
key_envelope_bytes,
context_ids,
application_id,
governance_ops,
default_capabilities,
},
next_nonce: nonce,
};
super::stream::send(stream, &msg, None).await?;
Ok(())
}
/// Handle an incoming `OpenSubgroupJoinRequest` (issue #2357) on the
/// responder side. Validates that the joiner has
/// `MembershipPath::Inherited` to the requested subgroup, wraps the
/// local subgroup key for the joiner via ECDH, and replies with the
/// envelope. Mirrors `handle_namespace_join_request` for the
/// inherited self-join path.
async fn handle_open_subgroup_join_request(
&self,
namespace_id: [u8; 32],
subgroup_id: [u8; 32],
joiner_public_key: PublicKey,
stream: &mut Stream,
nonce: Nonce,
) -> eyre::Result<()> {
use calimero_context::group_store::{
check_group_membership_path, load_current_group_key, load_group_meta,
resolve_namespace, resolve_namespace_identity_record, wrap_group_key_for_member,
MembershipPath,
};
use calimero_context_config::types::ContextGroupId;
let subgroup_gid = ContextGroupId::from(subgroup_id);
let store = self.context_client.datastore_handle().into_inner();
// Cross-namespace pin: the requested subgroup must belong to the
// namespace the joiner named, otherwise an attacker on namespace
// A could elicit a key for a subgroup of namespace B.
match resolve_namespace(&store, &subgroup_gid) {
Ok(ns) if ns.to_bytes() == namespace_id => {}
Ok(other_ns) => {
let msg = StreamMessage::Message {
sequence_id: 0,
payload: MessagePayload::OpenSubgroupJoinRejected {
reason: format!(
"subgroup belongs to namespace {} not {}",
hex::encode(other_ns.to_bytes()),
hex::encode(namespace_id),
),
},
next_nonce: nonce,
};
super::stream::send(stream, &msg, None).await?;
return Ok(());
}
Err(err) => {
let msg = StreamMessage::Message {
sequence_id: 0,
payload: MessagePayload::OpenSubgroupJoinRejected {
reason: format!("resolve namespace: {err}"),
},
next_nonce: nonce,
};
super::stream::send(stream, &msg, None).await?;
return Ok(());
}
}
if load_group_meta(&store, &subgroup_gid)?.is_none() {
let msg = StreamMessage::Message {
sequence_id: 0,
payload: MessagePayload::OpenSubgroupJoinRejected {
reason: "subgroup not found locally".to_owned(),
},
next_nonce: nonce,
};
super::stream::send(stream, &msg, None).await?;
return Ok(());
}
// Authorisation check: the joiner must reach the subgroup via the
// Open-chain inheritance walk. `MembershipPath::Inherited`
// implies every intermediate ancestor was Open (see
// `membership.rs:267`), so this is the proof of authorisation.
match check_group_membership_path(&store, &subgroup_gid, &joiner_public_key)? {
MembershipPath::Inherited { .. } | MembershipPath::Direct => {}
MembershipPath::None => {
let msg = StreamMessage::Message {
sequence_id: 0,
payload: MessagePayload::OpenSubgroupJoinRejected {
reason: "joiner has no membership path to subgroup".to_owned(),
},
next_nonce: nonce,
};
super::stream::send(stream, &msg, None).await?;
return Ok(());
}
}
let key_envelope_bytes = match load_current_group_key(&store, &subgroup_gid)? {
Some((_key_id, group_key)) => {
let ns_gid = ContextGroupId::from(namespace_id);
match resolve_namespace_identity_record(&store, &ns_gid)? {
Some(record) => {
let sender_sk =
calimero_primitives::identity::PrivateKey::from(record.private_key);
match wrap_group_key_for_member(&sender_sk, &joiner_public_key, &group_key)
{
Ok(envelope) => borsh::to_vec(&envelope).unwrap_or_default(),
Err(err) => {
warn!(
namespace_id = %hex::encode(namespace_id),
subgroup_id = %hex::encode(subgroup_id),
%err,
"failed to wrap subgroup key for joiner"
);
Vec::new()
}
}
}
None => {
warn!(
namespace_id = %hex::encode(namespace_id),
"no namespace identity, cannot wrap subgroup key"
);
Vec::new()
}
}
}
None => Vec::new(),
};
debug!(
namespace_id = %hex::encode(namespace_id),
subgroup_id = %hex::encode(subgroup_id),
has_key = !key_envelope_bytes.is_empty(),
"Sending OpenSubgroupJoinResponse"
);
let msg = StreamMessage::Message {
sequence_id: 0,
payload: MessagePayload::OpenSubgroupJoinResponse { key_envelope_bytes },
next_nonce: nonce,
};
super::stream::send(stream, &msg, None).await?;
Ok(())
}
/// Initiator side for `request_open_subgroup_join`. Picks a mesh peer
/// on the namespace topic, opens a stream, sends the request, and
/// returns the wrapped key envelope. Same peer-discovery retry loop
/// as `initiate_namespace_join`.
async fn initiate_open_subgroup_join(
&self,
params: OpenSubgroupJoinParams,
) -> eyre::Result<Vec<u8>> {
let topic = libp2p::gossipsub::TopicHash::from_raw(format!(
"ns/{}",
hex::encode(params.namespace_id)
));
let mut peers = Vec::new();
for attempt in 1..=super::config::DEFAULT_MESH_RETRIES_UNINITIALIZED {
peers = self.network_client.mesh_peers(topic.clone()).await;
if !peers.is_empty() {
break;
}
if attempt < super::config::DEFAULT_MESH_RETRIES_UNINITIALIZED {
debug!(
namespace_id = %hex::encode(params.namespace_id),
subgroup_id = %hex::encode(params.subgroup_id),
attempt,
"No namespace mesh peers yet for open-subgroup join, retrying..."
);
time::sleep(std::time::Duration::from_millis(
super::config::DEFAULT_MESH_RETRY_DELAY_MS_UNINITIALIZED,
))
.await;
}
}
let peer = peers.first().ok_or_else(|| {
eyre::eyre!(
"no mesh peers for namespace {} (open-subgroup join)",
hex::encode(params.namespace_id)
)
})?;
let mut stream = self
.network_client
.open_stream(*peer)
.await
.wrap_err("open stream for open-subgroup join")?;
let msg = StreamMessage::Init {
context_id: calimero_primitives::context::ContextId::from([0u8; 32]),
party_id: params.joiner_public_key,
payload: InitPayload::OpenSubgroupJoinRequest {
namespace_id: params.namespace_id,
subgroup_id: params.subgroup_id,
joiner_public_key: params.joiner_public_key,
},
next_nonce: rand::thread_rng().gen(),
};
super::stream::send(&mut stream, &msg, None).await?;
match super::stream::recv(&mut stream, None, self.sync_config.timeout).await? {
Some(StreamMessage::Message {
payload: MessagePayload::OpenSubgroupJoinResponse { key_envelope_bytes },
..
}) => {
if key_envelope_bytes.is_empty() {
eyre::bail!(
"responder did not hold the subgroup key for {}",
hex::encode(params.subgroup_id)
);
}
Ok(key_envelope_bytes)
}
Some(StreamMessage::Message {
payload: MessagePayload::OpenSubgroupJoinRejected { reason },
..
}) => {
eyre::bail!("open-subgroup join rejected: {}", reason)
}
other => {
eyre::bail!(
"unexpected response to open-subgroup join request: {:?}",
other.as_ref().map(|m| std::mem::discriminant(m))
)
}
}
}
/// Collect all governance ops for a namespace (reused by the join responder).
///
/// Returns bare `SignedNamespaceOp` bytes (not `StoredNamespaceEntry` wrapped)
/// so recipients can `borsh::from_slice::<SignedNamespaceOp>` directly.
fn collect_namespace_governance_ops(
&self,
namespace_id: [u8; 32],
) -> eyre::Result<Vec<Vec<u8>>> {
let store = self.context_client.datastore_handle().into_inner();
let handle = store.handle();
let mut ops = Vec::new();
let start = calimero_store::key::NamespaceGovOp::new(namespace_id, [0u8; 32]);
let mut iter = handle.iter::<calimero_store::key::NamespaceGovOp>()?;
let first = iter.seek(start).transpose();
for entry in first.into_iter().chain(iter.keys()) {
let key = match entry {
Ok(k) => k,
Err(_) => break,
};
if key.namespace_id() != namespace_id {
break;
}
if let Ok(Some(value)) = handle.get(&key) {
if let Some(bytes) =
crate::sync::helpers::extract_signed_op_bytes(&value.skeleton_bytes)
{
ops.push(bytes);
}
}
}
Ok(ops)
}
/// Initiator side: open a stream to a mesh peer and perform the
/// NamespaceJoinRequest / NamespaceJoinResponse exchange.
async fn initiate_namespace_join(
&self,
params: NamespaceJoinParams,
) -> eyre::Result<JoinBundle> {
let topic = libp2p::gossipsub::TopicHash::from_raw(format!(
"ns/{}",
hex::encode(params.namespace_id)
));
// Retry peer discovery: gossipsub mesh for the namespace topic may
// still be forming when this runs (mDNS + GRAFT exchange can take
// several seconds). Use the same retry window as uninitialized
// context sync (10 × 1 s = 10 s) to ensure we don't give up too
// quickly in slower environments (CI, Docker, mDNS cold-start).
let mut peers = Vec::new();
for attempt in 1..=super::config::DEFAULT_MESH_RETRIES_UNINITIALIZED {
peers = self.network_client.mesh_peers(topic.clone()).await;
if !peers.is_empty() {
break;
}
if attempt < super::config::DEFAULT_MESH_RETRIES_UNINITIALIZED {
debug!(
namespace_id = %hex::encode(params.namespace_id),
attempt,
"No namespace mesh peers yet, retrying..."
);
time::sleep(std::time::Duration::from_millis(
super::config::DEFAULT_MESH_RETRY_DELAY_MS_UNINITIALIZED,
))
.await;
}
}
let peer = peers.first().ok_or_else(|| {
eyre::eyre!(
"no mesh peers for namespace {}",
hex::encode(params.namespace_id)
)
})?;
let mut stream = self
.network_client
.open_stream(*peer)
.await
.wrap_err("open stream for namespace join")?;
let msg = StreamMessage::Init {
context_id: calimero_primitives::context::ContextId::from([0u8; 32]),
party_id: params.joiner_public_key,
payload: InitPayload::NamespaceJoinRequest {
namespace_id: params.namespace_id,
invitation_bytes: params.invitation_bytes,
joiner_public_key: params.joiner_public_key,
},
next_nonce: rand::thread_rng().gen(),
};
super::stream::send(&mut stream, &msg, None).await?;
match super::stream::recv(&mut stream, None, self.sync_config.timeout).await? {
Some(StreamMessage::Message {
payload:
MessagePayload::NamespaceJoinResponse {
key_envelope_bytes,
context_ids,
application_id,
governance_ops,
default_capabilities,
},
..
}) => Ok(JoinBundle {
key_envelope_bytes,
context_ids,
application_id: application_id.into(),
governance_ops,
default_capabilities,
}),
Some(StreamMessage::Message {
payload: MessagePayload::NamespaceJoinRejected { reason },
..
}) => {
eyre::bail!("namespace join rejected: {}", reason)
}
other => {
eyre::bail!(
"unexpected response to namespace join request: {:?}",
other.as_ref().map(|m| std::mem::discriminant(m))
)
}
}
}
/// Pull all namespace governance ops from a mesh peer.
async fn sync_namespace_from_peer(&self, namespace_id: [u8; 32]) {
use calimero_node_primitives::sync::{InitPayload, MessagePayload, StreamMessage};
let topic =
libp2p::gossipsub::TopicHash::from_raw(format!("ns/{}", hex::encode(namespace_id)));
let peers = self.network_client.mesh_peers(topic).await;
let Some(peer) = peers.first() else {
debug!(
namespace_id = %hex::encode(namespace_id),
"no mesh peers for namespace sync"
);
return;
};
let Ok(mut stream) = self.network_client.open_stream(*peer).await else {
debug!("failed to open stream for namespace sync");
return;
};
let msg = StreamMessage::Init {
context_id: calimero_primitives::context::ContextId::from([0u8; 32]),
party_id: calimero_primitives::identity::PublicKey::from([0u8; 32]),
payload: InitPayload::NamespaceBackfillRequest {
namespace_id,
delta_ids: vec![],
},
next_nonce: {
use rand::Rng;
rand::thread_rng().gen()
},
};
if let Err(err) = super::stream::send(&mut stream, &msg, None).await {
debug!(%err, "failed to send NamespaceBackfillRequest");
return;
}
match super::stream::recv(&mut stream, None, self.sync_config.timeout).await {
Ok(Some(StreamMessage::Message {
payload: MessagePayload::NamespaceBackfillResponse { deltas },
..
})) => {
info!(
namespace_id = %hex::encode(namespace_id),
ops = deltas.len(),
"received namespace governance ops from peer"
);
use calimero_context_client::messages::NamespaceApplyOutcome;
let mut newly_applied = false;
// Collect divergence reports surfaced by `MemberRemoved` /
// `MemberLeft` ops arriving via the namespace-backfill
// path. Same reasoning as the gossip-receive path: once
// the DAG marks an op `Applied`, any later gossipsub
// arrival of the same op becomes `Duplicate` and the
// apply work — including the post-apply hash check —
// is skipped. If a `MemberRemoved` op arrives first via
// backfill and divergence is dropped here, no later
// path will re-surface it. Fire reconcile after the
// batch loop so we don't hold `&mut` borrows across an
// await on `self`.
let mut pending_divergences: Vec<
calimero_context_client::messages::DivergenceReport,
> = Vec::new();
for (delta_id, op_bytes) in deltas {
match borsh::from_slice::<
calimero_context_client::local_governance::SignedNamespaceOp,
>(&op_bytes)
{
Ok(op) => {
match self
.context_client
.apply_signed_namespace_op(op.clone())
.await
{
Err(err) => {
// Capture enough context to diagnose codec/schema
// mismatches (observed as "Unexpected length of
// input" from the inner GroupOp decode when a
// variant's binary layout has drifted). The
// op-type tag + byte-length give us a fingerprint
// without logging potentially sensitive payload.
let op_kind = match &op.op {
calimero_context_client::local_governance::NamespaceOp::Root(r) => {
format!("Root::{r:?}").split('{').next().unwrap_or("Root").trim().to_owned()
}
calimero_context_client::local_governance::NamespaceOp::Group { .. } => {
"Group".to_owned()
}
};
warn!(
namespace_id = %hex::encode(namespace_id),
delta_id = %hex::encode(delta_id),
op_kind = %op_kind,
signer = %op.signer,
nonce = op.nonce,
op_bytes_len = op_bytes.len(),
?err,
"failed to apply namespace governance op from backfill"
);
}
Ok(NamespaceApplyOutcome::Applied { divergence }) => {
newly_applied = true;
if let Some(report) = divergence {
pending_divergences.push(report);
}
// Only react to a *newly-applied*
// `MemberJoined`. On `Duplicate`
// (the common case — a backfill
// re-sends the whole DAG every
// round) re-publishing a fresh
// `KeyDelivery` each time would
// grow the namespace governance
// DAG without bound until it hits
// the backfill cap and never
// converges again (#2319).
crate::key_delivery::maybe_publish_key_delivery(
&self.context_client,
&self.node_client,
&op,
)
.await;
}
Ok(_) => {}
}
}
Err(err) => {
warn!(
namespace_id = %hex::encode(namespace_id),
delta_id = %hex::encode(delta_id),
op_bytes_len = op_bytes.len(),
op_bytes_prefix = %hex::encode(&op_bytes[..op_bytes.len().min(64)]),
%err,
"failed to decode namespace governance op from backfill"
);
}
}
}
// FSM notify after the batch — gated on at least one
// `Applied` outcome (Pending/Duplicate are no-progress).
// See the governance-catch-up notify above for rationale.
if newly_applied {
self.node_client.notify_namespace_op_applied(namespace_id);
}
// Route any divergence reports surfaced during the
// backfill apply loop to the reconcile-via-anchor path.
// Run sequentially after the batch finishes; we're
// already in an async method on `&self` so no spawn
// is needed here (the gossip-receive path uses
// `actix::spawn` because it runs inside an actor's
// mailbox slot; this method is invoked by the sync
// tick which has no such constraint).
for report in pending_divergences {
self.reconcile_after_divergence(report).await;
}
}
_ => {
debug!("unexpected response to namespace sync request");
}
}
}
}
#[cfg(test)]
mod tests;