splinter 0.3.14

Splinter is a privacy-focused platform for distributed applications that provides a blockchain-inspired networking environment for communication and transactions between organizations.
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Copyright 2018-2020 Cargill Incorporated
// Copyright 2018 Bitwise IO, Inc.
//
// 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::fmt;
use std::fs::File;
use std::ops::{Deref, DerefMut};

use atomicwrites::{AllowOverwrite, AtomicFile};
use serde::de::DeserializeOwned;
use serde::Serialize;

use super::{Storage, StorageReadGuard, StorageWriteGuard};

/// A yaml read guard
pub struct YamlStorageReadGuard<'a, T: Serialize + DeserializeOwned + 'a> {
    storage: &'a YamlStorage<T>,
}

impl<'a, T: Serialize + DeserializeOwned> YamlStorageReadGuard<'a, T> {
    fn new(storage: &'a YamlStorage<T>) -> Self {
        Self { storage }
    }
}

impl<'a, T: Serialize + DeserializeOwned + 'a> Deref for YamlStorageReadGuard<'a, T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.storage.data
    }
}

impl<'a, T: 'a + Serialize + DeserializeOwned + fmt::Display> fmt::Display
    for YamlStorageReadGuard<'a, T>
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (**self).fmt(f)
    }
}

impl<'a, T: 'a + Serialize + DeserializeOwned> StorageReadGuard<'a, T>
    for YamlStorageReadGuard<'a, T>
{
}

/// A yaml write guard
pub struct YamlStorageWriteGuard<'a, T: Serialize + DeserializeOwned + 'a> {
    storage: &'a mut YamlStorage<T>,
}

impl<'a, T: Serialize + DeserializeOwned> YamlStorageWriteGuard<'a, T> {
    fn new(storage: &'a mut YamlStorage<T>) -> Self {
        Self { storage }
    }
}

impl<'a, T: Serialize + DeserializeOwned> Drop for YamlStorageWriteGuard<'a, T> {
    fn drop(&mut self) {
        self.storage
            .file
            .write(|f| serde_yaml::to_writer(f, &self.storage.data))
            .expect("File write failed while dropping YamlStorageWriteGuard!");
    }
}

impl<'a, T: Serialize + DeserializeOwned + 'a> Deref for YamlStorageWriteGuard<'a, T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.storage.data
    }
}

impl<'a, T: Serialize + DeserializeOwned + 'a> DerefMut for YamlStorageWriteGuard<'a, T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.storage.data
    }
}

impl<'a, T: 'a + Serialize + DeserializeOwned + fmt::Display> fmt::Display
    for YamlStorageWriteGuard<'a, T>
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (**self).fmt(f)
    }
}

impl<'a, T: 'a + Serialize + DeserializeOwned> StorageWriteGuard<'a, T>
    for YamlStorageWriteGuard<'a, T>
{
}

// A Yaml Storage implementation
///
/// File writes are atomic
pub struct YamlStorage<T: Serialize + DeserializeOwned> {
    data: T,
    file: AtomicFile,
}

impl<T: Serialize + DeserializeOwned> YamlStorage<T> {
    pub fn new<P: Into<String>, F: Fn() -> T>(path: P, default: F) -> Result<Self, String> {
        let path = path.into();

        let file = AtomicFile::new(path, AllowOverwrite);

        // Read the file first, to see if there's any existing data
        let data = match File::open(file.path()) {
            Ok(f) => {
                serde_yaml::from_reader(f).map_err(|err| format!("Couldn't read file: {}", err))?
            }
            Err(_) => {
                let data = default();

                file.write(|f| serde_yaml::to_writer(f, &data))
                    .map_err(|err| format!("File write failed: {}", err))?;

                data
            }
        };

        // Then open the file again and truncate, preparing it to be written to
        Ok(Self { data, file })
    }
}

impl<T: fmt::Display + Serialize + DeserializeOwned> fmt::Display for YamlStorage<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (*self).data.fmt(f)
    }
}

impl<T: Serialize + DeserializeOwned> Storage for YamlStorage<T> {
    type S = T;

    fn read<'a>(&'a self) -> Box<dyn StorageReadGuard<'a, T, Target = T> + 'a> {
        Box::new(YamlStorageReadGuard::new(self))
    }

    fn write<'a>(&'a mut self) -> Box<dyn StorageWriteGuard<'a, T, Target = T> + 'a> {
        Box::new(YamlStorageWriteGuard::new(self))
    }
}

