p2panda-sync 0.7.0

Local-first sync for append-only logs and traits to build your own
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Two-party sync protocol over a topic associated with a collection of append-only logs.
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::hash::Hash as StdHash;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};

use futures_channel::mpsc;
use futures_util::{Sink, SinkExt, Stream, StreamExt};
use p2panda_core::{Body, Extensions, Hash, Header, LogId, Operation, SeqNum, VerifyingKey};
use p2panda_store::logs::LogStore;
use p2panda_store::topics::TopicStore;
use pin_project_lite::pin_project;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::broadcast;
use tracing::{Level, debug, enabled, trace, warn};

use crate::ToSync;
use crate::dedup::DEFAULT_BUFFER_CAPACITY;
use crate::protocols::ShortFormat;
use crate::protocols::log_sync::{
    LogSync, LogSyncError, LogSyncEvent, LogSyncMessage, LogSyncMetrics,
};
use crate::traits::Protocol;

/// Protocol for synchronizing logs which are associated with a generic T topic.
///
/// The mapping of T to a set of logs is handled on the application layer using an implementation
/// of the `TopicStore` trait.
///
/// After sync is complete peers optionally enter "live-mode" where concurrently received and
/// future messages will be sent directly to the application layer and forwarded to any
/// concurrently running sync sessions. As we may receive messages from many sync sessions
/// concurrently, messages forwarded to a sync session in live-mode are de-duplicated in order to
/// avoid flooding the network with redundant data.
///
/// It is assumed that the T topic has been negotiated between parties prior to initiating this
/// sync protocol.
#[derive(Debug)]
pub struct TopicLogSync<T, S, L, E> {
    pub topic: T,
    pub store: S,
    pub event_tx: broadcast::Sender<TopicLogSyncEvent<E>>,
    pub live_mode_rx: Option<mpsc::Receiver<ToSync<Operation<E>>>>,
    pub buffer_capacity: usize,
    pub _phantom: PhantomData<L>,
}

impl<T, S, L, E> TopicLogSync<T, S, L, E>
where
    T: Eq + StdHash + Serialize + for<'a> Deserialize<'a>,
    S: LogStore<Operation<E>, VerifyingKey, L, SeqNum, Hash>
        + TopicStore<T, VerifyingKey, L>
        + Clone
        + Send
        + 'static,
    L: LogId,
    E: Extensions,
{
    /// Returns a new sync protocol instance, configured with a store and `TopicStore` implementation
    /// which associates the to-be-synced logs with a given topic.
    pub fn new(
        topic: T,
        store: S,
        live_mode_rx: Option<mpsc::Receiver<ToSync<Operation<E>>>>,
        event_tx: broadcast::Sender<TopicLogSyncEvent<E>>,
    ) -> Self {
        Self::new_with_capacity(
            topic,
            store,
            live_mode_rx,
            event_tx,
            DEFAULT_BUFFER_CAPACITY,
        )
    }

    /// Instantiates a sync protocol with custom buffer capacity.
    pub fn new_with_capacity(
        topic: T,
        store: S,
        live_mode_rx: Option<mpsc::Receiver<ToSync<Operation<E>>>>,
        event_tx: broadcast::Sender<TopicLogSyncEvent<E>>,
        buffer_capacity: usize,
    ) -> Self {
        Self {
            topic,
            store,
            event_tx,
            live_mode_rx,
            buffer_capacity,
            _phantom: PhantomData,
        }
    }
}

