corestore 0.1.0

Create and manage collections of related Hypercores, like JavaScript's corestore
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
//! [`Corestore`] provides a way to manage a related group of [`hypercore::Hypercore`]s.
//! Intended to be fully compatible with the [JavaScrpt `corestore`
//! library](https://github.com/holepunchto/corestore).
#![warn(
    missing_debug_implementations,
    missing_docs,
    redundant_lifetimes,
    //unsafe_code,
    non_local_definitions
)]

mod builder;
mod keys;
mod replicate;
mod storage;
use futures::future::{BoxFuture, FutureExt, Shared};
use hypercore_protocol::{discovery_key, DiscoveryKey};
use keys::{key_pair_from_name, DEFAULT_NAMESPACE};
use std::{
    collections::HashMap,
    sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard},
};
use storage::StorageKind;

use hypercore::{
    hypercore_handshake::CipherTrait, replication::CoreMethodsError, Hypercore, HypercoreError,
    PartialKeypair, VerifyingKey,
};

pub use builder::{CorestoreBuilder, CorestoreBuilderError};
pub use replicate::CorestoreConnection;

static MAX_EVENT_QUEUE_CAPACITY: usize = 32;
const CORES_DIR_NAME: &str = "cores";
const PRIMARY_KEY_FILE_NAME: &str = "primary-key";

/// The key [`Corestore`] uses for deriving keys for it's [`hypercore::Hypercore`]s.
pub type PrimaryKey = [u8; 32];
/// Used to prefix names of names of [`hypercore::Hypercore`] to create namespaces
pub type Namespace = [u8; 32];

