1616
1717use std:: sync:: Arc ;
1818
19- use ctypes:: BlockNumber ;
19+ use ctypes:: { BlockHash , BlockNumber } ;
2020use kvdb:: { DBTransaction , KeyValueDB } ;
2121use parking_lot:: RwLock ;
2222use primitives:: H256 ;
@@ -44,16 +44,16 @@ const BEST_PROPOSAL_BLOCK_KEY: &[u8] = b"best-proposal-block";
4444/// **Does not do input data verification.**
4545pub struct BlockChain {
4646 /// The hash of the best block of the canonical chain.
47- best_block_hash : RwLock < H256 > ,
47+ best_block_hash : RwLock < BlockHash > ,
4848 /// The hash of the block which has the best score among the proposal blocks
49- best_proposal_block_hash : RwLock < H256 > ,
49+ best_proposal_block_hash : RwLock < BlockHash > ,
5050
5151 headerchain : HeaderChain ,
5252 body_db : BodyDB ,
5353 invoice_db : InvoiceDB ,
5454
55- pending_best_block_hash : RwLock < Option < H256 > > ,
56- pending_best_proposal_block_hash : RwLock < Option < H256 > > ,
55+ pending_best_block_hash : RwLock < Option < BlockHash > > ,
56+ pending_best_proposal_block_hash : RwLock < Option < BlockHash > > ,
5757}
5858
5959impl BlockChain {
@@ -63,7 +63,7 @@ impl BlockChain {
6363
6464 // load best block
6565 let best_block_hash = match db. get ( db:: COL_EXTRA , BEST_BLOCK_KEY ) . unwrap ( ) {
66- Some ( hash) => H256 :: from_slice ( & hash) ,
66+ Some ( hash) => H256 :: from_slice ( & hash) . into ( ) ,
6767 None => {
6868 let hash = genesis_block. hash ( ) ;
6969
@@ -75,7 +75,7 @@ impl BlockChain {
7575 } ;
7676
7777 let best_proposal_block_hash = match db. get ( db:: COL_EXTRA , BEST_PROPOSAL_BLOCK_KEY ) . unwrap ( ) {
78- Some ( hash) => H256 :: from_slice ( & hash) ,
78+ Some ( hash) => H256 :: from_slice ( & hash) . into ( ) ,
7979 None => {
8080 let hash = genesis_block. hash ( ) ;
8181 let mut batch = DBTransaction :: new ( ) ;
@@ -235,7 +235,7 @@ impl BlockChain {
235235 /// The new best block should be a child of the current best block.
236236 /// This will not change the best proposal block chain. This means it is possible
237237 /// to have the best block and the best proposal block in different branches.
238- pub fn update_best_as_committed ( & self , batch : & mut DBTransaction , block_hash : H256 ) -> ImportRoute {
238+ pub fn update_best_as_committed ( & self , batch : & mut DBTransaction , block_hash : BlockHash ) -> ImportRoute {
239239 // FIXME: If it is possible, double check with the consensus engine.
240240 ctrace ! ( BLOCKCHAIN , "Update the best block to {}" , block_hash) ;
241241
@@ -311,12 +311,12 @@ impl BlockChain {
311311 }
312312
313313 /// Get best block hash.
314- pub fn best_block_hash ( & self ) -> H256 {
314+ pub fn best_block_hash ( & self ) -> BlockHash {
315315 * self . best_block_hash . read ( )
316316 }
317317
318318 /// Get best_proposal block hash.
319- pub fn best_proposal_block_hash ( & self ) -> H256 {
319+ pub fn best_proposal_block_hash ( & self ) -> BlockHash {
320320 * self . best_proposal_block_hash . read ( )
321321 }
322322
@@ -349,12 +349,12 @@ impl BlockChain {
349349pub trait BlockProvider : HeaderProvider + BodyProvider + InvoiceProvider {
350350 /// Returns true if the given block is known
351351 /// (though not necessarily a part of the canon chain).
352- fn is_known ( & self , hash : & H256 ) -> bool {
352+ fn is_known ( & self , hash : & BlockHash ) -> bool {
353353 self . is_known_header ( hash) && self . is_known_body ( hash)
354354 }
355355
356356 /// Get raw block data
357- fn block ( & self , hash : & H256 ) -> Option < encoded:: Block > {
357+ fn block ( & self , hash : & BlockHash ) -> Option < encoded:: Block > {
358358 let header = self . block_header_data ( hash) ?;
359359 let body = self . block_body ( hash) ?;
360360
@@ -377,7 +377,7 @@ pub trait BlockProvider: HeaderProvider + BodyProvider + InvoiceProvider {
377377
378378 /// Get a list of transactions for a given block.
379379 /// Returns None if block does not exist.
380- fn transactions ( & self , block_hash : & H256 ) -> Option < Vec < LocalizedTransaction > > {
380+ fn transactions ( & self , block_hash : & BlockHash ) -> Option < Vec < LocalizedTransaction > > {
381381 self . block_body ( block_hash)
382382 . and_then ( |body| self . block_number ( block_hash) . map ( |n| body. view ( ) . localized_transactions ( block_hash, n) ) )
383383 }
@@ -386,28 +386,28 @@ pub trait BlockProvider: HeaderProvider + BodyProvider + InvoiceProvider {
386386impl HeaderProvider for BlockChain {
387387 /// Returns true if the given block is known
388388 /// (though not necessarily a part of the canon chain).
389- fn is_known_header ( & self , hash : & H256 ) -> bool {
389+ fn is_known_header ( & self , hash : & BlockHash ) -> bool {
390390 self . headerchain . is_known_header ( hash)
391391 }
392392
393393 /// Get the familial details concerning a block.
394- fn block_details ( & self , hash : & H256 ) -> Option < BlockDetails > {
394+ fn block_details ( & self , hash : & BlockHash ) -> Option < BlockDetails > {
395395 self . headerchain . block_details ( hash)
396396 }
397397
398398 /// Get the hash of given block's number.
399- fn block_hash ( & self , index : BlockNumber ) -> Option < H256 > {
399+ fn block_hash ( & self , index : BlockNumber ) -> Option < BlockHash > {
400400 self . headerchain . block_hash ( index)
401401 }
402402
403403 /// Get the header RLP of a block.
404- fn block_header_data ( & self , hash : & H256 ) -> Option < encoded:: Header > {
404+ fn block_header_data ( & self , hash : & BlockHash ) -> Option < encoded:: Header > {
405405 self . headerchain . block_header_data ( hash)
406406 }
407407}
408408
409409impl BodyProvider for BlockChain {
410- fn is_known_body ( & self , hash : & H256 ) -> bool {
410+ fn is_known_body ( & self , hash : & BlockHash ) -> bool {
411411 self . body_db . is_known_body ( hash)
412412 }
413413
@@ -419,7 +419,7 @@ impl BodyProvider for BlockChain {
419419 self . body_db . transaction_address_by_tracker ( tracker)
420420 }
421421
422- fn block_body ( & self , hash : & H256 ) -> Option < encoded:: Body > {
422+ fn block_body ( & self , hash : & BlockHash ) -> Option < encoded:: Body > {
423423 self . body_db . block_body ( hash)
424424 }
425425}
0 commit comments