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
//! This module defines the trait for holochain networking / p2p
//! The goal is to make implementing concrete strucs as simple as possible
//! so that we can iterate quickly on the API.
//! All calls will go through one of two api functions, one using json
//! strings, and the other using binary buffers.

use base64;
use failure::Error;
use serde_json;

/// callback function type for api methods transporting json data
pub type ApiFnJson = Box<FnMut(&str) -> Result<String, Error>>;

/// callback function type for api methods transporting binary data
pub type ApiFnBin = Box<FnMut(&[u8]) -> Result<Vec<u8>, Error>>;

/// when the network is requesting we store data
/// this callback will be invoked, expecting the data to be validated
pub type DhtHoldCallback = Box<FnMut(&str) -> Result<bool, Error>>;

/// the identifier for an application
pub type GenomeHash = [u8; 32];

/// binary api function `track_app` uses this leading byte
pub const BIN_TYPE_TRACK_APP: u8 = 0x11;

/// binary api function `untrack_app` uses this leading byte
pub const BIN_TYPE_UNTRACK_APP: u8 = 0x12;

/// binary api function `set_app_signature_callback` uses this leading byte
pub const BIN_TYPE_APP_SIGNATURE: u8 = 0x21;

/// binary api function `set_app_encryption_callback` uses this leading byte
pub const BIN_TYPE_APP_ENCRYPTION: u8 = 0x22;

/// enum defining the state of this p2p/network connection
pub enum P2pNetworkState {
    /// we are still setting up the connection, please wait
    Pending,

    /// connection established, but needs config, please call `set_config`
    NeedConfig,

    /// connection established and configure, all APIs available
    Running,
}

/// Represents a connection to a peer to peer network module
/// On initial instantiation, only calling `get_state` is valid
/// see P2pNetorkState for usage
pub trait P2pNetwork {
    // -- only these top two functions are required to be implemented
    // -- by concrete structs

    /// This is the main backbone api throughput function
    /// that must be implemented by structs implementing this trait
    fn exec_raw_json(&mut self, input: &str, cb: Option<ApiFnJson>) -> Result<String, Error>;

    /// This is similar to `exec_raw_json`, but permits binary data transfer
    fn exec_raw_bin(&mut self, input: &[u8], cb: Option<ApiFnBin>) -> Result<Vec<u8>, Error>;

    // -- All following functions are default implementations
    // -- making use of the above two functions

    /// This call should return a state within:
    ///  - `pending`
    ///  - `need_config`
    ///  - `running`
    fn get_state(&mut self) -> Result<P2pNetworkState, Error> {
        let r = self.exec_raw_json(
            &(json!({
                "method": "getState"
            }).to_string()),
            None,
        )?;
        if "pending" == r {
            return Ok(P2pNetworkState::Pending);
        } else if "need_config" == r {
            return Ok(P2pNetworkState::NeedConfig);
        } else if "running" == r {
            return Ok(P2pNetworkState::Running);
        } else {
            bail!("unexpected state: '{}'", r);
        }
    }

    /// This call should return a json configuration blob for the p2p module
    fn get_default_config(&mut self) -> Result<String, Error> {
        self.exec_raw_json(
            &(json!({
                "method": "getDefaultConfig"
            }).to_string()),
            None,
        )
    }

    /// pass along configuration to the network module
    fn set_config(&mut self, config: &str) -> Result<(), Error> {
        let v: serde_json::value::Value = serde_json::from_str(config)?;
        let v = json!({
            "method": "setConfig",
            "config": v
        });
        self.exec_raw_json(&(v.to_string()), None)?;
        Ok(())
    }

    /// setup an app to be synced on the p2p network side
    fn track_app(
        &mut self,
        genome_hash: &GenomeHash,
        sign_pubkey: &[u8; 32],
        enc_pubkey: &[u8; 32],
    ) -> Result<(), Error> {
        let mut out: Vec<u8> = vec![BIN_TYPE_TRACK_APP];
        out.append(&mut genome_hash.to_vec());
        out.append(&mut sign_pubkey.to_vec());
        out.append(&mut enc_pubkey.to_vec());
        self.exec_raw_bin(out.as_slice(), None)?;
        Ok(())
    }

