ddcp 0.2.4

Distributed decentralized database-to-database copy
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
use std::collections::HashMap;

use tracing::debug;
use veilid_core::{
    CryptoTyped, DHTRecordDescriptor, DHTSchemaDFLT, KeyPair, RouteId, RoutingContext, TableDB,
    TableStore, TypedKey, TypedKeyPair, ValueSubkey, VeilidAPI, CRYPTO_KIND_VLD0,
};
use veilid_core::{FromStr, Target};

use crate::error::warn_err;
use crate::proto::codec::{
    ChangesResponse, Decodable, Encodable, Envelope, NodeStatus, Request, Response,
};
use crate::proto::crypto::Crypto;
use crate::{error::Result, other_err};

const DHT_N_SUBKEYS: u16 = 3;
const DHT_SUBKEY_STATUS: ValueSubkey = 0;

const TABLE_STORE_LOCAL_N_COLUMNS: u32 = 2;
const TABLE_STORE_LOCAL_COLUMN_DHT_KEY: u32 = 0;
const TABLE_STORE_LOCAL_COLUMN_DHT_OWNER_KEYPAIR: u32 = 1;

const TABLE_STORE_REMOTES_N_COLUMNS: u32 = 1;
const TABLE_STORE_REMOTES_COLUMN_DHT_KEY: u32 = 0;

#[derive(Clone)]
pub struct Sovereign {
    dht_key: TypedKey,
    dht_owner_keypair: CryptoTyped<KeyPair>,

    dht: Option<DHTRecordDescriptor>,
    route: Option<Route>,
}

#[derive(Clone)]
struct Route {
    id: RouteId,
    data: Vec<u8>,
}

#[derive(Clone)]
pub struct Status {
    pub site_id: Vec<u8>,
    pub db_version: i64,
}

impl Sovereign {
    async fn open_db(ts: &TableStore) -> Result<TableDB> {
        let db = ts
            .open("ddcp_conclave_local", TABLE_STORE_LOCAL_N_COLUMNS)
            .await?;
        Ok(db)
    }

    pub fn dht_key(&self) -> TypedKey {
        self.dht_key
    }

    pub fn auth_key(&self) -> TypedKey {
        CryptoTyped {
            kind: self.dht_owner_keypair.kind,
            value: self.dht_owner_keypair.value.key,
        }
    }

    pub async fn init(routing_context: &RoutingContext) -> Result<Sovereign> {
        let ts = routing_context.api().table_store()?;
        let db = Self::open_db(&ts).await?;

        // create DHT
        let new_dht = routing_context
            .create_dht_record(
                veilid_core::DHTSchema::DFLT(DHTSchemaDFLT {
                    o_cnt: DHT_N_SUBKEYS,
                }),
                None,
            )
            .await?;
        let dht_owner_keypair = KeyPair::new(
            new_dht.owner().to_owned(),
            new_dht
                .owner_secret()
                .ok_or(other_err("expected dht owner secret"))?
                .to_owned(),
        );
        routing_context
            .close_dht_record(new_dht.key().to_owned())
            .await?;

        // write these to db
        db.store_json(TABLE_STORE_LOCAL_COLUMN_DHT_KEY, &[], new_dht.key())
            .await?;
        db.store_json(
            TABLE_STORE_LOCAL_COLUMN_DHT_OWNER_KEYPAIR,
            &[],
            &dht_owner_keypair,
        )
        .await?;

        // read back from storage; redundant, but asserts data integrity
        Self::load(routing_context)
            .await?
            .ok_or(other_err("failed to initialize sovereign identity"))
    }

    pub async fn load(routing_context: &RoutingContext) -> Result<Option<Sovereign>> {
        let ts = routing_context.api().table_store()?;
        let db = Self::open_db(&ts).await?;
        let Some(dht_key) = db
            .load_json::<TypedKey>(TABLE_STORE_LOCAL_COLUMN_DHT_KEY, &[])
            .await?
        else {
            return Ok(None);
        };
        let Some(dht_owner_keypair) = db
            .load_json::<TypedKeyPair>(TABLE_STORE_LOCAL_COLUMN_DHT_OWNER_KEYPAIR, &[])
            .await?
        else {
            return Ok(None);
        };

        let dht = routing_context
            .open_dht_record(dht_key.clone(), Some(dht_owner_keypair.value.clone()))
            .await?;
        Ok(Some(Sovereign {
            dht_key,
            dht_owner_keypair,
            dht: Some(dht),
            route: None,
        }))
    }

