@@ -63,6 +63,10 @@ pub struct TokenInfo {
6363#[ derive( Debug ) ]
6464enum State {
6565 SnapshotHeader ( BlockHash , u64 ) ,
66+ SnapshotBody {
67+ block : BlockHash ,
68+ prev_root : H256 ,
69+ } ,
6670 SnapshotChunk {
6771 block : BlockHash ,
6872 restore : SnapshotRestore ,
@@ -90,28 +94,12 @@ impl Extension {
9094 pub fn new (
9195 client : Arc < Client > ,
9296 api : Box < dyn Api > ,
93- snapshot_target : Option < ( H256 , u64 ) > ,
97+ snapshot_target : Option < ( BlockHash , u64 ) > ,
9498 snapshot_dir : Option < String > ,
9599 ) -> Extension {
96100 api. set_timer ( SYNC_TIMER_TOKEN , Duration :: from_millis ( SYNC_TIMER_INTERVAL ) ) . expect ( "Timer set succeeds" ) ;
97101
98- let state = match snapshot_target {
99- Some ( ( hash, num) ) => match client. block_header ( & BlockId :: Number ( num) ) {
100- Some ( ref header) if * header. hash ( ) == hash => {
101- let state_db = client. state_db ( ) . read ( ) ;
102- let state_root = header. state_root ( ) ;
103- match TrieFactory :: readonly ( state_db. as_hashdb ( ) , & state_root) {
104- Ok ( ref trie) if trie. is_complete ( ) => State :: Full ,
105- _ => State :: SnapshotChunk {
106- block : hash. into ( ) ,
107- restore : SnapshotRestore :: new ( state_root) ,
108- } ,
109- }
110- }
111- _ => State :: SnapshotHeader ( hash. into ( ) , num) ,
112- } ,
113- None => State :: Full ,
114- } ;
102+ let state = Extension :: initial_state ( client. clone ( ) , snapshot_target) ;
115103 cdebug ! ( SYNC , "Initial state is {:?}" , state) ;
116104 let mut header = client. best_header ( ) ;
117105 let mut hollow_headers = vec ! [ header. decode( ) ] ;
@@ -149,6 +137,36 @@ impl Extension {
149137 }
150138 }
151139
140+ fn initial_state ( client : Arc < Client > , snapshot_target : Option < ( BlockHash , u64 ) > ) -> State {
141+ let ( hash, num) = match snapshot_target {
142+ Some ( target) => target,
143+ None => return State :: Full ,
144+ } ;
145+ let header = match client. block_header ( & num. into ( ) ) {
146+ Some ( ref h) if h. hash ( ) == hash => h. clone ( ) ,
147+ _ => return State :: SnapshotHeader ( hash, num) ,
148+ } ;
149+ if client. block_body ( & hash. into ( ) ) . is_none ( ) {
150+ let parent_hash = header. parent_hash ( ) ;
151+ let parent =
152+ client. block_header ( & parent_hash. into ( ) ) . expect ( "Parent header of the snapshot header must exist" ) ;
153+ return State :: SnapshotBody {
154+ block : hash,
155+ prev_root : parent. state_root ( ) ,
156+ }
157+ }
158+
159+ let state_db = client. state_db ( ) . read ( ) ;
160+ let state_root = header. state_root ( ) ;
161+ match TrieFactory :: readonly ( state_db. as_hashdb ( ) , & state_root) {
162+ Ok ( ref trie) if trie. is_complete ( ) => State :: Full ,
163+ _ => State :: SnapshotChunk {
164+ block : hash,
165+ restore : SnapshotRestore :: new ( state_root) ,
166+ } ,
167+ }
168+ }
169+
152170 fn dismiss_request ( & mut self , id : & NodeId , request_id : u64 ) {
153171 if let Some ( requests) = self . requests . get_mut ( id) {
154172 requests. retain ( |( i, _) | * i != request_id) ;
@@ -395,6 +413,9 @@ impl NetworkExtension<Event> for Extension {
395413 } ) ;
396414 }
397415 }
416+ State :: SnapshotBody {
417+ ..
418+ } => unimplemented ! ( ) ,
398419 State :: SnapshotChunk {
399420 block,
400421 ref mut restore,
@@ -509,6 +530,9 @@ impl Extension {
509530 None
510531 }
511532 }
533+ State :: SnapshotBody {
534+ ..
535+ } => None ,
512536 State :: SnapshotChunk {
513537 ..
514538 } => None ,
@@ -562,6 +586,9 @@ impl Extension {
562586 None
563587 }
564588 }
589+ State :: SnapshotBody {
590+ ..
591+ } => unimplemented ! ( ) ,
565592 State :: SnapshotChunk {
566593 ..
567594 } => None ,
@@ -855,6 +882,9 @@ impl Extension {
855882 headers. len( )
856883 ) ,
857884 } ,
885+ State :: SnapshotBody {
886+ ..
887+ } => { }
858888 State :: SnapshotChunk {
859889 ..
860890 } => { }
0 commit comments