mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 18:11:10 +00:00
The great refactor (#325)
* Move pallets to pallets folder and rename them * Move genesis file to service * Rename primitives to primitives-core * Delete cumulus-runtime * Move stuff to client folder and rename
This commit is contained in:
@@ -0,0 +1,571 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Parachain specific networking
|
||||
//!
|
||||
//! Provides a custom block announcement implementation for parachains
|
||||
//! that use the relay chain provided consensus. See [`BlockAnnounceValidator`]
|
||||
//! and [`WaitToAnnounce`] for more information about this implementation.
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
mod wait_on_relay_chain_block;
|
||||
|
||||
use sc_client_api::{Backend, BlockchainEvents};
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_blockchain::HeaderBackend;
|
||||
use sp_consensus::{
|
||||
block_validation::{BlockAnnounceValidator as BlockAnnounceValidatorT, Validation},
|
||||
SyncOracle,
|
||||
};
|
||||
use sp_core::traits::SpawnNamed;
|
||||
use sp_runtime::{
|
||||
generic::BlockId,
|
||||
traits::{Block as BlockT, HashFor, Header as HeaderT},
|
||||
};
|
||||
|
||||
use polkadot_node_primitives::{SignedFullStatement, Statement};
|
||||
use polkadot_node_subsystem::messages::StatementDistributionMessage;
|
||||
use polkadot_overseer::OverseerHandler;
|
||||
use polkadot_primitives::v1::{
|
||||
Block as PBlock, CandidateReceipt, CompactStatement, Hash as PHash, Id as ParaId,
|
||||
OccupiedCoreAssumption, ParachainHost, SignedStatement, SigningContext,
|
||||
};
|
||||
use polkadot_service::ClientHandle;
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
use futures::{
|
||||
channel::{mpsc, oneshot},
|
||||
future::{ready, FutureExt},
|
||||
pin_mut, select, Future, StreamExt,
|
||||
};
|
||||
use log::trace;
|
||||
|
||||
use std::{convert::TryFrom, fmt, marker::PhantomData, pin::Pin, sync::Arc};
|
||||
|
||||
use wait_on_relay_chain_block::WaitOnRelayChainBlock;
|
||||
|
||||
const LOG_TARGET: &str = "cumulus-network";
|
||||
|
||||
type BoxedError = Box<dyn std::error::Error + Send>;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct BlockAnnounceError(String);
|
||||
impl std::error::Error for BlockAnnounceError {}
|
||||
|
||||
impl fmt::Display for BlockAnnounceError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
/// The data that we attach to a block announcement.
|
||||
///
|
||||
/// This will be used to prove that a header belongs to a block that is probably being backed by
|
||||
/// the relay chain.
|
||||
#[derive(Encode, Decode, Debug)]
|
||||
pub struct BlockAnnounceData {
|
||||
receipt: CandidateReceipt,
|
||||
statement: SignedStatement,
|
||||
}
|
||||
|
||||
impl BlockAnnounceData {
|
||||
/// Validate that the receipt, statement and announced header match.
|
||||
///
|
||||
/// This will not check the signature, for this you should use [`BlockAnnounceData::check_signature`].
|
||||
fn validate(&self, encoded_header: Vec<u8>) -> Result<(), Validation> {
|
||||
let candidate_hash = if let CompactStatement::Candidate(h) = self.statement.payload() {
|
||||
h
|
||||
} else {
|
||||
log::debug!(
|
||||
target: LOG_TARGET,
|
||||
"`CompactStatement` isn't the candidate variant!",
|
||||
);
|
||||
return Err(Validation::Failure { disconnect: true });
|
||||
};
|
||||
|
||||
if *candidate_hash != self.receipt.hash() {
|
||||
log::debug!(
|
||||
target: LOG_TARGET,
|
||||
"Receipt candidate hash doesn't match candidate hash in statement",
|
||||
);
|
||||
return Err(Validation::Failure { disconnect: true });
|
||||
}
|
||||
|
||||
if polkadot_parachain::primitives::HeadData(encoded_header).hash()
|
||||
!= self.receipt.descriptor.para_head
|
||||
{
|
||||
log::debug!(
|
||||
target: LOG_TARGET,
|
||||
"Receipt para head hash doesn't match the hash of the header in the block announcement",
|
||||
);
|
||||
return Err(Validation::Failure { disconnect: true });
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check the signature of the statement.
|
||||
///
|
||||
/// Returns an `Err(_)` if it failed.
|
||||
fn check_signature<P>(&self, relay_chain_client: &Arc<P>) -> Result<Validation, BlockAnnounceError>
|
||||
where
|
||||
P: ProvideRuntimeApi<PBlock> + Send + Sync + 'static,
|
||||
P::Api: ParachainHost<PBlock>,
|
||||
{
|
||||
let runtime_api = relay_chain_client.runtime_api();
|
||||
let validator_index = self.statement.validator_index();
|
||||
|
||||
let runtime_api_block_id = BlockId::Hash(self.receipt.descriptor.relay_parent);
|
||||
let session_index = match runtime_api.session_index_for_child(&runtime_api_block_id) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
return Err(BlockAnnounceError(format!("{:?}", e)));
|
||||
}
|
||||
};
|
||||
|
||||
let signing_context = SigningContext {
|
||||
parent_hash: self.receipt.descriptor.relay_parent,
|
||||
session_index,
|
||||
};
|
||||
|
||||
// Check that the signer is a legit validator.
|
||||
let authorities = match runtime_api.validators(&runtime_api_block_id) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
return Err(BlockAnnounceError(format!("{:?}", e)));
|
||||
}
|
||||
};
|
||||
let signer = match authorities.get(validator_index as usize) {
|
||||
Some(r) => r,
|
||||
None => {
|
||||
log::debug!(
|
||||
target: LOG_TARGET,
|
||||
"Block announcement justification signer is a validator index out of bound",
|
||||
);
|
||||
|
||||
return Ok(Validation::Failure { disconnect: true })
|
||||
}
|
||||
};
|
||||
|
||||
// Check statement is correctly signed.
|
||||
if self
|
||||
.statement
|
||||
.check_signature(&signing_context, &signer)
|
||||
.is_err()
|
||||
{
|
||||
log::debug!(
|
||||
target: LOG_TARGET,
|
||||
"Block announcement justification signature is invalid.",
|
||||
);
|
||||
|
||||
return Ok(Validation::Failure { disconnect: true });
|
||||
}
|
||||
|
||||
Ok(Validation::Success { is_new_best: true })
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<SignedFullStatement> for BlockAnnounceData {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(stmt: SignedFullStatement) -> Result<BlockAnnounceData, ()> {
|
||||
let receipt = if let Statement::Seconded(receipt) = stmt.payload() {
|
||||
receipt.to_plain()
|
||||
} else {
|
||||
return Err(());
|
||||
};
|
||||
|
||||
Ok(BlockAnnounceData {
|
||||
receipt,
|
||||
statement: stmt.convert_payload(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Parachain specific block announce validator.
|
||||
///
|
||||
/// This block announce validator is required if the parachain is running
|
||||
/// with the relay chain provided consensus to make sure each node only
|
||||
/// imports a reasonable number of blocks per round. The relay chain provided
|
||||
/// consensus doesn't have any authorities and so it could happen that without
|
||||
/// this special block announce validator a node would need to import *millions*
|
||||
/// of blocks per round, which is clearly not doable.
|
||||
///
|
||||
/// To solve this problem, each block announcement is delayed until a collator
|
||||
/// has received a [`Statement::Seconded`] for its `PoV`. This message tells the
|
||||
/// collator that its `PoV` was validated successfully by a parachain validator and
|
||||
/// that it is very likely that this `PoV` will be included in the relay chain. Every
|
||||
/// collator that doesn't receive the message for its `PoV` will not announce its block.
|
||||
/// For more information on the block announcement, see [`WaitToAnnounce`].
|
||||
///
|
||||
/// For each block announcement that is received, the generic block announcement validation
|
||||
/// will call this validator and provides the extra data that was attached to the announcement.
|
||||
/// We call this extra data `justification`.
|
||||
/// It is expected that the attached data is a SCALE encoded [`BlockAnnounceData`]. The
|
||||
/// statement is checked to be a [`CompactStatement::Candidate`] and that it is signed by an active
|
||||
/// parachain validator.
|
||||
///
|
||||
/// If no justification was provided we check if the block announcement is at the tip of the known
|
||||
/// chain. If it is at the tip, it is required to provide a justification or otherwise we reject
|
||||
/// it. However, if the announcement is for a block below the tip the announcement is accepted
|
||||
/// as it probably comes from a node that is currently syncing the chain.
|
||||
pub struct BlockAnnounceValidator<Block, P, B, BCE> {
|
||||
phantom: PhantomData<Block>,
|
||||
relay_chain_client: Arc<P>,
|
||||
relay_chain_backend: Arc<B>,
|
||||
para_id: ParaId,
|
||||
relay_chain_sync_oracle: Box<dyn SyncOracle + Send>,
|
||||
wait_on_relay_chain_block: WaitOnRelayChainBlock<B, BCE>,
|
||||
}
|
||||
|
||||
impl<Block, P, B, BCE> BlockAnnounceValidator<Block, P, B, BCE> {
|
||||
/// Create a new [`BlockAnnounceValidator`].
|
||||
pub fn new(
|
||||
relay_chain_client: Arc<P>,
|
||||
para_id: ParaId,
|
||||
relay_chain_sync_oracle: Box<dyn SyncOracle + Send>,
|
||||
relay_chain_backend: Arc<B>,
|
||||
relay_chain_blockchain_events: Arc<BCE>,
|
||||
) -> Self {
|
||||
Self {
|
||||
phantom: Default::default(),
|
||||
relay_chain_client,
|
||||
para_id,
|
||||
relay_chain_sync_oracle,
|
||||
relay_chain_backend: relay_chain_backend.clone(),
|
||||
wait_on_relay_chain_block: WaitOnRelayChainBlock::new(
|
||||
relay_chain_backend,
|
||||
relay_chain_blockchain_events,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT, P, B, BCE> BlockAnnounceValidator<Block, P, B, BCE>
|
||||
where
|
||||
P: ProvideRuntimeApi<PBlock> + Send + Sync + 'static,
|
||||
P::Api: ParachainHost<PBlock>,
|
||||
B: Backend<PBlock> + 'static,
|
||||
// Rust bug: https://github.com/rust-lang/rust/issues/24159
|
||||
sc_client_api::StateBackendFor<B, PBlock>: sc_client_api::StateBackend<HashFor<PBlock>>,
|
||||
{
|
||||
/// Handle a block announcement with empty data (no statement) attached to it.
|
||||
fn handle_empty_block_announce_data(
|
||||
&self,
|
||||
header: Block::Header,
|
||||
) -> impl Future<Output = Result<Validation, BoxedError>> {
|
||||
let relay_chain_client = self.relay_chain_client.clone();
|
||||
let relay_chain_backend = self.relay_chain_backend.clone();
|
||||
let para_id = self.para_id;
|
||||
|
||||
async move {
|
||||
// Check if block is equal or higher than best (this requires a justification)
|
||||
let relay_chain_info = relay_chain_backend.blockchain().info();
|
||||
let runtime_api_block_id = BlockId::Hash(relay_chain_info.best_hash);
|
||||
let block_number = header.number();
|
||||
|
||||
let local_validation_data = relay_chain_client
|
||||
.runtime_api()
|
||||
.persisted_validation_data(
|
||||
&runtime_api_block_id,
|
||||
para_id,
|
||||
OccupiedCoreAssumption::TimedOut,
|
||||
)
|
||||
.map_err(|e| Box::new(BlockAnnounceError(format!("{:?}", e))) as Box<_>)?
|
||||
.ok_or_else(|| {
|
||||
Box::new(BlockAnnounceError(
|
||||
"Could not find parachain head in relay chain".into(),
|
||||
)) as Box<_>
|
||||
})?;
|
||||
let parent_head = Block::Header::decode(&mut &local_validation_data.parent_head.0[..])
|
||||
.map_err(|e| {
|
||||
Box::new(BlockAnnounceError(format!(
|
||||
"Failed to decode parachain head: {:?}",
|
||||
e
|
||||
))) as Box<_>
|
||||
})?;
|
||||
let known_best_number = parent_head.number();
|
||||
|
||||
if block_number >= known_best_number {
|
||||
trace!(
|
||||
target: "cumulus-network",
|
||||
"validation failed because a justification is needed if the block at the top of the chain."
|
||||
);
|
||||
|
||||
Ok(Validation::Failure { disconnect: false })
|
||||
} else {
|
||||
Ok(Validation::Success { is_new_best: false })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT, P, B, BCE> BlockAnnounceValidatorT<Block>
|
||||
for BlockAnnounceValidator<Block, P, B, BCE>
|
||||
where
|
||||
P: ProvideRuntimeApi<PBlock> + Send + Sync + 'static,
|
||||
P::Api: ParachainHost<PBlock>,
|
||||
B: Backend<PBlock> + 'static,
|
||||
BCE: BlockchainEvents<PBlock> + 'static + Send + Sync,
|
||||
// Rust bug: https://github.com/rust-lang/rust/issues/24159
|
||||
sc_client_api::StateBackendFor<B, PBlock>: sc_client_api::StateBackend<HashFor<PBlock>>,
|
||||
{
|
||||
fn validate(
|
||||
&mut self,
|
||||
header: &Block::Header,
|
||||
mut data: &[u8],
|
||||
) -> Pin<Box<dyn Future<Output = Result<Validation, BoxedError>> + Send>> {
|
||||
if self.relay_chain_sync_oracle.is_major_syncing() {
|
||||
return ready(Ok(Validation::Success { is_new_best: false })).boxed();
|
||||
}
|
||||
|
||||
if data.is_empty() {
|
||||
return self
|
||||
.handle_empty_block_announce_data(header.clone())
|
||||
.boxed();
|
||||
}
|
||||
|
||||
let block_announce_data = match BlockAnnounceData::decode(&mut data) {
|
||||
Ok(r) => r,
|
||||
Err(_) => {
|
||||
return ready(Err(Box::new(BlockAnnounceError(
|
||||
"Can not decode the `BlockAnnounceData`".into(),
|
||||
)) as Box<_>))
|
||||
.boxed()
|
||||
}
|
||||
};
|
||||
|
||||
let relay_chain_client = self.relay_chain_client.clone();
|
||||
let header_encoded = header.encode();
|
||||
let wait_on_relay_chain_block = self.wait_on_relay_chain_block.clone();
|
||||
|
||||
async move {
|
||||
if let Err(e) = block_announce_data.validate(header_encoded) {
|
||||
return Ok(e);
|
||||
}
|
||||
|
||||
let relay_parent = block_announce_data.receipt.descriptor.relay_parent;
|
||||
|
||||
wait_on_relay_chain_block
|
||||
.wait_on_relay_chain_block(relay_parent)
|
||||
.await
|
||||
.map_err(|e| Box::new(BlockAnnounceError(e.to_string())) as Box<_>)?;
|
||||
|
||||
block_announce_data
|
||||
.check_signature(&relay_chain_client)
|
||||
.map_err(|e| Box::new(e) as Box<_>)
|
||||
}
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a block announce validator instance.
|
||||
///
|
||||
/// Returns a boxed [`BlockAnnounceValidator`].
|
||||
pub fn build_block_announce_validator<Block: BlockT, B>(
|
||||
relay_chain_client: polkadot_service::Client,
|
||||
para_id: ParaId,
|
||||
relay_chain_sync_oracle: Box<dyn SyncOracle + Send>,
|
||||
relay_chain_backend: Arc<B>,
|
||||
) -> Box<dyn BlockAnnounceValidatorT<Block> + Send>
|
||||
where
|
||||
B: Backend<PBlock> + Send + 'static,
|
||||
// Rust bug: https://github.com/rust-lang/rust/issues/24159
|
||||
sc_client_api::StateBackendFor<B, PBlock>: sc_client_api::StateBackend<HashFor<PBlock>>,
|
||||
{
|
||||
BlockAnnounceValidatorBuilder::new(
|
||||
relay_chain_client,
|
||||
para_id,
|
||||
relay_chain_sync_oracle,
|
||||
relay_chain_backend,
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
/// Block announce validator builder.
|
||||
///
|
||||
/// Builds a [`BlockAnnounceValidator`] for a parachain. As this requires
|
||||
/// a concrete relay chain client instance, the builder takes a [`polkadot_service::Client`]
|
||||
/// that wraps this concrete instanace. By using [`polkadot_service::ExecuteWithClient`]
|
||||
/// the builder gets access to this concrete instance.
|
||||
struct BlockAnnounceValidatorBuilder<Block, B> {
|
||||
phantom: PhantomData<Block>,
|
||||
relay_chain_client: polkadot_service::Client,
|
||||
para_id: ParaId,
|
||||
relay_chain_sync_oracle: Box<dyn SyncOracle + Send>,
|
||||
relay_chain_backend: Arc<B>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT, B> BlockAnnounceValidatorBuilder<Block, B>
|
||||
where
|
||||
B: Backend<PBlock> + Send + 'static,
|
||||
// Rust bug: https://github.com/rust-lang/rust/issues/24159
|
||||
sc_client_api::StateBackendFor<B, PBlock>: sc_client_api::StateBackend<HashFor<PBlock>>,
|
||||
{
|
||||
/// Create a new instance of the builder.
|
||||
fn new(
|
||||
relay_chain_client: polkadot_service::Client,
|
||||
para_id: ParaId,
|
||||
relay_chain_sync_oracle: Box<dyn SyncOracle + Send>,
|
||||
relay_chain_backend: Arc<B>,
|
||||
) -> Self {
|
||||
Self {
|
||||
relay_chain_client,
|
||||
para_id,
|
||||
relay_chain_sync_oracle,
|
||||
relay_chain_backend,
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the block announce validator.
|
||||
fn build(self) -> Box<dyn BlockAnnounceValidatorT<Block> + Send> {
|
||||
self.relay_chain_client.clone().execute_with(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT, B> polkadot_service::ExecuteWithClient
|
||||
for BlockAnnounceValidatorBuilder<Block, B>
|
||||
where
|
||||
B: Backend<PBlock> + Send + 'static,
|
||||
// Rust bug: https://github.com/rust-lang/rust/issues/24159
|
||||
sc_client_api::StateBackendFor<B, PBlock>: sc_client_api::StateBackend<HashFor<PBlock>>,
|
||||
{
|
||||
type Output = Box<dyn BlockAnnounceValidatorT<Block> + Send>;
|
||||
|
||||
fn execute_with_client<PClient, Api, PBackend>(self, client: Arc<PClient>) -> Self::Output
|
||||
where
|
||||
<Api as sp_api::ApiExt<PBlock>>::StateBackend:
|
||||
sp_api::StateBackend<sp_runtime::traits::BlakeTwo256>,
|
||||
PBackend: Backend<PBlock>,
|
||||
PBackend::State: sp_api::StateBackend<sp_runtime::traits::BlakeTwo256>,
|
||||
Api: polkadot_service::RuntimeApiCollection<StateBackend = PBackend::State>,
|
||||
PClient: polkadot_service::AbstractClient<PBlock, PBackend, Api = Api> + 'static,
|
||||
{
|
||||
Box::new(BlockAnnounceValidator::new(
|
||||
client.clone(),
|
||||
self.para_id,
|
||||
self.relay_chain_sync_oracle,
|
||||
self.relay_chain_backend,
|
||||
client,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Wait before announcing a block that a candidate message has been received for this block, then
|
||||
/// add this message as justification for the block announcement.
|
||||
///
|
||||
/// This object will spawn a new task every time the method `wait_to_announce` is called and cancel
|
||||
/// the previous task running.
|
||||
pub struct WaitToAnnounce<Block: BlockT> {
|
||||
spawner: Arc<dyn SpawnNamed + Send + Sync>,
|
||||
announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
|
||||
overseer_handler: OverseerHandler,
|
||||
current_trigger: oneshot::Sender<()>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT> WaitToAnnounce<Block> {
|
||||
/// Create the `WaitToAnnounce` object
|
||||
pub fn new(
|
||||
spawner: Arc<dyn SpawnNamed + Send + Sync>,
|
||||
announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
|
||||
overseer_handler: OverseerHandler,
|
||||
) -> WaitToAnnounce<Block> {
|
||||
let (tx, _rx) = oneshot::channel();
|
||||
|
||||
WaitToAnnounce {
|
||||
spawner,
|
||||
announce_block,
|
||||
overseer_handler,
|
||||
current_trigger: tx,
|
||||
}
|
||||
}
|
||||
|
||||
/// Wait for a candidate message for the block, then announce the block. The candidate
|
||||
/// message will be added as justification to the block announcement.
|
||||
pub fn wait_to_announce(&mut self, block_hash: <Block as BlockT>::Hash, pov_hash: PHash) {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let announce_block = self.announce_block.clone();
|
||||
let overseer_handler = self.overseer_handler.clone();
|
||||
|
||||
self.current_trigger = tx;
|
||||
|
||||
self.spawner.spawn(
|
||||
"cumulus-wait-to-announce",
|
||||
async move {
|
||||
let t1 = wait_to_announce::<Block>(
|
||||
block_hash,
|
||||
pov_hash,
|
||||
announce_block,
|
||||
overseer_handler,
|
||||
)
|
||||
.fuse();
|
||||
let t2 = rx.fuse();
|
||||
|
||||
pin_mut!(t1, t2);
|
||||
|
||||
trace!(
|
||||
target: "cumulus-network",
|
||||
"waiting for announce block in a background task...",
|
||||
);
|
||||
|
||||
select! {
|
||||
_ = t1 => {
|
||||
trace!(
|
||||
target: "cumulus-network",
|
||||
"block announcement finished",
|
||||
);
|
||||
},
|
||||
_ = t2 => {
|
||||
trace!(
|
||||
target: "cumulus-network",
|
||||
"previous task that waits for announce block has been canceled",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
.boxed(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait_to_announce<Block: BlockT>(
|
||||
block_hash: <Block as BlockT>::Hash,
|
||||
pov_hash: PHash,
|
||||
announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
|
||||
mut overseer_handler: OverseerHandler,
|
||||
) {
|
||||
let (sender, mut receiver) = mpsc::channel(5);
|
||||
overseer_handler
|
||||
.send_msg(StatementDistributionMessage::RegisterStatementListener(
|
||||
sender,
|
||||
))
|
||||
.await;
|
||||
|
||||
while let Some(statement) = receiver.next().await {
|
||||
match statement.payload() {
|
||||
Statement::Seconded(c) if &c.descriptor.pov_hash == &pov_hash => {
|
||||
if let Ok(data) = BlockAnnounceData::try_from(statement) {
|
||||
announce_block(block_hash, data.encode());
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,422 @@
|
||||
// Copyright 2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use super::*;
|
||||
use cumulus_test_service::runtime::{Block, Header};
|
||||
use futures::{executor::block_on, poll, task::Poll};
|
||||
use polkadot_node_primitives::{SignedFullStatement, Statement};
|
||||
use polkadot_primitives::v1::{
|
||||
Block as PBlock, BlockNumber, CandidateCommitments, CandidateDescriptor, CandidateEvent,
|
||||
CommittedCandidateReceipt, CoreState, GroupRotationInfo, Hash as PHash, HeadData, Id as ParaId,
|
||||
InboundDownwardMessage, InboundHrmpMessage, OccupiedCoreAssumption, ParachainHost,
|
||||
PersistedValidationData, SessionIndex, SessionInfo, SigningContext, ValidationCode,
|
||||
ValidatorId, ValidatorIndex,
|
||||
};
|
||||
use polkadot_test_client::{
|
||||
Client as PClient, ClientBlockImportExt, DefaultTestClientBuilderExt, FullBackend as PBackend,
|
||||
InitPolkadotBlockBuilder, TestClientBuilder, TestClientBuilderExt,
|
||||
};
|
||||
use sp_api::{ApiRef, ProvideRuntimeApi};
|
||||
use sp_blockchain::HeaderBackend;
|
||||
use sp_consensus::{block_validation::BlockAnnounceValidator as _, BlockOrigin};
|
||||
use sp_core::H256;
|
||||
use sp_keyring::Sr25519Keyring;
|
||||
use sp_keystore::{testing::KeyStore, SyncCryptoStore, SyncCryptoStorePtr};
|
||||
use sp_runtime::RuntimeAppPublic;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
fn check_error(error: crate::BoxedError, check_error: impl Fn(&BlockAnnounceError) -> bool) {
|
||||
let error = *error
|
||||
.downcast::<BlockAnnounceError>()
|
||||
.expect("Downcasts error to `ClientError`");
|
||||
if !check_error(&error) {
|
||||
panic!("Invalid error: {:?}", error);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct DummyCollatorNetwork;
|
||||
|
||||
impl SyncOracle for DummyCollatorNetwork {
|
||||
fn is_major_syncing(&mut self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn is_offline(&mut self) -> bool {
|
||||
unimplemented!("Not required in tests")
|
||||
}
|
||||
}
|
||||
|
||||
fn make_validator_and_api() -> (
|
||||
BlockAnnounceValidator<Block, TestApi, PBackend, PClient>,
|
||||
Arc<TestApi>,
|
||||
) {
|
||||
let api = Arc::new(TestApi::new());
|
||||
|
||||
(
|
||||
BlockAnnounceValidator::new(
|
||||
api.clone(),
|
||||
ParaId::from(56),
|
||||
Box::new(DummyCollatorNetwork),
|
||||
api.relay_backend.clone(),
|
||||
api.relay_client.clone(),
|
||||
),
|
||||
api,
|
||||
)
|
||||
}
|
||||
|
||||
fn default_header() -> Header {
|
||||
Header {
|
||||
number: 1,
|
||||
digest: Default::default(),
|
||||
extrinsics_root: Default::default(),
|
||||
parent_hash: Default::default(),
|
||||
state_root: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Same as [`make_gossip_message_and_header`], but using the genesis header as relay parent.
|
||||
async fn make_gossip_message_and_header_using_genesis(
|
||||
api: Arc<TestApi>,
|
||||
validator_index: u32,
|
||||
) -> (SignedFullStatement, Header) {
|
||||
let relay_parent = api
|
||||
.relay_client
|
||||
.hash(0)
|
||||
.ok()
|
||||
.flatten()
|
||||
.expect("Genesis hash exists");
|
||||
|
||||
make_gossip_message_and_header(api, relay_parent, validator_index).await
|
||||
}
|
||||
|
||||
async fn make_gossip_message_and_header(
|
||||
api: Arc<TestApi>,
|
||||
relay_parent: H256,
|
||||
validator_index: u32,
|
||||
) -> (SignedFullStatement, Header) {
|
||||
let keystore: SyncCryptoStorePtr = Arc::new(KeyStore::new());
|
||||
let alice_public = SyncCryptoStore::sr25519_generate_new(
|
||||
&*keystore,
|
||||
ValidatorId::ID,
|
||||
Some(&Sr25519Keyring::Alice.to_seed()),
|
||||
)
|
||||
.unwrap();
|
||||
let session_index = api
|
||||
.runtime_api()
|
||||
.session_index_for_child(&BlockId::Hash(relay_parent))
|
||||
.unwrap();
|
||||
let signing_context = SigningContext {
|
||||
parent_hash: relay_parent,
|
||||
session_index,
|
||||
};
|
||||
|
||||
let header = default_header();
|
||||
let candidate_receipt = CommittedCandidateReceipt {
|
||||
commitments: CandidateCommitments {
|
||||
head_data: header.encode().into(),
|
||||
..Default::default()
|
||||
},
|
||||
descriptor: CandidateDescriptor {
|
||||
relay_parent,
|
||||
para_head: polkadot_parachain::primitives::HeadData(header.encode()).hash(),
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
let statement = Statement::Seconded(candidate_receipt);
|
||||
let signed = SignedFullStatement::sign(
|
||||
&keystore,
|
||||
statement,
|
||||
&signing_context,
|
||||
validator_index,
|
||||
&alice_public.into(),
|
||||
)
|
||||
.await
|
||||
.expect("Signing statement");
|
||||
|
||||
(signed, header)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_if_no_data_and_less_than_best_known_number() {
|
||||
let mut validator = make_validator_and_api().0;
|
||||
let header = Header {
|
||||
number: 0,
|
||||
..default_header()
|
||||
};
|
||||
let res = block_on(validator.validate(&header, &[]));
|
||||
|
||||
assert_eq!(
|
||||
res.unwrap(),
|
||||
Validation::Success { is_new_best: false },
|
||||
"validating without data with block number < best known number is always a success",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_if_no_data_exceeds_best_known_number() {
|
||||
let mut validator = make_validator_and_api().0;
|
||||
let header = Header {
|
||||
number: 1,
|
||||
..default_header()
|
||||
};
|
||||
let res = block_on(validator.validate(&header, &[]));
|
||||
|
||||
assert_eq!(
|
||||
res.unwrap(),
|
||||
Validation::Failure { disconnect: false },
|
||||
"validation fails if no justification and block number >= best known number",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_statement_is_encoded_correctly() {
|
||||
let mut validator = make_validator_and_api().0;
|
||||
let header = default_header();
|
||||
let res = block_on(validator.validate(&header, &[0x42]))
|
||||
.err()
|
||||
.expect("Should fail on invalid encoded statement");
|
||||
|
||||
check_error(res, |error| {
|
||||
matches!(
|
||||
error,
|
||||
BlockAnnounceError(x) if x.contains("Can not decode the `BlockAnnounceData`")
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_signer_is_legit_validator() {
|
||||
let (mut validator, api) = make_validator_and_api();
|
||||
|
||||
let (signed_statement, header) = block_on(make_gossip_message_and_header_using_genesis(api, 1));
|
||||
let data = BlockAnnounceData::try_from(signed_statement).unwrap().encode();
|
||||
|
||||
let res = block_on(validator.validate(&header, &data));
|
||||
assert_eq!(Validation::Failure { disconnect: true }, res.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_statement_is_correctly_signed() {
|
||||
let (mut validator, api) = make_validator_and_api();
|
||||
|
||||
let (signed_statement, header) = block_on(make_gossip_message_and_header_using_genesis(api, 0));
|
||||
|
||||
let mut data = BlockAnnounceData::try_from(signed_statement).unwrap().encode();
|
||||
|
||||
// The signature comes at the end of the type, so change a bit to make the signature invalid.
|
||||
let last = data.len() - 1;
|
||||
data[last] = data[last].wrapping_add(1);
|
||||
|
||||
let res = block_on(validator.validate(&header, &data));
|
||||
assert_eq!(Validation::Failure { disconnect: true }, res.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_statement_seconded() {
|
||||
let (mut validator, api) = make_validator_and_api();
|
||||
let header = default_header();
|
||||
let relay_parent = H256::from_low_u64_be(1);
|
||||
|
||||
let keystore: SyncCryptoStorePtr = Arc::new(KeyStore::new());
|
||||
let alice_public = SyncCryptoStore::sr25519_generate_new(
|
||||
&*keystore,
|
||||
ValidatorId::ID,
|
||||
Some(&Sr25519Keyring::Alice.to_seed()),
|
||||
)
|
||||
.unwrap();
|
||||
let session_index = api
|
||||
.runtime_api()
|
||||
.session_index_for_child(&BlockId::Hash(relay_parent))
|
||||
.unwrap();
|
||||
let signing_context = SigningContext {
|
||||
parent_hash: relay_parent,
|
||||
session_index,
|
||||
};
|
||||
|
||||
let statement = Statement::Valid(Default::default());
|
||||
|
||||
let signed_statement = block_on(SignedFullStatement::sign(
|
||||
&keystore,
|
||||
statement,
|
||||
&signing_context,
|
||||
0,
|
||||
&alice_public.into(),
|
||||
))
|
||||
.expect("Signs statement");
|
||||
let data = BlockAnnounceData {
|
||||
receipt: Default::default(),
|
||||
statement: signed_statement.convert_payload(),
|
||||
}.encode();
|
||||
|
||||
let res = block_on(validator.validate(&header, &data));
|
||||
assert_eq!(Validation::Failure { disconnect: true }, res.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_header_match_candidate_receipt_header() {
|
||||
let (mut validator, api) = make_validator_and_api();
|
||||
|
||||
let (signed_statement, mut header) =
|
||||
block_on(make_gossip_message_and_header_using_genesis(api, 0));
|
||||
let data = BlockAnnounceData::try_from(signed_statement).unwrap().encode();
|
||||
header.number = 300;
|
||||
|
||||
let res = block_on(validator.validate(&header, &data));
|
||||
assert_eq!(Validation::Failure { disconnect: true }, res.unwrap());
|
||||
}
|
||||
|
||||
/// Test that ensures that we postpone the block announce verification until
|
||||
/// a relay chain block is imported. This is important for when we receive a
|
||||
/// block announcement before we have imported the associated relay chain block
|
||||
/// which can happen on slow nodes or nodes with a slow network connection.
|
||||
#[test]
|
||||
fn relay_parent_not_imported_when_block_announce_is_processed() {
|
||||
block_on(async move {
|
||||
let (mut validator, api) = make_validator_and_api();
|
||||
|
||||
let mut client = api.relay_client.clone();
|
||||
let block = client
|
||||
.init_polkadot_block_builder()
|
||||
.build()
|
||||
.expect("Build new block")
|
||||
.block;
|
||||
|
||||
let (signed_statement, header) = make_gossip_message_and_header(api, block.hash(), 0).await;
|
||||
|
||||
let data = BlockAnnounceData::try_from(signed_statement).unwrap().encode();
|
||||
|
||||
let mut validation = validator.validate(&header, &data);
|
||||
|
||||
// The relay chain block is not available yet, so the first poll should return
|
||||
// that the future is still pending.
|
||||
assert!(poll!(&mut validation).is_pending());
|
||||
|
||||
client
|
||||
.import(BlockOrigin::Own, block)
|
||||
.expect("Imports the block");
|
||||
|
||||
assert!(matches!(
|
||||
poll!(validation),
|
||||
Poll::Ready(Ok(Validation::Success { is_new_best: true }))
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ApiData {
|
||||
validators: Vec<ValidatorId>,
|
||||
}
|
||||
|
||||
struct TestApi {
|
||||
data: Arc<ApiData>,
|
||||
relay_client: Arc<PClient>,
|
||||
relay_backend: Arc<PBackend>,
|
||||
}
|
||||
|
||||
impl TestApi {
|
||||
fn new() -> Self {
|
||||
let builder = TestClientBuilder::new();
|
||||
let relay_backend = builder.backend();
|
||||
|
||||
Self {
|
||||
data: Arc::new(ApiData {
|
||||
validators: vec![Sr25519Keyring::Alice.public().into()],
|
||||
}),
|
||||
relay_client: Arc::new(builder.build()),
|
||||
relay_backend,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct RuntimeApi {
|
||||
data: Arc<ApiData>,
|
||||
}
|
||||
|
||||
impl ProvideRuntimeApi<PBlock> for TestApi {
|
||||
type Api = RuntimeApi;
|
||||
|
||||
fn runtime_api<'a>(&'a self) -> ApiRef<'a, Self::Api> {
|
||||
RuntimeApi {
|
||||
data: self.data.clone(),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
sp_api::mock_impl_runtime_apis! {
|
||||
impl ParachainHost<PBlock> for RuntimeApi {
|
||||
type Error = sp_blockchain::Error;
|
||||
|
||||
fn validators(&self) -> Vec<ValidatorId> {
|
||||
self.data.validators.clone()
|
||||
}
|
||||
|
||||
fn validator_groups(&self) -> (Vec<Vec<ValidatorIndex>>, GroupRotationInfo<BlockNumber>) {
|
||||
(Vec::new(), GroupRotationInfo { session_start_block: 0, group_rotation_frequency: 0, now: 0 })
|
||||
}
|
||||
|
||||
fn availability_cores(&self) -> Vec<CoreState<PHash>> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn persisted_validation_data(&self, _: ParaId, _: OccupiedCoreAssumption) -> Option<PersistedValidationData<BlockNumber>> {
|
||||
Some(PersistedValidationData {
|
||||
parent_head: HeadData(default_header().encode()),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
fn session_index_for_child(&self) -> SessionIndex {
|
||||
0
|
||||
}
|
||||
|
||||
fn validation_code(&self, _: ParaId, _: OccupiedCoreAssumption) -> Option<ValidationCode> {
|
||||
None
|
||||
}
|
||||
|
||||
fn candidate_pending_availability(&self, _: ParaId) -> Option<CommittedCandidateReceipt<PHash>> {
|
||||
None
|
||||
}
|
||||
|
||||
fn candidate_events(&self) -> Vec<CandidateEvent<PHash>> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn session_info(_: SessionIndex) -> Option<SessionInfo> {
|
||||
None
|
||||
}
|
||||
|
||||
fn check_validation_outputs(_: ParaId, _: CandidateCommitments) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn dmq_contents(_: ParaId) -> Vec<InboundDownwardMessage<BlockNumber>> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn historical_validation_code(_: ParaId, _: BlockNumber) -> Option<ValidationCode> {
|
||||
None
|
||||
}
|
||||
|
||||
fn inbound_hrmp_channels_contents(
|
||||
_: ParaId,
|
||||
) -> BTreeMap<ParaId, Vec<InboundHrmpMessage<BlockNumber>>> {
|
||||
BTreeMap::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
// Copyright 2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Provides the [`WaitOnRelayChainBlock`] type.
|
||||
|
||||
use futures::{future::ready, Future, FutureExt, StreamExt};
|
||||
use polkadot_primitives::v1::{Block as PBlock, Hash as PHash};
|
||||
use sc_client_api::{
|
||||
blockchain::{self, BlockStatus, HeaderBackend},
|
||||
Backend, BlockchainEvents,
|
||||
};
|
||||
use sp_runtime::{generic::BlockId, traits::HashFor};
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
/// The timeout in seconds after that the waiting for a block should be aborted.
|
||||
const TIMEOUT_IN_SECONDS: u64 = 6;
|
||||
|
||||
/// Custom error type used by [`WaitOnRelayChainBlock`].
|
||||
#[derive(Debug, derive_more::Display)]
|
||||
pub enum Error {
|
||||
#[display(
|
||||
fmt = "Timeout while waiting for relay-chain block `{}` to be imported.",
|
||||
_0
|
||||
)]
|
||||
Timeout(PHash),
|
||||
#[display(
|
||||
fmt = "Import listener closed while waiting for relay-chain block `{}` to be imported.",
|
||||
_0
|
||||
)]
|
||||
ImportListenerClosed(PHash),
|
||||
#[display(
|
||||
fmt = "Blockchain returned an error while waiting for relay-chain block `{}` to be imported: {:?}",
|
||||
_0,
|
||||
_1
|
||||
)]
|
||||
BlockchainError(PHash, blockchain::Error),
|
||||
}
|
||||
|
||||
/// A helper to wait for a given relay chain block in an async way.
|
||||
///
|
||||
/// The caller needs to pass the hash of a block it waits for and the function will return when the
|
||||
/// block is available or an error occurred.
|
||||
///
|
||||
/// The waiting for the block is implemented as follows:
|
||||
///
|
||||
/// 1. Get a read lock on the import lock from the backend.
|
||||
///
|
||||
/// 2. Check if the block is already imported. If yes, return from the function.
|
||||
///
|
||||
/// 3. If the block isn't imported yet, add an import notification listener.
|
||||
///
|
||||
/// 4. Poll the import notification listener until the block is imported or the timeout is fired.
|
||||
///
|
||||
/// The timeout is set to 6 seconds. This should be enough time to import the block in the current
|
||||
/// round and if not, the new round of the relay chain already started anyway.
|
||||
pub struct WaitOnRelayChainBlock<B, BCE> {
|
||||
block_chain_events: Arc<BCE>,
|
||||
backend: Arc<B>,
|
||||
}
|
||||
|
||||
impl<B, BCE> Clone for WaitOnRelayChainBlock<B, BCE> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
backend: self.backend.clone(),
|
||||
block_chain_events: self.block_chain_events.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, BCE> WaitOnRelayChainBlock<B, BCE> {
|
||||
/// Creates a new instance of `Self`.
|
||||
pub fn new(backend: Arc<B>, block_chain_events: Arc<BCE>) -> Self {
|
||||
Self {
|
||||
backend,
|
||||
block_chain_events,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, BCE> WaitOnRelayChainBlock<B, BCE>
|
||||
where
|
||||
B: Backend<PBlock>,
|
||||
BCE: BlockchainEvents<PBlock>,
|
||||
// Rust bug: https://github.com/rust-lang/rust/issues/24159
|
||||
sc_client_api::StateBackendFor<B, PBlock>: sc_client_api::StateBackend<HashFor<PBlock>>,
|
||||
{
|
||||
pub fn wait_on_relay_chain_block(
|
||||
&self,
|
||||
hash: PHash,
|
||||
) -> impl Future<Output = Result<(), Error>> {
|
||||
let _lock = self.backend.get_import_lock().read();
|
||||
match self.backend.blockchain().status(BlockId::Hash(hash)) {
|
||||
Ok(BlockStatus::InChain) => {
|
||||
return ready(Ok(())).boxed();
|
||||
}
|
||||
Err(err) => return ready(Err(Error::BlockchainError(hash, err))).boxed(),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let mut listener = self.block_chain_events.import_notification_stream();
|
||||
// Now it is safe to drop the lock, even when the block is now imported, it should show
|
||||
// up in our registered listener.
|
||||
drop(_lock);
|
||||
|
||||
let mut timeout = futures_timer::Delay::new(Duration::from_secs(TIMEOUT_IN_SECONDS)).fuse();
|
||||
|
||||
async move {
|
||||
loop {
|
||||
futures::select! {
|
||||
_ = timeout => return Err(Error::Timeout(hash)),
|
||||
evt = listener.next() => match evt {
|
||||
Some(evt) if evt.hash == hash => return Ok(()),
|
||||
// Not the event we waited on.
|
||||
Some(_) => continue,
|
||||
None => return Err(Error::ImportListenerClosed(hash)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use polkadot_test_client::{
|
||||
construct_transfer_extrinsic, BlockBuilderExt, Client, ClientBlockImportExt,
|
||||
DefaultTestClientBuilderExt, ExecutionStrategy, FullBackend, InitPolkadotBlockBuilder,
|
||||
TestClientBuilder, TestClientBuilderExt,
|
||||
};
|
||||
use sp_consensus::BlockOrigin;
|
||||
use sp_runtime::traits::Block as BlockT;
|
||||
|
||||
use futures::{executor::block_on, poll, task::Poll};
|
||||
|
||||
fn build_client_backend_and_block() -> (Arc<Client>, Arc<FullBackend>, PBlock) {
|
||||
let builder =
|
||||
TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible);
|
||||
let backend = builder.backend();
|
||||
let client = Arc::new(builder.build());
|
||||
|
||||
let block_builder = client.init_polkadot_block_builder();
|
||||
let block = block_builder.build().expect("Finalizes the block").block;
|
||||
|
||||
(client, backend, block)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_directly_for_available_block() {
|
||||
let (mut client, backend, block) = build_client_backend_and_block();
|
||||
let hash = block.hash();
|
||||
|
||||
client
|
||||
.import(BlockOrigin::Own, block)
|
||||
.expect("Imports the block");
|
||||
|
||||
let wait = WaitOnRelayChainBlock::new(backend, client);
|
||||
|
||||
block_on(async move {
|
||||
// Should be ready on the first poll
|
||||
assert!(matches!(
|
||||
poll!(wait.wait_on_relay_chain_block(hash)),
|
||||
Poll::Ready(Ok(()))
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_after_block_import_notification_was_received() {
|
||||
let (mut client, backend, block) = build_client_backend_and_block();
|
||||
let hash = block.hash();
|
||||
|
||||
let wait = WaitOnRelayChainBlock::new(backend, client.clone());
|
||||
|
||||
block_on(async move {
|
||||
let mut future = wait.wait_on_relay_chain_block(hash);
|
||||
// As the block is not yet imported, the first poll should return `Pending`
|
||||
assert!(poll!(&mut future).is_pending());
|
||||
|
||||
// Import the block that should fire the notification
|
||||
client
|
||||
.import(BlockOrigin::Own, block)
|
||||
.expect("Imports the block");
|
||||
|
||||
// Now it should have received the notification and report that the block was imported
|
||||
assert!(matches!(poll!(future), Poll::Ready(Ok(()))));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wait_for_block_time_out_when_block_is_not_imported() {
|
||||
let (client, backend, block) = build_client_backend_and_block();
|
||||
let hash = block.hash();
|
||||
|
||||
let wait = WaitOnRelayChainBlock::new(backend, client.clone());
|
||||
|
||||
assert!(matches!(
|
||||
block_on(wait.wait_on_relay_chain_block(hash)),
|
||||
Err(Error::Timeout(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn do_not_resolve_after_different_block_import_notification_was_received() {
|
||||
let (mut client, backend, block) = build_client_backend_and_block();
|
||||
let hash = block.hash();
|
||||
|
||||
let ext = construct_transfer_extrinsic(
|
||||
&*client,
|
||||
sp_keyring::Sr25519Keyring::Alice,
|
||||
sp_keyring::Sr25519Keyring::Bob,
|
||||
1000,
|
||||
);
|
||||
let mut block_builder = client.init_polkadot_block_builder();
|
||||
// Push an extrinsic to get a different block hash.
|
||||
block_builder
|
||||
.push_polkadot_extrinsic(ext)
|
||||
.expect("Push extrinsic");
|
||||
let block2 = block_builder.build().expect("Build second block").block;
|
||||
let hash2 = block2.hash();
|
||||
|
||||
let wait = WaitOnRelayChainBlock::new(backend, client.clone());
|
||||
|
||||
block_on(async move {
|
||||
let mut future = wait.wait_on_relay_chain_block(hash);
|
||||
let mut future2 = wait.wait_on_relay_chain_block(hash2);
|
||||
// As the block is not yet imported, the first poll should return `Pending`
|
||||
assert!(poll!(&mut future).is_pending());
|
||||
assert!(poll!(&mut future2).is_pending());
|
||||
|
||||
// Import the block that should fire the notification
|
||||
client
|
||||
.import(BlockOrigin::Own, block2)
|
||||
.expect("Imports the second block");
|
||||
|
||||
// The import notification of the second block should not make this one finish
|
||||
assert!(poll!(&mut future).is_pending());
|
||||
// Now it should have received the notification and report that the block was imported
|
||||
assert!(matches!(poll!(future2), Poll::Ready(Ok(()))));
|
||||
|
||||
client
|
||||
.import(BlockOrigin::Own, block)
|
||||
.expect("Imports the first block");
|
||||
|
||||
// Now it should be ready
|
||||
assert!(matches!(poll!(future), Poll::Ready(Ok(()))));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user