Skip to content

Commit

Permalink
omni-node: add offchain worker (#7479)
Browse files Browse the repository at this point in the history
# Description

Copy pasted the `parachain-template-node` offchain worker setup to
omni-node-lib for both aura and manual seal nodes.

Closes #7447 

## Integration

Enabled offchain workers for both `polkadot-omni-node` and
`polkadot-parachain` nodes. This would allow executing offchain logic in
the runtime and considering it on the node side.

---------

Signed-off-by: Iulian Barbu <iulian.barbu@parity.io>
Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
iulianbarbu and github-actions[bot] authored Feb 5, 2025
1 parent 9c474d5 commit 87f4f3f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions cumulus/polkadot-omni-node/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ sc-consensus = { workspace = true, default-features = true }
sc-consensus-manual-seal = { workspace = true, default-features = true }
sc-executor = { workspace = true, default-features = true }
sc-network = { workspace = true, default-features = true }
sc-offchain = { workspace = true, default-features = true }
sc-rpc = { workspace = true, default-features = true }
sc-runtime-utilities = { workspace = true, default-features = true }
sc-service = { workspace = true, default-features = true }
sc-sysinfo = { workspace = true, default-features = true }
sc-telemetry = { workspace = true, default-features = true }
sc-tracing = { workspace = true, default-features = true }
sc-transaction-pool = { workspace = true, default-features = true }
sc-transaction-pool-api = { workspace = true, default-features = true }
sp-api = { workspace = true, default-features = true }
sp-block-builder = { workspace = true, default-features = true }
sp-consensus = { workspace = true, default-features = true }
Expand All @@ -66,6 +68,7 @@ sp-crypto-hashing = { workspace = true }
sp-genesis-builder = { workspace = true }
sp-inherents = { workspace = true, default-features = true }
sp-keystore = { workspace = true, default-features = true }
sp-offchain = { workspace = true, default-features = true }
sp-runtime = { workspace = true }
sp-session = { workspace = true, default-features = true }
sp-storage = { workspace = true, default-features = true }
Expand Down
3 changes: 3 additions & 0 deletions cumulus/polkadot-omni-node/lib/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub mod types;

use cumulus_primitives_core::{CollectCollationInfo, GetCoreSelectorApi};
use sc_client_db::DbHash;
use sc_offchain::OffchainWorkerApi;
use serde::de::DeserializeOwned;
use sp_api::{ApiExt, CallApiAt, ConstructRuntimeApi, Metadata};
use sp_block_builder::BlockBuilder;
Expand Down Expand Up @@ -65,6 +66,7 @@ pub trait NodeRuntimeApi<Block: BlockT>:
+ SessionKeys<Block>
+ BlockBuilder<Block>
+ TaggedTransactionQueue<Block>
+ OffchainWorkerApi<Block>
+ CollectCollationInfo<Block>
+ GetCoreSelectorApi<Block>
+ Sized
Expand All @@ -77,6 +79,7 @@ impl<T, Block: BlockT> NodeRuntimeApi<Block> for T where
+ SessionKeys<Block>
+ BlockBuilder<Block>
+ TaggedTransactionQueue<Block>
+ OffchainWorkerApi<Block>
+ GetCoreSelectorApi<Block>
+ CollectCollationInfo<Block>
{
Expand Down
24 changes: 24 additions & 0 deletions cumulus/polkadot-omni-node/lib/src/common/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ use cumulus_client_service::{
};
use cumulus_primitives_core::{BlockT, ParaId};
use cumulus_relay_chain_interface::{OverseerHandle, RelayChainInterface};
use futures::FutureExt;
use parachains_common::Hash;
use polkadot_primitives::CollatorPair;
use prometheus_endpoint::Registry;
use sc_client_api::Backend;
use sc_consensus::DefaultImportQueue;
use sc_executor::{HeapAllocStrategy, DEFAULT_HEAP_ALLOC_STRATEGY};
use sc_network::{config::FullNetworkConfiguration, NetworkBackend, NetworkBlock};
Expand All @@ -41,6 +43,7 @@ use sc_sysinfo::HwBench;
use sc_telemetry::{TelemetryHandle, TelemetryWorker};
use sc_tracing::tracing::Instrument;
use sc_transaction_pool::TransactionPoolHandle;
use sc_transaction_pool_api::OffchainTransactionPoolFactory;
use sp_keystore::KeystorePtr;
use std::{future::Future, pin::Pin, sync::Arc, time::Duration};

Expand Down Expand Up @@ -303,6 +306,27 @@ pub(crate) trait NodeSpec: BaseNodeSpec {
})
.await?;

if parachain_config.offchain_worker.enabled {
let offchain_workers =
sc_offchain::OffchainWorkers::new(sc_offchain::OffchainWorkerOptions {
runtime_api_provider: client.clone(),
keystore: Some(params.keystore_container.keystore()),
offchain_db: backend.offchain_storage(),
transaction_pool: Some(OffchainTransactionPoolFactory::new(
transaction_pool.clone(),
)),
network_provider: Arc::new(network.clone()),
is_validator: parachain_config.role.is_authority(),
enable_http_requests: false,
custom_extensions: move |_| vec![],
})?;
task_manager.spawn_handle().spawn(
"offchain-workers-runner",
"offchain-work",
offchain_workers.run(client.clone(), task_manager.spawn_handle()).boxed(),
);
}

let rpc_builder = {
let client = client.clone();
let transaction_pool = transaction_pool.clone();
Expand Down
6 changes: 6 additions & 0 deletions cumulus/polkadot-omni-node/lib/src/fake_runtime_api/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ macro_rules! impl_node_runtime_apis {
}
}

impl sp_offchain::OffchainWorkerApi<$block> for $runtime {
fn offchain_worker(_: &<$block as BlockT>::Header) {
unimplemented!()
}
}

impl sp_session::SessionKeys<$block> for $runtime {
fn generate_session_keys(_: Option<Vec<u8>>) -> Vec<u8> {
unimplemented!()
Expand Down
24 changes: 24 additions & 0 deletions cumulus/polkadot-omni-node/lib/src/nodes/manual_seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ use codec::Encode;
use cumulus_client_parachain_inherent::{MockValidationDataInherentDataProvider, MockXcmConfig};
use cumulus_primitives_aura::AuraUnincludedSegmentApi;
use cumulus_primitives_core::{CollectCollationInfo, ParaId};
use futures::FutureExt;
use polkadot_primitives::UpgradeGoAhead;
use sc_client_api::Backend;
use sc_consensus::{DefaultImportQueue, LongestChain};
use sc_consensus_manual_seal::rpc::{ManualSeal, ManualSealApiServer};
use sc_network::NetworkBackend;
use sc_service::{Configuration, PartialComponents, TaskManager};
use sc_telemetry::TelemetryHandle;
use sc_transaction_pool_api::OffchainTransactionPoolFactory;
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_runtime::traits::Header;
use std::{marker::PhantomData, sync::Arc};
Expand Down Expand Up @@ -118,6 +121,27 @@ impl<NodeSpec: NodeSpecT> ManualSealNode<NodeSpec> {
metrics,
})?;

if config.offchain_worker.enabled {
let offchain_workers =
sc_offchain::OffchainWorkers::new(sc_offchain::OffchainWorkerOptions {
runtime_api_provider: client.clone(),
keystore: Some(keystore_container.keystore()),
offchain_db: backend.offchain_storage(),
transaction_pool: Some(OffchainTransactionPoolFactory::new(
transaction_pool.clone(),
)),
network_provider: Arc::new(network.clone()),
is_validator: config.role.is_authority(),
enable_http_requests: true,
custom_extensions: move |_| vec![],
})?;
task_manager.spawn_handle().spawn(
"offchain-workers-runner",
"offchain-work",
offchain_workers.run(client.clone(), task_manager.spawn_handle()).boxed(),
);
}

let proposer = sc_basic_authorship::ProposerFactory::new(
task_manager.spawn_handle(),
client.clone(),
Expand Down
9 changes: 9 additions & 0 deletions prdoc/pr_7479.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
title: 'omni-node: add offchain worker'
doc:
- audience: [ Runtime Dev, Node Dev, Node Operator ]
description: |-
Added support for offchain worker to omni-node-lib for both aura and manual seal nodes.

crates:
- name: polkadot-omni-node-lib
bump: patch

0 comments on commit 87f4f3f

Please sign in to comment.