@@ -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) ;
@@ -418,6 +436,9 @@ impl NetworkExtension<Event> for Extension {
418436 } ) ;
419437 }
420438 }
439+ State :: SnapshotBody {
440+ ..
441+ } => unimplemented ! ( ) ,
421442 State :: SnapshotChunk {
422443 block,
423444 ref mut restore,
@@ -532,6 +553,9 @@ impl Extension {
532553 None
533554 }
534555 }
556+ State :: SnapshotBody {
557+ ..
558+ } => None ,
535559 State :: SnapshotChunk {
536560 ..
537561 } => None ,
@@ -582,6 +606,9 @@ impl Extension {
582606 None
583607 }
584608 }
609+ State :: SnapshotBody {
610+ ..
611+ } => unimplemented ! ( ) ,
585612 State :: SnapshotChunk {
586613 ..
587614 } => None ,
@@ -859,6 +886,9 @@ impl Extension {
859886 headers. len( )
860887 ) ,
861888 } ,
889+ State :: SnapshotBody {
890+ ..
891+ } => { }
862892 State :: SnapshotChunk {
863893 ..
864894 } => { }
0 commit comments