      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215

    // Copyright 2015-2017 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/>.

    //! Eth Filter RPC implementation

    use std::sync::{Arc, Weak};
    use std::collections::HashSet;
    use jsonrpc_core::*;
    use ethcore::miner::MinerService;
    use ethcore::filter::Filter as EthcoreFilter;
    use ethcore::client::{BlockChainClient, BlockId};
    use util::Mutex;
    use v1::traits::EthFilter;
    use v1::types::{BlockNumber, Index, Filter, FilterChanges, Log, H256 as RpcH256, U256 as RpcU256};
    use v1::helpers::{PollFilter, PollManager, limit_logs};
    use v1::impls::eth::pending_logs;

    /// Eth filter rpc implementation.
    pub struct EthFilterClient<C, M> where
    	C: BlockChainClient,
    	M: MinerService {

    	client: Weak<C>,
    	miner: Weak<M>,
    	polls: Mutex<PollManager<PollFilter>>,
    }

    impl<C, M> EthFilterClient<C, M> where
    	C: BlockChainClient,
    	M: MinerService {

    	/// Creates new Eth filter client.
    	pub fn new(client: &Arc<C>, miner: &Arc<M>) -> Self {
    		EthFilterClient {
    			client: Arc::downgrade(client),
    			miner: Arc::downgrade(miner),
    			polls: Mutex::new(PollManager::new()),
    		}
    	}

    	fn active(&self) -> Result<(), Error> {
    		// TODO: only call every 30s at most.
    		take_weak!(self.client).keep_alive();
    		Ok(())
    	}
    }

    impl<C, M> EthFilter for EthFilterClient<C, M>
    	where C: BlockChainClient + 'static, M: MinerService + 'static
    {
    	fn new_filter(&self, filter: Filter) -> Result<RpcU256, Error> {
    		self.active()?;
    		let mut polls = self.polls.lock();
    		let block_number = take_weak!(self.client).chain_info().best_block_number;
    		let id = polls.create_poll(PollFilter::Logs(block_number, Default::default(), filter));
    		Ok(id.into())
    	}

    	fn new_block_filter(&self) -> Result<RpcU256, Error> {
    		self.active()?;

    		let mut polls = self.polls.lock();
    		let id = polls.create_poll(PollFilter::Block(take_weak!(self.client).chain_info().best_block_number));
    		Ok(id.into())
    	}

    	fn new_pending_transaction_filter(&self) -> Result<RpcU256, Error> {
    		self.active()?;

    		let mut polls = self.polls.lock();
    		let best_block = take_weak!(self.client).chain_info().best_block_number;
    		let pending_transactions = take_weak!(self.miner).pending_transactions_hashes(best_block);
    		let id = polls.create_poll(PollFilter::PendingTransaction(pending_transactions));
    		Ok(id.into())
    	}

    	fn filter_changes(&self, index: Index) -> Result<FilterChanges, Error> {
    		self.active()?;
    		let client = take_weak!(self.client);
    		let mut polls = self.polls.lock();
    		match polls.poll_mut(&index.value()) {
    			None => Ok(FilterChanges::Empty),
    			Some(filter) => match *filter {
    				PollFilter::Block(ref mut block_number) => {
    					// + 1, cause we want to return hashes including current block hash.
    					let current_number = client.chain_info().best_block_number + 1;
    					let hashes = (*block_number..current_number).into_iter()
    						.map(BlockId::Number)
    						.filter_map(|id| client.block_hash(id))
    						.map(Into::into)
    						.collect::<Vec<RpcH256>>();

    					*block_number = current_number;

    					Ok(FilterChanges::Hashes(hashes))
    				},
    				PollFilter::PendingTransaction(ref mut previous_hashes) => {
    					// get hashes of pending transactions
    					let best_block = take_weak!(self.client).chain_info().best_block_number;
    					let current_hashes = take_weak!(self.miner).pending_transactions_hashes(best_block);

    					let new_hashes =
    					{
    						let previous_hashes_set = previous_hashes.iter().collect::<HashSet<_>>();

    						//	find all new hashes
    						current_hashes
    							.iter()
    							.filter(|hash| !previous_hashes_set.contains(hash))
    							.cloned()
    							.map(Into::into)
    							.collect::<Vec<RpcH256>>()
    					};

    					// save all hashes of pending transactions
    					*previous_hashes = current_hashes;

    					// return new hashes
    					Ok(FilterChanges::Hashes(new_hashes))
    				},
    				PollFilter::Logs(ref mut block_number, ref mut previous_logs, ref filter) => {
    					// retrive the current block number
    					let current_number = client.chain_info().best_block_number;

    					// check if we need to check pending hashes
    					let include_pending = filter.to_block == Some(BlockNumber::Pending);

    					// build appropriate filter
    					let mut filter: EthcoreFilter = filter.clone().into();
    					filter.from_block = BlockId::Number(*block_number);
    					filter.to_block = BlockId::Latest;

    					// retrieve logs in range from_block..min(BlockId::Latest..to_block)
    					let mut logs = client.logs(filter.clone())
    						.into_iter()
    						.map(From::from)
    						.collect::<Vec<Log>>();

    					// additionally retrieve pending logs
    					if include_pending {
    						let best_block = take_weak!(self.client).chain_info().best_block_number;
    						let pending_logs = pending_logs(&*take_weak!(self.miner), best_block, &filter);

    						// remove logs about which client was already notified about
    						let new_pending_logs: Vec<_> = pending_logs.iter()
    							.filter(|p| !previous_logs.contains(p))
    							.cloned()
    							.collect();

    						// save all logs retrieved by client
    						*previous_logs = pending_logs.into_iter().collect();

    						// append logs array with new pending logs
    						logs.extend(new_pending_logs);
    					}

    					let logs = limit_logs(logs, filter.limit);

    					// save the number of the next block as a first block from which
    					// we want to get logs
    					*block_number = current_number + 1;

    					Ok(FilterChanges::Logs(logs))
    				}
    			}
    		}
    	}

    	fn filter_logs(&self, index: Index) -> Result<Vec<Log>, Error> {
    		self.active()?;

    		let mut polls = self.polls.lock();
    		match polls.poll(&index.value()) {
    			Some(&PollFilter::Logs(ref _block_number, ref _previous_log, ref filter)) => {
    				let include_pending = filter.to_block == Some(BlockNumber::Pending);
    				let filter: EthcoreFilter = filter.clone().into();
    				let mut logs = take_weak!(self.client).logs(filter.clone())
    					.into_iter()
    					.map(From::from)
    					.collect::<Vec<Log>>();

    				if include_pending {
    					let best_block = take_weak!(self.client).chain_info().best_block_number;
    					logs.extend(pending_logs(&*take_weak!(self.miner), best_block, &filter));
    				}

    				let logs = limit_logs(logs, filter.limit);

    				Ok(logs)
    			},
    			// just empty array
    			_ => Ok(Vec::new()),
    		}
    	}

    	fn uninstall_filter(&self, index: Index) -> Result<bool, Error> {
    		self.active()?;

    		self.polls.lock().remove_poll(&index.value());
    		Ok(true)
    	}
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)