Skip to main content

linera_core/chain_worker/
config.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Configuration parameters for the chain worker.
5
6use std::{collections::HashSet, sync::Arc};
7
8use linera_base::{crypto::ValidatorSecretKey, identifiers::ChainId, time::Duration};
9
10use crate::CHAIN_INFO_MAX_RECEIVED_LOG_ENTRIES;
11
12/// Configuration parameters for the chain worker and its owning
13/// [`WorkerState`][`crate::worker::WorkerState`].
14#[derive(Clone)]
15pub struct ChainWorkerConfig {
16    /// A name used for logging.
17    pub nickname: String,
18    /// The signature key pair of the validator. The key may be missing for replicas
19    /// without voting rights (possibly with a partial view of chains).
20    pub key_pair: Option<Arc<ValidatorSecretKey>>,
21    /// Whether inactive chains are allowed in storage.
22    pub allow_inactive_chains: bool,
23    /// Whether the user application services should be long-lived.
24    pub long_lived_services: bool,
25    /// Blocks with a timestamp this far in the future will still be accepted, but the validator
26    /// will wait until that timestamp before voting.
27    pub block_time_grace_period: Duration,
28    /// Idle chain workers free their memory after this duration without requests.
29    /// `None` means no expiry (handle lives forever).
30    pub ttl: Option<Duration>,
31    /// TTL for sender chains. `None` means no expiry.
32    pub sender_chain_ttl: Option<Duration>,
33    /// The size to truncate receive log entries in chain info responses.
34    pub chain_info_max_received_log_entries: usize,
35    /// Maximum number of entries in the block cache.
36    pub block_cache_size: usize,
37    /// Maximum number of entries in the execution state cache.
38    pub execution_state_cache_size: usize,
39    /// Maximum estimated serialized size of bundles in a single `UpdateRecipient`
40    /// cross-chain message. When exceeded, the bundles are split into multiple requests.
41    /// Defaults to `usize::MAX` (no chunking).
42    pub cross_chain_message_chunk_limit: usize,
43    /// How often, at most, export progress is folded into the persisted `exported_heights` of an
44    /// active chain — the fold rewrites the whole register.
45    pub exported_heights_fold_interval: linera_base::time::Duration,
46    /// Maximum number of cross-chain requests coalesced into a single batch by the
47    /// per-chain driver. Smaller values bound the worst-case write-lock hold time at
48    /// the cost of more lock acquisitions; larger values amortize lock and storage
49    /// overhead better.
50    pub cross_chain_batch_size_limit: usize,
51    /// Whether to attempt recovery via `RevertConfirm` when an inbox gap is detected.
52    pub allow_revert_confirm: bool,
53    /// If set, reset the chain state and re-execute all blocks when the chain
54    /// state is detected to be corrupted — but only if the given duration has
55    /// elapsed since block 0 was last executed (to prevent reset loops).
56    pub reset_on_corrupted_chain_state: Option<Duration>,
57    /// Optional whitelist restricting which chains are eligible for the
58    /// `allow_revert_confirm` and `reset_on_corrupted_chain_state` recovery
59    /// mechanisms. If `None`, every chain is eligible (subject to the
60    /// respective feature flag). If `Some`, only chains in the set are.
61    pub recovery_whitelist: Option<HashSet<ChainId>>,
62}
63
64impl ChainWorkerConfig {
65    /// Configures the `key_pair` in this [`ChainWorkerConfig`].
66    #[cfg(with_testing)]
67    pub fn with_key_pair(mut self, key_pair: Option<ValidatorSecretKey>) -> Self {
68        self.key_pair = key_pair.map(Arc::new);
69        self
70    }
71
72    /// Gets a reference to the [`ValidatorSecretKey`], if available.
73    pub fn key_pair(&self) -> Option<&ValidatorSecretKey> {
74        self.key_pair.as_ref().map(Arc::as_ref)
75    }
76
77    /// Returns whether `chain_id` is allowed to attempt the `RevertConfirm` and
78    /// corrupted-state-reset recovery mechanisms.
79    pub(crate) fn recovery_allowed_for(&self, chain_id: &ChainId) -> bool {
80        self.recovery_whitelist
81            .as_ref()
82            .is_none_or(|set| set.contains(chain_id))
83    }
84}
85
86impl Default for ChainWorkerConfig {
87    fn default() -> Self {
88        Self {
89            nickname: String::new(),
90            key_pair: None,
91            allow_inactive_chains: false,
92            long_lived_services: false,
93            block_time_grace_period: Default::default(),
94            ttl: None,
95            sender_chain_ttl: Some(Duration::from_secs(1)),
96            chain_info_max_received_log_entries: CHAIN_INFO_MAX_RECEIVED_LOG_ENTRIES,
97            block_cache_size: 5000,
98            execution_state_cache_size: 10_000,
99            cross_chain_message_chunk_limit: usize::MAX,
100            exported_heights_fold_interval: linera_base::time::Duration::from_secs(5),
101            cross_chain_batch_size_limit: 1000,
102            allow_revert_confirm: false,
103            reset_on_corrupted_chain_state: None,
104            recovery_whitelist: None,
105        }
106    }
107}