    /// stop syncing an app on the p2p network side
    fn untrack_app(&mut self, genome_hash: &GenomeHash) -> Result<(), Error> {
        let mut out: Vec<u8> = vec![BIN_TYPE_UNTRACK_APP];
        out.append(&mut genome_hash.to_vec());
        self.exec_raw_bin(out.as_slice(), None)?;
        Ok(())
    }

    /// set a signature callback for an app
    fn set_app_signature_callback(
        &mut self,
        genome_hash: &GenomeHash,
        cb: ApiFnBin,
    ) -> Result<(), Error> {
        let mut out: Vec<u8> = vec![BIN_TYPE_APP_SIGNATURE];
        out.append(&mut genome_hash.to_vec());
        self.exec_raw_bin(out.as_slice(), Some(cb))?;
        Ok(())
    }

    /// set an encryption callback for an app
    fn set_app_encryption_callback(
        &mut self,
        genome_hash: &GenomeHash,
        cb: ApiFnBin,
    ) -> Result<(), Error> {
        let mut out: Vec<u8> = vec![BIN_TYPE_APP_ENCRYPTION];
        out.append(&mut genome_hash.to_vec());
        self.exec_raw_bin(out.as_slice(), Some(cb))?;
        Ok(())
    }

    /// when the network asks us to store a bit of DHT data
    /// we first need to make sure it is valid
    fn dht_set_on_hold_callback(&mut self, mut cb: DhtHoldCallback) -> Result<(), Error> {
        self.exec_raw_json(
            &(json!({
                "method": "dhtOnHoldCallback"
            }).to_string()),
            Some(Box::new(move |input| {
                if cb(input)? {
                    return Ok("true".to_string());
                }
                return Ok("false".to_string());
            })),
        )?;
        Ok(())
    }