#[cfg(test)]
mod tests {
    use std::io::Write;
    use std::path::PathBuf;

    use tempdir::TempDir;

    use super::*;
    use crate::circuit::directory::CircuitDirectory;
    use crate::circuit::service::SplinterNode;
    use crate::circuit::{AuthorizationType, Circuit, DurabilityType, PersistenceType, RouteType};

    /* Creates a state file that looks like the following:
        ---
        nodes:
          123:
            endpoints:
              - "tcp://1.2.3.4:1234"
              - "inproc://127.0.0.1:100001"
        circuits:
          alpha:
            auth: trust
            members:
              - "123"
            roster:
              - service_id: abc
                service_type: test_service
                allowed_nodes:
                  - "*"
                arguments:
                  test_arg: test_value
              - service_id: def
                service_type: test_service
                allowed_nodes:
                  - "*"
                arguments:
                  test_arg: test_value
            persistence: any
            durability: none
            routes: require_direct
            circuit_management_type: state_test_app
    */
    fn set_up_mock_state_file(mut temp_dir: PathBuf) -> String {
        // Create mock state
        let mut state = CircuitDirectory::new();
        let node = SplinterNode::new("123".into(), vec!["tcp://127.0.0.1:8000".into()]);
        state.add_node("123".into(), node);

        let circuit = Circuit::builder()
            .with_id("alpha".into())
            .with_auth(AuthorizationType::Trust)
            .with_members(vec!["123".into()])
            .with_roster(vec!["abc".into(), "def".into()])
            .with_persistence(PersistenceType::Any)
            .with_durability(DurabilityType::NoDurability)
            .with_routes(RouteType::Any)
            .with_circuit_management_type("state_test_app".into())
            .build()
            .expect("Should have built a correct circuit");

        state.add_circuit("alpha".into(), circuit);

        let state_string = serde_yaml::to_string(&state).unwrap();

        // Creat the temp file
        temp_dir.push("circuits.yaml");
        let path = temp_dir.to_str().unwrap().to_string();

        // Write out the mock state file to the temp directory
        let mut file = File::create(path.to_string()).unwrap();
        file.write_all(state_string.as_bytes()).unwrap();
        path
    }

    /* Creates a state file that looks like the following:
        ---
        nodes:
        circuits:
    */
    fn setup_empty_state_file(mut temp_dir: PathBuf) -> String {
        // Create empty CircuitDirectory object
        let state = CircuitDirectory::new();

        let state_string = serde_yaml::to_string(&state).unwrap();

        // Creat the temp file
        temp_dir.push("circuits.yaml");
        let path = temp_dir.to_str().unwrap().to_string();

        // Write out the mock state file to the temp directory
        let mut file = File::create(path.to_string()).unwrap();
        file.write_all(state_string.as_bytes()).unwrap();
        path
    }

    #[test]
    /* Test that an empty state is properly loaded and returns a YamlStorage with CircuitDirectory
       object that contains no nodes or circuits. The empty state file looks like the following:

       ---
       nodes:
       circuits:
    */
    fn test_load_empty_state() {
        // create temp directoy
        let temp_dir = TempDir::new("test_empty_state").unwrap();
        let temp_dir_path = temp_dir.path().to_path_buf();

        // setup empty state file
        let path = setup_empty_state_file(temp_dir_path);

        // load empty state file into the yaml storage
        let storage = YamlStorage::new(path, CircuitDirectory::new).unwrap();

        // check that state does not have any nodes or circuits
        assert!(storage.data.nodes().is_empty());
        assert!(storage.data.circuits().is_empty());
    }

    #[test]
    // Test that if the state file does not exist, it is created as an empty state.
    fn test_load_no_state() {
        // create temp directoy
        let temp_dir = TempDir::new("test_load_no_state").unwrap();
        let mut temp_dir_path = temp_dir.path().to_path_buf();
        temp_dir_path.push("circuits.yaml");
        let path = temp_dir_path.to_str().unwrap().to_string();

        // create state file empty state when file does not exist
        let storage = YamlStorage::new(path, CircuitDirectory::new).unwrap();

        // check that state does not have any nodes or circuits
        assert!(storage.data.nodes().is_empty());
        assert!(storage.data.circuits().is_empty());
    }

