derec-library 0.0.2

Rust SDK for the DeRec protocol, including native and WebAssembly bindings.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.

//! Replica catch-up: a member that was offline pulls the current state.
//!
//! A member is never *partially* behind — every sync carries the whole secret
//! and the whole roster, so it simply holds an older version, and any later
//! publish brings it fully current. Convergence is automatic while the group
//! is writing. This closes the remaining gap: a quiet group, where nobody has
//! written since the member came back.
//!
//! The device that was away is the one that knows it was away, so it pulls.
//! No durable per-member sync state is kept anywhere.
//!
//! # The exchange
//!
//! It reuses the discovery and get-share messages, overloaded on the replica
//! path exactly as [`derec_proto::StoreShareRequestMessage`] already is — a
//! share to a helper, the whole secret to a member.
//!
//! 1. [`start`] asks every other member which version it holds.
//! 2. [`answer_versions`] replies with the one secret and version this device
//!    holds. A member serves exactly one secret, unlike a helper.
//! 3. [`collect_version`] accumulates. Once every member has answered, the one
//!    holding the newest state is asked for it; ties resolve to the `Source`.
//! 4. [`answer_share`] replies with the whole `ReplicaSecretPayload` under
//!    `share_algorithm = SHARE_ALGORITHM_REPLICA_SECRET`.
//! 5. [`accept_share`] hydrates it, reporting install or update as usual.
//!
//! A member reporting an *older* version than the asker is ignored: the asker
//! is already current with respect to it. The check never turns into a
//! publish — a read-shaped operation that silently wrote to peers would
//! surprise its caller, and that peer's own application is the right place to
//! decide whether being behind matters.

use super::super::{
    DeRecChannelStore, DeRecEvent, DeRecSecretStore, DeRecStateStore, DeRecTransport,
    DeRecUserSecretStore, SecretKind, SecretValue, StateItem, StateKey,
};
use crate::derec_message::{DeRecMessageBuilder, current_timestamp};
use crate::primitives::sharing::request::SHARE_ALGORITHM_REPLICA_SECRET;
use crate::protocol::types::ReplicaMember;
use crate::types::{ChannelId, ReplicaId, SharedKey};
use crate::{Error, Result};
use derec_proto::{
    DeRecResult, GetSecretIdsVersionsRequestMessage, GetSecretIdsVersionsResponseMessage,
    GetShareRequestMessage, GetShareResponseMessage, MessageBody, StatusEnum,
    get_secret_ids_versions_response_message::VersionList,
    get_secret_ids_versions_response_message::version_list::VersionEntry,
};
use prost::Message as _;
use std::collections::{HashMap, HashSet};

#[cfg(target_arch = "wasm32")]
use crate::interop::wasm::now_secs;
#[cfg(not(target_arch = "wasm32"))]
use crate::utils::now_secs;

/// Ask every other member which version it holds.
///
/// Returns `Ok(None)` when there is nobody to ask — a device with no peers is
/// trivially current, and starting a round that can never complete would
/// leave state behind for the timeout sweep to clear.
#[cfg_attr(feature = "logging", tracing::instrument(skip_all, fields(secret_id)))]
#[allow(clippy::too_many_arguments)]
pub(in crate::protocol) async fn start<
    Ch: DeRecChannelStore,
    Ss: DeRecSecretStore,
    Us: DeRecUserSecretStore,
    St: DeRecStateStore,
    T: DeRecTransport,
