@@ -115,6 +115,48 @@ impl HeaderChain {
115115 }
116116 }
117117
118+ /// Inserts a bootstrap header into backing cache database.
119+ /// Makes the imported header the best header.
120+ /// Expects the header to be valid and already verified.
121+ /// If the header is already known, does nothing.
122+ // FIXME: Find better return type. Returning `None` at duplication is not natural
123+ pub fn insert_bootstrap_header ( & self , batch : & mut DBTransaction , header : & HeaderView ) {
124+ let hash = header. hash ( ) ;
125+
126+ ctrace ! ( HEADERCHAIN , "Inserting bootstrap block header #{}({}) to the headerchain." , header. number( ) , hash) ;
127+
128+ if self . is_known_header ( & hash) {
129+ ctrace ! ( HEADERCHAIN , "Block header #{}({}) is already known." , header. number( ) , hash) ;
130+ return
131+ }
132+
133+ assert ! ( self . pending_best_header_hash. read( ) . is_none( ) ) ;
134+ assert ! ( self . pending_best_proposal_block_hash. read( ) . is_none( ) ) ;
135+
136+ let compressed_header = compress ( header. rlp ( ) . as_raw ( ) , blocks_swapper ( ) ) ;
137+ batch. put ( db:: COL_HEADERS , & hash, & compressed_header) ;
138+
139+ let mut new_hashes = HashMap :: new ( ) ;
140+ new_hashes. insert ( header. number ( ) , hash) ;
141+ let mut new_details = HashMap :: new ( ) ;
142+ new_details. insert ( hash, BlockDetails {
143+ number : header. number ( ) ,
144+ total_score : 0 . into ( ) ,
145+ parent : header. parent_hash ( ) ,
146+ } ) ;
147+
148+ batch. put ( db:: COL_EXTRA , BEST_HEADER_KEY , & hash) ;
149+ * self . pending_best_header_hash . write ( ) = Some ( hash) ;
150+ batch. put ( db:: COL_EXTRA , BEST_PROPOSAL_HEADER_KEY , & hash) ;
151+ * self . pending_best_proposal_block_hash . write ( ) = Some ( hash) ;
152+
153+ let mut pending_hashes = self . pending_hashes . write ( ) ;
154+ let mut pending_details = self . pending_details . write ( ) ;
155+
156+ batch. extend_with_cache ( db:: COL_EXTRA , & mut * pending_details, new_details, CacheUpdatePolicy :: Overwrite ) ;
157+ batch. extend_with_cache ( db:: COL_EXTRA , & mut * pending_hashes, new_hashes, CacheUpdatePolicy :: Overwrite ) ;
158+ }
159+
118160 /// Inserts the header into backing cache database.
119161 /// Expects the header to be valid and already verified.
120162 /// If the header is already known, does nothing.
0 commit comments