moved chainfilter from util to ethcore, blockchain stores block blooms

This commit is contained in:
debris 2016-02-11 14:35:03 +01:00
parent b0f2ae147b
commit 2cf0f1b5f3
6 changed files with 28 additions and 22 deletions

View File

@ -22,6 +22,7 @@ use header::*;
use extras::*;
use transaction::*;
use views::*;
use receipt::Receipt;
/// Represents a tree route between `from` block and `to` block:
pub struct TreeRoute {
@ -425,7 +426,7 @@ impl BlockChain {
/// Inserts the block into backing cache database.
/// Expects the block to be valid and already verified.
/// If the block is already known, does nothing.
pub fn insert_block(&self, bytes: &[u8]) {
pub fn insert_block(&self, bytes: &[u8], receipts: &[Receipt]) {
// create views onto rlp
let block = BlockView::new(bytes);
let header = block.header_view();
@ -437,7 +438,7 @@ impl BlockChain {
// store block in db
self.blocks_db.put(&hash, &bytes).unwrap();
let (batch, new_best, details) = self.block_to_extras_insert_batch(bytes);
let (batch, new_best, details) = self.block_to_extras_insert_batch(bytes, receipts);
// update best block
let mut best_block = self.best_block.write().unwrap();
@ -457,7 +458,7 @@ impl BlockChain {
/// Transforms block into WriteBatch that may be written into database
/// Additionally, if it's new best block it returns new best block object.
fn block_to_extras_insert_batch(&self, bytes: &[u8]) -> (WriteBatch, Option<BestBlock>, BlockDetails) {
fn block_to_extras_insert_batch(&self, bytes: &[u8], receipts: &[Receipt]) -> (WriteBatch, Option<BestBlock>, BlockDetails) {
// create views onto rlp
let block = BlockView::new(bytes);
let header = block.header_view();
@ -495,6 +496,11 @@ impl BlockChain {
});
}
// update block blooms
batch.put_extras(&hash, &BlockLogBlooms {
blooms: receipts.iter().map(|r| r.log_bloom.clone()).collect()
});
// if it's not new best block, just return
if !is_new_best {
return (batch, None, details);
@ -682,7 +688,7 @@ mod tests {
let first = "f90285f90219a03caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0bac6177a79e910c98d86ec31a09ae37ac2de15b754fd7bed1ba52362c49416bfa0d45893a296c1490a978e0bd321b5f2635d8280365c1fe9f693d65f233e791344a0c7778a7376099ee2e5c455791c1885b5c361b95713fddcbe32d97fd01334d296b90100000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000400000000000000000000000000000000000000000000000000000008302000001832fefd882560b845627cb99a00102030405060708091011121314151617181920212223242526272829303132a08ccb2837fb2923bd97e8f2d08ea32012d6e34be018c73e49a0f98843e8f47d5d88e53be49fec01012ef866f864800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d8785012a05f200801ba0cb088b8d2ff76a7b2c6616c9d02fb6b7a501afbf8b69d7180b09928a1b80b5e4a06448fe7476c606582039bb72a9f6f4b4fad18507b8dfbd00eebbe151cc573cd2c0".from_hex().unwrap();
bc.insert_block(&first);
bc.insert_block(&first, &[]);
let first_hash = H256::from_str("a940e5af7d146b3b917c953a82e1966b906dace3a4e355b5b0a4560190357ea1").unwrap();
@ -715,10 +721,10 @@ mod tests {
let temp = RandomTempPath::new();
let bc = BlockChain::new(&genesis, temp.as_path());
bc.insert_block(&b1);
bc.insert_block(&b2);
bc.insert_block(&b3a);
bc.insert_block(&b3b);
bc.insert_block(&b1, &[]);
bc.insert_block(&b2, &[]);
bc.insert_block(&b3a, &[]);
bc.insert_block(&b3b, &[]);
assert_eq!(bc.best_block_hash(), best_block_hash);
assert_eq!(bc.block_number(&genesis_hash).unwrap(), 0);
@ -795,7 +801,7 @@ mod tests {
{
let bc = BlockChain::new(&genesis, temp.as_path());
assert_eq!(bc.best_block_hash(), genesis_hash);
bc.insert_block(&b1);
bc.insert_block(&b1, &[]);
assert_eq!(bc.best_block_hash(), b1_hash);
}
@ -854,7 +860,7 @@ mod tests {
let temp = RandomTempPath::new();
let bc = BlockChain::new(&genesis, temp.as_path());
bc.insert_block(&b1);
bc.insert_block(&b1, &[]);
let transactions = bc.transactions(&b1_hash).unwrap();
assert_eq!(transactions.len(), 7);

View File

@ -16,12 +16,13 @@
//! Multilevel blockchain bloom filter.
//!
//! ```
//! ```not_run
//! extern crate ethcore_util as util;
//! extern crate ethcore;
//! use std::str::FromStr;
//! use util::chainfilter::*;
//! use util::sha3::*;
//! use util::hash::*;
//! use ethcore::chainfilter::*;
//!
//! fn main() {
//! let (index_size, bloom_levels) = (16, 3);
@ -55,8 +56,8 @@
//! ```
//!
use std::collections::{HashMap};
use hash::*;
use sha3::*;
use util::hash::*;
use util::sha3::*;
/// Represents bloom index in cache
///
@ -350,10 +351,10 @@ impl<'a, D> ChainFilter<'a, D> where D: FilterDataSource
#[cfg(test)]
mod tests {
use hash::*;
use chainfilter::*;
use sha3::*;
use std::str::FromStr;
use util::hash::*;
use chainfilter::*;
use util::sha3::*;
#[test]
fn test_level_size() {

View File

@ -304,7 +304,7 @@ impl Client {
good_blocks.push(header.hash().clone());
self.chain.write().unwrap().insert_block(&block.bytes); //TODO: err here?
self.chain.write().unwrap().insert_block(&block.bytes, result.block().receipts()); //TODO: err here?
let ancient = if header.number() >= HISTORY { Some(header.number() - HISTORY) } else { None };
match result.drain().commit(header.number(), &header.hash(), ancient.map(|n|(n, self.chain.read().unwrap().block_hash(n).unwrap()))) {
Ok(_) => (),

View File

@ -118,6 +118,7 @@ mod account_db;
mod action_params;
mod null_engine;
mod builtin;
mod chainfilter;
mod extras;
mod substate;
mod executive;

View File

@ -224,7 +224,7 @@ pub fn generate_dummy_blockchain(block_number: u32) -> GuardedTempResult<BlockCh
let temp = RandomTempPath::new();
let bc = BlockChain::new(&create_unverifiable_block(0, H256::zero()), temp.as_path());
for block_order in 1..block_number {
bc.insert_block(&create_unverifiable_block(block_order, bc.best_block_hash()));
bc.insert_block(&create_unverifiable_block(block_order, bc.best_block_hash()), &[]);
}
GuardedTempResult::<BlockChain> {
@ -237,7 +237,7 @@ pub fn generate_dummy_blockchain_with_extra(block_number: u32) -> GuardedTempRes
let temp = RandomTempPath::new();
let bc = BlockChain::new(&create_unverifiable_block(0, H256::zero()), temp.as_path());
for block_order in 1..block_number {
bc.insert_block(&create_unverifiable_block_with_extra(block_order, bc.best_block_hash(), None));
bc.insert_block(&create_unverifiable_block_with_extra(block_order, bc.best_block_hash(), None), &[]);
}
GuardedTempResult::<BlockChain> {

View File

@ -121,7 +121,6 @@ pub mod memorydb;
pub mod overlaydb;
pub mod journaldb;
mod math;
pub mod chainfilter;
pub mod crypto;
pub mod triehash;
pub mod trie;
@ -143,7 +142,6 @@ pub use memorydb::*;
pub use overlaydb::*;
pub use journaldb::*;
pub use math::*;
pub use chainfilter::*;
pub use crypto::*;
pub use triehash::*;
pub use trie::*;