From e884588d8a5b3c8bcbeacc8eeebb60adfd84efc9 Mon Sep 17 00:00:00 2001 From: Marko Petrlic Date: Tue, 10 Oct 2023 21:44:42 +0200 Subject: [PATCH] Added missing offchain worker code. Toufeeq --- Cargo.lock | 35 +++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + node/Cargo.toml | 1 + node/src/service.rs | 31 ++++++++++++++++++++++++++----- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d8997608..828db08c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2061,6 +2061,7 @@ dependencies = [ "sc-network", "sc-network-common", "sc-network-sync", + "sc-offchain", "sc-rpc", "sc-rpc-api", "sc-rpc-spec-v2", @@ -8492,6 +8493,40 @@ dependencies = [ "substrate-prometheus-endpoint 0.10.0-dev", ] +[[package]] +name = "sc-offchain" +version = "4.0.0-dev" +source = "git+https://github.com/paritytech/substrate.git/?branch=polkadot-v1.0.0#948fbd2fd1233dc26dbb9f9bbc1d2cca2c03945d" +dependencies = [ + "array-bytes", + "bytes", + "fnv", + "futures", + "futures-timer", + "hyper", + "hyper-rustls", + "libp2p", + "log", + "num_cpus", + "once_cell", + "parity-scale-codec", + "parking_lot 0.12.1", + "rand 0.8.5", + "sc-client-api", + "sc-network", + "sc-network-common", + "sc-transaction-pool-api", + "sc-utils", + "sp-api", + "sp-core", + "sp-externalities", + "sp-keystore", + "sp-offchain", + "sp-runtime", + "threadpool", + "tracing", +] + [[package]] name = "sc-proposer-metrics" version = "0.10.0-dev" diff --git a/Cargo.toml b/Cargo.toml index 38fde80c6..7c3c450f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -73,6 +73,7 @@ sp-transaction-pool = { git = "https://github.com/paritytech/substrate.git", bra sp-transaction-storage-proof = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" } sp-keystore = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" } sp-state-machine = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" } +sc-offchain = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" } sp-statement-store = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" } sp-trie = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" } sp-externalities = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" } diff --git a/node/Cargo.toml b/node/Cargo.toml index dbcdea8b2..6fdb75e66 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -65,6 +65,7 @@ sc-consensus-babe-rpc = "0.10.0-dev" sp-consensus-babe = "0.10.0-dev" sp-consensus = "0.10.0-dev" sc-consensus = "0.10.0-dev" +sc-offchain = "4.0.0-dev" # Renamed: (https://github.com/paritytech/substrate/pull/13458) sc-consensus-grandpa-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" } sc-consensus-grandpa = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" } diff --git a/node/src/service.rs b/node/src/service.rs index 1e6b0872e..9d965e4ac 100644 --- a/node/src/service.rs +++ b/node/src/service.rs @@ -19,14 +19,13 @@ //! Service and ServiceFactory implementation. Specialized wrapper over substrate service. #![allow(dead_code)] -use std::sync::Arc; - use avail_core::AppId; use codec::Encode; use da_runtime::{apis::RuntimeApi, NodeBlock as Block, Runtime}; use frame_system_rpc_runtime_api::AccountNonceApi; use futures::prelude::*; use pallet_transaction_payment::ChargeTransactionPayment; +use sc_client_api::Backend; use sc_client_api::BlockBackend; use sc_consensus_babe::{self, SlotProportion}; pub use sc_executor::NativeElseWasmExecutor; @@ -39,6 +38,7 @@ use sc_transaction_pool_api::OffchainTransactionPoolFactory; use sp_api::ProvideRuntimeApi; use sp_core::crypto::Pair; use sp_runtime::{generic::Era, traits::Block as BlockT, SaturatedConversion}; +use std::sync::Arc; use substrate_prometheus_endpoint::{PrometheusError, Registry}; use crate::rpc as node_rpc; @@ -406,14 +406,14 @@ pub fn new_full_base( let name = config.network.node_name.clone(); let enable_grandpa = !config.disable_grandpa; let prometheus_registry = config.prometheus_registry().cloned(); - let _enable_offchain_worker = config.offchain_worker.enabled; + let enable_offchain_worker = config.offchain_worker.enabled; if let Some(reg) = prometheus_registry.as_ref() { extend_metrics(reg)?; } let rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams { config, - backend, + backend: backend.clone(), client: client.clone(), keystore: keystore_container.keystore(), network: network.clone(), @@ -546,7 +546,7 @@ pub fn new_full_base( name: Some(name), observer_enabled: false, keystore, - local_role: role, + local_role: role.clone(), telemetry: telemetry.as_ref().map(|x| x.handle()), protocol_name: grandpa_protocol_name, }; @@ -579,6 +579,27 @@ pub fn new_full_base( ); } + if enable_offchain_worker { + task_manager.spawn_handle().spawn( + "offchain-workers-runner", + "offchain-work", + 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: network.clone(), + is_validator: role.is_authority(), + enable_http_requests: true, + custom_extensions: move |_| vec![], + }) + .run(client.clone(), task_manager.spawn_handle()) + .boxed(), + ); + } + network_starter.start_network(); Ok(NewFullBase { task_manager,