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
/*use tonic::{Request, Response, Status};
use actix_web::web::Data;
use ant_core::data::Wallet;
use crate::service::graph_service::{GraphEntry as ServiceGraphEntry, GraphDescendants as ServiceGraphDescendants, GraphService};
use crate::controller::{DataKey, StoreType};
use crate::error::graph_error::GraphError;
pub mod graph_proto {
tonic::include_proto!("graph");
}
use graph_proto::graph_service_server::GraphService as GraphServiceTrait;
pub use graph_proto::graph_service_server::GraphServiceServer;
use graph_proto::{GraphEntry, GraphDescendants, GraphResponse, CreateGraphEntryRequest, GetGraphEntryRequest};
pub struct GraphHandler {
graph_service: Data<GraphService>,
evm_wallet: Data<Wallet>,
}
impl GraphHandler {
pub fn new(graph_service: Data<GraphService>, evm_wallet: Data<Wallet>) -> Self {
Self { graph_service, evm_wallet }
}
}
impl From<GraphDescendants> for ServiceGraphDescendants {
fn from(d: GraphDescendants) -> Self {
ServiceGraphDescendants::new(d.key, d.content)
}
}
impl From<ServiceGraphDescendants> for GraphDescendants {
fn from(d: ServiceGraphDescendants) -> Self {
GraphDescendants {
key: d.key,
content: d.content,
}
}
}
impl From<GraphEntry> for ServiceGraphEntry {
fn from(g: GraphEntry) -> Self {
ServiceGraphEntry::new(
g.name,
g.content,
g.address,
if g.parents.is_empty() { None } else { Some(g.parents) },
if g.descendants.is_empty() { None } else { Some(g.descendants.into_iter().map(ServiceGraphDescendants::from).collect()) },
)
}
}
impl From<ServiceGraphEntry> for GraphEntry {
fn from(g: ServiceGraphEntry) -> Self {
GraphEntry {
name: g.name,
content: g.content,
address: g.address,
parents: g.parents.unwrap_or_default(),
descendants: g.descendants.unwrap_or_default().into_iter().map(GraphDescendants::from).collect(),
}
}
}
impl From<GraphError> for Status {
fn from(graph_error: GraphError) -> Self {
Status::internal(graph_error.to_string())
}
}
#[tonic::async_trait]
impl GraphServiceTrait for GraphHandler {
async fn create_graph_entry(
&self,
request: Request<CreateGraphEntryRequest>,
) -> Result<Response<GraphResponse>, Status> {
let req = request.into_inner();
let graph_entry = req.graph_entry.ok_or_else(|| Status::invalid_argument("Graph entry is required"))?;
let result = self.graph_service.create_graph_entry(
ServiceGraphEntry::from(graph_entry),
self.evm_wallet.get_ref().clone(),
StoreType::from(req.store_type.unwrap_or_default()),
DataKey::from(req.data_key.unwrap_or_default()),
).await?;
Ok(Response::new(GraphResponse {
graph_entry: Some(GraphEntry::from(result)),
}))
}
async fn get_graph_entry(
&self,
request: Request<GetGraphEntryRequest>,
) -> Result<Response<GraphResponse>, Status> {
let req = request.into_inner();
let result = self.graph_service.get_graph_entry(req.address, DataKey::from(req.data_key.unwrap_or_default())).await?;
Ok(Response::new(GraphResponse {
graph_entry: Some(GraphEntry::from(result)),
}))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::service::graph_service::{GraphEntry as ServiceGraphEntry, GraphDescendants as ServiceGraphDescendants};
use graph_proto::{GraphEntry, GraphDescendants};
#[test]
fn test_graph_descendants_mapping() {
let proto = GraphDescendants {
key: "key".to_string(),
content: "content".to_string(),
};
let service = ServiceGraphDescendants::from(proto.clone());
assert_eq!(service.key, "key");
assert_eq!(service.content, "content");
let proto_back = GraphDescendants::from(service);
assert_eq!(proto_back.key, "key");
assert_eq!(proto_back.content, "content");
}
#[test]
fn test_graph_entry_mapping() {
let proto = GraphEntry {
name: Some("name".to_string()),
content: "content".to_string(),
address: Some("address".to_string()),
parents: vec!["parent1".to_string()],
descendants: vec![GraphDescendants {
key: "key".to_string(),
content: "content".to_string(),
}],
};
let service = ServiceGraphEntry::from(proto.clone());
assert_eq!(service.name, Some("name".to_string()));
assert_eq!(service.content, "content".to_string());
assert_eq!(service.address, Some("address".to_string()));
assert_eq!(service.parents, Some(vec!["parent1".to_string()]));
assert_eq!(service.descendants.as_ref().unwrap().len(), 1);
let proto_back = GraphEntry::from(service);
assert_eq!(proto_back.name, Some("name".to_string()));
assert_eq!(proto_back.content, "content".to_string());
assert_eq!(proto_back.address, Some("address".to_string()));
assert_eq!(proto_back.parents, vec!["parent1".to_string()]);
assert_eq!(proto_back.descendants.len(), 1);
}
#[test]
fn test_graph_entry_mapping_empty() {
let proto = GraphEntry {
name: None,
content: "content".to_string(),
address: None,
parents: vec![],
descendants: vec![],
};
let service = ServiceGraphEntry::from(proto.clone());
assert_eq!(service.name, None);
assert_eq!(service.parents, None);
assert_eq!(service.descendants, None);
let proto_back = GraphEntry::from(service);
assert_eq!(proto_back.parents, Vec::<String>::new());
assert_eq!(proto_back.descendants, Vec::<GraphDescendants>::new());
}
}
*/