grid-sdk 0.3.5

Hyperledger Grid is a platform for building supply chain solutions that include distributed ledger components. It provides a growing set of tools that accelerate development for supply chain smart contractsand client interfaces.
Documentation
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright 2018-2021 Cargill Incorporated
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::pin::Pin;
use std::str::FromStr;

use futures::prelude::*;
use protobuf::Message;
use reqwest::{Client, Error, Response, StatusCode};
use sawtooth_sdk::messages::batch::Batch;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde_json;

use super::{
    BackendClient, BackendClientError, BatchStatus, BatchStatusLink, BatchStatuses,
    InvalidTransaction, SubmitBatches,
};

macro_rules! try_fut {
    ($try_expr:expr) => {
        match $try_expr {
            Ok(res) => res,
            Err(err) => return futures::future::err(err).boxed(),
        }
    };
}

#[derive(Deserialize)]
pub struct SplinterErrorResponse {
    pub message: String,
}

#[derive(Deserialize)]
pub struct SplinterBatchLink {
    pub link: String,
}

#[derive(Clone)]
pub struct SplinterBackendClient {
    node_url: String,
    authorization: String,
}

impl SplinterBackendClient {
    /// Constructs a new splinter BackendClient instance, using the given url for the node's REST
    /// API.
    pub fn new(node_url: String, authorization: String) -> Self {
        Self {
            node_url,
            authorization,
        }
    }
}

type BatchStatusResponse =
    Pin<Box<dyn Future<Output = Result<Vec<BatchStatus>, BackendClientError>> + Send>>;

type BatchSubmitResponse =
    Pin<Box<dyn Future<Output = Result<BatchStatusLink, BackendClientError>> + Send>>;

pub fn handle_splinter_response<T: DeserializeOwned, R>(
    future: impl Future<Output = Result<Response, Error>> + Send + 'static,
    map: impl FnOnce(T) -> R + Send + 'static,
) -> Pin<Box<dyn Future<Output = Result<R, BackendClientError>> + Send>> {
    future
        .then(|res| match res {
            Ok(res) => future::join(future::ok(res.status()), res.bytes()).boxed(),
            Err(err) => future::join(
                future::err(BackendClientError::InternalError(format!(
                    "Unable to make request to Splinter: {}",
                    err
                ))),
                future::err(err),
            )
            .boxed(),
        })
        .map(|(status, bytes)| {
            let bytes = bytes.map_err(|err| {
                BackendClientError::InternalError(format!(
                    "Error reading bytes from Splinter: {err}",
                ))
            })?;

            match status? {
                StatusCode::ACCEPTED | StatusCode::OK => {
                    serde_json::from_slice(&bytes).map(map).map_err(|err| {
                        BackendClientError::InternalError(format!(
                            "Encountered error \"{err}\" while deserializing \
                                        Splinter response: {resp}",
                            resp = String::from_utf8_lossy(&bytes)
                        ))
                    })
                }
                status => {
                    let error: SplinterErrorResponse =
                        serde_json::from_slice(&bytes).map_err(|err| {
                            BackendClientError::InternalError(format!(
                                "Encountered error \"{err}\" while deserializing \
                                    Splinter error response: {resp}",
                                resp = String::from_utf8_lossy(&bytes)
                            ))
                        })?;

                    Err(BackendClientError::BadRequestError(format!(
                        "Splinter responded with {status}: {message}",
                        message = error.message
                    )))
                }
            }
        })
        .boxed()
}

impl BackendClient for SplinterBackendClient {
    fn submit_batches(&self, msg: SubmitBatches) -> BatchSubmitResponse {
        let service_arg = try_fut!(msg.service_id.ok_or_else(|| {
            BackendClientError::BadRequestError("A service id must be provided".into())
        }));

        let service_info = try_fut!(SplinterService::from_str(&service_arg));

        let url = format!(
            "{}/scabbard/{}/{}/batches",
            self.node_url, service_info.circuit_id, service_info.service_id
        );

        let batch_list_bytes = try_fut!(msg.batch_list.write_to_bytes().map_err(|err| {
            BackendClientError::BadRequestError(format!("Malformed batch list: {}", err))
        }));

        let batch_query = msg
            .batch_list
            .get_batches()
            .iter()
            .map(Batch::get_header_signature)
            .collect::<Vec<_>>()
            .join(",");
        let mut response_url = msg.response_url;
        response_url.set_query(Some(&format!("id={}", batch_query)));
        let link = response_url.to_string();

        handle_splinter_response(
            Client::new()
                .post(&url)
                .header("GridProtocolVersion", "1")
                .header("Content-Type", "octet-stream")
                .header("Authorization", &self.authorization.to_string())
                .body(batch_list_bytes)
                .send(),
            |_: SplinterBatchLink| BatchStatusLink { link },
        )
    }

