Revalidate some transactions on every block import. (#4292)

* Revalidate some transactions on every block import.

* Fix endless loop in revalidate_ready.

* Clean up logging a bit.

* More clean ups.

* Print status after resubmitting.

* Remove env_logger.

* Remove redundant log.
This commit is contained in:
Tomasz Drwięga
2019-12-04 19:16:00 +01:00
committed by Gavin Wood
parent 925b23a3cd
commit 1628ba3388
6 changed files with 85 additions and 35 deletions
@@ -23,7 +23,7 @@ use futures::{
Future, FutureExt,
future::{Either, join, ready},
};
use log::{warn, debug};
use log::{warn, debug, trace};
use parking_lot::Mutex;
use client_api::{
@@ -74,6 +74,11 @@ where
id: &BlockId<Block>,
retracted: &[Block::Hash],
) -> Box<dyn Future<Output=()> + Send + Unpin> {
let now = std::time::Instant::now();
let took = move || format!("Took {} ms", now.elapsed().as_millis());
let id = *id;
trace!(target: "txpool", "[{:?}] Starting pool maintainance", id);
// Put transactions from retracted blocks back into the pool.
let client_copy = self.client.clone();
let retracted_transactions = retracted.to_vec().into_iter()
@@ -82,13 +87,14 @@ where
// if signed information is not present, attempt to resubmit anyway.
.filter(|tx| tx.is_signed().unwrap_or(true));
let resubmit_future = self.pool
.submit_at(id, retracted_transactions, true)
.then(|resubmit_result| ready(match resubmit_result {
Ok(_) => (),
Err(e) => {
debug!(target: "txpool", "Error re-submitting transactions: {:?}", e);
()
}
.submit_at(&id, retracted_transactions, true)
.then(move |resubmit_result| ready(match resubmit_result {
Ok(_) => trace!(target: "txpool",
"[{:?}] Re-submitting retracted done. {}", id, took()
),
Err(e) => debug!(target: "txpool",
"[{:?}] Error re-submitting transactions: {:?}", id, e
),
}));
// Avoid calling into runtime if there is nothing to prune from the pool anyway.
@@ -96,28 +102,42 @@ where
return Box::new(resubmit_future)
}
let block = (self.client.header(*id), self.client.block_body(id));
match block {
let block = (self.client.header(id), self.client.block_body(&id));
let prune_future = match block {
(Ok(Some(header)), Ok(Some(extrinsics))) => {
let parent_id = BlockId::hash(*header.parent_hash());
let prune_future = self.pool
.prune(id, &parent_id, &extrinsics)
.then(|prune_result| ready(match prune_result {
Ok(_) => (),
Err(e) => {
warn!("Error pruning transactions: {:?}", e);
()
}
.prune(&id, &parent_id, &extrinsics)
.then(move |prune_result| ready(match prune_result {
Ok(_) => trace!(target: "txpool",
"[{:?}] Pruning done. {}", id, took()
),
Err(e) => warn!(target: "txpool",
"[{:?}] Error pruning transactions: {:?}", id, e
),
}));
Box::new(resubmit_future.then(|_| prune_future))
Either::Left(resubmit_future.then(|_| prune_future))
},
(Ok(_), Ok(_)) => Box::new(resubmit_future),
(Ok(_), Ok(_)) => Either::Right(resubmit_future),
err => {
warn!("Error reading block: {:?}", err);
Box::new(resubmit_future)
warn!(target: "txpool", "[{:?}] Error reading block: {:?}", id, err);
Either::Right(resubmit_future)
},
}
};
let revalidate_future = self.pool
.revalidate_ready(&id, Some(16))
.then(move |result| ready(match result {
Ok(_) => debug!(target: "txpool",
"[{:?}] Revalidation done: {}", id, took()
),
Err(e) => warn!(target: "txpool",
"[{:?}] Encountered errors while revalidating transactions: {:?}", id, e
),
}));
Box::new(prune_future.then(|_| revalidate_future))
}
}
@@ -228,7 +248,7 @@ impl<Block, Client, PoolApi, F> LightBasicPoolMaintainer<Block, Client, PoolApi,
true => {
let revalidation_status = self.revalidation_status.clone();
Either::Left(self.pool
.revalidate_ready(id)
.revalidate_ready(id, None)
.map(|r| r.map_err(|e| warn!("Error revalidating known transactions: {}", e)))
.map(move |_| revalidation_status.lock().clear()))
},