splinter 0.6.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
// Copyright 2018-2022 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.

//! Implementation of the circuit routing table reader and writer traits that uses a read/write
//! lock
//!
//! The public interface includes the structs [`RoutingTable`].
//!
//! [`RoutingTable`]: struct.RoutingTable.html

#[cfg(all(feature = "benchmark", test))]
mod benchmarks;

use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, RwLock};

use super::error::RoutingTableReaderError;
use super::AuthorizationType;
use super::{
    Circuit, CircuitIter, CircuitNode, CircuitNodeIter, RoutingTableReader, RoutingTableWriter,
    Service, ServiceId,
};

use crate::error::{InternalError, InvalidStateError};

const ADMIN_CIRCUIT_ID: &str = "admin";

/// The internal state of the routing table that will be wrapped in a read-write lock
#[derive(Clone, Default)]
struct RoutingTableState {
    /// Nodes that are listed in a set of circuits
    nodes: BTreeMap<String, CircuitNode>,
    /// The collection of circuits to be used for routing
    circuits: BTreeMap<String, Circuit>,
    /// Service ID to Service that contains the node the service is connected to. Not persisted.
    service_directory: HashMap<ServiceId, Service>,
}

// An implementation of a routing table that uses a read-write lock to wrap the state.
#[derive(Clone, Default)]
pub struct RoutingTable {
    state: Arc<RwLock<RoutingTableState>>,
}

impl RoutingTableReader for RoutingTable {
    // ---------- methods to access service directory ----------
    /// Returns the service with the provided ID
    ///
    /// # Arguments
    ///
    /// * `service_id` - The unique ID for the service to be fetched
    ///
    /// Returns an error if the lock is poisoned.
    fn get_service(
        &self,
        service_id: &ServiceId,
    ) -> Result<Option<Service>, RoutingTableReaderError> {
        Ok(self
            .state
            .read()
            .map_err(|_| {
                RoutingTableReaderError::InternalError(InternalError::with_message(String::from(
                    "RoutingTable lock poisoned",
                )))
            })?
            .service_directory
            .get(service_id)
            .map(Service::clone))
    }

    /// Returns all the services for the provided circuit
    ///
    /// # Arguments
    ///
    /// * `circuit_id` - The unique ID the circuit whose services should be returned
    ///
    /// Returns an error if the lock is poisoned or if the circuit does not exist
    fn list_services(&self, circuit_id: &str) -> Result<Vec<Service>, RoutingTableReaderError> {
        if let Some(circuit) = self
            .state
            .read()
            .map_err(|_| {
                RoutingTableReaderError::InternalError(InternalError::with_message(String::from(
                    "RoutingTable lock poisoned",
                )))
            })?
            .circuits
            .get(circuit_id)
        {
            Ok(circuit.roster.clone())
        } else {
            Err(RoutingTableReaderError::InvalidStateError(
                InvalidStateError::with_message(format!("Circuit {} was not found", circuit_id)),
            ))
        }
    }

    // ---------- methods to access circuit directory ----------

    /// Returns the nodes in the routing table
    ///
    /// Returns an error if the lock is poisoned
    fn list_nodes(&self) -> Result<CircuitNodeIter, RoutingTableReaderError> {
        Ok(Box::new(
            self.state
                .read()
                .map_err(|_| {
                    RoutingTableReaderError::InternalError(InternalError::with_message(
                        String::from("RoutingTable lock poisoned"),
                    ))
                })?
                .nodes
                .clone()
                .into_iter(),
        ))
    }

    /// Returns the node with the provided ID
    ///
    /// # Arguments
    ///
    /// * `node_id` - The unique ID for the node to be fetched
    ///
    /// Returns an error if the lock was poisoned
    fn get_node(&self, node_id: &str) -> Result<Option<CircuitNode>, RoutingTableReaderError> {
        Ok(self
            .state
            .read()
            .map_err(|_| {
                RoutingTableReaderError::InternalError(InternalError::with_message(String::from(
                    "RoutingTable lock poisoned",
                )))
            })?
            .nodes
            .get(node_id)
            .cloned())
    }

    /// Returns the circuits in the routing table
    ///
    /// Returns an error if the lock is poisoned
    fn list_circuits(&self) -> Result<CircuitIter, RoutingTableReaderError> {
        Ok(Box::new(
            self.state
                .read()
                .map_err(|_| {
                    RoutingTableReaderError::InternalError(InternalError::with_message(
                        String::from("RoutingTable lock poisoned"),
                    ))
                })?
                .circuits
                .clone()
                .into_iter(),
        ))
    }