impl<T, S, L, E> Protocol for TopicLogSync<T, S, L, E>
where
    T: Debug + Eq + StdHash + Serialize + for<'a> Deserialize<'a> + Send + 'static,
    S: LogStore<Operation<E>, VerifyingKey, L, SeqNum, Hash>
        + TopicStore<T, VerifyingKey, L>
        + Clone
        + Send
        + 'static,
    L: LogId + Debug + Send + 'static,
    E: Extensions + Send + 'static,
{
    type Error = TopicLogSyncError;
    type Message = TopicLogSyncMessage<L, E>;
    type Output = ();

    async fn run(
        self,
        mut sink: &mut (impl Sink<Self::Message, Error = impl Debug> + Unpin),
        mut stream: &mut (impl Stream<Item = Result<Self::Message, impl Debug>> + Unpin),
    ) -> Result<Self::Output, Self::Error> {
        // TODO: check there is overlap between the local and remote topic filters and end the
        // session now if not.
        debug!(
            live_mode = self.live_mode_rx.is_some(),
            "start sync session"
        );

        // Get the log ids which are associated with this topic query.
        let logs = self
            .store
            .resolve(&self.topic)
            .await
            .map_err(|err| TopicLogSyncError::TopicStore(err.to_string()))?;

        if enabled!(Level::DEBUG) {
            let display_logs: BTreeMap<String, usize> =
                logs.iter().map(|(k, v)| (k.fmt_short(), v.len())).collect();
            debug!(logs = ?display_logs, "local topic logs retrieved");
        }

        if enabled!(Level::TRACE) {
            let trace_logs: Vec<(String, &Vec<L>)> =
                logs.iter().map(|(k, v)| (k.fmt_short(), v)).collect();
            trace!(logs = ?trace_logs, "local topic logs retrieved");
        }

        // Run the log sync protocol passing in our local topic logs.
        let (mut dedup, sync_metrics) = {
            let (mut log_sync_sink, mut log_sync_stream) = sync_channels(&mut sink, &mut stream);
            let protocol = LogSync::new_with_capacity(
                self.store.clone(),
                logs,
                self.event_tx.clone(),
                self.buffer_capacity,
            );
            let result = protocol.run(&mut log_sync_sink, &mut log_sync_stream).await;

            // If the log sync session ended with an error, then send a "failed" event and return
            // here with the error itself.
            match result {
                Ok((dedup, metrics)) => {
                    self.event_tx
                        .send(TopicLogSyncEvent::SyncFinished {
                            metrics: metrics.clone().into(),
                        })
                        .map_err(|_| TopicLogSyncChannelError::EventSend)?;

                    (dedup, metrics)
                }
                Err(err) => {
                    self.event_tx
                        .send(TopicLogSyncEvent::Failed {
                            error: err.to_string(),
                        })
                        .map_err(|_| TopicLogSyncChannelError::EventSend)?;

                    log_sync_sink
                        .close()
                        .await
                        .map_err(|err| TopicLogSyncChannelError::MessageSink(format!("{err:?}")))?;

                    return Err(err.into());
                }
            }
        };

        let mut metrics: Metrics = sync_metrics.into();

        let result = match self.live_mode_rx {
            None => Ok(()),
            Some(mut live_mode_rx) => {
                // Enter live-mode.
                //
                // In live-mode we process messages sent from the remote peer and received locally from a
                // subscription or other concurrent sync sessions. In both cases we should deduplicate
                // messages and also check they are part of our topic sub-set selection before forwarding
                // them on the event stream, or to the remote peer.
                let mut close_sent = false;
                self.event_tx
                    .send(TopicLogSyncEvent::LiveModeStarted)
                    .map_err(|_| TopicLogSyncChannelError::EventSend)?;

                loop {
                    tokio::select! {
                        biased;
                        Some(message) = live_mode_rx.next() => {
                            match message {
                                ToSync::Payload(operation) => {
                                    if !dedup.insert(operation.hash) {
                                        trace!(id = ?operation.hash.fmt_short(), "ignore duplicate operation sent on live-mode channel");
                                        continue;
                                    }

                                    metrics.sent_live_bytes +=
                                        operation.header.to_bytes().len() as u32 + operation.header.payload_size;
                                    metrics.sent_live_operations += 1;

                                    trace!(
                                        phase = "live",
                                        id = ?operation.hash.fmt_short(),
                                        sent_ops = ?metrics.sent_live_operations,
                                        sent_bytes = ?metrics.sent_live_bytes,
                                        "sent operation"
                                    );

                                    let result = sink
                                        .send(TopicLogSyncMessage::Live(
                                            operation.header,
                                            operation.body,
                                        ))
                                        .await
                                        .map_err(|err| TopicLogSyncChannelError::MessageSink(format!("{err:?}")).into());

                                    if result.is_err() {
                                        break result;
                                    };
                                }
                                ToSync::Close => {
                                    // We send the close and wait for the remote to close the
                                    // connection.

                                    debug!("closing sync session");

                                    let result = sink
                                        .send(TopicLogSyncMessage::Close)
                                        .await
                                        .map_err(|err| TopicLogSyncChannelError::MessageSink(format!("{err:?}")).into());
                                    if result.is_err() {
                                        break result;
                                    };
                                    close_sent = true;
                                }
                            };
                        }
                        message = stream.next() => {
                            let Some(message) = message else {
                                if close_sent {
                                    break Ok(());
                                }
                                break Err(TopicLogSyncError::UnexpectedStreamClosure);
                            };

                            match message {
                                Ok(message) => {
                                    if let TopicLogSyncMessage::Close = message {
                                        // We received the remotes close message and should close the
                                        // connection ourselves.
                                        debug!("received close message from remote");
                                        break Ok(());
                                    };

                                    let TopicLogSyncMessage::Live(header, body) = message else {
                                        break Err(TopicLogSyncError::UnexpectedProtocolMessage(
                                            message.to_string(),
                                        ));
                                    };

                                    // TODO: check that this message is a part of our topic T set.

                                    // Insert operation hash into deduplication buffer and if it was
                                    // previously present do not forward the operation to the application
                                    // layer.
                                    if !dedup.insert(header.hash()) {
                                        trace!(phase = "live", operation_id = ?header.hash().fmt_short(), "ignore duplicate operation sent from remote");
                                        continue;
                                    }

                                    metrics.received_live_bytes += header.to_bytes().len() as u32 + header.payload_size;
                                    metrics.received_live_operations += 1;

                                    trace!(
                                        phase = "live",
                                        operation_id = ?header.hash().fmt_short(),
                                        received_ops = %metrics.received_live_operations,
                                        received_bytes = %metrics.received_live_bytes,
                                        "received operation"
                                    );

                                    self.event_tx
                                        .send(TopicLogSyncEvent::OperationReceived{operation: Box::new(Operation {
                                            hash: header.hash(),
                                            header,
                                            body,
                                        }), metrics: metrics.clone()})
                                        .map_err(|_| TopicLogSyncChannelError::EventSend)?;
                                }
                                Err(err) => {
                                    if close_sent {
                                        break Ok(());
                                    }
                                    break Err(TopicLogSyncError::DecodeMessage(format!("{err:?}")));
                                }
                            }
                        }
                    }
                }
            }
        };

        sink.close()
            .await
            .map_err(|err| TopicLogSyncChannelError::MessageSink(format!("{err:?}")))?;

        let final_event = match result.as_ref() {
            Ok(_) => {
                debug!(
                    sent_ops = ?metrics.sent_operations(),
                    sent_bytes = ?metrics.sent_bytes(),
                    received_ops = %metrics.received_operations(),
                    received_bytes = %metrics.received_bytes(),
                    "sync session closed"
                );
                TopicLogSyncEvent::SessionFinished { metrics }
            }
            Err(err) => {
                warn!(error = ?err, "sync session closed with error");
                TopicLogSyncEvent::Failed {
                    error: err.to_string(),
                }
            }
        };

        self.event_tx
            .send(final_event)
            .map_err(|_| TopicLogSyncChannelError::EventSend)?;

        result
    }
}

