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
//! Volatile memory backed repo
use crate::error::Error;
use crate::repo::{BlockPut, BlockStore, Column, DataStore, PinKind, PinMode, PinStore};
use crate::Block;
use async_trait::async_trait;
use cid::Cid;
use std::convert::TryFrom;
use std::path::PathBuf;
use tokio::sync::{Mutex, OwnedMutexGuard};

use super::{BlockRm, BlockRmError, RepoCid};
use std::collections::hash_map::Entry;

// FIXME: Transition to Persistent Map to make iterating more consistent
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

#[derive(Debug, Default)]
pub struct MemBlockStore {
    blocks: Mutex<HashMap<RepoCid, Block>>,
}

#[async_trait]
impl BlockStore for MemBlockStore {
    fn new(_path: PathBuf) -> Self {
        Default::default()
    }

    async fn init(&self) -> Result<(), Error> {
        Ok(())
    }

    async fn open(&self) -> Result<(), Error> {
        Ok(())
    }

    async fn contains(&self, cid: &Cid) -> Result<bool, Error> {
        let contains = self
            .blocks
            .lock()
            .await
            .contains_key(&RepoCid(cid.to_owned()));
        Ok(contains)
    }

    async fn get(&self, cid: &Cid) -> Result<Option<Block>, Error> {
        let block = self
            .blocks
            .lock()
            .await
            .get(&RepoCid(cid.to_owned()))
            .map(|block| block.to_owned());
        Ok(block)
    }

    async fn put(&self, block: Block) -> Result<(Cid, BlockPut), Error> {
        use std::collections::hash_map::Entry;
        let mut g = self.blocks.lock().await;
        match g.entry(RepoCid(block.cid.clone())) {
            Entry::Occupied(_) => {
                trace!("already existing block");
                Ok((block.cid, BlockPut::Existed))
            }
            Entry::Vacant(ve) => {
                trace!("new block");
                let cid = ve.key().0.clone();
                ve.insert(block);
                Ok((cid, BlockPut::NewBlock))
            }
        }
    }

    async fn remove(&self, cid: &Cid) -> Result<Result<BlockRm, BlockRmError>, Error> {
        match self.blocks.lock().await.remove(&RepoCid(cid.to_owned())) {
            Some(_block) => Ok(Ok(BlockRm::Removed(cid.clone()))),
            None => Ok(Err(BlockRmError::NotFound(cid.clone()))),
        }
    }

    async fn list(&self) -> Result<Vec<Cid>, Error> {
        let guard = self.blocks.lock().await;
        Ok(guard.iter().map(|(cid, _block)| cid.0.clone()).collect())
    }

    async fn wipe(&self) {
        self.blocks.lock().await.clear();
    }
}

#[derive(Debug, Default)]
pub struct MemDataStore {
    ipns: Mutex<HashMap<Vec<u8>, Vec<u8>>>,
    // this could also be PinDocument however doing any serialization allows to see the required
    // error types easier
    pin: Arc<Mutex<HashMap<Vec<u8>, Vec<u8>>>>,
}

