98b7c07171
* Update `add_license` script * run script * add `remove duplicate lines script` and run it * Revert changes `English spaces` * strip whitespaces * Revert `GPL` in files with `apache/mit license` * don't append `gpl license` in files with other lic * Don't append `gpl header` in files with other lic. * re-ran script * include c and cpp files too * remove duplicate header * rebase nit
185 lines
10 KiB
Rust
185 lines
10 KiB
Rust
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
|
// This file is part of Parity.
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
extern crate bloomchain;
|
|
extern crate rustc_hex;
|
|
|
|
mod util;
|
|
|
|
use bloomchain::{Bloom, BloomChain, Config};
|
|
use util::{BloomMemoryDatabase, FromHex, for_each_bloom, generate_n_random_blooms};
|
|
|
|
#[test]
|
|
fn simple_bloom_search() {
|
|
let config = Config::default();
|
|
let mut db = BloomMemoryDatabase::default();
|
|
let bloom = Bloom::from_hex("00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
let modified_blooms = {
|
|
let chain = BloomChain::new(config, &db);
|
|
let block_number = 23;
|
|
chain.insert(block_number, bloom.clone())
|
|
};
|
|
|
|
// number of modified blooms should always be equal number of levels
|
|
assert_eq!(modified_blooms.len(), config.levels);
|
|
db.insert_blooms(modified_blooms);
|
|
|
|
let chain = BloomChain::new(config, &db);
|
|
assert_eq!(chain.with_bloom(&(0..100), &bloom), vec![23]);
|
|
assert_eq!(chain.with_bloom(&(0..22), &bloom), vec![]);
|
|
assert_eq!(chain.with_bloom(&(23..23), &bloom), vec![23]);
|
|
assert_eq!(chain.with_bloom(&(24..100), &bloom), vec![]);
|
|
}
|
|
|
|
#[test]
|
|
fn partly_matching_bloom_searach() {
|
|
let config = Config::default();
|
|
let mut db = BloomMemoryDatabase::default();
|
|
let bloom0 = Bloom::from_hex("10100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
let bloom1 = Bloom::from_hex("11000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
let bloom2 = Bloom::from_hex("10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
let modified_blooms_0 = {
|
|
let chain = BloomChain::new(config, &db);
|
|
let block_number = 14;
|
|
chain.insert(block_number, bloom0)
|
|
};
|
|
|
|
db.insert_blooms(modified_blooms_0);
|
|
|
|
let modified_blooms_1 = {
|
|
let chain = BloomChain::new(config, &db);
|
|
let block_number = 15;
|
|
chain.insert(block_number, bloom1)
|
|
};
|
|
|
|
db.insert_blooms(modified_blooms_1);
|
|
|
|
let chain = BloomChain::new(config, &db);
|
|
assert_eq!(chain.with_bloom(&(0..100), &bloom2), vec![14, 15]);
|
|
}
|
|
|
|
#[test]
|
|
fn bloom_replace() {
|
|
let config = Config::default();
|
|
let mut db = BloomMemoryDatabase::default();
|
|
let bloom0 = Bloom::from_hex("10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
let bloom1 = Bloom::from_hex("01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
let bloom2 = Bloom::from_hex("00100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
let bloom3 = Bloom::from_hex("00010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
let bloom4 = Bloom::from_hex("00001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
let bloom5 = Bloom::from_hex("00000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
let modified_blooms_0 = {
|
|
let chain = BloomChain::new(config, &db);
|
|
let block_number = 14;
|
|
chain.insert(block_number, bloom0.clone())
|
|
};
|
|
|
|
db.insert_blooms(modified_blooms_0);
|
|
|
|
let modified_blooms_1 = {
|
|
let chain = BloomChain::new(config, &db);
|
|
let block_number = 15;
|
|
chain.insert(block_number, bloom1.clone())
|
|
};
|
|
|
|
db.insert_blooms(modified_blooms_1);
|
|
|
|
let modified_blooms_2 = {
|
|
let chain = BloomChain::new(config, &db);
|
|
let block_number = 16;
|
|
chain.insert(block_number, bloom2.clone())
|
|
};
|
|
|
|
db.insert_blooms(modified_blooms_2);
|
|
|
|
let modified_blooms_3 = {
|
|
let chain = BloomChain::new(config, &db);
|
|
let block_number = 17;
|
|
chain.insert(block_number, bloom3.clone())
|
|
};
|
|
|
|
db.insert_blooms(modified_blooms_3);
|
|
|
|
let reset_modified_blooms = {
|
|
let chain = BloomChain::new(config, &db);
|
|
chain.replace(&(15..17), vec![bloom4.clone(), bloom5.clone()])
|
|
};
|
|
|
|
db.insert_blooms(reset_modified_blooms);
|
|
|
|
let chain = BloomChain::new(config, &db);
|
|
assert_eq!(chain.with_bloom(&(0..100), &bloom0), vec![14]);
|
|
assert_eq!(chain.with_bloom(&(0..100), &bloom1), vec![]);
|
|
assert_eq!(chain.with_bloom(&(0..100), &bloom2), vec![]);
|
|
assert_eq!(chain.with_bloom(&(0..100), &bloom3), vec![]);
|
|
assert_eq!(chain.with_bloom(&(0..100), &bloom4), vec![15]);
|
|
assert_eq!(chain.with_bloom(&(0..100), &bloom5), vec![16]);
|
|
}
|
|
|
|
#[test]
|
|
fn file_test_bloom_search() {
|
|
let config = Config::default();
|
|
let mut db = BloomMemoryDatabase::default();
|
|
let blooms_file = include_bytes!("data/blooms.txt");
|
|
|
|
for_each_bloom(blooms_file, | block_number, bloom | {
|
|
let modified_blooms = {
|
|
let chain = BloomChain::new(config, &db);
|
|
chain.insert(block_number, bloom)
|
|
};
|
|
|
|
// number of modified blooms should always be equal number of levels
|
|
assert_eq!(modified_blooms.len(), config.levels);
|
|
db.insert_blooms(modified_blooms);
|
|
});
|
|
|
|
for_each_bloom(blooms_file, | block_number, bloom | {
|
|
let chain = BloomChain::new(config, &db);
|
|
let blocks = chain.with_bloom(&(block_number..block_number), &bloom);
|
|
assert_eq!(blocks.len(), 1);
|
|
assert_eq!(blocks[0], block_number);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn random_bloom_replacement() {
|
|
let insertions = 10_000;
|
|
|
|
let config = Config::default();
|
|
let mut db = BloomMemoryDatabase::default();
|
|
let blooms = generate_n_random_blooms(insertions);
|
|
|
|
for (i, bloom) in blooms.iter().enumerate() {
|
|
|
|
let modified_blooms = {
|
|
let chain = BloomChain::new(config, &db);
|
|
chain.replace(&(i..i), vec![bloom.clone()])
|
|
};
|
|
|
|
db.insert_blooms(modified_blooms);
|
|
}
|
|
|
|
for (i, bloom) in blooms.iter().enumerate() {
|
|
let chain = BloomChain::new(config, &db);
|
|
let blocks = chain.with_bloom(&(i..i), bloom);
|
|
assert_eq!(blocks.len(), 1);
|
|
assert_eq!(blocks[0], i);
|
|
}
|
|
}
|