/// Map raw message sink and stream into log sync protocol specific channels.
#[allow(clippy::complexity)]
fn sync_channels<'a, L, E>(
    sink: &mut (impl Sink<TopicLogSyncMessage<L, E>, Error = impl Debug> + Unpin),
    stream: &mut (impl Stream<Item = Result<TopicLogSyncMessage<L, E>, impl Debug>> + Unpin),
) -> (
    impl Sink<LogSyncMessage<L>, Error = TopicLogSyncChannelError> + Unpin,
    impl Stream<Item = Result<LogSyncMessage<L>, TopicLogSyncChannelError>> + Unpin,
)
where
    L: LogId,
    E: Extensions,
{
    let log_sync_sink = LogSyncSink::new(sink);

    let log_sync_stream = stream.by_ref().map(|message| match message {
        Ok(TopicLogSyncMessage::Sync(message)) => Ok(message),
        Ok(TopicLogSyncMessage::Live { .. }) | Ok(TopicLogSyncMessage::Close) => Err(
            TopicLogSyncChannelError::MessageStream("non-protocol message received".to_string()),
        ),
        Err(err) => Err(TopicLogSyncChannelError::MessageStream(format!("{err:?}"))),
    });

    (log_sync_sink, log_sync_stream)
}

/// Error type occurring in topic log sync channels.
#[derive(Debug, Error)]
pub enum TopicLogSyncChannelError {
    #[error("error sending on message sink: {0}")]
    MessageSink(String),

    #[error("error receiving from message stream: {0}")]
    MessageStream(String),

    #[error("no active receivers for broadcast")]
    EventSend,
}

