p2panda-stream 0.8.0

Event stream processing for p2p data-types
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
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::borrow::Borrow;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::marker::PhantomData;

use p2panda_core::traits::Digest;
use p2panda_core::{Extensions, Hash, Operation};
use p2panda_store::Transaction;
use p2panda_store::operations::OperationStore;
use p2panda_store::orderer::OrdererStore;
use p2panda_store::processor::ProcessorStore;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::{Mutex, Notify};

use crate::Processor;
use crate::orderer::CausalOrderer;

pub trait OrdererMetadata<E> {
    /// Metadata attached to an input event we want to persist in database next to ordering info.
    type Metadata: Serialize + for<'de> Deserialize<'de>;

    /// Extract metadata from input.
    fn metadata(&self) -> Self::Metadata;

    /// Re-construct input from operation and persisted metadata.
    fn from_operation(operation: Operation<E>, meta: Self::Metadata) -> Self;
}

#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub enum OrdererArgs {
    Process {
        dependencies: Vec<Hash>,
    },
    #[default]
    Ignore,
}

#[derive(Clone, Debug)]
pub enum OrdererResult {
    /// Item has unmet dependencies, is buffered and now in "pending" state.
    Pending,

    /// Item is "ready" and was a dependency for one or more operations which are "freed" now.
    ///
    /// This input may trigger zero to many ReadyOutput items to follow.
    ReadyInput { dependent_operations: Vec<Hash> },

    /// Item was buffered by orderer and is now marked as "ready" by another "input" item, to be
    /// finally forwarded in correct order.
    ReadyOutput,

    /// Item was ignored as specified in input arguments.
    Ignored,
}

impl OrdererResult {
    pub fn is_pending(&self) -> bool {
        matches!(self, OrdererResult::Pending)
    }
}

pub struct Orderer<S, T, E> {
    inner: Mutex<CausalOrderer<Hash, S>>,
    store: S,
    notify: Notify,
    // We're keeping an unbound, in-memory buffer of all "freed" events here. This means that if an
    // event frees n events, we will need to keep all n of them here. This shouldn't be a problem as
    // long as n stays low (<100 items).
    //
    // The alternative is to move the logic polling from the inner orderer into "next" which allows
    // a more atomic and memory-efficient streaming design where we only look at one item at a time.
    // We still need a way to tell the pipeline how many items got "freed" by it to allow the
    // correct input stream ordering to take place (see docs in pipeline.rs of p2panda crate), this
    // functionality should be possible to add to the inner orderer.
    queue: RefCell<VecDeque<(T, OrdererResult)>>,
    _marker: PhantomData<E>,
}

impl<S, T, E> Orderer<S, T, E>
where
    S: Clone + Transaction + OrdererStore<Hash> + OperationStore<Operation<E>, Hash>,
{
    pub fn new(store: S) -> Self {
        Self::from_namespace(store, "default")
    }

    pub fn from_namespace(store: S, namespace: impl AsRef<str>) -> Self {
        let inner = CausalOrderer::new(store.clone()).with_namespace(namespace);

        Self {
            inner: Mutex::new(inner),
            store,
            notify: Notify::new(),
            queue: RefCell::new(VecDeque::new()),
            _marker: PhantomData,
        }
    }
}