    #[test]
    /* Test that CircuitDirectory object is properly loaded into YamlStorage from a state yaml
       file that looks like the following:

       ---
       nodes:
         123:
           endpoints:
             - "tcp://1.2.3.4:1234"
             - "inproc://127.0.0.1:100001"
       circuits:
         alpha:
           auth: trust
           members:
             - "123"
           services:
             - abc
             - def
           persistence: any
           durability: none
           routes: require_direct
    */
    fn test_load_state() {
        // create temp directoy
        let temp_dir = TempDir::new("test_load_state").unwrap();
        let temp_dir_path = temp_dir.path().to_path_buf();

        // setup mock state file
        let path = set_up_mock_state_file(temp_dir_path);

        // load state file into yaml storage
        let storage = YamlStorage::new(path, CircuitDirectory::new).unwrap();

        // check that the CircuitDirectory data contains the correct node and circuit
        assert_eq!(storage.data.nodes().len(), 1);
        assert_eq!(storage.data.circuits().len(), 1);
        assert!(storage.data.nodes().contains_key("123"));
        assert!(storage.data.circuits().contains_key("alpha"));

        assert_eq!(
            storage
                .data
                .nodes()
                .get("123")
                .unwrap()
                .endpoints()
                .to_vec(),
            vec!["tcp://127.0.0.1:8000".to_string()]
        );

        assert_eq!(
            storage
                .data
                .circuits()
                .get("alpha")
                .unwrap()
                .roster()
                .to_vec(),
            vec!["abc".into(), "def".into()]
        );

        assert_eq!(
            storage
                .data
                .circuits()
                .get("alpha")
                .unwrap()
                .members()
                .to_vec(),
            vec!["123".to_string()],
        );

        assert_eq!(
            storage
                .data
                .circuits()
                .get("alpha")
                .unwrap()
                .circuit_management_type(),
            "state_test_app"
        );
    }

    #[test]
    // Using the mock state file as a starting point, test that a new node can be properly
    // added to the state file. CircuitDirectory is then loaded into yaml storage and verified
    // that the added node is there.
    fn test_write_node_state() {
        // create temp directoy
        let temp_dir = TempDir::new("test_write_node").unwrap();
        let temp_dir_path = temp_dir.path().to_path_buf();

        // setup mock state file
        let path = set_up_mock_state_file(temp_dir_path);
        {
            // load state file into yaml storage
            let mut storage = YamlStorage::new(path.clone(), CircuitDirectory::new).unwrap();

            // add new node to state
            let node = SplinterNode::new("123".into(), vec!["tcp://127.0.0.1:5000".into()]);
            storage.write().add_node("777".into(), node);

            //drop storage
        }

        // load state file into yaml storage
        let storage = YamlStorage::new(path, CircuitDirectory::new).unwrap();
        // check that the CircuitDirectory data contains the new node
        assert_eq!(storage.data.nodes().len(), 2);
        assert_eq!(storage.data.circuits().len(), 1);
        assert!(storage.data.nodes().contains_key("123"));
        assert!(storage.data.nodes().contains_key("777"));

        assert_eq!(
            storage
                .data
                .nodes()
                .get("123")
                .unwrap()
                .endpoints()
                .to_vec(),
            vec!["tcp://127.0.0.1:8000".to_string()]
        );

        assert_eq!(
            storage
                .data
                .nodes()
                .get("777")
                .unwrap()
                .endpoints()
                .to_vec(),
            vec!["tcp://127.0.0.1:5000".to_string()]
        );
    }

    #[test]
    // Using the mock state file as a starting point, test that node 123 can be properly
    // removed to the state file. CircuitDirectory is then loaded into yaml storage and verified
    // that node 123 has been removed. Verify that circuit alpha is still there.
    fn test_remove_node_from_state() {
        // create temp directoy
        let temp_dir = TempDir::new("test_write_circuit").unwrap();
        let temp_dir_path = temp_dir.path().to_path_buf();
        // setup mock state file
        let path = set_up_mock_state_file(temp_dir_path);
        {
            // load state file into yaml storage
            let mut storage = YamlStorage::new(path.clone(), CircuitDirectory::new).unwrap();

            storage.write().remove_node("123".into());

            // drop storage
        }
        // load state file into yaml storage
        let storage = YamlStorage::new(path.clone(), CircuitDirectory::new).unwrap();

        // check that the CircuitDirectory data does not contain node 123
        assert_eq!(storage.data.nodes().len(), 0);
        assert_eq!(storage.data.circuits().len(), 1);
        assert!(!storage.data.nodes().contains_key("123"));
        assert!(storage.data.circuits().contains_key("alpha"));

        assert_eq!(
            storage
                .data
                .circuits()
                .get("alpha")
                .unwrap()
                .roster()
                .to_vec(),
            vec!["abc".into(), "def".into()]
        );

        assert_eq!(
            storage
                .data
                .circuits()
                .get("alpha")
                .unwrap()
                .members()
                .to_vec(),
            vec!["123".to_string()],
        );
    }