    /// Returns the circuit with the provided ID
    ///
    /// # Arguments
    ///
    /// * `circuit_id` - The unique ID for the circuit to be fetched
    ///
    /// Returns an error if the lock is poisoned
    fn get_circuit(&self, circuit_id: &str) -> Result<Option<Circuit>, RoutingTableReaderError> {
        if circuit_id == ADMIN_CIRCUIT_ID {
            Ok(Some(Circuit::new(
                ADMIN_CIRCUIT_ID.to_string(),
                vec![],
                vec![],
                AuthorizationType::Trust,
            )))
        } else {
            Ok(self
                .state
                .read()
                .map_err(|_| {
                    RoutingTableReaderError::InternalError(InternalError::with_message(
                        String::from("RoutingTable lock poisoned"),
                    ))
                })?
                .circuits
                .get(circuit_id)
                .cloned())
        }
    }

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

impl RoutingTableWriter for RoutingTable {
    /// Adds a new service to the routing table
    ///
    /// # Arguments
    ///
    /// * `service_id` - The unique ServiceId for the service
    /// * `service` - The service to be added to the routing table
    ///
    /// Returns an error if the lock is poisoned
    fn add_service(
        &mut self,
        service_id: ServiceId,
        service: Service,
    ) -> Result<(), InternalError> {
        self.state
            .write()
            .map_err(|_| InternalError::with_message(String::from("RoutingTable lock poisoned")))?
            .service_directory
            .insert(service_id, service);
        Ok(())
    }

    /// Removes a service from the routing table if it exists
    ///
    /// # Arguments
    ///
    /// * `service_id` - The unique ServiceId for the service
    ///
    /// Returns an error if the lock is poisoned
    fn remove_service(&mut self, service_id: &ServiceId) -> Result<(), InternalError> {
        self.state
            .write()
            .map_err(|_| InternalError::with_message(String::from("RoutingTable lock poisoned")))?
            .service_directory
            .remove(service_id);
        Ok(())
    }

    /// Adds a new circuit to the routing table. Also adds the associated services and nodes.
    ///
    /// # Arguments
    ///
    /// * `circuit_id` - The unique ID for the circuit
    /// * `circuit` - The circuit to be added to the routing table
    /// * `nodes` - The list of circuit nodes that should be added along with the circuit
    ///
    /// Returns an error if the lock is poisoned
    fn add_circuit(
        &mut self,
        circuit_id: String,
        circuit: Circuit,
        nodes: Vec<CircuitNode>,
    ) -> Result<(), InternalError> {
        let mut state = self
            .state
            .write()
            .map_err(|_| InternalError::with_message(String::from("RoutingTable lock poisoned")))?;

        for service in circuit.roster.iter() {
            let service_id = ServiceId::new(
                circuit.circuit_id.to_string(),
                service.service_id.to_string(),
            );

            state.service_directory.insert(service_id, service.clone());
        }

        for node in nodes.into_iter() {
            if !state.nodes.contains_key(&node.node_id) {
                state.nodes.insert(node.node_id.to_string(), node);
            }
        }

        state.circuits.insert(circuit_id, circuit);
        Ok(())
    }

    /// Adds a list of circuits to the routing table. Also adds the associated services.
    ///
    /// # Arguments
    ///
    /// * `circuits` - The list of circuits to be added to the routing table
    ///
    /// Returns an error if the lock is poisoned
    fn add_circuits(&mut self, circuits: Vec<Circuit>) -> Result<(), InternalError> {
        let mut state = self
            .state
            .write()
            .map_err(|_| InternalError::with_message(String::from("RoutingTable lock poisoned")))?;
        for circuit in circuits.into_iter() {
            for service in circuit.roster.iter() {
                let service_id = ServiceId::new(
                    circuit.circuit_id.to_string(),
                    service.service_id.to_string(),
                );

                state.service_directory.insert(service_id, service.clone());
            }
            state
                .circuits
                .insert(circuit.circuit_id.to_string(), circuit);
        }
        Ok(())
    }