impl<S, T, E> Processor<T> for Orderer<S, T, E>
where
    S: Transaction
        + OrdererStore<Hash>
        + OperationStore<Operation<E>, Hash>
        + ProcessorStore<T::Metadata>,
    T: OrdererMetadata<E> + Borrow<OrdererArgs> + Digest<Hash>,
    E: Extensions,
{
    type Output = (T, OrdererResult);

    type Error = (T, OrdererError);

    async fn process(&self, input: T) -> Result<(), Self::Error> {
        let args = input.borrow();

        if let OrdererArgs::Process { dependencies } = args {
            let inner = self.inner.lock().await;

            let permit = match self.store.begin().await {
                Ok(permit) => permit,
                Err(err) => return Err((input, OrdererError::Transaction(err.to_string()))),
            };

            match inner.process(input.hash(), dependencies).await {
                // a) Item has all dependencies met, we can directly mark it as "ready".
                Ok(true) => {
                    let mut dependent_operations = Vec::new();

                    loop {
                        match inner.next().await {
                            Ok(Some(id)) => {
                                // Ignore our own input.
                                if id != input.hash() {
                                    dependent_operations.push(id);
                                }

                                continue;
                            }
                            Ok(None) => {
                                break;
                            }
                            Err(err) => {
                                return Err((input, OrdererError::OrdererStore(err.to_string())));
                            }
                        }
                    }

                    let mut to_queue = Vec::new();

                    for id in &dependent_operations {
                        let operation = match self
                            .store
                            .get_operation_tx(id)
                            .await
                            .map_err(|err| OrdererError::OperationStore(err.to_string()))
                        {
                            Ok(Some(operation)) => operation,
                            Ok(None) => {
                                return Err((input, OrdererError::StoreInconsistency(*id)));
                            }
                            Err(err) => return Err((input, err)),
                        };

                        let metadata = match self.store.get_event_tx(&inner.namespace(), id).await {
                            Ok(Some(metadata)) => metadata,
                            Ok(None) => {
                                return Err((input, OrdererError::StoreInconsistency(*id)));
                            }
                            Err(err) => {
                                return Err((input, OrdererError::ProcessorStore(err.to_string())));
                            }
                        };

                        to_queue.push((
                            T::from_operation(operation, metadata),
                            OrdererResult::ReadyOutput,
                        ));
                    }

                    if let Err(err) = self.store.commit(permit).await {
                        return Err((input, OrdererError::Transaction(err.to_string())));
                    }

                    // Always forward the current input first.
                    self.queue.borrow_mut().push_back((
                        input,
                        OrdererResult::ReadyInput {
                            dependent_operations,
                        },
                    ));

                    // .. followed by all items which have been marked as "ready" by input.
                    for item in to_queue {
                        self.queue.borrow_mut().push_back(item);
                    }
                }

                // b) Item doesn't have dependencies met yet, mark it as "pending", it is buffered now.
                Ok(false) => {
                    if let Err(err) = self
                        .store
                        .set_event_tx(&inner.namespace(), &input.hash(), &input.metadata())
                        .await
                    {
                        return Err((input, OrdererError::ProcessorStore(err.to_string())));
                    };

                    if let Err(err) = self.store.commit(permit).await {
                        return Err((input, OrdererError::Transaction(err.to_string())));
                    }

                    self.queue
                        .borrow_mut()
                        .push_back((input, OrdererResult::Pending));
                }
                Err(err) => return Err((input, OrdererError::OrdererStore(err.to_string()))),
            }
        } else {
            self.queue
                .borrow_mut()
                .push_back((input, OrdererResult::Ignored));
        }

        self.notify.notify_one(); // Wake up any pending next call

        Ok(())
    }

    async fn next(&self) -> Result<Self::Output, Self::Error> {
        // TODO: If we decide to handle all logic in the "process" part (less memory efficient
        // approach, see comment above) we should consider replacing the Processor trait with a
        // Stream as "next" is not required anymore.
        loop {
            if let Some(output) = self.queue.borrow_mut().pop_front() {
                return Ok(output);
            }

            self.notify.notified().await;
        }
    }
}

#[derive(Clone, Debug, Error)]
pub enum OrdererError {
    #[error("could not find item with id {0} in operation store")]
    StoreInconsistency(Hash),

    #[error("{0}")]
    OrdererStore(String),

    #[error("{0}")]
    OperationStore(String),

    #[error("{0}")]
    ProcessorStore(String),

    #[error("{0}")]
    Transaction(String),
}

#[cfg(test)]
mod tests {
    use std::assert_matches;
    use std::borrow::Borrow;

    use futures_util::stream;
    use p2panda_core::traits::Digest;
    use p2panda_core::{Body, Hash, Header, Operation, SigningKey, Topic};
    use p2panda_store::operations::OperationStore;
    use p2panda_store::{SqliteStore, tx_unwrap};
    use serde::{Deserialize, Serialize};
    use tokio::task;
    use tokio_stream::StreamExt;

    use crate::{Processor, StreamLayerExt};

    use super::{Orderer, OrdererArgs, OrdererMetadata, OrdererResult};

