radix_engine/updates/
protocol_update_settings.rs

1use super::*;
2use crate::internal_prelude::*;
3
4/// This requires [`ScryptoSbor`] so it can be used to override configuration in the node for tests.
5pub trait UpdateSettings: Sized + ScryptoSbor {
6    type UpdateGenerator: ProtocolUpdateGenerator;
7
8    fn protocol_version() -> ProtocolVersion;
9
10    fn all_enabled_as_default_for_network(network: &NetworkDefinition) -> Self;
11
12    fn all_disabled() -> Self;
13
14    fn create_generator(&self) -> Self::UpdateGenerator;
15
16    fn enable(mut self, prop: impl FnOnce(&mut Self) -> &mut UpdateSetting<NoSettings>) -> Self {
17        *prop(&mut self) = UpdateSetting::Enabled(NoSettings);
18        self
19    }
20
21    fn enable_with<T: UpdateSettingContent>(
22        mut self,
23        prop: impl FnOnce(&mut Self) -> &mut UpdateSetting<T>,
24        setting: T,
25    ) -> Self {
26        *prop(&mut self) = UpdateSetting::Enabled(setting);
27        self
28    }
29
30    fn disable<T: UpdateSettingContent>(
31        mut self,
32        prop: impl FnOnce(&mut Self) -> &mut UpdateSetting<T>,
33    ) -> Self {
34        *prop(&mut self) = UpdateSetting::Disabled;
35        self
36    }
37
38    fn set(mut self, updater: impl FnOnce(&mut Self)) -> Self {
39        updater(&mut self);
40        self
41    }
42}
43
44pub trait DefaultForNetwork {
45    fn default_for_network(network_definition: &NetworkDefinition) -> Self;
46}
47
48#[derive(Clone, Sbor)]
49pub enum UpdateSetting<T: UpdateSettingContent> {
50    Enabled(T),
51    Disabled,
52}
53
54impl UpdateSetting<NoSettings> {
55    pub fn new(is_enabled: bool) -> Self {
56        if is_enabled {
57            Self::Enabled(NoSettings)
58        } else {
59            Self::Disabled
60        }
61    }
62}
63
64impl<T: UpdateSettingContent> UpdateSetting<T> {
65    pub fn enabled_as_default_for_network(network_definition: &NetworkDefinition) -> Self {
66        Self::Enabled(T::default_setting(network_definition))
67    }
68}
69
70pub trait UpdateSettingContent {
71    fn default_setting(_: &NetworkDefinition) -> Self;
72}
73
74#[derive(Clone, Copy, Debug, Default, Sbor)]
75pub struct NoSettings;
76
77impl UpdateSettingContent for NoSettings {
78    fn default_setting(_: &NetworkDefinition) -> Self {
79        NoSettings
80    }
81}