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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use futures::FutureExt;
use http_body::Body as _;
use std::convert::Infallible;
use std::pin::Pin;
use std::{
    future::Future,
    task::{Context, Poll},
};
use tonic::transport::NamedService;
use tonic::{
    body::BoxBody,
    transport::{Body, Channel},
};
use tower::ServiceExt;

fn proxy(
    channel: Channel,
    req: http::Request<Body>,
) -> Pin<Box<dyn Future<Output = Result<http::Response<BoxBody>, Infallible>> + Send + 'static>> {
    tracing::debug!(headers = ?req.headers(), "proxying request");
    // Convert request types
    let req = req.map(|b| {
        b.map_err(|e| tonic::Status::from_error(Box::new(e)))
            .boxed_unsync()
    });

    let rsp = channel.oneshot(req);

    async move {
        // Once we get the response, we need to convert any transport errors into
        // an Ok(HTTP response reporting an internal error), so we can have Error = Infallible
        let rsp = match rsp.await {
            Ok(rsp) => rsp.map(|b| {
                b.map_err(|e| tonic::Status::from_error(Box::new(e)))
                    .boxed_unsync()
            }),
            Err(e) => tonic::Status::internal(format!("grpc proxy error: {e}")).to_http(),
        };
        Ok::<_, Infallible>(rsp)
    }
    .boxed()
}

#[derive(Clone)]
pub struct AppQueryProxy(pub Channel);

impl NamedService for AppQueryProxy {
    const NAME: &'static str = "penumbra.core.app.v1.QueryService";
}

impl tower::Service<http::Request<Body>> for AppQueryProxy {
    type Response = http::Response<BoxBody>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        proxy(self.0.clone(), req)
    }
}

#[derive(Clone)]
pub struct GovernanceQueryProxy(pub Channel);

impl NamedService for GovernanceQueryProxy {
    const NAME: &'static str = "penumbra.core.component.governance.v1.QueryService";
}

impl tower::Service<http::Request<Body>> for GovernanceQueryProxy {
    type Response = http::Response<BoxBody>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        proxy(self.0.clone(), req)
    }
}

#[derive(Clone)]
pub struct DexQueryProxy(pub Channel);

impl NamedService for DexQueryProxy {
    const NAME: &'static str = "penumbra.core.component.dex.v1.QueryService";
}

impl tower::Service<http::Request<Body>> for DexQueryProxy {
    type Response = http::Response<BoxBody>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        proxy(self.0.clone(), req)
    }
}

#[derive(Clone)]
pub struct DexSimulationProxy(pub Channel);

impl NamedService for DexSimulationProxy {
    const NAME: &'static str = "penumbra.core.component.dex.v1.SimulationService";
}

impl tower::Service<http::Request<Body>> for DexSimulationProxy {
    type Response = http::Response<BoxBody>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        proxy(self.0.clone(), req)
    }
}

#[derive(Clone)]
pub struct FeeQueryProxy(pub Channel);

impl NamedService for FeeQueryProxy {
    const NAME: &'static str = "penumbra.core.component.fee.v1.QueryService";
}

impl tower::Service<http::Request<Body>> for FeeQueryProxy {
    type Response = http::Response<BoxBody>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        proxy(self.0.clone(), req)
    }
}

#[derive(Clone)]
pub struct SctQueryProxy(pub Channel);

impl NamedService for SctQueryProxy {
    const NAME: &'static str = "penumbra.core.component.sct.v1.QueryService";
}

impl tower::Service<http::Request<Body>> for SctQueryProxy {
    type Response = http::Response<BoxBody>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        proxy(self.0.clone(), req)
    }
}

#[derive(Clone)]
pub struct ShieldedPoolQueryProxy(pub Channel);

impl NamedService for ShieldedPoolQueryProxy {
    const NAME: &'static str = "penumbra.core.component.shielded_pool.v1.QueryService";
}

impl tower::Service<http::Request<Body>> for ShieldedPoolQueryProxy {
    type Response = http::Response<BoxBody>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        proxy(self.0.clone(), req)
    }
}

#[derive(Clone)]
pub struct ChainQueryProxy(pub Channel);

impl NamedService for ChainQueryProxy {
    const NAME: &'static str = "penumbra.core.component.chain.v1.QueryService";
}

impl tower::Service<http::Request<Body>> for ChainQueryProxy {
    type Response = http::Response<BoxBody>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        proxy(self.0.clone(), req)
    }
}

#[derive(Clone)]
pub struct StakeQueryProxy(pub Channel);

impl NamedService for StakeQueryProxy {
    const NAME: &'static str = "penumbra.core.component.stake.v1.QueryService";
}

impl tower::Service<http::Request<Body>> for StakeQueryProxy {
    type Response = http::Response<BoxBody>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        proxy(self.0.clone(), req)
    }
}

#[derive(Clone)]
pub struct CompactBlockQueryProxy(pub Channel);

impl NamedService for CompactBlockQueryProxy {
    const NAME: &'static str = "penumbra.core.component.compact_block.v1.QueryService";
}

impl tower::Service<http::Request<Body>> for CompactBlockQueryProxy {
    type Response = http::Response<BoxBody>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        proxy(self.0.clone(), req)
    }
}

#[derive(Clone)]
pub struct TendermintProxyProxy(pub Channel);

impl NamedService for TendermintProxyProxy {
    const NAME: &'static str = "penumbra.util.tendermint_proxy.v1.TendermintProxyService";
}

impl tower::Service<http::Request<Body>> for TendermintProxyProxy {
    type Response = http::Response<BoxBody>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<Body>) -> Self::Future {
        proxy(self.0.clone(), req)
    }
}