    #[derive(Clone, Debug, Default, Serialize, Deserialize)]
    struct TestExtension {
        dependencies: Vec<Hash>,
    }

    #[derive(Clone, Debug, PartialEq, Eq)]
    struct Event {
        orderer_args: OrdererArgs,
        operation: Operation<TestExtension>,
    }

    impl Borrow<OrdererArgs> for Event {
        fn borrow(&self) -> &OrdererArgs {
            &self.orderer_args
        }
    }

    impl Digest<Hash> for Event {
        fn hash(&self) -> Hash {
            self.operation.hash
        }
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    struct EventMetadata;

    impl OrdererMetadata<TestExtension> for Event {
        type Metadata = EventMetadata;

        fn metadata(&self) -> Self::Metadata {
            EventMetadata
        }

        // The `meta` parameter is a requirement of the trait function signature but we do not
        // require it for this test implementation, hence the lint bypass.
        #[allow(unused_variables)]
        fn from_operation(operation: Operation<TestExtension>, meta: Self::Metadata) -> Self {
            Self {
                orderer_args: OrdererArgs::Process {
                    dependencies: operation.header.extensions.dependencies.clone(),
                },
                operation,
            }
        }
    }

    #[tokio::test]
    async fn out_of_order() {
        // Create two operations, one by Panda and one by Icebear. Panda's operation points at
        // Icebear's.
        let operation_panda = {
            let signing_key = SigningKey::generate();
            let body: Body = b"Hi, Icebear".to_vec().into();

            let header = Header::builder().body(&body).build(
                &signing_key,
                TestExtension {
                    dependencies: vec![],
                },
            );

            Operation::from_parts(header, Some(body))
        };

        let event_panda = Event::from_operation(operation_panda.clone(), EventMetadata);

        let operation_icebear = {
            let signing_key = SigningKey::generate();
            let body: Body = b"Hello, Pandasan!".to_vec().into();

            let header = Header::builder().body(&body).build(
                &signing_key,
                TestExtension {
                    dependencies: vec![operation_panda.hash],
                },
            );

            Operation::from_parts(header, Some(body))
        };

        let event_icebear = Event::from_operation(operation_icebear.clone(), EventMetadata);

        let local = task::LocalSet::new();

        local
            .run_until(async move {
                let store = SqliteStore::temporary().await;

                // Insert operations into store.
                tx_unwrap!(store, {
                    let log_id = Topic::random();

                    store
                        .insert_operation_tx(&operation_panda.hash, &operation_panda, &log_id)
                        .await
                        .unwrap();
                    store
                        .insert_operation_tx(&operation_icebear.hash, &operation_icebear, &log_id)
                        .await
                        .unwrap();
                });

                // Prepare processing pipeline for message ordering.
                let orderer_1 = Orderer::from_namespace(store.clone(), "first");

                let mut stream = stream::iter(vec![
                    // Process Icebear's operation first. It will arrive "out of order".
                    event_icebear.clone(),
                    // Process Pandas's operation next. It will "free" Icebear's operation.
                    event_panda.clone(),
                ])
                .layer(orderer_1);

                // Icebear's event has a dependency on Panda's event so it remains in a pending
                // state for now.
                let (event, result) = stream.next().await.unwrap().unwrap();
                assert!(result.is_pending());
                assert_eq!(event, event_icebear);

                // Panda's event is released. It frees up Icebear's event which is now ready.
                let (event, result) = stream.next().await.unwrap().unwrap();
                assert_matches!(
                    result,
                    OrdererResult::ReadyInput {
                        dependent_operations: _
                    }
                );
                assert_eq!(event, event_panda);

                // Icebear's event has it's dependencies met and is released.
                let (event, result) = stream.next().await.unwrap().unwrap();
                assert_matches!(result, OrdererResult::ReadyOutput);
                assert_eq!(event, event_icebear);

                // Make sure another, namespaced orderer is not affected:
                let orderer_2 = Orderer::from_namespace(store, "second");
                orderer_2.process(event_icebear.clone()).await.unwrap();
                let (_event, result) = orderer_2.next().await.unwrap();
                assert_matches!(
                    result,
                    OrdererResult::Pending,
                    "panda's event was not processed yet for second orderer"
                );
            })
            .await;
    }
}