    /// Removes a circuit from the routing table if it exists. Also removes the associated
    /// services.
    ///
    /// # Arguments
    ///
    /// * `circuit_id` - The unique ID for the circuit
    ///
    /// Returns an error if the lock is poisoned
    fn remove_circuit(&mut self, circuit_id: &str) -> Result<(), InternalError> {
        let mut state = self
            .state
            .write()
            .map_err(|_| InternalError::with_message(String::from("RoutingTable lock poisoned")))?;

        let circuit = state.circuits.remove(circuit_id);

        if let Some(circuit) = circuit {
            for service in circuit.roster.iter() {
                let service_id = ServiceId::new(
                    circuit.circuit_id.to_string(),
                    service.service_id.to_string(),
                );

                state.service_directory.remove(&service_id);
            }
        }
        Ok(())
    }

    /// Adds a new node to the routing table
    ///
    /// # Arguments
    ///
    /// * `node_id` - The unique ID for the node
    /// * `node`- The node to add to the routing table
    ///
    /// Returns an error if the lock is poisoned
    fn add_node(&mut self, id: String, node: CircuitNode) -> Result<(), InternalError> {
        self.state
            .write()
            .map_err(|_| InternalError::with_message(String::from("RoutingTable lock poisoned")))?
            .nodes
            .insert(id, node);
        Ok(())
    }

    /// Adds a list of node to the routing table
    ///
    /// # Arguments
    ///
    /// * `nodes`- The list of nodes to add to the routing table
    ///
    /// Returns an error if the lock is poisoned
    fn add_nodes(&mut self, nodes: Vec<CircuitNode>) -> Result<(), InternalError> {
        let mut state = self
            .state
            .write()
            .map_err(|_| InternalError::with_message(String::from("RoutingTable lock poisoned")))?;
        for node in nodes {
            if !state.nodes.contains_key(&node.node_id) {
                state.nodes.insert(node.node_id.to_string(), node);
            }
        }
        Ok(())
    }

    /// Removes a node from the routing table if it exists
    ///
    /// # Arguments
    ///
    /// * `node_id` -  The unique ID for the node that should be removed
    ///
    /// Returns an error if the lock is poisoned
    fn remove_node(&mut self, id: &str) -> Result<(), InternalError> {
        self.state
            .write()
            .map_err(|_| InternalError::with_message(String::from("RoutingTable lock poisoned")))?
            .nodes
            .remove(id);
        Ok(())
    }

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

#[cfg(test)]
mod test {
    use super::*;

    use crate::circuit::routing::AuthorizationType;

