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

Add method with_default_block_announcer to ServiceBuilder #5797

Merged
2 commits merged into from
Apr 27, 2020

Conversation

cecton
Copy link
Contributor

@cecton cecton commented Apr 27, 2020

This is a small change that will allow changing the default block announcer when building a service.

This PR is relevant for the issue: paritytech/cumulus#7 as we need to set a custom default block announcer.

@cecton cecton self-assigned this Apr 27, 2020
@parity-cla-bot
Copy link

It looks like @cecton signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

@gnunicorn
Copy link
Contributor

@cecton I assume this relevant for cumulus?

@cecton
Copy link
Contributor Author

cecton commented Apr 27, 2020

@cecton I assume this relevant for cumulus?

Yes! Sorry I forgot to mention that. I will update the PR description

pub fn with_block_announce_validator(
self,
block_announce_validator_builder:
impl FnOnce(Arc<TCl>) -> Box<dyn BlockAnnounceValidator<TBl> + Send> + 'static,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should actually return a Result. That's how it's done in all the others and I guess it's for the same reasons than sc-cli's SubstrateCli having a lot of Result everywhere too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type is pretty complex here, maybe traitify this and then implement the trait for every FnOnce() -> ... ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also why isn't this just a generic type on ServiceBuidler level with static new function instead of a builder? Is there some dynamic state we expect to be transferred with the closure?

fn with_block_announce_validator<TBAV: BlockAnnounceValidator<TCl, TBl>>(self) -> ServiceBuilder<TBAV, TBl, ...> {
 ... // copy over the fields.
}

trait BlockAnnounceValidator<TCl, B> {
   fn new(client: Arc<TCl>) -> Self;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. There are 2 reasons for that:

  1. In my use case for cumulus (see issue in description) we want to use a custom validator JustifiedBlockAnnounceValidator and as you can see the arguments for new() is actually something custom. I assume this can be personalized by the parachain as they want

  2. On substrate you can clearly see that this has been done with this intention because the function new is not in the trait.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To stay consistent with the existing API, I'd keep it this way for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that "staying consistent with the existing API" meant changing it to return a Result

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Changing the closure to return a Result? Yes that's what I meant)

) -> Result<ServiceBuilder<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp,
TExPool, TRpc, Backend>, Error>
where TSc: Clone, TFchr: Clone {
Ok(ServiceBuilder {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and the Result here makes no sense if the closure in input is not called right away. @gnunicorn what do you think I should do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for consistency with the current implementation of the servicebuilder it is best to call the closure directly and use a Result in the closure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this isn't pretty, but okay for now. And as we want to move to deferring calls (see #4587 ) we need to refactor this things alltogether soon anyways.

Copy link
Contributor

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels a bit messy with the FnOnce stuff, but I'm not an expert in this part of the codebase, so don't know if it can be done better. Also missing some context on the purpose.

pub fn with_block_announce_validator(
self,
block_announce_validator_builder:
impl FnOnce(Arc<TCl>) -> Box<dyn BlockAnnounceValidator<TBl> + Send> + 'static,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type is pretty complex here, maybe traitify this and then implement the trait for every FnOnce() -> ... ?

pub fn with_block_announce_validator(
self,
block_announce_validator_builder:
impl FnOnce(Arc<TCl>) -> Box<dyn BlockAnnounceValidator<TBl> + Send> + 'static,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also why isn't this just a generic type on ServiceBuidler level with static new function instead of a builder? Is there some dynamic state we expect to be transferred with the closure?

fn with_block_announce_validator<TBAV: BlockAnnounceValidator<TCl, TBl>>(self) -> ServiceBuilder<TBAV, TBl, ...> {
 ... // copy over the fields.
}

trait BlockAnnounceValidator<TCl, B> {
   fn new(client: Arc<TCl>) -> Self;
}

Copy link
Contributor

@NikVolf NikVolf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomaka will probably say again that he does not like this api

Copy link
Contributor

@gnunicorn gnunicorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. this builder needs to be refactored anyways ...

) -> Result<ServiceBuilder<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp,
TExPool, TRpc, Backend>, Error>
where TSc: Clone, TFchr: Clone {
Ok(ServiceBuilder {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this isn't pretty, but okay for now. And as we want to move to deferring calls (see #4587 ) we need to refactor this things alltogether soon anyways.

pub fn with_block_announce_validator(
self,
block_announce_validator_builder:
impl FnOnce(Arc<TCl>) -> Box<dyn BlockAnnounceValidator<TBl> + Send> + 'static,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To stay consistent with the existing API, I'd keep it this way for now.

@gnunicorn
Copy link
Contributor

bot merge

@ghost ghost merged commit 441beb2 into master Apr 27, 2020
@ghost ghost deleted the cecton-service-builder-block-announce-validator branch April 27, 2020 15:05
@cecton
Copy link
Contributor Author

cecton commented Apr 27, 2020

@tomaka will probably say again that he does not like this api

haha 😝 me neither. I wanted to change it but I had other things to do... 😞

@tomaka
Copy link
Contributor

tomaka commented Apr 27, 2020

@tomaka will probably say again that he does not like this api

Repeating it every time is probably the most efficient way to eventually have it changed.

This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants