p2panda 0.6.1

Out-of-the-box p2panda Node API for application developers
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
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::time::Duration;

use futures_util::StreamExt;
use mock_instant::thread_local::MockClock;
use p2panda::node::AckPolicy;
use p2panda::operation::{Extensions, LogId, Operation};
use p2panda::streams::{
    EphemeralMessage, ProcessedOperation, StreamEvent, StreamFrom, SystemEvent,
};
use p2panda::test_utils::setup_logging;
use p2panda_core::cbor::encode_cbor;
use p2panda_core::logs::LogHeights;
use p2panda_core::test_utils::TestLog;
use p2panda_core::{Cursor, Hash, SigningKey, Topic};
use p2panda_net::discovery::DiscoveryEvent;
use p2panda_store::logs::LogStore;
use tokio::task::JoinHandle;

fn assert_replay_started<M>(event: &StreamEvent<M>, expected_total_operations: u64) {
    let StreamEvent::ReplayStarted { total_operations } = event else {
        panic!("unexpected event");
    };
    assert_eq!(total_operations, &expected_total_operations);
}

fn assert_replay_ended<M>(event: &StreamEvent<M>) {
    let StreamEvent::ReplayEnded = event else {
        panic!("unexpected event");
    };
}

fn assert_message_id<M>(event: &StreamEvent<M>, id: Hash) {
    let StreamEvent::Processed { operation, .. } = event else {
        panic!("unexpected event");
    };
    assert_eq!(operation.id(), id);
}

#[tokio::test]
async fn build_and_spawn() -> Result<(), Box<dyn std::error::Error>> {
    // Default & instant setup.
    let _node = p2panda::spawn().await?;

    // Customizable "builder" setup flow.
    let _node = p2panda::builder()
        .database_url("sqlite::memory:")
        .signing_key(SigningKey::generate())
        .spawn()
        .await?;

    Ok(())
}

#[tokio::test]
async fn ephemeral_stream() {
    let chat_id = Topic::random();

    let panda = p2panda::spawn().await.unwrap();
    let icebear = p2panda::spawn().await.unwrap();

    // Panda joins the chat and sends a message to icebear, then waits for an answer.
    let panda_task: JoinHandle<EphemeralMessage<String>> = {
        let (tx, mut rx) = panda.ephemeral_stream(chat_id).await.unwrap();

        tokio::spawn(async move {
            tx.publish("Hello, Icebear!".into()).await.unwrap();
            let message = rx.next().await.unwrap();
            message
        })
    };

    // Icebear joins the chat and waits for a message of panda, to then answer.
    let icebear_task: JoinHandle<EphemeralMessage<String>> = {
        let (tx, mut rx) = icebear.ephemeral_stream(chat_id).await.unwrap();

        tokio::spawn(async move {
            let message = rx.next().await.unwrap();
            // Advance the clock to ensure icebear's message is published later than panda's.
            MockClock::advance_system_time(Duration::from_secs(1));
            tx.publish("Hi, Panda!".into()).await.unwrap();
            message
        })
    };

    let icebears_received_msg = icebear_task.await.unwrap();
    let pandas_received_msg = panda_task.await.unwrap();

    // Message authors match the senders.
    assert_eq!(icebears_received_msg.author(), panda.id());
    assert_eq!(pandas_received_msg.author(), icebear.id());

    // Everyone received the right messages.
    assert_eq!(icebears_received_msg.body(), &"Hello, Icebear!".to_string());
    assert_eq!(pandas_received_msg.body(), &"Hi, Panda!".to_string());

    // Icebear received the message before panda.
    assert!(icebears_received_msg.timestamp() < pandas_received_msg.timestamp())
}

#[tokio::test]
async fn eventually_consistent_stream() {
    setup_logging();

    let chat_id = Topic::random();

    let panda = p2panda::builder().spawn().await.unwrap();
    let icebear = p2panda::builder().spawn().await.unwrap();

    // Panda joins the chat and sends a message to icebear.
    let (panda_tx, _panda_rx) = panda.stream::<String>(chat_id).await.unwrap();
    panda_tx.publish("Hello, Icebear!".into()).await.unwrap();

    // Icebear joins the chat and waits for a message of panda.
    let (_icebear_tx, mut icebear_rx) = icebear.stream::<String>(chat_id).await.unwrap();

    let mut received: Option<ProcessedOperation<String>> = None;

    while let Some(event) = icebear_rx.next().await {
        if let StreamEvent::Processed { operation, .. } = event {
            received = Some(operation);
            break;
        }
    }

    let received = received.expect("icebear should have received operation");
    assert_eq!(received.message(), &"Hello, Icebear!".to_string());
    assert_eq!(received.author(), panda.id());
}