    // Test the routing table read and write operations for circuits
    //
    // 1. Create circuits with corresponding nodes and services and write one circuit to the
    //    routing table
    // 2. Check that the circuit was written to the routing table
    // 3. Check that the expected circuit is returned when fetched
    // 4. Remove the circuit from the routing table
    // 5. Check that the 'circuits' field of the routing table is empty
    // 6. Write both circuits to the routing table
    // 7. Check that both circuits were written to the routing table
    // 8. list circuits, validate both circuits are returned
    #[test]
    fn test_circuit() {
        let routing_table = RoutingTable::default();
        let mut writer: Box<dyn RoutingTableWriter> = Box::new(routing_table.clone());
        let reader: Box<dyn RoutingTableReader> = Box::new(routing_table.clone());

        // create four nodes and four services
        let mut roster = vec![];
        let mut nodes = vec![];
        let mut members = vec![];
        for x in 0..4 {
            let node = CircuitNode {
                node_id: format!("node-{}", x),
                endpoints: vec![format!("endpoint_{}", x)],
                public_key: None,
            };
            let service = Service {
                service_id: format!("service-{}", x),
                service_type: "test".to_string(),
                node_id: format!("endpoint_{}", x),
                arguments: vec![("peer_services".to_string(), "node-000".to_string())],
                local_peer_id: None,
            };
            roster.push(service.clone());
            nodes.push(node.clone());
            members.push(node.node_id.clone());
        }
        let circuit_roster0 = vec![roster[0].clone(), roster[1].clone()];
        let circuit_roster1 = vec![roster[2].clone(), roster[3].clone()];
        let circuit_members0 = vec![members[0].clone(), members[1].clone()];
        let circuit_members1 = vec![members[2].clone(), members[3].clone()];
        let circuit_nodes0 = vec![nodes[0].clone(), nodes[1].clone()];

        // create circuits with the previously created nodes and services
        let circuit0 = Circuit {
            circuit_id: "012-abc".to_string(),
            roster: circuit_roster0.clone(),
            members: circuit_members0.clone(),
            authorization_type: AuthorizationType::Trust,
        };
        let circuit1 = Circuit {
            circuit_id: "345-def".to_string(),
            roster: circuit_roster1.clone(),
            members: circuit_members1.clone(),
            authorization_type: AuthorizationType::Trust,
        };

        let mut expected_nodes = BTreeMap::new();
        let mut expected_circuits = BTreeMap::new();
        let mut expected_service_directory = HashMap::new();

        expected_nodes.insert(nodes[0].node_id.clone().to_string(), nodes[0].clone());
        expected_nodes.insert(nodes[1].node_id.clone().to_string(), nodes[1].clone());

        expected_circuits.insert(circuit0.circuit_id.clone().to_string(), circuit0.clone());

        expected_service_directory.insert(
            ServiceId::new(
                "012-abc".to_string(),
                circuit_roster0[0].service_id.clone().to_string(),
            ),
            circuit_roster0[0].clone(),
        );
        expected_service_directory.insert(
            ServiceId::new(
                "012-abc".to_string(),
                circuit_roster0[1].service_id.clone().to_string(),
            ),
            circuit_roster0[1].clone(),
        );

        // add a circuit to the routing table
        writer
            .add_circuit(
                "012-abc".to_string(),
                circuit0.clone(),
                circuit_nodes0.clone(),
            )
            .expect("Unable to add circuit");

        assert_eq!(routing_table.state.read().unwrap().nodes, expected_nodes);
        assert_eq!(
            routing_table.state.read().unwrap().circuits,
            expected_circuits
        );
        assert_eq!(
            routing_table.state.read().unwrap().service_directory,
            expected_service_directory
        );

        // remove circuit from the routing table
        writer
            .remove_circuit("012-abc")
            .expect("Unable to remove circuit");

        assert!(!routing_table.state.read().unwrap().nodes.is_empty());
        assert!(routing_table.state.read().unwrap().circuits.is_empty());
        assert!(routing_table
            .state
            .read()
            .unwrap()
            .service_directory
            .is_empty());

        expected_circuits.insert(circuit1.circuit_id.clone().to_string(), circuit1.clone());

        expected_service_directory.insert(
            ServiceId::new(
                "345-def".to_string(),
                circuit_roster1[0].service_id.clone().to_string(),
            ),
            circuit_roster1[0].clone(),
        );
        expected_service_directory.insert(
            ServiceId::new(
                "345-def".to_string(),
                circuit_roster1[1].service_id.clone().to_string(),
            ),
            circuit_roster1[1].clone(),
        );

        // add multiple circuits to the routing table
        writer
            .add_circuits(vec![circuit0.clone(), circuit1.clone()])
            .expect("Unable to add circuits");

        assert_eq!(routing_table.state.read().unwrap().nodes, expected_nodes);
        assert_eq!(
            routing_table.state.read().unwrap().circuits,
            expected_circuits
        );
        assert_eq!(
            routing_table.state.read().unwrap().service_directory,
            expected_service_directory
        );

        // get one of the circuits in the routing table
        let fetched_circuit = reader
            .get_circuit(&circuit0.circuit_id)
            .expect("Unable to get circuit");

        assert_eq!(fetched_circuit, Some(circuit0));

        // list all circuits in the routing table
        let fetched_circuit_list = reader.list_circuits().expect("Unable to list circuits");

        assert_eq!(
            fetched_circuit_list.collect::<BTreeMap<String, Circuit>>(),
            expected_circuits
        );
    }

