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
use std::io::{Error, ErrorKind, Result};
use crate::ids;
use avalanche_proto::appsender::{
app_sender_client::AppSenderClient, SendAppGossipMsg, SendAppGossipSpecificMsg,
SendAppRequestMsg, SendAppResponseMsg,
};
use prost::bytes::Bytes;
use tonic::transport::Channel;
#[derive(Clone)]
pub struct Client {
inner: AppSenderClient<Channel>,
}
impl Client {
pub fn new(
client_conn: Channel,
) -> Box<dyn crate::rpcchainvm::common::appsender::AppSender + Send + Sync> {
Box::new(Client {
inner: AppSenderClient::new(client_conn),
})
}
}
#[tonic::async_trait]
impl crate::rpcchainvm::common::appsender::AppSender for Client {
async fn send_app_request(
&self,
node_ids: ids::node::Set,
request_id: u32,
request: Vec<u8>,
) -> Result<()> {
let mut client = self.inner.clone();
let mut id_bytes: Vec<Bytes> = Vec::with_capacity(node_ids.len());
for node_id in node_ids.iter() {
let node_id = node_id;
id_bytes.push(Bytes::from(node_id.to_vec()))
}
client
.send_app_request(SendAppRequestMsg {
node_ids: id_bytes,
request_id,
request: Bytes::from(request),
})
.await
.map_err(|e| {
Error::new(
ErrorKind::Other,
format!("send_app_request failed: {:?}", e),
)
})?;
Ok(())
}
async fn send_app_response(
&self,
node_id: ids::node::Id,
request_id: u32,
response: Vec<u8>,
) -> Result<()> {
let mut client = self.inner.clone();
client
.send_app_response(SendAppResponseMsg {
node_id: Bytes::from(node_id.to_vec()),
request_id,
response: Bytes::from(response),
})
.await
.map_err(|e| {
Error::new(
ErrorKind::Other,
format!("send_app_response failed: {:?}", e),
)
})?;
Ok(())
}
async fn send_app_gossip(&self, msg: Vec<u8>) -> Result<()> {
let mut client = self.inner.clone();
client
.send_app_gossip(SendAppGossipMsg {
msg: Bytes::from(msg),
})
.await
.map_err(|e| {
Error::new(ErrorKind::Other, format!("send_app_gossip failed: {:?}", e))
})?;
Ok(())
}
async fn send_app_gossip_specific(&self, node_ids: ids::node::Set, msg: Vec<u8>) -> Result<()> {
let mut client = self.inner.clone();
let mut node_id_bytes: Vec<Bytes> = Vec::with_capacity(node_ids.len());
for node_id in node_ids.iter() {
node_id_bytes.push(Bytes::from(node_id.to_vec()))
}
client
.send_app_gossip_specific(SendAppGossipSpecificMsg {
node_ids: node_id_bytes,
msg: Bytes::from(msg),
})
.await
.map_err(|e| {
Error::new(
ErrorKind::Other,
format!("send_app_gossip_specific failed: {:?}", e),
)
})?;
Ok(())
}
}