>(
    channel_store: &mut Ch,
    secret_store: &mut Ss,
    user_secret_store: &Us,
    state_store: &mut St,
    transport: &T,
    secret_id: u64,
    local_replica_id: Option<u64>,
    own_transport: &derec_proto::TransportProtocol,
) -> Result<Vec<DeRecEvent>> {
    let own = local_replica_id.ok_or(Error::ReplicaIdNotConfigured)?;

    let peers: Vec<ReplicaMember> = channel_store
        .replicas(secret_id)
        .await?
        .into_iter()
        .filter(|m| m.replica_id.0 != own)
        .collect();

    if peers.is_empty() {
        return Ok(Vec::new());
    }

    let local_version = user_secret_store
        .load_latest(secret_id)
        .await?
        .map(|s| s.version)
        .unwrap_or(0);

    let group_channel = peers[0].channel_id;
    let key = load_channel_key(secret_store, secret_id, group_channel).await?;

    let mut asked: HashSet<ReplicaId> = HashSet::new();
    for peer in &peers {
        let timestamp = current_timestamp();
        let request = GetSecretIdsVersionsRequestMessage {
            timestamp: Some(timestamp),
            // The asker names itself so the answer can be routed back to a
            // member rather than treated as an owner ↔ helper exchange.
            replica_id: Some(own),
            reply_to: Some(own_transport.clone()),
        };
        let envelope = DeRecMessageBuilder::channel()
            .channel_id(peer.channel_id)
            .timestamp(timestamp)
            .message_body(MessageBody::GetSecretIdsVersionsRequest(request))
            .encrypt(&key)?
            .build()?
            .encode_to_vec();

        // A peer we cannot reach is simply not waited on: it can never
        // answer, and counting it would stall the check until the timeout.
        if transport.send(&peer.transport, envelope).await.is_ok() {
            asked.insert(peer.replica_id);
        }
    }

    if asked.is_empty() {
        return Ok(vec![DeRecEvent::SyncCheckComplete {
            local_version,
            group_version: 0,
            fetched_from: None,
        }]);
    }

    state_store
        .save(
            secret_id,
            StateItem::PendingSyncCheck {
                local_version,
                pending: asked,
                reported: HashMap::new(),
                started_at: now_secs(),
            },
        )
        .await?;

    Ok(Vec::new())
}

/// Answer another member's version query.
///
/// A member serves exactly one secret, so the response carries exactly one
/// entry — unlike a helper, which may hold many.
#[allow(clippy::too_many_arguments)]
pub(in crate::protocol) async fn answer_versions<Us: DeRecUserSecretStore, T: DeRecTransport>(
    user_secret_store: &Us,
    transport: &T,
    member: &ReplicaMember,
    request: &GetSecretIdsVersionsRequestMessage,
    shared_key: SharedKey,
    secret_id: u64,
    local_replica_id: Option<u64>,
    inbound_trace_id: u64,
) -> Result<Vec<DeRecEvent>> {
    let version = user_secret_store
        .load_latest(secret_id)
        .await?
        .map(|s| s.version);

    let timestamp = current_timestamp();
    let response = GetSecretIdsVersionsResponseMessage {
        result: Some(DeRecResult {
            status: StatusEnum::Ok as i32,
            memo: String::new(),
        }),
        // A device holding no snapshot reports nothing rather than version 0,
        // which would be indistinguishable from "I hold the empty version".
        secret_list: version
            .map(|version| VersionList {
                secret_id,
                versions: vec![VersionEntry {
                    version,
                    version_description: String::new(),
                }],
            })
            .into_iter()
            .collect(),
        timestamp: Some(timestamp),
        replica_id: local_replica_id,
    };

    let envelope = DeRecMessageBuilder::channel()
        .channel_id(member.channel_id)
        .timestamp(timestamp)
        .message_body(MessageBody::GetSecretIdsVersionsResponse(response))
        .encrypt(&shared_key)?
        .build()?
        .encode_to_vec();
    let envelope = super::apply_trace_id(envelope, inbound_trace_id)?;
    let endpoint = request
        .reply_to
        .clone()
        .unwrap_or_else(|| member.transport.clone());
    transport.send(&endpoint, envelope).await?;

    Ok(vec![DeRecEvent::NoOp])
}

/// Record one member's reported version; fetch once everyone has answered.
#[allow(clippy::too_many_arguments)]
pub(in crate::protocol) async fn collect_version<
    Ss: DeRecSecretStore,
    St: DeRecStateStore,
    Ch: DeRecChannelStore,
    T: DeRecTransport,
