Skip to content

Commit 443754f

Browse files
Seulgi Kimforiequal0
authored andcommitted
Distinguish BlockHash from H256
1 parent 5a477d5 commit 443754f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+637
-475
lines changed

core/src/blockchain/block_info.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
use super::route::TreeRoute;
1818
use crate::views::{BlockView, HeaderView};
19-
use primitives::{Bytes, H256};
19+
20+
use ctypes::BlockHash;
21+
use primitives::Bytes;
2022

2123
/// Describes how best block is changed
2224
#[derive(Debug, Clone, PartialEq)]
@@ -37,7 +39,7 @@ pub enum BestBlockChanged {
3739
}
3840

3941
impl BestBlockChanged {
40-
pub fn new_best_hash(&self) -> Option<H256> {
42+
pub fn new_best_hash(&self) -> Option<BlockHash> {
4143
Some(self.best_block()?.hash())
4244
}
4345

@@ -76,7 +78,7 @@ pub enum BestHeaderChanged {
7678
}
7779

7880
impl BestHeaderChanged {
79-
pub fn new_best_hash(&self) -> Option<H256> {
81+
pub fn new_best_hash(&self) -> Option<BlockHash> {
8082
Some(self.header()?.hash())
8183
}
8284

core/src/blockchain/blockchain.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use std::sync::Arc;
1818

19-
use ctypes::BlockNumber;
19+
use ctypes::{BlockHash, BlockNumber};
2020
use kvdb::{DBTransaction, KeyValueDB};
2121
use parking_lot::RwLock;
2222
use primitives::H256;
@@ -44,16 +44,16 @@ const BEST_PROPOSAL_BLOCK_KEY: &[u8] = b"best-proposal-block";
4444
/// **Does not do input data verification.**
4545
pub 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

5959
impl 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 {
349349
pub 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 {
386386
impl 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

409409
impl 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
}

core/src/blockchain/body_db.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::collections::{HashMap, HashSet};
1818
use std::mem;
1919
use std::sync::Arc;
2020

21+
use ctypes::BlockHash;
2122
use kvdb::{DBTransaction, KeyValueDB};
2223
use lru_cache::LruCache;
2324
use parking_lot::{Mutex, RwLock};
@@ -35,7 +36,7 @@ const BODY_CACHE_SIZE: usize = 1000;
3536

3637
pub struct BodyDB {
3738
// block cache
38-
body_cache: Mutex<LruCache<H256, Bytes>>,
39+
body_cache: Mutex<LruCache<BlockHash, Bytes>>,
3940
parcel_address_cache: RwLock<HashMap<H256, TransactionAddress>>,
4041
pending_parcel_addresses: RwLock<HashMap<H256, Option<TransactionAddress>>>,
4142

@@ -280,19 +281,19 @@ impl BodyDB {
280281
pub trait BodyProvider {
281282
/// Returns true if the given block is known
282283
/// (though not necessarily a part of the canon chain).
283-
fn is_known_body(&self, hash: &H256) -> bool;
284+
fn is_known_body(&self, hash: &BlockHash) -> bool;
284285

285286
/// Get the address of parcel with given hash.
286287
fn transaction_address(&self, hash: &H256) -> Option<TransactionAddress>;
287288

288289
fn transaction_address_by_tracker(&self, tracker: &H256) -> Option<TransactionAddress>;
289290

290291
/// Get the block body (uncles and parcels).
291-
fn block_body(&self, hash: &H256) -> Option<encoded::Body>;
292+
fn block_body(&self, hash: &BlockHash) -> Option<encoded::Body>;
292293
}
293294

294295
impl BodyProvider for BodyDB {
295-
fn is_known_body(&self, hash: &H256) -> bool {
296+
fn is_known_body(&self, hash: &BlockHash) -> bool {
296297
self.block_body(hash).is_some()
297298
}
298299

@@ -308,7 +309,7 @@ impl BodyProvider for BodyDB {
308309
}
309310

310311
/// Get block body data
311-
fn block_body(&self, hash: &H256) -> Option<encoded::Body> {
312+
fn block_body(&self, hash: &BlockHash) -> Option<encoded::Body> {
312313
// Check cache first
313314
{
314315
let mut lock = self.body_cache.lock();
@@ -330,7 +331,7 @@ impl BodyProvider for BodyDB {
330331
}
331332

332333
fn parcel_address_entries(
333-
block_hash: H256,
334+
block_hash: BlockHash,
334335
parcel_hashes: impl IntoIterator<Item = H256>,
335336
) -> impl Iterator<Item = (H256, Option<TransactionAddress>)> {
336337
parcel_hashes.into_iter().enumerate().map(move |(index, parcel_hash)| {
@@ -345,7 +346,7 @@ fn parcel_address_entries(
345346
}
346347

347348
fn transaction_address_entries(
348-
block_hash: H256,
349+
block_hash: BlockHash,
349350
parcel_hashes: impl IntoIterator<Item = UnverifiedTransaction>,
350351
) -> impl Iterator<Item = TransactionHashAndAddress> {
351352
parcel_hashes.into_iter().enumerate().filter_map(move |(parcel_index, parcel)| {

0 commit comments

Comments
 (0)