    pub fn release_route(&mut self, routing_context: &RoutingContext) -> Result<()> {
        if let Some(ref route) = self.route {
            routing_context.api().release_private_route(route.id)?;
            self.route = None
        }
        Ok(())
    }

    pub async fn announce(
        &mut self,
        routing_context: &RoutingContext,
        status: Status,
    ) -> Result<()> {
        let route = match self.route {
            Some(ref route) => route,
            None => {
                let (id, data) = routing_context.api().new_private_route().await?;
                self.route = Some(Route { id, data });
                self.route.as_ref().unwrap()
            }
        };

        routing_context
            .set_dht_value(
                self.dht_key,
                DHT_SUBKEY_STATUS,
                NodeStatus {
                    site_id: status.site_id.clone(),
                    db_version: status.db_version,
                    key: self.auth_key().to_string(),
                    route: route.data.clone(),
                }
                .encode()?,
            )
            .await?;
        Ok(())
    }

    async fn close(&mut self, routing_context: &RoutingContext) -> Result<()> {
        if let Some(dht) = self.dht.take() {
            routing_context
                .close_dht_record(dht.key().to_owned())
                .await?;
        }
        if let Some(route) = self.route.take() {
            routing_context.api().release_private_route(route.id)?;
        }
        Ok(())
    }
}

#[derive(Clone)]
pub struct Peer {
    name: String,
    dht_key: TypedKey,

    dht: Option<DHTRecordDescriptor>,
    node_status: Option<NodeStatus>,
    route_id: Option<RouteId>,
}

impl Peer {
    async fn open_db(ts: &TableStore) -> Result<TableDB> {
        let db = ts
            .open("ddcp_conclave_remotes", TABLE_STORE_REMOTES_N_COLUMNS)
            .await?;
        Ok(db)
    }

    pub async fn new(api: &VeilidAPI, name: &str, dht_public_key: &str) -> Result<Peer> {
        let ts = api.table_store()?;
        let db = Self::open_db(&ts).await?;
        let db_key = name.as_bytes().to_vec();

        // Parse and store dht key
        let dht_key = TypedKey::from_str(dht_public_key)?;
        db.store_json(
            TABLE_STORE_REMOTES_COLUMN_DHT_KEY,
            db_key.as_slice(),
            &dht_key,
        )
        .await?;

        Self::load(name, &db, &db_key).await
    }

    pub async fn load_all(api: &VeilidAPI) -> Result<HashMap<String, Peer>> {
        let ts = api.table_store()?;
        let db = Self::open_db(&ts).await?;
        let mut remotes = HashMap::new();
        for remote_key in db
            .get_keys(TABLE_STORE_REMOTES_COLUMN_DHT_KEY)
            .await?
            .iter()
        {
            let remote_name = String::from_utf8(remote_key.to_owned()).map_err(other_err)?;
            let peer = Peer::load(&remote_name, &db, remote_key).await?;
            remotes.insert(remote_name, peer);
        }
        Ok(remotes)
    }

    async fn load(name: &str, db: &TableDB, db_key: &Vec<u8>) -> Result<Peer> {
        let dht_public_key = db
            .load_json::<TypedKey>(TABLE_STORE_REMOTES_COLUMN_DHT_KEY, db_key)
            .await?
            .ok_or(other_err("remote peer missing dht key"))?;
        Ok(Peer {
            name: name.to_owned(),
            dht_key: dht_public_key,
            dht: None,
            node_status: None,
            route_id: None,
        })
    }

    pub async fn remove(self, ts: &TableStore) -> Result<()> {
        let db = Self::open_db(ts).await?;
        let db_key = self.name.as_bytes();
        db.delete(TABLE_STORE_REMOTES_COLUMN_DHT_KEY, db_key)
            .await?;
        Ok(())
    }

    pub fn dht_key(&self) -> TypedKey {
        self.dht_key
    }

    pub fn auth_key(&self) -> Result<Option<TypedKey>> {
        match self.node_status {
            Some(ref ns) => Ok(Some(CryptoTyped::from_str(&ns.key)?)),
            None => Ok(None),
        }
    }

    pub fn name(&self) -> String {
        self.name.to_owned()
    }

    pub async fn refresh(
        &mut self,
        routing_context: &RoutingContext,
    ) -> Result<Option<NodeStatus>> {
        self.refresh_dht(routing_context).await?;
        self.refresh_route(routing_context).await?;
        Ok(self.node_status())
    }

