radix_transactions/manifest/
blob_provider.rs1use radix_common::prelude::{hash, Hash};
2use sbor::prelude::*;
3
4type Blob = Vec<u8>;
5type BlobReference = Hash;
6
7pub 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#[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#[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 }
72
73 fn get_blob(&self, _: &BlobReference) -> Option<Blob> {
74 Some(vec![])
76 }
77
78 fn blobs(self) -> IndexMap<BlobReference, Blob> {
79 Default::default()
80 }
81}