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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use crate::{data_notification::NotificationId, data_stream::DataStreamListener, error::Error};
use aptos_types::{ledger_info::LedgerInfoWithSignatures, transaction::Version};
use async_trait::async_trait;
use futures::{
channel::{mpsc, oneshot},
stream::FusedStream,
SinkExt, Stream,
};
use std::{
pin::Pin,
task::{Context, Poll},
};
pub type Epoch = u64;
#[async_trait]
pub trait DataStreamingClient {
async fn get_all_accounts(
&self,
version: Version,
start_index: Option<u64>,
) -> Result<DataStreamListener, Error>;
async fn get_all_epoch_ending_ledger_infos(
&self,
start_epoch: Epoch,
) -> Result<DataStreamListener, Error>;
async fn get_all_transaction_outputs(
&self,
start_version: Version,
end_version: Version,
proof_version: Version,
) -> Result<DataStreamListener, Error>;
async fn get_all_transactions(
&self,
start_version: Version,
end_version: Version,
proof_version: Version,
include_events: bool,
) -> Result<DataStreamListener, Error>;
async fn continuously_stream_transaction_outputs(
&self,
known_version: u64,
known_epoch: u64,
target: Option<LedgerInfoWithSignatures>,
) -> Result<DataStreamListener, Error>;
async fn continuously_stream_transactions(
&self,
start_version: Version,
start_epoch: Epoch,
include_events: bool,
target: Option<LedgerInfoWithSignatures>,
) -> Result<DataStreamListener, Error>;
async fn terminate_stream_with_feedback(
&self,
notification_id: NotificationId,
notification_feedback: NotificationFeedback,
) -> Result<(), Error>;
}
#[derive(Debug)]
pub struct StreamRequestMessage {
pub stream_request: StreamRequest,
pub response_sender: oneshot::Sender<Result<DataStreamListener, Error>>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StreamRequest {
GetAllAccounts(GetAllAccountsRequest),
GetAllEpochEndingLedgerInfos(GetAllEpochEndingLedgerInfosRequest),
GetAllTransactions(GetAllTransactionsRequest),
GetAllTransactionOutputs(GetAllTransactionOutputsRequest),
ContinuouslyStreamTransactions(ContinuouslyStreamTransactionsRequest),
ContinuouslyStreamTransactionOutputs(ContinuouslyStreamTransactionOutputsRequest),
TerminateStream(TerminateStreamRequest),
}
impl StreamRequest {
pub fn get_label(&self) -> &'static str {
match self {
Self::GetAllAccounts(_) => "get_all_accounts",
Self::GetAllEpochEndingLedgerInfos(_) => "get_all_epoch_ending_ledger_infos",
Self::GetAllTransactions(_) => "get_all_transactions",
Self::GetAllTransactionOutputs(_) => "get_all_transaction_outputs",
Self::ContinuouslyStreamTransactions(_) => "continuously_stream_transactions",
Self::ContinuouslyStreamTransactionOutputs(_) => {
"continuously_stream_transaction_outputs"
}
Self::TerminateStream(_) => "terminate_stream",
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GetAllAccountsRequest {
pub version: Version,
pub start_index: u64,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GetAllEpochEndingLedgerInfosRequest {
pub start_epoch: Epoch,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GetAllTransactionsRequest {
pub start_version: Version,
pub end_version: Version,
pub proof_version: Version,
pub include_events: bool,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GetAllTransactionOutputsRequest {
pub start_version: Version,
pub end_version: Version,
pub proof_version: Version,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ContinuouslyStreamTransactionsRequest {
pub known_version: Version,
pub known_epoch: Epoch,
pub include_events: bool,
pub target: Option<LedgerInfoWithSignatures>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ContinuouslyStreamTransactionOutputsRequest {
pub known_version: Version,
pub known_epoch: Epoch,
pub target: Option<LedgerInfoWithSignatures>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TerminateStreamRequest {
pub notification_id: NotificationId,
pub notification_feedback: NotificationFeedback,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum NotificationFeedback {
EmptyPayloadData,
EndOfStream,
InvalidPayloadData,
PayloadProofFailed,
PayloadTypeIsIncorrect,
}
impl NotificationFeedback {
pub fn get_label(&self) -> &'static str {
match self {
Self::EmptyPayloadData => "empty_payload_data",
Self::EndOfStream => "end_of_stream",
Self::InvalidPayloadData => "invalid_payload_data",
Self::PayloadProofFailed => "payload_proof_failed",
Self::PayloadTypeIsIncorrect => "payload_type_is_correct",
}
}
}
#[derive(Clone)]
pub struct StreamingServiceClient {
request_sender: mpsc::UnboundedSender<StreamRequestMessage>,
}
impl StreamingServiceClient {
pub fn new(request_sender: mpsc::UnboundedSender<StreamRequestMessage>) -> Self {
Self { request_sender }
}
async fn send_stream_request(
&self,
client_request: StreamRequest,
) -> Result<oneshot::Receiver<Result<DataStreamListener, Error>>, Error> {
let mut request_sender = self.request_sender.clone();
let (response_sender, response_receiver) = oneshot::channel();
let request_message = StreamRequestMessage {
stream_request: client_request,
response_sender,
};
request_sender.send(request_message).await?;
Ok(response_receiver)
}
async fn send_request_and_await_response(
&self,
client_request: StreamRequest,
) -> Result<DataStreamListener, Error> {
let response_receiver = self.send_stream_request(client_request).await?;
response_receiver.await?
}
}
#[async_trait]
impl DataStreamingClient for StreamingServiceClient {
async fn get_all_accounts(
&self,
version: u64,
start_index: Option<u64>,
) -> Result<DataStreamListener, Error> {
let start_index = start_index.unwrap_or(0);
let client_request = StreamRequest::GetAllAccounts(GetAllAccountsRequest {
version,
start_index,
});
self.send_request_and_await_response(client_request).await
}
async fn get_all_epoch_ending_ledger_infos(
&self,
start_epoch: u64,
) -> Result<DataStreamListener, Error> {
let client_request =
StreamRequest::GetAllEpochEndingLedgerInfos(GetAllEpochEndingLedgerInfosRequest {
start_epoch,
});
self.send_request_and_await_response(client_request).await
}
async fn get_all_transaction_outputs(
&self,
start_version: u64,
end_version: u64,
proof_version: u64,
) -> Result<DataStreamListener, Error> {
let client_request =
StreamRequest::GetAllTransactionOutputs(GetAllTransactionOutputsRequest {
start_version,
end_version,
proof_version,
});
self.send_request_and_await_response(client_request).await
}
async fn get_all_transactions(
&self,
start_version: u64,
end_version: u64,
proof_version: u64,
include_events: bool,
) -> Result<DataStreamListener, Error> {
let client_request = StreamRequest::GetAllTransactions(GetAllTransactionsRequest {
start_version,
end_version,
proof_version,
include_events,
});
self.send_request_and_await_response(client_request).await
}
async fn continuously_stream_transaction_outputs(
&self,
known_version: u64,
known_epoch: u64,
target: Option<LedgerInfoWithSignatures>,
) -> Result<DataStreamListener, Error> {
let client_request = StreamRequest::ContinuouslyStreamTransactionOutputs(
ContinuouslyStreamTransactionOutputsRequest {
known_version,
known_epoch,
target,
},
);
self.send_request_and_await_response(client_request).await
}
async fn continuously_stream_transactions(
&self,
known_version: u64,
known_epoch: u64,
include_events: bool,
target: Option<LedgerInfoWithSignatures>,
) -> Result<DataStreamListener, Error> {
let client_request =
StreamRequest::ContinuouslyStreamTransactions(ContinuouslyStreamTransactionsRequest {
known_version,
known_epoch,
include_events,
target,
});
self.send_request_and_await_response(client_request).await
}
async fn terminate_stream_with_feedback(
&self,
notification_id: u64,
notification_feedback: NotificationFeedback,
) -> Result<(), Error> {
let client_request = StreamRequest::TerminateStream(TerminateStreamRequest {
notification_id,
notification_feedback,
});
let _ = self.send_stream_request(client_request).await?;
Ok(())
}
}
#[derive(Debug)]
pub struct StreamingServiceListener {
request_receiver: mpsc::UnboundedReceiver<StreamRequestMessage>,
}
impl StreamingServiceListener {
pub fn new(request_receiver: mpsc::UnboundedReceiver<StreamRequestMessage>) -> Self {
Self { request_receiver }
}
}
impl Stream for StreamingServiceListener {
type Item = StreamRequestMessage;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.get_mut().request_receiver).poll_next(cx)
}
}
impl FusedStream for StreamingServiceListener {
fn is_terminated(&self) -> bool {
self.request_receiver.is_terminated()
}
}
pub fn new_streaming_service_client_listener_pair(
) -> (StreamingServiceClient, StreamingServiceListener) {
let (request_sender, request_listener) = mpsc::unbounded();
let streaming_service_client = StreamingServiceClient::new(request_sender);
let streaming_service_listener = StreamingServiceListener::new(request_listener);
(streaming_service_client, streaming_service_listener)
}