This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
support for rpc polling #504
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
35cabec
support for polling
debris 4ce9aad
Merge branch 'jsonrpc2' into rpc_poll_ids
debris 932b312
Merge branch 'jsonrpc2' into rpc_poll_ids
debris 0318907
rpc eth_getFilterChanges returns new blocks, implemented eth_uninstal…
debris 2947a14
Merge branch 'master' into rpc_poll_ids
debris f1538eb
Merge branch 'master' into rpc_poll_ids
debris b60b84f
use transient-hashmap from crates.io
debris 3b4d4a9
Merge branch 'master' into rpc_poll_ids
debris 5f97d51
renamed poll_indexed to poll_manager
debris 05897dd
fixed pending transaction mock implementation
debris a0acfd5
Merge branch 'master' into rpc_poll_ids
debris 5a7dfa4
Merge branch 'master' into rpc_poll_ids
debris 28c5f6f
fixed failing test
debris 03ffcd0
Merge branch 'master' into rpc_poll_ids
debris 190468e
fixed trailing ,
debris b1a6257
fixed PollFilter::PendingTransaction returning empty hash
debris File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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/>. | ||
|
||
mod poll_manager; | ||
mod poll_filter; | ||
|
||
pub use self::poll_manager::PollManager; | ||
pub use self::poll_filter::PollFilter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//! Helper type with all filter possibilities. | ||
|
||
use ethcore::filter::Filter; | ||
|
||
#[derive(Clone)] | ||
pub enum PollFilter { | ||
Block, | ||
PendingTransaction, | ||
Logs(Filter) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// 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 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. | ||
/// | ||
/// Lazily garbage collects unused polls info. | ||
pub struct PollManager<F, T = StandardTimer> where T: Timer { | ||
polls: TransientHashMap<PollId, PollInfo<F>, T>, | ||
next_available_id: PollId | ||
} | ||
|
||
impl<F> PollManager<F, StandardTimer> { | ||
/// Creates new instance of indexer. | ||
pub fn new() -> Self { | ||
PollManager::new_with_timer(Default::default()) | ||
} | ||
} | ||
|
||
impl<F, T> PollManager<F, T> where T: Timer { | ||
pub fn new_with_timer(timer: T) -> Self { | ||
PollManager { | ||
polls: TransientHashMap::new_with_timer(POLL_LIFETIME, timer), | ||
next_available_id: 0, | ||
} | ||
} | ||
|
||
/// Returns id which can be used for new poll. | ||
/// | ||
/// Stores information when last poll happend. | ||
pub fn create_poll(&mut self, filter: F, block: BlockNumber) -> PollId { | ||
self.polls.prune(); | ||
let id = self.next_available_id; | ||
self.next_available_id += 1; | ||
self.polls.insert(id, PollInfo { | ||
filter: filter, | ||
block_number: block, | ||
}); | ||
id | ||
} | ||
|
||
/// Updates information when last poll happend. | ||
pub fn update_poll(&mut self, id: &PollId, block: BlockNumber) { | ||
self.polls.prune(); | ||
if let Some(info) = self.polls.get_mut(id) { | ||
info.block_number = block; | ||
} | ||
} | ||
|
||
/// Returns number of block when last poll happend. | ||
pub fn get_poll_info(&mut self, id: &PollId) -> Option<&PollInfo<F>> { | ||
self.polls.prune(); | ||
self.polls.get(id) | ||
} | ||
|
||
/// Removes poll info. | ||
pub fn remove_poll(&mut self, id: &PollId) { | ||
self.polls.remove(id); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::cell::RefCell; | ||
use transient_hashmap::Timer; | ||
use v1::helpers::PollManager; | ||
|
||
struct TestTimer<'a> { | ||
time: &'a RefCell<i64>, | ||
} | ||
|
||
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 { | ||
time: &time, | ||
}; | ||
|
||
let mut indexer = PollManager::new_with_timer(timer); | ||
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); | ||
assert_eq!(indexer.get_poll_info(&0).unwrap().filter, false); | ||
assert_eq!(indexer.get_poll_info(&0).unwrap().block_number, 21); | ||
|
||
*time.borrow_mut() = 30; | ||
indexer.update_poll(&1, 23); | ||
assert_eq!(indexer.get_poll_info(&1).unwrap().filter, true); | ||
assert_eq!(indexer.get_poll_info(&1).unwrap().block_number, 23); | ||
|
||
*time.borrow_mut() = 75; | ||
indexer.update_poll(&0, 30); | ||
assert!(indexer.get_poll_info(&0).is_none()); | ||
assert_eq!(indexer.get_poll_info(&1).unwrap().filter, true); | ||
assert_eq!(indexer.get_poll_info(&1).unwrap().block_number, 23); | ||
|
||
indexer.remove_poll(&1); | ||
assert!(indexer.get_poll_info(&1).is_none()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is a trailing
,
valid there?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is required here and it means that value is a tuple of one elem. And as a tuple it could be easily deserialised from
Params::Array
of len 1.