impl MemDataStore {
    /// Returns true if the pin document was changed, false otherwise.
    fn insert_pin<'a>(
        g: &mut OwnedMutexGuard<HashMap<Vec<u8>, Vec<u8>>>,
        target: &'a Cid,
        kind: &'a PinKind<&'_ Cid>,
    ) -> Result<bool, Error> {
        // rationale for storing as Cid: the same multihash can be pinned with different codecs.
        // even if there aren't many polyglot documents known, pair of raw and the actual codec is
        // always a possibility.
        let key = target.to_bytes();

        match g.entry(key) {
            Entry::Occupied(mut oe) => {
                let mut doc: PinDocument = serde_json::from_slice(oe.get())?;
                if doc.update(true, kind)? {
                    let vec = oe.get_mut();
                    vec.clear();
                    serde_json::to_writer(vec, &doc)?;
                    trace!(doc = ?doc, kind = ?kind, "updated on insert");
                    Ok(true)
                } else {
                    trace!(doc = ?doc, kind = ?kind, "update not needed on insert");
                    Ok(false)
                }
            }
            Entry::Vacant(ve) => {
                let mut doc = PinDocument {
                    version: 0,
                    direct: false,
                    recursive: Recursive::Not,
                    cid_version: match target.version() {
                        cid::Version::V0 => 0,
                        cid::Version::V1 => 1,
                    },
                    indirect_by: Vec::new(),
                };

                doc.update(true, &kind).unwrap();
                let vec = serde_json::to_vec(&doc)?;
                ve.insert(vec);
                trace!(doc = ?doc, kind = ?kind, "created on insert");
                Ok(true)
            }
        }
    }

    /// Returns true if the pin document was changed, false otherwise.
    fn remove_pin<'a>(
        g: &mut OwnedMutexGuard<HashMap<Vec<u8>, Vec<u8>>>,
        target: &'a Cid,
        kind: &'a PinKind<&'_ Cid>,
    ) -> Result<bool, Error> {
        // see cid vs. multihash from [`insert_direct_pin`]
        let key = target.to_bytes();

        match g.entry(key) {
            Entry::Occupied(mut oe) => {
                let mut doc: PinDocument = serde_json::from_slice(oe.get())?;
                if !doc.update(false, kind)? {
                    trace!(doc = ?doc, kind = ?kind, "update not needed on removal");
                    return Ok(false);
                }

                if doc.can_remove() {
                    oe.remove();
                } else {
                    let vec = oe.get_mut();
                    vec.clear();
                    serde_json::to_writer(vec, &doc)?;
                }

                Ok(true)
            }
            Entry::Vacant(_) => Err(anyhow::anyhow!("not pinned")),
        }
    }
}

#[async_trait]
impl PinStore for MemDataStore {
    async fn is_pinned(&self, block: &Cid) -> Result<bool, Error> {
        let key = block.to_bytes();

        let g = self.pin.lock().await;

        // the use of PinKind::RecursiveIntention necessitates the only return fast for
        // only the known pins; we should somehow now query to see if there are any
        // RecursiveIntention's. If there are any, we must walk the refs of each to see if the
        // `block` is amongst of those recursive references which are not yet written to disk.
        //
        // doing this without holding a repo lock is not possible, so leaving this as partial
        // implementation right now.
        Ok(g.contains_key(&key))
    }

    async fn insert_direct_pin(&self, target: &Cid) -> Result<(), Error> {
        let mut g = Mutex::lock_owned(Arc::clone(&self.pin)).await;
        Self::insert_pin(&mut g, target, &PinKind::Direct)?;
        Ok(())
    }

    async fn remove_direct_pin(&self, target: &Cid) -> Result<(), Error> {
        let mut g = Mutex::lock_owned(Arc::clone(&self.pin)).await;
        Self::remove_pin(&mut g, target, &PinKind::Direct)?;
        Ok(())
    }

    async fn insert_recursive_pin(
        &self,
        target: &Cid,
        mut refs: super::References<'_>,
    ) -> Result<(), Error> {
        use futures::stream::TryStreamExt;

        let mut g = Mutex::lock_owned(Arc::clone(&self.pin)).await;

        // this must fail if it is already fully pinned
        Self::insert_pin(&mut g, target, &PinKind::RecursiveIntention)?;

        let target_v1 = if target.version() == cid::Version::V1 {
            target.to_owned()
        } else {
            // this is one more allocation
            Cid::new_v1(target.codec(), target.hash().to_owned())
        };

        // collect these before even if they are many ... not sure if this is a good idea but, the
        // inmem version doesn't need to be all that great. this could be for nothing, if the root
        // was already pinned.

        let mut count = 0;
        let kind = PinKind::IndirectFrom(&target_v1);
        while let Some(next) = refs.try_next().await? {
            // no rollback, nothing
            Self::insert_pin(&mut g, &next, &kind)?;
            count += 1;
        }

        let kind = PinKind::Recursive(count as u64);
        Self::insert_pin(&mut g, target, &kind)?;

        Ok(())
    }

