openethereum/util/bloomchain/src/position/manager.rs
André Silva 610f6f1425 [Beta] Backports (#8136)
* Support parity protocol. (#8035)

* updater: apply exponential backoff after download failure (#8059)

* updater: apply exponential backoff after download failure

* updater: reset backoff on new release

* Max code size on Kovan (#8067)

* Enable code size limit on kovan

* Fix formatting.

* Limit incoming connections.  (#8060)

* Limit ingress connections
* Optimized handshakes logging

* WASM libraries bump (#7970)

* update wasmi, parity-wasm, wasm-utils to latest version

* Update to new wasmi & error handling

* also utilize new stack limiter

* fix typo

* replace dependency url

* Cargo.lock update

* add some dos protection (#8084)

* revert removing blooms (#8066)

* Revert "fix traces, removed bloomchain crate, closes #7228, closes #7167"

This reverts commit 1bf62038678295e5586f02a38a0c5aab9a9efe62.

* Revert "fixed broken logs (#7934)"

This reverts commit f8a2e53f3e.

* fixed broken logs

* bring back old lock order

* remove migration v13

* revert CURRENT_VERSION to 12 in migration.rs

* more dos protection (#8104)

* Const time comparison (#8113)

* Use `subtle::slices_equal` for constant time comparison.

Also update the existing version of subtle in `ethcrypto` from
0.1 to 0.5

* Test specifically for InvalidPassword error.

* fix trace filter returning returning unrelated reward calls, closes #8070 (#8098)

* network: init discovery using healthy nodes (#8061)

* network: init discovery using healthy nodes

* network: fix style grumble

* network: fix typo

* Postpone Kovan hard fork (#8137)

* ethcore: postpone Kovan hard fork

* util: update version fork metadata

* Disable UI by default. (#8105)

* dapps: update parity-ui dependencies (#8160)
2018-03-19 21:40:56 +01:00

143 lines
3.0 KiB
Rust

//! Simplifies working with bloom indexes.
use super::Position;
/// Simplifies working with bloom indexes.
pub struct Manager {
index_size: usize,
level_sizes: Vec<usize>,
}
impl Manager {
/// Creates new indexer.
pub fn new(index_size: usize, levels: usize) -> Self {
if levels == 0 {
panic!("Manager requires at least 1 level.");
}
let mut level_sizes = vec![1];
level_sizes.extend_from_slice(&(1..).into_iter()
.scan(1, |acc, _| {
*acc = *acc * index_size;
Some(*acc)
})
.take(levels - 1)
.collect::<Vec<usize>>());
Manager {
index_size: index_size,
level_sizes: level_sizes,
}
}
/// Unsafely get level size.
pub fn level_size(&self, level: usize) -> usize {
self.level_sizes[level as usize]
}
/// Converts block number and level to `Position`.
pub fn position(&self, block_number: usize, level: usize) -> Position {
Position {
level: level,
index: block_number / self.level_size(level),
}
}
/// Return bloom which are dependencies for given index.
///
/// Bloom indexes are ordered from lowest to highest.
pub fn lower_level_positions(&self, index: &Position) -> Vec<Position> {
// this is the lowest level
if index.level == 0 {
return vec![];
}
let new_level = index.level - 1;
let offset = self.index_size * index.index;
(0..self.index_size)
.map(|i| Position {
level: new_level,
index: offset + i
})
.collect()
}
/// Return number of levels.
pub fn levels(&self) -> usize {
self.level_sizes.len()
}
/// Returns max indexer level.
pub fn max_level(&self) -> usize {
self.level_sizes.len() - 1
}
}
#[cfg(test)]
mod tests {
use position::Position;
use super::*;
#[test]
fn test_level_size() {
let indexer = Manager::new(16, 3);
assert_eq!(indexer.level_size(0), 1);
assert_eq!(indexer.level_size(1), 16);
assert_eq!(indexer.level_size(2), 256);
}
#[test]
fn test_position() {
let indexer = Manager::new(16, 3);
let bi0 = indexer.position(0, 0);
assert_eq!(bi0.level, 0);
assert_eq!(bi0.index, 0);
let bi1 = indexer.position(1, 0);
assert_eq!(bi1.level, 0);
assert_eq!(bi1.index, 1);
let bi2 = indexer.position(2, 0);
assert_eq!(bi2.level, 0);
assert_eq!(bi2.index, 2);
let bi3 = indexer.position(3, 1);
assert_eq!(bi3.level, 1);
assert_eq!(bi3.index, 0);
let bi4 = indexer.position(15, 1);
assert_eq!(bi4.level, 1);
assert_eq!(bi4.index, 0);
let bi5 = indexer.position(16, 1);
assert_eq!(bi5.level, 1);
assert_eq!(bi5.index, 1);
let bi6 = indexer.position(255, 2);
assert_eq!(bi6.level, 2);
assert_eq!(bi6.index, 0);
let bi7 = indexer.position(256, 2);
assert_eq!(bi7.level, 2);
assert_eq!(bi7.index, 1);
}
#[test]
fn test_lower_level_positions() {
let indexer = Manager::new(16, 3);
let bi = indexer.position(256, 2);
assert_eq!(bi.level, 2);
assert_eq!(bi.index, 1);
let mut ebis = vec![];
for i in 16..32 {
ebis.push(Position { level: 1, index: i});
}
let bis = indexer.lower_level_positions(&bi);
assert_eq!(ebis, bis);
}
}