/// Corestore's Errors
#[non_exhaustive]
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum Error {
    #[error("error from hypercore: {0}")]
    Hypercore(#[from] HypercoreError),
    #[error("error from hypercore CoreMethods: {0}")]
    CoreMethods(#[from] CoreMethodsError),
    #[error("Signature error")]
    Signature(#[from] signature::Error),
    /// IO failed: the replication connection's (such as its peer going away) or storage's.
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    #[error("Invalid primary key")]
    InvalidPrimaryKey,
    #[error("could not build the corestore: {0}")]
    Builder(#[from] CorestoreBuilderError),
    #[error("Could not build corestore because a primary key value was provided, but one already exists on disk at [{0}]")]
    PrimaryKeyConflict(String),
    #[error("libsodium's generichash function did not return `0`. Got: {0}")]
    LibSodiumGenericHash(i32),
    #[error("libsodium's sign_seed_keypair function did not return `0`. Got: {0}")]
    LibSodiumSignSeedKeypair(i32),
    #[error("error reading dirs {0}")]
    ReadDir(std::io::Error),
    /// Opening a core failed. Everyone who asked for the core while it was opening gets the
    /// same error, so it is shared.
    #[error("error opening core: {0}")]
    Opening(Arc<Error>),
}

type Result<T> = std::result::Result<T, Error>;

mod events {
    #![allow(unused)]

    use super::{Result, MAX_EVENT_QUEUE_CAPACITY};
    use hypercore::VerifyingKey;
    use hypercore_protocol::DiscoveryKey;
    use tokio::sync::{broadcast, BarrierWaitResult};

    #[derive(Debug, Clone)]
    /// Coresstore events
    pub enum Event {
        /// A new core was added
        CoreAdded(VerifyingKey),
        /// Corestore is shutting down
        Shutdown,
    }

    #[derive(Debug, Clone)]
    /// Event bus for Corestore. Clones send to the same subscribers.
    pub struct Events {
        channel: broadcast::Sender<Event>,
    }

    impl Events {
        fn new() -> Self {
            Self {
                channel: broadcast::channel(MAX_EVENT_QUEUE_CAPACITY).0,
            }
        }

        pub fn send(&self, evt: Event) -> Result<()> {
            let _ = self.channel.send(evt);
            Ok(())
        }

        pub fn subscribe(&self) -> broadcast::Receiver<Event> {
            self.channel.subscribe()
        }
    }
    impl Default for Events {
        fn default() -> Self {
            Events::new()
        }
    }
}

pub use events::Event as CorestoreEvents;

/// A core being created, shared by everyone who asks for it before it is ready.
type OpeningCore = Shared<BoxFuture<'static, std::result::Result<Hypercore, Arc<Error>>>>;

#[derive(Clone)]
enum CachedCore {
    Ready(Hypercore),
    Opening(OpeningCore),
}

impl std::fmt::Debug for CachedCore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CachedCore::Ready(core) => f.debug_tuple("Ready").field(core).finish(),
            CachedCore::Opening(_) => write!(f, "Opening"),
        }
    }
}

#[derive(Debug, Default)]
struct CoreCache {
    verifying_key_to_cores: HashMap<VerifyingKey, CachedCore>,
}

impl CoreCache {
    fn get(&self, verifying_key: &VerifyingKey) -> Option<CachedCore> {
        self.verifying_key_to_cores.get(verifying_key).cloned()
    }

    fn insert_ready(&mut self, verifying_key: VerifyingKey, core: Hypercore) {
        self.verifying_key_to_cores
            .insert(verifying_key, CachedCore::Ready(core));
    }

    fn insert_opening(&mut self, verifying_key: VerifyingKey, opening: OpeningCore) {
        self.verifying_key_to_cores
            .insert(verifying_key, CachedCore::Opening(opening));
    }

    fn remove(&mut self, verifying_key: &VerifyingKey) {
        self.verifying_key_to_cores.remove(verifying_key);
    }

    /// The keys of every core that has finished opening.
    fn verifying_keys(&self) -> Vec<VerifyingKey> {
        self.verifying_key_to_cores
            .iter()
            .filter(|(_, cached)| matches!(cached, CachedCore::Ready(_)))
            .map(|(vk, _)| *vk)
            .collect()
    }

    /// TODO make this O(1) by storing a dk -> vk map
    fn verifying_key_from_discovery_key(&self, dk: &DiscoveryKey) -> Option<VerifyingKey> {
        self.verifying_keys()
            .into_iter()
            .find(|vk| dk == &discovery_key(vk.as_bytes()))
    }
}

/// Container for managing groups of related [`hypercore::Hypercore`]s. It should match the behavior of the
/// [JavaScript version](https://github.com/holepunchto/corestore?tab=readme-ov-file#api).
#[derive(Debug, Clone)]
pub struct Corestore {
    ///  shared ref to corestore
    corestore: Arc<RwLock<builder::InnerCorstore>>,
    /// The same event bus the inner store sends on, kept outside its lock so subscribing never
    /// waits for, or misses events because of, a store operation in progress.
    events: events::Events,
}

impl Corestore {
    fn read(&self) -> RwLockReadGuard<'_, builder::InnerCorstore> {
        self.corestore.read().expect("corestore lock poisoned")
    }

    fn write(&self) -> RwLockWriteGuard<'_, builder::InnerCorstore> {
        self.corestore.write().expect("corestore lock poisoned")
    }

    /// Get the [`VerifyingKey`] that corresponds to a [`DiscoveryKey`], among the cores that
    /// have finished opening.
    pub fn verifying_key_from_discovery_key(&self, dk: &DiscoveryKey) -> Option<VerifyingKey> {
        self.read().verifying_key_from_discovery_key(dk)
    }

    /// The [`VerifyingKey`]s of every core that has finished opening.
    pub(crate) fn verifying_keys(&self) -> Vec<VerifyingKey> {
        self.read().verifying_keys()
    }

    /// Get a hypercore by name. If the core does not exist, create it.
    pub async fn get_from_name(&self, name: &str) -> Result<Hypercore> {
        let primary_key = self.read().primary_key();
        let key_pair = key_pair_from_name(primary_key, &DEFAULT_NAMESPACE, name)?;
        self.get_core(key_pair).await
    }

    /// Get a core from its [`VerifyingKey`]. With no secret key, it is read-only.
    pub async fn get_from_verifying_key(&self, verifying_key: &VerifyingKey) -> Result<Hypercore> {
        self.get_core(PartialKeypair {
            public: *verifying_key,
            secret: None,
        })
        .await
    }

    /// Get the core for `key_pair`, creating it if needed. Callers asking for a core that is
    /// already being created wait on that same creation, so a key never gets two cores, and
    /// the lock is never held while a core is being created.
    async fn get_core(&self, key_pair: PartialKeypair) -> Result<Hypercore> {
        let verifying_key = key_pair.public;
        let opening = {
            let mut inner = self.write();
            match inner.core_cache.get(&verifying_key) {
                Some(CachedCore::Ready(core)) => return Ok(core),
                Some(CachedCore::Opening(opening)) => opening,
                None => {
                    let storage = inner.storage().clone();
                    let opening = async move {
                        storage
                            .get_core_from_key_pair(key_pair)
                            .await
                            .map_err(Arc::new)
                    }
                    .boxed()
                    .shared();
                    inner
                        .core_cache
                        .insert_opening(verifying_key, opening.clone());
                    opening
                }
            }
        };

        let opened = opening.clone().await;

        // Whoever finishes first settles the entry. Leave it alone if it has already changed.
        let newly_ready = {
            let mut inner = self.write();
            let still_opening = matches!(
                inner.core_cache.get(&verifying_key),
                Some(CachedCore::Opening(current)) if current.ptr_eq(&opening)
            );
            match &opened {
                Ok(core) if still_opening => {
                    inner.core_cache.insert_ready(verifying_key, core.clone());
                    true
                }
                // Forget the failure, so asking again retries.
                Err(_) if still_opening => {
                    inner.core_cache.remove(&verifying_key);
                    false
                }
                _ => false,
            }
        };
        if newly_ready {
            let _ = self.events.send(CorestoreEvents::CoreAdded(verifying_key));
        }
        opened.map_err(Error::Opening)
    }

    /// Create a new [`Corestore`] that stores it data in RAM
    pub async fn new_mem() -> Corestore {
        CorestoreBuilder::default()
            .storage(StorageKind::new_mem())
            .build()
            .await
            .expect("should always work")
    }

    /// Create a new [`Corestore`] that stores its data on disk under `path`, creating the
    /// directory if it does not exist. Reopening the same `path` restores the store's primary
    /// key and every core previously written there, so named cores keep their identity across
    /// restarts.
    ///
    /// Unlike [`Corestore::new_mem`] this can fail, because it touches the filesystem.
    pub async fn new_disk(path: impl AsRef<std::path::Path>) -> Result<Corestore> {
        let path = path.as_ref();
        // Creating a store at a path that does not exist yet is the common case, and the
        // primary-key write below fails with a bare `NotFound` without this.
        std::fs::create_dir_all(path).map_err(hypercore::HypercoreError::from)?;
        CorestoreBuilder::default()
            .storage(StorageKind::new_disk(path))
            .build()
            .await
    }

    /// Subscribe to [`CorestoreEvents`], such as a core being added. Never waits on the store.
    pub(crate) fn subscribe(&self) -> tokio::sync::broadcast::Receiver<CorestoreEvents> {
        self.events.subscribe()
    }

    /// Start replicating every core in this store over `stream`, multiplexed via one
    /// `hypercore_protocol` connection — mirrors JS corestore's `.replicate()`
    /// (`corestore/index.js:481`). Poll (or `.await`) the returned [`CorestoreConnection`]
    /// to drive it. Cores opened in this store *after* this is called are attached as soon as
    /// they are added.
    pub fn replicate(&self, stream: impl CipherTrait + 'static) -> CorestoreConnection {
        CorestoreConnection::new(self.clone(), stream)
    }
}

