Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
reenvision the subsystem requests as an extension trait
Browse files Browse the repository at this point in the history
This works within `util.rs`, but fails in `core/backing/src/lib.rs`,
because we don't actually create the struct soon enough. Continuing
down this path would imply substantial rewriting.
  • Loading branch information
coriolinus committed Jul 13, 2020
1 parent 802981a commit a5639e3
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 154 deletions.
74 changes: 17 additions & 57 deletions node/core/backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ use polkadot_subsystem::{
messages::{
AllMessages, AvailabilityStoreMessage, CandidateBackingMessage, CandidateSelectionMessage,
CandidateValidationMessage, NewBackedCandidate, PoVDistributionMessage, ProvisionableData,
ProvisionerMessage, RuntimeApiMessage, StatementDistributionMessage, ValidationFailed,
ProvisionerMessage, RuntimeApiMessage, RuntimeApiRequest, StatementDistributionMessage, ValidationFailed,
},
util::{
self,
request_signing_context,
request_validator_groups,
request_validators,
JobTrait,
JobTraitExt,
Validator,
},
};
Expand Down Expand Up @@ -198,23 +197,6 @@ impl From<FromJob> for AllMessages {
}
}

impl TryFrom<AllMessages> for FromJob {
type Error = &'static str;

fn try_from(f: AllMessages) -> Result<Self, Self::Error> {
match f {
AllMessages::AvailabilityStore(msg) => Ok(FromJob::AvailabilityStore(msg)),
AllMessages::RuntimeApi(msg) => Ok(FromJob::RuntimeApiMessage(msg)),
AllMessages::CandidateValidation(msg) => Ok(FromJob::CandidateValidation(msg)),
AllMessages::CandidateSelection(msg) => Ok(FromJob::CandidateSelection(msg)),
AllMessages::StatementDistribution(msg) => Ok(FromJob::StatementDistribution(msg)),
AllMessages::PoVDistribution(msg) => Ok(FromJob::PoVDistribution(msg)),
AllMessages::Provisioner(msg) => Ok(FromJob::Provisioner(msg)),
_ => Err("can't convert this AllMessages variant to FromJob"),
}
}
}

// It looks like it's not possible to do an `impl From` given the current state of
// the code. So this does the necessary conversion.
fn primitive_statement_to_table(s: &SignedFullStatement) -> TableSignedStatement {
Expand Down Expand Up @@ -547,38 +529,6 @@ impl CandidateBackingJob {
Ok(())
}

async fn request_pov_from_distribution(
&mut self,
descriptor: CandidateDescriptor,
) -> Result<Arc<PoV>, Error> {
let (tx, rx) = oneshot::channel();

self.tx_from.send(FromJob::PoVDistribution(
PoVDistributionMessage::FetchPoV(self.parent, descriptor, tx)
)).await?;

Ok(rx.await?)
}

async fn request_candidate_validation(
&mut self,
candidate: CandidateDescriptor,
pov: Arc<PoV>,
) -> Result<ValidationResult, Error> {
let (tx, rx) = oneshot::channel();

self.tx_from.send(FromJob::CandidateValidation(
CandidateValidationMessage::ValidateFromChainState(
candidate,
pov,
tx,
)
)
).await?;

Ok(rx.await??)
}

async fn store_chunk(
&mut self,
id: ValidatorIndex,
Expand Down Expand Up @@ -656,7 +606,7 @@ impl CandidateBackingJob {
}
}

impl util::JobTrait for CandidateBackingJob {
impl JobTrait for CandidateBackingJob {
type ToJob = ToJob;
type FromJob = FromJob;
type Error = Error;
Expand All @@ -672,9 +622,9 @@ impl util::JobTrait for CandidateBackingJob {
) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send>> {
async move {
let (validators, roster, signing_context) = futures::try_join!(
request_validators(parent, &mut tx_from).await?,
request_validator_groups(parent, &mut tx_from).await?,
request_signing_context(parent, &mut tx_from).await?,
self.request_validators().await?,
self.request_validator_groups().await?,
self.request_signing_context().await?,
)?;

let validator = Validator::construct(&validators, signing_context, keystore.clone())?;
Expand Down Expand Up @@ -724,6 +674,16 @@ impl util::JobTrait for CandidateBackingJob {
}
}

impl JobTraitExt for CandidateBackingJob {
fn sender(&self) -> mpsc::Sender<Self::FromJob> {
self.tx_from.clone()
}

fn relay_parent(&self) -> Hash {
self.parent
}
}

/// An implementation of the Candidate Backing subsystem.
pub type CandidateBackingSubsystem<Spawner, Context> =
util::JobManager<Spawner, Context, CandidateBackingJob>;
Expand Down
Loading

0 comments on commit a5639e3

Please sign in to comment.