2016-06-01 19:37:34 +02: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/>.
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
use std::mem;
|
|
|
|
use std::cell::RefCell;
|
2016-07-13 19:59:59 +02:00
|
|
|
use std::sync::{mpsc, Arc};
|
2016-08-03 10:36:54 +02:00
|
|
|
use std::collections::BTreeMap;
|
2016-06-15 00:17:23 +02:00
|
|
|
use jsonrpc_core;
|
2016-07-13 19:59:59 +02:00
|
|
|
use util::{Mutex, RwLock, U256};
|
2016-08-03 10:36:54 +02:00
|
|
|
use v1::helpers::{ConfirmationRequest, ConfirmationPayload};
|
2016-11-09 13:13:35 +01:00
|
|
|
use v1::types::ConfirmationResponse;
|
2016-06-02 17:05:13 +02:00
|
|
|
|
2016-06-15 00:17:23 +02:00
|
|
|
/// Result that can be returned from JSON RPC.
|
2016-11-09 13:13:35 +01:00
|
|
|
pub type RpcResult = Result<ConfirmationResponse, jsonrpc_core::Error>;
|
2016-06-02 17:05:13 +02:00
|
|
|
|
|
|
|
/// Possible events happening in the queue that can be listened to.
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum QueueEvent {
|
|
|
|
/// Receiver should stop work upon receiving `Finish` message.
|
|
|
|
Finish,
|
|
|
|
/// Informs about new request.
|
|
|
|
NewRequest(U256),
|
|
|
|
/// Request rejected.
|
|
|
|
RequestRejected(U256),
|
|
|
|
/// Request resolved.
|
|
|
|
RequestConfirmed(U256),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Defines possible errors returned from queue receiving method.
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum QueueError {
|
|
|
|
/// Returned when method has been already used (no receiver available).
|
|
|
|
AlreadyUsed,
|
|
|
|
/// Returned when receiver encounters an error.
|
|
|
|
ReceiverError(mpsc::RecvError),
|
|
|
|
}
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
/// Defines possible errors when inserting to queue
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum QueueAddError {
|
|
|
|
LimitReached,
|
|
|
|
}
|
|
|
|
|
2016-06-02 17:05:13 +02:00
|
|
|
/// Message Receiver type
|
|
|
|
pub type QueueEventReceiver = mpsc::Receiver<QueueEvent>;
|
2016-06-01 19:37:34 +02:00
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
// TODO [todr] to consider: timeout instead of limit?
|
|
|
|
const QUEUE_LIMIT: usize = 50;
|
|
|
|
|
2016-06-01 19:37:34 +02:00
|
|
|
/// A queue of transactions awaiting to be confirmed and signed.
|
|
|
|
pub trait SigningQueue: Send + Sync {
|
|
|
|
/// Add new request to the queue.
|
2016-06-02 17:05:13 +02:00
|
|
|
/// Returns a `ConfirmationPromise` that can be used to await for resolution of given request.
|
2016-09-01 12:00:00 +02:00
|
|
|
fn add_request(&self, request: ConfirmationPayload) -> Result<ConfirmationPromise, QueueAddError>;
|
2016-06-01 19:37:34 +02:00
|
|
|
|
2016-06-02 17:05:13 +02:00
|
|
|
/// Removes a request from the queue.
|
2016-08-03 10:36:54 +02:00
|
|
|
/// Notifies possible token holders that request was rejected.
|
|
|
|
fn request_rejected(&self, id: U256) -> Option<ConfirmationRequest>;
|
2016-06-02 17:05:13 +02:00
|
|
|
|
|
|
|
/// Removes a request from the queue.
|
2016-08-03 10:36:54 +02:00
|
|
|
/// Notifies possible token holders that request was confirmed and given hash was assigned.
|
|
|
|
fn request_confirmed(&self, id: U256, result: RpcResult) -> Option<ConfirmationRequest>;
|
2016-06-02 17:05:13 +02:00
|
|
|
|
|
|
|
/// Returns a request if it is contained in the queue.
|
2016-08-03 10:36:54 +02:00
|
|
|
fn peek(&self, id: &U256) -> Option<ConfirmationRequest>;
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
/// Return copy of all the requests in the queue.
|
2016-08-03 10:36:54 +02:00
|
|
|
fn requests(&self) -> Vec<ConfirmationRequest>;
|
2016-06-21 14:55:25 +02:00
|
|
|
|
2016-08-03 10:36:54 +02:00
|
|
|
/// Returns number of requests awaiting confirmation.
|
2016-06-21 14:55:25 +02:00
|
|
|
fn len(&self) -> usize;
|
|
|
|
|
2016-08-03 10:36:54 +02:00
|
|
|
/// Returns true if there are no requests awaiting confirmation.
|
2016-06-21 14:55:25 +02:00
|
|
|
fn is_empty(&self) -> bool;
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2016-07-19 09:19:58 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2016-08-03 10:36:54 +02:00
|
|
|
/// Result of a pending confirmation request.
|
2016-07-19 09:19:58 +02:00
|
|
|
pub enum ConfirmationResult {
|
2016-08-03 10:36:54 +02:00
|
|
|
/// The request has not yet been confirmed nor rejected.
|
2016-06-02 17:05:13 +02:00
|
|
|
Waiting,
|
2016-08-03 10:36:54 +02:00
|
|
|
/// The request has been rejected.
|
2016-06-02 17:05:13 +02:00
|
|
|
Rejected,
|
2016-08-03 10:36:54 +02:00
|
|
|
/// The request has been confirmed.
|
2016-06-15 00:17:23 +02:00
|
|
|
Confirmed(RpcResult),
|
2016-06-02 17:05:13 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
type Listener = Box<FnMut(Option<RpcResult>) + Send>;
|
2016-06-02 17:05:13 +02:00
|
|
|
|
|
|
|
/// A handle to submitted request.
|
|
|
|
/// Allows to block and wait for a resolution of that request.
|
|
|
|
pub struct ConfirmationToken {
|
|
|
|
result: Arc<Mutex<ConfirmationResult>>,
|
2016-09-01 12:00:00 +02:00
|
|
|
listeners: Arc<Mutex<Vec<Listener>>>,
|
2016-08-03 10:36:54 +02:00
|
|
|
request: ConfirmationRequest,
|
2016-06-02 17:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ConfirmationPromise {
|
|
|
|
id: U256,
|
|
|
|
result: Arc<Mutex<ConfirmationResult>>,
|
2016-09-01 12:00:00 +02:00
|
|
|
listeners: Arc<Mutex<Vec<Listener>>>,
|
2016-06-02 17:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfirmationToken {
|
|
|
|
/// Submit solution to all listeners
|
2016-06-15 00:17:23 +02:00
|
|
|
fn resolve(&self, result: Option<RpcResult>) {
|
2016-09-01 12:00:00 +02:00
|
|
|
let wrapped = result.clone().map_or(ConfirmationResult::Rejected, |h| ConfirmationResult::Confirmed(h));
|
|
|
|
{
|
|
|
|
let mut res = self.result.lock();
|
|
|
|
*res = wrapped.clone();
|
|
|
|
}
|
2016-06-02 17:05:13 +02:00
|
|
|
// Notify listener
|
2016-09-01 12:00:00 +02:00
|
|
|
let listeners = {
|
|
|
|
let mut listeners = self.listeners.lock();
|
|
|
|
mem::replace(&mut *listeners, Vec::new())
|
|
|
|
};
|
|
|
|
for mut listener in listeners {
|
|
|
|
listener(result.clone());
|
|
|
|
}
|
2016-06-02 17:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn as_promise(&self) -> ConfirmationPromise {
|
|
|
|
ConfirmationPromise {
|
|
|
|
id: self.request.id,
|
|
|
|
result: self.result.clone(),
|
2016-09-01 12:00:00 +02:00
|
|
|
listeners: self.listeners.clone(),
|
2016-06-02 17:05:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfirmationPromise {
|
2016-07-19 09:19:58 +02:00
|
|
|
/// Get the ID for this request.
|
|
|
|
pub fn id(&self) -> U256 { self.id }
|
|
|
|
|
|
|
|
/// Just get the result, assuming it exists.
|
2016-09-01 12:00:00 +02:00
|
|
|
pub fn result(&self) -> ConfirmationResult {
|
|
|
|
self.result.lock().clone()
|
|
|
|
}
|
2016-06-02 17:05:13 +02:00
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
pub fn wait_for_result<F>(self, callback: F) where F: FnOnce(Option<RpcResult>) + Send + 'static {
|
2016-08-03 10:36:54 +02:00
|
|
|
trace!(target: "own_tx", "Signer: Awaiting confirmation... ({:?}).", self.id);
|
2016-09-01 12:00:00 +02:00
|
|
|
let _result = self.result.lock();
|
|
|
|
let mut listeners = self.listeners.lock();
|
|
|
|
// TODO [todr] Overcoming FnBox unstability
|
|
|
|
let callback = RefCell::new(Some(callback));
|
|
|
|
listeners.push(Box::new(move |result| {
|
|
|
|
let ref mut f = *callback.borrow_mut();
|
|
|
|
f.take().expect("Callbacks are called only once.")(result)
|
|
|
|
}));
|
2016-06-02 17:05:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
|
2016-08-03 10:36:54 +02:00
|
|
|
/// Queue for all unconfirmed requests.
|
2016-06-01 19:37:34 +02:00
|
|
|
pub struct ConfirmationsQueue {
|
|
|
|
id: Mutex<U256>,
|
2016-08-03 10:36:54 +02:00
|
|
|
queue: RwLock<BTreeMap<U256, ConfirmationToken>>,
|
2016-06-02 17:05:13 +02:00
|
|
|
sender: Mutex<mpsc::Sender<QueueEvent>>,
|
|
|
|
receiver: Mutex<Option<mpsc::Receiver<QueueEvent>>>,
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ConfirmationsQueue {
|
|
|
|
fn default() -> Self {
|
2016-06-02 17:05:13 +02:00
|
|
|
let (send, recv) = mpsc::channel();
|
|
|
|
|
2016-06-01 19:37:34 +02:00
|
|
|
ConfirmationsQueue {
|
|
|
|
id: Mutex::new(U256::from(0)),
|
2016-08-03 10:36:54 +02:00
|
|
|
queue: RwLock::new(BTreeMap::new()),
|
2016-06-02 17:05:13 +02:00
|
|
|
sender: Mutex::new(send),
|
|
|
|
receiver: Mutex::new(Some(recv)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfirmationsQueue {
|
2016-08-03 10:36:54 +02:00
|
|
|
|
2016-06-02 17:05:13 +02:00
|
|
|
/// Blocks the thread and starts listening for notifications regarding all actions in the queue.
|
|
|
|
/// For each event, `listener` callback will be invoked.
|
|
|
|
/// This method can be used only once (only single consumer of events can exist).
|
|
|
|
pub fn start_listening<F>(&self, listener: F) -> Result<(), QueueError>
|
|
|
|
where F: Fn(QueueEvent) -> () {
|
2016-07-13 19:59:59 +02:00
|
|
|
let recv = self.receiver.lock().take();
|
2016-06-02 17:05:13 +02:00
|
|
|
if let None = recv {
|
|
|
|
return Err(QueueError::AlreadyUsed);
|
|
|
|
}
|
|
|
|
let recv = recv.expect("Check for none is done earlier.");
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let message = try!(recv.recv().map_err(|e| QueueError::ReceiverError(e)));
|
|
|
|
if let QueueEvent::Finish = message {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
listener(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Notifies consumer that the communcation is over.
|
|
|
|
/// No more events will be sent after this function is invoked.
|
|
|
|
pub fn finish(&self) {
|
|
|
|
self.notify(QueueEvent::Finish);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Notifies receiver about the event happening in this queue.
|
|
|
|
fn notify(&self, message: QueueEvent) {
|
|
|
|
// We don't really care about the result
|
2016-07-13 19:59:59 +02:00
|
|
|
let _ = self.sender.lock().send(message);
|
2016-06-02 17:05:13 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 10:36:54 +02:00
|
|
|
/// Removes requests from this queue and notifies `ConfirmationPromise` holders about the result.
|
2016-06-02 17:05:13 +02:00
|
|
|
/// Notifies also a receiver about that event.
|
2016-08-03 10:36:54 +02:00
|
|
|
fn remove(&self, id: U256, result: Option<RpcResult>) -> Option<ConfirmationRequest> {
|
2016-07-13 19:59:59 +02:00
|
|
|
let token = self.queue.write().remove(&id);
|
2016-06-02 17:05:13 +02:00
|
|
|
|
|
|
|
if let Some(token) = token {
|
|
|
|
// notify receiver about the event
|
2016-06-15 00:17:23 +02:00
|
|
|
self.notify(result.clone().map_or_else(
|
2016-06-02 17:05:13 +02:00
|
|
|
|| QueueEvent::RequestRejected(id),
|
|
|
|
|_| QueueEvent::RequestConfirmed(id)
|
|
|
|
));
|
|
|
|
// notify token holders about resolution
|
|
|
|
token.resolve(result);
|
|
|
|
// return a result
|
|
|
|
return Some(token.request.clone());
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-06-02 17:05:13 +02:00
|
|
|
None
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 17:05:13 +02:00
|
|
|
impl Drop for ConfirmationsQueue {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 09:19:58 +02:00
|
|
|
impl SigningQueue for ConfirmationsQueue {
|
2016-09-01 12:00:00 +02:00
|
|
|
fn add_request(&self, request: ConfirmationPayload) -> Result<ConfirmationPromise, QueueAddError> {
|
|
|
|
if self.len() > QUEUE_LIMIT {
|
|
|
|
return Err(QueueAddError::LimitReached);
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:37:34 +02:00
|
|
|
// Increment id
|
|
|
|
let id = {
|
2016-07-13 19:59:59 +02:00
|
|
|
let mut last_id = self.id.lock();
|
2016-06-01 19:37:34 +02:00
|
|
|
*last_id = *last_id + U256::from(1);
|
|
|
|
*last_id
|
|
|
|
};
|
2016-06-02 17:05:13 +02:00
|
|
|
// Add request to queue
|
|
|
|
let res = {
|
2016-08-03 10:36:54 +02:00
|
|
|
debug!(target: "own_tx", "Signer: New entry ({:?}) in confirmation queue.", id);
|
|
|
|
trace!(target: "own_tx", "Signer: ({:?}) : {:?}", id, request);
|
|
|
|
|
2016-07-13 19:59:59 +02:00
|
|
|
let mut queue = self.queue.write();
|
2016-06-02 17:05:13 +02:00
|
|
|
queue.insert(id, ConfirmationToken {
|
|
|
|
result: Arc::new(Mutex::new(ConfirmationResult::Waiting)),
|
2016-09-01 12:00:00 +02:00
|
|
|
listeners: Default::default(),
|
2016-08-03 10:36:54 +02:00
|
|
|
request: ConfirmationRequest {
|
2016-06-02 17:05:13 +02:00
|
|
|
id: id,
|
2016-08-03 10:36:54 +02:00
|
|
|
payload: request,
|
2016-06-02 17:05:13 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
queue.get(&id).map(|token| token.as_promise()).expect("Token was just inserted.")
|
|
|
|
};
|
|
|
|
// Notify listeners
|
|
|
|
self.notify(QueueEvent::NewRequest(id));
|
2016-09-01 12:00:00 +02:00
|
|
|
Ok(res)
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 10:36:54 +02:00
|
|
|
fn peek(&self, id: &U256) -> Option<ConfirmationRequest> {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.queue.read().get(id).map(|token| token.request.clone())
|
2016-06-02 17:05:13 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 10:36:54 +02:00
|
|
|
fn request_rejected(&self, id: U256) -> Option<ConfirmationRequest> {
|
|
|
|
debug!(target: "own_tx", "Signer: Request rejected ({:?}).", id);
|
2016-06-02 17:05:13 +02:00
|
|
|
self.remove(id, None)
|
|
|
|
}
|
|
|
|
|
2016-08-03 10:36:54 +02:00
|
|
|
fn request_confirmed(&self, id: U256, result: RpcResult) -> Option<ConfirmationRequest> {
|
2016-06-02 17:05:13 +02:00
|
|
|
debug!(target: "own_tx", "Signer: Transaction confirmed ({:?}).", id);
|
2016-06-15 00:17:23 +02:00
|
|
|
self.remove(id, Some(result))
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 10:36:54 +02:00
|
|
|
fn requests(&self) -> Vec<ConfirmationRequest> {
|
2016-07-13 19:59:59 +02:00
|
|
|
let queue = self.queue.read();
|
2016-06-02 17:05:13 +02:00
|
|
|
queue.values().map(|token| token.request.clone()).collect()
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-06-21 14:55:25 +02:00
|
|
|
|
|
|
|
fn len(&self) -> usize {
|
2016-07-13 19:59:59 +02:00
|
|
|
let queue = self.queue.read();
|
2016-06-21 14:55:25 +02:00
|
|
|
queue.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
2016-07-13 19:59:59 +02:00
|
|
|
let queue = self.queue.read();
|
2016-06-21 14:55:25 +02:00
|
|
|
queue.is_empty()
|
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2016-06-02 17:05:13 +02:00
|
|
|
use std::time::Duration;
|
|
|
|
use std::thread;
|
2016-09-01 12:00:00 +02:00
|
|
|
use std::sync::{mpsc, Arc};
|
2016-11-09 13:13:35 +01:00
|
|
|
use util::{Address, U256, Mutex};
|
2016-08-03 10:36:54 +02:00
|
|
|
use v1::helpers::{SigningQueue, ConfirmationsQueue, QueueEvent, FilledTransactionRequest, ConfirmationPayload};
|
2016-11-09 13:13:35 +01:00
|
|
|
use v1::types::ConfirmationResponse;
|
2016-06-01 19:37:34 +02:00
|
|
|
|
2016-08-03 10:36:54 +02:00
|
|
|
fn request() -> ConfirmationPayload {
|
2016-11-09 13:13:35 +01:00
|
|
|
ConfirmationPayload::SendTransaction(FilledTransactionRequest {
|
2016-06-01 19:37:34 +02:00
|
|
|
from: Address::from(1),
|
|
|
|
to: Some(Address::from(2)),
|
2016-08-03 10:36:54 +02:00
|
|
|
gas_price: 0.into(),
|
|
|
|
gas: 10_000.into(),
|
|
|
|
value: 10_000_000.into(),
|
|
|
|
data: vec![],
|
2016-06-01 19:37:34 +02:00
|
|
|
nonce: None,
|
2016-08-03 10:36:54 +02:00
|
|
|
})
|
2016-06-02 17:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_wait_for_hash() {
|
|
|
|
// given
|
|
|
|
let queue = Arc::new(ConfirmationsQueue::default());
|
|
|
|
let request = request();
|
|
|
|
|
|
|
|
// when
|
|
|
|
let q = queue.clone();
|
|
|
|
let handle = thread::spawn(move || {
|
2016-09-01 12:00:00 +02:00
|
|
|
let v = q.add_request(request).unwrap();
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
v.wait_for_result(move |res| {
|
|
|
|
tx.send(res).unwrap();
|
|
|
|
});
|
|
|
|
rx.recv().unwrap().expect("Should return hash")
|
2016-06-02 17:05:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let id = U256::from(1);
|
|
|
|
while queue.peek(&id).is_none() {
|
|
|
|
// Just wait for the other thread to start
|
|
|
|
thread::sleep(Duration::from_millis(100));
|
|
|
|
}
|
2016-11-09 13:13:35 +01:00
|
|
|
queue.request_confirmed(id, Ok(ConfirmationResponse::SendTransaction(1.into())));
|
2016-06-02 17:05:13 +02:00
|
|
|
|
|
|
|
// then
|
2016-11-09 13:13:35 +01:00
|
|
|
assert_eq!(handle.join().expect("Thread should finish nicely"), Ok(ConfirmationResponse::SendTransaction(1.into())));
|
2016-06-02 17:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_receive_notification() {
|
|
|
|
// given
|
|
|
|
let received = Arc::new(Mutex::new(None));
|
|
|
|
let queue = Arc::new(ConfirmationsQueue::default());
|
|
|
|
let request = request();
|
|
|
|
|
|
|
|
// when
|
|
|
|
let q = queue.clone();
|
|
|
|
let r = received.clone();
|
|
|
|
let handle = thread::spawn(move || {
|
|
|
|
q.start_listening(move |notification| {
|
2016-07-13 19:59:59 +02:00
|
|
|
let mut v = r.lock();
|
2016-06-02 17:05:13 +02:00
|
|
|
*v = Some(notification);
|
|
|
|
}).expect("Should be closed nicely.")
|
|
|
|
});
|
2016-09-01 12:00:00 +02:00
|
|
|
queue.add_request(request).unwrap();
|
2016-06-02 17:05:13 +02:00
|
|
|
queue.finish();
|
|
|
|
|
|
|
|
// then
|
|
|
|
handle.join().expect("Thread should finish nicely");
|
2016-07-13 19:59:59 +02:00
|
|
|
let r = received.lock().take();
|
2016-06-02 17:05:13 +02:00
|
|
|
assert_eq!(r, Some(QueueEvent::NewRequest(U256::from(1))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_transactions() {
|
|
|
|
// given
|
|
|
|
let queue = ConfirmationsQueue::default();
|
|
|
|
let request = request();
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
// when
|
2016-09-01 12:00:00 +02:00
|
|
|
queue.add_request(request.clone()).unwrap();
|
2016-06-01 19:37:34 +02:00
|
|
|
let all = queue.requests();
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert_eq!(all.len(), 1);
|
|
|
|
let el = all.get(0).unwrap();
|
|
|
|
assert_eq!(el.id, U256::from(1));
|
2016-08-03 10:36:54 +02:00
|
|
|
assert_eq!(el.payload, request);
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
}
|