mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-17 21:51:06 +00:00
Make produce_candidate return an Option (#1309)
* Make `produce_candidate` return an `Option` Instead of `produce_candidate` returning a `Result`, it should return an `Option`. The only supported error was `InvalidHead` anyway and Cumulus will take care to print appropriate information on what failed and Polkadot can just ignore it. * Fix warning
This commit is contained in:
@@ -50,7 +50,7 @@ use std::sync::Arc;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|
||||||
use futures::{future, Future, Stream, FutureExt, TryFutureExt, StreamExt, task::Spawn};
|
use futures::{future, Future, Stream, FutureExt, StreamExt, task::Spawn};
|
||||||
use log::warn;
|
use log::warn;
|
||||||
use sc_client_api::{StateBackend, BlockchainEvents};
|
use sc_client_api::{StateBackend, BlockchainEvents};
|
||||||
use sp_blockchain::HeaderBackend;
|
use sp_blockchain::HeaderBackend;
|
||||||
@@ -100,24 +100,17 @@ impl Network for polkadot_network::protocol::Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Error to return when the head data was invalid.
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
|
||||||
pub struct InvalidHead;
|
|
||||||
|
|
||||||
/// Collation errors.
|
/// Collation errors.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// Error on the relay-chain side of things.
|
/// Error on the relay-chain side of things.
|
||||||
Polkadot(String),
|
Polkadot(String),
|
||||||
/// Error on the collator side of things.
|
|
||||||
Collator(InvalidHead),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
Error::Polkadot(ref err) => write!(f, "Polkadot node error: {}", err),
|
Error::Polkadot(ref err) => write!(f, "Polkadot node error: {}", err),
|
||||||
Error::Collator(_) => write!(f, "Collator node error: Invalid head data"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,7 +140,7 @@ pub trait BuildParachainContext {
|
|||||||
/// This can be implemented through an externally attached service or a stub.
|
/// This can be implemented through an externally attached service or a stub.
|
||||||
/// This is expected to be a lightweight, shared type like an Arc.
|
/// This is expected to be a lightweight, shared type like an Arc.
|
||||||
pub trait ParachainContext: Clone {
|
pub trait ParachainContext: Clone {
|
||||||
type ProduceCandidate: Future<Output = Result<(BlockData, HeadData), InvalidHead>>;
|
type ProduceCandidate: Future<Output = Option<(BlockData, HeadData)>>;
|
||||||
|
|
||||||
/// Produce a candidate, given the relay parent hash, the latest ingress queue information
|
/// Produce a candidate, given the relay parent hash, the latest ingress queue information
|
||||||
/// and the last parachain head.
|
/// and the last parachain head.
|
||||||
@@ -167,8 +160,7 @@ pub async fn collate<P>(
|
|||||||
local_validation_data: LocalValidationData,
|
local_validation_data: LocalValidationData,
|
||||||
mut para_context: P,
|
mut para_context: P,
|
||||||
key: Arc<CollatorPair>,
|
key: Arc<CollatorPair>,
|
||||||
)
|
) -> Option<parachain::Collation>
|
||||||
-> Result<parachain::Collation, Error>
|
|
||||||
where
|
where
|
||||||
P: ParachainContext,
|
P: ParachainContext,
|
||||||
P::ProduceCandidate: Send,
|
P::ProduceCandidate: Send,
|
||||||
@@ -177,7 +169,7 @@ pub async fn collate<P>(
|
|||||||
relay_parent,
|
relay_parent,
|
||||||
global_validation,
|
global_validation,
|
||||||
local_validation_data,
|
local_validation_data,
|
||||||
).map_err(Error::Collator).await?;
|
).await?;
|
||||||
|
|
||||||
let pov_block = PoVBlock {
|
let pov_block = PoVBlock {
|
||||||
block_data,
|
block_data,
|
||||||
@@ -204,7 +196,7 @@ pub async fn collate<P>(
|
|||||||
pov: pov_block,
|
pov: pov_block,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(collation)
|
Some(collation)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "service-rewr")]
|
#[cfg(feature = "service-rewr")]
|
||||||
@@ -341,8 +333,13 @@ fn build_collator_service<SP, P, C, R, Extrinsic>(
|
|||||||
local_validation,
|
local_validation,
|
||||||
parachain_context,
|
parachain_context,
|
||||||
key,
|
key,
|
||||||
).map_ok(move |collation| {
|
).map(move |collation| {
|
||||||
network.distribute_collation(targets, collation)
|
match collation {
|
||||||
|
Some(collation) => network.distribute_collation(targets, collation),
|
||||||
|
None => log::trace!("Skipping collation as `collate` returned `None`"),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
});
|
});
|
||||||
|
|
||||||
future::Either::Right(collation_work)
|
future::Either::Right(collation_work)
|
||||||
@@ -470,7 +467,7 @@ mod tests {
|
|||||||
struct DummyParachainContext;
|
struct DummyParachainContext;
|
||||||
|
|
||||||
impl ParachainContext for DummyParachainContext {
|
impl ParachainContext for DummyParachainContext {
|
||||||
type ProduceCandidate = future::Ready<Result<(BlockData, HeadData), InvalidHead>>;
|
type ProduceCandidate = future::Ready<Option<(BlockData, HeadData)>>;
|
||||||
|
|
||||||
fn produce_candidate(
|
fn produce_candidate(
|
||||||
&mut self,
|
&mut self,
|
||||||
@@ -479,10 +476,10 @@ mod tests {
|
|||||||
_local_validation: LocalValidationData,
|
_local_validation: LocalValidationData,
|
||||||
) -> Self::ProduceCandidate {
|
) -> Self::ProduceCandidate {
|
||||||
// send messages right back.
|
// send messages right back.
|
||||||
future::ok((
|
future::ready(Some((
|
||||||
BlockData(vec![1, 2, 3, 4, 5,]),
|
BlockData(vec![1, 2, 3, 4, 5,]),
|
||||||
HeadData(vec![9, 9, 9]),
|
HeadData(vec![9, 9, 9]),
|
||||||
))
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,11 +26,9 @@ use primitives::{
|
|||||||
Hash,
|
Hash,
|
||||||
parachain::{HeadData, BlockData, Id as ParaId, LocalValidationData, GlobalValidationSchedule},
|
parachain::{HeadData, BlockData, Id as ParaId, LocalValidationData, GlobalValidationSchedule},
|
||||||
};
|
};
|
||||||
use collator::{
|
use collator::{ParachainContext, Network, BuildParachainContext, Cli, SubstrateCli};
|
||||||
InvalidHead, ParachainContext, Network, BuildParachainContext, Cli, SubstrateCli,
|
|
||||||
};
|
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use futures::future::{Ready, ok, err, TryFutureExt};
|
use futures::future::{Ready, ready, TryFutureExt};
|
||||||
|
|
||||||
const GENESIS: AdderHead = AdderHead {
|
const GENESIS: AdderHead = AdderHead {
|
||||||
number: 0,
|
number: 0,
|
||||||
@@ -55,7 +53,7 @@ struct AdderContext {
|
|||||||
|
|
||||||
/// The parachain context.
|
/// The parachain context.
|
||||||
impl ParachainContext for AdderContext {
|
impl ParachainContext for AdderContext {
|
||||||
type ProduceCandidate = Ready<Result<(BlockData, HeadData), InvalidHead>>;
|
type ProduceCandidate = Ready<Option<(BlockData, HeadData)>>;
|
||||||
|
|
||||||
fn produce_candidate(
|
fn produce_candidate(
|
||||||
&mut self,
|
&mut self,
|
||||||
@@ -64,9 +62,9 @@ impl ParachainContext for AdderContext {
|
|||||||
local_validation: LocalValidationData,
|
local_validation: LocalValidationData,
|
||||||
) -> Self::ProduceCandidate
|
) -> Self::ProduceCandidate
|
||||||
{
|
{
|
||||||
let adder_head = match AdderHead::decode(&mut &local_validation.parent_head.0[..]) {
|
let adder_head = match AdderHead::decode(&mut &local_validation.parent_head.0[..]).ok() {
|
||||||
Ok(adder_head) => adder_head,
|
Some(res) => res,
|
||||||
Err(_) => return err(InvalidHead)
|
None => return ready(None),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut db = self.db.lock();
|
let mut db = self.db.lock();
|
||||||
@@ -94,7 +92,7 @@ impl ParachainContext for AdderContext {
|
|||||||
next_head.number, next_body.state.overflowing_add(next_body.add).0);
|
next_head.number, next_body.state.overflowing_add(next_body.add).0);
|
||||||
|
|
||||||
db.insert(next_head.clone(), next_body);
|
db.insert(next_head.clone(), next_body);
|
||||||
ok((encoded_body, encoded_head))
|
ready(Some((encoded_body, encoded_head)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user