    // Test the routing table read and write operations for services
    //
    // 1. Create a circuit with corresponding nodes and services
    // 2. Write a service to the routing table
    // 3. Check the service was written to the routing table
    // 4. Remove service from the routing table
    // 5. Add circuit with two services to the routing table, validate ok
    // 6. Check the expected service is returned when fetched
    // 7. List services, validate both services are returned
    #[test]
    fn test_service() {
        let routing_table = RoutingTable::default();
        let mut writer: Box<dyn RoutingTableWriter> = Box::new(routing_table.clone());
        let reader: Box<dyn RoutingTableReader> = Box::new(routing_table.clone());

        // create two nodes, two services, and one circuit
        let node0 = CircuitNode {
            node_id: "node-0".to_string(),
            endpoints: vec!["endpoint_0".to_string()],
            public_key: None,
        };
        let service0 = Service {
            service_id: "service-0".to_string(),
            service_type: "test".to_string(),
            node_id: "endpoint_0".to_string(),
            arguments: vec![("peer_services".to_string(), "node-000".to_string())],
            local_peer_id: None,
        };
        let node1 = CircuitNode {
            node_id: "node-1".to_string(),
            endpoints: vec!["endpoint_1".to_string()],
            public_key: None,
        };
        let service1 = Service {
            service_id: "service-1".to_string(),
            service_type: "test".to_string(),
            node_id: "endpoint_1".to_string(),
            arguments: vec![("peer_services".to_string(), "node-000".to_string())],
            local_peer_id: None,
        };
        let circuit = Circuit {
            circuit_id: "012-abc".to_string(),
            roster: vec![service0.clone(), service1.clone()],
            members: vec![node0.node_id.clone(), node1.node_id.clone()],
            authorization_type: AuthorizationType::Trust,
        };
        let service_id0 = ServiceId::new(
            "012-abc".to_string(),
            service0.service_id.clone().to_string(),
        );

        let mut expected_service_directory = HashMap::new();

        let expected_service_id = ServiceId::new("012-abc".to_string(), "service-0".to_string());
        expected_service_directory.insert(expected_service_id, service0.clone());

        // add service to the routing table
        writer
            .add_service(service_id0.clone(), service0.clone())
            .expect("Unable to add service");

        assert_eq!(
            routing_table.state.read().unwrap().service_directory,
            expected_service_directory
        );

        // remove service from the routing table
        writer
            .remove_service(&service_id0.clone())
            .expect("Unable to remove service");

        assert!(routing_table
            .state
            .read()
            .unwrap()
            .service_directory
            .is_empty());

        // add circuit with two services to the routing table
        writer
            .add_circuit(
                circuit.circuit_id.clone(),
                circuit.clone(),
                vec![node0.clone(), node1.clone()],
            )
            .expect("Unable to add circuit");

        assert!(routing_table
            .state
            .read()
            .unwrap()
            .circuits
            .contains_key("012-abc"));

        // get one of the services in the routing table
        let fetched_service = reader
            .get_service(&service_id0.clone())
            .expect("Unable to get service");

        assert_eq!(fetched_service, Some(service0.clone()));

        // list all services in the routing table
        let fetched_service_list = reader
            .list_services(&circuit.circuit_id)
            .expect("Unable to list services");

        assert_eq!(fetched_service_list, vec![service0, service1]);
    }

    // Test the routing table read and write operations for nodes
    //
    // 1. Create two nodes, write one node to the routing table
    // 2. Check node was written to the routing table
    // 3. Remove node from the routing table
    // 4. Check node was removed from the routing table
    // 5. Add two nodes to the routing table
    // 6. Check both nodes written to the routing table
    // 7. Check the expected node is returned when fetched
    // 8. List nodes, validate both nodes are returned
    #[test]
    fn test_node() {
        let routing_table = RoutingTable::default();
        let mut writer: Box<dyn RoutingTableWriter> = Box::new(routing_table.clone());
        let reader: Box<dyn RoutingTableReader> = Box::new(routing_table.clone());

        let node0 = CircuitNode {
            node_id: "node-0".to_string(),
            endpoints: vec!["endpoint_0".to_string()],
            public_key: None,
        };
        let node1 = CircuitNode {
            node_id: "node-1".to_string(),
            endpoints: vec!["endpoint_1".to_string()],
            public_key: None,
        };

        let mut expected_nodes = BTreeMap::new();
        expected_nodes.insert(node0.node_id.clone(), node0.clone());

        // add node to the routing table
        writer
            .add_node(node0.node_id.clone(), node0.clone())
            .expect("Unable to add node");

        assert_eq!(routing_table.state.read().unwrap().nodes, expected_nodes);

        // remove node from the routing table
        writer
            .remove_node(&node0.node_id)
            .expect("Unable to remove node");

        assert!(routing_table.state.read().unwrap().nodes.is_empty());

        // add multiple nodes to the routing table
        writer
            .add_nodes(vec![node0.clone(), node1.clone()])
            .expect("Unable to add nodes");

        expected_nodes.insert(node1.node_id.clone(), node1.clone());

        assert_eq!(routing_table.state.read().unwrap().nodes, expected_nodes);

        // get a node from the routing table
        let fetched_node = reader
            .get_node(&node0.node_id.clone())
            .expect("Unable to get node");

        assert_eq!(fetched_node, Some(node0.clone()));

        // list all nodes in the routing table
        let fetched_node_list = reader.list_nodes().expect("Unable to list nodes");

        assert_eq!(
            fetched_node_list.collect::<BTreeMap<String, CircuitNode>>(),
            expected_nodes
        );
    }
}