-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmod.rs
More file actions
438 lines (392 loc) · 13.2 KB
/
mod.rs
File metadata and controls
438 lines (392 loc) · 13.2 KB
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
//! Save data to a desired storage backend.
mod node;
mod persist;
pub use self::node::Node;
pub use self::persist::Persist;
pub use merkle_tree_stream::Node as NodeTrait;
use anyhow::{anyhow, ensure, Result};
use ed25519_dalek::{PublicKey, SecretKey, Signature, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH};
use flat_tree as flat;
use futures::future::FutureExt;
use random_access_disk::RandomAccessDisk;
use random_access_memory::RandomAccessMemory;
use random_access_storage::RandomAccess;
use sleep_parser::*;
use std::borrow::Borrow;
use std::fmt::Debug;
use std::ops::Range;
use std::path::PathBuf;
const HEADER_OFFSET: u64 = 32;
#[derive(Debug)]
pub struct PartialKeypair {
pub public: PublicKey,
pub secret: Option<SecretKey>,
}
/// The types of stores that can be created.
#[derive(Debug)]
pub enum Store {
/// Tree
Tree,
/// Data
Data,
/// Bitfield
Bitfield,
/// Signatures
Signatures,
/// Keypair
Keypair,
}
/// Save data to a desired storage backend.
#[derive(Debug)]
pub struct Storage<T>
where
T: RandomAccess + Debug,
{
tree: T,
data: T,
bitfield: T,
signatures: T,
keypair: T,
}
impl<T> Storage<T>
where
T: RandomAccess<Error = Box<dyn std::error::Error + Send + Sync>> + Debug + Send,
{
/// Create a new instance. Takes a keypair and a callback to create new
/// storage instances.
// Named `.open()` in the JS version. Replaces the `.openKey()` method too by
// requiring a key pair to be initialized before creating a new instance.
pub async fn new<Cb>(create: Cb, overwrite: bool) -> Result<Self>
where
Cb: Fn(Store) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<T>> + Send>>,
{
let tree = create(Store::Tree).await?;
let data = create(Store::Data).await?;
let bitfield = create(Store::Bitfield).await?;
let signatures = create(Store::Signatures).await?;
let keypair = create(Store::Keypair).await?;
let mut instance = Self {
tree,
data,
bitfield,
signatures,
keypair,
};
if overwrite || instance.bitfield.len().await.unwrap_or(0) == 0 {
let header = create_bitfield();
instance
.bitfield
.write(0, &header.to_vec())
.await
.map_err(|e| anyhow!(e))?;
}
if overwrite || instance.signatures.len().await.unwrap_or(0) == 0 {
let header = create_signatures();
instance
.signatures
.write(0, &header.to_vec())
.await
.map_err(|e| anyhow!(e))?;
}
if overwrite || instance.tree.len().await.unwrap_or(0) == 0 {
let header = create_tree();
instance
.tree
.write(0, &header.to_vec())
.await
.map_err(|e| anyhow!(e))?;
}
Ok(instance)
}
/// Write data to the feed.
#[inline]
pub async fn write_data(&mut self, offset: u64, data: &[u8]) -> Result<()> {
self.data.write(offset, &data).await.map_err(|e| anyhow!(e))
}
/// Write a byte vector to a data storage (random-access instance) at the
/// position of `index`.
///
/// NOTE: Meant to be called from the `.put()` feed method. Probably used to
/// insert data as-is after receiving it from the network (need to confirm
/// with mafintosh).
/// TODO: Ensure the signature size is correct.
/// NOTE: Should we create a `Data` entry type?
pub async fn put_data(&mut self, index: u64, data: &[u8], nodes: &[Node]) -> Result<()> {
if data.is_empty() {
return Ok(());
}
let range = self.data_offset(index, nodes).await?;
ensure!(
(range.end - range.start) as usize == data.len(),
format!("length `{:?} != {:?}`", range.count(), data.len())
);
self.data
.write(range.start, data)
.await
.map_err(|e| anyhow!(e))
}
/// Get data from disk that the user has written to it. This is stored
/// unencrypted, so there's no decryption needed.
// FIXME: data_offset always reads out index 0, length 0
#[inline]
pub async fn get_data(&mut self, index: u64) -> Result<Vec<u8>> {
let cached_nodes = Vec::new(); // TODO: reuse allocation.
let range = self.data_offset(index, &cached_nodes).await?;
self.data
.read(range.start, range.count() as u64)
.await
.map_err(|e| anyhow!(e))
}
/// Search the signature stores for a `Signature`, starting at `index`.
pub fn next_signature(
&mut self,
index: u64,
) -> futures::future::BoxFuture<'_, Result<Signature>> {
let bytes = async_std::task::block_on(async {
self.signatures
.read(HEADER_OFFSET + 64 * index, 64)
.await
.map_err(|e| anyhow!(e))
});
async move {
let bytes = bytes?;
if not_zeroes(&bytes) {
Ok(Signature::from_bytes(&bytes)?)
} else {
Ok(self.next_signature(index + 1).await?)
}
}
.boxed()
}
/// Get a `Signature` from the store.
#[inline]
pub async fn get_signature(&mut self, index: u64) -> Result<Signature> {
let bytes = self
.signatures
.read(HEADER_OFFSET + 64 * index, 64)
.await
.map_err(|e| anyhow!(e))?;
ensure!(not_zeroes(&bytes), "No signature found");
Ok(Signature::from_bytes(&bytes)?)
}
/// Write a `Signature` to `self.Signatures`.
/// TODO: Ensure the signature size is correct.
/// NOTE: Should we create a `Signature` entry type?
#[inline]
pub async fn put_signature(
&mut self,
index: u64,
signature: impl Borrow<Signature>,
) -> Result<()> {
let signature = signature.borrow();
self.signatures
.write(HEADER_OFFSET + 64 * index, &signature.to_bytes())
.await
.map_err(|e| anyhow!(e))
}
/// TODO(yw) docs
/// Get the offset for the data, return `(offset, size)`.
///
/// ## Panics
/// A panic can occur if no maximum value is found.
pub async fn data_offset(&mut self, index: u64, cached_nodes: &[Node]) -> Result<Range<u64>> {
let mut roots = Vec::new(); // TODO: reuse alloc
flat::full_roots(tree_index(index), &mut roots);
let mut offset = 0;
let mut pending = roots.len() as u64;
let block_index = tree_index(index);
if pending == 0 {
let len = match find_node(&cached_nodes, block_index) {
Some(node) => node.len(),
None => (self.get_node(block_index).await?).len(),
};
return Ok(offset..offset + len);
}
for root in roots {
// FIXME: we're always having a cache miss here. Check cache first before
// getting a node from the backend.
//
// ```rust
// let node = match find_node(cached_nodes, root) {
// Some(node) => node,
// None => self.get_node(root),
// };
// ```
let node = self.get_node(root).await?;
offset += node.len();
pending -= 1;
if pending > 0 {
continue;
}
let len = match find_node(&cached_nodes, block_index) {
Some(node) => node.len(),
None => (self.get_node(block_index).await?).len(),
};
return Ok(offset..offset + len);
}
unreachable!();
}
/// Get a `Node` from the `tree` storage.
#[inline]
pub async fn get_node(&mut self, index: u64) -> Result<Node> {
let buf = self
.tree
.read(HEADER_OFFSET + 40 * index, 40)
.await
.map_err(|e| anyhow!(e))?;
let node = Node::from_bytes(index, &buf)?;
Ok(node)
}
/// Write a `Node` to the `tree` storage.
/// TODO: prevent extra allocs here. Implement a method on node that can reuse
/// a buffer.
#[inline]
pub async fn put_node(&mut self, node: &Node) -> Result<()> {
let index = node.index();
let buf = node.to_bytes()?;
self.tree
.write(HEADER_OFFSET + 40 * index, &buf)
.await
.map_err(|e| anyhow!(e))
}
/// Write data to the internal bitfield module.
/// TODO: Ensure the chunk size is correct.
/// NOTE: Should we create a bitfield entry type?
#[inline]
pub async fn put_bitfield(&mut self, offset: u64, data: &[u8]) -> Result<()> {
self.bitfield
.write(HEADER_OFFSET + offset, data)
.await
.map_err(|e| anyhow!(e))
}
/// Read bitfield header.
pub async fn read_bitfield(&mut self) -> Result<Vec<u8>> {
let buf = self
.bitfield
.read(0, 32)
.await
.map_err(|_| anyhow::anyhow!("read bitfield header"))?;
let header = Header::from_vec(&buf).map_err(|e| anyhow::anyhow!(e))?;
// khodzha:
// TODO: we should handle eof vs errors here somehow but idk how to do that
let mut buf: Vec<u8> = Vec::new();
let mut idx: u64 = 0;
let ent_size: u64 = header.entry_size as u64;
loop {
let result = self
.bitfield
.read(HEADER_OFFSET + idx * ent_size, ent_size)
.await;
if let Ok(slice) = result {
buf.extend_from_slice(&slice);
idx += 1;
} else {
return Ok(buf);
}
}
}
/// Read a public key from storage
pub async fn read_public_key(&mut self) -> Result<PublicKey> {
let buf = self
.keypair
.read(0, PUBLIC_KEY_LENGTH as u64)
.await
.map_err(|e| anyhow!(e))?;
let public_key = PublicKey::from_bytes(&buf)?;
Ok(public_key)
}
/// Read a secret key from storage
pub async fn read_secret_key(&mut self) -> Result<SecretKey> {
let buf = self
.keypair
.read(PUBLIC_KEY_LENGTH as u64, SECRET_KEY_LENGTH as u64)
.await
.map_err(|e| anyhow!(e))?;
let secret_key = SecretKey::from_bytes(&buf)?;
Ok(secret_key)
}
/// Write a public key to the storage
pub async fn write_public_key(&mut self, public_key: &PublicKey) -> Result<()> {
let buf: [u8; PUBLIC_KEY_LENGTH] = public_key.to_bytes();
self.keypair.write(0, &buf).await.map_err(|e| anyhow!(e))
}
/// Write a secret key to the storage
pub async fn write_secret_key(&mut self, secret_key: &SecretKey) -> Result<()> {
let buf: [u8; SECRET_KEY_LENGTH] = secret_key.to_bytes();
self.keypair
.write(PUBLIC_KEY_LENGTH as u64, &buf)
.await
.map_err(|e| anyhow!(e))
}
/// Tries to read a partial keypair (ie: with an optional secret_key) from the storage
pub async fn read_partial_keypair(&mut self) -> Option<PartialKeypair> {
match self.read_public_key().await {
Ok(public) => match self.read_secret_key().await {
Ok(secret) => Some(PartialKeypair {
public,
secret: Some(secret),
}),
Err(_) => Some(PartialKeypair {
public,
secret: None,
}),
},
Err(_) => None,
}
}
}
impl Storage<RandomAccessMemory> {
/// Create a new instance backed by a `RandomAccessMemory` instance.
pub async fn new_memory() -> Result<Self> {
let create = |_| async { Ok(RandomAccessMemory::default()) }.boxed();
Ok(Self::new(create, true).await?)
}
}
impl Storage<RandomAccessDisk> {
/// Create a new instance backed by a `RandomAccessDisk` instance.
pub async fn new_disk(dir: &PathBuf, overwrite: bool) -> Result<Self> {
let storage = |storage: Store| {
let name = match storage {
Store::Tree => "tree",
Store::Data => "data",
Store::Bitfield => "bitfield",
Store::Signatures => "signatures",
Store::Keypair => "key",
};
RandomAccessDisk::open(dir.as_path().join(name)).boxed()
};
Ok(Self::new(storage, overwrite).await?)
}
}
/// Get a node from a vector of nodes.
#[inline]
fn find_node(nodes: &[Node], index: u64) -> Option<&Node> {
for node in nodes {
if node.index() == index {
return Some(node);
}
}
None
}
/// Check if a byte slice is not completely zero-filled.
#[inline]
fn not_zeroes(bytes: &[u8]) -> bool {
for byte in bytes {
if *byte != 0 {
return true;
}
}
false
}
/// Convert the index to the index in the tree.
#[inline]
fn tree_index(index: u64) -> u64 {
2 * index
}
#[test]
fn should_detect_zeroes() {
let nums = vec![0; 10];
assert!(!not_zeroes(&nums));
let nums = vec![1; 10];
assert!(not_zeroes(&nums));
}