2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2017-03-23 22:20:00 +01:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
//! Service for culling the light client's transaction queue.
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2018-03-13 11:49:57 +01:00
|
|
|
use ethcore::client::ClientIoMessage;
|
2018-04-10 12:13:49 +02:00
|
|
|
use sync::LightSync;
|
2017-03-23 22:20:00 +01:00
|
|
|
use io::{IoContext, IoHandler, TimerToken};
|
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
use light::client::LightChainClient;
|
2017-03-23 22:20:00 +01:00
|
|
|
use light::on_demand::{request, OnDemand};
|
|
|
|
use light::TransactionQueue;
|
|
|
|
|
2017-05-23 12:39:25 +02:00
|
|
|
use futures::{future, Future};
|
2017-03-23 22:20:00 +01:00
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
use parity_runtime::Executor;
|
2017-03-23 22:20:00 +01:00
|
|
|
|
2017-09-02 20:09:13 +02:00
|
|
|
use parking_lot::RwLock;
|
2017-03-23 22:20:00 +01:00
|
|
|
|
|
|
|
// Attepmt to cull once every 10 minutes.
|
|
|
|
const TOKEN: TimerToken = 1;
|
2018-04-14 21:35:58 +02:00
|
|
|
const TIMEOUT: Duration = Duration::from_secs(60 * 10);
|
2017-03-23 22:20:00 +01:00
|
|
|
|
|
|
|
// But make each attempt last only 9 minutes
|
2018-04-14 21:35:58 +02:00
|
|
|
const PURGE_TIMEOUT: Duration = Duration::from_secs(60 * 9);
|
2017-03-23 22:20:00 +01:00
|
|
|
|
|
|
|
/// Periodically culls the transaction queue of mined transactions.
|
2017-09-05 17:54:05 +02:00
|
|
|
pub struct QueueCull<T> {
|
2017-03-23 22:20:00 +01:00
|
|
|
/// A handle to the client, for getting the latest block header.
|
2017-09-05 17:54:05 +02:00
|
|
|
pub client: Arc<T>,
|
2017-03-23 22:20:00 +01:00
|
|
|
/// A handle to the sync service.
|
|
|
|
pub sync: Arc<LightSync>,
|
|
|
|
/// The on-demand request service.
|
|
|
|
pub on_demand: Arc<OnDemand>,
|
|
|
|
/// The transaction queue.
|
|
|
|
pub txq: Arc<RwLock<TransactionQueue>>,
|
2018-10-22 09:40:50 +02:00
|
|
|
/// Event loop executor.
|
|
|
|
pub executor: Executor,
|
2017-03-23 22:20:00 +01:00
|
|
|
}
|
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
impl<T: LightChainClient + 'static> IoHandler<ClientIoMessage> for QueueCull<T> {
|
2017-03-23 22:20:00 +01:00
|
|
|
fn initialize(&self, io: &IoContext<ClientIoMessage>) {
|
2018-04-14 21:35:58 +02:00
|
|
|
io.register_timer(TOKEN, TIMEOUT).expect("Error registering timer");
|
2017-03-23 22:20:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn timeout(&self, _io: &IoContext<ClientIoMessage>, timer: TimerToken) {
|
|
|
|
if timer != TOKEN { return }
|
|
|
|
|
|
|
|
let senders = self.txq.read().queued_senders();
|
|
|
|
if senders.is_empty() { return }
|
|
|
|
|
|
|
|
let (sync, on_demand, txq) = (self.sync.clone(), self.on_demand.clone(), self.txq.clone());
|
|
|
|
let best_header = self.client.best_block_header();
|
2017-06-28 09:10:57 +02:00
|
|
|
let start_nonce = self.client.engine().account_start_nonce(best_header.number());
|
2017-03-23 22:20:00 +01:00
|
|
|
|
|
|
|
info!(target: "cull", "Attempting to cull queued transactions from {} senders.", senders.len());
|
2018-10-22 09:40:50 +02:00
|
|
|
self.executor.spawn_with_timeout(move || {
|
2017-03-23 22:20:00 +01:00
|
|
|
let maybe_fetching = sync.with_context(move |ctx| {
|
|
|
|
// fetch the nonce of each sender in the queue.
|
2017-05-23 12:39:25 +02:00
|
|
|
let nonce_reqs = senders.iter()
|
|
|
|
.map(|&address| request::Account { header: best_header.clone().into(), address: address })
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// when they come in, update each sender to the new nonce.
|
|
|
|
on_demand.request(ctx, nonce_reqs)
|
|
|
|
.expect("No back-references; therefore all back-references are valid; qed")
|
|
|
|
.map(move |accs| {
|
|
|
|
let txq = txq.write();
|
|
|
|
let _ = accs.into_iter()
|
|
|
|
.map(|maybe_acc| maybe_acc.map_or(start_nonce, |acc| acc.nonce))
|
|
|
|
.zip(senders)
|
|
|
|
.fold(txq, |mut txq, (nonce, addr)| {
|
|
|
|
txq.cull(addr, nonce);
|
|
|
|
txq
|
|
|
|
});
|
2017-04-06 17:44:31 +02:00
|
|
|
})
|
2017-03-23 22:20:00 +01:00
|
|
|
.map_err(|_| debug!(target: "cull", "OnDemand prematurely closed channel."))
|
|
|
|
});
|
|
|
|
|
|
|
|
match maybe_fetching {
|
2017-10-05 12:35:01 +02:00
|
|
|
Some(fut) => future::Either::A(fut),
|
2017-05-23 12:39:25 +02:00
|
|
|
None => {
|
|
|
|
debug!(target: "cull", "Unable to acquire network context; qed");
|
2017-10-05 12:35:01 +02:00
|
|
|
future::Either::B(future::ok(()))
|
|
|
|
},
|
2017-03-23 22:20:00 +01:00
|
|
|
}
|
2018-04-02 10:47:56 +02:00
|
|
|
}, PURGE_TIMEOUT, || {})
|
2017-03-23 22:20:00 +01:00
|
|
|
}
|
|
|
|
}
|