Skip to content

Commit 9ae6f95

Browse files
Joon-Mo YangJoon-Mo Yang
authored andcommitted
Create snapshot folder periodically
1 parent e0dfe6e commit 9ae6f95

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
**/*.iml
99
.idea/
1010
db/
11+
/snapshot/
1112
/log/
1213
/keystoreData/
1314

codechain/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use clogger::LoggerConfig;
6464
use cnetwork::{NetworkConfig, NetworkService, SocketAddr};
6565
use creactor::EventLoop;
6666
use crpc::Server as RpcServer;
67-
use csync::{BlockSyncExtension, ParcelSyncExtension};
67+
use csync::{BlockSyncExtension, ParcelSyncExtension, SnapshotService};
6868
use ctrlc::CtrlC;
6969
use fdlimit::raise_fd_limit;
7070
use parking_lot::{Condvar, Mutex};
@@ -225,6 +225,11 @@ fn run_node(matches: ArgMatches) -> Result<(), String> {
225225
}
226226
};
227227

228+
// FIXME: Get snapshot root directory from config
229+
// FIXME: Get snapshot period from genesis block
230+
let snapshot_service = SnapshotService::new(client.client(), "snapshot".to_string(), 1 << 14);
231+
client.client().add_notify(snapshot_service.clone());
232+
228233
// drop the spec to free up genesis state.
229234
drop(spec);
230235

sync/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ extern crate triehash;
3232

3333
mod block;
3434
mod parcel;
35+
mod snapshot;
3536

3637
pub use self::block::BlockSyncExtension;
3738
pub use self::parcel::ParcelSyncExtension;
39+
pub use self::snapshot::SnapshotService;
3840

3941
#[cfg(test)]
4042
extern crate codechain_keys as ckeys;

sync/src/snapshot/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 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+
mod service;
18+
19+
pub use self::service::Service as SnapshotService;

sync/src/snapshot/service.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2018 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;
18+
use std::path::PathBuf;
19+
use std::sync::Arc;
20+
use std::thread::spawn;
21+
22+
use ccore::{BlockChainClient, BlockId, ChainNotify};
23+
use ctypes::H256;
24+
25+
pub struct Service {
26+
client: Arc<BlockChainClient>,
27+
/// Snapshot root directory
28+
root_dir: String,
29+
/// Snapshot creation period in unit of block numbers
30+
period: u64,
31+
}
32+
33+
impl Service {
34+
pub fn new(client: Arc<BlockChainClient>, root_dir: String, period: u64) -> Arc<Self> {
35+
Arc::new(Self {
36+
client,
37+
root_dir,
38+
period,
39+
})
40+
}
41+
}
42+
43+
impl ChainNotify for Service {
44+
/// fires when chain has new blocks.
45+
fn new_blocks(
46+
&self,
47+
_imported: Vec<H256>,
48+
_invalid: Vec<H256>,
49+
enacted: Vec<H256>,
50+
_retracted: Vec<H256>,
51+
_sealed: Vec<H256>,
52+
_duration: u64,
53+
) {
54+
let best_number = self.client.chain_info().best_block_number;
55+
let is_checkpoint = enacted
56+
.iter()
57+
.map(|hash| self.client.block_number(BlockId::Hash(*hash)).expect("Enacted block must exist"))
58+
.any(|number| number % self.period == 0);
59+
if is_checkpoint && best_number > self.period {
60+
let root_dir = self.root_dir.clone();
61+
let period = self.period;
62+
spawn(move || {
63+
let target = (best_number / period - 1) * period;
64+
let path: PathBuf = [root_dir, target.to_string()].iter().collect();
65+
if let Ok(_) = create_dir_all(path) {
66+
// FIXME: implement this
67+
// unimplemented!()
68+
}
69+
});
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)