#[cfg(test)]
mod test {
    use super::{storage::get_storage_root, *};
    use hypercore::{
        hypercore_handshake::{
            state_machine::{hc_specific::generate_keypair, SecStream},
            Cipher,
        },
        GetOptions,
    };
    use std::time::Duration;
    use tokio_util::compat::TokioAsyncReadCompatExt;
    use uint24le_framing::Uint24LELengthPrefixedFraming;

    /// How long a test waits for a block to replicate before failing.
    const REPLICATION_BUDGET: Duration = Duration::from_secs(5);

    /// Read block `index` of `core`, waiting for replication to deliver it. A `get` waits even
    /// before any channel is attached, so this also covers a core that has yet to get one.
    async fn replicated_block(core: &Hypercore, index: u64) -> Result<Vec<u8>> {
        let block = core
            .get_with_options(index, GetOptions::with_timeout(REPLICATION_BUDGET))
            .await?;
        Ok(block.expect("a waiting get resolves with the block, never None"))
    }

    /// A disk-backed store reopened from the same path keeps its primary key, so a named
    /// core resolves to the same public key and its data is still there. This is the whole
    /// point of `new_disk`, and it is what lets a publisher keep its feed id across restarts.
    #[tokio::test]
    async fn new_disk_round_trips_a_named_core() -> Result<()> {
        let tmp = tempfile::tempdir().expect("tempdir");
        // A path that does not exist yet: the usual case for a fresh store.
        let dir = tmp.path().join("store");

        let key = {
            let store = Corestore::new_disk(&dir).await?;
            let core = store.get_from_name("feed").await?;
            core.append(b"hello").await?;
            core.key_pair().public
        };

        let store = Corestore::new_disk(&dir).await?;
        let core = store.get_from_name("feed").await?;
        assert_eq!(core.key_pair().public, key, "named core changed identity");
        assert_eq!(core.get(0).await?.as_deref(), Some(&b"hello"[..]));
        Ok(())
    }

    /// Create a pair of connected in-memory encrypted streams, mirroring the same helper in
    /// `bee`/`hrss`'s tests.
    fn create_connected_streams() -> (impl CipherTrait + 'static, impl CipherTrait + 'static) {
        let (a_b, b_a) = tokio::io::duplex(64 * 1024);
        let a_b = Uint24LELengthPrefixedFraming::new(a_b.compat());
        let b_a = Uint24LELengthPrefixedFraming::new(b_a.compat());
        let keypair = generate_keypair().unwrap();
        let initiator = Cipher::new(
            Some(Box::new(a_b)),
            SecStream::new_initiator_xx(&[]).unwrap().into(),
        );
        let responder = Cipher::new(
            Some(Box::new(b_a)),
            SecStream::new_responder_xx(&keypair, &[]).unwrap().into(),
        );
        (initiator, responder)
    }

    const TEST_PK: PrimaryKey = [
        124, 229, 174, 223, 232, 201, 160, 10, 235, 143, 37, 249, 107, 92, 35, 125, 68, 246, 2,
        197, 41, 248, 234, 65, 9, 222, 77, 144, 50, 243, 222, 65,
    ];

    #[tokio::test]
    async fn disk_core_by_name() -> Result<()> {
        // initialize CS with a fixed primary key
        // check it producets the expected fixed file path
        let storage_dir = tempfile::tempdir().unwrap();
        let mut pk = TEST_PK;
        pk[0] = 0;

        let cs = CorestoreBuilder::default()
            .primary_key(pk)
            .storage(StorageKind::new_disk(storage_dir.path()))
            .build()
            .await?;

        let hc = cs.get_from_name("foo").await?;
        hc.append(b"hello").await?;
        assert_eq!(hc.get(0).await?, Some(b"hello".to_vec()));
        let vk = hc.key_pair().public;

        let core_path = storage_dir.path().join(get_storage_root(&vk));
        assert!(core_path.exists());

        let hc2 = cs.get_from_verifying_key(&vk).await?;
        assert_eq!(hc2.get(0).await?, Some(b"hello".to_vec()));
        Ok(())
    }