>(
    channel_store: &Ch,
    secret_store: &mut Ss,
    state_store: &mut St,
    transport: &T,
    secret_id: u64,
    from: ReplicaId,
    response: &GetSecretIdsVersionsResponseMessage,
    local_replica_id: Option<u64>,
    own_transport: &derec_proto::TransportProtocol,
) -> Result<Vec<DeRecEvent>> {
    let Some(StateItem::PendingSyncCheck {
        local_version,
        mut pending,
        mut reported,
        started_at,
    }) = state_store
        .load(secret_id, StateKey::PendingSyncCheck)
        .await?
    else {
        // No check in flight — a late answer from an abandoned one.
        return Ok(vec![DeRecEvent::NoOp]);
    };

    if pending.remove(&from) {
        // A member with no snapshot reports no entry at all; treat that as
        // version 0 rather than skipping it, so the tally stays complete.
        let version = response
            .secret_list
            .iter()
            .flat_map(|list| list.versions.iter())
            .map(|v| v.version)
            .max()
            .unwrap_or(0);
        reported.insert(from, version);
    }

    if !pending.is_empty() {
        state_store
            .save(
                secret_id,
                StateItem::PendingSyncCheck {
                    local_version,
                    pending,
                    reported,
                    started_at,
                },
            )
            .await?;
        return Ok(vec![DeRecEvent::NoOp]);
    }

    state_store
        .remove(secret_id, StateKey::PendingSyncCheck)
        .await?;
    finish(
        channel_store,
        secret_store,
        transport,
        secret_id,
        local_version,
        &reported,
        local_replica_id,
        own_transport,
    )
    .await
}

/// Choose the member holding the newest state and ask it, or report that this
/// device is already current.
#[allow(clippy::too_many_arguments)]
async fn finish<Ch: DeRecChannelStore, Ss: DeRecSecretStore, T: DeRecTransport>(
    channel_store: &Ch,
    secret_store: &mut Ss,
    transport: &T,
    secret_id: u64,
    local_version: u32,
    reported: &HashMap<ReplicaId, u32>,
    local_replica_id: Option<u64>,
    own_transport: &derec_proto::TransportProtocol,
) -> Result<Vec<DeRecEvent>> {
    let group_version = reported.values().copied().max().unwrap_or(0);

    if group_version <= local_version {
        // Members reporting an older version are ignored: this device is
        // current with respect to them, and the check never writes to a peer.
        return Ok(vec![DeRecEvent::SyncCheckComplete {
            local_version,
            group_version,
            fetched_from: None,
        }]);
    }

    let roster = channel_store.replicas(secret_id).await?;
    let source: Option<ReplicaId> = roster
        .iter()
        .find(|m| m.role == crate::protocol::types::ReplicaRole::Source)
        .map(|m| m.replica_id);

    // Ties resolve to the Source; failing that, to the lowest id, so the
    // choice is deterministic rather than dependent on map iteration order.
    let winner = reported
        .iter()
        .filter(|(_, v)| **v == group_version)
        .map(|(id, _)| *id)
        .min_by_key(|id| (Some(*id) != source, id.0))
        .ok_or(Error::Invariant(
            "a newer group version was reported by nobody",
        ))?;

    let member = roster
        .iter()
        .find(|m| m.replica_id == winner)
        .ok_or(Error::InvalidInput(
            "the member holding the newest state is no longer in the roster",
        ))?;

    let key = load_channel_key(secret_store, secret_id, member.channel_id).await?;
    let timestamp = current_timestamp();
    let request = GetShareRequestMessage {
        secret_id,
        version: group_version,
        timestamp: Some(timestamp),
        reply_to: Some(own_transport.clone()),
        replica_id: local_replica_id,
    };
    let envelope = DeRecMessageBuilder::channel()
        .channel_id(member.channel_id)
        .timestamp(timestamp)
        .message_body(MessageBody::GetShareRequest(request))
        .encrypt(&key)?
        .build()?
        .encode_to_vec();
    transport.send(&member.transport, envelope).await?;

    #[cfg(feature = "logging")]
    tracing::info!(
        secret_id,
        local_version,
        group_version,
        from = winner.0,
        "replica catch-up: fetching the newest state"
    );

    Ok(vec![DeRecEvent::SyncCheckComplete {
        local_version,
        group_version,
        fetched_from: Some(winner.0),
    }])
}

/// Serve another member's request for the current state.
///
/// The reply carries the whole secret under
/// `SHARE_ALGORITHM_REPLICA_SECRET`, not a share — the same overload a
/// publish uses on the replica path.
#[allow(clippy::too_many_arguments)]
pub(in crate::protocol) async fn answer_share<
    Ch: DeRecChannelStore,
    Ss: DeRecSecretStore,
    Us: DeRecUserSecretStore,
    T: DeRecTransport,