    #[test]
    // Using the mock state file as a starting point, test that a new circuit can be properly
    // added to the state file. CircuitDirectory is then loaded into yaml storage and verified
    // that the added circuit is there.
    fn test_write_circuit_directory() {
        // create temp directoy
        let temp_dir = TempDir::new("test_write_circuit").unwrap();
        let temp_dir_path = temp_dir.path().to_path_buf();
        // setup mock state file
        let path = set_up_mock_state_file(temp_dir_path);
        {
            // load state file into yaml storage
            let mut storage = YamlStorage::new(path.clone(), CircuitDirectory::new).unwrap();
            let circuit = Circuit::builder()
                .with_id("alpha".into())
                .with_auth(AuthorizationType::Trust)
                .with_members(vec!["456".into(), "789".into()])
                .with_roster(vec!["qwe".into(), "rty".into(), "uio".into()])
                .with_persistence(PersistenceType::Any)
                .with_durability(DurabilityType::NoDurability)
                .with_routes(RouteType::Any)
                .with_circuit_management_type("state_write_test_app".into())
                .build()
                .expect("Should have built a correct circuit");

            storage.write().add_circuit("beta".into(), circuit);

            //drop storage
        }

        // load state file into yaml storage
        let storage = YamlStorage::new(path.clone(), CircuitDirectory::new).unwrap();

        // check that the CircuitDirectory data contains the new circuit
        assert_eq!(storage.data.circuits().len(), 2);
        assert!(storage.data.circuits().contains_key("alpha"));
        assert!(storage.data.circuits().contains_key("beta"));

        assert_eq!(
            storage
                .data
                .circuits()
                .get("alpha")
                .unwrap()
                .roster()
                .to_vec(),
            vec!["abc".into(), "def".into()]
        );

        assert_eq!(
            storage
                .data
                .circuits()
                .get("alpha")
                .unwrap()
                .members()
                .to_vec(),
            vec!["123".to_string()],
        );

        assert_eq!(
            storage
                .data
                .circuits()
                .get("beta")
                .unwrap()
                .roster()
                .to_vec(),
            vec!["qwe".into(), "rty".into(), "uio".into()]
        );

        assert_eq!(
            storage
                .data
                .circuits()
                .get("beta")
                .unwrap()
                .members()
                .to_vec(),
            vec!["456".to_string(), "789".to_string()],
        );

        assert_eq!(
            storage
                .data
                .circuits()
                .get("beta")
                .unwrap()
                .circuit_management_type(),
            "state_write_test_app"
        );
    }

    #[test]
    // Using the mock state file as a starting point, test that circuit alpha can be properly
    // removed to the state file. CircuitDirectory is then loaded into yaml storage and verified
    // that circuit alpha has been removed. Verify that node 123 is still there.
    fn test_remove_circuit_from_state() {
        // create temp directoy
        let temp_dir = TempDir::new("test_write_circuit").unwrap();
        let temp_dir_path = temp_dir.path().to_path_buf();
        // setup mock state file
        let path = set_up_mock_state_file(temp_dir_path);
        {
            // load state file into yaml storage
            let mut storage = YamlStorage::new(path.clone(), CircuitDirectory::new).unwrap();
            storage.write().remove_circuit("alpha".into());

            // drop storage
        }

        // load state file into yaml storage
        let storage = YamlStorage::new(path.clone(), CircuitDirectory::new).unwrap();

        // check that the CircuitDirectory data does not contain cirucit alpha
        assert_eq!(storage.data.nodes().len(), 1);
        assert_eq!(storage.data.circuits().len(), 0);
        assert!(storage.data.nodes().contains_key("123"));
        assert!(!storage.data.circuits().contains_key("alpha"));

        assert_eq!(
            storage
                .data
                .nodes()
                .get("123")
                .unwrap()
                .endpoints()
                .to_vec(),
            vec!["tcp://127.0.0.1:8000".to_string()]
        );
    }
}