#[tokio::test]
async fn event_stream() {
    setup_logging();

    let chat_id = Topic::random();

    let panda = p2panda::builder().spawn().await.unwrap();
    let icebear = p2panda::builder().spawn().await.unwrap();

    // Panda joins the chat and sends a message to icebear.
    let (panda_tx, _panda_rx) = panda.stream::<String>(chat_id).await.unwrap();
    panda_tx.publish("Hello, Icebear!".into()).await.unwrap();

    // Icebear joins the chat.
    let (_icebear_tx, _icebear_rx) = icebear.stream::<String>(chat_id).await.unwrap();

    // Create a system event stream for panda.
    let mut events = panda.event_stream().await.unwrap();

    let mut received_event = false;

    // Wait for the first discovery session started event.
    while let Some(event) = events.next().await {
        if let SystemEvent::Discovery(DiscoveryEvent::SessionStarted { .. }) = event {
            received_event = true;
            break;
        }
    }

    assert!(received_event);
}

#[tokio::test]
async fn log_prefix_pruning() {
    setup_logging();

    let topic = Topic::random();

    let panda = p2panda::builder().spawn().await.unwrap();
    let icebear = p2panda::builder().spawn().await.unwrap();

    let (panda_tx, _) = panda.stream::<usize>(topic).await.unwrap();

    // 1. Panda publishes 3 operations into their append-only log.
    panda_tx.publish(1).await.unwrap();
    panda_tx.publish(2).await.unwrap();
    panda_tx.publish(3).await.unwrap();

    // 2. Icebear joins the topic and starts syncing Panda's operations. Please note that due to
    //    async behaviour we don't know how many operations Icebear will _exactly_ sync before
    //    pruning takes place.
    let (_, mut icebear_rx) = icebear.stream::<usize>(topic).await.unwrap();

    // 3. Panda prunes their log now and sets the last message to be "4".
    let processing = panda_tx.prune(Some(4)).await.unwrap();

    // We keep around the hash of the operation which pruned the log.
    let hash = processing.hash();

    // 4. Panda waits until their pruning operation was successfully processed in their local
    //    processing pipeline.
    let result = processing.await.unwrap();
    assert!(result.is_completed());
    assert!(!result.is_failed());

    // 5. We wait until icebear processed the (from their perspective remotely incoming) pruning
    //    operation as well.
    while let Some(event) = icebear_rx.next().await {
        if let StreamEvent::Processed { operation, .. } = event {
            assert!(operation.processed().is_completed());
            assert!(!operation.processed().is_failed());

            if operation.id() == hash {
                break;
            }
        }
    }

    // There should only be 1 message in Panda's and Icebear's database as the log was pruned.
    let log_id = LogId::from_topic(topic);
    let panda_result: Vec<(Operation, Vec<u8>)> = panda
        .store()
        .get_log_entries(&panda.id(), &log_id, None, None)
        .await
        .expect("no store failure")
        .expect("result to be Some");
    assert_eq!(panda_result.iter().count(), 1);

    let icebear_result: Vec<(Operation, Vec<u8>)> = icebear
        .store()
        .get_log_entries(&panda.id(), &log_id, None, None)
        .await
        .expect("no store failure")
        .expect("result to be Some");
    assert_eq!(icebear_result.iter().count(), 1);
}

#[tokio::test]
async fn automatic_acking() {
    setup_logging();

    let topic = Topic::random();
    let node = p2panda::builder().spawn().await.unwrap();

    let (tx, mut rx) = node.stream::<String>(topic).await.unwrap();

    // Publish two messages into the stream.
    let processing = tx.publish("first".into()).await.unwrap();
    let message_id_1 = processing.hash();
    processing.await.unwrap();

    let processing = tx.publish("second".into()).await.unwrap();
    let message_id_2 = processing.hash();
    processing.await.unwrap();

    // We except to receive them.
    assert_message_id(&rx.next().await.unwrap(), message_id_1);
    assert_message_id(&rx.next().await.unwrap(), message_id_2);

    // Create a new subscription.
    drop(tx);
    drop(rx);

    let (tx, mut rx) = node.stream::<String>(topic).await.unwrap();

    // Publish one more message.
    let processing = tx.publish("third".into()).await.unwrap();
    let message_id_3 = processing.hash();
    processing.await.unwrap();

    // We expect to only receive the new, third message and not the previous two ones anymore.
    assert_message_id(&rx.next().await.unwrap(), message_id_3);
}

