diff --git a/Cargo.lock b/Cargo.lock index 346eb5d1..dea47253 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7450,6 +7450,7 @@ dependencies = [ "sc-consensus", "sc-executor", "sc-keystore", + "sc-network", "sc-rpc", "sc-rpc-api", "sc-service", diff --git a/substrate/consensus/src/block_import.rs b/substrate/consensus/src/block_import.rs index 9672f2ce..0ae477d7 100644 --- a/substrate/consensus/src/block_import.rs +++ b/substrate/consensus/src/block_import.rs @@ -12,7 +12,7 @@ use sc_consensus::{BlockCheckParams, BlockImportParams, ImportResult, BlockImpor use sc_client_api::{Backend, Finalizer}; -use crate::tendermint::TendermintImport; +use crate::{tendermint::TendermintImport, Announce}; #[async_trait] impl< @@ -22,7 +22,8 @@ impl< I: Send + Sync + BlockImport> + 'static, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, - > BlockImport for TendermintImport + A: Announce, + > BlockImport for TendermintImport where I::Error: Into, TransactionFor: Send + Sync + 'static, diff --git a/substrate/consensus/src/import_queue.rs b/substrate/consensus/src/import_queue.rs index fe17b7e3..20139c7d 100644 --- a/substrate/consensus/src/import_queue.rs +++ b/substrate/consensus/src/import_queue.rs @@ -21,7 +21,7 @@ use substrate_prometheus_endpoint::Registry; use tendermint_machine::{ext::BlockNumber, TendermintMachine}; -use crate::tendermint::TendermintImport; +use crate::{tendermint::TendermintImport, Announce}; pub type TendermintImportQueue = BasicQueue; @@ -78,9 +78,11 @@ pub fn import_queue< I: Send + Sync + BlockImport> + 'static, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, + A: Announce, >( client: Arc, inner: I, + announce: A, providers: Arc, env: E, spawner: &impl sp_core::traits::SpawnEssentialNamed, @@ -90,7 +92,7 @@ where I::Error: Into, TransactionFor: Send + Sync + 'static, { - let import = TendermintImport::new(client, inner, providers, env); + let import = TendermintImport::new(client, inner, announce, providers, env); let authority = { let machine_clone = import.machine.clone(); diff --git a/substrate/consensus/src/lib.rs b/substrate/consensus/src/lib.rs index a97a91dc..57c1d27a 100644 --- a/substrate/consensus/src/lib.rs +++ b/substrate/consensus/src/lib.rs @@ -1,5 +1,6 @@ use std::{sync::Arc, future::Future}; +use sp_runtime::traits::Block as BlockTrait; use sp_api::TransactionFor; use sc_executor::{NativeVersion, NativeExecutionDispatch, NativeElseWasmExecutor}; @@ -43,15 +44,21 @@ impl NativeExecutionDispatch for ExecutorDispatch { pub type FullClient = TFullClient>; -pub fn import_queue( +pub trait Announce: Send + Sync + Clone + 'static { + fn announce(&self, hash: B::Hash); +} + +pub fn import_queue>( task_manager: &TaskManager, client: Arc, + announce: A, pool: Arc>, registry: Option<&Registry>, ) -> (impl Future, TendermintImportQueue>) { import_queue::import_queue( client.clone(), client.clone(), + announce, Arc::new(|_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) }), sc_basic_authorship::ProposerFactory::new( task_manager.spawn_handle(), diff --git a/substrate/consensus/src/tendermint.rs b/substrate/consensus/src/tendermint.rs index 809bb853..037af327 100644 --- a/substrate/consensus/src/tendermint.rs +++ b/substrate/consensus/src/tendermint.rs @@ -36,6 +36,7 @@ use crate::{ signature_scheme::TendermintSigner, weights::TendermintWeights, import_queue::{ImportFuture, TendermintImportQueue}, + Announce, }; pub(crate) struct TendermintImport< @@ -45,6 +46,7 @@ pub(crate) struct TendermintImport< I: Send + Sync + BlockImport> + 'static, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, + A: Announce, > where TransactionFor: Send + Sync + 'static, { @@ -56,6 +58,7 @@ pub(crate) struct TendermintImport< pub(crate) client: Arc, pub(crate) inner: Arc>, + announce: A, providers: Arc, env: Arc>, @@ -69,7 +72,8 @@ impl< I: Send + Sync + BlockImport> + 'static, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, - > Clone for TendermintImport + A: Announce, + > Clone for TendermintImport where TransactionFor: Send + Sync + 'static, { @@ -83,6 +87,7 @@ where client: self.client.clone(), inner: self.inner.clone(), + announce: self.announce.clone(), providers: self.providers.clone(), env: self.env.clone(), @@ -98,16 +103,18 @@ impl< I: Send + Sync + BlockImport> + 'static, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, - > TendermintImport + A: Announce, + > TendermintImport where TransactionFor: Send + Sync + 'static, { pub(crate) fn new( client: Arc, inner: I, + announce: A, providers: Arc, env: E, - ) -> TendermintImport { + ) -> TendermintImport { TendermintImport { _block: PhantomData, _backend: PhantomData, @@ -117,6 +124,7 @@ where client, inner: Arc::new(AsyncRwLock::new(inner)), + announce, providers, env: Arc::new(AsyncRwLock::new(env)), @@ -281,7 +289,8 @@ impl< I: Send + Sync + BlockImport> + 'static, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, - > Network for TendermintImport + A: Announce, + > Network for TendermintImport where TransactionFor: Send + Sync + 'static, { @@ -361,6 +370,7 @@ where .finalize_block(BlockId::Hash(hash), Some(justification), true) .map_err(|_| Error::InvalidJustification) .unwrap(); + self.announce.announce(hash); self.get_proposal(block.header()).await } diff --git a/substrate/consensus/src/verifier.rs b/substrate/consensus/src/verifier.rs index f5427c3f..4dae0fc0 100644 --- a/substrate/consensus/src/verifier.rs +++ b/substrate/consensus/src/verifier.rs @@ -10,7 +10,7 @@ use sc_consensus::{BlockImportParams, Verifier, BlockImport}; use sc_client_api::{Backend, Finalizer}; -use crate::tendermint::TendermintImport; +use crate::{tendermint::TendermintImport, Announce}; #[async_trait] impl< @@ -20,7 +20,8 @@ impl< I: Send + Sync + BlockImport> + 'static, CIDP: CreateInherentDataProviders + 'static, E: Send + Sync + Environment + 'static, - > Verifier for TendermintImport + A: Announce, + > Verifier for TendermintImport where TransactionFor: Send + Sync + 'static, { diff --git a/substrate/node/Cargo.toml b/substrate/node/Cargo.toml index a25473e7..262e6c20 100644 --- a/substrate/node/Cargo.toml +++ b/substrate/node/Cargo.toml @@ -24,15 +24,16 @@ sp-api = { git = "https://github.com/serai-dex/substrate" } sp-blockchain = { git = "https://github.com/serai-dex/substrate" } sp-block-builder = { git = "https://github.com/serai-dex/substrate" } -sc-cli = { git = "https://github.com/serai-dex/substrate" } -sc-executor = { git = "https://github.com/serai-dex/substrate" } -sc-service = { git = "https://github.com/serai-dex/substrate" } -sc-telemetry = { git = "https://github.com/serai-dex/substrate" } sc-keystore = { git = "https://github.com/serai-dex/substrate" } sc-transaction-pool = { git = "https://github.com/serai-dex/substrate" } sc-transaction-pool-api = { git = "https://github.com/serai-dex/substrate" } -sc-consensus = { git = "https://github.com/serai-dex/substrate" } +sc-executor = { git = "https://github.com/serai-dex/substrate" } +sc-service = { git = "https://github.com/serai-dex/substrate" } sc-client-api = { git = "https://github.com/serai-dex/substrate" } +sc-network = { git = "https://github.com/serai-dex/substrate" } +sc-consensus = { git = "https://github.com/serai-dex/substrate" } +sc-telemetry = { git = "https://github.com/serai-dex/substrate" } +sc-cli = { git = "https://github.com/serai-dex/substrate" } frame-system = { git = "https://github.com/serai-dex/substrate" } frame-benchmarking = { git = "https://github.com/serai-dex/substrate" } diff --git a/substrate/node/src/service.rs b/substrate/node/src/service.rs index 214ae0d6..b4f402a6 100644 --- a/substrate/node/src/service.rs +++ b/substrate/node/src/service.rs @@ -1,11 +1,14 @@ -use std::{sync::Arc, future::Future}; +use std::{sync::{Arc, RwLock}, future::Future}; + +use sp_core::H256; -use sc_service::{error::Error as ServiceError, Configuration, TaskManager}; use sc_executor::NativeElseWasmExecutor; +use sc_service::{error::Error as ServiceError, Configuration, TaskManager}; +use sc_network::{NetworkService, NetworkBlock}; use sc_telemetry::{Telemetry, TelemetryWorker}; use serai_runtime::{self, opaque::Block, RuntimeApi}; -pub(crate) use serai_consensus::{ExecutorDispatch, FullClient}; +pub(crate) use serai_consensus::{ExecutorDispatch, Announce, FullClient}; type FullBackend = sc_service::TFullBackend; type FullSelectChain = serai_consensus::TendermintSelectChain; @@ -19,9 +22,24 @@ type PartialComponents = sc_service::PartialComponents< Option, >; +#[derive(Clone)] +pub struct NetworkAnnounce(Arc>>>>); +impl NetworkAnnounce { + fn new() -> NetworkAnnounce { + NetworkAnnounce(Arc::new(RwLock::new(None))) + } +} +impl Announce for NetworkAnnounce { + fn announce(&self, hash: H256) { + if let Some(network) = self.0.read().unwrap().as_ref() { + network.announce_block(hash, None); + } + } +} + pub fn new_partial( config: &Configuration, -) -> Result<(impl Future, PartialComponents), ServiceError> { +) -> Result<((NetworkAnnounce, impl Future), PartialComponents), ServiceError> { if config.keystore_remote.is_some() { return Err(ServiceError::Other("Remote Keystores are not supported".to_string())); } @@ -65,9 +83,11 @@ pub fn new_partial( client.clone(), ); + let announce = NetworkAnnounce::new(); let (authority, import_queue) = serai_consensus::import_queue( &task_manager, client.clone(), + announce.clone(), transaction_pool.clone(), config.prometheus_registry(), ); @@ -75,7 +95,7 @@ pub fn new_partial( let select_chain = serai_consensus::TendermintSelectChain::new(backend.clone()); Ok(( - authority, + (announce, authority), sc_service::PartialComponents { client, backend, @@ -91,7 +111,7 @@ pub fn new_partial( pub async fn new_full(config: Configuration) -> Result { let ( - authority, + (announce, authority), sc_service::PartialComponents { client, backend, @@ -114,6 +134,7 @@ pub async fn new_full(config: Configuration) -> Result