    async fn remove_recursive_pin(
        &self,
        target: &Cid,
        mut refs: super::References<'_>,
    ) -> Result<(), Error> {
        use futures::stream::TryStreamExt;

        let mut g = Mutex::lock_owned(Arc::clone(&self.pin)).await;

        let doc: PinDocument = match g.get(&target.to_bytes()) {
            Some(raw) => match serde_json::from_slice(raw) {
                Ok(doc) => doc,
                Err(e) => return Err(e.into()),
            },
            // well we know it's not pinned at all but this is the general error message
            None => return Err(anyhow::anyhow!("not pinned or pinned indirectly")),
        };

        let kind = match doc.pick_kind() {
            Some(Ok(kind @ PinKind::Recursive(_)))
            | Some(Ok(kind @ PinKind::RecursiveIntention)) => kind,
            Some(Ok(PinKind::Direct)) => {
                Self::remove_pin(&mut g, target, &PinKind::Direct)?;
                return Ok(());
            }
            Some(Ok(PinKind::IndirectFrom(cid))) => {
                return Err(anyhow::anyhow!("pinned indirectly through {}", cid))
            }
            // same here as above with the same message
            _ => return Err(anyhow::anyhow!("not pinned or pinned indirectly")),
        };

        // this must fail if it is already fully pinned
        Self::remove_pin(&mut g, target, &kind.as_ref())?;

        let target_v1 = if target.version() == cid::Version::V1 {
            target.to_owned()
        } else {
            // this is one more allocation
            Cid::new_v1(target.codec(), target.hash().to_owned())
        };

        let kind = PinKind::IndirectFrom(&target_v1);
        while let Some(next) = refs.try_next().await? {
            // no rollback, nothing
            Self::remove_pin(&mut g, &next, &kind)?;
        }

        Ok(())
    }

    async fn list(
        &self,
        mode: Option<PinMode>,
    ) -> futures::stream::BoxStream<'static, Result<(Cid, PinMode), Error>> {
        use futures::stream::StreamExt;
        use std::convert::TryFrom;
        let g = self.pin.lock().await;

        let copy = g
            .iter()
            .map(|(key, value)| {
                let cid = Cid::try_from(key.as_slice())?;
                let doc: PinDocument = serde_json::from_slice(value)?;
                let mode = doc.mode().ok_or_else(|| anyhow::anyhow!("invalid mode"))?;

                Ok((cid, mode))
            })
            .filter(move |res| {
                // could return just two different boxed streams
                if let Some(f) = &mode {
                    match res {
                        Ok((_, mode)) => mode == f,
                        Err(_) => true,
                    }
                } else {
                    true
                }
            })
            .collect::<Vec<_>>();

        futures::stream::iter(copy).boxed()
    }

    async fn query(
        &self,
        cids: Vec<Cid>,
        requirement: Option<PinMode>,
    ) -> Result<Vec<(Cid, PinKind<Cid>)>, Error> {
        let g = self.pin.lock().await;

        cids.into_iter()
            .map(move |cid| {
                match g.get(&cid.to_bytes()) {
                    Some(raw) => {
                        let doc: PinDocument = match serde_json::from_slice(raw) {
                            Ok(doc) => doc,
                            Err(e) => return Err(e.into()),
                        };
                        // None from document is bad result, since the document shouldn't exist in the
                        // first place
                        let mode = match doc.pick_kind() {
                            Some(Ok(kind)) => kind,
                            Some(Err(invalid_cid)) => return Err(Error::new(invalid_cid)),
                            None => {
                                trace!(doc = ?doc, "could not pick pin kind");
                                return Err(anyhow::anyhow!("{} is not pinned", cid));
                            }
                        };

                        // would be more clear if this business was in a separate map; quite awful
                        // as it is now

                        let matches = requirement.as_ref().map(|req| mode == *req).unwrap_or(true);

                        if matches {
                            trace!(cid = %cid, req = ?requirement, "pin matches");
                            return Ok((cid, mode));
                        } else {
                            // FIXME: this error is about the same as http api expects
                            return Err(anyhow::anyhow!(
                                "{} is not pinned as {:?}",
                                cid,
                                requirement
                                    .as_ref()
                                    .expect("matches is never false if requirement is none")
                            ));
                        }
                    }
                    None => {
                        trace!(cid = %cid, "no record found");
                    }
                }

                // FIXME: this error is expected on http interface
                Err(anyhow::anyhow!("{} is not pinned", cid))
            })
            .collect::<Result<Vec<_>, _>>()
    }
}

