|
16 | 16 |
|
17 | 17 | use std::collections::hash_map::Entry; |
18 | 18 | use std::collections::{HashMap, HashSet}; |
| 19 | +use std::fs; |
19 | 20 | use std::sync::Arc; |
20 | 21 | use std::time::Duration; |
21 | 22 |
|
@@ -43,6 +44,7 @@ use token_generator::TokenGenerator; |
43 | 44 |
|
44 | 45 | use super::downloader::{BodyDownloader, HeaderDownloader}; |
45 | 46 | use super::message::{Message, RequestMessage, ResponseMessage}; |
| 47 | +use crate::snapshot::snapshot_path; |
46 | 48 |
|
47 | 49 | const SYNC_TIMER_TOKEN: TimerToken = 0; |
48 | 50 | const SYNC_EXPIRE_TOKEN_BEGIN: TimerToken = SYNC_TIMER_TOKEN + 1; |
@@ -81,10 +83,16 @@ pub struct Extension { |
81 | 83 | api: Box<dyn Api>, |
82 | 84 | last_request: u64, |
83 | 85 | nonce: u64, |
| 86 | + snapshot_dir: Option<String>, |
84 | 87 | } |
85 | 88 |
|
86 | 89 | 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 { |
88 | 96 | api.set_timer(SYNC_TIMER_TOKEN, Duration::from_millis(SYNC_TIMER_INTERVAL)).expect("Timer set succeeds"); |
89 | 97 |
|
90 | 98 | let state = match snapshot_target { |
@@ -137,6 +145,7 @@ impl Extension { |
137 | 145 | api, |
138 | 146 | last_request: Default::default(), |
139 | 147 | nonce: Default::default(), |
| 148 | + snapshot_dir, |
140 | 149 | } |
141 | 150 | } |
142 | 151 |
|
@@ -689,8 +698,18 @@ impl Extension { |
689 | 698 | ResponseMessage::Bodies(bodies) |
690 | 699 | } |
691 | 700 |
|
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) |
694 | 713 | } |
695 | 714 |
|
696 | 715 | fn on_peer_response(&mut self, from: &NodeId, id: u64, mut response: ResponseMessage) { |
|
0 commit comments