radix_transactions/manifest/
blob_provider.rs

1use radix_common::prelude::{hash, Hash};
2use sbor::prelude::*;
3
4type Blob = Vec<u8>;
5type BlobReference = Hash;
6
7//========
8// Traits
9//========
10
11pub trait IsBlobProvider {
12    fn add_blob(&mut self, blob: Blob);
13
14    fn get_blob(&self, blob_reference: &BlobReference) -> Option<Blob>;
15
16    fn blobs(self) -> IndexMap<BlobReference, Blob>;
17}
18
19//=======================
20// Default Blob Provider
21//=======================
22
23#[derive(Default, Debug, Clone)]
24pub struct BlobProvider(IndexMap<BlobReference, Blob>);
25
26impl BlobProvider {
27    pub fn new() -> Self {
28        Default::default()
29    }
30
31    pub fn new_with_blobs(blobs: Vec<Blob>) -> Self {
32        Self(blobs.into_iter().map(|blob| (hash(&blob), blob)).collect())
33    }
34
35    pub fn new_with_prehashed_blobs(blobs: IndexMap<BlobReference, Blob>) -> Self {
36        Self(blobs)
37    }
38}
39
40impl IsBlobProvider for BlobProvider {
41    fn add_blob(&mut self, blob: Blob) {
42        let hash = hash(&blob);
43        self.0.insert(hash, blob);
44    }
45
46    fn get_blob(&self, blob_reference: &BlobReference) -> Option<Blob> {
47        self.0.get(blob_reference).cloned()
48    }
49
50    fn blobs(self) -> IndexMap<BlobReference, Blob> {
51        self.0
52    }
53}
54
55//====================
56// Mock Blob Provider
57//====================
58
59#[derive(Default, Debug, Clone)]
60pub struct MockBlobProvider;
61
62impl MockBlobProvider {
63    pub fn new() -> Self {
64        Default::default()
65    }
66}
67
68impl IsBlobProvider for MockBlobProvider {
69    fn add_blob(&mut self, _: Blob) {
70        /* No OP */
71    }
72
73    fn get_blob(&self, _: &BlobReference) -> Option<Blob> {
74        // All hashes are valid
75        Some(vec![])
76    }
77
78    fn blobs(self) -> IndexMap<BlobReference, Blob> {
79        Default::default()
80    }
81}