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
use std::{
collections::{HashMap, HashSet},
io::Write,
};
use priority_queue::PriorityQueue;
use wot_network::{
Binding,
Certificate,
Network,
TrustDepth,
search::{Path as WotNetworkPath, Paths, TrustAnchor as WotTrustAnchor, WotSearchTrait},
};
use crate::{
debug::mermaid_graph,
internal_prelude::*,
queue::NodePriority,
types::{
certification::Certification,
edges::{Delegation, PathEdge, ReverseEdge, UsedEdge},
node::{EdgeSearchInfo, Node, TrustAnchor},
path::Path,
target_node::TargetNode,
},
};
impl WotSearchTrait for FlowGraph {
fn new(network: Network, anchors: Vec<WotTrustAnchor>, target: Binding) -> Self {
Self::new(network, anchors, target)
}
fn search(&mut self, target_trust_amount: usize) -> Paths {
FlowGraph::search(self, Some(target_trust_amount))
}
}
/// Represents a flow network graph for determining the total trust between a target [`Binding`]
/// and a set of [`TrustAnchor`](WotTrustAnchor)s.
#[derive(Debug, Clone)]
pub struct FlowGraph {
/// The [`Node`]s in the flow graph.
///
/// Each node represents a [`Certificate`] in the network.
nodes: HashMap<Certificate, Node>,
trust_anchors: HashMap<Certificate, TrustAnchor>,
/// The [`TargetNode`] representation.
///
/// Contains runtime data that's relevant for our Ford-Fulkerson variant.
target_node: TargetNode,
current_path_id: usize,
}
impl FlowGraph {
/// Constructs a new [`FlowGraph`] instance.
///
/// The resulting [`FlowGraph`] can be used to detect flow paths from the trust anchors towards
/// the target binding by calling [`FlowGraph::search`].
///
/// # Parameters
///
/// - `network`: The [`Network`] containing all certificates, delegations, and certifications.
/// - `trust_anchors`: The set of trust anchors that should be used for this search.
/// - `target_binding`: The target [`Binding`] for which the trust is to be determined.
pub fn new(
mut network: Network,
trust_anchors: Vec<WotTrustAnchor>,
target_binding: Binding,
) -> Self {
// Since we're only interested in a single target, we can ignore all certifications except
// the ones on the target binding.
let target_certifications = network
.certifications
.remove(&target_binding)
.unwrap_or_default();
// Transform them from wot_network Certifications into our own representation.
let certifications = target_certifications
.into_iter()
.map(Certification::new)
.collect();
let target_node = TargetNode::new(target_binding, certifications);
// Convert the certificates into `Node`s.
// Nodes contain additional runtime information used during our Ford-Fulkerson derivative.
let mut nodes = HashMap::new();
for (cert, delegations) in network.delegations.into_iter() {
// Make sure all delegating nodes exist as well.
for delegation in &delegations {
let issuer = delegation.issuer().clone();
nodes
.entry(issuer.clone())
.or_insert(Node::new(issuer, Vec::new()));
}
let node = nodes
.entry(cert.clone())
.or_insert(Node::new(cert, Vec::new()));
node.delegations = delegations.into_iter().map(Delegation::new).collect();
}
// Create `Node`s for certifications.
// Certification issuer nodes may not be part of the remaining network. For the search
// algorithm to work, a respective Node must exist for every certification issuer.
for certification in target_node.certifications.iter() {
if !nodes.contains_key(&certification.inner.issuer) {
nodes.insert(
certification.inner.issuer.clone(),
Node::new(certification.inner.issuer.clone(), Vec::new()),
);
}
}
// A map of trust anchors and their respective trust.
// Converted into a HashMap for faster lookup.
let trust_anchors: HashMap<Certificate, TrustAnchor> = trust_anchors
.into_iter()
.map(|anchor| (anchor.cert.clone(), TrustAnchor::new(anchor.clone())))
.collect();
FlowGraph {
nodes,
trust_anchors,
target_node,
current_path_id: 0,
}
}
/// Returns a reference to the nodes in the flow graph.
pub(crate) fn nodes(&self) -> &HashMap<Certificate, Node> {
&self.nodes
}
/// Returns a reference to the trust anchors map.
pub(crate) fn trust_anchors(&self) -> &HashMap<Certificate, TrustAnchor> {
&self.trust_anchors
}
/// Returns a reference to the target node.
pub(crate) fn target_node(&self) -> &TargetNode {
&self.target_node
}
/// Performs a full Berblom search on a Web-of-Trust (WoT) network.
///
/// Returns fully-resolved, valid, and non-conflicting trust paths that represent one possible
/// set of paths achieving the maximum possible trust between the target binding and the
/// specified trust anchors.
///
/// # Note
///
/// There may exist multiple different sets of paths that achieve the same total trust in the
/// network. This algorithm only returns a single of these possible sets.
///
/// # Parameters
///
/// - `target_trust_amount`: Without this parameter, the maximum flow will be determined, even
/// if it's above `120` or `255`. If you are only interested in figuring out whether a certain
/// trust amount can be reached, use this prevent unnecessary path exploration.
pub fn search(&mut self, target_trust_amount: Option<usize>) -> Paths {
// The list of paths
// It is **absolutely crucial** to not re-order this list of paths!!
// Reverse-edges reference the indices of these paths, which is a necessary relationship to
// efficiently consolidate paths in the very last step!
//
// Path indices must **never** be changed!!!
let mut paths = Vec::new();
let mut total_trust: usize = 0;
debug!("===== Berblom Algorithm =====");
debug!(
"→ Search for target binding {} ({})",
self.target_node.binding.cert, self.target_node.binding.identity
);
debug!("→ Trust anchors are: {:#?}", self.trust_anchors);
while let Some(path) = self.step() {
let total_trust_after_path = total_trust + path.trust_amount as usize;
total_trust = total_trust_after_path;
trace!(
"Path is '{}' with amount {}",
path.simple_display(),
path.trust_amount,
);
paths.push(path);
// Check if we reached the target amount. If so, we may stop early.
if let Some(target_amount) = target_trust_amount
&& total_trust >= target_amount
{
break;
}
}
let paths = Self::consolidate_paths(paths, None::<&mut std::fs::File>).unwrap();
// Now transform all paths into the [`wot_network::Path`] type.
let wot_network_paths = paths
.into_iter()
.map(|path| {
(WotNetworkPath {
start: path.trust_anchor,
delegations: path
.edges
.into_iter()
.map(|edge| {
let PathEdge::ForwardEdge(delegation) = edge else {
panic!("Found reverse edge {edge:?} on resolved path. This is a bug!")
};
delegation.inner
})
.collect(),
certification: path.certification.inner,
}, path.trust_amount)
})
.collect();
Paths::new_with_paths(wot_network_paths)
}
/// Call [`Self::search`], whilst creating a debug markdown writer with mermaid
/// representations of the search process.
///
/// At every step of the Berblom algorithm, the residual graph will be written as a mermaid
/// graph. If a path can found on that graph, the graph will be written once more, but
/// with that path being highlighted.
///
/// # Note
///
/// This is a debug tool with limits:
/// - Mermaid is not capable of keeping a layout between steps. Especially the introduction of
/// reverse edges drastically changes the resulting graphs.
/// - Mermaid does not prevent overlap of edge text. Some edge information may simply not be
/// readable.
/// - For performance reasons, this implementation uses HashMaps, which is why there are no
/// guarantees in which order edges are added to the Mermaid diagram. Edge declaration order
/// matters in Mermaid and will result in different layouts. This means that graph layouts
/// will not be idempotent, despite the input data being identical.
pub fn search_with_mermaid<W: Write>(
&mut self,
target_trust_amount: Option<usize>,
writer: &mut W,
) -> std::io::Result<Paths> {
// The list of paths
// It is **absolutely crucial** to not re-order this list of paths!!
// Reverse-edges reference the indices of these paths, which is a necessary relationship to
// efficiently consolidate paths in the very last step!
//
// Path indices must **never** change!!!
let mut paths = Vec::new();
let mut current_step = 1;
writeln!(writer, "# Step 1")?;
writeln!(writer, "\n```mermaid")?;
write!(writer, "{}", &mermaid_graph(self, None))?;
writeln!(writer, "```\n")?;
// Save the current graph for the mermaid graph with a found path.
// Once we get the path in here, the residual graph (self) is already adjusted.
// But the path needs to be drawn on the original graph.
let mut previous_graph = self.clone();
let mut total_trust: usize = 0;
while let Some(path) = self.step() {
println!("Path: {}", path.simple_display());
// Draw the path on the graph before i has been updated.
writeln!(writer, "## Path for {current_step}")?;
writeln!(writer, "Path: `{}`\\n", path.simple_display())?;
writeln!(writer, "\n```mermaid")?;
write!(writer, "{}", mermaid_graph(&previous_graph, Some(&path)))?;
writeln!(writer, "```\n")?;
current_step += 1;
// Draw the updated graph for the next step.
writeln!(writer, "# Step {current_step}")?;
writeln!(writer, "\n```mermaid")?;
write!(writer, "{}", &mermaid_graph(self, None))?;
writeln!(writer, "```\n")?;
// Save the current graph for the next iteration
previous_graph = self.clone();
let total_trust_after_path = total_trust + path.trust_amount as usize;
total_trust = total_trust_after_path;
trace!(
"Path is '{}' with amount {}",
path.simple_display(),
path.trust_amount,
);
paths.push(path);
// Check if we reached the target amount. If so, we may stop early.
if let Some(target_amount) = target_trust_amount
&& total_trust >= target_amount
{
break;
}
}
let paths = Self::consolidate_paths(paths, Some(writer))?;
// Now transform all paths into the [`wot_network::Path`] type.
let wot_network_paths = paths
.into_iter()
.map(|path| {
(WotNetworkPath {
start: path.trust_anchor,
delegations: path
.edges
.into_iter()
.map(|edge| {
let PathEdge::ForwardEdge(delegation) = edge else {
panic!("Found reverse edge {edge:?} on resolved path. This is a bug!")
};
delegation.inner
})
.collect(),
certification: path.certification.inner,
}, path.trust_amount)
})
.collect();
Ok(Paths::new_with_paths(wot_network_paths))
}
/// Performs a single step of the [Ford-Fulkerson] derivative algorithm.
///
/// See the "Flow Computation" chapter to get a high-level overview of what this does.
pub(crate) fn step(&mut self) -> Option<Path> {
// Perform a backwards BFS lookup on the internal residual network.
let trust_anchor_id = self.search_path()?;
// Assemble the path and adjust the residual network.
let path = match self.finalize_path(trust_anchor_id) {
Ok(path) => path,
Err(message) => {
error!("Dumping network:\n{self:#?}");
panic!("{message}");
}
};
// Reset all path search related variables on the whole graph.
self.reset_search_data();
// We found a path, increment id for the next path.
self.current_path_id += 1;
Some(path)
}
/// Searches for a path in the residual trust network.
///
/// Performs a backwards BFS from the target [`Binding`] towards the [`TrustAnchor`]s.
///
/// For more information see the "Path Finding" chapter.
fn search_path(&mut self) -> Option<Certificate> {
debug!("===== Starting path search =====");
// Queue holds (certificate, path_length, current_amount, previous_cert)
let mut queue: PriorityQueue<Certificate, NodePriority> = PriorityQueue::new();
// Get a handle to the target binding identity.
// This is necessary for delegations with regex restrictions against the target binding.
let target_id = &self.target_node.binding.identity;
// Populate the discovery queue with the certifications from the TargetNode.
for (edge_id, certification) in self.target_node.certifications.iter().enumerate() {
trace!(
"Checking certification {} → ({}, {})",
certification.inner.issuer(),
certification.inner.target_cert(),
certification.inner.target_id(),
);
// Skip the certification if no trust is left.
if certification.available_amount() == 0 {
trace!("→ Skip certification no trust is left.");
continue;
}
let Some(issuer) = self.nodes.get_mut(certification.inner.issuer()) else {
error!(
"→ Cannot find certification issuer node: {}",
certification.inner.issuer()
);
continue;
};
// Continue early if the node has no trust left.
if issuer.available_amount() == 0 {
trace!("→ Skip delegation as node has no available trust.");
continue;
}
// Calculate next trust amount of the issuer node.
let issuer_trust_amount = self
.target_node
.available_amount()
.min(certification.available_amount());
// Create a set of visited nodes to prevent cycling.
let mut visited_nodes = HashSet::new();
visited_nodes.insert(issuer.id.clone());
issuer.path_info = EdgeSearchInfo::Some {
current_amount: issuer_trust_amount,
target: self.target_node.binding.cert.clone(),
used_edge: UsedEdge::Certification(edge_id),
path_length: 1,
visited_nodes,
};
// At this point, it's possible that we already immediately hit the trust anchor!
// Check if the certification issuer is in the set of trust anchors and whether that
// anchor still has some trust left to give.
if let Some(anchor) = self.trust_anchors.get(certification.inner.issuer())
&& anchor.available_amount() > 0
{
trace!(
"SUCCESS - found trust anchor {}",
certification.inner.issuer()
);
return Some(certification.inner.issuer().clone());
}
queue.push(issuer.id.clone(), NodePriority::new(1, issuer_trust_amount));
}
trace!("Queue after handling certifications: {queue:?}");
// While searching for a path in the network, we don't just return on the first anchor we
// find, but rather look for all anchors with the same (or smaller) path length.
//
// We then pick the one with the highest trust amount first.
let mut found_path_length: Option<usize> = None;
// List of found anchors with the structure of `(trust_anchor_id, trust_amount)`.
let mut found_anchors: Vec<(Certificate, u8)> = Vec::new();
// This is where the real BFS starts.
//
// Work through the discovery queue one node at a time:
// - Check if the Node is a TrustAnchor. If so, we can stop, as we found a valid path
// towards a TrustAnchor.
// - For every delegation on the current node:
// - Check if the current path length is shorter or equal than the allowed trust depth
// on the delegation.
// - If there are any regexes on the delegation, make sure that they match the target
// binding's `target_id`.
// - For every other Node that is then reached by a valid delegation:
// - The `used_amount` must not be touched during the search process!
// - Ensure that the Node has some trust amount left.
// - Make sure that there isn't already a previous_node on the Node with a shorter
// `path_length`. If so, ignore this delegation.
// - If there's already a previous_node with an equal `path_length`, but a lower
// trust amount, this delegation overrules any other previous delegations.
// - If the Delegation and Node are valid, adjust the `previous_node`. The
// `current_amount` is the minimum((allowed_amount - used_amount),
// delegation.trust_amount, previous_node.current_amount).
// - Put the discovered Node on top at the end of the discovery queue.
while let Some((node_id, priority)) = queue.pop() {
trace!("Current: {node_id}, Priority: {priority:?}");
let Some(target) = self.nodes.get(&node_id).cloned() else {
error!("Found a previously registered Node that's now missing: {node_id}");
continue;
};
// Get the current path length.
let EdgeSearchInfo::Some {
path_length: target_path_length,
current_amount: target_current_amount,
visited_nodes: target_visited_nodes,
used_edge: target_used_edge,
..
} = target.path_info
else {
panic!(
"Encountered path in queue without EdgeSearchInfo. This is a bug! Path: {target:#?}"
);
};
// This is one of the exit conditions for finding a path.
// As soon as we find any trust anchor, we continue to visit all nodes with that same
// path length, so that we find the best path for that length.
//
// We stop exploration, as soon as we visit any node with a longer path.
if let Some(found_path_length) = found_path_length
&& target_path_length > found_path_length
{
break;
}
// Check if the node is a TrustAnchor with remaining trust.
//
// If so, mark the anchor as one of found anchors for this path length.
// We will pick (one of) the best found anchors, once all nodes for this path
// length are explored.
//
// Path creation and adjustments of the residual network are done outside of this
// function
//
// If this is not the case, the node is a "normal" node and we need to further explore
// the graph.
if let Some(anchor) = self.trust_anchors.get(&node_id)
&& anchor.available_amount() > 0
{
trace!("SUCCESS - found trust anchor {node_id}",);
if found_path_length.is_none() {
found_path_length = Some(target_path_length);
found_anchors.push((
target.id.clone(),
target_current_amount.min(anchor.available_amount()),
));
}
continue;
} else if !found_anchors.is_empty() {
// If we already found an anchor for this path length, we no longer have to explore
// any further along the graph. We simply look at the remaining nodes for this
// length and stop afterwards, picking (one of) the best found anchors.
continue;
}
// Start discovery of new nodes via the target's incoming delegations.
for (edge_id, delegation) in target.delegations.iter().enumerate() {
trace!(
"Checking delegation {} → {}",
delegation.issuer(),
delegation.target()
);
// Only follow the delegation, if there's any trust amount left in the first place.
if delegation.available_amount() == 0 {
trace!("→ Skip delegation with trust 0");
continue;
}
// Path depth check for the upholding of trust_depth restrictions.
//
// The delegation.trust_depth must be bigger or equal than the path length from the
// current node towards the target binding. If we wouldn't do this, we would allow a
// longer path than is actually permitted by this delegation.
if delegation.trust_depth() < target_path_length.min(u8::MAX as usize) as u8 {
trace!(
"→ Skip delegation as trust_depth ({}) < path_length ({})",
delegation.trust_depth(),
target_path_length
);
continue;
}
// Some delegations may restrict their forwarded trust towards the target binding
// via a regular expression.
//
// Ensure that the target binding identifier satisfies all delegation regexes.
if !delegation.regexes().is_empty()
&& !delegation
.regexes()
.iter()
.any(|regex| regex.matches(target_id))
{
trace!("→ Skip delegation, no matching regex for target binding");
continue;
}
// Get the issuer of the delegation
let Some(issuer) = self.nodes.get_mut(delegation.issuer()) else {
error!(
"→ Found delegation towards '{}' from non-existent node '{}'. Ignoring this delegation!",
delegation.target(),
delegation.issuer()
);
continue;
};
// Calculate the new trust amount that would be set on the `issuer`, if we would
// follow this edge.
//
// This is the minimum value of:
// - `target_current_amount`: the max trust amount that can be pushed along the
// current path up and including the target.
// - `delegation.available_amount()` The remaining trust amount that can be pushed
// through this specific delegation.
// - `issuer.available_amount()` The remaining available trust amount that can be
// pushed through `issuer`.
let issuer_trust_amount = target_current_amount
.min(delegation.available_amount())
.min(issuer.available_amount());
// Don't add the node, if we don't have any flow.
if issuer_trust_amount == 0 {
continue;
}
// Check if the issuer node is already enqueued.
// If so, we have some guarantees due to the fact that we use a BFS:
//
// - All elements that are currently in the queue have either **the same path
// length** or **a bigger path length** than `target`.
// - It follows from that, that no node with a longer path than `target` has reached
// the front of the queue.
//
// This guarantee allows us to simply modify `issuer` **in case** we manage to
// find a better path to it, without having to worry about any queue positioning.
let already_enqueued = issuer.path_info.is_some();
// Right now, we know that we are allowed to follow this delegation.
// Now, we need to check whether we already reached the issuer via a different
// path. If so, we may only proceed if the current path is shorter than the
// previously discovered path to `issuer`.
if let EdgeSearchInfo::Some {
current_amount: issuer_current_amount,
path_length: issuer_path_length,
..
} = &issuer.path_info
{
trace!("→ Node has already been visited",);
// The previously detected path is shorter. Ignore this delegation.
if *issuer_path_length < target_path_length + 1 {
trace!("→ Skipping - shorter path to {} exists", issuer.id);
continue;
}
trace!(
"→ Node would have new trust amount of {issuer_trust_amount} (old {issuer_current_amount})",
);
// If the paths have the same length, make sure the current trust amount is
// bigger than that of the other path. Otherwise, ignore this delegation.
//
// In case of an equal trust amount, we simply use the existing path and ignore
// this delegation.
if *issuer_path_length == target_path_length + 1
&& issuer_current_amount >= &issuer_trust_amount
{
trace!(
"→ Skipping - {} already has a path with equal length and better trust amount {} vs {}",
issuer.id, issuer_current_amount, issuer_trust_amount,
);
continue;
}
if *issuer_path_length > target_path_length + 1 {
trace!("→ SUCCESS - found a shorter path to {}", issuer.id);
} else {
trace!(
"→ SUCCESS - found a path with to {} with more trust {issuer_trust_amount} (old {issuer_current_amount})",
issuer.id
);
}
} else {
trace!("→ SUCCESS - found a new path to {}", issuer.id);
}
let mut visited_nodes = target_visited_nodes.clone();
visited_nodes.insert(issuer.id.clone());
let path_length = target_path_length + 1;
// At this point, we have determined that current path is either shorter or has a
// higher trust value and should therefore be used to reach `issuer`.
// Update the path info accordingly
issuer.path_info = EdgeSearchInfo::Some {
current_amount: issuer_trust_amount,
target: node_id.clone(),
used_edge: UsedEdge::Delegation(edge_id),
path_length,
visited_nodes,
};
let priority = NodePriority::new(path_length, issuer_trust_amount);
if !already_enqueued {
trace!(
"→ Pushing {} onto queue with priority: {}",
issuer.id, &priority
);
} else {
trace!(
"→ Update {} in queue with priority: {}",
issuer.id, &priority
);
}
// Enqueue/Update the issuer node onto the list.
queue.push(issuer.id.clone(), priority);
}
// Check if we used a reverse edge to get to the current node and if so, to which path
// it belongs to.
// If we traverse any successive reverse edges and there are multiple reverse edges to
// one node, we must favor the one which belongs to the same path.
// Read the `001_Path_Finding/002_Traversing_Reverse_Edges.md` for more information.
let previous_reverse_edge_path_id = match target_used_edge {
UsedEdge::Certification(_) => None,
UsedEdge::Delegation(_) => None,
UsedEdge::ReverseEdge { path_id, .. } => Some(path_id),
};
// Save if a successive reverse edge for a previous reverse edge has been
// found.
// Read the `001_Path_Finding/002_Traversing_Reverse_Edges.md` for more information.
let mut successive_reverse_node = None;
// Start discovery of new nodes via the target's incoming reverse_edges.
//
// # Note
//
// In contrast to delegations, we **do not** have to check regexes on reverse edges:
// By using a reverse edge, practically cancel out the usage of the associated
// forward edge. I.e. we remove some usage from the paths.
// Since we effectively remove/reroute usage, there are no constraints that need to be
// checked.
for (edge_id, reverse_edge) in target.reverse_edges.iter().enumerate() {
// A successive reverse edge has been found towards this reverse edge's issuer.
// That means that we can not allow any other reverse edges to overrule that path.
if let Some(node) = &successive_reverse_node
&& node == &reverse_edge.issuer
{
continue;
}
// Make sure that we don't use any reverse edges to nodes that were already visited
// along the current path! This would result in cycles and is an illegal operation.
//
// See chapter Cycle_Detection for more information.
if target_visited_nodes.contains(&reverse_edge.issuer) {
continue;
}
trace!(
"Checking reverse edge {} → {}",
reverse_edge.issuer, reverse_edge.target
);
// Only follow the reverse edge, if there's any trust amount left in the first
// place.
if reverse_edge.trust_amount == 0 {
trace!("→ Skipping - trust amount of edge is 0");
continue;
}
// Path depth check for the upholding of trust_depth restrictions.
//
// The trust_depth must be bigger or equal than the path length from the
// current node towards the target binding. If we wouldn't do this, we would allow a
// longer path than is actually permitted by this reverse edge.
if reverse_edge.trust_depth < target_path_length.min(u8::MAX as usize) as u8 {
trace!(
"→ Skipping - trust_depth ({}) < path_length ({})",
reverse_edge.trust_depth, target_path_length
);
continue;
}
// Get the issuer node.
let Some(issuer) = self.nodes.get_mut(&reverse_edge.issuer) else {
error!(
"Found reverse edge towards '{}' from non-existent node '{}'. Ignoring this reverse edge!",
target.id, reverse_edge.issuer
);
continue;
};
// Calculate the new trust amount that would be set on the `issuer`, if we would
// follow this edge.
//
// This is the minimum value of:
// - `target_current_amount`: the max trust amount that can be pushed along the
// current path up and including the target.
// - `delegation.trust_amount` The max trust_amount that can be pushed through this
// specific reverse edge.
// - `issuer.available_amount()` The remaining available trust amount that can be
// pushed through `issuer`.
let new_trust_amount = target_current_amount
.min(reverse_edge.trust_amount)
.min(issuer.available_amount());
// Check if this reverse edge is a successive reverse edge on the same path.
let is_successive = if let Some(prev_path_id) = previous_reverse_edge_path_id
&& reverse_edge.path_id == prev_path_id
{
true
} else {
false
};
// Right now, we know that we are allowed to follow this reverse edge.
//
// Next we need to check whether we already reached the issuer node via a different
// path. If so, we may only proceed if the new path is shorter than the
// previously discovered path to `issuer`.
//
// Since we're traversing a reverse edge, this process involves checking whether the
// `remaining_path_length` after rerouting the trust along the reverse edge would be
// shorter than the current path length on the node.
//
// See the documentation chapter "Traversing Reverse Edges" for more info.
if let EdgeSearchInfo::Some {
current_amount,
path_length,
// This `previous_issuer` info is used to prefer traversal along multiple
// successive reverse edges that belong to the same path, in case multiple
// forward/reverse edges point towards the same node.
target: previous_of_issuer,
used_edge,
..
} = &issuer.path_info
{
trace!("→ Node has already been visited",);
// Perform the length check
if *path_length < reverse_edge.remaining_path_length {
trace!("→ Skipping - shorter path to {} exists", issuer.id);
continue;
}
trace!(
"→ Node would have new trust amount of {new_trust_amount} (old {current_amount})",
);
// Make sure we prefer successive reverse edges towards the target node.
//
// I.e. if another reverse edge from `target` already visited the `issuer`
// node, we must overrule that route, even if it's a similar or even better
// match.
if is_successive
&& *previous_of_issuer == target.id
&& used_edge.is_reverse_edge()
{
successive_reverse_node = Some(reverse_edge.issuer.clone());
trace!("→ SUCCESS - found a successive reverse edge to the target node");
}
// If the paths have the same length, make sure the current trust amount is
// bigger than that of the other path. Otherwise, ignore this delegation.
//
// In case of an equal trust amount, we simply use the existing path and ignore
// this reverse edge.
else if *path_length == reverse_edge.remaining_path_length
&& *current_amount >= new_trust_amount
{
trace!(
"→ Skipping - {} already has a path with same length and better or equal trust amount ({} > {})",
issuer.id, current_amount, new_trust_amount,
);
continue;
} else if reverse_edge.remaining_path_length < *path_length {
trace!(
"→ SUCCESS - found a shorter path to {} ({} < {})",
issuer.id, reverse_edge.remaining_path_length, path_length,
);
} else {
trace!(
"→ SUCCESS - found a path with more trust ({new_trust_amount} > {current_amount}) to {}",
issuer.id
);
}
} else {
trace!("→ SUCCESS - found a new path to the target node");
}
let mut visited_nodes = target_visited_nodes.clone();
visited_nodes.insert(issuer.id.clone());
// At this point, we have determined that current path can and should be extended
// towards issuer. Update the path info accordingly.
issuer.path_info = EdgeSearchInfo::Some {
current_amount: new_trust_amount,
target: node_id.clone(),
used_edge: UsedEdge::ReverseEdge {
index: edge_id,
path_id: self.current_path_id,
},
path_length: reverse_edge.remaining_path_length,
visited_nodes,
};
let priority =
NodePriority::new(reverse_edge.remaining_path_length, new_trust_amount);
if !queue.contains(&issuer.id) {
trace!(
"→ Pushing {} onto queue with priority: {priority}",
issuer.id
);
} else {
trace!(
"→ Updating {} in queue with priority: {priority}",
issuer.id
);
}
// Enqueue/Update the issuer node onto the list.
queue.push(issuer.id.clone(), priority);
}
}
// Exit early with `None` if we haven't found any path.
let _found_path_length = found_path_length?;
// We expect at least one anchor to be there.
let mut anchors_iter = found_anchors.into_iter();
let mut best_anchor = anchors_iter.next().unwrap();
// Determine (one of) the best anchors.
for anchor in anchors_iter {
if anchor.1 > best_anchor.1 {
best_anchor = anchor
}
}
Some(best_anchor.0)
}
/// Called by [`Self::step`] whenever a path has been detected.
///
/// At this point, the internal network is in a state where we can follow
/// [`Node::path_info`]'s [`EdgeSearchInfo::Some::target`] fields from the node with the given
/// `trust_anchor_id` until we reach the target binding. At this point, the path is embedded
/// in the graph and must be converted into a usable form. While traversing the path, this
/// function also takes care of adjusting the residual network.
///
/// In detail, this means that we perform the (roughly summarized) following steps:
/// - Traverse the graph by hopping along the [`Node::path_info`]
/// [target](`EdgeSearchInfo::Some::target`)s' [edge](`EdgeSearchInfo::Some::used_edge`)s on
/// each node
/// - Build a path representation from those nodes.
/// - While traversing the path:
/// - For every forward edge that has been used:
/// - Subtract the used trust amount on the forward edges.
/// - Introduce a reverse edge.
/// - Consult the Chapter "Creation of Reverse Edges" for more details.
/// - If reverse edges have been used:
/// - Subtract the used trust amount on the reverse edges.
/// - Add the used trust amount on the reverse edge back to the **associated** forward edge!
/// Consult the chapter "Traversing Reverse Edges" for more info.
fn finalize_path(&mut self, trust_anchor_id: Certificate) -> Result<Path, String> {
debug!("===== Assembling path and adjusting residual network =====");
// # `Path` related variables
//
// A full `Path` can only be created once all of its components are in place.
// Since this includes the certificate to the target binding, we can only create it at the
// very end.
//
// - `trust_anchor_id`: We already know this, as it's passed in as a parameter. It's the
// starting point of the path.
// - `edges`: A vector of `PathEdge` that is filled with delegations while building the
// path. It contains all edges from the trust anchor up to the certificate that holds the
// certification of the target binding.
// - `path_trust_amount`: The trust amount for the full length of this path. This is
// equivalent to the `current_amount` on the target anchor node. It's the minimum value of
// remaining trust amount of both nodes or edges along the path. This value is very
// important, as this is the value that's used to adjust remaining flow on edges, nodes
// and the value used when creating reverse edges.
//
// # Loop related runtime variables
//
// - `target_id` This is the reference to the next target node along in the path. As we loop
// and advance through the path, the target will become the issuer in the next iteration.
// - `used_edge`: This is a reference to the edge that's used to get from the current
// (`issuer`) node to the target node (`target_id`). Since our path finding is backwards,
// the edges are counterintuitively saved on the target of an edge instead of the source.
let (path_trust_amount, mut target_id, mut used_edge) = {
// Get the trust anchor node.
let Some(anchor_node) = self.nodes.get_mut(&trust_anchor_id) else {
return Err(format!(
"Missing trust anchor node '{trust_anchor_id}' after path has been found! This is a bug.",
));
};
let EdgeSearchInfo::Some {
current_amount: path_trust_amount,
target,
used_edge,
..
} = anchor_node.path_info.clone()
else {
panic!("Missing path info on trust anchor after successful BFS: {anchor_node:#?}");
};
// Get the actual trust anchor info.
// Nodes may act as both, nodes **and** trust anchors. Both roles may have individual
// trust amount restrictions. So we need to consider both, the trust amount
// node, but also on the trust anchor meta information itself.
let Some(anchor) = self.trust_anchors.get_mut(&trust_anchor_id) else {
return Err(format!(
"Missing trust anchor '{trust_anchor_id}' after path has been found! This is a bug.",
));
};
// The final limiting factor on the trust amount is the remaining trust on the anchor.
let path_trust_amount = path_trust_amount.min(anchor.available_amount());
// Adjust the used amounts of the anchor node.
let Some(new_amount) = anchor_node.used_amount.checked_add(path_trust_amount) else {
return Err(format!(
"Total amount of used trust ({}) and found trust ({}) on TA node {} in path exceed u8 bound! This is a bug.",
anchor_node.used_amount, path_trust_amount, anchor_node.id
));
};
anchor_node.used_amount = new_amount;
// Adjust the used amounts of the actual anchor.
let Some(new_anchor_amount) = anchor.used_amount.checked_add(path_trust_amount) else {
return Err(format!(
"Total amount of used trust ({}) and found trust ({}) on TA {} in path exceed u8 bound! This is a bug.",
anchor.used_amount, path_trust_amount, trust_anchor_id
));
};
anchor.used_amount = new_anchor_amount;
(path_trust_amount, target, used_edge)
};
let mut edges = Vec::new();
trace!("→ Start path walk");
// Now that we handled the initial logic, we walk along the path until we hit the final
// node.
//
// This process is a bit weird, as we always have to perform steps somewhat backward, as
// our BFS operates backward and our edges are thereby also saved "backwards" (on the
// target).
//
// - Get the next node in the path, which we call `issuer`, as it's the issuer for the next
// edge to take.
// - Put the edge used to get to this `issuer` into the `edges` list.
// - Adjust the used trust amount of both the `issuer` as well as the edge's value
// - Get the next edge and the id of the next edge's target node.
let certification_index = loop {
// At first, check if we hit an exit condition.
// This means that we reached our target binding id, while also following a
// certification.
if target_id == self.target_node.binding.cert
&& let UsedEdge::Certification(index) = used_edge
{
break index;
}
let Some(issuer) = self.nodes.get_mut(&target_id) else {
return Err(format!(
"Missing Node '{trust_anchor_id}' along path! This is a bug.",
));
};
// Adjust the used amounts of the node
let Some(new_amount) = issuer.used_amount.checked_add(path_trust_amount) else {
return Err(format!(
"Total amount of used trust ({}) and found trust ({}) on node {} in path exceed u8 bound! This is a bug.",
issuer.used_amount, path_trust_amount, issuer.id,
));
};
trace!(
"→ Adjusting used trust amount of {}: {} -> {} (of {})",
issuer.id, issuer.used_amount, new_amount, issuer.allowed_amount
);
issuer.used_amount = new_amount;
// Push a copy of the used edge to the path, while adjusting the edges on
// the original edges to shape the residual graph.
match &used_edge {
UsedEdge::Delegation(index) => {
let Some(delegation) = issuer.delegations.get_mut(*index) else {
return Err(format!(
"Found invalid delegation index {index} on node {}. This is a bug.",
issuer.id
));
};
// Consume the used trust amount from the delegation.
let Some(new_amount) = delegation.used_amount.checked_add(path_trust_amount)
else {
return Err(format!(
"Total amount of used trust ({}) and found trust ({}) on delegation exceeds u8 bound! This is a bug.",
path_trust_amount, delegation.used_amount,
));
};
trace!(
"→ Adjust used trust amount of delegation: {} -> {}",
delegation.issuer(),
delegation.target()
);
trace!(
" ↳ Trust amount changed from: {} -> {} (of {})",
delegation.used_amount,
new_amount,
delegation.trust_amount()
);
delegation.used_amount = new_amount;
edges.push(PathEdge::ForwardEdge(delegation.clone()))
}
UsedEdge::ReverseEdge { index, path_id: _ } => {
let Some(reverse_edge) = issuer.reverse_edges.get_mut(*index) else {
return Err(format!(
"Found invalid reverse edge index {index} on node {}. This is a bug.",
issuer.id
));
};
// Subtract the used trust amount from the edge.
let Some(new_amount) = reverse_edge.trust_amount.checked_sub(path_trust_amount)
else {
return Err(format!(
"Total amount of used trust ({}) in reverse edge exceeds available trust ({})! This is a bug.\nReverse edge: {} -> {}",
path_trust_amount,
reverse_edge.trust_amount,
reverse_edge.issuer,
reverse_edge.target
));
};
trace!(
"→ Adjust used trust amount of reverse edge: {} -> {}",
reverse_edge.issuer, reverse_edge.target
);
trace!(
" ↳ Trust amount changed from: {} -> {}",
reverse_edge.trust_amount, new_amount,
);
reverse_edge.trust_amount = new_amount;
// Push the path
edges.push(PathEdge::ReverseEdge(reverse_edge.clone()))
}
UsedEdge::Certification(index) => {
return Err(format!(
"Tried to get certification with index {index} on intermediate node. This is a bug."
));
}
}
// Make sure we have a previous node.
let EdgeSearchInfo::Some {
target: next_target_id,
used_edge: next_used_edge,
..
} = &issuer.path_info
else {
panic!("Node in path doesn't have a `path_info`! This is a bug: {issuer:#?}");
};
// Set the next node to look at with the used edge to get there.
target_id = next_target_id.clone();
used_edge = *next_used_edge;
};
// At this point, we hit the target binding and can now build the path.
let Some(certification) = self.target_node.certifications.get_mut(certification_index)
else {
return Err(format!(
"Found invalid certification index {certification_index} on target binding node. This is a bug.",
));
};
trace!(
"→ Reached certification edge {} -> {} ({})",
certification.inner.issuer(),
certification.inner.target_cert(),
certification.inner.target_id(),
);
// As a last step, also consume the used trust amount on the certification.
let Some(new_amount) = certification.used_amount.checked_add(path_trust_amount) else {
return Err(format!(
"Total amount of used trust ({}) and found trust ({}) on certification exceeds u8 bound! This is a bug.",
path_trust_amount, certification.used_amount,
));
};
certification.used_amount = new_amount;
let path = Path {
trust_amount: path_trust_amount,
trust_anchor: trust_anchor_id,
edges,
certification: certification.clone(),
};
let path_length = path.len();
// While traversing the path, we must be able to know the allowed delegation length at
// each step. The reason for this is that we need to know this value on [`ReverseEdges`]
// so that we may simulate rerouting the trust flow along another node.
//
// To calculate this value, we need to compute the delegation length based on the
// allowed depth values on each delegation and the length of the path.
//
// Consider the following flow. The letters are nodes are the numbers on the arrows are
// the trust depth.
//
// ```
// A -5-> B -8-> C -2-> D
// ```
//
// - A allows a depth of 5.
// - When reaching `B`, `B` it effectively "inherits" a depth of 4.
// - The `min(4, 8)` is used, so `B` delegates with a depth of 4 towards `C`.
// - Now `C` inherits the depth of `3`.
// - The `min(3, 2)` is used, so `C` delegates with a depth of 2 towards `D`.
let mut allowed_delegation_length = TrustDepth::Unlimited;
trace!("→ Perform reverse edge creation and reverse-forward graph adjustments.");
for (current_length, edge) in path.edges.iter().enumerate() {
match edge {
// As a forward edge (delegation) has been used, we now need to create a respective
// reverse edge.
PathEdge::ForwardEdge(delegation) => {
let Some(node) = self.nodes.get_mut(delegation.issuer()) else {
return Err(format!(
"Missing Node '{}' while creating reverse edges! This is a bug.",
delegation.issuer()
));
};
let reverse_edge = ReverseEdge {
issuer: delegation.target().clone(),
target: delegation.issuer().clone(),
trust_amount: path_trust_amount,
trust_depth: allowed_delegation_length,
remaining_path_length: path_length - (current_length + 1),
path_id: self.current_path_id,
};
trace!("→ Created new reverse edge: {reverse_edge:?}.");
node.reverse_edges.push(reverse_edge);
// Calculate the new trust depth.
allowed_delegation_length = allowed_delegation_length
.minus(1)
.min(delegation.trust_depth());
}
// For every usage of a reverse edge, we need to adjust the respective forward edge
// as well. I.e. We practically "rerouted" some trust flow along a
// different path, which means that we freed up some trust along the
// forward edge.
PathEdge::ReverseEdge(reverse_edge) => {
let Some(node) = self.nodes.get_mut(&reverse_edge.issuer) else {
return Err(format!(
"Missing Node '{}' while adjusting reverse-forward edges! This is a bug.",
reverse_edge.issuer
));
};
// As we just traversed a reverse edge, we no longer work with the
// allowed_delegation_length up until this point, as this is now overwritten by
// the reverse_edge's saved value.
allowed_delegation_length = reverse_edge.trust_depth;
// TODO: We currently don't handle the case that multiple delegations between
// the same edges exist!
// The current implementation could result in undefined behavior in case:
// - Two delegations between A and B exist
// - The delegations have different trust levels and/or different trust_amounts.
// - One of the delegation edges is used.
// - The resulting reverse edge is used as well.
//
// -> We now need to add the used trust amount on the reverse edge back to the
// **original** edge that created this reverse edge. There's however currently
// no way to determine which forward edge has been used, so
// any of the two could be picked.
//
// For example, if the forward edge with the higher trust depth is selected
// instead of the original edge with a lower trust depth,
// trust could be consumed from a more permissive edge, potentially resulting in
// a lower overall trust discovered in the network.
//
// This case is probably very esoteric though.
//
// To properly handle this, we need to do either of these two things:
// - Prune any duplicate edges (not 100% correct, but this is somewhat of an
// esoteric edge-case)
// - Save the index of the forward edge on the reverse edge. That way, we can
// reference specific delegations when introducing reverse edges. That allows
// us to differentiate which of the two edges was used for that path, given
// the case that the reverse edge is being used and the original forward edge
// needs to have its `used_amount` adjusted.
//
// **UPDATE:** This could theoretically already be easily fixable by the
// introduction of `UsedEdge`.
// Find a delegation that matches the reverse_edge.
let Some(delegation) = node.delegations.iter_mut().find(|delegation| {
delegation.issuer() == &reverse_edge.target
&& delegation.target() == &reverse_edge.issuer
}) else {
return Err(format!(
"Couldn't find matching delegation for reverse edge with issuer {} and target {}! This is a bug.",
reverse_edge.issuer, reverse_edge.target,
));
};
trace!(
"→ Adjust delegation whose reverse edge was used: {} -> {}",
delegation.issuer(),
delegation.target()
);
// Add the freed up trust amount back to the delegation.
let Some(new_amount) = delegation.used_amount.checked_sub(path_trust_amount)
else {
return Err(format!(
"Total amount of used trust on delegation ({}) and restored trust ({}) is smaller than 0! This is a bug.",
delegation.trust_amount(),
path_trust_amount,
));
};
trace!(
" ↳ Used trust amount changed from: {} -> {} (of {})",
delegation.used_amount,
new_amount,
delegation.trust_amount()
);
delegation.used_amount = new_amount;
}
}
}
Ok(path)
}
/// Resolves all reverse edges introduced during the max-flow detection algorithm.
///
/// All reverse edge path segments have a corresponding forward path segment in a different
/// path. In the [`Self::finalize_path`] function, where the residual network is adjusted
/// and reverse edges are created based on a chosen path, an extra important step is
/// performed:
///
/// The path id (index) that is responsible for creating a reverse edge is **saved** on
/// each reverse edges.
///
/// When another path now uses that reverse edge, we know for which exact path we simulate
/// "rerouting" trust.
///
/// This enables us to directly consolidate reverse edges with perfectly matching paths,
/// which allows to **skip any trust depth checks**!
///
/// Takes an optional `writer` string to which debug info is added/printed.
fn consolidate_paths<W: Write>(
mut paths: Vec<Path>,
mut writer: Option<&mut W>,
) -> std::io::Result<Vec<Path>> {
#[derive(PartialEq, Debug)]
enum Segment {
None,
Some {
// The associated path
path_id: usize,
// The indices of the segment
segment_indices: Vec<usize>,
},
}
if let Some(ref mut writer) = writer {
writeln!(writer, "\n\n# Path Consolidation\n```")?;
}
// Create a list of all paths that need to be checked.
// We will manipulate the paths inside the original path vector.
let mut path_ids_to_check: Vec<usize> = (0..paths.iter().len()).collect();
while let Some(path_id) = path_ids_to_check.pop() {
// Direct access as path ids must reference valid array indices.
let mut path = paths[path_id].clone();
if let Some(ref mut writer) = writer {
write!(writer, "\nChecking Path: {}\n", path.simple_display())?;
}
// Save the indices of the reverse edges in a vector.
let mut segment = Segment::None;
for (index, edge) in path.edges.iter().enumerate() {
if let Some(edge) = edge.reverse() {
// To keep things simple, we only look at chains of reverse edges that belong to
// the same path. This means that if there are multiple reverse edges in a
// single path, the path will be checked multiple times.
match &mut segment {
Segment::None => {
// We encountered the first reverse edge.
// Lock the path id in.
segment = Segment::Some {
path_id: edge.path_id,
segment_indices: vec![index],
}
}
Segment::Some {
segment_indices, ..
} => {
segment_indices.push(index);
}
}
}
}
// Only continue if we found a segment.
let Segment::Some {
path_id: forward_path_id,
segment_indices,
} = segment
else {
continue;
};
if let Some(ref mut writer) = writer {
writeln!(writer, "→ Found segments with indices: {segment_indices:?}")?;
}
// We now have a segment of reverse edge(s) that belong to the same path, and we also
// know which exact path we need to consolidate with.
// Now we just need to figure out the corresponding segment in the forward path.
// The last segment of the reverse path will be the first segment of the forward path.
let first_reverse_index = segment_indices.first().unwrap();
let last_reverse_index = segment_indices.last().unwrap();
let last_reverse_edge = path
.edges
.get(*last_reverse_index)
.expect("We know this index exists")
.reverse()
.expect("We know this is a reverse edge");
// Now we need to find a forward edge that matches the reverse edge.
let mut forward_path = paths[forward_path_id].clone();
if let Some(ref mut writer) = writer {
writeln!(
writer,
"→ Looking for matching segment in associated path: {}",
forward_path.simple_display()
)?;
}
let mut first_forward_index = None;
for (index, edge) in forward_path.edges.iter().enumerate() {
let PathEdge::ForwardEdge(forward_edge) = edge else {
continue;
};
// The target/issuer don't match, so skip it.
if forward_edge.issuer() != &last_reverse_edge.target
|| forward_edge.target() != &last_reverse_edge.issuer
{
continue;
}
first_forward_index = Some(index);
}
let Some(first_forward_index) = first_forward_index else {
error!(
"Couldn't find matching forward edge for reverse edge segment {segment_indices:?} of path {path:?}"
);
error!("Associated forward path is {forward_path:?}");
panic!("Encountered critical error in path consolidation");
};
if let Some(ref mut writer) = writer {
writeln!(
writer,
"→ Found matching segment at index {first_forward_index}",
)?;
}
// Calculate the last segment index.
let last_forward_index = first_forward_index + segment_indices.len() - 1;
// Before we actually consolidate the paths, we need to handle the case that the
// reverse edge doesn't consume all of the available amount that's provided by the
// forward edge.
//
// This is effectively a partial reroute of the forward path.
// To handle this, we make a copy of the forward path with the remaining trust amount.
// The original path will be updated to the amount of the reverse path.
if forward_path.trust_amount > path.trust_amount {
let mut forward_path_clone = forward_path.clone();
forward_path_clone.trust_amount = forward_path.trust_amount - path.trust_amount;
if let Some(ref mut writer) = writer {
writeln!(
writer,
"→ Trust of forward path ({}) exceeds reverse path ({})",
forward_path.trust_amount, path.trust_amount
)?;
writeln!(
writer,
"→ Pushing copy of forward path with remaining trust {}",
forward_path_clone.trust_amount
)?;
}
// Push the cloned paths to be checked.
paths.push(forward_path_clone);
path_ids_to_check.push(paths.len() - 1);
// Update the forward path to match the reverse path.
forward_path.trust_amount = path.trust_amount;
}
// Remove the segments and swap tails between path and forward_path
//
// This is effectively this operation:
// path[0..first_reverse_index] + forward_path[last_forward_index..]
// forward_path[0..first_forward_index] + path[last_reverse_index..]
//
// We create a clone of the tails before mutating anything for better visualization and
// to make sure that we can safely swap the certifications as well.
let mut path_tail = path.clone();
path_tail.edges = path_tail.edges[last_reverse_index + 1..].to_vec();
let mut forward_path_tail = forward_path.clone();
forward_path_tail.edges = forward_path_tail.edges[last_forward_index + 1..].to_vec();
// Truncate to the start of the segment
path.edges.truncate(*first_reverse_index);
forward_path.edges.truncate(first_forward_index);
if let Some(ref mut writer) = writer {
writeln!(
writer,
"→ Splicing start of reverse path\n {}\n with end of forward path\n {}",
path.simple_display_without_target(),
forward_path_tail.simple_display_without_anchor(),
)?;
writeln!(
writer,
"→ Splicing start of forward path\n {}\n with end of reverse path\n {}",
forward_path.simple_display_without_target(),
path_tail.simple_display_without_anchor(),
)?;
}
// Append the swapped tails and overwrite certifications.
path.edges.extend(forward_path_tail.edges);
path.certification = forward_path_tail.certification;
forward_path.edges.extend(path_tail.edges);
forward_path.certification = path_tail.certification;
if let Some(ref mut writer) = writer {
writeln!(
writer,
"→ Resulting reverse path: {}",
path.simple_display()
)?;
writeln!(
writer,
"→ Resulting forward path: {}",
forward_path.simple_display()
)?;
}
paths[path_id] = path;
paths[forward_path_id] = forward_path;
// Both path might now contain reverse edges, due to the splicing, so enqueue them both
// for checking.
path_ids_to_check.push(path_id);
path_ids_to_check.push(forward_path_id);
}
if let Some(ref mut writer) = writer {
write!(writer, "\n```")?;
}
Ok(paths)
}
/// Resets the graph's path finding algorithm related variables.
/// This is called at the end of each [`Self::step`].
fn reset_search_data(&mut self) {
for (_, node) in self.nodes.iter_mut() {
node.path_info = EdgeSearchInfo::None;
}
}
}