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
use std::sync::Arc;
#[cfg(feature = "use-async-std")]
use async_std::task::spawn_blocking;
use async_trait::async_trait;
#[cfg(feature = "use-smol")]
use smol::unblock as spawn_blocking;
#[cfg(feature = "use-tokio")]
use tokio::task::spawn_blocking;
use crate::{
async_api::{
Consensus as AsyncConsensus, Info as AsyncInfo, Mempool as AsyncMempool,
Snapshot as AsyncSnapshot,
},
sync_api::{Consensus, Info, Mempool, Snapshot},
types::*,
};
macro_rules! spawn_blocking {
($expr: expr) => {{
cfg_if::cfg_if! {
if #[cfg(any(feature = "use-async-std", feature = "use-smol"))] {
spawn_blocking($expr).await
} else if #[cfg(feature = "use-tokio")] {
spawn_blocking($expr).await.expect("Failed to execute blocking task")
}
}
}};
}
pub struct AsyncConsensusImpl<C>
where
C: Consensus + Send + Sync + 'static,
{
inner: Arc<C>,
}
impl<C> AsyncConsensusImpl<C>
where
C: Consensus + Send + Sync,
{
pub fn new(inner: C) -> Self {
Self {
inner: Arc::new(inner),
}
}
}
#[async_trait]
impl<C> AsyncConsensus for AsyncConsensusImpl<C>
where
C: Consensus + Send + Sync + 'static,
{
async fn echo(&self, echo_request: RequestEcho) -> ResponseEcho {
let inner = self.inner.clone();
spawn_blocking!(move || inner.echo(echo_request))
}
async fn init_chain(&self, init_chain_request: RequestInitChain) -> ResponseInitChain {
let inner = self.inner.clone();
spawn_blocking!(move || inner.init_chain(init_chain_request))
}
async fn begin_block(&self, begin_block_request: RequestBeginBlock) -> ResponseBeginBlock {
let inner = self.inner.clone();
spawn_blocking!(move || inner.begin_block(begin_block_request))
}
async fn deliver_tx(&self, deliver_tx_request: RequestDeliverTx) -> ResponseDeliverTx {
let inner = self.inner.clone();
spawn_blocking!(move || inner.deliver_tx(deliver_tx_request))
}
async fn end_block(&self, end_block_request: RequestEndBlock) -> ResponseEndBlock {
let inner = self.inner.clone();
spawn_blocking!(move || inner.end_block(end_block_request))
}
async fn commit(&self, commit_request: RequestCommit) -> ResponseCommit {
let inner = self.inner.clone();
spawn_blocking!(move || inner.commit(commit_request))
}
async fn flush(&self, flush_request: RequestFlush) -> ResponseFlush {
let inner = self.inner.clone();
spawn_blocking!(move || inner.flush(flush_request))
}
}
pub struct AsyncInfoImpl<I>
where
I: Info + Send + Sync + 'static,
{
inner: Arc<I>,
}
impl<I> AsyncInfoImpl<I>
where
I: Info + Send + Sync,
{
pub fn new(inner: I) -> Self {
Self {
inner: Arc::new(inner),
}
}
}
#[async_trait]
impl<I> AsyncInfo for AsyncInfoImpl<I>
where
I: Info + Send + Sync + 'static,
{
async fn echo(&self, echo_request: RequestEcho) -> ResponseEcho {
let inner = self.inner.clone();
spawn_blocking!(move || inner.echo(echo_request))
}
async fn info(&self, info_request: RequestInfo) -> ResponseInfo {
let inner = self.inner.clone();
spawn_blocking!(move || inner.info(info_request))
}
async fn set_option(&self, set_option_request: RequestSetOption) -> ResponseSetOption {
let inner = self.inner.clone();
spawn_blocking!(move || inner.set_option(set_option_request))
}
async fn query(&self, query_request: RequestQuery) -> ResponseQuery {
let inner = self.inner.clone();
spawn_blocking!(move || inner.query(query_request))
}
async fn flush(&self, flush_request: RequestFlush) -> ResponseFlush {
let inner = self.inner.clone();
spawn_blocking!(move || inner.flush(flush_request))
}
}
pub struct AsyncMempoolImpl<M>
where
M: Mempool + Send + Sync + 'static,
{
inner: Arc<M>,
}
impl<M> AsyncMempoolImpl<M>
where
M: Mempool + Send + Sync,
{
pub fn new(inner: M) -> Self {
Self {
inner: Arc::new(inner),
}
}
}
#[async_trait]
impl<M> AsyncMempool for AsyncMempoolImpl<M>
where
M: Mempool + Send + Sync + 'static,
{
async fn echo(&self, echo_request: RequestEcho) -> ResponseEcho {
let inner = self.inner.clone();
spawn_blocking!(move || inner.echo(echo_request))
}
async fn check_tx(&self, check_tx_request: RequestCheckTx) -> ResponseCheckTx {
let inner = self.inner.clone();
spawn_blocking!(move || inner.check_tx(check_tx_request))
}
async fn flush(&self, flush_request: RequestFlush) -> ResponseFlush {
let inner = self.inner.clone();
spawn_blocking!(move || inner.flush(flush_request))
}
}
pub struct AsyncSnapshotImpl<S>
where
S: Snapshot + Send + Sync + 'static,
{
inner: Arc<S>,
}
impl<S> AsyncSnapshotImpl<S>
where
S: Snapshot + Send + Sync,
{
pub fn new(inner: S) -> Self {
Self {
inner: Arc::new(inner),
}
}
}
#[async_trait]
impl<S> AsyncSnapshot for AsyncSnapshotImpl<S>
where
S: Snapshot + Send + Sync + 'static,
{
async fn echo(&self, echo_request: RequestEcho) -> ResponseEcho {
let inner = self.inner.clone();
spawn_blocking!(move || inner.echo(echo_request))
}
async fn list_snapshots(
&self,
list_snapshots_request: RequestListSnapshots,
) -> ResponseListSnapshots {
let inner = self.inner.clone();
spawn_blocking!(move || inner.list_snapshots(list_snapshots_request))
}
async fn offer_snapshot(
&self,
offer_snapshot_request: RequestOfferSnapshot,
) -> ResponseOfferSnapshot {
let inner = self.inner.clone();
spawn_blocking!(move || inner.offer_snapshot(offer_snapshot_request))
}
async fn load_snapshot_chunk(
&self,
load_snapshot_chunk_request: RequestLoadSnapshotChunk,
) -> ResponseLoadSnapshotChunk {
let inner = self.inner.clone();
spawn_blocking!(move || inner.load_snapshot_chunk(load_snapshot_chunk_request))
}
async fn apply_snapshot_chunk(
&self,
apply_snapshot_chunk_request: RequestApplySnapshotChunk,
) -> ResponseApplySnapshotChunk {
let inner = self.inner.clone();
spawn_blocking!(move || inner.apply_snapshot_chunk(apply_snapshot_chunk_request))
}
async fn flush(&self, flush_request: RequestFlush) -> ResponseFlush {
let inner = self.inner.clone();
spawn_blocking!(move || inner.flush(flush_request))
}
}