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
use std::{
io::{Error, ErrorKind},
sync::Arc,
};
use crate::ids;
use avalanche_proto::{
appsender::{
SendAppGossipMsg, SendAppGossipSpecificMsg, SendAppRequestMsg, SendAppResponseMsg,
},
google::protobuf::Empty,
};
use tokio::sync::RwLock;
use tonic::{Request, Response, Status};
#[derive(Clone)]
pub struct Server {
pub inner: Arc<RwLock<Box<dyn crate::rpcchainvm::common::appsender::AppSender + Send + Sync>>>,
}
impl Server {
pub fn new(
appsender: Box<dyn crate::rpcchainvm::common::appsender::AppSender + Send + Sync>,
) -> impl avalanche_proto::appsender::app_sender_server::AppSender {
Server {
inner: Arc::new(RwLock::new(appsender)),
}
}
}
#[tonic::async_trait]
impl avalanche_proto::appsender::app_sender_server::AppSender for Server {
async fn send_app_request(
&self,
request: Request<SendAppRequestMsg>,
) -> Result<Response<Empty>, Status> {
let req = request.into_inner();
let appsender = self.inner.read().await;
let mut node_ids = ids::node::new_set(req.node_ids.len());
for node_id_bytes in req.node_ids.iter() {
let node_id = ids::node::Id::from_slice(node_id_bytes);
node_ids.insert(node_id);
}
appsender
.send_app_request(node_ids, req.request_id, req.request.to_vec())
.await
.map_err(|e| {
Error::new(
ErrorKind::Other,
format!("send_app_request failed: {:?}", e),
)
})?;
Ok(Response::new(Empty {}))
}
async fn send_app_response(
&self,
request: Request<SendAppResponseMsg>,
) -> Result<Response<Empty>, Status> {
let req = request.into_inner();
let appsender = self.inner.read().await;
let node_id = ids::node::Id::from_slice(&req.node_id);
appsender
.send_app_response(node_id, req.request_id, req.response.to_vec())
.await
.map_err(|e| {
Error::new(
ErrorKind::Other,
format!("send_app_response failed: {:?}", e),
)
})?;
Ok(Response::new(Empty {}))
}
async fn send_app_gossip(
&self,
request: Request<SendAppGossipMsg>,
) -> Result<Response<Empty>, Status> {
let req = request.into_inner();
let appsender = self.inner.read().await;
appsender
.send_app_gossip(req.msg.to_vec())
.await
.map_err(|e| {
Error::new(ErrorKind::Other, format!("send_app_gossip failed: {:?}", e))
})?;
Ok(Response::new(Empty {}))
}
async fn send_app_gossip_specific(
&self,
request: Request<SendAppGossipSpecificMsg>,
) -> Result<Response<Empty>, Status> {
let req = request.into_inner();
let appsender = self.inner.read().await;
let mut node_ids = ids::node::new_set(req.node_ids.len());
for node_id_bytes in req.node_ids.iter() {
let node_id = ids::node::Id::from_slice(node_id_bytes);
node_ids.insert(node_id);
}
appsender
.send_app_gossip_specific(node_ids, req.msg.to_vec())
.await
.map_err(|e| {
Error::new(
ErrorKind::Other,
format!("send_app_gossip_specific failed: {:?}", e),
)
})?;
Ok(Response::new(Empty {}))
}
}