/// Error type occurring in topic log sync protocol.
#[derive(Debug, Error)]
pub enum TopicLogSyncError {
    #[error(transparent)]
    Sync(#[from] LogSyncError),

    #[error("topic store error: {0}")]
    TopicStore(String),

    #[error("unexpected protocol message: {0}")]
    UnexpectedProtocolMessage(String),

    #[error(transparent)]
    Channel(#[from] TopicLogSyncChannelError),

    #[error("remote unexpectedly closed stream in live-mode")]
    UnexpectedStreamClosure,

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

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Metrics {
    pub outbound_sync_bytes: u32,
    pub outbound_sync_operations: u32,
    pub inbound_sync_bytes: u32,
    pub inbound_sync_operations: u32,
    pub sent_sync_bytes: u32,
    pub sent_sync_operations: u32,
    pub received_sync_bytes: u32,
    pub received_sync_operations: u32,
    pub sent_live_bytes: u32,
    pub sent_live_operations: u32,
    pub received_live_bytes: u32,
    pub received_live_operations: u32,
}

impl Metrics {
    pub fn sent_bytes(&self) -> u32 {
        self.sent_sync_bytes + self.sent_live_bytes
    }

    pub fn received_bytes(&self) -> u32 {
        self.received_sync_bytes + self.received_live_bytes
    }

    pub fn sent_operations(&self) -> u32 {
        self.sent_sync_operations + self.sent_live_operations
    }

    pub fn received_operations(&self) -> u32 {
        self.received_sync_operations + self.received_live_operations
    }
}

impl From<LogSyncMetrics> for Metrics {
    fn from(value: LogSyncMetrics) -> Metrics {
        Metrics {
            inbound_sync_bytes: value.inbound_bytes,
            outbound_sync_bytes: value.outbound_bytes,
            sent_sync_bytes: value.sent_bytes,
            received_sync_bytes: value.received_bytes,
            inbound_sync_operations: value.inbound_operations,
            outbound_sync_operations: value.outbound_operations,
            sent_sync_operations: value.sent_operations,
            received_sync_operations: value.received_operations,
            ..Default::default()
        }
    }
}

/// Events emitted from topic log sync sessions.
#[derive(Debug, Clone, PartialEq)]
pub enum TopicLogSyncEvent<E = ()> {
    /// A session has been initiated locally.
    ///
    /// This event is always sent and will be followed by `SyncStarted` or `Failed` events.
    SessionStarted,

    /// We have exchanged initial session metrics with the remote and the sync phase of this
    /// session has started.
    ///
    /// This event will be followed by any number of `OperationReceived` events, or a `SyncFinished` or `Failed`.
    SyncStarted { metrics: Metrics },

    /// All past state has been replicated and we will now enter live mode `LiveModeStarted` (if configured) or the
    /// session will end `SessionFinished`.
    ///
    /// This event will be followed by a `LiveModeStarted` event or a `SyncFinished` or `Failed` event.
    SyncFinished { metrics: Metrics },

    /// The session has entered live mode, we will send and receive operations in realtime.
    ///
    /// This event will be followed by any number of `OperationReceived` events or a `SyncFinished` or `Failed` event.
    LiveModeStarted,

    /// An operation has been received, this can be in the "sync" or "live mode" phase of a session.
    OperationReceived {
        operation: Box<Operation<E>>,
        metrics: Metrics,
    },

    /// The session has finished.
    ///
    /// When no error occurs this event will always be sent at the end of a session.
    SessionFinished { metrics: Metrics },

    /// The session failed.
    ///
    /// This event will always be the final event sent when an error occurred in any phase of the
    /// session.
    Failed { error: String },
}

impl<E> From<LogSyncEvent<E>> for TopicLogSyncEvent<E> {
    fn from(event: LogSyncEvent<E>) -> Self {
        match event {
            LogSyncEvent::MetricsExchanged { metrics } => TopicLogSyncEvent::SyncStarted {
                metrics: metrics.into(),
            },
            LogSyncEvent::OperationReceived { operation, metrics } => {
                TopicLogSyncEvent::OperationReceived {
                    operation,
                    metrics: metrics.into(),
                }
            }
        }
    }
}

/// Protocol message types.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(bound(deserialize = "L: LogId"))]
#[allow(clippy::large_enum_variant)]
pub enum TopicLogSyncMessage<L, E>
where
    L: LogId,
    E: Extensions,
{
    Sync(LogSyncMessage<L>),
    Live(Header<E>, Option<Body>),
    Close,
}

impl<L, E> std::fmt::Display for TopicLogSyncMessage<L, E>
where
    L: LogId,
    E: Extensions,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let value = match self {
            TopicLogSyncMessage::Sync(_) => "sync",
            TopicLogSyncMessage::Live(_, _) => "live",
            TopicLogSyncMessage::Close => "close",
        };
        write!(f, "{value}")
    }
}

pin_project! {
    /// Sink wrapper which converts messages and errors into the expected types.
    pub struct LogSyncSink<S, L, E> {
        #[pin]
        inner: S,
        _phantom: std::marker::PhantomData<(L, E)>,
    }
}

impl<S, L, E> LogSyncSink<S, L, E> {
    pub fn new(inner: S) -> Self {
        Self {
            inner,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<S, L, E> Sink<LogSyncMessage<L>> for LogSyncSink<S, L, E>
where
    L: LogId,
    S: Sink<TopicLogSyncMessage<L, E>>,
    S::Error: Debug,
    E: Extensions,
{
    type Error = TopicLogSyncChannelError;

    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        let this = self.project();
        this.inner
            .poll_ready(cx)
            .map_err(|err| TopicLogSyncChannelError::MessageSink(format!("{err:?}")))
    }

    fn start_send(self: Pin<&mut Self>, item: LogSyncMessage<L>) -> Result<(), Self::Error> {
        let this = self.project();
        let msg = TopicLogSyncMessage::Sync(item);
        this.inner
            .start_send(msg)
            .map_err(|err| TopicLogSyncChannelError::MessageSink(format!("{err:?}")))
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        let this = self.project();
        this.inner
            .poll_flush(cx)
            .map_err(|err| TopicLogSyncChannelError::MessageSink(format!("{err:?}")))
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        let this = self.project();
        this.inner
            .poll_close(cx)
            .map_err(|err| TopicLogSyncChannelError::MessageSink(format!("{err:?}")))
    }
}
#[cfg(test)]
pub mod tests {
    use std::collections::BTreeMap;

    use futures_channel::mpsc;
    use futures_util::{SinkExt, StreamExt};
    use p2panda_core::test_utils::setup_logging;
    use p2panda_core::{Body, Operation, Topic};

    use crate::ToSync;
    use crate::protocols::{LogSyncError, LogSyncMessage};
    use crate::test_utils::{Peer, TestTopicSyncMessage, run_protocol, run_protocol_uni};
    use crate::traits::Protocol;

    use super::{TopicLogSyncError, TopicLogSyncEvent};

    #[tokio::test]
    async fn sync_session_no_operations() {
        let topic = Topic::random();
        let mut peer = Peer::new(0).await;
        peer.associate(&topic, &BTreeMap::default()).await;

        let (session, mut events_rx, _) = peer.topic_sync_protocol(topic.clone(), false);

        let (_, remote_rx) = run_protocol_uni(
            session,
            &[
                TestTopicSyncMessage::Sync(LogSyncMessage::Have(BTreeMap::default())),
                TestTopicSyncMessage::Sync(LogSyncMessage::Done),
            ],
        )
        .await
        .unwrap();

        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncStarted { .. }
        );
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncFinished { .. }
        );
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SessionFinished { .. }
        );

