Skip to content

Commit cbe8322

Browse files
remagpieforiequal0
authored andcommitted
Add RPC snapshot_getList
1 parent ef3ad4f commit cbe8322

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

codechain/rpc_apis.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ impl ApiDependencies {
3737
use crpc::v1::*;
3838
handler.extend_with(ChainClient::new(Arc::clone(&self.client)).to_delegate());
3939
handler.extend_with(MempoolClient::new(Arc::clone(&self.client)).to_delegate());
40+
handler.extend_with(SnapshotClient::new(Arc::clone(&self.client), config.snapshot.path.clone()).to_delegate());
4041
if config.rpc.enable_devel_api {
4142
handler.extend_with(
4243
DevelClient::new(Arc::clone(&self.client), Arc::clone(&self.miner), self.block_sync.clone())

rpc/src/v1/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ pub fn invalid_custom_action(err: String) -> Error {
294294
}
295295
}
296296

297+
pub fn io(error: std::io::Error) -> Error {
298+
Error {
299+
code: ErrorCode::InternalError,
300+
message: format!("{}", error),
301+
data: None,
302+
}
303+
}
304+
297305
/// Internal error signifying a logic error in code.
298306
/// Should not be used when function can just fail
299307
/// because of invalid parameters or incomplete node state.

rpc/src/v1/impls/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod engine;
2121
mod mempool;
2222
mod miner;
2323
mod net;
24+
mod snapshot;
2425

2526
pub use self::account::AccountClient;
2627
pub use self::chain::ChainClient;
@@ -29,3 +30,4 @@ pub use self::engine::EngineClient;
2930
pub use self::mempool::MempoolClient;
3031
pub use self::miner::MinerClient;
3132
pub use self::net::NetClient;
33+
pub use self::snapshot::SnapshotClient;

rpc/src/v1/impls/snapshot.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

rpc/src/v1/traits/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod engine;
2121
mod mempool;
2222
mod miner;
2323
mod net;
24+
mod snapshot;
2425

2526
pub use self::account::Account;
2627
pub use self::chain::Chain;
@@ -29,3 +30,4 @@ pub use self::engine::Engine;
2930
pub use self::mempool::Mempool;
3031
pub use self::miner::Miner;
3132
pub use self::net::Net;
33+
pub use self::snapshot::Snapshot;

rpc/src/v1/traits/snapshot.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 jsonrpc_core::Result;
18+
19+
use super::super::types::BlockNumberAndHash;
20+
21+
#[rpc(server)]
22+
pub trait Snapshot {
23+
/// Gets list of block numbers and block hashes of the snapshots.
24+
#[rpc(name = "snapshot_getList")]
25+
fn get_snapshot_list(&self) -> Result<Vec<BlockNumberAndHash>>;
26+
}

0 commit comments

Comments
 (0)