Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

First part of multi-mining support #804

Merged
merged 1 commit into from
Mar 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion miner/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub struct Miner {
gas_floor_target: RwLock<U256>,
author: RwLock<Address>,
extra_data: RwLock<Bytes>,

}

impl Default for Miner {
Expand Down
2 changes: 2 additions & 0 deletions util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub mod hash;
pub mod bytes;
pub mod rlp;
pub mod misc;
pub mod using_queue;
mod json_aid;
pub mod vector;
pub mod sha3;
Expand All @@ -149,6 +150,7 @@ pub mod table;

pub use common::*;
pub use misc::*;
pub use using_queue::*;
pub use json_aid::*;
pub use rlp::*;
pub use hashdb::*;
Expand Down
207 changes: 207 additions & 0 deletions util/src/using_queue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// 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/>.
//! Queue-like datastructure including notion of usage.

/// Special queue-like datastructure that includes the notion of
/// usage to avoid items that were queued but never used from making it into
/// the queue.
pub struct UsingQueue<T> where T: Clone {
/// Not yet being sealed by a miner, but if one asks for work, we'd prefer they do this.
pending: Option<T>,
/// Currently being sealed by miners.
in_use: Vec<T>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functions accepting predicates (below) are always returning only single value. So I guess it's possible to just fetch using some key. LinkedHashMap would be perfect here.
Something like this?
https://contain-rs.github.io/linked-hash-map/linked_hash_map/struct.LinkedHashMap.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be a sensible optimisation to do later, yeah.

/// The maximum allowable number of items in_use.
max_size: usize,
}

impl<T> UsingQueue<T> where T: Clone {
/// Create a new struct with a maximum size of `max_size`.
pub fn new(max_size: usize) -> UsingQueue<T> {
UsingQueue {
pending: None,
in_use: vec![],
max_size: max_size,
}
}

/// Return a reference to the item at the top of the queue (or `None` if the queue is empty);
/// it doesn't constitute noting that the item is used.
pub fn peek_last_ref(&self) -> Option<&T> {
self.pending.as_ref().or(self.in_use.last())
}

/// Return a reference to the item at the top of the queue (or `None` if the queue is empty);
/// this constitutes using the item and will remain in the queue for at least another
/// `max_size` invocations of `push()`.
pub fn use_last_ref(&mut self) -> Option<&T> {
if let Some(x) = self.pending.take() {
self.in_use.push(x);
if self.in_use.len() > self.max_size {
self.in_use.remove(0);
}
}
self.in_use.last()
}

/// Place an item on the end of the queue. The previously `push()`ed item will be removed
/// if `use_last_ref()` since it was `push()`ed.
pub fn push(&mut self, b: T) {
self.pending = Some(b);
}

/// Clears everything; the queue is entirely reset.
pub fn reset(&mut self) {
self.pending = None;
self.in_use.clear();
}

/// Returns `Some` reference to first block that `f` returns `true` with it as a parameter
/// or `None` if no such block exists in the queue.
pub fn find_if<P>(&self, predicate: P) -> Option<&T> where P: Fn(&T) -> bool {
if self.pending.as_ref().map(|r| predicate(r)).unwrap_or(false) {
self.pending.as_ref()
} else {
self.in_use.iter().find(|r| predicate(r))
}
}

/// Returns the most recently pushed block if `f` returns `true` with a reference to it as
/// a parameter, otherwise `None`.
/// Will not destroy a block if a reference to it has previously been returned by `use_last_ref`,
/// but rather clone it.
pub fn pop_if<P>(&mut self, predicate: P) -> Option<T> where P: Fn(&T) -> bool {
// a bit clumsy - TODO: think about a nicer way of expressing this.
if let Some(x) = self.pending.take() {
if predicate(&x) {
Some(x)
} else {
self.pending = Some(x);
None
}
} else {
self.in_use.last().into_iter().filter(|x| predicate(x)).next().cloned()
}
}
}

#[test]
fn should_find_when_pushed() {
let mut q = UsingQueue::new(2);
q.push(1);
assert!(q.find_if(|i| i == &1).is_some());
}

#[test]
fn should_find_when_pushed_and_used() {
let mut q = UsingQueue::new(2);
q.push(1);
q.use_last_ref();
assert!(q.find_if(|i| i == &1).is_some());
}

#[test]
fn should_find_when_others_used() {
let mut q = UsingQueue::new(2);
q.push(1);
q.use_last_ref();
q.push(2);
q.use_last_ref();
assert!(q.find_if(|i| i == &1).is_some());
}

#[test]
fn should_not_find_when_too_many_used() {
let mut q = UsingQueue::new(1);
q.push(1);
q.use_last_ref();
q.push(2);
q.use_last_ref();
assert!(q.find_if(|i| i == &1).is_none());
}

#[test]
fn should_not_find_when_not_used_and_then_pushed() {
let mut q = UsingQueue::new(3);
q.push(1);
q.push(2);
q.use_last_ref();
assert!(q.find_if(|i| i == &1).is_none());
}

#[test]
fn should_peek_correctly_after_push() {
let mut q = UsingQueue::new(3);
q.push(1);
assert_eq!(q.peek_last_ref(), Some(&1));
q.push(2);
assert_eq!(q.peek_last_ref(), Some(&2));
}

#[test]
fn should_inspect_correctly() {
let mut q = UsingQueue::new(3);
q.push(1);
assert_eq!(q.use_last_ref(), Some(&1));
assert_eq!(q.peek_last_ref(), Some(&1));
q.push(2);
assert_eq!(q.use_last_ref(), Some(&2));
assert_eq!(q.peek_last_ref(), Some(&2));
}

#[test]
fn should_not_find_when_not_used_peeked_and_then_pushed() {
let mut q = UsingQueue::new(3);
q.push(1);
q.peek_last_ref();
q.push(2);
q.use_last_ref();
assert!(q.find_if(|i| i == &1).is_none());
}

#[test]
fn should_pop_used() {
let mut q = UsingQueue::new(3);
q.push(1);
q.use_last_ref();
let popped = q.pop_if(|i| i == &1);
assert_eq!(popped, Some(1));
}

#[test]
fn should_pop_unused() {
let mut q = UsingQueue::new(3);
q.push(1);
assert_eq!(q.pop_if(|i| i == &1), Some(1));
assert_eq!(q.pop_if(|i| i == &1), None);
}

#[test]
fn should_not_pop_unused_before_used() {
let mut q = UsingQueue::new(3);
q.push(1);
q.push(2);
let popped = q.pop_if(|i| i == &1);
assert_eq!(popped, None);
}

#[test]
fn should_not_remove_used_popped() {
let mut q = UsingQueue::new(3);
q.push(1);
q.use_last_ref();
assert_eq!(q.pop_if(|i| i == &1), Some(1));
assert_eq!(q.pop_if(|i| i == &1), Some(1));
}