trace filters binary ser

This commit is contained in:
Nikolay Volf 2016-05-06 17:30:36 +04:00
parent c622fc62d5
commit f9c08df235
3 changed files with 23 additions and 10 deletions

View File

@ -21,34 +21,40 @@ use util::sha3::Hashable;
use basic_types::LogBloom; use basic_types::LogBloom;
use trace::flat::FlatTrace; use trace::flat::FlatTrace;
use types::trace_types::trace::Action; use types::trace_types::trace::Action;
use ipc::binary::BinaryConvertError;
use std::mem;
use std::collections::VecDeque;
/// Addresses filter. /// Addresses filter.
/// ///
/// Used to create bloom possibilities and match filters. /// Used to create bloom possibilities and match filters.
pub struct AddressesFilter(Vec<Address>); #[derive(Binary)]
pub struct AddressesFilter {
list: Vec<Address>
}
impl From<Vec<Address>> for AddressesFilter { impl From<Vec<Address>> for AddressesFilter {
fn from(addresses: Vec<Address>) -> Self { fn from(addresses: Vec<Address>) -> Self {
AddressesFilter(addresses) AddressesFilter { list: addresses }
} }
} }
impl AddressesFilter { impl AddressesFilter {
/// Returns true if address matches one of the searched addresses. /// Returns true if address matches one of the searched addresses.
pub fn matches(&self, address: &Address) -> bool { pub fn matches(&self, address: &Address) -> bool {
self.matches_all() || self.0.contains(address) self.matches_all() || self.list.contains(address)
} }
/// Returns true if this address filter matches everything. /// Returns true if this address filter matches everything.
pub fn matches_all(&self) -> bool { pub fn matches_all(&self) -> bool {
self.0.is_empty() self.list.is_empty()
} }
/// Returns blooms of this addresses filter. /// Returns blooms of this addresses filter.
pub fn blooms(&self) -> Vec<LogBloom> { pub fn blooms(&self) -> Vec<LogBloom> {
match self.0.is_empty() { match self.list.is_empty() {
true => vec![LogBloom::new()], true => vec![LogBloom::new()],
false => self.0.iter() false => self.list.iter()
.map(|address| LogBloom::from_bloomed(&address.sha3())) .map(|address| LogBloom::from_bloomed(&address.sha3()))
.collect() .collect()
} }
@ -56,11 +62,11 @@ impl AddressesFilter {
/// Returns vector of blooms zipped with blooms of this addresses filter. /// Returns vector of blooms zipped with blooms of this addresses filter.
pub fn with_blooms(&self, blooms: Vec<LogBloom>) -> Vec<LogBloom> { pub fn with_blooms(&self, blooms: Vec<LogBloom>) -> Vec<LogBloom> {
match self.0.is_empty() { match self.list.is_empty() {
true => blooms, true => blooms,
false => blooms false => blooms
.into_iter() .into_iter()
.flat_map(|bloom| self.0.iter() .flat_map(|bloom| self.list.iter()
.map(|address| bloom.with_bloomed(&address.sha3())) .map(|address| bloom.with_bloomed(&address.sha3()))
.collect::<Vec<_>>()) .collect::<Vec<_>>())
.collect() .collect()
@ -68,6 +74,7 @@ impl AddressesFilter {
} }
} }
#[derive(Binary)]
/// Traces filter. /// Traces filter.
pub struct Filter { pub struct Filter {
/// Block range. /// Block range.

View File

@ -17,9 +17,12 @@
use util::H256; use util::H256;
use super::trace::{Action, Res}; use super::trace::{Action, Res};
use header::BlockNumber; use header::BlockNumber;
use ipc::binary::BinaryConvertError;
use std::mem;
use std::collections::VecDeque;
/// Localized trace. /// Localized trace.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Binary)]
pub struct LocalizedTrace { pub struct LocalizedTrace {
/// Type of action performed by a transaction. /// Type of action performed by a transaction.
pub action: Action, pub action: Action,

View File

@ -20,6 +20,7 @@ use util::bytes::Populatable;
use util::numbers::{U256, H256, H2048, Address}; use util::numbers::{U256, H256, H2048, Address};
use std::mem; use std::mem;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::ops::Range;
#[derive(Debug)] #[derive(Debug)]
pub struct BinaryConvertError; pub struct BinaryConvertError;
@ -366,7 +367,7 @@ pub fn serialize<T: BinaryConvertable>(t: &T) -> Result<Vec<u8>, BinaryConvertEr
} }
macro_rules! binary_fixed_size { macro_rules! binary_fixed_size {
($target_ty: ident) => { ($target_ty: ty) => {
impl BinaryConvertable for $target_ty { impl BinaryConvertable for $target_ty {
fn from_bytes(bytes: &[u8], _length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> { fn from_bytes(bytes: &[u8], _length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
match bytes.len().cmp(&::std::mem::size_of::<$target_ty>()) { match bytes.len().cmp(&::std::mem::size_of::<$target_ty>()) {
@ -401,6 +402,8 @@ binary_fixed_size!(U256);
binary_fixed_size!(H256); binary_fixed_size!(H256);
binary_fixed_size!(H2048); binary_fixed_size!(H2048);
binary_fixed_size!(Address); binary_fixed_size!(Address);
binary_fixed_size!(Range<usize>);
binary_fixed_size!(Range<u64>);
#[test] #[test]
fn vec_serialize() { fn vec_serialize() {