bloom possibilities

This commit is contained in:
debris 2016-02-15 11:47:51 +01:00
parent 89bc3e9153
commit bae2682518

View File

@ -57,7 +57,7 @@ pub struct Filter {
}
impl Filter {
/// Returns combinations of each of address topic.
/// Returns combinations of each address and topic.
pub fn bloom_possibilities(&self) -> Vec<H2048> {
let blooms = match self.address {
Some(VariadicValue::Single(ref address)) => {
@ -77,33 +77,21 @@ impl Filter {
match self.topics {
None => blooms,
Some(ref topics) => blooms.into_iter().map(|bloom| {
//for topic in topics {
//match topic {
//VariadicValue::Single => {
//bloom.shift_bloomed(&topic.sha3());
//bloom
//}
//}
//}
}).collect()
Some(ref topics) => topics.iter().fold(blooms, | bs, topic | match *topic {
VariadicValue::Null => bs,
VariadicValue::Single(ref topic) => bs.into_iter().map(|mut bloom| {
bloom.shift_bloomed(&topic.sha3());
bloom
}).collect(),
VariadicValue::Multiple(ref topics) => bs.into_iter().map(|bloom| {
topics.into_iter().map(|topic| {
let mut b = bloom.clone();
b.shift_bloomed(&topic.sha3());
b
}).collect::<Vec<H2048>>()
}).flat_map(|m| m).collect::<Vec<H2048>>()
})
}
//self.address.as_ref().map(|a| match *a {
//VariadicValue::Single(ref address) => {
//let mut bloom = H2048::new();
//bloom.shift_bloomed(&address.sha3());
//vec![bloom]
//},
//VariadicValue::Multiple(ref addresses) => {
//addresses.iter().map(|ref address| {
//let mut bloom = H2048::new();
//bloom.shift_bloomed(&address.sha3());
//bloom
//}).collect()
//},
//VariadicValue::Null => vec![H2048::new()]
//}.into_iter().map(|bloom| match self. {
//}).unwrap_or_else(Vec::new)
}
}
@ -140,4 +128,75 @@ mod tests {
topics: None
});
}
#[test]
fn test_bloom_possibilities_none() {
let none_filter = Filter {
from_block: None,
to_block: None,
address: None,
topics: None
};
let possibilities = none_filter.bloom_possibilities();
assert_eq!(possibilities, vec![H2048::new()]);
}
// block 399849
#[test]
fn test_bloom_possibilities_single_address_and_topic() {
let filter = Filter {
from_block: None,
to_block: None,
address: Some(VariadicValue::Single(Address::from_str("b372018f3be9e171df0581136b59d2faf73a7d5d").unwrap())),
topics: Some(vec![VariadicValue::Single(H256::from_str("ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9").unwrap())])
};
let possibilities = filter.bloom_possibilities();
assert_eq!(possibilities, vec![H2048::from_str("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000004000000004000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000").unwrap()]);
}
#[test]
fn test_bloom_possibilities_single_address_and_many_topics() {
let filter = Filter {
from_block: None,
to_block: None,
address: Some(VariadicValue::Single(Address::from_str("b372018f3be9e171df0581136b59d2faf73a7d5d").unwrap())),
topics: Some(vec![
VariadicValue::Single(H256::from_str("ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9").unwrap()),
VariadicValue::Single(H256::from_str("ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9").unwrap())
])
};
let possibilities = filter.bloom_possibilities();
assert_eq!(possibilities, vec![H2048::from_str("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000004000000004000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000").unwrap()]);
}
#[test]
fn test_bloom_possibilites_multiple_addresses_and_topics() {
let filter = Filter {
from_block: None,
to_block: None,
address: Some(VariadicValue::Multiple(vec![
Address::from_str("b372018f3be9e171df0581136b59d2faf73a7d5d").unwrap(),
Address::from_str("b372018f3be9e171df0581136b59d2faf73a7d5d").unwrap()
])),
topics: Some(vec![
VariadicValue::Multiple(vec![
H256::from_str("ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9").unwrap(),
H256::from_str("ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9").unwrap()
]),
VariadicValue::Multiple(vec![
H256::from_str("ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9").unwrap(),
H256::from_str("ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9").unwrap()
]),
VariadicValue::Single(H256::from_str("ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9").unwrap())
])
};
// number of possibilites should be equal 2 * 2 * 2 * 1 = 8
let possibilities = filter.bloom_possibilities();
assert_eq!(possibilities.len(), 8);
assert_eq!(possibilities[0], H2048::from_str("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000004000000004000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000").unwrap());
}
}