>(
    channel_store: &mut Ch,
    secret_store: &mut Ss,
    user_secret_store: &Us,
    transport: &T,
    member: &ReplicaMember,
    request: &GetShareRequestMessage,
    shared_key: SharedKey,
    secret_id: u64,
    local_replica_id: Option<u64>,
    inbound_trace_id: u64,
) -> Result<Vec<DeRecEvent>> {
    let composite = super::sharing::build_catch_up_payload(
        channel_store,
        secret_store,
        user_secret_store,
        secret_id,
        local_replica_id,
    )
    .await?;

    let (status, memo, share) = match composite {
        Some(payload) => (StatusEnum::Ok, String::new(), payload.encode_to_vec()),
        None => (
            StatusEnum::UnknownSecretId,
            "this device holds no state for that secret".to_owned(),
            Vec::new(),
        ),
    };

    let timestamp = current_timestamp();
    let response = GetShareResponseMessage {
        result: Some(DeRecResult {
            status: status as i32,
            memo,
        }),
        committed_de_rec_share: share,
        // The discriminator: the bytes are the whole secret, not a share.
        share_algorithm: SHARE_ALGORITHM_REPLICA_SECRET,
        timestamp: Some(timestamp),
        secret_id,
        version: request.version,
        replica_id: local_replica_id,
    };

    let envelope = DeRecMessageBuilder::channel()
        .channel_id(member.channel_id)
        .timestamp(timestamp)
        .message_body(MessageBody::GetShareResponse(response))
        .encrypt(&shared_key)?
        .build()?
        .encode_to_vec();
    let envelope = super::apply_trace_id(envelope, inbound_trace_id)?;
    let endpoint = request
        .reply_to
        .clone()
        .unwrap_or_else(|| member.transport.clone());
    transport.send(&endpoint, envelope).await?;

    Ok(vec![DeRecEvent::NoOp])
}

/// Hydrate the state a peer served.
pub(in crate::protocol) async fn accept_share<
    Ch: DeRecChannelStore,
    Ss: DeRecSecretStore,
    Us: DeRecUserSecretStore,
>(
    channel_store: &mut Ch,
    secret_store: &mut Ss,
    user_secret_store: &mut Us,
    secret_id: u64,
    from: ReplicaId,
    response: &GetShareResponseMessage,
) -> Result<Vec<DeRecEvent>> {
    let status = response.result.as_ref().map(|r| r.status).unwrap_or(-1);
    if status != StatusEnum::Ok as i32 {
        return Ok(vec![DeRecEvent::NoOp]);
    }
    if response.share_algorithm != SHARE_ALGORITHM_REPLICA_SECRET {
        return Err(Error::InvalidInput(
            "a member answered a catch-up with a helper share rather than the secret",
        ));
    }

    super::sharing::hydrate_catch_up(
        channel_store,
        secret_store,
        user_secret_store,
        secret_id,
        from.0,
        response.version,
        &response.committed_de_rec_share,
    )
    .await
}

