make blockchain functions more idiomatic, avoid needless writes to cache_man (#8054)
This commit is contained in:
parent
ff722cac72
commit
f54944bbfc
@ -216,17 +216,15 @@ impl BlockProvider for BlockChain {
|
|||||||
|
|
||||||
/// Get raw block data
|
/// Get raw block data
|
||||||
fn block(&self, hash: &H256) -> Option<encoded::Block> {
|
fn block(&self, hash: &H256) -> Option<encoded::Block> {
|
||||||
match (self.block_header_data(hash), self.block_body(hash)) {
|
let header = self.block_header_data(hash)?;
|
||||||
(Some(header), Some(body)) => {
|
let body = self.block_body(hash)?;
|
||||||
|
|
||||||
let mut block = RlpStream::new_list(3);
|
let mut block = RlpStream::new_list(3);
|
||||||
let body_rlp = body.rlp();
|
let body_rlp = body.rlp();
|
||||||
block.append_raw(header.rlp().as_raw(), 1);
|
block.append_raw(header.rlp().as_raw(), 1);
|
||||||
block.append_raw(body_rlp.at(0).as_raw(), 1);
|
block.append_raw(body_rlp.at(0).as_raw(), 1);
|
||||||
block.append_raw(body_rlp.at(1).as_raw(), 1);
|
block.append_raw(body_rlp.at(1).as_raw(), 1);
|
||||||
Some(encoded::Block::new(block.out()))
|
Some(encoded::Block::new(block.out()))
|
||||||
},
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get block header data
|
/// Get block header data
|
||||||
@ -250,21 +248,15 @@ impl BlockProvider for BlockChain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read from DB and populate cache
|
// Read from DB and populate cache
|
||||||
let opt = self.db.get(db::COL_HEADERS, hash)
|
let b = self.db.get(db::COL_HEADERS, hash)
|
||||||
.expect("Low level database error. Some issue with disk?");
|
.expect("Low level database error. Some issue with disk?")?;
|
||||||
|
|
||||||
let result = match opt {
|
|
||||||
Some(b) => {
|
|
||||||
let bytes = decompress(&b, blocks_swapper()).into_vec();
|
let bytes = decompress(&b, blocks_swapper()).into_vec();
|
||||||
let mut write = self.block_headers.write();
|
let mut write = self.block_headers.write();
|
||||||
write.insert(*hash, bytes.clone());
|
write.insert(*hash, bytes.clone());
|
||||||
Some(encoded::Header::new(bytes))
|
|
||||||
},
|
|
||||||
None => None
|
|
||||||
};
|
|
||||||
|
|
||||||
self.cache_man.lock().note_used(CacheId::BlockHeader(*hash));
|
self.cache_man.lock().note_used(CacheId::BlockHeader(*hash));
|
||||||
result
|
Some(encoded::Header::new(bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get block body data
|
/// Get block body data
|
||||||
@ -286,50 +278,43 @@ impl BlockProvider for BlockChain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read from DB and populate cache
|
// Read from DB and populate cache
|
||||||
let opt = self.db.get(db::COL_BODIES, hash)
|
let b = self.db.get(db::COL_BODIES, hash)
|
||||||
.expect("Low level database error. Some issue with disk?");
|
.expect("Low level database error. Some issue with disk?")?;
|
||||||
|
|
||||||
let result = match opt {
|
|
||||||
Some(b) => {
|
|
||||||
let bytes = decompress(&b, blocks_swapper()).into_vec();
|
let bytes = decompress(&b, blocks_swapper()).into_vec();
|
||||||
let mut write = self.block_bodies.write();
|
let mut write = self.block_bodies.write();
|
||||||
write.insert(*hash, bytes.clone());
|
write.insert(*hash, bytes.clone());
|
||||||
Some(encoded::Body::new(bytes))
|
|
||||||
},
|
|
||||||
None => None
|
|
||||||
};
|
|
||||||
|
|
||||||
self.cache_man.lock().note_used(CacheId::BlockBody(*hash));
|
self.cache_man.lock().note_used(CacheId::BlockBody(*hash));
|
||||||
|
Some(encoded::Body::new(bytes))
|
||||||
result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the familial details concerning a block.
|
/// Get the familial details concerning a block.
|
||||||
fn block_details(&self, hash: &H256) -> Option<BlockDetails> {
|
fn block_details(&self, hash: &H256) -> Option<BlockDetails> {
|
||||||
let result = self.db.read_with_cache(db::COL_EXTRA, &self.block_details, hash);
|
let result = self.db.read_with_cache(db::COL_EXTRA, &self.block_details, hash)?;
|
||||||
self.cache_man.lock().note_used(CacheId::BlockDetails(*hash));
|
self.cache_man.lock().note_used(CacheId::BlockDetails(*hash));
|
||||||
result
|
Some(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the hash of given block's number.
|
/// Get the hash of given block's number.
|
||||||
fn block_hash(&self, index: BlockNumber) -> Option<H256> {
|
fn block_hash(&self, index: BlockNumber) -> Option<H256> {
|
||||||
let result = self.db.read_with_cache(db::COL_EXTRA, &self.block_hashes, &index);
|
let result = self.db.read_with_cache(db::COL_EXTRA, &self.block_hashes, &index)?;
|
||||||
self.cache_man.lock().note_used(CacheId::BlockHashes(index));
|
self.cache_man.lock().note_used(CacheId::BlockHashes(index));
|
||||||
result
|
Some(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the address of transaction with given hash.
|
/// Get the address of transaction with given hash.
|
||||||
fn transaction_address(&self, hash: &H256) -> Option<TransactionAddress> {
|
fn transaction_address(&self, hash: &H256) -> Option<TransactionAddress> {
|
||||||
let result = self.db.read_with_cache(db::COL_EXTRA, &self.transaction_addresses, hash);
|
let result = self.db.read_with_cache(db::COL_EXTRA, &self.transaction_addresses, hash)?;
|
||||||
self.cache_man.lock().note_used(CacheId::TransactionAddresses(*hash));
|
self.cache_man.lock().note_used(CacheId::TransactionAddresses(*hash));
|
||||||
result
|
Some(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get receipts of block with given hash.
|
/// Get receipts of block with given hash.
|
||||||
fn block_receipts(&self, hash: &H256) -> Option<BlockReceipts> {
|
fn block_receipts(&self, hash: &H256) -> Option<BlockReceipts> {
|
||||||
let result = self.db.read_with_cache(db::COL_EXTRA, &self.block_receipts, hash);
|
let result = self.db.read_with_cache(db::COL_EXTRA, &self.block_receipts, hash)?;
|
||||||
self.cache_man.lock().note_used(CacheId::BlockReceipts(*hash));
|
self.cache_man.lock().note_used(CacheId::BlockReceipts(*hash));
|
||||||
result
|
Some(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn blocks_with_blooms(&self, blooms: &[Bloom], from_block: BlockNumber, to_block: BlockNumber) -> Vec<BlockNumber> {
|
fn blocks_with_blooms(&self, blooms: &[Bloom], from_block: BlockNumber, to_block: BlockNumber) -> Vec<BlockNumber> {
|
||||||
@ -428,11 +413,14 @@ impl<'a> Iterator for EpochTransitionIter<'a> {
|
|||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
loop {
|
loop {
|
||||||
match self.prefix_iter.next() {
|
// some epochs never occurred on the main chain.
|
||||||
Some((key, val)) => {
|
let (key, val) = self.prefix_iter.next()?;
|
||||||
|
|
||||||
// iterator may continue beyond values beginning with this
|
// iterator may continue beyond values beginning with this
|
||||||
// prefix.
|
// prefix.
|
||||||
if !key.starts_with(&EPOCH_KEY_PREFIX[..]) { return None }
|
if !key.starts_with(&EPOCH_KEY_PREFIX[..]) {
|
||||||
|
return None
|
||||||
|
}
|
||||||
|
|
||||||
let transitions: EpochTransitions = ::rlp::decode(&val[..]);
|
let transitions: EpochTransitions = ::rlp::decode(&val[..]);
|
||||||
|
|
||||||
@ -451,11 +439,6 @@ impl<'a> Iterator for EpochTransitionIter<'a> {
|
|||||||
return Some((transitions.number, transition))
|
return Some((transitions.number, transition))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// some epochs never occurred on the main chain.
|
|
||||||
}
|
|
||||||
None => return None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -879,7 +862,6 @@ impl BlockChain {
|
|||||||
let mut update = HashMap::new();
|
let mut update = HashMap::new();
|
||||||
update.insert(block_hash, parent_details);
|
update.insert(block_hash, parent_details);
|
||||||
|
|
||||||
|
|
||||||
let mut write_details = self.block_details.write();
|
let mut write_details = self.block_details.write();
|
||||||
batch.extend_with_cache(db::COL_EXTRA, &mut *write_details, update, CacheUpdatePolicy::Overwrite);
|
batch.extend_with_cache(db::COL_EXTRA, &mut *write_details, update, CacheUpdatePolicy::Overwrite);
|
||||||
|
|
||||||
@ -1072,13 +1054,12 @@ impl BlockChain {
|
|||||||
|
|
||||||
/// Given a block's `parent`, find every block hash which represents a valid possible uncle.
|
/// Given a block's `parent`, find every block hash which represents a valid possible uncle.
|
||||||
pub fn find_uncle_hashes(&self, parent: &H256, uncle_generations: usize) -> Option<Vec<H256>> {
|
pub fn find_uncle_hashes(&self, parent: &H256, uncle_generations: usize) -> Option<Vec<H256>> {
|
||||||
if !self.is_known(parent) { return None; }
|
if !self.is_known(parent) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
let mut excluded = HashSet::new();
|
let mut excluded = HashSet::new();
|
||||||
let ancestry = match self.ancestry_iter(parent.clone()) {
|
let ancestry = self.ancestry_iter(parent.clone())?;
|
||||||
Some(iter) => iter,
|
|
||||||
None => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
for a in ancestry.clone().take(uncle_generations) {
|
for a in ancestry.clone().take(uncle_generations) {
|
||||||
if let Some(uncles) = self.uncle_hashes(&a) {
|
if let Some(uncles) = self.uncle_hashes(&a) {
|
||||||
|
Loading…
Reference in New Issue
Block a user