2022-10-21 02:30:38 -04:00
|
|
|
use std::sync::Arc;
|
2022-07-15 00:05:00 -04:00
|
|
|
|
2022-10-21 02:30:38 -04:00
|
|
|
use sp_api::TransactionFor;
|
|
|
|
|
use sp_consensus::Error;
|
2022-07-15 00:05:00 -04:00
|
|
|
|
2022-10-21 02:30:38 -04:00
|
|
|
use sc_executor::{NativeVersion, NativeExecutionDispatch, NativeElseWasmExecutor};
|
2022-10-21 23:36:24 -04:00
|
|
|
use sc_transaction_pool::FullPool;
|
2022-10-21 02:30:38 -04:00
|
|
|
use sc_service::{TaskManager, TFullClient};
|
2022-07-15 00:05:00 -04:00
|
|
|
|
2022-10-21 02:30:38 -04:00
|
|
|
use substrate_prometheus_endpoint::Registry;
|
2022-07-15 00:05:00 -04:00
|
|
|
|
2022-10-21 02:30:38 -04:00
|
|
|
use serai_runtime::{self, opaque::Block, RuntimeApi};
|
2022-10-20 03:50:06 -04:00
|
|
|
|
|
|
|
|
mod signature_scheme;
|
2022-10-21 02:30:38 -04:00
|
|
|
mod weights;
|
|
|
|
|
|
|
|
|
|
mod import_queue;
|
|
|
|
|
use import_queue::TendermintImportQueue;
|
2022-07-15 00:05:00 -04:00
|
|
|
|
2022-10-21 05:28:26 -04:00
|
|
|
mod select_chain;
|
|
|
|
|
pub use select_chain::TendermintSelectChain;
|
|
|
|
|
|
2022-07-15 00:05:00 -04:00
|
|
|
pub struct ExecutorDispatch;
|
2022-10-21 02:30:38 -04:00
|
|
|
impl NativeExecutionDispatch for ExecutorDispatch {
|
2022-07-15 00:05:00 -04:00
|
|
|
#[cfg(feature = "runtime-benchmarks")]
|
|
|
|
|
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;
|
|
|
|
|
#[cfg(not(feature = "runtime-benchmarks"))]
|
|
|
|
|
type ExtendHostFunctions = ();
|
|
|
|
|
|
|
|
|
|
fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
|
|
|
|
|
serai_runtime::api::dispatch(method, data)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 02:30:38 -04:00
|
|
|
fn native_version() -> NativeVersion {
|
2022-07-15 00:05:00 -04:00
|
|
|
serai_runtime::native_version()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 02:30:38 -04:00
|
|
|
pub type FullClient = TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
|
2022-07-15 00:05:00 -04:00
|
|
|
|
2022-10-21 02:30:38 -04:00
|
|
|
pub fn import_queue(
|
2022-07-15 00:05:00 -04:00
|
|
|
task_manager: &TaskManager,
|
|
|
|
|
client: Arc<FullClient>,
|
2022-10-21 23:36:24 -04:00
|
|
|
pool: Arc<FullPool<Block, FullClient>>,
|
2022-07-15 01:26:07 -04:00
|
|
|
registry: Option<&Registry>,
|
2022-10-21 02:30:38 -04:00
|
|
|
) -> Result<TendermintImportQueue<Block, TransactionFor<FullClient, Block>>, Error> {
|
|
|
|
|
Ok(import_queue::import_queue(
|
2022-07-15 01:26:07 -04:00
|
|
|
client.clone(),
|
2022-10-21 23:36:24 -04:00
|
|
|
client.clone(),
|
2022-10-21 03:17:02 -04:00
|
|
|
Arc::new(|_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) }),
|
2022-10-21 23:36:24 -04:00
|
|
|
sc_basic_authorship::ProposerFactory::new(
|
|
|
|
|
task_manager.spawn_handle(),
|
|
|
|
|
client,
|
|
|
|
|
pool,
|
|
|
|
|
registry,
|
|
|
|
|
None,
|
|
|
|
|
),
|
2022-07-15 00:05:00 -04:00
|
|
|
&task_manager.spawn_essential_handle(),
|
2022-07-15 01:26:07 -04:00
|
|
|
registry,
|
2022-10-21 02:30:38 -04:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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>>,
|
2022-10-21 23:36:24 -04:00
|
|
|
pool: Arc<FullPool<Block, FullClient>>,
|
2022-10-21 02:30:38 -04:00
|
|
|
registry: Option<&Registry>,
|
|
|
|
|
) {
|
|
|
|
|
todo!()
|
2022-07-15 00:05:00 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-21 02:30:38 -04:00
|
|
|
/*
|
|
|
|
|
// Produce a block every 6 seconds
|
2022-07-15 00:05:00 -04:00
|
|
|
async fn produce<
|
|
|
|
|
Block: sp_api::BlockT<Hash = sp_core::H256>,
|
2022-07-15 01:26:07 -04:00
|
|
|
Algorithm: sc_pow::PowAlgorithm<Block, Difficulty = sp_core::U256> + Send + Sync + 'static,
|
2022-07-15 00:05:00 -04:00
|
|
|
C: sp_api::ProvideRuntimeApi<Block> + 'static,
|
|
|
|
|
Link: sc_consensus::JustificationSyncLink<Block> + 'static,
|
2022-07-15 01:26:07 -04:00
|
|
|
P: Send + 'static,
|
|
|
|
|
>(
|
|
|
|
|
worker: sc_pow::MiningHandle<Block, Algorithm, C, Link, P>,
|
|
|
|
|
) where
|
|
|
|
|
sp_api::TransactionFor<C, Block>: Send + 'static,
|
|
|
|
|
{
|
2022-07-15 00:05:00 -04:00
|
|
|
loop {
|
|
|
|
|
let worker_clone = worker.clone();
|
|
|
|
|
std::thread::spawn(move || {
|
2022-07-15 01:26:07 -04:00
|
|
|
tokio::runtime::Runtime::new().unwrap().handle().block_on(async {
|
|
|
|
|
worker_clone.submit(vec![]).await;
|
|
|
|
|
});
|
2022-07-15 00:05:00 -04:00
|
|
|
});
|
2022-07-16 21:06:54 -04:00
|
|
|
tokio::time::sleep(Duration::from_secs(6)).await;
|
2022-07-15 00:05:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we're an authority, produce blocks
|
|
|
|
|
pub fn authority<S: sp_consensus::SelectChain<Block> + 'static>(
|
|
|
|
|
task_manager: &TaskManager,
|
|
|
|
|
client: Arc<FullClient>,
|
|
|
|
|
network: Arc<sc_network::NetworkService<Block, <Block as sp_runtime::traits::Block>::Hash>>,
|
|
|
|
|
pool: Arc<sc_transaction_pool::FullPool<Block, FullClient>>,
|
|
|
|
|
select_chain: S,
|
2022-07-15 01:26:07 -04:00
|
|
|
registry: Option<&Registry>,
|
2022-07-15 00:05:00 -04:00
|
|
|
) {
|
|
|
|
|
let proposer = sc_basic_authorship::ProposerFactory::new(
|
|
|
|
|
task_manager.spawn_handle(),
|
|
|
|
|
client.clone(),
|
|
|
|
|
pool,
|
|
|
|
|
registry,
|
2022-07-15 01:26:07 -04:00
|
|
|
None,
|
2022-07-15 00:05:00 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let pow_block_import = Box::new(sc_pow::PowBlockImport::new(
|
|
|
|
|
client.clone(),
|
|
|
|
|
client.clone(),
|
|
|
|
|
algorithm::AcceptAny,
|
|
|
|
|
0, // Block to start checking inherents at
|
|
|
|
|
select_chain.clone(),
|
2022-07-15 01:26:07 -04:00
|
|
|
move |_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) },
|
2022-07-15 00:05:00 -04:00
|
|
|
));
|
|
|
|
|
|
|
|
|
|
let (worker, worker_task) = sc_pow::start_mining_worker(
|
|
|
|
|
pow_block_import,
|
|
|
|
|
client,
|
|
|
|
|
select_chain,
|
|
|
|
|
algorithm::AcceptAny,
|
|
|
|
|
proposer,
|
|
|
|
|
network.clone(),
|
2022-07-22 02:34:36 -04:00
|
|
|
network,
|
2022-07-15 00:05:00 -04:00
|
|
|
None,
|
2022-07-15 01:26:07 -04:00
|
|
|
move |_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) },
|
2022-07-16 21:06:54 -04:00
|
|
|
Duration::from_secs(6),
|
2022-07-15 00:05:00 -04:00
|
|
|
Duration::from_secs(2),
|
|
|
|
|
);
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
task_manager.spawn_essential_handle().spawn_blocking("pow", None, worker_task);
|
2022-07-15 00:05:00 -04:00
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
task_manager.spawn_essential_handle().spawn("producer", None, produce(worker));
|
2022-07-15 00:05:00 -04:00
|
|
|
}
|
2022-10-21 02:30:38 -04:00
|
|
|
*/
|