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
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6
8 changed files with 66 additions and 22 deletions

1
Cargo.lock generated
View file

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

View file

@ -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<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<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
I::Error: Into<Error>,
TransactionFor<C, B>: Send + Sync + 'static,

View file

@ -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<Block, Transaction> = BasicQueue<Block, Transaction>;
@ -78,9 +78,11 @@ pub fn import_queue<
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static,
A: Announce<B>,
>(
client: Arc<C>,
inner: I,
announce: A,
providers: Arc<CIDP>,
env: E,
spawner: &impl sp_core::traits::SpawnEssentialNamed,
@ -90,7 +92,7 @@ where
I::Error: Into<Error>,
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 machine_clone = import.machine.clone();

View file

@ -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<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,
client: Arc<FullClient>,
announce: A,
pool: Arc<FullPool<Block, FullClient>>,
registry: Option<&Registry>,
) -> (impl Future<Output = ()>, TendermintImportQueue<Block, TransactionFor<FullClient, Block>>) {
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(),

View file

@ -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<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<B, ()> + 'static,
E: Send + Sync + Environment<B> + 'static,
A: Announce<B>,
> where
TransactionFor<C, B>: Send + Sync + 'static,
{
@ -56,6 +58,7 @@ pub(crate) struct TendermintImport<
pub(crate) client: Arc<C>,
pub(crate) inner: Arc<AsyncRwLock<I>>,
announce: A,
providers: Arc<CIDP>,
env: Arc<AsyncRwLock<E>>,
@ -69,7 +72,8 @@ impl<
I: Send + Sync + BlockImport<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<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
TransactionFor<C, B>: 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<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<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
TransactionFor<C, B>: Send + Sync + 'static,
{
pub(crate) fn new(
client: Arc<C>,
inner: I,
announce: A,
providers: Arc<CIDP>,
env: E,
) -> TendermintImport<B, Be, C, I, CIDP, E> {
) -> TendermintImport<B, Be, C, I, CIDP, E, A> {
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<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<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
TransactionFor<C, B>: 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
}

View file

@ -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<B, Transaction = TransactionFor<C, B>> + 'static,
CIDP: CreateInherentDataProviders<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
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-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" }

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_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<Block>;
type FullSelectChain = serai_consensus::TendermintSelectChain<Block, FullBackend>;
@ -19,9 +22,24 @@ type PartialComponents = sc_service::PartialComponents<
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(
config: &Configuration,
) -> Result<(impl Future<Output = ()>, PartialComponents), ServiceError> {
) -> Result<((NetworkAnnounce, impl Future<Output = ()>), 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<TaskManager, ServiceError> {
let (
authority,
(announce, authority),
sc_service::PartialComponents {
client,
backend,
@ -114,6 +134,7 @@ pub async fn new_full(config: Configuration) -> Result<TaskManager, ServiceError
block_announce_validator_builder: None,
warp_sync: None,
})?;
*announce.0.write().unwrap() = Some(network.clone());
if config.offchain_worker.enabled {
sc_service::build_offchain_workers(