    fn batch_status(&self, msg: BatchStatuses) -> BatchStatusResponse {
        let service_arg = try_fut!(msg.service_id.ok_or_else(|| {
            BackendClientError::BadRequestError("A service id must be provided".into())
        }));

        let service_info = try_fut!(SplinterService::from_str(&service_arg));

        // {base_url}/scabbard/{circuit_id}/{service_id}/batch_statuses?[wait={time}&]ids={batch_ids}
        let mut url = self.node_url.clone();
        url.push_str("/scabbard/");
        url.push_str(&service_info.circuit_id);
        url.push('/');
        url.push_str(&service_info.service_id);
        url.push_str("/batch_statuses?");

        if let Some(wait_time) = msg.wait {
            url.push_str("wait=");
            url.push_str(&wait_time.to_string());
            url.push('&');
        }

        url.push_str("ids=");
        url.push_str(&msg.batch_ids.join(","));

        handle_splinter_response(
            Client::new()
                .get(&url)
                .header("GridProtocolVersion", "1")
                .header("Authorization", &self.authorization.to_string())
                .send(),
            |stats: Vec<SplinterBatchStatus>| {
                stats.into_iter().map(|status| status.into()).collect()
            },
        )
    }

    fn clone_box(&self) -> Box<dyn BackendClient> {
        Box::new(self.clone())
    }
}

#[derive(Deserialize, Debug)]
struct SplinterBatchStatus {
    id: String,
    status: Status,
}

#[derive(Deserialize, Debug)]
struct Status {
    #[serde(rename(deserialize = "statusType"))]
    status_type: String,
    message: Vec<ErrorMessage>,
}

#[derive(Deserialize, Debug)]
struct ErrorMessage {
    transaction_id: String,
    error_message: Option<String>,
    error_data: Option<Vec<u8>>,
}

impl From<SplinterBatchStatus> for BatchStatus {
    fn from(batch_status: SplinterBatchStatus) -> Self {
        Self {
            id: batch_status.id,
            status: batch_status.status.status_type,
            invalid_transactions: batch_status
                .status
                .message
                .into_iter()
                .filter(|message| message.error_message.is_some() && message.error_data.is_some())
                .map(|message| InvalidTransaction {
                    id: message.transaction_id,
                    message: message.error_message.unwrap(),
                    extended_data: base64::encode(&message.error_data.unwrap()),
                })
                .collect(),
        }
    }
}

struct SplinterService {
    circuit_id: String,
    service_id: String,
}

impl FromStr for SplinterService {
    type Err = BackendClientError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut parts = s.split("::");
        let circuit_id: String = parts
            .next()
            .ok_or_else(|| {
                BackendClientError::BadRequestError("Empty service_id parameter provided".into())
            })?
            .into();
        let service_id: String = parts
            .next()
            .ok_or_else(|| {
                BackendClientError::BadRequestError(
                    "Must provide a fully-qualified service_id: <circuit_id>::<service_id>".into(),
                )
            })?
            .into();

        Ok(Self {
            circuit_id,
            service_id,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mockito::{self, Matcher, Mock};
    use pretty_assertions::assert_eq;
    use sawtooth_sdk::messages::batch::BatchList;
    use url::Url;

    const TEST_CIRCUIT_ID: &str = "z7499-QGFd3";
    const TEST_SERVICE_ID: &str = "gsAA";
    const TEST_AUTHORIZATION: &str = "foo";
    const TEST_BATCH_ID: &str = "one";
    const TEST_SUCCESS_STATUS_RESPONSE: &str = r#"[
    {
        "id": "one",
        "status": {
            "statusType": "sampleStatusType",
            "message": []
        }
    }
]"#;
    const TEST_SUCCESS_SUBMIT_RESPONSE: &str = r#"{
        "link": "https://test/link?ids=1,2,3"
    }"#;

    fn splinter_error_response() -> String {
        format!(
            "{{\"message\":\"scabbard service {TEST_SERVICE_ID} \
                on circuit {TEST_CIRCUIT_ID} not found\"}}"
        )
    }

