sc_cli/params/offchain_worker_params.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Offchain worker related configuration parameters.
20//!
21//! A subset of configuration parameters which are relevant to
22//! the inner working of offchain workers. The usage is solely
23//! targeted at handling input parameter parsing providing
24//! a reasonable abstraction.
25
26use clap::{ArgAction, Args};
27use sc_network::config::Role;
28use sc_service::config::OffchainWorkerConfig;
29
30use crate::{error, OffchainWorkerEnabled};
31
32/// Offchain worker related parameters.
33#[derive(Debug, Clone, Args)]
34pub struct OffchainWorkerParams {
35 /// Execute offchain workers on every block.
36 #[arg(
37 long = "offchain-worker",
38 value_name = "ENABLED",
39 value_enum,
40 ignore_case = true,
41 default_value_t = OffchainWorkerEnabled::WhenAuthority
42 )]
43 pub enabled: OffchainWorkerEnabled,
44
45 /// Enable offchain indexing API.
46 ///
47 /// Allows the runtime to write directly to offchain workers DB during block import.
48 #[arg(long = "enable-offchain-indexing", value_name = "ENABLE_OFFCHAIN_INDEXING", default_value_t = false, action = ArgAction::Set)]
49 pub indexing_enabled: bool,
50}
51
52impl OffchainWorkerParams {
53 /// Load spec to `Configuration` from `OffchainWorkerParams` and spec factory.
54 pub fn offchain_worker(&self, role: &Role) -> error::Result<OffchainWorkerConfig> {
55 let enabled = match (&self.enabled, role) {
56 (OffchainWorkerEnabled::WhenAuthority, Role::Authority { .. }) => true,
57 (OffchainWorkerEnabled::Always, _) => true,
58 (OffchainWorkerEnabled::Never, _) => false,
59 (OffchainWorkerEnabled::WhenAuthority, _) => false,
60 };
61
62 let indexing_enabled = self.indexing_enabled;
63 Ok(OffchainWorkerConfig { enabled, indexing_enabled })
64 }
65}