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
use crate::{
    blockcfg::{Block, HeaderHash},
    intercom::{self, ReplySendError, ReplyStreamHandle},
};
use chain_core::{
    packer::Codec,
    property::{Deserialize, ReadError, Serialize, WriteError},
};
use chain_storage::{BlockInfo, BlockStore, Error as StorageError};
use futures::prelude::*;
use std::{convert::identity, path::Path};
use thiserror::Error;
use tracing::Span;

const MINIMUM_BLOCKS_TO_FLUSH: usize = 256;

#[derive(Debug, Error)]
pub enum Error {
    #[error("block not found")]
    BlockNotFound,
    #[error("database backend error")]
    BackendError(#[source] StorageError),
    #[error("deserialization error")]
    Deserialize(#[source] ReadError),
    #[error("serialization error")]
    Serialize(#[source] WriteError),
    #[error("Block already present in DB")]
    BlockAlreadyPresent,
    #[error("the parent block is missing for the required write")]
    MissingParent,
    #[error("cannot iterate between the 2 given blocks")]
    CannotIterate,
}

impl From<StorageError> for Error {
    fn from(source: StorageError) -> Self {
        match source {
            StorageError::BlockNotFound => Error::BlockNotFound,
            StorageError::BlockAlreadyPresent => Error::BlockAlreadyPresent,
            StorageError::MissingParent => Error::MissingParent,
            e => Error::BackendError(e),
        }
    }
}

#[derive(Clone)]
pub struct Storage {
    storage: BlockStore,
    span: Span,
}

pub struct Ancestor {
    pub header_hash: HeaderHash,
    pub distance: u32,
}

#[derive(Debug, thiserror::Error)]
enum StreamingError {
    #[error("error accessing storage")]
    Storage(#[from] Error),
    #[error("failed to send block")]
    Sending(#[from] ReplySendError),
}

impl Storage {
    pub fn file<P: AsRef<Path>>(path: P, span: Span) -> Result<Self, Error> {
        let storage = BlockStore::file(path, HeaderHash::zero_hash().as_bytes().to_vec())?;
        Ok(Storage { storage, span })
    }

    pub fn memory(span: Span) -> Result<Self, Error> {
        let storage = BlockStore::memory(HeaderHash::zero_hash().as_bytes().to_vec())?;
        Ok(Storage { storage, span })
    }

    pub fn get_tag(&self, tag: &str) -> Result<Option<HeaderHash>, Error> {
        self.storage
            .get_tag(tag)
            .map_err(Into::into)
            .and_then(|maybe_block_id| {
                maybe_block_id
                    .map(|block_id| {
                        HeaderHash::deserialize(&mut Codec::new(block_id.as_ref()))
                            .map_err(Error::Deserialize)
                    })
                    .transpose()
            })
    }

    pub fn put_tag(&self, tag: &str, header_hash: HeaderHash) -> Result<(), Error> {
        self.storage
            .put_tag(tag, header_hash.as_bytes())
            .map_err(Into::into)
    }

    pub fn get(&self, header_hash: HeaderHash) -> Result<Option<Block>, Error> {
        match self.storage.get_block(header_hash.as_bytes()) {
            Ok(block) => Block::deserialize(&mut Codec::new(block.as_ref()))
                .map(Some)
                .map_err(Error::Deserialize),
            Err(StorageError::BlockNotFound) => Ok(None),
            Err(e) => Err(Error::BackendError(e)),
        }
    }

    pub fn block_exists(&self, header_hash: HeaderHash) -> Result<bool, Error> {
        self.storage
            .block_exists(header_hash.as_ref())
            .map_err(Into::into)
    }

    pub fn get_branches(&self) -> Result<Vec<HeaderHash>, Error> {
        self.storage
            .get_tips_ids()?
            .into_iter()
            .map(|branch| {
                HeaderHash::deserialize(&mut Codec::new(branch.as_ref()))
                    .map_err(Error::Deserialize)
            })
            .collect::<Result<Vec<_>, Error>>()
    }

    pub fn get_blocks_by_chain_length(&self, chain_length: u32) -> Result<Vec<Block>, Error> {
        self.storage
            .get_blocks_by_chain_length(chain_length)
            .map_err(Into::into)
            .and_then(|blocks| {
                blocks
                    .into_iter()
                    .map(|block| Block::deserialize(&mut Codec::new(block.as_ref())))
                    .collect::<Result<Vec<_>, _>>()
                    .map_err(Error::Deserialize)
            })
    }

    pub fn get_nth_ancestor(
        &self,
        header_hash: HeaderHash,
        distance: u32,
    ) -> Result<Option<Block>, Error> {
        match self
            .storage
            .get_nth_ancestor(header_hash.as_bytes(), distance)
        {
            Ok(block) => {
                let block = self
                    .storage
                    .get_block(block.id().as_ref())
                    .expect("already found this block, it must exists inside the storage");
                Block::deserialize(&mut Codec::new(block.as_ref()))
                    .map(Some)
                    .map_err(Error::Deserialize)
            }
            Err(StorageError::BlockNotFound) => Ok(None),
            Err(e) => Err(Error::BackendError(e)),
        }
    }

    pub fn put_block(&self, block: &Block) -> Result<(), Error> {
        let id = block
            .header()
            .hash()
            .serialize_as_vec()
            .map_err(Error::Serialize)?;
        let parent_id = block
            .header()
            .block_parent_hash()
            .serialize_as_vec()
            .map_err(Error::Serialize)?;
        let chain_length = block.header().chain_length().into();
        let block_info = BlockInfo::new(id, parent_id, chain_length);
        self.storage
            .put_block(
                &block.serialize_as_vec().map_err(Error::Serialize)?[..],
                block_info,
            )
            .map_err(Into::into)
    }

    pub fn get_parent(&self, header_hash: HeaderHash) -> Result<Option<HeaderHash>, Error> {
        let block_info = match self.storage.get_block_info(header_hash.as_ref()) {
            Ok(block_info) => block_info,
            Err(_) => return Ok(None),
        };

        HeaderHash::deserialize(&mut Codec::new(block_info.parent_id().as_ref()))
            .map_err(Error::Deserialize)
            .map(Some)
    }

    pub fn is_ancestor(&self, a: HeaderHash, b: HeaderHash) -> bool {
        self.storage
            .is_ancestor(a.as_ref(), b.as_ref())
            .map(|x| x.is_some())
            .unwrap_or(false)
    }

    pub fn get_chain_length(&self, block_id: HeaderHash) -> Option<u32> {
        let block_info = match self.storage.get_block_info(block_id.as_ref()) {
            Ok(block_info) => block_info,
            Err(_) => return None,
        };

        Some(block_info.chain_length())
    }

    /// Return values:
    /// - `Ok(stream)` - `from` is ancestor of `to`, returns blocks between them
    /// - `Err(CannotIterate)` - `from` is not ancestor of `to`
    /// - `Err(BlockNotFound)` - `from` or `to` was not found
    /// - `Err(_)` - some other storage error
    pub fn stream_from_to(
        &self,
        from: HeaderHash,
        to: HeaderHash,
    ) -> Result<impl Stream<Item = Result<Block, intercom::Error>>, Error> {
        let distance = self
            .storage
            .is_ancestor(from.as_bytes(), to.as_bytes())?
            .ok_or(Error::CannotIterate)?;

        let stream = futures::stream::iter(self.storage.iter(to.as_bytes(), distance)?)
            .map_err(Into::into)
            .and_then(|raw_block| async move {
                Block::deserialize(&mut Codec::new(raw_block.as_ref())).map_err(Error::Deserialize)
            })
            .map_err(Into::into);

        Ok(stream)
    }

    /// Stream a branch ending at `to` and starting from the ancestor
    /// at `depth` or at the first ancestor since genesis block
    /// if `depth` is given as `None`.
    ///
    /// This function uses buffering in the in-memory channel to reduce
    /// synchronization overhead.
    pub async fn send_branch(
        &self,
        to: HeaderHash,
        depth: Option<u32>,
        handle: ReplyStreamHandle<Block>,
    ) -> Result<(), ReplySendError> {
        self.send_branch_with(to, depth, handle, identity).await
    }

    /// Like `send_branch`, but with a transformation function applied
    /// to the block content before sending to the in-memory channel.
    pub async fn send_branch_with<T, F>(
        &self,
        to: HeaderHash,
        depth: Option<u32>,
        handle: ReplyStreamHandle<T>,
        transform: F,
    ) -> Result<(), ReplySendError>
    where
        F: FnMut(Block) -> T,
        F: Send + 'static,
        T: Send + 'static,
    {
        let iter_result = self.storage.iter(to.as_bytes(), depth.unwrap_or(1));

        let iter = match iter_result {
            Ok(iter) => iter,
            Err(err) => {
                let err: Error = err.into();
                handle.reply_error(err.into());
                return Ok(());
            }
        };

        let mut stream = futures::stream::iter(iter)
            .map(|raw_block_result| {
                raw_block_result.map_err(Into::into).and_then(|raw_block| {
                    Block::deserialize(&mut Codec::new(raw_block.as_ref()))
                        .map_err(Error::Deserialize)
                })
            })
            .map_ok(transform)
            .map_err(Into::into)
            .map(Ok);

        handle.start_sending().send_all(&mut stream).await
    }

    pub fn find_closest_ancestor(
        &self,
        checkpoints: Vec<HeaderHash>,
        descendant: HeaderHash,
    ) -> Result<Option<Ancestor>, Error> {
        let mut ancestor = None;
        let mut closest_found = std::u32::MAX;

        for checkpoint in checkpoints {
            // Checkpoints sent by a peer may not
            // be present locally, so we need to ignore certain errors
            match self
                .storage
                .is_ancestor(checkpoint.as_bytes(), descendant.as_bytes())
            {
                Ok(None) => {}
                Ok(Some(distance)) => {
                    if closest_found > distance {
                        ancestor = Some(checkpoint);
                        closest_found = distance;
                    }
                }
                Err(e) => {
                    // Checkpoints sent by a peer may not
                    // be present locally, so we need to ignore certain errors
                    match e {
                        StorageError::BlockNotFound => {
                            // FIXME: add block hash into the error so we
                            // can see which of the two it is.
                            // For now, just ignore either.
                        }
                        e => return Err(e.into()),
                    }
                }
            }
        }

        Ok(ancestor.map(|header_hash| Ancestor {
            header_hash,
            distance: closest_found,
        }))
    }

    pub fn find_common_ancestor(
        &self,
        tip_1: HeaderHash,
        tip_2: HeaderHash,
    ) -> Result<HeaderHash, Error> {
        HeaderHash::deserialize(&mut Codec::new(
            self.storage
                .find_lowest_common_ancestor(tip_1.as_ref(), tip_2.as_ref())?
                // No common ancestor means that we accepted blocks originating from two different block0
                .unwrap()
                .id()
                .as_ref(),
        ))
        .map_err(Error::Deserialize)
    }

    pub fn gc(&self, threshold_depth: u32, main_branch_tip: &[u8]) -> Result<(), Error> {
        let _enter = self.span.enter();
        let main_info = self.storage.get_block_info(main_branch_tip)?;
        let threshold_length = match main_info.chain_length().checked_sub(threshold_depth) {
            Some(result) => result,
            None => return Ok(()),
        };

        tracing::debug!(
            "pruning all branches below stability depth {} (chain length: {})",
            threshold_depth,
            threshold_length
        );

        let tips_ids = self.storage.get_tips_ids()?;

        for id in tips_ids {
            let info = self.storage.get_block_info(id.as_ref())?;

            if info.chain_length() > threshold_length {
                continue;
            }

            self.storage.prune_branch(id.as_ref())?;

            tracing::debug!(
                "removed branch with head {}",
                HeaderHash::hash_bytes(id.as_ref())
            );
        }

        let to_block_info = self
            .storage
            .get_nth_ancestor(main_branch_tip, threshold_depth)?;
        let blocks_flushed = self
            .storage
            .flush_to_permanent_store(to_block_info.id().as_ref(), MINIMUM_BLOCKS_TO_FLUSH)?;

        tracing::debug!(
            "flushed all blocks ({}) up to {} to the permanent store",
            blocks_flushed,
            HeaderHash::hash_bytes(to_block_info.id().as_ref())
        );

        Ok(())
    }
}