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 new messages from deprecated epochs are allowed.
24    pub allow_messages_from_deprecated_epochs: bool,
25    /// Whether the user application services should be long-lived.
26    pub long_lived_services: bool,
27    /// Blocks with a timestamp this far in the future will still be accepted, but the validator
28    /// will wait until that timestamp before voting.
29    pub block_time_grace_period: Duration,
30    /// Idle chain workers free their memory after this duration without requests.
31    /// `None` means no expiry (handle lives forever).
32    pub ttl: Option<Duration>,
33    /// TTL for sender chains. `None` means no expiry.
34    pub sender_chain_ttl: Option<Duration>,
35    /// The size to truncate receive log entries in chain info responses.
36    pub chain_info_max_received_log_entries: usize,
37    /// Maximum number of entries in the block cache.
38    pub block_cache_size: usize,
39    /// Maximum number of entries in the execution state cache.
40    pub execution_state_cache_size: usize,
41    /// Maximum estimated serialized size of bundles in a single `UpdateRecipient`
42    /// cross-chain message. When exceeded, the bundles are split into multiple requests.
43    /// Defaults to `usize::MAX` (no chunking).
44    pub cross_chain_message_chunk_limit: usize,
45    /// Maximum number of cross-chain requests coalesced into a single batch by the
46    /// per-chain driver. Smaller values bound the worst-case write-lock hold time at
47    /// the cost of more lock acquisitions; larger values amortize lock and storage
48    /// overhead better.
49    pub cross_chain_batch_size_limit: usize,
50    /// Whether to attempt recovery via `RevertConfirm` when an inbox gap is detected.
51    pub allow_revert_confirm: bool,
52    /// If set, reset the chain state and re-execute all blocks when the chain
53    /// state is detected to be corrupted — but only if the given duration has
54    /// elapsed since block 0 was last executed (to prevent reset loops).
55    pub reset_on_corrupted_chain_state: Option<Duration>,
56    /// Optional whitelist restricting which chains are eligible for the
57    /// `allow_revert_confirm` and `reset_on_corrupted_chain_state` recovery
58    /// mechanisms. If `None`, every chain is eligible (subject to the
59    /// respective feature flag). If `Some`, only chains in the set are.
60    pub recovery_whitelist: Option<HashSet<ChainId>>,
61}
62
63impl ChainWorkerConfig {
64    /// Configures the `key_pair` in this [`ChainWorkerConfig`].
65    #[cfg(with_testing)]
66    pub fn with_key_pair(mut self, key_pair: Option<ValidatorSecretKey>) -> Self {
67        self.key_pair = key_pair.map(Arc::new);
68        self
69    }
70
71    /// Gets a reference to the [`ValidatorSecretKey`], if available.
72    pub fn key_pair(&self) -> Option<&ValidatorSecretKey> {
73        self.key_pair.as_ref().map(Arc::as_ref)
74    }
75
76    /// Returns whether `chain_id` is allowed to attempt the `RevertConfirm` and
77    /// corrupted-state-reset recovery mechanisms.
78    pub(crate) fn recovery_allowed_for(&self, chain_id: &ChainId) -> bool {
79        self.recovery_whitelist
80            .as_ref()
81            .is_none_or(|set| set.contains(chain_id))
82    }
83}
84
85impl Default for ChainWorkerConfig {
86    fn default() -> Self {
87        Self {
88            nickname: String::new(),
89            key_pair: None,
90            allow_inactive_chains: false,
91            allow_messages_from_deprecated_epochs: false,
92            long_lived_services: false,
93            block_time_grace_period: Default::default(),
94            ttl: None,
95            sender_chain_ttl: None,
96            chain_info_max_received_log_entries: CHAIN_INFO_MAX_RECEIVED_LOG_ENTRIES,
97            block_cache_size: crate::worker::DEFAULT_BLOCK_CACHE_SIZE,
98            execution_state_cache_size: crate::worker::DEFAULT_EXECUTION_STATE_CACHE_SIZE,
99            cross_chain_message_chunk_limit: usize::MAX,
100            cross_chain_batch_size_limit: 1000,
101            allow_revert_confirm: false,
102            reset_on_corrupted_chain_state: None,
103            recovery_whitelist: None,
104        }
105    }
106}