Revalidation queue for transaction pool (#4781)

* Revalidation queeue.

* add docs and license

* move test

* refactor worker to async/await

* address review

* fix warnings

* update Cargo.lock

* move background task to service

* use tomusdrw loop

* naming

* return From::from

* add doc comment

* add more doc comments

* fix merge bug

* add doc comment for test function

* Update client/transaction-pool/src/testing/pool.rs

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* more review fixes

* refactor to allow service keep background tasks from isntantiated subsystems

* use const delay

* fix fallout

* remove fallout

* remove already moved test

* fix doc test

* add valid_at helper

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
This commit is contained in:
Nikolay Volf
2020-02-17 16:48:24 +03:00
committed by GitHub
parent 590142b928
commit 86ab0cb4d9
18 changed files with 554 additions and 195 deletions
+35 -8
View File
@@ -55,6 +55,8 @@ use sp_transaction_pool::{MaintainedTransactionPool, ChainEvent};
use sp_blockchain;
use grafana_data_source::{self, record_metrics};
pub type BackgroundTask = Pin<Box<dyn Future<Output=()> + Send>>;
/// Aggregator for the components required to build a service.
///
/// # Usage
@@ -90,6 +92,7 @@ pub struct ServiceBuilder<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TF
rpc_extensions: TRpc,
remote_backend: Option<Arc<dyn RemoteBlockchain<TBl>>>,
marker: PhantomData<(TBl, TRtApi)>,
background_tasks: Vec<(&'static str, BackgroundTask)>,
}
/// Full client type.
@@ -265,6 +268,7 @@ where TGen: RuntimeGenesis, TCSExt: Extension {
transaction_pool: Arc::new(()),
rpc_extensions: Default::default(),
remote_backend: None,
background_tasks: Default::default(),
marker: PhantomData,
})
}
@@ -350,6 +354,7 @@ where TGen: RuntimeGenesis, TCSExt: Extension {
transaction_pool: Arc::new(()),
rpc_extensions: Default::default(),
remote_backend: Some(remote_blockchain),
background_tasks: Default::default(),
marker: PhantomData,
})
}
@@ -398,6 +403,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP, TEx
transaction_pool: self.transaction_pool,
rpc_extensions: self.rpc_extensions,
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
})
}
@@ -440,6 +446,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP, TEx
transaction_pool: self.transaction_pool,
rpc_extensions: self.rpc_extensions,
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
})
}
@@ -466,6 +473,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP, TEx
transaction_pool: self.transaction_pool,
rpc_extensions: self.rpc_extensions,
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
})
}
@@ -506,6 +514,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP, TEx
transaction_pool: self.transaction_pool,
rpc_extensions: self.rpc_extensions,
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
})
}
@@ -570,6 +579,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP, TEx
transaction_pool: self.transaction_pool,
rpc_extensions: self.rpc_extensions,
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
})
}
@@ -596,21 +606,25 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP, TEx
/// Defines which transaction pool to use.
pub fn with_transaction_pool<UExPool>(
self,
mut self,
transaction_pool_builder: impl FnOnce(
sc_transaction_pool::txpool::Options,
Arc<TCl>,
Option<TFchr>,
) -> Result<UExPool, Error>
) -> Result<(UExPool, Option<BackgroundTask>), Error>
) -> Result<ServiceBuilder<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp,
TNetP, UExPool, TRpc, Backend>, Error>
where TSc: Clone, TFchr: Clone {
let transaction_pool = transaction_pool_builder(
let (transaction_pool, background_task) = transaction_pool_builder(
self.config.transaction_pool.clone(),
self.client.clone(),
self.fetcher.clone(),
)?;
if let Some(background_task) = background_task{
self.background_tasks.push(("txpool-background", background_task));
}
Ok(ServiceBuilder {
config: self.config,
client: self.client,
@@ -625,6 +639,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP, TEx
transaction_pool: Arc::new(transaction_pool),
rpc_extensions: self.rpc_extensions,
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
})
}
@@ -664,6 +679,7 @@ impl<TBl, TRtApi, TGen, TCSExt, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP, TEx
transaction_pool: self.transaction_pool,
rpc_extensions,
remote_backend: self.remote_backend,
background_tasks: self.background_tasks,
marker: self.marker,
})
}
@@ -778,6 +794,7 @@ ServiceBuilder<
transaction_pool,
rpc_extensions,
remote_backend,
background_tasks,
} = self;
sp_session::generate_initial_session_keys(
@@ -874,6 +891,15 @@ ServiceBuilder<
_ => None,
};
// Spawn background tasks which were stacked during the
// service building.
for (title, background_task) in background_tasks {
let _ = to_spawn_tx.unbounded_send((
background_task,
title.into(),
));
}
{
// block notifications
let txpool = Arc::downgrade(&transaction_pool);
@@ -930,9 +956,10 @@ ServiceBuilder<
ready(())
});
let _ = to_spawn_tx.unbounded_send((
Box::pin(select(events, exit.clone()).map(drop)),
From::from("txpool-and-offchain-notif")
From::from("txpool-and-offchain-notif"),
));
}
@@ -955,7 +982,7 @@ ServiceBuilder<
let _ = to_spawn_tx.unbounded_send((
Box::pin(select(events, exit.clone()).map(drop)),
From::from("telemetry-on-block")
From::from("telemetry-on-block"),
));
}
@@ -1028,7 +1055,7 @@ ServiceBuilder<
let _ = to_spawn_tx.unbounded_send((
Box::pin(select(tel_task, exit.clone()).map(drop)),
From::from("telemetry-periodic-send")
From::from("telemetry-periodic-send"),
));
// Periodically send the network state to the telemetry.
@@ -1044,7 +1071,7 @@ ServiceBuilder<
});
let _ = to_spawn_tx.unbounded_send((
Box::pin(select(tel_task_2, exit.clone()).map(drop)),
From::from("telemetry-periodic-network-state")
From::from("telemetry-periodic-network-state"),
));
// RPC
@@ -1130,7 +1157,7 @@ ServiceBuilder<
system_rpc_rx,
has_bootnodes,
), exit.clone()).map(drop)),
From::from("network-worker")
From::from("network-worker"),
));
let telemetry_connection_sinks: Arc<Mutex<Vec<futures::channel::mpsc::UnboundedSender<()>>>> = Default::default();
+1 -1
View File
@@ -716,7 +716,7 @@ mod tests {
let pool = Arc::new(BasicPool::new(
Default::default(),
Arc::new(FullChainApi::new(client.clone())),
));
).0);
let best = longest_chain.best_chain().unwrap();
let transaction = Transfer {
amount: 5,