-
Notifications
You must be signed in to change notification settings - Fork 23
/
blockstore.rs
325 lines (272 loc) · 10.9 KB
/
blockstore.rs
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
use crate::{
decode, encode,
utils::{Arc, CondSend, CondSync},
BlockStoreError, MAX_BLOCK_SIZE,
};
use bytes::Bytes;
use futures::Future;
use libipld::{
cbor::DagCborCodec,
cid::Version,
multihash::{Code, MultihashDigest},
Cid,
};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
//--------------------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------------------
/// The value representing the DAG-JSON codec.
///
/// - <https://ipld.io/docs/codecs/#known-codecs>
/// - <https://github.com/multiformats/multicodec/blob/master/table.csv>
pub const CODEC_DAG_JSON: u64 = 0x0129;
/// The value representing the DAG-CBOR codec.
///
/// - <https://ipld.io/docs/codecs/#known-codecs>
/// - <https://github.com/multiformats/multicodec/blob/master/table.csv>
pub const CODEC_DAG_CBOR: u64 = 0x71;
/// The value representing the DAG-Protobuf codec.
///
/// - <https://ipld.io/docs/codecs/#known-codecs>
/// - <https://github.com/multiformats/multicodec/blob/master/table.csv>
pub const CODEC_DAG_PB: u64 = 0x70;
/// The value representing the raw codec.
///
/// - <https://ipld.io/docs/codecs/#known-codecs>
/// - <https://github.com/multiformats/multicodec/blob/master/table.csv>
pub const CODEC_RAW: u64 = 0x55;
//--------------------------------------------------------------------------------------------------
// Traits
//--------------------------------------------------------------------------------------------------
/// For types that implement block store operations like adding, getting content from the store.
pub trait BlockStore: CondSync {
/// Retrieve a block from this store via its hash (`Cid`).
///
/// If this store can't find the block, it may raise an error like `BlockNotFound`.
fn get_block(
&self,
cid: &Cid,
) -> impl Future<Output = Result<Bytes, BlockStoreError>> + CondSend;
/// Put some bytes into the blockstore. These bytes should be encoded with the given codec.
///
/// E.g. `CODEC_RAW` for raw bytes blocks, `CODEC_DAG_CBOR` for dag-cbor, etc.
///
/// This codec will determine the codec encoded in the final `Cid` that's returned.
///
/// If the codec is incorrect, this function won't fail, but any tools that depend on the
/// correctness of the codec may fail. (E.g. tools that follow the links of blocks).
///
/// This funciton allows the blockstore to choose the hashing function itself.
/// The hashing function that was chosen will be readable from the `Cid` metadata.
///
/// If you need control over the concrete hashing function that's used, see `put_block_keyed`.
fn put_block(
&self,
bytes: impl Into<Bytes> + CondSend,
codec: u64,
) -> impl Future<Output = Result<Cid, BlockStoreError>> + CondSend {
let bytes = bytes.into();
async move {
let cid = self.create_cid(&bytes, codec)?;
self.put_block_keyed(cid, bytes).await?;
Ok(cid)
}
}
/// Put a block of data into this blockstore. The block's CID needs to match the CID given.
///
/// It's up to the blockstore whether to check this fact or assume it when this function is called.
///
/// The default implementation of `put_block` will use this function under the hood and use
/// the correct CID provided by the `create_cid` function.
///
/// This is useful to be able to add blocks that were generated from other
/// clients with differently configured hashing functions to this blockstore.
fn put_block_keyed(
&self,
cid: Cid,
bytes: impl Into<Bytes> + CondSend,
) -> impl Future<Output = Result<(), BlockStoreError>> + CondSend;
/// Find out whether a call to `get_block` would return with a result or not.
///
/// This is useful for data exchange protocols to find out what needs to be fetched
/// externally and what doesn't.
fn has_block(
&self,
cid: &Cid,
) -> impl Future<Output = Result<bool, BlockStoreError>> + CondSend;
// This should be the same in all implementations of BlockStore
fn create_cid(&self, bytes: &[u8], codec: u64) -> Result<Cid, BlockStoreError> {
// If there are too many bytes, abandon this task
if bytes.len() > MAX_BLOCK_SIZE {
return Err(BlockStoreError::MaximumBlockSizeExceeded(bytes.len()));
}
// Compute the Blake3 hash of the bytes
let hash = Code::Blake3_256.digest(bytes);
// Represent the hash as a V1 CID
let cid = Cid::new(Version::V1, codec, hash)?;
Ok(cid)
}
}
//--------------------------------------------------------------------------------------------------
// Implementations
//--------------------------------------------------------------------------------------------------
impl<B: BlockStore> BlockStore for &B {
async fn get_block(&self, cid: &Cid) -> Result<Bytes, BlockStoreError> {
(**self).get_block(cid).await
}
async fn put_block(
&self,
bytes: impl Into<Bytes> + CondSend,
codec: u64,
) -> Result<Cid, BlockStoreError> {
(**self).put_block(bytes, codec).await
}
async fn put_block_keyed(
&self,
cid: Cid,
bytes: impl Into<Bytes> + CondSend,
) -> Result<(), BlockStoreError> {
(**self).put_block_keyed(cid, bytes).await
}
async fn has_block(&self, cid: &Cid) -> Result<bool, BlockStoreError> {
(**self).has_block(cid).await
}
fn create_cid(&self, bytes: &[u8], codec: u64) -> Result<Cid, BlockStoreError> {
(**self).create_cid(bytes, codec)
}
}
impl<B: BlockStore> BlockStore for Box<B> {
async fn get_block(&self, cid: &Cid) -> Result<Bytes, BlockStoreError> {
(**self).get_block(cid).await
}
async fn put_block(
&self,
bytes: impl Into<Bytes> + CondSend,
codec: u64,
) -> Result<Cid, BlockStoreError> {
(**self).put_block(bytes, codec).await
}
async fn put_block_keyed(
&self,
cid: Cid,
bytes: impl Into<Bytes> + CondSend,
) -> Result<(), BlockStoreError> {
(**self).put_block_keyed(cid, bytes).await
}
async fn has_block(&self, cid: &Cid) -> Result<bool, BlockStoreError> {
(**self).has_block(cid).await
}
fn create_cid(&self, bytes: &[u8], codec: u64) -> Result<Cid, BlockStoreError> {
(**self).create_cid(bytes, codec)
}
}
/// An in-memory block store to simulate IPFS.
///
/// IPFS is basically a glorified HashMap.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct MemoryBlockStore(
#[serde(serialize_with = "crate::utils::serialize_cid_map")]
#[serde(deserialize_with = "crate::utils::deserialize_cid_map")]
pub(crate) Arc<Mutex<HashMap<Cid, Bytes>>>,
);
impl MemoryBlockStore {
/// Creates a new in-memory block store.
pub fn new() -> Self {
Self::default()
}
}
impl BlockStore for MemoryBlockStore {
async fn get_block(&self, cid: &Cid) -> Result<Bytes, BlockStoreError> {
let bytes = self
.0
.lock()
.get(cid)
.ok_or(BlockStoreError::CIDNotFound(*cid))?
.clone();
Ok(bytes)
}
async fn put_block_keyed(
&self,
cid: Cid,
bytes: impl Into<Bytes> + CondSend,
) -> Result<(), BlockStoreError> {
self.0.lock().insert(cid, bytes.into());
Ok(())
}
async fn has_block(&self, cid: &Cid) -> Result<bool, BlockStoreError> {
Ok(self.0.lock().contains_key(cid))
}
}
//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------
/// Tests the retrieval property of a BlockStore-conforming type.
pub async fn bs_retrieval_test<T>(store: impl BlockStore) -> Result<(), BlockStoreError> {
// Example objects to insert and remove from the blockstore
let first_bytes = vec![1, 2, 3, 4, 5];
let second_bytes = b"hello world".to_vec();
// Insert the objects into the blockstore
let first_cid = store.put_block(first_bytes.clone(), CODEC_RAW).await?;
let second_cid = store.put_block(second_bytes.clone(), CODEC_RAW).await?;
// Retrieve the objects from the blockstore
let first_loaded = store.get_block(&first_cid).await?;
let second_loaded = store.get_block(&second_cid).await?;
// Assert that the objects are the same as the ones we inserted
assert_eq!(first_loaded, first_bytes);
assert_eq!(second_loaded, second_bytes);
Ok(())
}
/// Tests the duplication of a BlockStore-conforming type.
pub async fn bs_duplication_test<T>(store: impl BlockStore) -> Result<(), BlockStoreError> {
// Example objects to insert and remove from the blockstore
let first_bytes = vec![1, 2, 3, 4, 5];
let second_bytes = first_bytes.clone();
// Insert the objects into the blockstore
let first_cid = store.put_block(first_bytes.clone(), CODEC_RAW).await?;
let second_cid = store.put_block(second_bytes.clone(), CODEC_RAW).await?;
// Assert that the two vecs produced the same CID
assert_eq!(first_cid, second_cid);
// Retrieve the objects from the blockstore
let first_loaded = store.get_block(&first_cid).await?;
let second_loaded = store.get_block(&second_cid).await?;
// Assert that the objects are the same as the ones we inserted
assert_eq!(first_loaded, first_bytes);
assert_eq!(second_loaded, second_bytes);
// Assert that the objects we loaded are the same
assert_eq!(first_loaded, second_loaded);
Ok(())
}
/// Tests the serialization of a BlockStore-conforming type.
pub async fn bs_serialization_test<T>(store: &T) -> Result<(), BlockStoreError>
where
T: BlockStore + Serialize + for<'de> Deserialize<'de>,
{
// Example objects to insert and remove from the blockstore
let bytes = vec![1, 2, 3, 4, 5];
// Insert the object into the blockstore
let cid = store.put_block(bytes.clone(), CODEC_RAW).await?;
// Serialize the BlockStore
let serial_store: Vec<u8> = encode(&store, DagCborCodec)?;
// Construct a new BlockStore from the Serialized object
let deserial_store: T = decode(&serial_store, DagCborCodec)?;
// Retrieve the object from the blockstore
let loaded = deserial_store.get_block(&cid).await?;
// Assert that the objects are the same as the ones we inserted
assert_eq!(loaded, bytes);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;
#[async_std::test]
async fn memory_blockstore() -> Result<()> {
let store = &MemoryBlockStore::new();
bs_retrieval_test::<MemoryBlockStore>(store).await?;
bs_duplication_test::<MemoryBlockStore>(store).await?;
bs_serialization_test::<MemoryBlockStore>(store).await?;
Ok(())
}
}