        let messages = remote_rx.collect::<Vec<_>>().await;
        assert_eq!(messages.len(), 2);
        for (index, message) in messages.into_iter().enumerate() {
            match index {
                0 => assert_eq!(
                    message,
                    TestTopicSyncMessage::Sync(LogSyncMessage::Have(BTreeMap::default()))
                ),
                1 => {
                    assert_eq!(message, TestTopicSyncMessage::Sync(LogSyncMessage::Done));
                    break;
                }
                _ => panic!(),
            };
        }
    }

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

        let log_id = 0;
        let topic = Topic::random();
        let mut peer = Peer::new(0).await;

        let body = Body::new("Hello, Sloth!".as_bytes());
        let (header_0, header_bytes_0) = peer.create_operation(&body, log_id).await;
        let (header_1, header_bytes_1) = peer.create_operation(&body, log_id).await;
        let (header_2, header_bytes_2) = peer.create_operation(&body, log_id).await;

        let logs = BTreeMap::from([(peer.id(), vec![log_id])]);
        peer.associate(&topic, &logs).await;

        let (session, mut events_rx, _) = peer.topic_sync_protocol(topic.clone(), false);

        let (_, remote_rx) = run_protocol_uni(
            session,
            &[
                TestTopicSyncMessage::Sync(LogSyncMessage::Have(BTreeMap::default())),
                TestTopicSyncMessage::Sync(LogSyncMessage::Done),
            ],
        )
        .await
        .unwrap();

        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncStarted { .. }
        );
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncFinished { .. }
        );
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SessionFinished { .. }
        );

        let messages = remote_rx.collect::<Vec<_>>().await;
        assert_eq!(messages.len(), 6);
        for (index, message) in messages.into_iter().enumerate() {
            match index {
                0 => assert_eq!(
                    message,
                    TestTopicSyncMessage::Sync(LogSyncMessage::Have(BTreeMap::from([(
                        peer.id(),
                        BTreeMap::from([(0, 2)])
                    )])))
                ),
                1 => {
                    let expected_bytes = header_0.payload_size
                        + header_bytes_0.len() as u32
                        + header_1.payload_size
                        + header_bytes_1.len() as u32
                        + header_2.payload_size
                        + header_bytes_2.len() as u32;

                    assert_eq!(
                        message,
                        TestTopicSyncMessage::Sync(LogSyncMessage::PreSync {
                            total_operations: 3,
                            total_bytes: expected_bytes
                        })
                    )
                }
                2 => {
                    let TestTopicSyncMessage::Sync(LogSyncMessage::Operation(
                        header,
                        Some(body_inner),
                    )) = message
                    else {
                        panic!("Not a TestTopicSyncMessage::Sync: {message:?}");
                    };
                    assert_eq!(header, header_bytes_0);
                    assert_eq!(Body::new(&body_inner), body)
                }
                3 => {
                    let TestTopicSyncMessage::Sync(LogSyncMessage::Operation(
                        header,
                        Some(body_inner),
                    )) = message
                    else {
                        panic!("Not a TestTopicSyncMessage::Sync: {message:?}");
                    };
                    assert_eq!(header, header_bytes_1);
                    assert_eq!(Body::new(&body_inner), body)
                }
                4 => {
                    let TestTopicSyncMessage::Sync(LogSyncMessage::Operation(
                        header,
                        Some(body_inner),
                    )) = message
                    else {
                        panic!("Not a TestTopicSyncMessage::Sync: {message:?}");
                    };
                    assert_eq!(header, header_bytes_2);
                    assert_eq!(Body::new(&body_inner), body)
                }
                5 => {
                    assert_eq!(message, TestTopicSyncMessage::Sync(LogSyncMessage::Done));
                    break;
                }
                _ => panic!(),
            };
        }
    }

    #[tokio::test]
    async fn topic_log_sync_full_duplex() {
        setup_logging();
        let topic = Topic::random();
        let log_id = 0;

        let mut peer_a = Peer::new(0).await;
        let mut peer_b = Peer::new(1).await;

        let body = Body::new("Hello, Sloth!".as_bytes());
        let (header_0, _) = peer_a.create_operation(&body, 0).await;
        let (header_1, _) = peer_a.create_operation(&body, 0).await;
        let (header_2, _) = peer_a.create_operation(&body, 0).await;

        let logs = BTreeMap::from([(peer_a.id(), vec![log_id])]);
        peer_a.associate(&topic, &logs).await;

        let (peer_a_session, mut peer_a_events_rx, _) =
            peer_a.topic_sync_protocol(topic.clone(), false);

        let (peer_b_session, mut peer_b_events_rx, _) =
            peer_b.topic_sync_protocol(topic.clone(), false);

        run_protocol(peer_a_session, peer_b_session).await.unwrap();

        // Assert peer a events.
        std::assert_matches!(
            peer_a_events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncStarted { .. }
        );
        std::assert_matches!(
            peer_a_events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncFinished { .. }
        );
        std::assert_matches!(
            peer_a_events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SessionFinished { .. }
        );

        // Assert peer b events.
        std::assert_matches!(
            peer_b_events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncStarted { .. }
        );
        let recv = peer_b_events_rx.recv().await.unwrap();
        let TopicLogSyncEvent::OperationReceived { operation, .. } = recv else {
            panic!("Not a TopicLogSyncEvent::OperationReceived: {recv:?}");
        };
        let Operation {
            header,
            body: body_inner,
            ..
        } = *operation;
        assert_eq!(header, header_0);
        assert_eq!(body_inner.unwrap(), body);
        let recv = peer_b_events_rx.recv().await.unwrap();
        let TopicLogSyncEvent::OperationReceived { operation, .. } = recv else {
            panic!("Not a TopicLogSyncEvent::OperationReceived: {recv:?}");
        };
        let Operation {
            header,
            body: body_inner,
            ..
        } = *operation;
        assert_eq!(header, header_1);
        assert_eq!(body_inner.unwrap(), body);
        let recv = peer_b_events_rx.recv().await.unwrap();
        let TopicLogSyncEvent::OperationReceived { operation, .. } = recv else {
            panic!("Not a TopicLogSyncEvent::OperationReceived: {recv:?}");
        };
        let Operation {
            header,
            body: body_inner,
            ..
        } = *operation;
        assert_eq!(header, header_2);
        assert_eq!(body_inner.unwrap(), body);
        std::assert_matches!(
            peer_b_events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncFinished { .. }
        );
        std::assert_matches!(
            peer_b_events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SessionFinished { .. }
        );
    }

    #[tokio::test]
    async fn live_mode() {
        let log_id = 0;
        let topic = Topic::random();
        let mut peer_a = Peer::new(0).await;
        let mut peer_b = Peer::new(1).await;

        let body = Body::new("Hello, Sloth!".as_bytes());
        let (header_0, header_bytes_0) = peer_b.create_operation(&body, log_id).await;

        let logs = BTreeMap::from([(peer_a.id(), vec![log_id])]);
        peer_a.associate(&topic, &logs).await;

        let logs = BTreeMap::default();
        peer_a.associate(&topic, &logs).await;

        let (header_1, _) = peer_b.create_operation_no_insert(&body, log_id).await;
        let expected_bytes_received = header_0.payload_size
            + header_0.to_bytes().len() as u32
            + header_1.payload_size
            + header_1.to_bytes().len() as u32;
        let (header_2, _) = peer_a.create_operation_no_insert(&body, log_id).await;
        let expected_bytes_sent = header_2.payload_size + header_2.to_bytes().len() as u32;

        let (protocol, mut events_rx, mut live_mode_tx) =
            peer_a.topic_sync_protocol(topic.clone(), true);

        live_mode_tx
            .send(ToSync::Payload(Operation {
                hash: header_2.hash(),
                header: header_2.clone(),
                body: Some(body.clone()),
            }))
            .await
            .unwrap();
        live_mode_tx.send(ToSync::Close).await.unwrap();

        let total_bytes = header_bytes_0.len() + body.to_bytes().len();
        let (_, remote_rx) = run_protocol_uni(
            protocol,
            &[
                TestTopicSyncMessage::Sync(LogSyncMessage::Have(BTreeMap::default())),
                TestTopicSyncMessage::Sync(LogSyncMessage::PreSync {
                    total_operations: 1,
                    total_bytes: total_bytes as u32,
                }),
                TestTopicSyncMessage::Sync(LogSyncMessage::Operation(
                    header_bytes_0,
                    Some(body.to_bytes()),
                )),
                TestTopicSyncMessage::Sync(LogSyncMessage::Done),
                TestTopicSyncMessage::Live(header_1.clone(), Some(body.clone())),
                TestTopicSyncMessage::Close,
            ],
        )
        .await
        .unwrap();

        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncStarted { .. }
        );
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::OperationReceived { .. }
        );
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncFinished { .. }
        );
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::LiveModeStarted
        );
        let TopicLogSyncEvent::OperationReceived { metrics, .. } = events_rx.recv().await.unwrap()
        else {
            panic!("Not a TopicLogSyncEvent::OperationReceived");
        };
        assert_eq!(metrics.received_operations(), 2);
        assert_eq!(metrics.sent_operations(), 1);
        assert_eq!(metrics.received_bytes(), expected_bytes_received);
        assert_eq!(metrics.sent_bytes(), expected_bytes_sent);
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SessionFinished { .. }
        );

        let messages = remote_rx.collect::<Vec<_>>().await;
        assert_eq!(messages.len(), 4);
        for (index, message) in messages.into_iter().enumerate() {
            match index {
                0 => std::assert_matches!(
                    message,
                    TestTopicSyncMessage::Sync(LogSyncMessage::Have(_))
                ),
                1 => {
                    std::assert_matches!(message, TestTopicSyncMessage::Sync(LogSyncMessage::Done))
                }
                2 => {
                    let TestTopicSyncMessage::Live(header, Some(body_inner)) = message else {
                        panic!("Not a TestTopicSyncMessage::Live");
                    };
                    assert_eq!(header, header_2);
                    assert_eq!(body_inner, body);
                }
                3 => {
                    std::assert_matches!(message, TestTopicSyncMessage::Close)
                }
                _ => panic!(),
            };
        }
    }

    #[tokio::test]
    async fn dedup_live_mode_messages() {
        let log_id = 0;
        let topic = Topic::random();
        let mut peer_a = Peer::new(0).await;
        let mut peer_b = Peer::new(1).await;

        let body = Body::new("Hello, Sloth!".as_bytes());
        let (header_0, header_bytes_0) = peer_b.create_operation(&body, log_id).await;

        let logs = BTreeMap::from([(peer_a.id(), vec![log_id])]);
        peer_a.associate(&topic, &logs).await;

        let logs = BTreeMap::default();
        peer_a.associate(&topic, &logs).await;

        let (header_1, _) = peer_b.create_operation_no_insert(&body, log_id).await;
        let expected_bytes_received = header_0.payload_size
            + header_0.to_bytes().len() as u32
            + header_1.payload_size
            + header_1.to_bytes().len() as u32;
        let (header_2, _) = peer_a.create_operation_no_insert(&body, log_id).await;
        let expected_bytes_sent = header_2.payload_size + header_2.to_bytes().len() as u32;

        let (protocol, mut events_rx, mut live_mode_tx) =
            peer_a.topic_sync_protocol(topic.clone(), true);

        live_mode_tx
            .send(ToSync::Payload(Operation {
                hash: header_2.hash(),
                header: header_2.clone(),
                body: Some(body.clone()),
            }))
            .await
            .unwrap();

        // Sending subscription message twice.
        live_mode_tx
            .send(ToSync::Payload(Operation {
                hash: header_2.hash(),
                header: header_2.clone(),
                body: Some(body.clone()),
            }))
            .await
            .unwrap();

        live_mode_tx.send(ToSync::Close).await.unwrap();

        let total_bytes = header_bytes_0.len() + body.to_bytes().len();
        let (_, remote_rx) = run_protocol_uni(
            protocol,
            &[
                TestTopicSyncMessage::Sync(LogSyncMessage::Have(BTreeMap::default())),
                TestTopicSyncMessage::Sync(LogSyncMessage::PreSync {
                    total_operations: 1,
                    total_bytes: total_bytes as u32,
                }),
                TestTopicSyncMessage::Sync(LogSyncMessage::Operation(
                    header_bytes_0,
                    Some(body.to_bytes()),
                )),
                TestTopicSyncMessage::Sync(LogSyncMessage::Done),
                TestTopicSyncMessage::Live(header_1.clone(), Some(body.clone())),
                // Duplicate of message sent during sync.
                TestTopicSyncMessage::Live(header_0.clone(), Some(body.clone())),
                // Duplicate of message sent earlier in live-mode.
                TestTopicSyncMessage::Live(header_1.clone(), Some(body.clone())),
                TestTopicSyncMessage::Close,
            ],
        )
        .await
        .unwrap();

        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncStarted { .. }
        );
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::OperationReceived { .. }
        );
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SyncFinished { .. }
        );
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::LiveModeStarted
        );
        let TopicLogSyncEvent::OperationReceived { metrics, .. } = events_rx.recv().await.unwrap()
        else {
            panic!("Not a TopicLogSyncEvent::OperationReceived");
        };
        assert_eq!(metrics.received_operations(), 2);
        assert_eq!(metrics.sent_operations(), 1);
        assert_eq!(metrics.received_bytes(), expected_bytes_received);
        assert_eq!(metrics.sent_bytes(), expected_bytes_sent);
        std::assert_matches!(
            events_rx.recv().await.unwrap(),
            TopicLogSyncEvent::SessionFinished { .. }
        );

        let messages = remote_rx.collect::<Vec<_>>().await;
        assert_eq!(messages.len(), 4);
        for (index, message) in messages.into_iter().enumerate() {
            match index {
                0 => std::assert_matches!(
                    message,
                    TestTopicSyncMessage::Sync(LogSyncMessage::Have(_))
                ),
                1 => {
                    std::assert_matches!(message, TestTopicSyncMessage::Sync(LogSyncMessage::Done))
                }
                2 => {
                    std::assert_matches!(message, TestTopicSyncMessage::Live(_, Some(_)));
                    let TestTopicSyncMessage::Live(header, Some(body_inner)) = message else {
                        unreachable!();
                    };
                    assert_eq!(header, header_2);
                    assert_eq!(body_inner, body);
                }
                3 => {
                    std::assert_matches!(message, TestTopicSyncMessage::Close)
                }
                _ => panic!(),
            };
        }
    }

    #[tokio::test]
    async fn unexpected_stream_closure_sync() {
        let topic = Topic::random();
        let mut peer = Peer::new(0).await;
        peer.associate(&topic, &Default::default()).await;

        let (session, mut events_rx, _live_tx) = peer.topic_sync_protocol(topic.clone(), true);

        let messages = [TestTopicSyncMessage::Sync(LogSyncMessage::Have(
            BTreeMap::default(),
        ))];

        let (mut local_message_tx, _remote_message_rx) = mpsc::channel(128);
        let (mut remote_message_tx, local_message_rx) = mpsc::channel(128);
        let mut local_message_rx = local_message_rx.map(|message| Ok::<_, ()>(message));

        for message in messages {
            remote_message_tx.send(message.to_owned()).await.unwrap();
        }

        let handle = tokio::spawn(async move {
            session
                .run(&mut local_message_tx, &mut local_message_rx)
                .await
                .expect_err("expected unexpected stream closure")
        });

        drop(remote_message_tx);

        let err = handle.await.unwrap();
        std::assert_matches!(
            err,
            TopicLogSyncError::Sync(LogSyncError::UnexpectedStreamClosure)
        );

        while let Ok(event) = events_rx.recv().await {
            if let TopicLogSyncEvent::Failed { error } = event {
                assert_eq!(
                    error,
                    "remote unexpectedly closed stream during initial sync".to_string()
                );
                break;
            }
        }
    }

    #[tokio::test]
    async fn unexpected_stream_closure_live_mode() {
        let topic = Topic::random();
        let mut peer = Peer::new(0).await;
        peer.associate(&topic, &Default::default()).await;

        let (session, mut events_rx, _live_tx) = peer.topic_sync_protocol(topic.clone(), true);

        let messages = [
            TestTopicSyncMessage::Sync(LogSyncMessage::Have(BTreeMap::default())),
            TestTopicSyncMessage::Sync(LogSyncMessage::Done),
        ];

        let (mut local_message_tx, _remote_message_rx) = mpsc::channel(128);
        let (mut remote_message_tx, local_message_rx) = mpsc::channel(128);
        let mut local_message_rx = local_message_rx.map(|message| Ok::<_, ()>(message));

        for message in messages {
            remote_message_tx.send(message.to_owned()).await.unwrap();
        }

        let handle = tokio::spawn(async move {
            session
                .run(&mut local_message_tx, &mut local_message_rx)
                .await
                .expect_err("expected unexpected stream closure")
        });

        drop(remote_message_tx);

        let err = handle.await.unwrap();
        std::assert_matches!(err, TopicLogSyncError::UnexpectedStreamClosure);

        while let Ok(event) = events_rx.recv().await {
            if let TopicLogSyncEvent::Failed { error } = event {
                assert_eq!(
                    error,
                    "remote unexpectedly closed stream in live-mode".to_string()
                );
                break;
            }
        }
    }
}