    #[tokio::test]
    async fn primary_key_cstore_dir_gets_used() -> Result<()> {
        let storage_dir = tempfile::tempdir().unwrap();
        let mut pk = TEST_PK;
        pk[0] = 1;

        {
            let cs = CorestoreBuilder::default()
                .primary_key(pk)
                .storage(StorageKind::new_disk(storage_dir.path()))
                .build()
                .await?;

            let hc = cs.get_from_name("foo").await?;
            hc.append(b"hello").await?;
        }
        {
            // corestore uses pk in directory if it exists already
            let cs = CorestoreBuilder::default()
                .storage(StorageKind::new_disk(storage_dir.path()))
                .build()
                .await?;
            let hc = cs.get_from_name("foo").await?;
            assert_eq!(hc.get(0).await?, Some(b"hello".to_vec()));
        }
        {
            // providing a pk while there is one on disk is err
            assert!(matches!(
                CorestoreBuilder::default()
                    .storage(StorageKind::new_disk(storage_dir.path()))
                    .primary_key(pk)
                    .build()
                    .await,
                Err(Error::PrimaryKeyConflict(_))
            ));
        }

        Ok(())
    }

    #[tokio::test]
    async fn prexisting_cores_replicate() -> Result<()> {
        let (cs_a, cs_b) = (Corestore::new_mem().await, Corestore::new_mem().await);
        let (a, b) = create_connected_streams();
        let name = "foo";
        let core_a = cs_a.get_from_name(name).await?;
        let vk = core_a.key_pair().public;
        let core_b = cs_b.get_from_verifying_key(&vk).await?;

        core_a.append(b"hello").await?;
        let before_replicating = core_b
            .get_with_options(0, GetOptions::without_waiting())
            .await?;
        assert!(before_replicating.is_none());

        tokio::spawn(cs_a.replicate(a));
        tokio::spawn(cs_b.replicate(b));

        assert_eq!(replicated_block(&core_b, 0).await?, b"hello");
        Ok(())
    }

    #[tokio::test]
    async fn new_cores_replicate() -> Result<()> {
        let (cs_a, cs_b) = (Corestore::new_mem().await, Corestore::new_mem().await);
        let (a, b) = create_connected_streams();

        // No cores exist in either store yet: this proves cores opened *after* replicate()
        // is called still get attached (from their `CoreAdded` events), not just
        // ones that existed up front.
        tokio::spawn(cs_a.replicate(a));
        tokio::spawn(cs_b.replicate(b));

        let name = "foo";
        let core_a = cs_a.get_from_name(name).await?;
        core_a.append(b"hello").await?;

        let vk = core_a.key_pair().public;
        let core_b = cs_b.get_from_verifying_key(&vk).await?;

        core_a.append(b"world").await?;
        assert_eq!(replicated_block(&core_b, 0).await?, b"hello");
        assert_eq!(replicated_block(&core_b, 1).await?, b"world");
        Ok(())
    }

    /// `hrss`'s reader opens its feed cores after the connection is running and quiet, once it
    /// has read their keys from a bootstrap core. The connection used to wake on `CoreAdded`
    /// while the store was still locked, find nothing with its non-blocking lookups, and never
    /// be woken again, so the core never got a channel. That can't happen now, since the event
    /// carries the key and lookups don't fail, but the scenario is what has to keep working.
    #[tokio::test]
    async fn a_core_opened_after_the_connection_goes_quiet_replicates() -> Result<()> {
        let (cs_a, cs_b) = (Corestore::new_mem().await, Corestore::new_mem().await);
        let (a, b) = create_connected_streams();

        // A core both sides have from the start, standing in for `hrss`'s keys core.
        let bootstrap_a = cs_a.get_from_name("bootstrap").await?;
        bootstrap_a.append(b"bootstrap").await?;
        let bootstrap_b = cs_b
            .get_from_verifying_key(&bootstrap_a.key_pair().public)
            .await?;
        let late_a = cs_a.get_from_name("late").await?;
        late_a.append(b"late hello").await?;

        tokio::spawn(cs_a.replicate(a));
        tokio::spawn(cs_b.replicate(b));
        assert_eq!(replicated_block(&bootstrap_b, 0).await?, b"bootstrap");

        let late_b = cs_b
            .get_from_verifying_key(&late_a.key_pair().public)
            .await?;

        // Times out if the late core never gets a channel.
        assert_eq!(replicated_block(&late_b, 0).await?, b"late hello");
        Ok(())
    }