#[async_trait]
impl DataStore for MemDataStore {
    fn new(_path: PathBuf) -> Self {
        Default::default()
    }

    async fn init(&self) -> Result<(), Error> {
        Ok(())
    }

    async fn open(&self) -> Result<(), Error> {
        Ok(())
    }

    async fn contains(&self, col: Column, key: &[u8]) -> Result<bool, Error> {
        let map = match col {
            Column::Ipns => &self.ipns,
        };
        let contains = map.lock().await.contains_key(key);
        Ok(contains)
    }

    async fn get(&self, col: Column, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
        let map = match col {
            Column::Ipns => &self.ipns,
        };
        let value = map.lock().await.get(key).map(|value| value.to_owned());
        Ok(value)
    }

    async fn put(&self, col: Column, key: &[u8], value: &[u8]) -> Result<(), Error> {
        let map = match col {
            Column::Ipns => &self.ipns,
        };
        map.lock().await.insert(key.to_owned(), value.to_owned());
        Ok(())
    }

    async fn remove(&self, col: Column, key: &[u8]) -> Result<(), Error> {
        let map = match col {
            Column::Ipns => &self.ipns,
        };
        map.lock().await.remove(key);
        Ok(())
    }

    async fn wipe(&self) {
        self.ipns.lock().await.clear();
        self.pin.lock().await.clear();
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
enum Recursive {
    /// Persistent record of **completed** recursive pinning. All references now have indirect pins
    /// recorded.
    Count(u64),
    /// Persistent record of intent to add recursive pins to all indirect blocks or even not to
    /// keep the go-ipfs way which might not be a bad idea after all. Adding all the indirect pins
    /// on disk will cause massive write amplification in the end, but lets keep that way until we
    /// get everything working at least.
    Intent,
    /// Not pinned recursively.
    Not,
}

impl Recursive {
    fn is_set(&self) -> bool {
        match self {
            Recursive::Count(_) | Recursive::Intent => true,
            Recursive::Not => false,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
struct PinDocument {
    version: u8,
    direct: bool,
    // how many descendants; something to check when walking
    recursive: Recursive,
    // no further metadata necessary; cids are pinned by full cid
    cid_version: u8,
    // using the cidv1 versions of all cids here, not sure if that makes sense or is important
    indirect_by: Vec<String>,
}

impl PinDocument {
    fn update(&mut self, add: bool, kind: &PinKind<&'_ Cid>) -> Result<bool, PinUpdateError> {
        // these update rules are a bit complex and there are cases we don't need to handle.
        // Updating on upon `PinKind` forces the caller to inspect what the current state is for
        // example to handle the case of failing "unpin currently recursively pinned as direct".
        // the ruleset seems quite strange to be honest.
        match kind {
            PinKind::IndirectFrom(root) => {
                let root = if root.version() == cid::Version::V1 {
                    root.to_string()
                } else {
                    // this is one more allocation
                    Cid::new_v1(root.codec(), (*root).hash().to_owned()).to_string()
                };

                let modified = if self.indirect_by.is_empty() {
                    if add {
                        self.indirect_by.push(root);
                        true
                    } else {
                        false
                    }
                } else {
                    let mut set = self
                        .indirect_by
                        .drain(..)
                        .collect::<std::collections::BTreeSet<_>>();

                    let modified = if add {
                        set.insert(root)
                    } else {
                        set.remove(&root)
                    };

                    self.indirect_by.extend(set.into_iter());
                    modified
                };

                Ok(modified)
            }
            PinKind::Direct => {
                if self.recursive.is_set() && !self.direct && add {
                    // go-ipfs: cannot make recursive pin also direct
                    // not really sure why does this rule exist; the other way around is allowed
                    return Err(PinUpdateError::AlreadyPinnedRecursive);
                }

                if !add && !self.direct {
                    if !self.recursive.is_set() {
                        return Err(PinUpdateError::CannotUnpinUnpinned);
                    } else {
                        return Err(PinUpdateError::CannotUnpinDirectOnRecursivelyPinned);
                    }
                }

                let modified = self.direct != add;
                self.direct = add;
                Ok(modified)
            }
            PinKind::RecursiveIntention => {
                let modified = if add {
                    match self.recursive {
                        Recursive::Count(_) => return Err(PinUpdateError::AlreadyPinnedRecursive),
                        // can overwrite Intent with another Intent, as Ipfs::insert_pin is now moving to fix
                        // the Intent into the "final form" of Recursive::Count.
                        Recursive::Intent => false,
                        Recursive::Not => {
                            self.recursive = Recursive::Intent;
                            self.direct = false;
                            true
                        }
                    }
                } else {
                    match self.recursive {
                        Recursive::Count(_) | Recursive::Intent => {
                            self.recursive = Recursive::Not;
                            true
                        }
                        Recursive::Not => false,
                    }
                };

                Ok(modified)
            }
            PinKind::Recursive(descendants) => {
                let descendants = *descendants;
                let modified = if add {
                    match self.recursive {
                        Recursive::Count(other) if other != descendants => {
                            return Err(PinUpdateError::UnexpectedNumberOfDescendants(
                                other,
                                descendants,
                            ))
                        }
                        Recursive::Count(_) => false,
                        Recursive::Intent | Recursive::Not => {
                            self.recursive = Recursive::Count(descendants);
                            // the previously direct has now been upgraded to recursive, it can
                            // still be indirect though
                            self.direct = false;
                            true
                        }
                    }
                } else {
                    match self.recursive {
                        Recursive::Count(other) if other != descendants => {
                            return Err(PinUpdateError::UnexpectedNumberOfDescendants(
                                other,
                                descendants,
                            ))
                        }
                        Recursive::Count(_) | Recursive::Intent => {
                            self.recursive = Recursive::Not;
                            true
                        }
                        Recursive::Not => return Err(PinUpdateError::NotPinnedRecursive),
                    }
                    // FIXME: removing ... not sure if this is an issue; was thinking that maybe
                    // the update might need to be split to allow different api for removal than
                    // addition.
                };
                Ok(modified)
            }
        }
    }

    fn can_remove(&self) -> bool {
        !self.direct && !self.recursive.is_set() && self.indirect_by.is_empty()
    }

    fn mode(&self) -> Option<PinMode> {
        if self.recursive.is_set() {
            Some(PinMode::Recursive)
        } else if !self.indirect_by.is_empty() {
            Some(PinMode::Indirect)
        } else if self.direct {
            Some(PinMode::Direct)
        } else {
            None
        }
    }

    fn pick_kind(&self) -> Option<Result<PinKind<Cid>, cid::Error>> {
        self.mode().map(|p| {
            Ok(match p {
                PinMode::Recursive => match self.recursive {
                    Recursive::Intent => PinKind::RecursiveIntention,
                    Recursive::Count(total) => PinKind::Recursive(total),
                    _ => unreachable!("mode shuold not have returned PinKind::Recursive"),
                },
                PinMode::Indirect => {
                    // go-ipfs does seem to be doing a fifo looking, perhaps this is a list there, or
                    // the indirect pins aren't being written down anywhere and they just refs from
                    // recursive roots.
                    let cid = Cid::try_from(self.indirect_by[0].as_str())?;
                    PinKind::IndirectFrom(cid)
                }
                PinMode::Direct => PinKind::Direct,
            })
        })
    }
}

#[derive(Debug, thiserror::Error)]
pub enum PinUpdateError {
    #[error("unexpected number of descendants ({}), found {}", .1, .0)]
    UnexpectedNumberOfDescendants(u64, u64),
    #[error("not pinned recursively")]
    NotPinnedRecursive,
    /// Not allowed: Adding direct pin while pinned recursive
    #[error("already pinned recursively")]
    AlreadyPinnedRecursive,
    #[error("not pinned or pinned indirectly")]
    CannotUnpinUnpinned,
    // go-ipfs prepends the ipfspath here
    #[error("is pinned recursively")]
    CannotUnpinDirectOnRecursivelyPinned,
}

#[cfg(test)]
crate::pinstore_interface_tests!(common_tests, crate::repo::mem::MemDataStore::new);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Block;
    use cid::{Cid, Codec};
    use multihash::Sha2_256;
    use std::env::temp_dir;

    #[tokio::test(max_threads = 1)]
    async fn test_mem_blockstore() {
        let tmp = temp_dir();
        let store = MemBlockStore::new(tmp);
        let data = b"1".to_vec().into_boxed_slice();
        let cid = Cid::new_v1(Codec::Raw, Sha2_256::digest(&data));
        let block = Block::new(data, cid.clone());

        store.init().await.unwrap();
        store.open().await.unwrap();

        let contains = store.contains(&cid);
        assert_eq!(contains.await.unwrap(), false);
        let get = store.get(&cid);
        assert_eq!(get.await.unwrap(), None);
        if store.remove(&cid).await.unwrap().is_ok() {
            panic!("block should not be found")
        }

        let put = store.put(block.clone());
        assert_eq!(put.await.unwrap().0, cid.to_owned());
        let contains = store.contains(&cid);
        assert_eq!(contains.await.unwrap(), true);
        let get = store.get(&cid);
        assert_eq!(get.await.unwrap(), Some(block.clone()));

        store.remove(&cid).await.unwrap().unwrap();
        let contains = store.contains(&cid);
        assert_eq!(contains.await.unwrap(), false);
        let get = store.get(&cid);
        assert_eq!(get.await.unwrap(), None);
    }

    #[tokio::test(max_threads = 1)]
    async fn test_mem_blockstore_list() {
        let tmp = temp_dir();
        let mem_store = MemBlockStore::new(tmp);

        mem_store.init().await.unwrap();
        mem_store.open().await.unwrap();

        for data in &[b"1", b"2", b"3"] {
            let data_slice = data.to_vec().into_boxed_slice();
            let cid = Cid::new_v1(Codec::Raw, Sha2_256::digest(&data_slice));
            let block = Block::new(data_slice, cid);
            mem_store.put(block.clone()).await.unwrap();
            assert!(mem_store.contains(block.cid()).await.unwrap());
        }

        let cids = mem_store.list().await.unwrap();
        assert_eq!(cids.len(), 3);
        for cid in cids.iter() {
            assert!(mem_store.contains(cid).await.unwrap());
        }
    }

    #[tokio::test(max_threads = 1)]
    async fn test_mem_datastore() {
        let tmp = temp_dir();
        let store = MemDataStore::new(tmp);
        let col = Column::Ipns;
        let key = [1, 2, 3, 4];
        let value = [5, 6, 7, 8];

        store.init().await.unwrap();
        store.open().await.unwrap();

        let contains = store.contains(col, &key);
        assert_eq!(contains.await.unwrap(), false);
        let get = store.get(col, &key);
        assert_eq!(get.await.unwrap(), None);
        store.remove(col, &key).await.unwrap();

        let put = store.put(col, &key, &value);
        put.await.unwrap();
        let contains = store.contains(col, &key);
        assert_eq!(contains.await.unwrap(), true);
        let get = store.get(col, &key);
        assert_eq!(get.await.unwrap(), Some(value.to_vec()));

        store.remove(col, &key).await.unwrap();
        let contains = store.contains(col, &key);
        assert_eq!(contains.await.unwrap(), false);
        let get = store.get(col, &key);
        assert_eq!(get.await.unwrap(), None);
    }

    #[test]
    fn pindocument_on_direct_pin() {
        let mut doc = PinDocument {
            version: 0,
            direct: false,
            recursive: Recursive::Not,
            cid_version: 0,
            indirect_by: Vec::new(),
        };

        assert!(doc.update(true, &PinKind::Direct).unwrap());

        assert_eq!(doc.mode(), Some(PinMode::Direct));
        assert_eq!(doc.pick_kind().unwrap().unwrap(), PinKind::Direct);
    }
}