casper_node/types/
node_config.rs

1use datasize::DataSize;
2use serde::{Deserialize, Serialize};
3
4use casper_types::{BlockHash, TimeDiff};
5
6const DEFAULT_IDLE_TOLERANCE: &str = "20min";
7const DEFAULT_MAX_ATTEMPTS: usize = 3;
8const DEFAULT_CONTROL_LOGIC_DEFAULT_DELAY: &str = "1sec";
9const DEFAULT_SHUTDOWN_FOR_UPGRADE_TIMEOUT: &str = "2min";
10const DEFAULT_UPGRADE_TIMEOUT: &str = "30sec";
11
12/// Node sync configuration.
13#[derive(DataSize, Debug, Deserialize, Serialize, Clone, Default, Eq, PartialEq)]
14#[serde(rename_all = "lowercase")]
15pub enum SyncHandling {
16    /// Attempt to acquire all historical state back to genesis.
17    Genesis,
18    /// Only attempt to acquire necessary blocks to satisfy Time to Live requirements.
19    #[default]
20    Ttl,
21    /// Don't attempt to sync historical blocks.
22    NoSync,
23    /// Don't attempt to sync historical blocks and shut down node instead of switching to KeepUp
24    /// after acquiring the first complete block
25    CompleteBlock,
26    /// The node operates in isolation - no peers are needed, the node won't wait for peers to
27    /// switch to KeepUp.
28    Isolated,
29}
30
31impl SyncHandling {
32    /// Sync to Genesis?
33    pub fn is_sync_to_genesis(&self) -> bool {
34        matches!(self, SyncHandling::Genesis)
35    }
36
37    /// Sync to Ttl?
38    pub fn is_sync_to_ttl(&self) -> bool {
39        matches!(self, SyncHandling::Ttl)
40    }
41
42    /// Don't Sync?
43    pub fn is_no_sync(&self) -> bool {
44        matches!(self, SyncHandling::NoSync)
45    }
46
47    /// Don't Sync and shut down?
48    pub fn is_complete_block(&self) -> bool {
49        matches!(self, SyncHandling::CompleteBlock)
50    }
51
52    /// Isolated?
53    pub fn is_isolated(&self) -> bool {
54        matches!(self, SyncHandling::Isolated)
55    }
56}
57
58/// Node fast-sync configuration.
59#[derive(DataSize, Debug, Deserialize, Serialize, Clone)]
60// Disallow unknown fields to ensure config files and command-line overrides contain valid keys.
61#[serde(deny_unknown_fields)]
62pub struct NodeConfig {
63    /// Hash used as a trust anchor when joining, if any.
64    pub trusted_hash: Option<BlockHash>,
65
66    /// Which historical sync option?
67    ///  Genesis: sync all the way back to genesis
68    ///  Ttl: sync the necessary number of historical blocks to satisfy TTL requirement.
69    ///  NoSync: don't attempt to get any historical records; i.e. go forward only.
70    pub sync_handling: SyncHandling,
71
72    /// Idle time after which the syncing process is considered stalled.
73    pub idle_tolerance: TimeDiff,
74
75    /// When the syncing process is considered stalled, it'll be retried up to `max_attempts`
76    /// times.
77    pub max_attempts: usize,
78
79    /// Default delay for the control events that have no dedicated delay requirements.
80    pub control_logic_default_delay: TimeDiff,
81
82    /// Flag which forces the node to resync all of the blocks.
83    pub force_resync: bool,
84
85    /// Shutdown for upgrade state timeout, after which the node will upgrade regardless whether
86    /// all the conditions are satisfied.
87    pub shutdown_for_upgrade_timeout: TimeDiff,
88
89    /// Maximum time a node will wait for an upgrade to commit.
90    pub upgrade_timeout: TimeDiff,
91
92    /// If true, prevents a node from shutting down if it is supposed to be a validator in the era.
93    pub prevent_validator_shutdown: bool,
94}
95
96impl Default for NodeConfig {
97    fn default() -> NodeConfig {
98        NodeConfig {
99            trusted_hash: None,
100            sync_handling: SyncHandling::default(),
101            idle_tolerance: DEFAULT_IDLE_TOLERANCE.parse().unwrap(),
102            max_attempts: DEFAULT_MAX_ATTEMPTS,
103            control_logic_default_delay: DEFAULT_CONTROL_LOGIC_DEFAULT_DELAY.parse().unwrap(),
104            force_resync: false,
105            shutdown_for_upgrade_timeout: DEFAULT_SHUTDOWN_FOR_UPGRADE_TIMEOUT.parse().unwrap(),
106            upgrade_timeout: DEFAULT_UPGRADE_TIMEOUT.parse().unwrap(),
107            prevent_validator_shutdown: false,
108        }
109    }
110}