make SelectChain async (#9128)

* make SelectChain async

* make JustificationImport async
This commit is contained in:
André Silva
2021-06-20 12:01:09 +01:00
committed by GitHub
parent 650fc2f9c9
commit a26ec52450
18 changed files with 800 additions and 454 deletions
@@ -363,15 +363,16 @@ impl<B: BlockT, T, E: std::error::Error + Send + 'static, Transaction> BlockImpo
}
/// Justification import trait
#[async_trait::async_trait]
pub trait JustificationImport<B: BlockT> {
type Error: std::error::Error + Send + 'static;
/// Called by the import queue when it is started. Returns a list of justifications to request
/// from the network.
fn on_start(&mut self) -> Vec<(B::Hash, NumberFor<B>)> { Vec::new() }
async fn on_start(&mut self) -> Vec<(B::Hash, NumberFor<B>)>;
/// Import a Block justification and finalize the given block.
fn import_justification(
async fn import_justification(
&mut self,
hash: B::Hash,
number: NumberFor<B>,
@@ -220,16 +220,16 @@ impl<B: BlockT> BlockImportWorker<B> {
metrics,
};
// Let's initialize `justification_import`
if let Some(justification_import) = worker.justification_import.as_mut() {
for (hash, number) in justification_import.on_start() {
worker.result_sender.request_justification(&hash, number);
}
}
let delay_between_blocks = Duration::default();
let future = async move {
// Let's initialize `justification_import`
if let Some(justification_import) = worker.justification_import.as_mut() {
for (hash, number) in justification_import.on_start().await {
worker.result_sender.request_justification(&hash, number);
}
}
let block_import_process = block_import_process(
block_import,
verifier,
@@ -254,15 +254,18 @@ impl<B: BlockT> BlockImportWorker<B> {
// Make sure to first process all justifications
while let Poll::Ready(justification) = futures::poll!(justification_port.next()) {
match justification {
Some(ImportJustification(who, hash, number, justification)) =>
worker.import_justification(who, hash, number, justification),
Some(ImportJustification(who, hash, number, justification)) => {
worker
.import_justification(who, hash, number, justification)
.await
}
None => {
log::debug!(
target: "block-import",
"Stopping block import because justification channel was closed!",
);
return
},
return;
}
}
}
@@ -278,7 +281,7 @@ impl<B: BlockT> BlockImportWorker<B> {
(future, justification_sender, block_import_sender)
}
fn import_justification(
async fn import_justification(
&mut self,
who: Origin,
hash: B::Hash,
@@ -286,8 +289,11 @@ impl<B: BlockT> BlockImportWorker<B> {
justification: Justification,
) {
let started = wasm_timer::Instant::now();
let success = self.justification_import.as_mut().map(|justification_import| {
justification_import.import_justification(hash, number, justification)
let success = match self.justification_import.as_mut() {
Some(justification_import) => justification_import
.import_justification(hash, number, justification)
.await
.map_err(|e| {
debug!(
target: "sync",
@@ -298,14 +304,19 @@ impl<B: BlockT> BlockImportWorker<B> {
who,
);
e
}).is_ok()
}).unwrap_or(false);
})
.is_ok(),
None => false,
};
if let Some(metrics) = self.metrics.as_ref() {
metrics.justification_import_time.observe(started.elapsed().as_secs_f64());
metrics
.justification_import_time
.observe(started.elapsed().as_secs_f64());
}
self.result_sender.justification_imported(who, &hash, number, success);
self.result_sender
.justification_imported(who, &hash, number, success);
}
}
@@ -472,10 +483,15 @@ mod tests {
}
}
#[async_trait::async_trait]
impl JustificationImport<Block> for () {
type Error = crate::Error;
fn import_justification(
async fn on_start(&mut self) -> Vec<(Hash, BlockNumber)> {
Vec::new()
}
async fn import_justification(
&mut self,
_hash: Hash,
_number: BlockNumber,
@@ -33,23 +33,24 @@ use sp_runtime::traits::{Block as BlockT, NumberFor};
/// some implementations.
///
/// Non-deterministically finalizing chains may only use the `_authoring` functions.
#[async_trait::async_trait]
pub trait SelectChain<Block: BlockT>: Sync + Send + Clone {
/// Get all leaves of the chain: block hashes that have no children currently.
/// Get all leaves of the chain, i.e. block hashes that have no children currently.
/// Leaves that can never be finalized will not be returned.
fn leaves(&self) -> Result<Vec<<Block as BlockT>::Hash>, Error>;
async fn leaves(&self) -> Result<Vec<<Block as BlockT>::Hash>, Error>;
/// Among those `leaves` deterministically pick one chain as the generally
/// best chain to author new blocks upon and probably finalize.
fn best_chain(&self) -> Result<<Block as BlockT>::Header, Error>;
/// best chain to author new blocks upon and probably (but not necessarily)
/// finalize.
async fn best_chain(&self) -> Result<<Block as BlockT>::Header, Error>;
/// Get the best descendent of `target_hash` that we should attempt to
/// finalize next, if any. It is valid to return the given `target_hash`
/// itself if no better descendent exists.
fn finality_target(
async fn finality_target(
&self,
target_hash: <Block as BlockT>::Hash,
_maybe_max_number: Option<NumberFor<Block>>
_maybe_max_number: Option<NumberFor<Block>>,
) -> Result<Option<<Block as BlockT>::Hash>, Error> {
Ok(Some(target_hash))
}