    fn setup_basic_batch_statuses_request() -> (Mock, BatchStatusResponse) {
        let mock_endpoint = mockito::mock(
            "GET",
            Matcher::Exact(format!(
                "/scabbard/{TEST_CIRCUIT_ID}/\
                {TEST_SERVICE_ID}/batch_statuses?ids={TEST_BATCH_ID}"
            )),
        );

        let response =
            SplinterBackendClient::new(mockito::server_url(), TEST_AUTHORIZATION.to_string())
                .batch_status(BatchStatuses {
                    batch_ids: vec![TEST_BATCH_ID.to_string()],
                    wait: None,
                    service_id: Some(format!("{TEST_CIRCUIT_ID}::{TEST_SERVICE_ID}")),
                });

        (mock_endpoint, response)
    }

    #[tokio::test]
    async fn batch_statuses_returns_useful_message_on_404() {
        let (endpoint, response) = setup_basic_batch_statuses_request();

        let endpoint = endpoint
            .with_status(404)
            .with_body(splinter_error_response())
            .create();

        let result = response.await;

        endpoint.assert();
        assert_eq!(
            format!("{:?}", result),
            "Err(BadRequestError(\"Splinter \
            responded with 404 Not Found: scabbard service \
            gsAA on circuit z7499-QGFd3 not found\"))"
        );
    }

    #[tokio::test]
    async fn batch_statuses_returns_correctly_on_200_success() {
        let (endpoint, response) = setup_basic_batch_statuses_request();

        let endpoint = endpoint
            .with_status(200)
            .with_body(TEST_SUCCESS_STATUS_RESPONSE)
            .create();

        let result = response.await;

        endpoint.assert();
        assert_eq!(
            format!("{:?}", result),
            "Ok([BatchStatus { id: \"one\", invalid_transactions: [], status: \
            \"sampleStatusType\" }])"
        );
    }

    #[tokio::test]
    async fn batch_statuses_returns_useful_message_on_200_deserialize_error() {
        let (endpoint, response) = setup_basic_batch_statuses_request();

        let endpoint = endpoint.with_status(200).with_body("bad json").create();

        let result = response.await;

        endpoint.assert();
        assert_eq!(
            format!("{:?}", result),
            "Err(InternalError(\"Encountered \
            error \\\"expected value at line 1 column 1\\\" while deserializing \
            Splinter response: bad json\"))"
        );
    }

    #[tokio::test]
    async fn batch_statuses_returns_useful_message_on_503_deserialize_error() {
        let (endpoint, response) = setup_basic_batch_statuses_request();

        let endpoint = endpoint.with_status(503).with_body("bad json").create();

        let result = response.await;

        endpoint.assert();
        assert_eq!(
            format!("{:?}", result),
            "Err(InternalError(\"Encountered error \\\"expected value at line 1 column 1\\\" \
            while deserializing Splinter error response: bad json\"))"
        );
    }

    fn setup_basic_batches_request() -> (Mock, BatchSubmitResponse) {
        let mock_endpoint = mockito::mock(
            "POST",
            Matcher::Exact(format!(
                "/scabbard/{TEST_CIRCUIT_ID}/\
                {TEST_SERVICE_ID}/batches"
            )),
        );

        let response_url = Url::parse("https://localhost:8080/").expect("could not parse url");

        let response =
            SplinterBackendClient::new(mockito::server_url(), TEST_AUTHORIZATION.to_string())
                .submit_batches(SubmitBatches {
                    batch_list: BatchList::default(),
                    response_url,
                    service_id: Some(format!("{TEST_CIRCUIT_ID}::{TEST_SERVICE_ID}")),
                });

        (mock_endpoint, response)
    }

    #[tokio::test]
    async fn submit_batches_returns_correctly_on_200_success() {
        let (endpoint, response) = setup_basic_batches_request();

        let endpoint = endpoint
            .with_status(202)
            .with_body(TEST_SUCCESS_SUBMIT_RESPONSE)
            .create();

        let result = response.await;

        assert_eq!(
            format!("{:?}", result),
            "Ok(BatchStatusLink { link: \"https://localhost:8080/?id=\" })"
        );
        endpoint.assert();
    }

    #[tokio::test]
    async fn submit_batches_returns_useful_message_on_404() {
        let (endpoint, response) = setup_basic_batches_request();

        let endpoint = endpoint
            .with_status(404)
            .with_body(splinter_error_response())
            .create();

        let result = response.await;

        endpoint.assert();
        assert_eq!(
            format!("{:?}", result),
            "Err(BadRequestError(\"Splinter \
            responded with 404 Not Found: scabbard service \
            gsAA on circuit z7499-QGFd3 not found\"))"
        );
    }
}