    /// Two callers opening the same new key at once must share one core. Two cores for one
    /// key would mean two writers on the same storage. On disk, because creating an in-memory
    /// core never yields, so two opens of one would never overlap.
    #[tokio::test]
    async fn concurrent_opens_of_one_key_share_one_core() -> Result<()> {
        let tmp = tempfile::tempdir().expect("tempdir");
        let store = Corestore::new_disk(tmp.path()).await?;

        let (first, second) =
            tokio::join!(store.get_from_name("feed"), store.get_from_name("feed"));
        let (first, second) = (first?, second?);
        first.append(b"hello").await?;

        assert_eq!(second.info().length, 1, "both handles are the same core");
        Ok(())
    }

    #[tokio::test]
    async fn concurrent_opens_announce_the_core_once() -> Result<()> {
        let tmp = tempfile::tempdir().expect("tempdir");
        let store = Corestore::new_disk(tmp.path()).await?;
        let mut events = store.subscribe();

        let (first, second) =
            tokio::join!(store.get_from_name("feed"), store.get_from_name("feed"));
        let vk = first?.key_pair().public;
        second?;

        assert!(matches!(events.try_recv(), Ok(CorestoreEvents::CoreAdded(added)) if added == vk));
        assert!(events.try_recv().is_err(), "announced only once");
        Ok(())
    }

    #[tokio::test]
    async fn a_failed_open_can_be_retried() -> Result<()> {
        let tmp = tempfile::tempdir().expect("tempdir");
        let store = Corestore::new_disk(tmp.path()).await?;
        let key_pair = key_pair_from_name(store.read().primary_key(), &DEFAULT_NAMESPACE, "feed")?;
        // A file where the core's storage directory should go.
        let blocker = tmp.path().join(get_storage_root(&key_pair));
        std::fs::create_dir_all(blocker.parent().unwrap()).unwrap();
        std::fs::write(&blocker, b"not a directory").unwrap();

        assert!(matches!(
            store.get_from_name("feed").await,
            Err(Error::Opening(_))
        ));
        assert!(
            store.read().core_cache.get(&key_pair.public).is_none(),
            "a failure is not cached"
        );

        std::fs::remove_file(&blocker).unwrap();
        let core = store.get_from_name("feed").await?;
        assert_eq!(core.key_pair().public, key_pair.public);
        Ok(())
    }

    #[tokio::test]
    async fn multiple_cores_multiplex_over_one_connection() -> Result<()> {
        let (cs_a, cs_b) = (Corestore::new_mem().await, Corestore::new_mem().await);
        let (a, b) = create_connected_streams();

        let foo_a = cs_a.get_from_name("foo").await?;
        let bar_a = cs_a.get_from_name("bar").await?;
        foo_a.append(b"foo hello").await?;
        bar_a.append(b"bar hello").await?;

        let foo_b = cs_b
            .get_from_verifying_key(&foo_a.key_pair().public)
            .await?;
        let bar_b = cs_b
            .get_from_verifying_key(&bar_a.key_pair().public)
            .await?;

        // Only one connection for both cores.
        tokio::spawn(cs_a.replicate(a));
        tokio::spawn(cs_b.replicate(b));

        assert_eq!(replicated_block(&foo_b, 0).await?, b"foo hello");
        assert_eq!(replicated_block(&bar_b, 0).await?, b"bar hello");
        Ok(())
    }
}

/// The README's example, compiled and run as a doctest.
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;