    async fn refresh_dht(&mut self, routing_context: &RoutingContext) -> Result<()> {
        if let Some(dht) = self.dht.as_ref() {
            routing_context
                .close_dht_record(dht.key().to_owned())
                .await?;
        };
        self.dht = Some(routing_context.open_dht_record(self.dht_key, None).await?);
        self.node_status = match routing_context
            .get_dht_value(self.dht_key, DHT_SUBKEY_STATUS, true)
            .await?
        {
            Some(data) => Some(NodeStatus::decode(data.data())?),
            None => None,
        };
        Ok(())
    }

    async fn refresh_route(&mut self, routing_context: &RoutingContext) -> Result<()> {
        if let Some(status) = &self.node_status {
            self.route_id = Some(
                routing_context
                    .api()
                    .import_remote_private_route(status.route.to_vec())?,
            );
        }
        Ok(())
    }

    pub fn node_status(&self) -> Option<NodeStatus> {
        self.node_status.clone()
    }

    async fn close(&mut self, routing_context: &RoutingContext) -> Result<()> {
        if let Some(dht) = self.dht.take() {
            routing_context
                .close_dht_record(dht.key().to_owned())
                .await?;
        }
        if let Some(route_id) = self.route_id.take() {
            routing_context.api().release_private_route(route_id)?;
        }
        Ok(())
    }
}

/// A Conclave defines a local sovereign identity and a group of peers which
/// share a replication secret.
#[derive(Clone)]
pub struct Conclave {
    routing_context: RoutingContext,

    sovereign: Sovereign,
    remotes: HashMap<String, Peer>,
}

impl Conclave {
    pub async fn new(routing_context: RoutingContext) -> Result<Conclave> {
        let sovereign = match Sovereign::load(&routing_context).await? {
            Some(sov) => sov,
            None => Sovereign::init(&routing_context).await?,
        };

        let remotes = Peer::load_all(&routing_context.api()).await?;

        Ok(Conclave {
            routing_context,
            sovereign,
            remotes,
        })
    }

    pub async fn refresh(&mut self, status: Status) -> Result<()> {
        self.sovereign
            .announce(&self.routing_context, status)
            .await?;
        self.refresh_peers().await?;
        Ok(())
    }

    pub async fn refresh_peers(&mut self) -> Result<()> {
        for peer in self.remotes.values_mut().into_iter() {
            peer.refresh(&self.routing_context).await?;
        }
        Ok(())
    }

    pub fn sovereign(&self) -> &Sovereign {
        return &self.sovereign;
    }

    pub fn sovereign_mut(&mut self) -> &mut Sovereign {
        return &mut self.sovereign;
    }

    pub fn peer(&self, name: &str) -> Option<&Peer> {
        return self.remotes.get(name);
    }

    pub fn peer_mut(&mut self, name: &str) -> Option<&mut Peer> {
        return self.remotes.get_mut(name);
    }

    pub async fn set_peer(&mut self, peer: Peer) -> Result<()> {
        self.remotes.insert(peer.name.clone(), peer);
        Ok(())
    }

    pub async fn remove_peer(&mut self, name: &str) -> Result<bool> {
        match self.remotes.remove(name) {
            Some(peer) => {
                peer.remove(&self.routing_context.api().table_store()?)
                    .await?;
                Ok(true)
            }
            None => Ok(false),
        }
    }

