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
use bytes::Bytes;
use futures::Future;
use interledger_service::Account;
use std::collections::HashMap;
use std::{str::FromStr, string::ToString};
#[cfg(test)]
mod fixtures;
mod packet;
mod routing_table;
mod server;
#[cfg(test)]
mod test_helpers;
pub use packet::{Mode, RouteControlRequest};
pub use server::{CcpRouteManager, CcpRouteManagerBuilder};
use serde::{Deserialize, Serialize};
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum RoutingRelation {
NonRoutingAccount = 0,
Parent = 1,
Peer = 2,
Child = 3,
}
impl FromStr for RoutingRelation {
type Err = ();
fn from_str(string: &str) -> Result<Self, ()> {
match string.to_lowercase().as_str() {
"nonroutingaccount" => Ok(RoutingRelation::NonRoutingAccount),
"parent" => Ok(RoutingRelation::Parent),
"peer" => Ok(RoutingRelation::Peer),
"child" => Ok(RoutingRelation::Child),
_ => Err(()),
}
}
}
impl ToString for RoutingRelation {
fn to_string(&self) -> String {
match self {
RoutingRelation::NonRoutingAccount => "NonRoutingAccount".to_string(),
RoutingRelation::Parent => "Parent".to_string(),
RoutingRelation::Peer => "Peer".to_string(),
RoutingRelation::Child => "Child".to_string(),
}
}
}
pub trait CcpRoutingAccount: Account {
fn routing_relation(&self) -> RoutingRelation;
fn should_send_routes(&self) -> bool {
self.routing_relation() == RoutingRelation::Child
|| self.routing_relation() == RoutingRelation::Peer
}
fn should_receive_routes(&self) -> bool {
self.routing_relation() == RoutingRelation::Parent
|| self.routing_relation() == RoutingRelation::Peer
}
}
type Route<T> = HashMap<Bytes, T>;
type LocalAndConfiguredRoutes<T> = (Route<T>, Route<T>);
pub trait RouteManagerStore: Clone {
type Account: CcpRoutingAccount;
fn get_local_and_configured_routes(
&self,
) -> Box<dyn Future<Item = LocalAndConfiguredRoutes<Self::Account>, Error = ()> + Send>;
fn get_accounts_to_send_routes_to(
&self,
ignore_accounts: Vec<<Self::Account as Account>::AccountId>,
) -> Box<dyn Future<Item = Vec<Self::Account>, Error = ()> + Send>;
fn get_accounts_to_receive_routes_from(
&self,
) -> Box<dyn Future<Item = Vec<Self::Account>, Error = ()> + Send>;
fn set_routes(
&mut self,
routes: impl IntoIterator<Item = (Bytes, Self::Account)>,
) -> Box<dyn Future<Item = (), Error = ()> + Send>;
}