|
| 1 | +// Copyright 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::{create_dir_all, File}; |
| 18 | +use std::path::PathBuf; |
| 19 | +use std::sync::Arc; |
| 20 | +use std::thread::{spawn, JoinHandle}; |
| 21 | + |
| 22 | +use ccore::snapshot_notify::{NotifyReceiverSource, ReceiverCanceller}; |
| 23 | +use ccore::{BlockChainTrait, BlockId, Client}; |
| 24 | +use cmerkle::snapshot::{ChunkCompressor, Error as SnapshotError, Snapshot}; |
| 25 | +use ctypes::BlockHash; |
| 26 | +use hashdb::{AsHashDB, HashDB}; |
| 27 | +use primitives::H256; |
| 28 | +use std::ops::Deref; |
| 29 | + |
| 30 | +pub struct Service { |
| 31 | + join_handle: Option<JoinHandle<()>>, |
| 32 | + canceller: Option<ReceiverCanceller>, |
| 33 | +} |
| 34 | + |
| 35 | +impl Service { |
| 36 | + pub fn new(client: Arc<Client>, notify_receiver_source: NotifyReceiverSource, root_dir: String) -> Self { |
| 37 | + let NotifyReceiverSource(canceller, receiver) = notify_receiver_source; |
| 38 | + let join_handle = spawn(move || { |
| 39 | + cinfo!(SYNC, "Snapshot service is on"); |
| 40 | + while let Ok(block_hash) = receiver.recv() { |
| 41 | + cinfo!(SYNC, "Snapshot is requested for block: {}", block_hash); |
| 42 | + let state_root = if let Some(header) = client.block_header(&BlockId::Hash(block_hash)) { |
| 43 | + header.state_root() |
| 44 | + } else { |
| 45 | + cerror!(SYNC, "There isn't corresponding header for the requested block hash: {}", block_hash,); |
| 46 | + continue |
| 47 | + }; |
| 48 | + let db_lock = client.state_db().read(); |
| 49 | + if let Some(err) = snapshot(db_lock.as_hashdb(), block_hash, state_root, &root_dir).err() { |
| 50 | + cerror!( |
| 51 | + SYNC, |
| 52 | + "Snapshot request failed for block: {}, chunk_root: {}, err: {}", |
| 53 | + block_hash, |
| 54 | + state_root, |
| 55 | + err |
| 56 | + ); |
| 57 | + } else { |
| 58 | + cinfo!(SYNC, "Snapshot is ready for block: {}", block_hash) |
| 59 | + } |
| 60 | + } |
| 61 | + cinfo!(SYNC, "Snapshot service is stopped") |
| 62 | + }); |
| 63 | + |
| 64 | + Self { |
| 65 | + canceller: Some(canceller), |
| 66 | + join_handle: Some(join_handle), |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +fn snapshot(db: &dyn HashDB, block_hash: BlockHash, chunk_root: H256, root_dir: &str) -> Result<(), SnapshotError> { |
| 72 | + let snapshot_dir = { |
| 73 | + let mut res = PathBuf::new(); |
| 74 | + res.push(root_dir); |
| 75 | + res.push(format!("{:x}", block_hash.deref())); |
| 76 | + res |
| 77 | + }; |
| 78 | + create_dir_all(&snapshot_dir)?; |
| 79 | + |
| 80 | + for chunk in Snapshot::from_hashdb(db, chunk_root) { |
| 81 | + let mut chunk_path = snapshot_dir.clone(); |
| 82 | + chunk_path.push(format!("{:x}", chunk.root)); |
| 83 | + let chunk_file = File::create(chunk_path)?; |
| 84 | + let compressor = ChunkCompressor::new(chunk_file); |
| 85 | + compressor.compress_chunk(&chunk)?; |
| 86 | + } |
| 87 | + |
| 88 | + Ok(()) |
| 89 | +} |
| 90 | + |
| 91 | +impl Drop for Service { |
| 92 | + fn drop(&mut self) { |
| 93 | + if let Some(canceller) = self.canceller.take() { |
| 94 | + // The thread corresponding to the `self.join_handle` waits for the `self.canceller` is dropped. |
| 95 | + // It must be dropped first not to make deadlock at `handle.join()`. |
| 96 | + drop(canceller); |
| 97 | + } |
| 98 | + |
| 99 | + if let Some(handle) = self.join_handle.take() { |
| 100 | + handle.join().expect("Snapshot service thread shouldn't panic"); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments