@dashevo/dapi-grpc 5.0.0-beta.4 → 5.0.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +7 -0
- package/build.rs +48 -6
- package/clients/drive/v0/nodejs/drive_pbjs.js +25 -235
- package/clients/platform/v0/nodejs/platform_pbjs.js +25 -235
- package/clients/platform/v0/nodejs/platform_protoc.js +18 -261
- package/clients/platform/v0/web/platform_pb.d.ts +6 -34
- package/clients/platform/v0/web/platform_pb.js +18 -261
- package/package.json +2 -2
- package/protos/platform/v0/platform.proto +21 -8
- package/src/lib.rs +3 -0
- package/src/request_codec.rs +49 -0
- package/test/unit/nonunique-proof.spec.js +42 -0
- package/tests/protobuf_request_errors.rs +372 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
//! A malformed request belongs to its caller; a valid frame still reaches the service.
|
|
2
|
+
|
|
3
|
+
#![cfg(all(feature = "core", feature = "server", not(target_arch = "wasm32")))]
|
|
4
|
+
|
|
5
|
+
use bytes::Bytes;
|
|
6
|
+
use dapi_grpc::core::v0::core_server::{Core, CoreServer};
|
|
7
|
+
use dapi_grpc::core::v0::{
|
|
8
|
+
BlockHeadersWithChainLocksRequest, BlockHeadersWithChainLocksResponse,
|
|
9
|
+
BroadcastTransactionRequest, BroadcastTransactionResponse, GetBestBlockHeightRequest,
|
|
10
|
+
GetBestBlockHeightResponse, GetBlockRequest, GetBlockResponse, GetBlockchainStatusRequest,
|
|
11
|
+
GetBlockchainStatusResponse, GetEstimatedTransactionFeeRequest,
|
|
12
|
+
GetEstimatedTransactionFeeResponse, GetMasternodeStatusRequest, GetMasternodeStatusResponse,
|
|
13
|
+
GetTransactionRequest, GetTransactionResponse, MasternodeListRequest, MasternodeListResponse,
|
|
14
|
+
TransactionsWithProofsRequest, TransactionsWithProofsResponse,
|
|
15
|
+
};
|
|
16
|
+
use dapi_grpc::drive::v0::drive_internal_server::{DriveInternal, DriveInternalServer};
|
|
17
|
+
use dapi_grpc::drive::v0::{GetProofsRequest, GetProofsResponse};
|
|
18
|
+
use dapi_grpc::tonic::codegen::http::{HeaderMap, Request as HttpRequest};
|
|
19
|
+
use dapi_grpc::tonic::transport::Server;
|
|
20
|
+
use dapi_grpc::tonic::{Code, Request, Response, Status};
|
|
21
|
+
use dapi_grpc::Message;
|
|
22
|
+
use h2::client::SendRequest;
|
|
23
|
+
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
24
|
+
use std::sync::Arc;
|
|
25
|
+
use tokio::task::JoinHandle;
|
|
26
|
+
use tokio_stream::{Iter, StreamExt};
|
|
27
|
+
|
|
28
|
+
type ResponseStream<T> = Iter<std::vec::IntoIter<Result<T, Status>>>;
|
|
29
|
+
|
|
30
|
+
#[derive(Clone, Default)]
|
|
31
|
+
struct RecordingService(Arc<AtomicUsize>);
|
|
32
|
+
|
|
33
|
+
impl RecordingService {
|
|
34
|
+
fn response<T: Default>(&self) -> Result<Response<T>, Status> {
|
|
35
|
+
self.0.fetch_add(1, Ordering::SeqCst);
|
|
36
|
+
Ok(Response::new(T::default()))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
fn stream<T: Default>(&self) -> Result<Response<ResponseStream<T>>, Status> {
|
|
40
|
+
self.0.fetch_add(1, Ordering::SeqCst);
|
|
41
|
+
Ok(Response::new(tokio_stream::iter(vec![Ok(T::default())])))
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#[dapi_grpc::tonic::async_trait]
|
|
46
|
+
impl Core for RecordingService {
|
|
47
|
+
async fn get_blockchain_status(
|
|
48
|
+
&self,
|
|
49
|
+
_: Request<GetBlockchainStatusRequest>,
|
|
50
|
+
) -> Result<Response<GetBlockchainStatusResponse>, Status> {
|
|
51
|
+
self.response()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async fn get_masternode_status(
|
|
55
|
+
&self,
|
|
56
|
+
_: Request<GetMasternodeStatusRequest>,
|
|
57
|
+
) -> Result<Response<GetMasternodeStatusResponse>, Status> {
|
|
58
|
+
self.response()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async fn get_block(
|
|
62
|
+
&self,
|
|
63
|
+
_: Request<GetBlockRequest>,
|
|
64
|
+
) -> Result<Response<GetBlockResponse>, Status> {
|
|
65
|
+
self.response()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async fn get_best_block_height(
|
|
69
|
+
&self,
|
|
70
|
+
_: Request<GetBestBlockHeightRequest>,
|
|
71
|
+
) -> Result<Response<GetBestBlockHeightResponse>, Status> {
|
|
72
|
+
self.response()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async fn broadcast_transaction(
|
|
76
|
+
&self,
|
|
77
|
+
_: Request<BroadcastTransactionRequest>,
|
|
78
|
+
) -> Result<Response<BroadcastTransactionResponse>, Status> {
|
|
79
|
+
self.response()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async fn get_transaction(
|
|
83
|
+
&self,
|
|
84
|
+
_: Request<GetTransactionRequest>,
|
|
85
|
+
) -> Result<Response<GetTransactionResponse>, Status> {
|
|
86
|
+
self.response()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async fn get_estimated_transaction_fee(
|
|
90
|
+
&self,
|
|
91
|
+
_: Request<GetEstimatedTransactionFeeRequest>,
|
|
92
|
+
) -> Result<Response<GetEstimatedTransactionFeeResponse>, Status> {
|
|
93
|
+
self.response()
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
type subscribeToBlockHeadersWithChainLocksStream =
|
|
97
|
+
ResponseStream<BlockHeadersWithChainLocksResponse>;
|
|
98
|
+
type subscribeToTransactionsWithProofsStream = ResponseStream<TransactionsWithProofsResponse>;
|
|
99
|
+
type subscribeToMasternodeListStream = ResponseStream<MasternodeListResponse>;
|
|
100
|
+
|
|
101
|
+
async fn subscribe_to_block_headers_with_chain_locks(
|
|
102
|
+
&self,
|
|
103
|
+
_: Request<BlockHeadersWithChainLocksRequest>,
|
|
104
|
+
) -> Result<Response<Self::subscribeToBlockHeadersWithChainLocksStream>, Status> {
|
|
105
|
+
self.stream()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async fn subscribe_to_transactions_with_proofs(
|
|
109
|
+
&self,
|
|
110
|
+
_: Request<TransactionsWithProofsRequest>,
|
|
111
|
+
) -> Result<Response<Self::subscribeToTransactionsWithProofsStream>, Status> {
|
|
112
|
+
self.stream()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async fn subscribe_to_masternode_list(
|
|
116
|
+
&self,
|
|
117
|
+
_: Request<MasternodeListRequest>,
|
|
118
|
+
) -> Result<Response<Self::subscribeToMasternodeListStream>, Status> {
|
|
119
|
+
self.stream()
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
#[dapi_grpc::tonic::async_trait]
|
|
124
|
+
impl DriveInternal for RecordingService {
|
|
125
|
+
async fn get_proofs(
|
|
126
|
+
&self,
|
|
127
|
+
_: Request<GetProofsRequest>,
|
|
128
|
+
) -> Result<Response<GetProofsResponse>, Status> {
|
|
129
|
+
self.response()
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
struct Node {
|
|
134
|
+
sender: SendRequest<Bytes>,
|
|
135
|
+
calls: Arc<AtomicUsize>,
|
|
136
|
+
server: JoinHandle<()>,
|
|
137
|
+
connection: JoinHandle<()>,
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
impl Drop for Node {
|
|
141
|
+
fn drop(&mut self) {
|
|
142
|
+
self.server.abort();
|
|
143
|
+
self.connection.abort();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
struct WireResponse {
|
|
148
|
+
status: Status,
|
|
149
|
+
data: Vec<u8>,
|
|
150
|
+
headers: HeaderMap,
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
impl Node {
|
|
154
|
+
async fn new() -> Self {
|
|
155
|
+
let (client_io, server_io) = tokio::io::duplex(64 * 1024);
|
|
156
|
+
let service = RecordingService::default();
|
|
157
|
+
let calls = Arc::clone(&service.0);
|
|
158
|
+
let server = tokio::spawn(async move {
|
|
159
|
+
let incoming = tokio_stream::iter([Ok::<_, std::io::Error>(server_io)])
|
|
160
|
+
.chain(tokio_stream::pending());
|
|
161
|
+
Server::builder()
|
|
162
|
+
.add_service(CoreServer::new(service.clone()).max_decoding_message_size(512 * 1024))
|
|
163
|
+
.add_service(DriveInternalServer::new(service))
|
|
164
|
+
.serve_with_incoming(incoming)
|
|
165
|
+
.await
|
|
166
|
+
.expect("serve in-memory generated servers");
|
|
167
|
+
});
|
|
168
|
+
let (sender, connection) = h2::client::handshake(client_io)
|
|
169
|
+
.await
|
|
170
|
+
.expect("HTTP/2 handshake");
|
|
171
|
+
let connection = tokio::spawn(async move {
|
|
172
|
+
connection.await.expect("in-memory HTTP/2 connection");
|
|
173
|
+
});
|
|
174
|
+
Self {
|
|
175
|
+
sender,
|
|
176
|
+
calls,
|
|
177
|
+
server,
|
|
178
|
+
connection,
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async fn request(&self, path: &str, frame: Vec<u8>, encoding: Option<&str>) -> WireResponse {
|
|
183
|
+
let mut request = HttpRequest::builder()
|
|
184
|
+
.method("POST")
|
|
185
|
+
.uri(format!("http://generated.in-memory{path}"))
|
|
186
|
+
.header("content-type", "application/grpc")
|
|
187
|
+
.header("te", "trailers");
|
|
188
|
+
if let Some(encoding) = encoding {
|
|
189
|
+
request = request.header("grpc-encoding", encoding);
|
|
190
|
+
}
|
|
191
|
+
let request = request.body(()).expect("wire request");
|
|
192
|
+
let mut sender = self
|
|
193
|
+
.sender
|
|
194
|
+
.clone()
|
|
195
|
+
.ready()
|
|
196
|
+
.await
|
|
197
|
+
.expect("ready HTTP/2 client");
|
|
198
|
+
let (response, mut body) = sender
|
|
199
|
+
.send_request(request, frame.is_empty())
|
|
200
|
+
.expect("send headers");
|
|
201
|
+
if !frame.is_empty() {
|
|
202
|
+
body.send_data(Bytes::from(frame), true)
|
|
203
|
+
.expect("send framed payload");
|
|
204
|
+
}
|
|
205
|
+
let response = response.await.expect("wire response");
|
|
206
|
+
let headers = response.headers().clone();
|
|
207
|
+
let mut body = response.into_body();
|
|
208
|
+
let mut data = Vec::new();
|
|
209
|
+
while let Some(chunk) = body.data().await {
|
|
210
|
+
let chunk = chunk.expect("response bytes");
|
|
211
|
+
body.flow_control()
|
|
212
|
+
.release_capacity(chunk.len())
|
|
213
|
+
.expect("release response capacity");
|
|
214
|
+
data.extend_from_slice(&chunk);
|
|
215
|
+
}
|
|
216
|
+
let trailers = body.trailers().await.expect("response trailers");
|
|
217
|
+
let status = Status::from_header_map(&headers)
|
|
218
|
+
.or_else(|| trailers.as_ref().and_then(Status::from_header_map))
|
|
219
|
+
.expect("gRPC status");
|
|
220
|
+
WireResponse {
|
|
221
|
+
status,
|
|
222
|
+
data,
|
|
223
|
+
headers,
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
fn frame(payload: &[u8]) -> Vec<u8> {
|
|
229
|
+
let mut frame = vec![0];
|
|
230
|
+
frame.extend_from_slice(&(payload.len() as u32).to_be_bytes());
|
|
231
|
+
frame.extend_from_slice(payload);
|
|
232
|
+
frame
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const CORE_UNARY: &str = "/org.dash.platform.dapi.v0.Core/getTransaction";
|
|
236
|
+
const CORE_STREAM: &str = "/org.dash.platform.dapi.v0.Core/subscribeToTransactionsWithProofs";
|
|
237
|
+
const DRIVE_UNARY: &str = "/org.dash.platform.drive.v0.DriveInternal/getProofs";
|
|
238
|
+
|
|
239
|
+
#[tokio::test]
|
|
240
|
+
async fn should_refuse_malformed_caller_protobuf_without_invoking_generated_services() {
|
|
241
|
+
let node = Node::new().await;
|
|
242
|
+
let mut wrong = Vec::new();
|
|
243
|
+
for path in [CORE_UNARY, CORE_STREAM, DRIVE_UNARY] {
|
|
244
|
+
for payload in [&[0][..], &[0x80], &[0x0a, 0x02, 0x08]] {
|
|
245
|
+
let response = node.request(path, frame(payload), None).await;
|
|
246
|
+
if response.status.code() != Code::InvalidArgument {
|
|
247
|
+
wrong.push(format!(
|
|
248
|
+
"{path} {payload:02x?}: {:?}: {}",
|
|
249
|
+
response.status.code(),
|
|
250
|
+
response.status.message()
|
|
251
|
+
));
|
|
252
|
+
}
|
|
253
|
+
assert!(response
|
|
254
|
+
.status
|
|
255
|
+
.message()
|
|
256
|
+
.starts_with("failed to decode Protobuf message: "));
|
|
257
|
+
assert!(
|
|
258
|
+
response.data.is_empty(),
|
|
259
|
+
"a refused stream must emit no item"
|
|
260
|
+
);
|
|
261
|
+
assert!(
|
|
262
|
+
response.headers.contains_key("grpc-status"),
|
|
263
|
+
"a decode refusal is trailers-only"
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
let invalid_utf8 = node
|
|
268
|
+
.request(CORE_UNARY, frame(&[0x0a, 0x01, 0xff]), None)
|
|
269
|
+
.await;
|
|
270
|
+
assert!(invalid_utf8
|
|
271
|
+
.status
|
|
272
|
+
.message()
|
|
273
|
+
.starts_with("failed to decode Protobuf message: "));
|
|
274
|
+
assert!(invalid_utf8.data.is_empty());
|
|
275
|
+
assert!(invalid_utf8.headers.contains_key("grpc-status"));
|
|
276
|
+
if invalid_utf8.status.code() != Code::InvalidArgument {
|
|
277
|
+
wrong.push(format!("invalid UTF-8: {:?}", invalid_utf8.status.code()));
|
|
278
|
+
}
|
|
279
|
+
assert_eq!(
|
|
280
|
+
node.calls.load(Ordering::SeqCst),
|
|
281
|
+
0,
|
|
282
|
+
"no malformed request reaches a handler"
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
let valid = node
|
|
286
|
+
.request(
|
|
287
|
+
CORE_UNARY,
|
|
288
|
+
frame(
|
|
289
|
+
&GetTransactionRequest {
|
|
290
|
+
id: "transaction".to_owned(),
|
|
291
|
+
}
|
|
292
|
+
.encode_to_vec(),
|
|
293
|
+
),
|
|
294
|
+
None,
|
|
295
|
+
)
|
|
296
|
+
.await;
|
|
297
|
+
assert_eq!(
|
|
298
|
+
valid.status.code(),
|
|
299
|
+
Code::Ok,
|
|
300
|
+
"the same route accepts valid protobuf"
|
|
301
|
+
);
|
|
302
|
+
assert_eq!(node.calls.load(Ordering::SeqCst), 1);
|
|
303
|
+
assert!(wrong.is_empty(), "caller protobuf statuses: {wrong:#?}");
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
#[tokio::test]
|
|
307
|
+
async fn should_preserve_valid_unknown_fields_and_stream_termination() {
|
|
308
|
+
let node = Node::new().await;
|
|
309
|
+
let mut request = GetTransactionRequest {
|
|
310
|
+
id: "transaction".to_owned(),
|
|
311
|
+
}
|
|
312
|
+
.encode_to_vec();
|
|
313
|
+
request.extend_from_slice(&[0xf8, 0x07, 0x01]);
|
|
314
|
+
let response = node.request(CORE_UNARY, frame(&request), None).await;
|
|
315
|
+
assert_eq!(response.status.code(), Code::Ok);
|
|
316
|
+
assert_eq!(
|
|
317
|
+
GetTransactionResponse::decode(&response.data[5..]).expect("valid response"),
|
|
318
|
+
GetTransactionResponse::default()
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
let response = node
|
|
322
|
+
.request(
|
|
323
|
+
CORE_STREAM,
|
|
324
|
+
frame(&TransactionsWithProofsRequest::default().encode_to_vec()),
|
|
325
|
+
None,
|
|
326
|
+
)
|
|
327
|
+
.await;
|
|
328
|
+
assert_eq!(response.status.code(), Code::Ok);
|
|
329
|
+
assert_eq!(
|
|
330
|
+
response.data.len(),
|
|
331
|
+
5,
|
|
332
|
+
"one empty protobuf item and clean stream completion"
|
|
333
|
+
);
|
|
334
|
+
let response = node
|
|
335
|
+
.request(
|
|
336
|
+
DRIVE_UNARY,
|
|
337
|
+
frame(&GetProofsRequest::default().encode_to_vec()),
|
|
338
|
+
None,
|
|
339
|
+
)
|
|
340
|
+
.await;
|
|
341
|
+
assert_eq!(response.status.code(), Code::Ok);
|
|
342
|
+
assert_eq!(
|
|
343
|
+
GetProofsResponse::decode(&response.data[5..]).expect("valid response"),
|
|
344
|
+
GetProofsResponse::default()
|
|
345
|
+
);
|
|
346
|
+
assert_eq!(node.calls.load(Ordering::SeqCst), 3);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
#[tokio::test]
|
|
350
|
+
async fn should_preserve_framing_compression_and_message_limit_errors() {
|
|
351
|
+
let node = Node::new().await;
|
|
352
|
+
let cases = [
|
|
353
|
+
(vec![1, 0, 0, 0, 0], None, Code::Internal),
|
|
354
|
+
(vec![2, 0, 0, 0, 0], None, Code::Internal),
|
|
355
|
+
(vec![0, 0, 0, 0, 2, 0], None, Code::Internal),
|
|
356
|
+
(vec![], None, Code::Internal),
|
|
357
|
+
(frame(&[]), Some("unsupported"), Code::Unimplemented),
|
|
358
|
+
(frame(&[]), Some("gzip"), Code::Unimplemented),
|
|
359
|
+
(vec![0, 0, 8, 0, 1], None, Code::OutOfRange),
|
|
360
|
+
];
|
|
361
|
+
for (frame, encoding, expected) in cases {
|
|
362
|
+
let response = node.request(CORE_UNARY, frame, encoding).await;
|
|
363
|
+
assert_eq!(
|
|
364
|
+
response.status.code(),
|
|
365
|
+
expected,
|
|
366
|
+
"{}",
|
|
367
|
+
response.status.message()
|
|
368
|
+
);
|
|
369
|
+
assert!(response.data.is_empty());
|
|
370
|
+
}
|
|
371
|
+
assert_eq!(node.calls.load(Ordering::SeqCst), 0);
|
|
372
|
+
}
|