Update consensus/lib.rs from PoW to Tendermint

Not possible to be used as the previous consensus could. It will not
produce blocks nor does it currenly even instantiate a machine. This is
just he next step.
This commit is contained in:
Luke Parker 2022-10-21 02:30:38 -04:00
parent 3b08633445
commit c0432e122c
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6
2 changed files with 33 additions and 58 deletions

View file

@ -1,27 +0,0 @@
use sp_core::U256;
use sc_consensus_pow::{Error, PowAlgorithm};
use sp_consensus_pow::Seal;
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
#[derive(Clone)]
pub struct AcceptAny;
impl<B: BlockT> PowAlgorithm<B> for AcceptAny {
type Difficulty = U256;
fn difficulty(&self, _: B::Hash) -> Result<Self::Difficulty, Error<B>> {
Ok(U256::one())
}
fn verify(
&self,
_: &BlockId<B>,
_: &B::Hash,
_: Option<&[u8]>,
_: &Seal,
_: Self::Difficulty,
) -> Result<bool, Error<B>> {
Ok(true)
}
}

View file

@ -1,21 +1,23 @@
use std::{marker::Sync, sync::Arc, time::Duration}; use std::sync::Arc;
use sp_api::TransactionFor;
use sp_consensus::Error;
use sc_executor::{NativeVersion, NativeExecutionDispatch, NativeElseWasmExecutor};
use sc_service::{TaskManager, TFullClient};
use substrate_prometheus_endpoint::Registry; use substrate_prometheus_endpoint::Registry;
use sc_consensus_pow as sc_pow;
use sc_executor::NativeElseWasmExecutor;
use sc_service::TaskManager;
use serai_runtime::{self, opaque::Block, RuntimeApi}; use serai_runtime::{self, opaque::Block, RuntimeApi};
mod algorithm;
mod signature_scheme; mod signature_scheme;
mod import; mod weights;
//mod tendermint;
mod import_queue;
use import_queue::TendermintImportQueue;
pub struct ExecutorDispatch; pub struct ExecutorDispatch;
impl sc_executor::NativeExecutionDispatch for ExecutorDispatch { impl NativeExecutionDispatch for ExecutorDispatch {
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions; type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;
#[cfg(not(feature = "runtime-benchmarks"))] #[cfg(not(feature = "runtime-benchmarks"))]
@ -25,41 +27,40 @@ impl sc_executor::NativeExecutionDispatch for ExecutorDispatch {
serai_runtime::api::dispatch(method, data) serai_runtime::api::dispatch(method, data)
} }
fn native_version() -> sc_executor::NativeVersion { fn native_version() -> NativeVersion {
serai_runtime::native_version() serai_runtime::native_version()
} }
} }
pub type FullClient = pub type FullClient = TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
type Db = sp_trie::PrefixedMemoryDB<sp_runtime::traits::BlakeTwo256>; pub fn import_queue(
pub fn import_queue<S: sp_consensus::SelectChain<Block> + 'static>(
task_manager: &TaskManager, task_manager: &TaskManager,
client: Arc<FullClient>, client: Arc<FullClient>,
select_chain: S,
registry: Option<&Registry>, registry: Option<&Registry>,
) -> Result<sc_pow::PowImportQueue<Block, Db>, sp_consensus::Error> { ) -> Result<TendermintImportQueue<Block, TransactionFor<FullClient, Block>>, Error> {
let pow_block_import = Box::new(sc_pow::PowBlockImport::new( Ok(import_queue::import_queue(
client.clone(), client.clone(),
client, client,
algorithm::AcceptAny, Arc::new(|_, _| async { Ok(()) }),
0,
select_chain,
|_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) },
));
sc_pow::import_queue(
pow_block_import,
None,
algorithm::AcceptAny,
&task_manager.spawn_essential_handle(), &task_manager.spawn_essential_handle(),
registry, registry,
) ))
} }
// Produce a block every 5 seconds // 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<sc_transaction_pool::FullPool<Block, FullClient>>,
registry: Option<&Registry>,
) {
todo!()
}
/*
// Produce a block every 6 seconds
async fn produce< async fn produce<
Block: sp_api::BlockT<Hash = sp_core::H256>, Block: sp_api::BlockT<Hash = sp_core::H256>,
Algorithm: sc_pow::PowAlgorithm<Block, Difficulty = sp_core::U256> + Send + Sync + 'static, Algorithm: sc_pow::PowAlgorithm<Block, Difficulty = sp_core::U256> + Send + Sync + 'static,
@ -126,3 +127,4 @@ pub fn authority<S: sp_consensus::SelectChain<Block> + 'static>(
task_manager.spawn_essential_handle().spawn("producer", None, produce(worker)); task_manager.spawn_essential_handle().spawn("producer", None, produce(worker));
} }
*/