|
| 1 | +// Copyright 2018-2019 Kodebox, Inc. |
| 2 | +// This file is part of CodeChain. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use std::fs; |
| 18 | +use std::str::FromStr; |
| 19 | +use std::sync::Arc; |
| 20 | + |
| 21 | +use ccore::{BlockChainClient, BlockId}; |
| 22 | +use ctypes::BlockHash; |
| 23 | +use primitives::H256; |
| 24 | + |
| 25 | +use jsonrpc_core::Result; |
| 26 | + |
| 27 | +use super::super::errors; |
| 28 | +use super::super::traits::Snapshot; |
| 29 | +use super::super::types::BlockNumberAndHash; |
| 30 | + |
| 31 | +pub struct SnapshotClient<C> |
| 32 | +where |
| 33 | + C: BlockChainClient, { |
| 34 | + client: Arc<C>, |
| 35 | + snapshot_path: Option<String>, |
| 36 | +} |
| 37 | + |
| 38 | +impl<C> SnapshotClient<C> |
| 39 | +where |
| 40 | + C: BlockChainClient, |
| 41 | +{ |
| 42 | + pub fn new(client: Arc<C>, snapshot_path: Option<String>) -> Self { |
| 43 | + SnapshotClient { |
| 44 | + client, |
| 45 | + snapshot_path, |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<C> Snapshot for SnapshotClient<C> |
| 51 | +where |
| 52 | + C: BlockChainClient + 'static, |
| 53 | +{ |
| 54 | + fn get_snapshot_list(&self) -> Result<Vec<BlockNumberAndHash>> { |
| 55 | + if let Some(snapshot_path) = &self.snapshot_path { |
| 56 | + let mut result = Vec::new(); |
| 57 | + for entry in fs::read_dir(snapshot_path).map_err(errors::io)? { |
| 58 | + let entry = entry.map_err(errors::io)?; |
| 59 | + |
| 60 | + // Check if the entry is a directory |
| 61 | + let file_type = entry.file_type().map_err(errors::io)?; |
| 62 | + if !file_type.is_dir() { |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + let path = entry.path(); |
| 67 | + let name = match path.file_name().expect("Directories always have file name").to_str() { |
| 68 | + Some(n) => n, |
| 69 | + None => continue, |
| 70 | + }; |
| 71 | + let hash = match H256::from_str(name) { |
| 72 | + Ok(h) => BlockHash::from(h), |
| 73 | + Err(_) => continue, |
| 74 | + }; |
| 75 | + if let Some(number) = self.client.block_number(&BlockId::Hash(hash)) { |
| 76 | + result.push(BlockNumberAndHash { |
| 77 | + number, |
| 78 | + hash, |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + result.sort_unstable_by(|a, b| b.number.cmp(&a.number)); |
| 83 | + Ok(result) |
| 84 | + } else { |
| 85 | + Ok(Vec::new()) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments