consensus_config/
consensus_protocol_config.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4/// Protocol configuration values that consensus reads. This is a standalone
5/// struct so that `consensus-core` does not depend on `sui-protocol-config`
6/// (and transitively on the Move VM).
7#[derive(Clone, Debug)]
8pub struct ConsensusProtocolConfig {
9    protocol_version: u64,
10    max_transaction_size_bytes: u64,
11    max_transactions_in_block_bytes: u64,
12    max_num_transactions_in_block: u64,
13    gc_depth: u32,
14    transaction_voting_enabled: bool,
15    num_leaders_per_round: Option<usize>,
16    bad_nodes_stake_threshold: u64,
17    always_accept_system_transactions: bool,
18}
19
20impl Default for ConsensusProtocolConfig {
21    fn default() -> Self {
22        Self {
23            protocol_version: 0,
24            max_transaction_size_bytes: 256 * 1024,
25            max_transactions_in_block_bytes: if cfg!(msim) { 256 * 1024 } else { 512 * 1024 },
26            max_num_transactions_in_block: if cfg!(msim) { 8 } else { 512 },
27            gc_depth: 0,
28            transaction_voting_enabled: false,
29            num_leaders_per_round: None,
30            bad_nodes_stake_threshold: 0,
31            always_accept_system_transactions: false,
32        }
33    }
34}
35
36impl ConsensusProtocolConfig {
37    pub fn new(
38        protocol_version: u64,
39        max_transaction_size_bytes: u64,
40        max_transactions_in_block_bytes: u64,
41        max_num_transactions_in_block: u64,
42        gc_depth: u32,
43        transaction_voting_enabled: bool,
44        num_leaders_per_round: Option<usize>,
45        bad_nodes_stake_threshold: u64,
46        always_accept_system_transactions: bool,
47    ) -> Self {
48        Self {
49            protocol_version,
50            max_transaction_size_bytes,
51            max_transactions_in_block_bytes,
52            max_num_transactions_in_block,
53            gc_depth,
54            transaction_voting_enabled,
55            num_leaders_per_round,
56            bad_nodes_stake_threshold,
57            always_accept_system_transactions,
58        }
59    }
60
61    /// Returns a config with all features enabled and reasonable defaults
62    /// for use in tests.
63    pub fn for_testing() -> Self {
64        Self {
65            protocol_version: u64::MAX,
66            max_transaction_size_bytes: 256 * 1024,
67            max_transactions_in_block_bytes: if cfg!(msim) { 256 * 1024 } else { 512 * 1024 },
68            max_num_transactions_in_block: if cfg!(msim) { 8 } else { 512 },
69            gc_depth: if cfg!(msim) { 6 } else { 60 },
70            transaction_voting_enabled: true,
71            num_leaders_per_round: Some(1),
72            bad_nodes_stake_threshold: 30,
73            always_accept_system_transactions: true,
74        }
75    }
76
77    // Getter methods
78
79    pub fn protocol_version(&self) -> u64 {
80        self.protocol_version
81    }
82
83    pub fn max_transaction_size_bytes(&self) -> u64 {
84        self.max_transaction_size_bytes
85    }
86
87    pub fn max_transactions_in_block_bytes(&self) -> u64 {
88        self.max_transactions_in_block_bytes
89    }
90
91    pub fn max_num_transactions_in_block(&self) -> u64 {
92        self.max_num_transactions_in_block
93    }
94
95    pub fn gc_depth(&self) -> u32 {
96        self.gc_depth
97    }
98
99    pub fn transaction_voting_enabled(&self) -> bool {
100        self.transaction_voting_enabled
101    }
102
103    pub fn num_leaders_per_round(&self) -> Option<usize> {
104        self.num_leaders_per_round
105    }
106
107    pub fn bad_nodes_stake_threshold(&self) -> u64 {
108        self.bad_nodes_stake_threshold
109    }
110
111    pub fn always_accept_system_transactions(&self) -> bool {
112        self.always_accept_system_transactions
113    }
114
115    // Test setter methods
116
117    pub fn set_gc_depth_for_testing(&mut self, val: u32) {
118        self.gc_depth = val;
119    }
120
121    pub fn set_transaction_voting_enabled_for_testing(&mut self, val: bool) {
122        self.transaction_voting_enabled = val;
123    }
124
125    pub fn set_max_transaction_size_bytes_for_testing(&mut self, val: u64) {
126        self.max_transaction_size_bytes = val;
127    }
128
129    pub fn set_max_transactions_in_block_bytes_for_testing(&mut self, val: u64) {
130        self.max_transactions_in_block_bytes = val;
131    }
132
133    pub fn set_max_num_transactions_in_block_for_testing(&mut self, val: u64) {
134        self.max_num_transactions_in_block = val;
135    }
136
137    pub fn set_bad_nodes_stake_threshold_for_testing(&mut self, val: u64) {
138        self.bad_nodes_stake_threshold = val;
139    }
140
141    pub fn set_num_leaders_per_round_for_testing(&mut self, val: Option<usize>) {
142        self.num_leaders_per_round = val;
143    }
144}