    /// we want to publish a bit of DHT data
    fn dht_publish(&mut self, genome_hash: &GenomeHash, data: &str) -> Result<(), Error> {
        let v: serde_json::value::Value = serde_json::from_str(data)?;
        let v = json!({
            "method": "dhtPublish",
            "genomeHash": base64::encode(genome_hash),
            "payload": v
        });
        self.exec_raw_json(&(v.to_string()), None)?;
        Ok(())
    }
}

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

    /// helper for easy conversion of None to an error type
    #[derive(Debug, Clone, Fail)]
    enum E {
        #[fail(display = "None")]
        None,
    }

    /// short name better than `Value`
    type Json = serde_json::value::Value;

    /// helper to convert a String into `Json`
    fn json_parse(input: &str) -> Result<Json, Error> {
        let v: Json = serde_json::from_str(input)?;
        Ok(v)
    }

    /// helper to grab a string from a `Json` instance of type object
    fn json_obj_str(input: &Json, property: &str) -> Result<String, Error> {
        Ok(input
            .as_object()
            .ok_or(E::None)?
            .get(property)
            .ok_or(E::None)?
            .as_str()
            .ok_or(E::None)?
            .to_string())
    }

    /// test struct stub function for handling ApiFnJson requests
    type JsonHandler = Box<FnMut(&str, Option<ApiFnJson>) -> Result<String, Error>>;

    /// test struct stub function for handling ApiFnBin requests
    type BinHandler = Box<FnMut(&[u8], Option<ApiFnBin>) -> Result<Vec<u8>, Error>>;

    /// test struct stub that will implement P2pNetwork trait
    struct P2pStub {
        pub json_handler_queue: Vec<JsonHandler>,
        pub bin_handler_queue: Vec<BinHandler>,
    }

    /// some custom test functions
    impl P2pStub {
        /// create a new network stub
        pub fn new() -> Self {
            P2pStub {
                json_handler_queue: Vec::new(),
                bin_handler_queue: Vec::new(),
            }
        }
    }

    /// the stub implementation will just sequentially execute any
    /// handlers that have been define in the test function
    impl P2pNetwork for P2pStub {
        /// execute the next test stub json handler
        fn exec_raw_json(&mut self, input: &str, cb: Option<ApiFnJson>) -> Result<String, Error> {
            self.json_handler_queue.remove(0)(input, cb)
        }

        /// execute the next test stub binary handler
        fn exec_raw_bin(&mut self, input: &[u8], cb: Option<ApiFnBin>) -> Result<Vec<u8>, Error> {
            self.bin_handler_queue.remove(0)(input, cb)
        }
    }

    /// a wrapper struct in case we need to track additional state
    struct NodeStub {
        pub net: P2pStub,
    }

    impl NodeStub {
        /// create a new wrapper struct
        pub fn new() -> Self {
            NodeStub {
                net: P2pStub::new(),
            }
        }
    }

    /// assert that an expression is None
    macro_rules! assert_none {
        ($e:expr) => {
            if let Some(_) = $e {
                panic!("was not None");
            }
        };
    }

    /// assert that an expression is Some(??)
    macro_rules! assert_some {
        ($e:expr) => {
            if let None = $e {
                panic!("was None, expected Some");
            }
        };
    }

    /// parse a json string and make sure it is an object
    /// with a "method" property that matches `$method`
    macro_rules! setup_handler {
        ($input:expr, $method:expr) => {{
            let v: Json = json_parse($input)?;
            assert_eq!($method.to_string(), json_obj_str(&v, "method")?);
            v
        }};
    }

    #[test]
    fn it_should_construct() {
        NodeStub::new();
    }

    #[test]
    fn it_should_return_default_config() {
        let mut node = NodeStub::new();
        node.net.json_handler_queue.push(Box::new(|input, cb| {
            assert_none!(cb);
            setup_handler!(input, "getDefaultConfig");
            Ok("{\"test\":\"holo\"}".to_string())
        }));
        assert_eq!(
            "{\"test\":\"holo\"}".to_string(),
            node.net.get_default_config().unwrap()
        );
    }

    #[test]
    fn it_should_return_state_pending() {
        let mut node = NodeStub::new();
        node.net.json_handler_queue.push(Box::new(|input, cb| {
            assert_none!(cb);
            setup_handler!(input, "getState");
            Ok("pending".to_string())
        }));
        match node.net.get_state().unwrap() {
            P2pNetworkState::Pending => (),
            _ => panic!("unexpected get_state return value"),
        };
    }

    #[test]
    fn it_should_return_state_need_config() {
        let mut node = NodeStub::new();
        node.net.json_handler_queue.push(Box::new(|input, cb| {
            assert_none!(cb);
            setup_handler!(input, "getState");
            Ok("need_config".to_string())
        }));
        match node.net.get_state().unwrap() {
            P2pNetworkState::NeedConfig => (),
            _ => panic!("unexpected get_state return value"),
        };
    }

    #[test]
    fn it_should_return_state_running() {
        let mut node = NodeStub::new();
        node.net.json_handler_queue.push(Box::new(|input, cb| {
            assert_none!(cb);
            setup_handler!(input, "getState");
            Ok("running".to_string())
        }));
        match node.net.get_state().unwrap() {
            P2pNetworkState::Running => (),
            _ => panic!("unexpected get_state return value"),
        };
    }

    #[test]
    fn it_should_set_config() {
        let mut node = NodeStub::new();
        node.net.json_handler_queue.push(Box::new(|input, cb| {
            assert_none!(cb);
            let v = setup_handler!(input, "setConfig");
            let c = v
                .as_object()
                .ok_or(E::None)?
                .get("config")
                .ok_or(E::None)?
                .to_string();
            assert_eq!("{\"test\":\"holo\"}".to_string(), c);
            Ok("undefined".to_string())
        }));
        node.net.set_config("{\"test\":\"holo\"}").unwrap();
    }

    #[test]
    fn it_should_track_app() {
        let mut node = NodeStub::new();
        node.net.bin_handler_queue.push(Box::new(|input, cb| {
            assert_none!(cb);
            assert_eq!(BIN_TYPE_TRACK_APP, input[0]);
            assert_eq!(1_u8, input[1]);
            assert_eq!(2_u8, input[33]);
            assert_eq!(3_u8, input[65]);
            Ok(Vec::new())
        }));
        node.net
            .track_app(&[1_u8; 32], &[2_u8; 32], &[3_u8; 32])
            .unwrap();
    }

    #[test]
    fn it_should_untrack_app() {
        let mut node = NodeStub::new();
        node.net.bin_handler_queue.push(Box::new(|input, cb| {
            assert_none!(cb);
            assert_eq!(BIN_TYPE_UNTRACK_APP, input[0]);
            assert_eq!(4_u8, input[1]);
            Ok(Vec::new())
        }));
        node.net.untrack_app(&[4_u8; 32]).unwrap();
    }

    #[test]
    fn it_should_set_app_sig_callback() {
        let mut node = NodeStub::new();
        node.net.bin_handler_queue.push(Box::new(|input, cb| {
            assert_some!(cb);
            assert_eq!(BIN_TYPE_APP_SIGNATURE, input[0]);
            assert_eq!(5_u8, input[1]);
            Ok(Vec::new())
        }));
        node.net
            .set_app_signature_callback(&[5_u8; 32], Box::new(|_i| Ok(Vec::new())))
            .unwrap();
    }

    #[test]
    fn it_should_set_app_enc_callback() {
        let mut node = NodeStub::new();
        node.net.bin_handler_queue.push(Box::new(|input, cb| {
            assert_some!(cb);
            assert_eq!(BIN_TYPE_APP_ENCRYPTION, input[0]);
            assert_eq!(6_u8, input[1]);
            Ok(Vec::new())
        }));
        node.net
            .set_app_encryption_callback(&[6_u8; 32], Box::new(|_i| Ok(Vec::new())))
            .unwrap();
    }

    #[test]
    fn it_should_call_on_dht_hold_callback_true() {
        let mut node = NodeStub::new();
        node.net.json_handler_queue.push(Box::new(|input, cb| {
            assert_some!(cb);
            setup_handler!(input, "dhtOnHoldCallback");
            let res = cb.unwrap()(
                &(json!({
                "genomeHash": "blabla",
                "payload": "blabla",
            }).to_string()),
            )?;
            assert_eq!("true".to_string(), res);
            Ok("undefined".to_string())
        }));
        node.net
            .dht_set_on_hold_callback(Box::new(|_i| Ok(true)))
            .unwrap();
    }

    #[test]
    fn it_should_call_on_dht_hold_callback_false() {
        let mut node = NodeStub::new();
        node.net.json_handler_queue.push(Box::new(|input, cb| {
            assert_some!(cb);
            setup_handler!(input, "dhtOnHoldCallback");
            let res = cb.unwrap()(
                &(json!({
                "genomeHash": "blabla",
                "payload": "blabla",
            }).to_string()),
            )?;
            assert_eq!("false".to_string(), res);
            Ok("undefined".to_string())
        }));
        node.net
            .dht_set_on_hold_callback(Box::new(|_i| Ok(false)))
            .unwrap();
    }

    #[test]
    fn it_should_dht_publish() {
        let mut node = NodeStub::new();
        node.net.json_handler_queue.push(Box::new(|input, cb| {
            assert_none!(cb);
            let v = setup_handler!(input, "dhtPublish");
            assert_eq!(
                "CQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQk=".to_string(),
                json_obj_str(&v, "genomeHash").unwrap()
            );
            let c = v
                .as_object()
                .ok_or(E::None)?
                .get("payload")
                .ok_or(E::None)?
                .to_string();
            assert_eq!("{\"test\":\"holo\"}".to_string(), c);
            Ok("undefined".to_string())
        }));
        node.net
            .dht_publish(&[9_u8; 32], "{\"test\":\"holo\"}")
            .unwrap();
    }
}