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
use std::{
    io::{Error, ErrorKind},
    sync::Arc,
};

use crate::{
    ids,
    proto::pb::{
        self,
        appsender::{
            SendAppGossipMsg, SendAppGossipSpecificMsg, SendAppRequestMsg, SendAppResponseMsg,
            SendCrossChainAppRequestMsg, SendCrossChainAppResponseMsg,
        },
        google::protobuf::Empty,
    },
};
use tokio::sync::RwLock;
use tonic::{Request, Response, Status};

#[derive(Clone)]
pub struct Server {
    pub inner: Arc<RwLock<Box<dyn super::AppSender + Send + Sync>>>,
}

/// A gRPC server which wraps a subnet::rpc::database::Database impl allowing client control over over RPC.
impl Server {
    pub fn new(
        appsender: Box<dyn super::AppSender + Send + Sync>,
    ) -> impl pb::appsender::app_sender_server::AppSender {
        Server {
            inner: Arc::new(RwLock::new(appsender)),
        }
    }
}

#[tonic::async_trait]
impl pb::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 {}))
    }

    async fn send_cross_chain_app_request(
        &self,
        request: Request<SendCrossChainAppRequestMsg>,
    ) -> Result<Response<Empty>, Status> {
        let req = request.into_inner();

        let appsender = self.inner.read().await;
        appsender
            .send_cross_chain_app_request(
                ids::Id::from_slice(&req.chain_id),
                req.request_id,
                req.request.to_vec(),
            )
            .await
            .map_err(|e| {
                Error::new(
                    ErrorKind::Other,
                    format!("send_cross_chain_app_request failed: {:?}", e),
                )
            })?;

        Ok(Response::new(Empty {}))
    }

    async fn send_cross_chain_app_response(
        &self,
        request: Request<SendCrossChainAppResponseMsg>,
    ) -> Result<Response<Empty>, Status> {
        let req = request.into_inner();

        let appsender = self.inner.read().await;
        appsender
            .send_cross_chain_app_response(
                ids::Id::from_slice(&req.chain_id),
                req.request_id,
                req.response.to_vec(),
            )
            .await
            .map_err(|e| {
                Error::new(
                    ErrorKind::Other,
                    format!("send_cross_chain_app_response failed: {:?}", e),
                )
            })?;

        Ok(Response::new(Empty {}))
    }
}