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())
}

View File

@@ -9,14 +9,12 @@ use sp_blockchain::HeaderBackend;
use sp_api::{TransactionFor, ProvideRuntimeApi};
use sp_consensus::{Error, Environment};
use sc_consensus::{BlockImport, JustificationImport, BasicQueue};
use sc_consensus::{BlockImport, JustificationImport};
use sc_client_api::{Backend, Finalizer};
use crate::tendermint::TendermintImport;
pub type TendermintImportQueue<Block, Transaction> = BasicQueue<Block, Transaction>;
#[async_trait]
impl<
B: Block,

View File

@@ -1,7 +1,6 @@
use std::sync::Arc;
use std::{sync::Arc, future::Future};
use sp_api::TransactionFor;
use sp_consensus::Error;
use sc_executor::{NativeVersion, NativeExecutionDispatch, NativeElseWasmExecutor};
use sc_transaction_pool::FullPool;
@@ -50,8 +49,8 @@ pub fn import_queue(
client: Arc<FullClient>,
pool: Arc<FullPool<Block, FullClient>>,
registry: Option<&Registry>,
) -> Result<TendermintImportQueue<Block, TransactionFor<FullClient, Block>>, Error> {
Ok(import_queue::import_queue(
) -> (impl Future<Output = ()>, TendermintImportQueue<Block, TransactionFor<FullClient, Block>>) {
import_queue::import_queue(
client.clone(),
client.clone(),
Arc::new(|_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) }),
@@ -64,18 +63,7 @@ pub fn import_queue(
),
&task_manager.spawn_essential_handle(),
registry,
))
}
// If we're an authority, produce blocks
pub fn authority(
task_manager: &TaskManager,
client: Arc<FullClient>,
network: Arc<sc_network::NetworkService<Block, <Block as sp_runtime::traits::Block>::Hash>>,
pool: Arc<FullPool<Block, FullClient>>,
registry: Option<&Registry>,
) {
todo!()
)
}
/*

View File

@@ -28,7 +28,7 @@ use sc_client_api::{Backend, Finalizer};
use tendermint_machine::{
ext::{BlockError, Commit, Network},
SignedMessage,
SignedMessage, TendermintHandle,
};
use crate::{
@@ -45,13 +45,16 @@ pub(crate) struct TendermintImport<
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static,
> {
> where
TransactionFor<C, B>: Send + Sync + 'static,
{
_block: PhantomData<B>,
_backend: PhantomData<Be>,
importing_block: Arc<RwLock<Option<B::Hash>>>,
pub(crate) machine: Arc<RwLock<Option<TendermintHandle<Self>>>>,
client: Arc<C>,
pub(crate) client: Arc<C>,
pub(crate) inner: Arc<AsyncRwLock<I>>,
providers: Arc<CIDP>,
@@ -67,6 +70,8 @@ impl<
CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static,
> Clone for TendermintImport<B, Be, C, I, CIDP, E>
where
TransactionFor<C, B>: Send + Sync + 'static,
{
fn clone(&self) -> Self {
TendermintImport {
@@ -74,6 +79,7 @@ impl<
_backend: PhantomData,
importing_block: self.importing_block.clone(),
machine: self.machine.clone(),
client: self.client.clone(),
inner: self.inner.clone(),
@@ -107,6 +113,7 @@ where
_backend: PhantomData,
importing_block: Arc::new(RwLock::new(None)),
machine: Arc::new(RwLock::new(None)),
client,
inner: Arc::new(AsyncRwLock::new(inner)),
@@ -233,28 +240,28 @@ where
Ok(())
}
async fn get_proposal(&mut self, block: &B) -> B {
let inherent_data = match self.providers.create_inherent_data_providers(block.hash(), ()).await
{
Ok(providers) => match providers.create_inherent_data() {
Ok(data) => Some(data),
pub(crate) async fn get_proposal(&mut self, header: &B::Header) -> B {
let inherent_data =
match self.providers.create_inherent_data_providers(header.hash(), ()).await {
Ok(providers) => match providers.create_inherent_data() {
Ok(data) => Some(data),
Err(err) => {
warn!(target: "tendermint", "Failed to create inherent data: {}", err);
None
}
},
Err(err) => {
warn!(target: "tendermint", "Failed to create inherent data: {}", err);
warn!(target: "tendermint", "Failed to create inherent data providers: {}", err);
None
}
},
Err(err) => {
warn!(target: "tendermint", "Failed to create inherent data providers: {}", err);
None
}
}
.unwrap_or_else(InherentData::new);
.unwrap_or_else(InherentData::new);
let proposer = self
.env
.write()
.await
.init(block.header())
.init(header)
.await
.expect("Failed to create a proposer for the new block");
// TODO: Production time, size limit
@@ -355,6 +362,6 @@ where
async fn add_block(&mut self, block: B, commit: Commit<TendermintSigner>) -> B {
self.import_justification_actual(block.hash(), (CONSENSUS_ID, commit.encode())).unwrap();
self.get_proposal(&block).await
self.get_proposal(block.header()).await
}
}