2016-12-11 19:30:54 +01:00
|
|
|
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
2016-02-16 18:21:45 +01:00
|
|
|
// 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/>.
|
|
|
|
|
2016-02-17 14:13:51 +01:00
|
|
|
//! Blockchain filter
|
|
|
|
|
2016-08-15 15:09:00 +02:00
|
|
|
use util::{Address, H256, Hashable, H2048};
|
2016-08-04 15:38:16 +02:00
|
|
|
use util::bloom::Bloomable;
|
2016-12-09 23:01:43 +01:00
|
|
|
use client::BlockId;
|
2016-02-17 12:35:37 +01:00
|
|
|
use log_entry::LogEntry;
|
2016-02-16 18:21:45 +01:00
|
|
|
|
2016-02-17 14:13:51 +01:00
|
|
|
/// Blockchain Filter.
|
2016-12-21 15:09:35 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(feature = "ipc", binary)]
|
2016-02-16 18:21:45 +01:00
|
|
|
pub struct Filter {
|
|
|
|
/// Blockchain will be searched from this block.
|
2016-12-09 23:01:43 +01:00
|
|
|
pub from_block: BlockId,
|
2016-02-16 18:21:45 +01:00
|
|
|
|
|
|
|
/// Till this block.
|
2016-12-09 23:01:43 +01:00
|
|
|
pub to_block: BlockId,
|
2016-02-16 18:21:45 +01:00
|
|
|
|
2016-07-07 09:39:32 +02:00
|
|
|
/// Search addresses.
|
|
|
|
///
|
2016-02-16 18:21:45 +01:00
|
|
|
/// If None, match all.
|
|
|
|
/// If specified, log must be produced by one of these addresses.
|
2016-02-17 12:35:37 +01:00
|
|
|
pub address: Option<Vec<Address>>,
|
2016-02-16 18:21:45 +01:00
|
|
|
|
|
|
|
/// Search topics.
|
2016-07-07 09:39:32 +02:00
|
|
|
///
|
2016-02-16 18:21:45 +01:00
|
|
|
/// If None, match all.
|
|
|
|
/// If specified, log must contain one of these topics.
|
2016-07-07 09:39:32 +02:00
|
|
|
pub topics: Vec<Option<Vec<H256>>>,
|
2016-09-21 12:51:10 +02:00
|
|
|
|
|
|
|
/// Logs limit
|
|
|
|
///
|
|
|
|
/// If None, return all logs
|
|
|
|
/// If specified, should only return *last* `n` logs.
|
|
|
|
pub limit: Option<usize>,
|
2016-02-16 18:21:45 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 18:51:29 +01:00
|
|
|
impl Clone for Filter {
|
|
|
|
fn clone(&self) -> Self {
|
2016-07-07 09:39:32 +02:00
|
|
|
let mut topics = [
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
];
|
2016-02-23 18:51:29 +01:00
|
|
|
for i in 0..4 {
|
|
|
|
topics[i] = self.topics[i].clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
Filter {
|
|
|
|
from_block: self.from_block.clone(),
|
|
|
|
to_block: self.to_block.clone(),
|
|
|
|
address: self.address.clone(),
|
2016-09-21 12:51:10 +02:00
|
|
|
topics: topics[..].to_vec(),
|
|
|
|
limit: self.limit,
|
2016-02-23 18:51:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 18:21:45 +01:00
|
|
|
impl Filter {
|
2016-07-07 09:39:32 +02:00
|
|
|
/// Returns combinations of each address and topic.
|
2016-02-16 18:21:45 +01:00
|
|
|
pub fn bloom_possibilities(&self) -> Vec<H2048> {
|
|
|
|
let blooms = match self.address {
|
|
|
|
Some(ref addresses) if !addresses.is_empty() =>
|
|
|
|
addresses.iter().map(|ref address| {
|
2016-08-15 15:09:00 +02:00
|
|
|
let mut bloom = H2048::default();
|
2016-02-16 18:21:45 +01:00
|
|
|
bloom.shift_bloomed(&address.sha3());
|
|
|
|
bloom
|
|
|
|
}).collect(),
|
2016-08-15 15:09:00 +02:00
|
|
|
_ => vec![H2048::default()]
|
2016-02-16 18:21:45 +01:00
|
|
|
};
|
|
|
|
|
2016-07-07 09:39:32 +02:00
|
|
|
self.topics.iter().fold(blooms, |bs, topic| match *topic {
|
2016-02-16 18:21:45 +01:00
|
|
|
None => bs,
|
2016-02-24 10:35:05 +01:00
|
|
|
Some(ref topics) => bs.into_iter().flat_map(|bloom| {
|
2016-02-16 18:21:45 +01:00
|
|
|
topics.into_iter().map(|topic| {
|
|
|
|
let mut b = bloom.clone();
|
|
|
|
b.shift_bloomed(&topic.sha3());
|
|
|
|
b
|
|
|
|
}).collect::<Vec<H2048>>()
|
2016-02-24 10:35:05 +01:00
|
|
|
}).collect()
|
2016-02-16 18:21:45 +01:00
|
|
|
})
|
|
|
|
}
|
2016-02-17 12:35:37 +01:00
|
|
|
|
|
|
|
/// Returns true if given log entry matches filter.
|
|
|
|
pub fn matches(&self, log: &LogEntry) -> bool {
|
|
|
|
let matches = match self.address {
|
2016-02-24 10:35:05 +01:00
|
|
|
Some(ref addresses) if !addresses.is_empty() => addresses.iter().any(|address| &log.address == address),
|
2016-02-17 12:35:37 +01:00
|
|
|
_ => true
|
|
|
|
};
|
|
|
|
|
2016-02-24 10:35:05 +01:00
|
|
|
matches && self.topics.iter().enumerate().all(|(i, topic)| match *topic {
|
|
|
|
Some(ref topics) if !topics.is_empty() => topics.iter().any(|topic| log.topics.get(i) == Some(topic)),
|
|
|
|
_ => true
|
2016-02-17 12:35:37 +01:00
|
|
|
})
|
|
|
|
}
|
2016-02-16 18:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-08-15 15:09:00 +02:00
|
|
|
use util::FixedHash;
|
2016-02-16 18:21:45 +01:00
|
|
|
use filter::Filter;
|
2016-12-09 23:01:43 +01:00
|
|
|
use client::BlockId;
|
2016-02-17 12:35:37 +01:00
|
|
|
use log_entry::LogEntry;
|
2016-02-16 18:21:45 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bloom_possibilities_none() {
|
|
|
|
let none_filter = Filter {
|
2016-12-09 23:01:43 +01:00
|
|
|
from_block: BlockId::Earliest,
|
|
|
|
to_block: BlockId::Latest,
|
2016-02-16 18:21:45 +01:00
|
|
|
address: None,
|
2016-07-07 09:39:32 +02:00
|
|
|
topics: vec![None, None, None, None],
|
2016-09-21 12:51:10 +02:00
|
|
|
limit: None,
|
2016-02-16 18:21:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let possibilities = none_filter.bloom_possibilities();
|
|
|
|
assert_eq!(possibilities.len(), 1);
|
|
|
|
assert!(possibilities[0].is_zero())
|
|
|
|
}
|
|
|
|
|
|
|
|
// block 399849
|
|
|
|
#[test]
|
|
|
|
fn test_bloom_possibilities_single_address_and_topic() {
|
|
|
|
let filter = Filter {
|
2016-12-09 23:01:43 +01:00
|
|
|
from_block: BlockId::Earliest,
|
|
|
|
to_block: BlockId::Latest,
|
2016-08-15 15:09:00 +02:00
|
|
|
address: Some(vec!["b372018f3be9e171df0581136b59d2faf73a7d5d".into()]),
|
2016-07-07 09:39:32 +02:00
|
|
|
topics: vec![
|
2016-08-15 15:09:00 +02:00
|
|
|
Some(vec!["ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into()]),
|
2016-07-07 09:39:32 +02:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
2016-09-21 12:51:10 +02:00
|
|
|
],
|
|
|
|
limit: None,
|
2016-02-16 18:21:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let possibilities = filter.bloom_possibilities();
|
2016-08-15 15:09:00 +02:00
|
|
|
assert_eq!(possibilities, vec!["00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000004000000004000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000".into()]);
|
2016-02-16 18:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bloom_possibilities_single_address_and_many_topics() {
|
|
|
|
let filter = Filter {
|
2016-12-09 23:01:43 +01:00
|
|
|
from_block: BlockId::Earliest,
|
|
|
|
to_block: BlockId::Latest,
|
2016-08-15 15:09:00 +02:00
|
|
|
address: Some(vec!["b372018f3be9e171df0581136b59d2faf73a7d5d".into()]),
|
2016-07-07 09:39:32 +02:00
|
|
|
topics: vec![
|
2016-08-15 15:09:00 +02:00
|
|
|
Some(vec!["ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into()]),
|
|
|
|
Some(vec!["ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into()]),
|
2016-07-07 09:39:32 +02:00
|
|
|
None,
|
|
|
|
None,
|
2016-09-21 12:51:10 +02:00
|
|
|
],
|
|
|
|
limit: None,
|
2016-02-16 18:21:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let possibilities = filter.bloom_possibilities();
|
2016-08-15 15:09:00 +02:00
|
|
|
assert_eq!(possibilities, vec!["00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000004000000004000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000".into()]);
|
2016-02-16 18:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bloom_possibilites_multiple_addresses_and_topics() {
|
|
|
|
let filter = Filter {
|
2016-12-09 23:01:43 +01:00
|
|
|
from_block: BlockId::Earliest,
|
|
|
|
to_block: BlockId::Latest,
|
2016-02-16 18:21:45 +01:00
|
|
|
address: Some(vec![
|
2016-08-15 15:09:00 +02:00
|
|
|
"b372018f3be9e171df0581136b59d2faf73a7d5d".into(),
|
|
|
|
"b372018f3be9e171df0581136b59d2faf73a7d5d".into(),
|
2016-02-16 18:21:45 +01:00
|
|
|
]),
|
2016-07-07 09:39:32 +02:00
|
|
|
topics: vec![
|
2016-02-16 18:21:45 +01:00
|
|
|
Some(vec![
|
2016-08-15 15:09:00 +02:00
|
|
|
"ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into(),
|
|
|
|
"ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into()
|
2016-02-16 18:21:45 +01:00
|
|
|
]),
|
|
|
|
Some(vec![
|
2016-08-15 15:09:00 +02:00
|
|
|
"ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into(),
|
|
|
|
"ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into()
|
2016-02-16 18:21:45 +01:00
|
|
|
]),
|
2016-08-15 15:09:00 +02:00
|
|
|
Some(vec!["ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into()]),
|
2016-02-16 18:21:45 +01:00
|
|
|
None
|
2016-09-21 12:51:10 +02:00
|
|
|
],
|
|
|
|
limit: None,
|
2016-02-16 18:21:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// number of possibilites should be equal 2 * 2 * 2 * 1 = 8
|
|
|
|
let possibilities = filter.bloom_possibilities();
|
|
|
|
assert_eq!(possibilities.len(), 8);
|
2016-08-15 15:09:00 +02:00
|
|
|
assert_eq!(possibilities[0], "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000004000000004000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000".into());
|
2016-02-16 18:21:45 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 12:35:37 +01:00
|
|
|
#[test]
|
|
|
|
fn test_filter_matches() {
|
|
|
|
let filter = Filter {
|
2016-12-09 23:01:43 +01:00
|
|
|
from_block: BlockId::Earliest,
|
|
|
|
to_block: BlockId::Latest,
|
2016-08-15 15:09:00 +02:00
|
|
|
address: Some(vec!["b372018f3be9e171df0581136b59d2faf73a7d5d".into()]),
|
2016-07-07 09:39:32 +02:00
|
|
|
topics: vec![
|
2016-08-15 15:09:00 +02:00
|
|
|
Some(vec!["ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into()]),
|
|
|
|
Some(vec!["ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23fa".into()]),
|
2016-07-07 09:39:32 +02:00
|
|
|
None,
|
|
|
|
None,
|
2016-09-21 12:51:10 +02:00
|
|
|
],
|
|
|
|
limit: None,
|
2016-02-17 12:35:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let entry0 = LogEntry {
|
2016-08-15 15:09:00 +02:00
|
|
|
address: "b372018f3be9e171df0581136b59d2faf73a7d5d".into(),
|
2016-02-17 12:35:37 +01:00
|
|
|
topics: vec![
|
2016-08-15 15:09:00 +02:00
|
|
|
"ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into(),
|
|
|
|
"ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23fa".into(),
|
|
|
|
"ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into(),
|
2016-02-17 12:35:37 +01:00
|
|
|
],
|
|
|
|
data: vec![]
|
|
|
|
};
|
|
|
|
|
|
|
|
let entry1 = LogEntry {
|
2016-08-15 15:09:00 +02:00
|
|
|
address: "b372018f3be9e171df0581136b59d2faf73a7d5e".into(),
|
2016-02-17 12:35:37 +01:00
|
|
|
topics: vec![
|
2016-08-15 15:09:00 +02:00
|
|
|
"ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into(),
|
|
|
|
"ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23fa".into(),
|
|
|
|
"ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into(),
|
2016-02-17 12:35:37 +01:00
|
|
|
],
|
|
|
|
data: vec![]
|
|
|
|
};
|
|
|
|
|
|
|
|
let entry2 = LogEntry {
|
2016-08-15 15:09:00 +02:00
|
|
|
address: "b372018f3be9e171df0581136b59d2faf73a7d5d".into(),
|
2016-02-17 12:35:37 +01:00
|
|
|
topics: vec![
|
2016-08-15 15:09:00 +02:00
|
|
|
"ff74e91598aed6ae5d2fdcf8b24cd2c7be49a0808112a305069355b7160f23f9".into(),
|
2016-02-17 12:35:37 +01:00
|
|
|
],
|
|
|
|
data: vec![]
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(filter.matches(&entry0), true);
|
|
|
|
assert_eq!(filter.matches(&entry1), false);
|
|
|
|
assert_eq!(filter.matches(&entry2), false);
|
|
|
|
}
|
2016-02-16 18:21:45 +01:00
|
|
|
}
|