Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Extensions to register offchain worker custom extensions #1719

Merged
merged 5 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cumulus/parachain-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ async fn start_node_impl(
network_provider: network.clone(),
is_validator: parachain_config.role.is_authority(),
enable_http_requests: false,
custom_extensions: move |_| vec![],
custom_extensions: move |_| Default::default(),
})
.run(client.clone(), task_manager.spawn_handle())
.boxed(),
Expand Down
2 changes: 1 addition & 1 deletion polkadot/node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ pub fn new_full<OverseerGenerator: OverseerGen>(
network_provider: network.clone(),
is_validator: role.is_authority(),
enable_http_requests: false,
custom_extensions: move |_| vec![],
custom_extensions: move |_| Default::default(),
})
.run(client.clone(), task_manager.spawn_handle())
.boxed(),
Expand Down
2 changes: 1 addition & 1 deletion substrate/bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
)),
network_provider: network.clone(),
enable_http_requests: true,
custom_extensions: |_| vec![],
custom_extensions: |_| Default::default(),
})
.run(client.clone(), task_manager.spawn_handle())
.boxed(),
Expand Down
1 change: 1 addition & 0 deletions substrate/bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ sp-timestamp = { path = "../../../primitives/timestamp" }
sp-inherents = { path = "../../../primitives/inherents" }
sp-keyring = { path = "../../../primitives/keyring" }
sp-keystore = { path = "../../../primitives/keystore" }
sp-externalities = { path = "../../../primitives/externalities" }
sp-consensus = { path = "../../../primitives/consensus/common" }
sp-transaction-storage-proof = { path = "../../../primitives/transaction-storage-proof" }
sp-io = { path = "../../../primitives/io" }
Expand Down
5 changes: 4 additions & 1 deletion substrate/bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use sc_telemetry::{Telemetry, TelemetryWorker};
use sc_transaction_pool_api::OffchainTransactionPoolFactory;
use sp_api::ProvideRuntimeApi;
use sp_core::crypto::Pair;
use sp_externalities::Extensions;
use sp_runtime::{generic, traits::Block as BlockT, SaturatedConversion};
use std::sync::Arc;

Expand Down Expand Up @@ -602,7 +603,9 @@ pub fn new_full_base(
is_validator: role.is_authority(),
enable_http_requests: true,
custom_extensions: move |_| {
vec![Box::new(statement_store.clone().as_statement_store_ext()) as Box<_>]
let mut extensions = Extensions::new();
extensions.register(statement_store.clone().as_statement_store_ext());
extensions
},
})
.run(client.clone(), task_manager.spawn_handle())
Expand Down
14 changes: 8 additions & 6 deletions substrate/client/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use sc_network::{NetworkPeers, NetworkStateInfo};
use sc_transaction_pool_api::OffchainTransactionPoolFactory;
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_core::{offchain, traits::SpawnNamed};
use sp_externalities::Extension;
use sp_externalities::Extensions;
use sp_keystore::{KeystoreExt, KeystorePtr};
use sp_runtime::traits::{self, Header};
use threadpool::ThreadPool;
Expand Down Expand Up @@ -120,7 +120,9 @@ pub struct OffchainWorkerOptions<RA, Block: traits::Block, Storage, CE> {
///
/// ```nocompile
/// custom_extensions: |block_hash| {
/// vec![MyCustomExtension::new()]
/// let extensions = Extensions::new();
/// extensions.register(MyCustomExtension::new());
/// extensions
/// }
/// ```
pub custom_extensions: CE,
Expand All @@ -137,12 +139,12 @@ pub struct OffchainWorkers<RA, Block: traits::Block, Storage> {
transaction_pool: Option<OffchainTransactionPoolFactory<Block>>,
network_provider: Arc<dyn NetworkProvider + Send + Sync>,
is_validator: bool,
custom_extensions: Box<dyn Fn(Block::Hash) -> Vec<Box<dyn Extension>> + Send>,
custom_extensions: Box<dyn Fn(Block::Hash) -> Extensions + Send>,
}

impl<RA, Block: traits::Block, Storage> OffchainWorkers<RA, Block, Storage> {
/// Creates new [`OffchainWorkers`].
pub fn new<CE: Fn(Block::Hash) -> Vec<Box<dyn Extension>> + Send + 'static>(
pub fn new<CE: Fn(Block::Hash) -> Extensions + Send + 'static>(
OffchainWorkerOptions {
runtime_api_provider,
keystore,
Expand Down Expand Up @@ -283,7 +285,7 @@ where
offchain::LimitedExternalities::new(capabilities, api),
));

custom_extensions.into_iter().for_each(|ext| runtime.register_extension(ext));
runtime.register_extensions(custom_extensions);

let run = if version == 2 {
runtime.offchain_worker(hash, &header)
Expand Down Expand Up @@ -444,7 +446,7 @@ mod tests {
network_provider: network,
is_validator: false,
enable_http_requests: false,
custom_extensions: |_| Vec::new(),
custom_extensions: |_| Default::default(),
});
futures::executor::block_on(offchain.on_block_imported(&header));

Expand Down
4 changes: 4 additions & 0 deletions substrate/primitives/api/proc-macro/src/impl_runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
fn register_extension<E: #crate_::Extension>(&mut self, extension: E) {
std::cell::RefCell::borrow_mut(&self.extensions).register(extension);
}

fn register_extensions(&mut self, extension: #crate_::Extensions) {
skunert marked this conversation as resolved.
Show resolved Hide resolved
std::cell::RefCell::borrow_mut(&self.extensions).merge(extension);
}
}

impl<Block: #crate_::BlockT, C> #crate_::ConstructRuntimeApi<Block, C>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
fn register_extension<E: #crate_::Extension>(&mut self, _: E) {
unimplemented!("`register_extension` not implemented for runtime api mocks")
}

fn register_extensions(&mut self, _: #crate_::Extensions) {
unimplemented!("`register_extensions` not implemented for runtime api mocks")
}
}

impl #crate_::Core<#block_type> for #self_ty {
Expand Down
3 changes: 3 additions & 0 deletions substrate/primitives/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,9 @@ pub trait ApiExt<Block: BlockT> {

/// Register an [`Extension`] that will be accessible while executing a runtime api call.
fn register_extension<E: Extension>(&mut self, extension: E);

/// Register a set of [`Extensions`] that will be accessible while executing a runtime api call.
fn register_extensions(&mut self, extensions: Extensions);
}

/// Parameters for [`CallApiAt::call_api_at`].
Expand Down