openethereum/rpc/src/v1/helpers/poll_manager.rs

245 lines
6.2 KiB
Rust
Raw Normal View History

2016-02-23 18:51:29 +01:00
// Copyright 2015, 2016 Ethcore (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/>.
//! Indexes all rpc poll requests.
use util::hash::H256;
use std::collections::HashMap;
2016-02-23 18:51:29 +01:00
use transient_hashmap::{TransientHashMap, Timer, StandardTimer};
/// Lifetime of poll (in seconds).
const POLL_LIFETIME: u64 = 60;
pub type PollId = usize;
pub type BlockNumber = u64;
pub struct PollInfo<F> {
pub filter: F,
pub block_number: BlockNumber
}
impl<F> Clone for PollInfo<F> where F: Clone {
fn clone(&self) -> Self {
PollInfo {
filter: self.filter.clone(),
block_number: self.block_number.clone()
}
}
}
/// Indexes all poll requests.
2016-03-02 05:46:38 +01:00
///
2016-02-23 18:51:29 +01:00
/// Lazily garbage collects unused polls info.
2016-03-02 05:46:38 +01:00
pub struct PollManager<F, T = StandardTimer> where T: Timer {
2016-02-23 18:51:29 +01:00
polls: TransientHashMap<PollId, PollInfo<F>, T>,
transactions_data: HashMap<PollId, Vec<H256>>,
next_available_id: PollId,
2016-02-23 18:51:29 +01:00
}
2016-03-02 05:46:38 +01:00
impl<F> PollManager<F, StandardTimer> {
2016-02-23 18:51:29 +01:00
/// Creates new instance of indexer.
pub fn new() -> Self {
2016-03-02 05:46:38 +01:00
PollManager::new_with_timer(Default::default())
2016-02-23 18:51:29 +01:00
}
}
2016-03-02 05:46:38 +01:00
impl<F, T> PollManager<F, T> where T: Timer {
2016-02-23 18:51:29 +01:00
pub fn new_with_timer(timer: T) -> Self {
2016-03-02 05:46:38 +01:00
PollManager {
2016-02-23 18:51:29 +01:00
polls: TransientHashMap::new_with_timer(POLL_LIFETIME, timer),
transactions_data: HashMap::new(),
2016-03-02 14:03:43 +01:00
next_available_id: 0,
2016-02-23 18:51:29 +01:00
}
}
fn prune(&mut self) {
self.polls.prune();
// self.polls.prune()
// .into_iter()
// .map(|key| {
// self.transactions_data.remove(key);
// });
}
2016-03-02 05:46:38 +01:00
/// Returns id which can be used for new poll.
///
2016-02-23 18:51:29 +01:00
/// Stores information when last poll happend.
pub fn create_poll(&mut self, filter: F, block: BlockNumber) -> PollId {
self.prune();
2016-02-23 18:51:29 +01:00
let id = self.next_available_id;
self.next_available_id += 1;
self.polls.insert(id, PollInfo {
filter: filter,
2016-03-02 14:03:43 +01:00
block_number: block,
2016-02-23 18:51:29 +01:00
});
id
}
/// Updates information when last poll happend.
pub fn update_poll(&mut self, id: &PollId, block: BlockNumber) {
self.prune();
2016-02-23 18:51:29 +01:00
if let Some(info) = self.polls.get_mut(id) {
info.block_number = block;
}
}
/// Returns number of block when last poll happend.
2016-03-10 14:24:33 +01:00
pub fn poll_info(&mut self, id: &PollId) -> Option<&PollInfo<F>> {
self.prune();
2016-02-23 18:51:29 +01:00
self.polls.get(id)
}
2016-03-10 16:00:55 +01:00
pub fn update_transactions(&mut self, id: &PollId, transactions: Vec<H256>) -> Option<Vec<H256>> {
self.prune();
if self.polls.get(id).is_some() {
2016-03-10 16:00:55 +01:00
self.transactions_data.insert(*id, transactions)
} else {
None
}
}
2016-03-10 16:00:55 +01:00
// Normal code always replaces transactions
#[cfg(test)]
/// Returns last transactions hashes for given poll.
2016-03-10 16:00:55 +01:00
pub fn transactions(&mut self, id: &PollId) -> Option<&Vec<H256>> {
self.prune();
self.transactions_data.get(id)
}
2016-02-23 18:51:29 +01:00
/// Removes poll info.
pub fn remove_poll(&mut self, id: &PollId) {
self.polls.remove(id);
self.transactions_data.remove(id);
2016-02-23 18:51:29 +01:00
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use transient_hashmap::Timer;
2016-03-02 05:46:38 +01:00
use v1::helpers::PollManager;
use util::hash::H256;
2016-02-23 18:51:29 +01:00
struct TestTimer<'a> {
2016-03-02 14:03:43 +01:00
time: &'a RefCell<i64>,
2016-02-23 18:51:29 +01:00
}
impl<'a> Timer for TestTimer<'a> {
fn get_time(&self) -> i64 {
*self.time.borrow()
}
}
#[test]
fn test_poll_indexer() {
let time = RefCell::new(0);
let timer = TestTimer {
2016-03-02 14:03:43 +01:00
time: &time,
2016-02-23 18:51:29 +01:00
};
2016-03-02 05:46:38 +01:00
let mut indexer = PollManager::new_with_timer(timer);
2016-02-23 18:51:29 +01:00
assert_eq!(indexer.create_poll(false, 20), 0);
assert_eq!(indexer.create_poll(true, 20), 1);
*time.borrow_mut() = 10;
indexer.update_poll(&0, 21);
2016-03-10 14:24:33 +01:00
assert_eq!(indexer.poll_info(&0).unwrap().filter, false);
assert_eq!(indexer.poll_info(&0).unwrap().block_number, 21);
2016-02-23 18:51:29 +01:00
*time.borrow_mut() = 30;
indexer.update_poll(&1, 23);
2016-03-10 14:24:33 +01:00
assert_eq!(indexer.poll_info(&1).unwrap().filter, true);
assert_eq!(indexer.poll_info(&1).unwrap().block_number, 23);
2016-02-23 18:51:29 +01:00
*time.borrow_mut() = 75;
indexer.update_poll(&0, 30);
2016-03-10 14:24:33 +01:00
assert!(indexer.poll_info(&0).is_none());
assert_eq!(indexer.poll_info(&1).unwrap().filter, true);
assert_eq!(indexer.poll_info(&1).unwrap().block_number, 23);
2016-02-23 18:51:29 +01:00
indexer.remove_poll(&1);
2016-03-10 14:24:33 +01:00
assert!(indexer.poll_info(&1).is_none());
2016-02-23 18:51:29 +01:00
}
#[test]
fn should_return_poll_transactions_hashes() {
// given
let mut indexer = PollManager::new();
let poll_id = indexer.create_poll(false, 20);
2016-03-10 16:00:55 +01:00
assert!(indexer.transactions(&poll_id).is_none());
let transactions = vec![H256::from(1), H256::from(2)];
// when
2016-03-10 16:00:55 +01:00
indexer.update_transactions(&poll_id, transactions.clone());
// then
2016-03-10 16:00:55 +01:00
let txs = indexer.transactions(&poll_id);
assert_eq!(txs.unwrap(), &transactions);
}
#[test]
fn should_remove_transaction_data_when_poll_timed_out() {
// given
let time = RefCell::new(0);
let timer = TestTimer {
time: &time,
};
let mut indexer = PollManager::new_with_timer(timer);
let poll_id = indexer.create_poll(false, 20);
let transactions = vec![H256::from(1), H256::from(2)];
2016-03-10 16:00:55 +01:00
indexer.update_transactions(&poll_id, transactions.clone());
assert!(indexer.transactions(&poll_id).is_some());
// when
*time.borrow_mut() = 75;
indexer.prune();
// then
2016-03-10 16:00:55 +01:00
assert!(indexer.transactions(&poll_id).is_none());
}
#[test]
fn should_remove_transaction_data_when_poll_is_removed() {
// given
let mut indexer = PollManager::new();
let poll_id = indexer.create_poll(false, 20);
let transactions = vec![H256::from(1), H256::from(2)];
// when
2016-03-10 16:00:55 +01:00
indexer.update_transactions(&poll_id, transactions.clone());
assert!(indexer.transactions(&poll_id).is_some());
indexer.remove_poll(&poll_id);
// then
2016-03-10 16:00:55 +01:00
assert!(indexer.transactions(&poll_id).is_none());
}
#[test]
fn should_ignore_transactions_for_invalid_poll_id() {
// given
let mut indexer = PollManager::<()>::new();
let transactions = vec![H256::from(1), H256::from(2)];
// when
2016-03-10 16:00:55 +01:00
indexer.update_transactions(&5, transactions.clone());
// then
2016-03-10 16:00:55 +01:00
assert!(indexer.transactions(&5).is_none());
}
2016-02-23 18:51:29 +01:00
}