Skip to content

Commit fe9c785

Browse files
remagpieforiequal0
authored andcommitted
Serve snapshot responses to peers
1 parent 6d5af4d commit fe9c785

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

codechain/run_node.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ pub fn run_node(matches: &ArgMatches) -> Result<(), String> {
302302
(Some(hash), Some(num)) => Some((hash, num)),
303303
_ => None,
304304
};
305-
service.register_extension(move |api| BlockSyncExtension::new(client, api, snapshot_target))
305+
let snapshot_dir = config.snapshot.path.clone();
306+
service.register_extension(move |api| {
307+
BlockSyncExtension::new(client, api, snapshot_target, snapshot_dir)
308+
})
306309
};
307310
let sync = Arc::new(BlockSyncSender::from(sync_sender.clone()));
308311
client.client().add_notify(Arc::downgrade(&sync) as Weak<dyn ChainNotify>);

sync/src/block/extension.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use std::collections::hash_map::Entry;
1818
use std::collections::{HashMap, HashSet};
19+
use std::fs;
1920
use std::sync::Arc;
2021
use std::time::Duration;
2122

@@ -43,6 +44,7 @@ use token_generator::TokenGenerator;
4344

4445
use super::downloader::{BodyDownloader, HeaderDownloader};
4546
use super::message::{Message, RequestMessage, ResponseMessage};
47+
use crate::snapshot::snapshot_path;
4648

4749
const SYNC_TIMER_TOKEN: TimerToken = 0;
4850
const SYNC_EXPIRE_TOKEN_BEGIN: TimerToken = SYNC_TIMER_TOKEN + 1;
@@ -81,10 +83,16 @@ pub struct Extension {
8183
api: Box<dyn Api>,
8284
last_request: u64,
8385
nonce: u64,
86+
snapshot_dir: Option<String>,
8487
}
8588

8689
impl Extension {
87-
pub fn new(client: Arc<Client>, api: Box<dyn Api>, snapshot_target: Option<(H256, u64)>) -> Extension {
90+
pub fn new(
91+
client: Arc<Client>,
92+
api: Box<dyn Api>,
93+
snapshot_target: Option<(H256, u64)>,
94+
snapshot_dir: Option<String>,
95+
) -> Extension {
8896
api.set_timer(SYNC_TIMER_TOKEN, Duration::from_millis(SYNC_TIMER_INTERVAL)).expect("Timer set succeeds");
8997

9098
let state = match snapshot_target {
@@ -137,6 +145,7 @@ impl Extension {
137145
api,
138146
last_request: Default::default(),
139147
nonce: Default::default(),
148+
snapshot_dir,
140149
}
141150
}
142151

@@ -689,8 +698,18 @@ impl Extension {
689698
ResponseMessage::Bodies(bodies)
690699
}
691700

692-
fn create_state_chunk_response(&self, _hash: BlockHash, _tree_root: Vec<H256>) -> ResponseMessage {
693-
unimplemented!()
701+
fn create_state_chunk_response(&self, hash: BlockHash, chunk_roots: Vec<H256>) -> ResponseMessage {
702+
let mut result = Vec::new();
703+
for root in chunk_roots {
704+
if let Some(dir) = &self.snapshot_dir {
705+
let chunk_path = snapshot_path(&dir, &hash, &root);
706+
match fs::read(chunk_path) {
707+
Ok(chunk) => result.push(chunk),
708+
_ => result.push(Vec::new()),
709+
}
710+
}
711+
}
712+
ResponseMessage::StateChunk(result)
694713
}
695714

696715
fn on_peer_response(&mut self, from: &NodeId, id: u64, mut response: ResponseMessage) {

sync/src/snapshot/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ pub struct Service {
3232
canceller: Option<ReceiverCanceller>,
3333
}
3434

35+
pub fn snapshot_dir(root_dir: &str, block: &BlockHash) -> PathBuf {
36+
let mut path = PathBuf::new();
37+
path.push(root_dir);
38+
path.push(format!("{:x}", block.deref()));
39+
path
40+
}
41+
42+
pub fn snapshot_path(root_dir: &str, block: &BlockHash, chunk_root: &H256) -> PathBuf {
43+
let mut path = snapshot_dir(root_dir, block);
44+
path.push(format!("{:x}", chunk_root));
45+
path
46+
}
47+
3548
impl Service {
3649
pub fn new(client: Arc<Client>, notify_receiver_source: NotifyReceiverSource, root_dir: String) -> Self {
3750
let NotifyReceiverSource(canceller, receiver) = notify_receiver_source;
@@ -70,17 +83,11 @@ impl Service {
7083
}
7184

7285
fn snapshot(db: &dyn HashDB, block_hash: BlockHash, chunk_root: H256, root_dir: &str) -> Result<(), SnapshotError> {
73-
let snapshot_dir = {
74-
let mut res = PathBuf::new();
75-
res.push(root_dir);
76-
res.push(format!("{:x}", block_hash.deref()));
77-
res
78-
};
79-
create_dir_all(&snapshot_dir)?;
86+
let snapshot_dir = snapshot_dir(root_dir, &block_hash);
87+
create_dir_all(snapshot_dir)?;
8088

8189
for chunk in Snapshot::from_hashdb(db, chunk_root) {
82-
let mut chunk_path = snapshot_dir.clone();
83-
chunk_path.push(format!("{:x}", chunk.root));
90+
let chunk_path = snapshot_path(root_dir, &block_hash, &chunk.root);
8491
let chunk_file = File::create(chunk_path)?;
8592
let compressor = ChunkCompressor::new(chunk_file);
8693
compressor.compress_chunk(&chunk)?;

0 commit comments

Comments
 (0)