#[tokio::test]
async fn explicit_acking() {
    setup_logging();

    let topic = Topic::random();
    let node = p2panda::builder()
        .ack_policy(AckPolicy::Explicit)
        .spawn()
        .await
        .unwrap();

    let (tx, mut rx) = node.stream::<String>(topic).await.unwrap();

    // Publish two messages into the stream.
    let message_id_1 = {
        let processing = tx.publish("first".into()).await.unwrap();
        let id = processing.hash();
        processing.await.unwrap();
        id
    };

    let message_id_2 = {
        let processing = tx.publish("second".into()).await.unwrap();
        let id = processing.hash();
        processing.await.unwrap();
        id
    };

    // We except to receive them from the subscription stream.
    assert_message_id(&rx.next().await.unwrap(), message_id_1);
    assert_message_id(&rx.next().await.unwrap(), message_id_2);

    // Acknowledge first message.
    rx.ack(message_id_1).await.unwrap();

    // Create a new subscription, streaming from "acked frontier".
    drop(tx);
    drop(rx);

    let (_tx, mut rx) = node.stream::<String>(topic).await.unwrap();

    // We except to receive only the un-acked messages from the subscription stream.
    assert_replay_started(&rx.next().await.unwrap(), 1);
    assert_message_id(&rx.next().await.unwrap(), message_id_2);
    assert_replay_ended(&rx.next().await.unwrap());
}

#[tokio::test]
async fn replay_stream_from_start() {
    setup_logging();

    let chat_id = Topic::random();

    let panda = p2panda::builder().spawn().await.unwrap();
    let icebear = p2panda::builder().spawn().await.unwrap();

    // Panda subscribes to chat and publishes one message.
    {
        let (panda_tx, _panda_rx) = panda.stream::<String>(chat_id).await.unwrap();
        panda_tx.publish("Hello, Icebear!".into()).await.unwrap();
    }

    // Panda subscribes again, this time asking to replay all messages from start.
    let (_panda_tx, mut panda_rx) = panda
        .stream_from::<String>(chat_id, StreamFrom::Start)
        .await
        .unwrap();

    // Icebear joins the chat and publishes one message.
    let (icebear_tx, _icebear_rx) = icebear.stream::<String>(chat_id).await.unwrap();
    icebear_tx.publish("Hello, Panda!".into()).await.unwrap();

    // Panda should receive the first message they sent again, followed by Icebear's message which
    // arrived via sync.
    let mut received = vec![];

    while let Some(event) = panda_rx.next().await {
        if let StreamEvent::Processed { operation, .. } = event {
            received.push(operation);
            if received.len() == 2 {
                break;
            }
        }
    }

    assert_eq!(received[0].message(), &"Hello, Icebear!".to_string());
    assert_eq!(received[1].message(), &"Hello, Panda!".to_string());
}

#[tokio::test]
async fn replay_stream_from_cursor() {
    setup_logging();

    let topic = Topic::random();
    let node = p2panda::builder().spawn().await.unwrap();

    let (tx, rx) = node.stream::<String>(topic).await.unwrap();

    // Publish two messages into the stream.
    let _message_id_1 = {
        let processing = tx.publish("first".into()).await.unwrap();
        let id = processing.hash();
        processing.await.unwrap();
        id
    };

    let message_id_2 = {
        let processing = tx.publish("second".into()).await.unwrap();
        let id = processing.hash();
        processing.await.unwrap();
        id
    };

    let message_id_3 = {
        let processing = tx.publish("third".into()).await.unwrap();
        let id = processing.hash();
        processing.await.unwrap();
        id
    };

    // Force re-playing from custom cursor position with new stream subscription.
    drop(tx);
    drop(rx);

    let mut cursor = Cursor::new(topic.to_string(), LogHeights::default());
    cursor.advance(node.id(), LogId::from_topic(topic), 0); // seq_num = 0, the first message

    let (_tx, mut rx) = node
        .stream_from::<String>(topic, StreamFrom::Cursor(cursor))
        .await
        .unwrap();

    // We expect to only receive the second and third message.
    assert_replay_started(&rx.next().await.unwrap(), 2);
    assert_message_id(&rx.next().await.unwrap(), message_id_2);
    assert_message_id(&rx.next().await.unwrap(), message_id_3);
    assert_replay_ended(&rx.next().await.unwrap());
}