async fn load_channel_key<Ss: DeRecSecretStore>(
    secret_store: &mut Ss,
    secret_id: u64,
    channel_id: ChannelId,
) -> Result<SharedKey> {
    match secret_store
        .load(secret_id, channel_id, SecretKind::SharedKey)
        .await?
    {
        Some(SecretValue::SharedKey(key)) => Ok(key),
        _ => Err(Error::InvalidInput(
            "channel has no shared key — not yet paired",
        )),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::test::{
        InMemChannelStore, InMemPersistedStateStore, InMemSecretStore, InMemUserSecretStore,
        RecordingTransport, run_async,
    };
    use crate::protocol::types::{ChannelRecord, ChannelStatus, ReplicaRole, UserSecrets};

    const SECRET_ID: u64 = 0xCA7C4;
    const GROUP: ChannelId = ChannelId(5001);

    fn endpoint(uri: &str) -> derec_proto::TransportProtocol {
        derec_proto::TransportProtocol {
            uri: uri.to_owned(),
            protocol: derec_proto::Protocol::Https as i32,
        }
    }

    async fn seed(channels: &mut InMemChannelStore, id: u64, role: ReplicaRole, uri: &str) {
        channels
            .save(
                SECRET_ID,
                ChannelRecord::Replica(ReplicaMember {
                    channel_id: GROUP,
                    replica_id: ReplicaId(id),
                    transport: endpoint(uri),
                    communication_info: std::collections::HashMap::new(),
                    role,
                    status: ChannelStatus::Paired,
                    created_at: 0,
                }),
            )
            .await
            .expect("seed member");
    }

    async fn seed_key(secrets: &mut InMemSecretStore) {
        secrets
            .save(SECRET_ID, GROUP, SecretValue::SharedKey([0x11; 32]))
            .await
            .expect("seed key");
    }

    async fn seed_version(store: &mut InMemUserSecretStore, version: u32) {
        store
            .save_latest(
                SECRET_ID,
                UserSecrets {
                    version,
                    secrets: Vec::new(),
                    description: None,
                    replicas: None,
                },
            )
            .await
            .expect("seed snapshot");
    }

    fn versions_response(version: Option<u32>) -> GetSecretIdsVersionsResponseMessage {
        GetSecretIdsVersionsResponseMessage {
            result: Some(DeRecResult {
                status: StatusEnum::Ok as i32,
                memo: String::new(),
            }),
            secret_list: version
                .map(|version| VersionList {
                    secret_id: SECRET_ID,
                    versions: vec![VersionEntry {
                        version,
                        version_description: String::new(),
                    }],
                })
                .into_iter()
                .collect(),
            timestamp: None,
            replica_id: None,
        }
    }

    /// The asker addresses every member but itself — a self-addressed query
    /// could never be answered.
    #[test]
    fn start_asks_every_member_but_itself() {
        run_async(async {
            let mut channels = InMemChannelStore::default();
            let mut secrets = InMemSecretStore::default();
            let mut user_secrets = InMemUserSecretStore::default();
            let mut state = InMemPersistedStateStore::default();
            let transport = RecordingTransport::default();

            seed(&mut channels, 1001, ReplicaRole::Source, "https://alice").await;
            seed(
                &mut channels,
                1002,
                ReplicaRole::Destination,
                "https://alice-2",
            )
            .await;
            seed(
                &mut channels,
                1003,
                ReplicaRole::Destination,
                "https://alice-3",
            )
            .await;
            seed_key(&mut secrets).await;
            seed_version(&mut user_secrets, 5).await;

            super::start(
                &mut channels,
                &mut secrets,
                &user_secrets,
                &mut state,
                &transport,
                SECRET_ID,
                Some(1002),
                &endpoint("https://alice-2"),
            )
            .await
            .expect("start succeeds");

            let sent = transport.sent_uris();
            assert_eq!(sent.len(), 2, "two peers asked, not three");
            assert!(
                !sent.iter().any(|u| u == "https://alice-2"),
                "the asker must not query itself, got {sent:?}"
            );
        });
    }

    /// A device already current reports so and fetches nothing. This is the
    /// "asker is ahead or level" case: peers behind it are ignored, and the
    /// check never turns into a publish.
    #[test]
    fn a_current_device_fetches_nothing() {
        run_async(async {
            let mut channels = InMemChannelStore::default();
            let mut secrets = InMemSecretStore::default();
            let mut state = InMemPersistedStateStore::default();
            let transport = RecordingTransport::default();

            seed(&mut channels, 1001, ReplicaRole::Source, "https://alice").await;
            seed(
                &mut channels,
                1002,
                ReplicaRole::Destination,
                "https://alice-2",
            )
            .await;
            state
                .save(
                    SECRET_ID,
                    StateItem::PendingSyncCheck {
                        local_version: 7,
                        pending: HashSet::from([ReplicaId(1001)]),
                        reported: HashMap::new(),
                        started_at: 0,
                    },
                )
                .await
                .expect("seed check");

            // The only peer reports an older version.
            let events = super::collect_version(
                &channels,
                &mut secrets,
                &mut state,
                &transport,
                SECRET_ID,
                ReplicaId(1001),
                &versions_response(Some(5)),
                Some(1002),
                &endpoint("https://alice-2"),
            )
            .await
            .expect("collect succeeds");

            match events.as_slice() {
                [
                    DeRecEvent::SyncCheckComplete {
                        local_version,
                        group_version,
                        fetched_from,
                    },
                ] => {
                    assert_eq!((*local_version, *group_version), (7, 5));
                    assert!(
                        fetched_from.is_none(),
                        "a member behind us must not be fetched from"
                    );
                }
                other => panic!("expected SyncCheckComplete, got {other:?}"),
            }
            assert!(
                transport.sent_uris().is_empty(),
                "a check that finds nothing newer must not send anything"
            );
        });
    }

    /// The newest member is the one asked, and the fetch is addressed to it.
    #[test]
    fn the_newest_member_is_fetched_from() {
        run_async(async {
            let mut channels = InMemChannelStore::default();
            let mut secrets = InMemSecretStore::default();
            let mut state = InMemPersistedStateStore::default();
            let transport = RecordingTransport::default();

            seed(&mut channels, 1001, ReplicaRole::Source, "https://alice").await;
            seed(
                &mut channels,
                1002,
                ReplicaRole::Destination,
                "https://alice-2",
            )
            .await;
            seed(
                &mut channels,
                1003,
                ReplicaRole::Destination,
                "https://alice-3",
            )
            .await;
            seed_key(&mut secrets).await;
            state
                .save(
                    SECRET_ID,
                    StateItem::PendingSyncCheck {
                        local_version: 5,
                        pending: HashSet::from([ReplicaId(1001), ReplicaId(1003)]),
                        reported: HashMap::new(),
                        started_at: 0,
                    },
                )
                .await
                .expect("seed check");

            // The source is at v6; alice-3 is further ahead at v7.
            let events = super::collect_version(
                &channels,
                &mut secrets,
                &mut state,
                &transport,
                SECRET_ID,
                ReplicaId(1001),
                &versions_response(Some(6)),
                Some(1002),
                &endpoint("https://alice-2"),
            )
            .await
            .expect("first answer");
            assert!(
                matches!(events.as_slice(), [DeRecEvent::NoOp]),
                "the check stays open until every member has answered"
            );

            let events = super::collect_version(
                &channels,
                &mut secrets,
                &mut state,
                &transport,
                SECRET_ID,
                ReplicaId(1003),
                &versions_response(Some(7)),
                Some(1002),
                &endpoint("https://alice-2"),
            )
            .await
            .expect("second answer");

            match events.as_slice() {
                [
                    DeRecEvent::SyncCheckComplete {
                        group_version,
                        fetched_from,
                        ..
                    },
                ] => {
                    assert_eq!(*group_version, 7);
                    assert_eq!(
                        *fetched_from,
                        Some(1003),
                        "the member holding the newest state is the one asked"
                    );
                }
                other => panic!("expected SyncCheckComplete, got {other:?}"),
            }
            assert_eq!(
                transport.sent_uris(),
                vec!["https://alice-3".to_owned()],
                "the fetch is addressed to that member, not broadcast"
            );
        });
    }

    /// A tie resolves to the Source rather than to whichever member the map
    /// happened to yield first.
    #[test]
    fn a_tie_resolves_to_the_source() {
        run_async(async {
            let mut channels = InMemChannelStore::default();
            let mut secrets = InMemSecretStore::default();
            let mut state = InMemPersistedStateStore::default();
            let transport = RecordingTransport::default();

            seed(&mut channels, 1001, ReplicaRole::Source, "https://alice").await;
            seed(
                &mut channels,
                1002,
                ReplicaRole::Destination,
                "https://alice-2",
            )
            .await;
            seed(
                &mut channels,
                1003,
                ReplicaRole::Destination,
                "https://alice-3",
            )
            .await;
            seed_key(&mut secrets).await;
            state
                .save(
                    SECRET_ID,
                    StateItem::PendingSyncCheck {
                        local_version: 5,
                        pending: HashSet::from([ReplicaId(1001)]),
                        reported: HashMap::from([(ReplicaId(1003), 7)]),
                        started_at: 0,
                    },
                )
                .await
                .expect("seed check");

            let events = super::collect_version(
                &channels,
                &mut secrets,
                &mut state,
                &transport,
                SECRET_ID,
                ReplicaId(1001),
                &versions_response(Some(7)),
                Some(1002),
                &endpoint("https://alice-2"),
            )
            .await
            .expect("collect succeeds");

            match events.as_slice() {
                [DeRecEvent::SyncCheckComplete { fetched_from, .. }] => assert_eq!(
                    *fetched_from,
                    Some(1001),
                    "both are at v7; the Source wins the tie"
                ),
                other => panic!("expected SyncCheckComplete, got {other:?}"),
            }
        });
    }
}