-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add method with_default_block_announcer to ServiceBuilder #5797
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,10 @@ use sc_client_api::{ | |
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedSender}; | ||
use sc_client::{Client, ClientConfig}; | ||
use sc_chain_spec::get_extension; | ||
use sp_consensus::import_queue::ImportQueue; | ||
use sp_consensus::{ | ||
block_validation::{BlockAnnounceValidator, DefaultBlockAnnounceValidator}, | ||
import_queue::ImportQueue, | ||
}; | ||
use futures::{ | ||
Future, FutureExt, StreamExt, | ||
future::ready, | ||
|
@@ -93,6 +96,7 @@ pub struct ServiceBuilder<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, | |
remote_backend: Option<Arc<dyn RemoteBlockchain<TBl>>>, | ||
marker: PhantomData<(TBl, TRtApi)>, | ||
background_tasks: Vec<(&'static str, BackgroundTask)>, | ||
block_announce_validator_builder: Option<Box<dyn FnOnce(Arc<TCl>) -> Box<dyn BlockAnnounceValidator<TBl> + Send>>>, | ||
} | ||
|
||
/// Full client type. | ||
|
@@ -261,6 +265,7 @@ impl ServiceBuilder<(), (), (), (), (), (), (), (), (), (), ()> { | |
rpc_extensions: Default::default(), | ||
remote_backend: None, | ||
background_tasks: Default::default(), | ||
block_announce_validator_builder: None, | ||
marker: PhantomData, | ||
}) | ||
} | ||
|
@@ -344,6 +349,7 @@ impl ServiceBuilder<(), (), (), (), (), (), (), (), (), (), ()> { | |
rpc_extensions: Default::default(), | ||
remote_backend: Some(remote_blockchain), | ||
background_tasks: Default::default(), | ||
block_announce_validator_builder: None, | ||
marker: PhantomData, | ||
}) | ||
} | ||
|
@@ -417,6 +423,7 @@ impl<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, TRpc, Backend> | |
rpc_extensions: self.rpc_extensions, | ||
remote_backend: self.remote_backend, | ||
background_tasks: self.background_tasks, | ||
block_announce_validator_builder: self.block_announce_validator_builder, | ||
marker: self.marker, | ||
}) | ||
} | ||
|
@@ -460,6 +467,7 @@ impl<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, TRpc, Backend> | |
rpc_extensions: self.rpc_extensions, | ||
remote_backend: self.remote_backend, | ||
background_tasks: self.background_tasks, | ||
block_announce_validator_builder: self.block_announce_validator_builder, | ||
marker: self.marker, | ||
}) | ||
} | ||
|
@@ -498,6 +506,7 @@ impl<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, TRpc, Backend> | |
rpc_extensions: self.rpc_extensions, | ||
remote_backend: self.remote_backend, | ||
background_tasks: self.background_tasks, | ||
block_announce_validator_builder: self.block_announce_validator_builder, | ||
marker: self.marker, | ||
}) | ||
} | ||
|
@@ -560,6 +569,7 @@ impl<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, TRpc, Backend> | |
rpc_extensions: self.rpc_extensions, | ||
remote_backend: self.remote_backend, | ||
background_tasks: self.background_tasks, | ||
block_announce_validator_builder: self.block_announce_validator_builder, | ||
marker: self.marker, | ||
}) | ||
} | ||
|
@@ -622,6 +632,7 @@ impl<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, TRpc, Backend> | |
rpc_extensions: self.rpc_extensions, | ||
remote_backend: self.remote_backend, | ||
background_tasks: self.background_tasks, | ||
block_announce_validator_builder: self.block_announce_validator_builder, | ||
marker: self.marker, | ||
}) | ||
} | ||
|
@@ -650,6 +661,36 @@ impl<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, TExPool, TRpc, Backend> | |
rpc_extensions, | ||
remote_backend: self.remote_backend, | ||
background_tasks: self.background_tasks, | ||
block_announce_validator_builder: self.block_announce_validator_builder, | ||
marker: self.marker, | ||
}) | ||
} | ||
|
||
/// Defines the `BlockAnnounceValidator` to use. `DefaultBlockAnnounceValidator` will be used by | ||
/// default. | ||
pub fn with_block_announce_validator( | ||
self, | ||
block_announce_validator_builder: | ||
impl FnOnce(Arc<TCl>) -> Box<dyn BlockAnnounceValidator<TBl> + Send> + 'static, | ||
) -> Result<ServiceBuilder<TBl, TRtApi, TCl, TFchr, TSc, TImpQu, TFprb, TFpp, | ||
TExPool, TRpc, Backend>, Error> | ||
where TSc: Clone, TFchr: Clone { | ||
Ok(ServiceBuilder { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
config: self.config, | ||
client: self.client, | ||
backend: self.backend, | ||
task_manager: self.task_manager, | ||
keystore: self.keystore, | ||
fetcher: self.fetcher, | ||
select_chain: self.select_chain, | ||
import_queue: self.import_queue, | ||
finality_proof_request_builder: self.finality_proof_request_builder, | ||
finality_proof_provider: self.finality_proof_provider, | ||
transaction_pool: self.transaction_pool, | ||
rpc_extensions: self.rpc_extensions, | ||
remote_backend: self.remote_backend, | ||
background_tasks: self.background_tasks, | ||
block_announce_validator_builder: Some(Box::new(block_announce_validator_builder)), | ||
marker: self.marker, | ||
}) | ||
} | ||
|
@@ -761,6 +802,7 @@ ServiceBuilder< | |
rpc_extensions, | ||
remote_backend, | ||
background_tasks, | ||
block_announce_validator_builder, | ||
} = self; | ||
|
||
sp_session::generate_initial_session_keys( | ||
|
@@ -809,8 +851,11 @@ ServiceBuilder< | |
sc_network::config::ProtocolId::from(protocol_id_full) | ||
}; | ||
|
||
let block_announce_validator = | ||
Box::new(sp_consensus::block_validation::DefaultBlockAnnounceValidator::new(client.clone())); | ||
let block_announce_validator = if let Some(f) = block_announce_validator_builder { | ||
f(client.clone()) | ||
} else { | ||
Box::new(DefaultBlockAnnounceValidator::new(client.clone())) | ||
}; | ||
|
||
let network_params = sc_network::config::Params { | ||
role: config.role.clone(), | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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() -> ...
?There was a problem hiding this comment.
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 staticnew
function instead of a builder? Is there some dynamic state we expect to be transferred with the closure?There was a problem hiding this comment.
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:
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 wantOn substrate you can clearly see that this has been done with this intention because the function
new
is not in the trait.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)