#[tokio::test]
async fn import_external_stream() {
    setup_logging();

    let chat_id = Topic::random();

    // Panda opens their app and publishes some messages into a chat.
    let panda_log = TestLog::new();
    let extensions = Extensions {
        prune_flag: false.into(),
        log_id: LogId::from_topic(chat_id),
        version: Default::default(),
    };
    let operation_1 = panda_log.operation(
        &encode_cbor(&"Hello, Icebear!").unwrap(),
        extensions.clone(),
    );
    let operation_2 = panda_log.operation(
        &encode_cbor(&"I'm in a remote place with no internet, it's really nice :-p").unwrap(),
        extensions.clone(),
    );
    let operation_3 = panda_log.operation(
        &encode_cbor(&"Gunna post these messages to you on an SD card yo!").unwrap(),
        extensions,
    );

    // Panda exports messages to an SD card.
    let exported = vec![
        operation_1.clone(),
        operation_2.clone(),
        operation_3.clone(),
    ];

    // Panda goes offline and walks to the post office to send the SD card to Icebear.

    // Icebear receives the SD card, opens their app and initiates import.
    let import_stream = futures_util::stream::iter(exported);
    let icebear = p2panda::builder().spawn().await.unwrap();
    let (icebear_tx, mut icebear_rx) = icebear.stream::<String>(chat_id).await.unwrap();
    let import = icebear_tx.import(import_stream).await.unwrap();

    assert_eq!(import.session_id(), 0);
    assert!(import.await.is_ok());

    // Icebear receives the new messages after they've been processed.
    let mut imported = Vec::new();
    let mut start_received = false;
    let mut end_received = false;
    while let Some(event) = icebear_rx.next().await {
        if let StreamEvent::ImportStarted { session_id } = &event {
            assert!(!start_received);
            assert_eq!(session_id, &0);
            start_received = true;
            continue;
        };

        if let StreamEvent::Processed { operation, .. } = &event {
            assert!(start_received);
            assert!(!end_received);
            imported.push(operation.clone());
            if imported.len() == 3 {
                continue;
            }
        }

        if let StreamEvent::ImportEnded { session_id } = event {
            assert!(start_received);
            assert!(!end_received);
            assert_eq!(session_id, 0);
            end_received = true;
            break;
        };
    }

    assert!(start_received);
    assert!(end_received);
    assert_eq!(imported.len(), 3);
    assert!(
        imported
            .iter()
            .any(|event| event.id() == operation_1.header().hash())
    );
    assert!(
        imported
            .iter()
            .any(|event| event.id() == operation_2.header().hash())
    );
    assert!(
        imported
            .iter()
            .any(|event| event.id() == operation_3.header().hash())
    );
    assert!(end_received);
}

#[tokio::test]
async fn deduplicate_events() {
    setup_logging();

    let chat_id = Topic::random();

    let panda = p2panda::builder().spawn().await.unwrap();
    let icebear = p2panda::builder().spawn().await.unwrap();

    // Panda joins the chat.
    let (panda_tx, _panda_rx) = panda.stream::<String>(chat_id).await.unwrap();

    // Icebear joins the chat and waits for a message of panda.
    let (_icebear_tx, mut icebear_rx) = icebear.stream::<String>(chat_id).await.unwrap();

    panda_tx.publish("Hello, Icebear!".into()).await.unwrap();
    panda_tx
        .publish("Hello, Icebear again!".into())
        .await
        .unwrap();
    panda_tx
        .publish("Hello, Icebear and again!".into())
        .await
        .unwrap();

    let mut received = vec![];

    loop {
        let event = icebear_rx.next().await.unwrap();
        if let StreamEvent::SyncStarted { .. } = event {
            break;
        }
    }

    loop {
        tokio::select! {
            Some(event) = icebear_rx.next() => {
                if let StreamEvent::Processed { operation, .. } = event {
                    received.push(operation);
                }
            }
            _ = tokio::time::sleep(Duration::from_secs(2)) => {
                break;
            }
        }
    }

    assert_eq!(received.len(), 3);
}