Announce blocks

By claiming File, they're not sent ovber the P2P network before they 
have a justification, as desired. Unfortunately, they never were. This 
works around that.
This commit is contained in:
Luke Parker
2022-10-22 07:36:13 -04:00
parent dee6993ac8
commit 8a682cd25c
8 changed files with 66 additions and 22 deletions

1
Cargo.lock generated
View File

@@ -7450,6 +7450,7 @@ dependencies = [
"sc-consensus", "sc-consensus",
"sc-executor", "sc-executor",
"sc-keystore", "sc-keystore",
"sc-network",
"sc-rpc", "sc-rpc",
"sc-rpc-api", "sc-rpc-api",
"sc-service", "sc-service",

View File

@@ -12,7 +12,7 @@ use sc_consensus::{BlockCheckParams, BlockImportParams, ImportResult, BlockImpor
use sc_client_api::{Backend, Finalizer}; use sc_client_api::{Backend, Finalizer};
use crate::tendermint::TendermintImport; use crate::{tendermint::TendermintImport, Announce};
#[async_trait] #[async_trait]
impl< impl<
@@ -22,7 +22,8 @@ impl<
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static, I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<B, ()> + 'static, CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static, E: Send + Sync + Environment<B> + 'static,
> BlockImport<B> for TendermintImport<B, Be, C, I, CIDP, E> A: Announce<B>,
> BlockImport<B> for TendermintImport<B, Be, C, I, CIDP, E, A>
where where
I::Error: Into<Error>, I::Error: Into<Error>,
TransactionFor<C, B>: Send + Sync + 'static, TransactionFor<C, B>: Send + Sync + 'static,

View File

@@ -21,7 +21,7 @@ use substrate_prometheus_endpoint::Registry;
use tendermint_machine::{ext::BlockNumber, TendermintMachine}; use tendermint_machine::{ext::BlockNumber, TendermintMachine};
use crate::tendermint::TendermintImport; use crate::{tendermint::TendermintImport, Announce};
pub type TendermintImportQueue<Block, Transaction> = BasicQueue<Block, Transaction>; pub type TendermintImportQueue<Block, Transaction> = BasicQueue<Block, Transaction>;
@@ -78,9 +78,11 @@ pub fn import_queue<
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static, I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<B, ()> + 'static, CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static, E: Send + Sync + Environment<B> + 'static,
A: Announce<B>,
>( >(
client: Arc<C>, client: Arc<C>,
inner: I, inner: I,
announce: A,
providers: Arc<CIDP>, providers: Arc<CIDP>,
env: E, env: E,
spawner: &impl sp_core::traits::SpawnEssentialNamed, spawner: &impl sp_core::traits::SpawnEssentialNamed,
@@ -90,7 +92,7 @@ where
I::Error: Into<Error>, I::Error: Into<Error>,
TransactionFor<C, B>: Send + Sync + 'static, TransactionFor<C, B>: Send + Sync + 'static,
{ {
let import = TendermintImport::new(client, inner, providers, env); let import = TendermintImport::new(client, inner, announce, providers, env);
let authority = { let authority = {
let machine_clone = import.machine.clone(); let machine_clone = import.machine.clone();

View File

@@ -1,5 +1,6 @@
use std::{sync::Arc, future::Future}; use std::{sync::Arc, future::Future};
use sp_runtime::traits::Block as BlockTrait;
use sp_api::TransactionFor; use sp_api::TransactionFor;
use sc_executor::{NativeVersion, NativeExecutionDispatch, NativeElseWasmExecutor}; use sc_executor::{NativeVersion, NativeExecutionDispatch, NativeElseWasmExecutor};
@@ -43,15 +44,21 @@ impl NativeExecutionDispatch for ExecutorDispatch {
pub type FullClient = TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>; pub type FullClient = TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
pub fn import_queue( pub trait Announce<B: BlockTrait>: Send + Sync + Clone + 'static {
fn announce(&self, hash: B::Hash);
}
pub fn import_queue<A: Announce<Block>>(
task_manager: &TaskManager, task_manager: &TaskManager,
client: Arc<FullClient>, client: Arc<FullClient>,
announce: A,
pool: Arc<FullPool<Block, FullClient>>, pool: Arc<FullPool<Block, FullClient>>,
registry: Option<&Registry>, registry: Option<&Registry>,
) -> (impl Future<Output = ()>, TendermintImportQueue<Block, TransactionFor<FullClient, Block>>) { ) -> (impl Future<Output = ()>, TendermintImportQueue<Block, TransactionFor<FullClient, Block>>) {
import_queue::import_queue( import_queue::import_queue(
client.clone(), client.clone(),
client.clone(), client.clone(),
announce,
Arc::new(|_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) }), Arc::new(|_, _| async { Ok(sp_timestamp::InherentDataProvider::from_system_time()) }),
sc_basic_authorship::ProposerFactory::new( sc_basic_authorship::ProposerFactory::new(
task_manager.spawn_handle(), task_manager.spawn_handle(),

View File

@@ -36,6 +36,7 @@ use crate::{
signature_scheme::TendermintSigner, signature_scheme::TendermintSigner,
weights::TendermintWeights, weights::TendermintWeights,
import_queue::{ImportFuture, TendermintImportQueue}, import_queue::{ImportFuture, TendermintImportQueue},
Announce,
}; };
pub(crate) struct TendermintImport< pub(crate) struct TendermintImport<
@@ -45,6 +46,7 @@ pub(crate) struct TendermintImport<
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static, I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<B, ()> + 'static, CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static, E: Send + Sync + Environment<B> + 'static,
A: Announce<B>,
> where > where
TransactionFor<C, B>: Send + Sync + 'static, TransactionFor<C, B>: Send + Sync + 'static,
{ {
@@ -56,6 +58,7 @@ pub(crate) struct TendermintImport<
pub(crate) client: Arc<C>, pub(crate) client: Arc<C>,
pub(crate) inner: Arc<AsyncRwLock<I>>, pub(crate) inner: Arc<AsyncRwLock<I>>,
announce: A,
providers: Arc<CIDP>, providers: Arc<CIDP>,
env: Arc<AsyncRwLock<E>>, env: Arc<AsyncRwLock<E>>,
@@ -69,7 +72,8 @@ impl<
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static, I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<B, ()> + 'static, CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static, E: Send + Sync + Environment<B> + 'static,
> Clone for TendermintImport<B, Be, C, I, CIDP, E> A: Announce<B>,
> Clone for TendermintImport<B, Be, C, I, CIDP, E, A>
where where
TransactionFor<C, B>: Send + Sync + 'static, TransactionFor<C, B>: Send + Sync + 'static,
{ {
@@ -83,6 +87,7 @@ where
client: self.client.clone(), client: self.client.clone(),
inner: self.inner.clone(), inner: self.inner.clone(),
announce: self.announce.clone(),
providers: self.providers.clone(), providers: self.providers.clone(),
env: self.env.clone(), env: self.env.clone(),
@@ -98,16 +103,18 @@ impl<
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static, I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<B, ()> + 'static, CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static, E: Send + Sync + Environment<B> + 'static,
> TendermintImport<B, Be, C, I, CIDP, E> A: Announce<B>,
> TendermintImport<B, Be, C, I, CIDP, E, A>
where where
TransactionFor<C, B>: Send + Sync + 'static, TransactionFor<C, B>: Send + Sync + 'static,
{ {
pub(crate) fn new( pub(crate) fn new(
client: Arc<C>, client: Arc<C>,
inner: I, inner: I,
announce: A,
providers: Arc<CIDP>, providers: Arc<CIDP>,
env: E, env: E,
) -> TendermintImport<B, Be, C, I, CIDP, E> { ) -> TendermintImport<B, Be, C, I, CIDP, E, A> {
TendermintImport { TendermintImport {
_block: PhantomData, _block: PhantomData,
_backend: PhantomData, _backend: PhantomData,
@@ -117,6 +124,7 @@ where
client, client,
inner: Arc::new(AsyncRwLock::new(inner)), inner: Arc::new(AsyncRwLock::new(inner)),
announce,
providers, providers,
env: Arc::new(AsyncRwLock::new(env)), env: Arc::new(AsyncRwLock::new(env)),
@@ -281,7 +289,8 @@ impl<
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static, I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<B, ()> + 'static, CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static, E: Send + Sync + Environment<B> + 'static,
> Network for TendermintImport<B, Be, C, I, CIDP, E> A: Announce<B>,
> Network for TendermintImport<B, Be, C, I, CIDP, E, A>
where where
TransactionFor<C, B>: Send + Sync + 'static, TransactionFor<C, B>: Send + Sync + 'static,
{ {
@@ -361,6 +370,7 @@ where
.finalize_block(BlockId::Hash(hash), Some(justification), true) .finalize_block(BlockId::Hash(hash), Some(justification), true)
.map_err(|_| Error::InvalidJustification) .map_err(|_| Error::InvalidJustification)
.unwrap(); .unwrap();
self.announce.announce(hash);
self.get_proposal(block.header()).await self.get_proposal(block.header()).await
} }

View File

@@ -10,7 +10,7 @@ use sc_consensus::{BlockImportParams, Verifier, BlockImport};
use sc_client_api::{Backend, Finalizer}; use sc_client_api::{Backend, Finalizer};
use crate::tendermint::TendermintImport; use crate::{tendermint::TendermintImport, Announce};
#[async_trait] #[async_trait]
impl< impl<
@@ -20,7 +20,8 @@ impl<
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static, I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<B, ()> + 'static, CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static, E: Send + Sync + Environment<B> + 'static,
> Verifier<B> for TendermintImport<B, Be, C, I, CIDP, E> A: Announce<B>,
> Verifier<B> for TendermintImport<B, Be, C, I, CIDP, E, A>
where where
TransactionFor<C, B>: Send + Sync + 'static, TransactionFor<C, B>: Send + Sync + 'static,
{ {

View File

@@ -24,15 +24,16 @@ sp-api = { git = "https://github.com/serai-dex/substrate" }
sp-blockchain = { 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" } 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-keystore = { git = "https://github.com/serai-dex/substrate" }
sc-transaction-pool = { 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-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-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-system = { git = "https://github.com/serai-dex/substrate" }
frame-benchmarking = { git = "https://github.com/serai-dex/substrate" } frame-benchmarking = { git = "https://github.com/serai-dex/substrate" }

View File

@@ -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_executor::NativeElseWasmExecutor;
use sc_service::{error::Error as ServiceError, Configuration, TaskManager};
use sc_network::{NetworkService, NetworkBlock};
use sc_telemetry::{Telemetry, TelemetryWorker}; use sc_telemetry::{Telemetry, TelemetryWorker};
use serai_runtime::{self, opaque::Block, RuntimeApi}; 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<Block>; type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = serai_consensus::TendermintSelectChain<Block, FullBackend>; type FullSelectChain = serai_consensus::TendermintSelectChain<Block, FullBackend>;
@@ -19,9 +22,24 @@ type PartialComponents = sc_service::PartialComponents<
Option<Telemetry>, Option<Telemetry>,
>; >;
#[derive(Clone)]
pub struct NetworkAnnounce(Arc<RwLock<Option<Arc<NetworkService<Block, H256>>>>>);
impl NetworkAnnounce {
fn new() -> NetworkAnnounce {
NetworkAnnounce(Arc::new(RwLock::new(None)))
}
}
impl Announce<Block> 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( pub fn new_partial(
config: &Configuration, config: &Configuration,
) -> Result<(impl Future<Output = ()>, PartialComponents), ServiceError> { ) -> Result<((NetworkAnnounce, impl Future<Output = ()>), PartialComponents), ServiceError> {
if config.keystore_remote.is_some() { if config.keystore_remote.is_some() {
return Err(ServiceError::Other("Remote Keystores are not supported".to_string())); return Err(ServiceError::Other("Remote Keystores are not supported".to_string()));
} }
@@ -65,9 +83,11 @@ pub fn new_partial(
client.clone(), client.clone(),
); );
let announce = NetworkAnnounce::new();
let (authority, import_queue) = serai_consensus::import_queue( let (authority, import_queue) = serai_consensus::import_queue(
&task_manager, &task_manager,
client.clone(), client.clone(),
announce.clone(),
transaction_pool.clone(), transaction_pool.clone(),
config.prometheus_registry(), config.prometheus_registry(),
); );
@@ -75,7 +95,7 @@ pub fn new_partial(
let select_chain = serai_consensus::TendermintSelectChain::new(backend.clone()); let select_chain = serai_consensus::TendermintSelectChain::new(backend.clone());
Ok(( Ok((
authority, (announce, authority),
sc_service::PartialComponents { sc_service::PartialComponents {
client, client,
backend, backend,
@@ -91,7 +111,7 @@ pub fn new_partial(
pub async fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> { pub async fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
let ( let (
authority, (announce, authority),
sc_service::PartialComponents { sc_service::PartialComponents {
client, client,
backend, backend,
@@ -114,6 +134,7 @@ pub async fn new_full(config: Configuration) -> Result<TaskManager, ServiceError
block_announce_validator_builder: None, block_announce_validator_builder: None,
warp_sync: None, warp_sync: None,
})?; })?;
*announce.0.write().unwrap() = Some(network.clone());
if config.offchain_worker.enabled { if config.offchain_worker.enabled {
sc_service::build_offchain_workers( sc_service::build_offchain_workers(