    pub fn peers<'a>(&'a self) -> std::collections::hash_map::Values<'a, String, Peer> {
        self.remotes.values().into_iter()
    }

    pub async fn changes(&self, peer: &Peer, since_db_version: i64) -> Result<ChangesResponse> {
        let crypto = self.crypto(peer)?;
        let req_bytes = Envelope {
            sender: self.sovereign.dht_key.to_string(),
            contents: crypto.encode(Request::Changes { since_db_version })?,
        }
        .encode()?;
        debug!(len = req_bytes.len(), "app_call request");
        let peer_route_id = peer.route_id.ok_or(other_err("no route to peer"))?;
        let resp_bytes = match self
            .routing_context
            .app_call(Target::PrivateRoute(peer_route_id), req_bytes)
            .await
        {
            Ok(resp_bytes) => resp_bytes,
            Err(e) => {
                let _ = warn_err(
                    self.routing_context
                        .api()
                        .release_private_route(peer_route_id),
                    "failed to release peer route",
                );
                return Err(crate::Error::VeilidAPI(e));
            }
        };
        debug!(len = resp_bytes.len(), "app_call response");
        let resp = Envelope::decode(resp_bytes.as_slice())?;
        match crypto.decode::<Response>(&resp.contents)? {
            Response::Changes(changes) => Ok(changes),
            r => Err(other_err(format!("invalid response: {:?}", r))),
        }
    }

    pub fn crypto(&self, peer: &Peer) -> Result<Crypto> {
        Ok(Crypto::new(
            self.routing_context
                .api()
                .crypto()?
                .get(CRYPTO_KIND_VLD0)
                .ok_or(other_err("VLD0 not available"))?,
            self.sovereign.dht_owner_keypair,
            peer.auth_key()?.ok_or(other_err("peer key not found"))?,
        ))
    }

    pub async fn close(mut self) -> Result<()> {
        self.sovereign.close(&self.routing_context).await?;
        for peer in self.remotes.values_mut().into_iter() {
            peer.close(&self.routing_context).await?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use veilid_core::Sequencing;

    use crate::tests::api::{setup_api, teardown_api, TEST_API_MUTEX};

    use super::*;

    #[tokio::test]
    async fn basic() {
        let _lock = TEST_API_MUTEX.lock().expect("lock");

        let api = setup_api().await;
        let routing_context = api
            .routing_context()
            .expect("routing context")
            .with_sequencing(Sequencing::PreferOrdered)
            .with_default_safety()
            .expect("ok");

        // Add a peer and look it up
        let mut ccl = Conclave::new(routing_context).await.expect("ok");
        assert_eq!(ccl.peers().len(), 0);

        let vld0 = ccl
            .routing_context
            .api()
            .crypto()
            .expect("crypto")
            .get(CRYPTO_KIND_VLD0)
            .expect("VLD0");
        let peer_keypair = vld0.generate_keypair();

        let peer = Peer::new(&api, "bob", &peer_keypair.key.to_string())
            .await
            .expect("new peer");
        ccl.set_peer(peer).await.expect("set peer");
        assert_eq!(ccl.peers().len(), 1);

        ccl.close().await.expect("ok");
        teardown_api(api).await;
    }

    #[tokio::test]
    async fn pair() {
        let _lock = TEST_API_MUTEX.lock().expect("lock");

        let api = setup_api().await;
        let routing_context = api
            .routing_context()
            .expect("routing context")
            .with_sequencing(Sequencing::PreferOrdered)
            .with_default_safety()
            .expect("ok");

        let mut ccl = Conclave::new(routing_context).await.expect("ok");
        let vld0 = ccl
            .routing_context
            .api()
            .crypto()
            .expect("crypto")
            .get(CRYPTO_KIND_VLD0)
            .expect("VLD0");
        let peer_keypair = CryptoTyped {
            kind: CRYPTO_KIND_VLD0,
            value: vld0.generate_keypair(),
        };
        let peer_auth_key = CryptoTyped {
            kind: CRYPTO_KIND_VLD0,
            value: peer_keypair.value.key,
        };

        // Add a peer and look it up
        let mut peer = Peer::new(&api, "bob", &peer_auth_key.to_string())
            .await
            .expect("new peer");
        peer.node_status = Some(NodeStatus {
            site_id: vec![],
            db_version: 42,
            key: peer_auth_key.to_string(),
            route: vec![],
        });
        ccl.set_peer(peer.clone()).await.expect("set peer");

        let send_crypto = ccl.crypto(&peer).expect("peer crypto");
        let recv_crypto = Crypto::new(vld0, peer_keypair, ccl.sovereign().auth_key());

        // Test peer crypto
        let req = Request::Changes {
            since_db_version: 8,
        };
        let enc_req = send_crypto.encode(req.clone()).expect("encode");
        let dec_req = recv_crypto
            .decode::<Request>(enc_req.as_slice())
            .expect("decode");
        assert_eq!(req, dec_req);

        // Through envelope
        let req_send = Envelope {
            sender: ccl.sovereign().dht_key().to_string(),
            contents: send_crypto.encode(req.clone()).expect("encode"),
        };
        let msg = req_send.encode().expect("encode");
        let req_recv = Envelope::decode(msg.as_slice()).expect("decode");
        let req_decrypt = recv_crypto
            .decode::<Request>(req_recv.contents.as_slice())
            .expect("decode");
        assert_eq!(req, req_decrypt);

        ccl.close().await.expect("ok");
        teardown_api(api).await;
    }
}