Provide a way to create the machine

The BasicQueue returned obscures the TendermintImport struct. 
Accordingly, a Future scoped with access is returned upwards, which when 
awaited will create the machine. This makes creating the machine 
optional while maintaining scope boundaries.

Is sufficient to create a 1-node net which produces and finalizes 
blocks.
This commit is contained in:
Luke Parker
2022-10-22 03:41:49 -04:00
parent 39984bd07b
commit 9b0dca06d0
8 changed files with 104 additions and 89 deletions

View File

@@ -1,16 +1,15 @@
use std::{
pin::Pin,
sync::{Arc, RwLock},
task::{Poll, Context},
task::{Poll, /* Wake, Waker, */ Context},
future::Future,
time::SystemTime,
};
use tokio::runtime::Handle;
use sp_inherents::CreateInherentDataProviders;
use sp_runtime::traits::{Header, Block};
use sp_blockchain::HeaderBackend;
use sp_api::{TransactionFor, ProvideRuntimeApi};
use sp_api::{BlockId, TransactionFor, ProvideRuntimeApi};
use sp_consensus::{Error, Environment};
use sc_consensus::{BlockImport, BlockImportStatus, BlockImportError, Link, BasicQueue};
@@ -20,6 +19,8 @@ use sc_client_api::{Backend, Finalizer};
use substrate_prometheus_endpoint::Registry;
use tendermint_machine::{ext::BlockNumber, TendermintMachine};
use crate::tendermint::TendermintImport;
pub type TendermintImportQueue<Block, Transaction> = BasicQueue<Block, Transaction>;
@@ -84,16 +85,33 @@ pub fn import_queue<
env: E,
spawner: &impl sp_core::traits::SpawnEssentialNamed,
registry: Option<&Registry>,
) -> TendermintImportQueue<B, TransactionFor<C, B>>
) -> (impl Future<Output = ()>, TendermintImportQueue<B, TransactionFor<C, B>>)
where
I::Error: Into<Error>,
TransactionFor<C, B>: Send + Sync + 'static,
{
let import = TendermintImport::new(client, inner, providers, env);
let authority = {
let machine_clone = import.machine.clone();
let mut import_clone = import.clone();
async move {
*machine_clone.write().unwrap() = Some(TendermintMachine::new(
import_clone.clone(),
// TODO
0,
(BlockNumber(1), SystemTime::now()),
import_clone
.get_proposal(&import_clone.client.header(BlockId::Number(0u8.into())).unwrap().unwrap())
.await,
));
}
};
let boxed = Box::new(import.clone());
let queue =
|| BasicQueue::new(import.clone(), boxed.clone(), Some(boxed.clone()), spawner, registry);
*Handle::current().block_on(import.queue.write()) = Some(queue());
queue()
*futures::executor::block_on(import.queue.write()) = Some(queue());
(authority, queue())
}