Skip to main content

iota_protocol_config/
lib.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::{
6    cell::RefCell,
7    cmp::min,
8    sync::atomic::{AtomicBool, Ordering},
9};
10
11use clap::*;
12use iota_protocol_config_macros::{
13    ProtocolConfigAccessors, ProtocolConfigFeatureFlagsGetters, ProtocolConfigOverride,
14};
15use move_vm_config::verifier::VerifierConfig;
16use serde::{Deserialize, Serialize};
17use serde_with::skip_serializing_none;
18use tracing::{info, warn};
19
20/// The minimum and maximum protocol versions supported by this build.
21const MIN_PROTOCOL_VERSION: u64 = 1;
22pub const MAX_PROTOCOL_VERSION: u64 = 29;
23
24/// Protocol version that IIP8 took effect.
25pub const PROTOCOL_VERSION_IIP8: u64 = 20;
26// Record history of protocol version allocations here:
27//
28// Version 1:  Original version.
29// Version 2:  Don't redistribute slashed staking rewards, fix computation of
30//             SystemEpochInfoEventV1.
31// Version 3:  Set the `relocate_event_module` to be true so that the module
32//             that is associated as the "sending module" for an event is
33//             relocated by linkage.
34//             Add `Clock` based unlock to `Timelock` objects.
35// Version 4:  Introduce the `max_type_to_layout_nodes` config that sets the
36//             maximal nodes which are allowed when converting to a type layout.
37// Version 5:  Introduce fixed protocol-defined base fee, IotaSystemStateV2 and
38//             SystemEpochInfoEventV2.
39//             Disallow adding new modules in `deps-only` packages.
40//             Improve gas/wall time efficiency of some Move stdlib vector
41//             functions.
42//             Add new gas model version to update charging of functions.
43//             Enable proper conversion of certain type argument errors in the
44//             execution layer.
45// Version 6:  Bound size of values created in the adapter.
46// Version 7:  Improve handling of stake withdrawal from candidate validators.
47// Version 8:  Variants as type nodes.
48//             Enable smart ancestor selection for testnet.
49//             Enable probing for accepted rounds in round prober for testnet.
50//             Switch to distributed vote scoring in consensus in testnet.
51//             Enable zstd compression for consensus tonic network in testnet.
52//             Enable consensus garbage collection for testnet
53//             Enable the new consensus commit rule for testnet.
54//             Enable min_free_execution_slot for the shared object congestion
55//             tracker in devnet.
56// Version 9:  Disable smart ancestor selection for the testnet.
57//             Enable zstd compression for consensus tonic network in mainnet.
58//             Enable passkey auth in multisig for devnet.
59//             Remove the iota-bridge from the framework.
60// Version 10: Enable min_free_execution_slot for the shared object congestion
61//             tracker in all networks.
62//             Increase the committee size to 80 on all networks.
63//             Enable round prober in consensus for mainnet.
64//             Enable probing for accepted rounds in round prober for mainnet.
65//             Switch to distributed vote scoring in consensus for mainnet.
66//             Enable the new consensus commit rule for mainnet.
67//             Enable consensus garbage collection for mainnet with GC depth set
68//             to 60 rounds.
69//             Enable batching in synchronizer for testnet
70//             Enable the gas price feedback mechanism in devnet.
71//             Enable Identifier input validation.
72//             Removes unnecessary child object mutations
73//             Add additional signature checks
74//             Add additional linkage checks
75// Version 11: Framework fix regarding candidate validator commission rate.
76// Version 12: Enable the gas price feedback mechanism in all networks.
77//             Enable the normalization of PTB arguments.
78// Version 13: Introduce logic to allow the committee to be selected from a set
79//             of eligible active validators.
80//             Enable processing and tracking AuthorityCapabilitiesV1 from
81//             non-committee validators in the devnet.
82// Version 14: Switches the consensus protocol to Starfish in devnet.
83//             Enable median-based commit timestamp calculation in consensus,
84//             and enforce checkpoint timestamp monotonicity for testnet.
85//             Enable batched block sync for mainnet.
86//             Enable selecting committee only from active validators that
87//             support the next epoch's version and issued valid
88//             AuthorityCapabilities notification in testnet.
89// Version 15: Enable shared object transaction bursts of 10 times average load
90//             on devnet.
91// Version 16: Enable selecting committee only from active validators that
92//             support the next epoch's version and issued valid
93//             AuthorityCapabilities notification.
94//             Enable committing transactions only for traversed headers in
95//             Starfish.
96// Version 17: Increase the committee size to 100 on all networks.
97// Version 18: Enable passkey authentication support in testnet.
98// Version 19: Enable congestion limit overshoot in the gas price feedback
99//             mechanism on devnet.
100//             Enable a separate gas price feedback mechanism for transactions
101//             using randomness on devnet.
102//             Allow metadata bytes indexed with a dedicated key in compiled
103//             Move modules in devnet.
104//             Enable publishing package metadata v1 along with the package in
105//             devnet.
106//             Enable Move-based account authentication in devnet.
107//             Increase the base cost for transfer receive object in devnet.
108//             Switch consensus protocol to Starfish in testnet.
109//             Enable passkey authentication support in mainnet.
110//             Change epoch transaction will contain validator scores.
111//             Enable validator scoring on testnet and enable adjustment of
112//             validator rewards based on scores on Devnet.
113// Version 20: Supports the calculation of validator scores while still passing
114//             a default score value to the advance_epoch call. Enables this
115//             decoupling on Testnet; Devnet and Mainnet behavior remain the
116//             same.
117//             Introduce Dynamic Minimum Commission (IIP-8) on all networks.
118// Version 21: Enable overshoot of 100 in congestion control on testnet.
119//             Enable congestion limit overshoot in the gas price feedback
120//             mechanism on testnet.
121//             Enable a separate gas price feedback mechanism for transactions
122//             using randomness on testnet.
123//             Enable fast commit syncer for faster recovery in devnet.
124//             Add auth_context_tx native functions costs.
125//             Reduce max_auth_gas in Devnet.
126// Version 22: Enable overshoot of 100 in congestion control on all networks.
127//             Enable congestion limit overshoot in the gas price feedback
128//             mechanism on all networks.
129//             Enable a separate gas price feedback mechanism for transactions
130//             using randomness on all networks.
131//             Enable Move-based account authentication in testnet.
132//             Enable fast commit syncer for faster recovery on testnet.
133// Version 23: Enable Move native context (TxContext via native functions) in
134//             all networks. TxContext fields are read via native functions
135//             instead of being deserialized from a BCS-encoded struct.
136//             Enables sponsor, rgp, gas_price, and gas_budget to be exposed to
137//             Move.
138// Version 24: Switch consensus protocol to Starfish in all networks.
139//             Enable Move-based sponsor account authentication in devnet.
140//             Add AuthContext native functions cost for reading tx_data_bytes.
141//             Enable additional borrow checks.
142// Version 25: Deprecate zkLogin related parameters since zkLogin is no longer
143//             supported.
144// Version 26: Introduce a module to allow Move code to query protocol feature
145//             flags at runtime.
146// Version 27: Only sponsor Move authentication is performed pre-consensus in
147//             devnet.
148//             Enable consensus block restrictions on testnet and devnet:
149//             bound block-header size to O(committee_size) and enable
150//             garbage collection in the block manager.
151// Version 28: Move authenticator contracts can now inspect which authenticator
152//             function the sender and sponsor used during transaction execution
153//             via new AuthContext accessors.
154//             Enable Move-based account authentication in mainnet.
155//             Enable Move-based sponsor account authentication in testnet.
156// Version 29: Keep advancing the random beacon DKG state machine on every
157//             commit while it is still pending -- regardless of whether new DKG
158//             messages or confirmations arrived that commit -- so DKG resolves
159//             from persisted state (completing, or failing once the timeout
160//             round passes) even with no fresh inbound traffic, e.g. after a
161//             validator restart. Without this it can stay pending forever and
162//             block epoch close.
163//             Enable median-based commit timestamp calculation in consensus,
164//             and enforce checkpoint timestamp monotonicity for mainnet.
165#[derive(Copy, Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
166pub struct ProtocolVersion(u64);
167
168impl ProtocolVersion {
169    // The minimum and maximum protocol version supported by this binary.
170    // Counterintuitively, this constant may change over time as support for old
171    // protocol versions is removed from the source. This ensures that when a
172    // new network (such as a testnet) is created, its genesis committee will
173    // use a protocol version that is actually supported by the binary.
174    pub const MIN: Self = Self(MIN_PROTOCOL_VERSION);
175
176    pub const MAX: Self = Self(MAX_PROTOCOL_VERSION);
177
178    #[cfg(not(msim))]
179    const MAX_ALLOWED: Self = Self::MAX;
180
181    // We create one additional "fake" version in simulator builds so that we can
182    // test upgrades.
183    #[cfg(msim)]
184    pub const MAX_ALLOWED: Self = Self(MAX_PROTOCOL_VERSION + 1);
185
186    pub fn new(v: u64) -> Self {
187        Self(v)
188    }
189
190    pub const fn as_u64(&self) -> u64 {
191        self.0
192    }
193
194    // For serde deserialization - we don't define a Default impl because there
195    // isn't a single universally appropriate default value.
196    pub fn max() -> Self {
197        Self::MAX
198    }
199}
200
201impl From<u64> for ProtocolVersion {
202    fn from(v: u64) -> Self {
203        Self::new(v)
204    }
205}
206
207impl std::ops::Sub<u64> for ProtocolVersion {
208    type Output = Self;
209    fn sub(self, rhs: u64) -> Self::Output {
210        Self::new(self.0 - rhs)
211    }
212}
213
214impl std::ops::Add<u64> for ProtocolVersion {
215    type Output = Self;
216    fn add(self, rhs: u64) -> Self::Output {
217        Self::new(self.0 + rhs)
218    }
219}
220
221#[derive(
222    Clone, Serialize, Deserialize, Debug, PartialEq, Copy, PartialOrd, Ord, Eq, ValueEnum, Default,
223)]
224pub enum Chain {
225    Mainnet,
226    Testnet,
227    #[default]
228    Unknown,
229}
230
231impl Chain {
232    pub fn as_str(self) -> &'static str {
233        match self {
234            Chain::Mainnet => "mainnet",
235            Chain::Testnet => "testnet",
236            Chain::Unknown => "unknown",
237        }
238    }
239}
240
241pub struct Error(pub String);
242
243// TODO: There are quite a few non boolean values in the feature flags. We
244// should move them out.
245/// Records on/off feature flags that may vary at each protocol version.
246#[derive(
247    Default,
248    Clone,
249    Serialize,
250    Deserialize,
251    Debug,
252    ProtocolConfigFeatureFlagsGetters,
253    ProtocolConfigOverride,
254)]
255struct FeatureFlags {
256    // Add feature flags here, e.g.:
257    // new_protocol_feature: bool,
258
259    // Disables unnecessary invariant check in the Move VM when swapping the value out of a local
260    // This flag is used to provide the correct MoveVM configuration for clients.
261    #[serde(skip_serializing_if = "is_true")]
262    disable_invariant_violation_check_in_swap_loc: bool,
263
264    // If true, checks no extra bytes in a compiled module
265    // This flag is used to provide the correct MoveVM configuration for clients.
266    #[serde(skip_serializing_if = "is_true")]
267    no_extraneous_module_bytes: bool,
268
269    // How we order transactions coming out of consensus before sending to execution.
270    #[serde(skip_serializing_if = "ConsensusTransactionOrdering::is_none")]
271    consensus_transaction_ordering: ConsensusTransactionOrdering,
272
273    // If true, use the hardened OTW check
274    // This flag is used to provide the correct MoveVM configuration for clients.
275    #[serde(skip_serializing_if = "is_true")]
276    hardened_otw_check: bool,
277
278    // Enable the poseidon hash function
279    #[serde(skip_serializing_if = "is_false")]
280    enable_poseidon: bool,
281
282    // Enable native function for msm.
283    #[serde(skip_serializing_if = "is_false")]
284    enable_group_ops_native_function_msm: bool,
285
286    // Controls the behavior of per object congestion control in consensus handler.
287    #[serde(skip_serializing_if = "PerObjectCongestionControlMode::is_none")]
288    per_object_congestion_control_mode: PerObjectCongestionControlMode,
289
290    // The consensus protocol to be used for the epoch.
291    #[serde(
292        default = "ConsensusChoice::mysticeti_deprecated",
293        skip_serializing_if = "ConsensusChoice::is_mysticeti_deprecated"
294    )]
295    consensus_choice: ConsensusChoice,
296
297    // Consensus network to use.
298    #[serde(skip_serializing_if = "ConsensusNetwork::is_tonic")]
299    consensus_network: ConsensusNetwork,
300
301    // Set the upper bound allowed for max_epoch in zklogin signature.
302    #[deprecated]
303    #[serde(skip_serializing_if = "Option::is_none")]
304    zklogin_max_epoch_upper_bound_delta: Option<u64>,
305
306    // Enable VDF
307    #[serde(skip_serializing_if = "is_false")]
308    enable_vdf: bool,
309
310    // Enable passkey auth (SIP-9)
311    #[serde(skip_serializing_if = "is_false")]
312    passkey_auth: bool,
313
314    // Rethrow type layout errors during serialization instead of trying to convert them.
315    // This flag is used to provide the correct MoveVM configuration for clients.
316    #[serde(skip_serializing_if = "is_true")]
317    rethrow_serialization_type_layout_errors: bool,
318
319    // Makes the event's sending module version-aware.
320    #[serde(skip_serializing_if = "is_false")]
321    relocate_event_module: bool,
322
323    // Enable a protocol-defined base gas price for all transactions.
324    #[serde(skip_serializing_if = "is_false")]
325    protocol_defined_base_fee: bool,
326
327    // Enable uncompressed group elements in BLS123-81 G1
328    #[serde(skip_serializing_if = "is_false")]
329    uncompressed_g1_group_elements: bool,
330
331    // Disallow adding new modules in `deps-only` packages.
332    #[serde(skip_serializing_if = "is_false")]
333    disallow_new_modules_in_deps_only_packages: bool,
334
335    // Enable v2 native charging for natives.
336    #[serde(skip_serializing_if = "is_false")]
337    native_charging_v2: bool,
338
339    // Properly convert certain type argument errors in the execution layer.
340    #[serde(skip_serializing_if = "is_false")]
341    convert_type_argument_error: bool,
342
343    // Probe rounds received by peers from every authority.
344    #[serde(skip_serializing_if = "is_false")]
345    consensus_round_prober: bool,
346
347    // Use distributed vote leader scoring strategy in consensus.
348    #[serde(skip_serializing_if = "is_false")]
349    consensus_distributed_vote_scoring_strategy: bool,
350
351    // Enables the new logic for collecting the subdag in the consensus linearizer. The new logic
352    // does not stop the recursion at the highest committed round for each authority, but
353    // allows to commit uncommitted blocks up to gc round (excluded) for that authority.
354    #[serde(skip_serializing_if = "is_false")]
355    consensus_linearize_subdag_v2: bool,
356
357    // Variants count as nodes
358    #[serde(skip_serializing_if = "is_false")]
359    variant_nodes: bool,
360
361    // Use smart ancestor selection in consensus.
362    #[serde(skip_serializing_if = "is_false")]
363    consensus_smart_ancestor_selection: bool,
364
365    // Probe accepted rounds in round prober.
366    #[serde(skip_serializing_if = "is_false")]
367    consensus_round_prober_probe_accepted_rounds: bool,
368
369    // If true, enable zstd compression for consensus tonic network.
370    #[serde(skip_serializing_if = "is_false")]
371    consensus_zstd_compression: bool,
372
373    // Use the minimum free execution slot to schedule execution of a transaction in the shared
374    // object congestion tracker.
375    #[serde(skip_serializing_if = "is_false")]
376    congestion_control_min_free_execution_slot: bool,
377
378    // If true, multisig containing passkey sig is accepted.
379    #[serde(skip_serializing_if = "is_false")]
380    accept_passkey_in_multisig: bool,
381
382    // If true, enabled batched block sync in consensus.
383    #[serde(skip_serializing_if = "is_false")]
384    consensus_batched_block_sync: bool,
385
386    // To enable/disable the gas price feedback mechanism used for transactions
387    // cancelled due to shared object congestion
388    #[serde(skip_serializing_if = "is_false")]
389    congestion_control_gas_price_feedback_mechanism: bool,
390
391    // Validate identifier inputs separately
392    #[serde(skip_serializing_if = "is_false")]
393    validate_identifier_inputs: bool,
394
395    // If true, enables the optimizations for child object mutations, removing unnecessary
396    // mutations
397    #[serde(skip_serializing_if = "is_false")]
398    minimize_child_object_mutations: bool,
399
400    // If true enable additional linkage checks.
401    #[serde(skip_serializing_if = "is_false")]
402    dependency_linkage_error: bool,
403
404    // If true enable additional multisig checks.
405    #[serde(skip_serializing_if = "is_false")]
406    additional_multisig_checks: bool,
407
408    // If true, enables the normalization of PTB arguments but does not yet enable splatting
409    // `Result`s of length not equal to 1
410    #[serde(skip_serializing_if = "is_false")]
411    normalize_ptb_arguments: bool,
412
413    // If true, use ChangeEpochV3 for epoch change to pass an additional eligible_active_validators
414    // parameter to IotaSystem's advance_epoch call. This should only be enabled when on-chain
415    // IotaSystem objects are updated as well.
416    #[serde(skip_serializing_if = "is_false")]
417    select_committee_from_eligible_validators: bool,
418
419    // If true, non-committee active validators will sign and send AuthorityCapabilitiesV1 to the
420    // committee. Once the committee reaches consensus over the AuthorityCapabilitiesV1, it is
421    // recorded and possible to use in the committee selection if
422    // select_validators_supporting_next_epoch_version is enabled. This flag does not change the
423    // way that eligible_validators vector is created - still all active validators are used for
424    // selecting the committee.
425    #[serde(skip_serializing_if = "is_false")]
426    track_non_committee_eligible_validators: bool,
427
428    // The committee be selected from active_validators who support the next protocol version AND
429    // have issued a correct AuthorityCapabilities notification. This flag should only be enabled
430    // if both select_committee_from_eligible_validators and
431    // track_non_committee_eligible_validators are enabled. If this is disabled, then all
432    // active validators are used for selecting the committee (default behavior).
433    #[serde(skip_serializing_if = "is_false")]
434    select_committee_supporting_next_epoch_version: bool,
435
436    // If true, then it (1) will not enforce monotonicity checks for a block's ancestors, (2)
437    // calculates the commit's timestamp based on the weighted by stake median timestamp of the
438    // leader's ancestors, and (3) enforces checkpoint timestamps are non-decreasing.
439    #[serde(skip_serializing_if = "is_false")]
440    consensus_median_timestamp_with_checkpoint_enforcement: bool,
441
442    // If true, then transactions are committed only for traversed headers
443    #[serde(skip_serializing_if = "is_false")]
444    consensus_commit_transactions_only_for_traversed_headers: bool,
445
446    // To enable/disable congestion limit overshoot in the gas price feedback mechanism.
447    #[serde(skip_serializing_if = "is_false")]
448    congestion_limit_overshoot_in_gas_price_feedback_mechanism: bool,
449
450    // To enable/disable a separate gas price feedback mechanism for transactions using
451    // randomness.
452    #[serde(skip_serializing_if = "is_false")]
453    separate_gas_price_feedback_mechanism_for_randomness: bool,
454
455    // If true, it allows metadata bytes indexed with a dedicated key in a compiled module.
456    // This flag is used to provide the correct MoveVM configuration for clients.
457    #[serde(skip_serializing_if = "is_false")]
458    metadata_in_module_bytes: bool,
459
460    // If true, enables publishing package metadata v1 along with the package.
461    #[serde(skip_serializing_if = "is_false")]
462    publish_package_metadata: bool,
463
464    // If true, enables the authentication of account using Move code.
465    #[serde(skip_serializing_if = "is_false")]
466    enable_move_authentication: bool,
467
468    // If true, enables the authentication of a sponsor account using Move code.
469    #[serde(skip_serializing_if = "is_false")]
470    enable_move_authentication_for_sponsor: bool,
471
472    // If true, the change epoch transaction will contain validator scores.
473    #[serde(skip_serializing_if = "is_false")]
474    pass_validator_scores_to_advance_epoch: bool,
475
476    // If true, enables calculation of validator scores.
477    #[serde(skip_serializing_if = "is_false")]
478    calculate_validator_scores: bool,
479
480    // If true, validators will use the committee's score to adjust rewards.
481    #[serde(skip_serializing_if = "is_false")]
482    adjust_rewards_by_score: bool,
483
484    // If true, the change epoch transaction will contain the locally calculated validator scores.
485    // If false, a default score (MAX_SCORE) is passed
486    #[serde(skip_serializing_if = "is_false")]
487    pass_calculated_validator_scores_to_advance_epoch: bool,
488
489    // If true, enables the fast commit syncer in Starfish consensus for faster recovery
490    // from large commit gaps. Also controls whether TransactionRef is used in commits
491    // instead of BlockRef, and enables the associated gRPC endpoints for fetching
492    // commits and transactions.
493    #[serde(skip_serializing_if = "is_false")]
494    consensus_fast_commit_sync: bool,
495
496    // If true, enables consensus block restrictions: bounds the block header size for
497    // a given committee size.
498    #[serde(skip_serializing_if = "is_false")]
499    consensus_block_restrictions: bool,
500
501    // If true, enable `TxContext` Move API to go native.
502    #[serde(skip_serializing_if = "is_false")]
503    move_native_tx_context: bool,
504
505    // If true, perform additional borrow checks
506    #[serde(skip_serializing_if = "is_false")]
507    additional_borrow_checks: bool,
508
509    // If true, only sponsor Move authentication is performed pre-consensus.
510    #[serde(skip_serializing_if = "is_false")]
511    pre_consensus_sponsor_only_move_authentication: bool,
512
513    // If true, enables the optimistic commit rule (StarfishSpeed) in Starfish consensus.
514    #[serde(skip_serializing_if = "is_false")]
515    consensus_starfish_speed: bool,
516
517    // If true, keep advancing the random beacon DKG state machine on every
518    // consensus commit while DKG is still pending, even when no new messages or
519    // confirmations were processed that commit. This lets a validator resolve
520    // DKG from already-persisted state (completing, or failing once the timeout
521    // round passes) with no fresh inbound traffic -- e.g. after a restart --
522    // instead of staying pending forever.
523    #[serde(skip_serializing_if = "is_false")]
524    always_advance_dkg_to_resolution: bool,
525
526    // If true, enables white flag flow for post-consensus owned object conflict resolution.
527    // Transactions bypass pre-consensus certification and owned object locking.
528    // Conflicts are resolved deterministically post-consensus using persistent locks.
529    #[serde(skip_serializing_if = "is_false")]
530    enable_white_flag_flow: bool,
531}
532
533fn is_true(b: &bool) -> bool {
534    *b
535}
536
537fn is_false(b: &bool) -> bool {
538    !b
539}
540
541/// Ordering mechanism for transactions in one consensus output.
542#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
543pub enum ConsensusTransactionOrdering {
544    /// No ordering. Transactions are processed in the order they appear in the
545    /// consensus output.
546    #[default]
547    None,
548    /// Order transactions by gas price, highest first.
549    ByGasPrice,
550}
551
552impl ConsensusTransactionOrdering {
553    pub fn is_none(&self) -> bool {
554        matches!(self, ConsensusTransactionOrdering::None)
555    }
556}
557
558// The config for per object congestion control in consensus handler.
559#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
560pub enum PerObjectCongestionControlMode {
561    #[default]
562    None, // No congestion control.
563    TotalGasBudget, // Use txn gas budget as execution cost.
564    TotalTxCount,   // Use total txn count as execution cost.
565}
566
567impl PerObjectCongestionControlMode {
568    pub fn is_none(&self) -> bool {
569        matches!(self, PerObjectCongestionControlMode::None)
570    }
571}
572
573// Configuration options for consensus algorithm.
574#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
575pub enum ConsensusChoice {
576    /// Kept only so protocol-config serialization of historical epochs stays
577    /// bit-for-bit identical; no runtime code branches on it.
578    #[deprecated(note = "Mysticeti was replaced by Starfish")]
579    MysticetiDeprecated,
580    #[default]
581    Starfish,
582}
583
584#[expect(deprecated)]
585impl ConsensusChoice {
586    /// serde deserialization default: an absent `consensus_choice` field in a
587    /// historical snapshot deserializes to `MysticetiDeprecated` so that
588    /// re-serialization stays byte-identical (the skip condition below also
589    /// triggers on that variant). Decoupled from the Rust `Default` impl,
590    /// which returns `Starfish` to reflect that Starfish is the current
591    /// consensus protocol.
592    fn mysticeti_deprecated() -> Self {
593        ConsensusChoice::MysticetiDeprecated
594    }
595
596    pub fn is_mysticeti_deprecated(&self) -> bool {
597        matches!(self, ConsensusChoice::MysticetiDeprecated)
598    }
599    pub fn is_starfish(&self) -> bool {
600        matches!(self, ConsensusChoice::Starfish)
601    }
602}
603
604// Configuration options for consensus network.
605#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
606pub enum ConsensusNetwork {
607    #[default]
608    Tonic,
609}
610
611impl ConsensusNetwork {
612    pub fn is_tonic(&self) -> bool {
613        matches!(self, ConsensusNetwork::Tonic)
614    }
615}
616
617/// Constants that change the behavior of the protocol.
618///
619/// The value of each constant here must be fixed for a given protocol version.
620/// To change the value of a constant, advance the protocol version, and add
621/// support for it in `get_for_version` under the new version number.
622/// (below).
623///
624/// To add a new field to this struct, use the following procedure:
625/// - Advance the protocol version.
626/// - Add the field as a private `Option<T>` to the struct.
627/// - Initialize the field to `None` in prior protocol versions.
628/// - Initialize the field to `Some(val)` for your new protocol version.
629/// - Add a public getter that simply unwraps the field.
630/// - Two public getters of the form `field(&self) -> field_type` and
631///   `field_as_option(&self) -> Option<field_type>` will be automatically
632///   generated for you.
633/// Example for a field: `new_constant: Option<u64>`
634/// ```rust,ignore
635///      pub fn new_constant(&self) -> u64 {
636///         self.new_constant.expect(Self::CONSTANT_ERR_MSG)
637///     }
638///      pub fn new_constant_as_option(&self) -> Option<u64> {
639///         self.new_constant.expect(Self::CONSTANT_ERR_MSG)
640///     }
641/// ```
642/// With `pub fn new_constant(&self) -> u64`, if the constant is accessed in a
643/// protocol version in which it is not defined, the validator will crash.
644/// (Crashing is necessary because this type of error would almost always result
645/// in forking if not prevented here). If you don't want the validator to crash,
646/// you can use the `pub fn new_constant_as_option(&self) -> Option<u64>`
647/// getter, which will return `None` if the field is not defined at that
648/// version.
649/// - If you want a customized getter, you can add a method in the impl.
650#[skip_serializing_none]
651#[derive(Clone, Serialize, Debug, ProtocolConfigAccessors, ProtocolConfigOverride)]
652pub struct ProtocolConfig {
653    pub version: ProtocolVersion,
654
655    feature_flags: FeatureFlags,
656
657    // ==== Transaction input limits ====
658
659    //
660    /// Maximum serialized size of a transaction (in bytes).
661    max_tx_size_bytes: Option<u64>,
662
663    /// Maximum number of input objects to a transaction. Enforced by the
664    /// transaction input checker
665    max_input_objects: Option<u64>,
666
667    /// Max size of objects a transaction can write to disk after completion.
668    /// Enforce by the IOTA adapter. This is the sum of the serialized size
669    /// of all objects written to disk. The max size of individual objects
670    /// on the other hand is `max_move_object_size`.
671    max_size_written_objects: Option<u64>,
672    /// Max size of objects a system transaction can write to disk after
673    /// completion. Enforce by the IOTA adapter. Similar to
674    /// `max_size_written_objects` but for system transactions.
675    max_size_written_objects_system_tx: Option<u64>,
676
677    /// Maximum size of serialized transaction effects.
678    max_serialized_tx_effects_size_bytes: Option<u64>,
679
680    /// Maximum size of serialized transaction effects for system transactions.
681    max_serialized_tx_effects_size_bytes_system_tx: Option<u64>,
682
683    /// Maximum number of gas payment objects for a transaction.
684    max_gas_payment_objects: Option<u32>,
685
686    /// Maximum number of modules in a Publish transaction.
687    max_modules_in_publish: Option<u32>,
688
689    /// Maximum number of transitive dependencies in a package when publishing.
690    max_package_dependencies: Option<u32>,
691
692    /// Maximum number of arguments in a move call or a
693    /// ProgrammableTransaction's TransferObjects command.
694    max_arguments: Option<u32>,
695
696    /// Maximum number of total type arguments, computed recursively.
697    max_type_arguments: Option<u32>,
698
699    /// Maximum depth of an individual type argument.
700    max_type_argument_depth: Option<u32>,
701
702    /// Maximum size of a Pure CallArg.
703    max_pure_argument_size: Option<u32>,
704
705    /// Maximum number of Commands in a ProgrammableTransaction.
706    max_programmable_tx_commands: Option<u32>,
707
708    // ==== Move VM, Move bytecode verifier, and execution limits ===
709
710    //
711    /// Maximum Move bytecode version the VM understands. All older versions are
712    /// accepted.
713    move_binary_format_version: Option<u32>,
714    min_move_binary_format_version: Option<u32>,
715
716    /// Configuration controlling binary tables size.
717    binary_module_handles: Option<u16>,
718    binary_struct_handles: Option<u16>,
719    binary_function_handles: Option<u16>,
720    binary_function_instantiations: Option<u16>,
721    binary_signatures: Option<u16>,
722    binary_constant_pool: Option<u16>,
723    binary_identifiers: Option<u16>,
724    binary_address_identifiers: Option<u16>,
725    binary_struct_defs: Option<u16>,
726    binary_struct_def_instantiations: Option<u16>,
727    binary_function_defs: Option<u16>,
728    binary_field_handles: Option<u16>,
729    binary_field_instantiations: Option<u16>,
730    binary_friend_decls: Option<u16>,
731    binary_enum_defs: Option<u16>,
732    binary_enum_def_instantiations: Option<u16>,
733    binary_variant_handles: Option<u16>,
734    binary_variant_instantiation_handles: Option<u16>,
735
736    /// Maximum size of the `contents` part of an object, in bytes. Enforced by
737    /// the IOTA adapter when effects are produced.
738    max_move_object_size: Option<u64>,
739
740    // TODO: Option<increase to 500 KB. currently, publishing a package > 500 KB exceeds the max
741    // computation gas cost
742    /// Maximum size of a Move package object, in bytes. Enforced by the IOTA
743    /// adapter at the end of a publish transaction.
744    max_move_package_size: Option<u64>,
745
746    /// Max number of publish or upgrade commands allowed in a programmable
747    /// transaction block.
748    max_publish_or_upgrade_per_ptb: Option<u64>,
749
750    /// Maximum gas budget in NANOS that a transaction can use.
751    max_tx_gas: Option<u64>,
752
753    /// Maximum gas budget in NANOS that a authentication transaction can use.
754    max_auth_gas: Option<u64>,
755
756    /// Maximum amount of the proposed gas price in NANOS (defined in the
757    /// transaction).
758    max_gas_price: Option<u64>,
759
760    /// The max computation bucket for gas. This is the max that can be charged
761    /// for computation.
762    max_gas_computation_bucket: Option<u64>,
763
764    // Define the value used to round up computation gas charges
765    gas_rounding_step: Option<u64>,
766
767    /// Maximum number of nested loops. Enforced by the Move bytecode verifier.
768    max_loop_depth: Option<u64>,
769
770    /// Maximum number of type arguments that can be bound to generic type
771    /// parameters. Enforced by the Move bytecode verifier.
772    max_generic_instantiation_length: Option<u64>,
773
774    /// Maximum number of parameters that a Move function can have. Enforced by
775    /// the Move bytecode verifier.
776    max_function_parameters: Option<u64>,
777
778    /// Maximum number of basic blocks that a Move function can have. Enforced
779    /// by the Move bytecode verifier.
780    max_basic_blocks: Option<u64>,
781
782    /// Maximum stack size value. Enforced by the Move bytecode verifier.
783    max_value_stack_size: Option<u64>,
784
785    /// Maximum number of "type nodes", a metric for how big a SignatureToken
786    /// will be when expanded into a fully qualified type. Enforced by the Move
787    /// bytecode verifier.
788    max_type_nodes: Option<u64>,
789
790    /// Maximum number of push instructions in one function. Enforced by the
791    /// Move bytecode verifier.
792    max_push_size: Option<u64>,
793
794    /// Maximum number of struct definitions in a module. Enforced by the Move
795    /// bytecode verifier.
796    max_struct_definitions: Option<u64>,
797
798    /// Maximum number of function definitions in a module. Enforced by the Move
799    /// bytecode verifier.
800    max_function_definitions: Option<u64>,
801
802    /// Maximum number of fields allowed in a struct definition. Enforced by the
803    /// Move bytecode verifier.
804    max_fields_in_struct: Option<u64>,
805
806    /// Maximum dependency depth. Enforced by the Move linker when loading
807    /// dependent modules.
808    max_dependency_depth: Option<u64>,
809
810    /// Maximum number of Move events that a single transaction can emit.
811    /// Enforced by the VM during execution.
812    max_num_event_emit: Option<u64>,
813
814    /// Maximum number of new IDs that a single transaction can create. Enforced
815    /// by the VM during execution.
816    max_num_new_move_object_ids: Option<u64>,
817
818    /// Maximum number of new IDs that a single system transaction can create.
819    /// Enforced by the VM during execution.
820    max_num_new_move_object_ids_system_tx: Option<u64>,
821
822    /// Maximum number of IDs that a single transaction can delete. Enforced by
823    /// the VM during execution.
824    max_num_deleted_move_object_ids: Option<u64>,
825
826    /// Maximum number of IDs that a single system transaction can delete.
827    /// Enforced by the VM during execution.
828    max_num_deleted_move_object_ids_system_tx: Option<u64>,
829
830    /// Maximum number of IDs that a single transaction can transfer. Enforced
831    /// by the VM during execution.
832    max_num_transferred_move_object_ids: Option<u64>,
833
834    /// Maximum number of IDs that a single system transaction can transfer.
835    /// Enforced by the VM during execution.
836    max_num_transferred_move_object_ids_system_tx: Option<u64>,
837
838    /// Maximum size of a Move user event. Enforced by the VM during execution.
839    max_event_emit_size: Option<u64>,
840
841    /// Maximum size of a Move user event. Enforced by the VM during execution.
842    max_event_emit_size_total: Option<u64>,
843
844    /// Maximum length of a vector in Move. Enforced by the VM during execution,
845    /// and for constants, by the verifier.
846    max_move_vector_len: Option<u64>,
847
848    /// Maximum length of an `Identifier` in Move. Enforced by the bytecode
849    /// verifier at signing.
850    max_move_identifier_len: Option<u64>,
851
852    /// Maximum depth of a Move value within the VM.
853    max_move_value_depth: Option<u64>,
854
855    /// Maximum number of variants in an enum. Enforced by the bytecode verifier
856    /// at signing.
857    max_move_enum_variants: Option<u64>,
858
859    /// Maximum number of back edges in Move function. Enforced by the bytecode
860    /// verifier at signing.
861    max_back_edges_per_function: Option<u64>,
862
863    /// Maximum number of back edges in Move module. Enforced by the bytecode
864    /// verifier at signing.
865    max_back_edges_per_module: Option<u64>,
866
867    /// Maximum number of meter `ticks` spent verifying a Move function.
868    /// Enforced by the bytecode verifier at signing.
869    max_verifier_meter_ticks_per_function: Option<u64>,
870
871    /// Maximum number of meter `ticks` spent verifying a Move function.
872    /// Enforced by the bytecode verifier at signing.
873    max_meter_ticks_per_module: Option<u64>,
874
875    /// Maximum number of meter `ticks` spent verifying a Move package. Enforced
876    /// by the bytecode verifier at signing.
877    max_meter_ticks_per_package: Option<u64>,
878
879    // === Object runtime internal operation limits ====
880    // These affect dynamic fields
881
882    //
883    /// Maximum number of cached objects in the object runtime ObjectStore.
884    /// Enforced by object runtime during execution
885    object_runtime_max_num_cached_objects: Option<u64>,
886
887    /// Maximum number of cached objects in the object runtime ObjectStore in
888    /// system transaction. Enforced by object runtime during execution
889    object_runtime_max_num_cached_objects_system_tx: Option<u64>,
890
891    /// Maximum number of stored objects accessed by object runtime ObjectStore.
892    /// Enforced by object runtime during execution
893    object_runtime_max_num_store_entries: Option<u64>,
894
895    /// Maximum number of stored objects accessed by object runtime ObjectStore
896    /// in system transaction. Enforced by object runtime during execution
897    object_runtime_max_num_store_entries_system_tx: Option<u64>,
898
899    // === Execution gas costs ====
900
901    //
902    /// Base cost for any IOTA transaction
903    base_tx_cost_fixed: Option<u64>,
904
905    /// Additional cost for a transaction that publishes a package
906    /// i.e., the base cost of such a transaction is base_tx_cost_fixed +
907    /// package_publish_cost_fixed
908    package_publish_cost_fixed: Option<u64>,
909
910    /// Cost per byte of a Move call transaction
911    /// i.e., the cost of such a transaction is base_cost +
912    /// (base_tx_cost_per_byte * size)
913    base_tx_cost_per_byte: Option<u64>,
914
915    /// Cost per byte for a transaction that publishes a package
916    package_publish_cost_per_byte: Option<u64>,
917
918    // Per-byte cost of reading an object during transaction execution
919    obj_access_cost_read_per_byte: Option<u64>,
920
921    // Per-byte cost of writing an object during transaction execution
922    obj_access_cost_mutate_per_byte: Option<u64>,
923
924    // Per-byte cost of deleting an object during transaction execution
925    obj_access_cost_delete_per_byte: Option<u64>,
926
927    /// Per-byte cost charged for each input object to a transaction.
928    /// Meant to approximate the cost of checking locks for each object
929    // TODO: Option<I'm not sure that this cost makes sense. Checking locks is "free"
930    // in the sense that an invalid tx that can never be committed/pay gas can
931    // force validators to check an arbitrary number of locks. If those checks are
932    // "free" for invalid transactions, why charge for them in valid transactions
933    // TODO: Option<if we keep this, I think we probably want it to be a fixed cost rather
934    // than a per-byte cost. checking an object lock should not require loading an
935    // entire object, just consulting an ID -> tx digest map
936    obj_access_cost_verify_per_byte: Option<u64>,
937
938    // Maximal nodes which are allowed when converting to a type layout.
939    max_type_to_layout_nodes: Option<u64>,
940
941    // Maximal size in bytes that a PTB value can be
942    max_ptb_value_size: Option<u64>,
943
944    // === Gas version. gas model ===
945
946    //
947    /// Gas model version, what code we are using to charge gas
948    gas_model_version: Option<u64>,
949
950    // === Storage gas costs ===
951
952    //
953    /// Per-byte cost of storing an object in the IOTA global object store. Some
954    /// of this cost may be refundable if the object is later freed
955    obj_data_cost_refundable: Option<u64>,
956
957    // Per-byte cost of storing an object in the IOTA transaction log (e.g., in
958    // CertifiedTransactionEffects) This depends on the size of various fields including the
959    // effects TODO: Option<I don't fully understand this^ and more details would be useful
960    obj_metadata_cost_non_refundable: Option<u64>,
961
962    // === Tokenomics ===
963
964    // TODO: Option<this should be changed to u64.
965    /// Sender of a txn that touches an object will get this percent of the
966    /// storage rebate back. In basis point.
967    storage_rebate_rate: Option<u64>,
968
969    /// The share of rewards that will be slashed and redistributed is 50%.
970    /// In basis point.
971    reward_slashing_rate: Option<u64>,
972
973    /// Unit storage gas price, Nanos per internal gas unit.
974    storage_gas_price: Option<u64>,
975
976    // Base gas price for computation gas, nanos per computation unit.
977    base_gas_price: Option<u64>,
978
979    /// The number of tokens minted as a validator subsidy per epoch.
980    validator_target_reward: Option<u64>,
981
982    // === Core Protocol ===
983
984    //
985    /// Max number of transactions per checkpoint.
986    /// Note that this is a protocol constant and not a config as validators
987    /// must have this set to the same value, otherwise they *will* fork.
988    max_transactions_per_checkpoint: Option<u64>,
989
990    /// Max size of a checkpoint in bytes.
991    /// Note that this is a protocol constant and not a config as validators
992    /// must have this set to the same value, otherwise they *will* fork.
993    max_checkpoint_size_bytes: Option<u64>,
994
995    /// A protocol upgrade always requires 2f+1 stake to agree. We support a
996    /// buffer of additional stake (as a fraction of f, expressed in basis
997    /// points) that is required before an upgrade can happen automatically.
998    /// 10000bps would indicate that complete unanimity is required (all
999    /// 3f+1 must vote), while 0bps would indicate that 2f+1 is sufficient.
1000    buffer_stake_for_protocol_upgrade_bps: Option<u64>,
1001
1002    // === Native Function Costs ===
1003
1004    // `address` module
1005    // Cost params for the Move native function `address::from_bytes(bytes: vector<u8>)`
1006    address_from_bytes_cost_base: Option<u64>,
1007    // Cost params for the Move native function `address::to_u256(address): u256`
1008    address_to_u256_cost_base: Option<u64>,
1009    // Cost params for the Move native function `address::from_u256(u256): address`
1010    address_from_u256_cost_base: Option<u64>,
1011
1012    // `config` module
1013    // Cost params for the Move native function `read_setting_impl<Name: copy + drop + store,
1014    // SettingValue: key + store, SettingDataValue: store, Value: copy + drop + store,
1015    // >(config: address, name: address, current_epoch: u64): Option<Value>`
1016    config_read_setting_impl_cost_base: Option<u64>,
1017    config_read_setting_impl_cost_per_byte: Option<u64>,
1018
1019    // `dynamic_field` module
1020    // Cost params for the Move native function `hash_type_and_key<K: copy + drop + store>(parent:
1021    // address, k: K): address`
1022    dynamic_field_hash_type_and_key_cost_base: Option<u64>,
1023    dynamic_field_hash_type_and_key_type_cost_per_byte: Option<u64>,
1024    dynamic_field_hash_type_and_key_value_cost_per_byte: Option<u64>,
1025    dynamic_field_hash_type_and_key_type_tag_cost_per_byte: Option<u64>,
1026    // Cost params for the Move native function `add_child_object<Child: key>(parent: address,
1027    // child: Child)`
1028    dynamic_field_add_child_object_cost_base: Option<u64>,
1029    dynamic_field_add_child_object_type_cost_per_byte: Option<u64>,
1030    dynamic_field_add_child_object_value_cost_per_byte: Option<u64>,
1031    dynamic_field_add_child_object_struct_tag_cost_per_byte: Option<u64>,
1032    // Cost params for the Move native function `borrow_child_object_mut<Child: key>(parent: &mut
1033    // UID, id: address): &mut Child`
1034    dynamic_field_borrow_child_object_cost_base: Option<u64>,
1035    dynamic_field_borrow_child_object_child_ref_cost_per_byte: Option<u64>,
1036    dynamic_field_borrow_child_object_type_cost_per_byte: Option<u64>,
1037    // Cost params for the Move native function `remove_child_object<Child: key>(parent: address,
1038    // id: address): Child`
1039    dynamic_field_remove_child_object_cost_base: Option<u64>,
1040    dynamic_field_remove_child_object_child_cost_per_byte: Option<u64>,
1041    dynamic_field_remove_child_object_type_cost_per_byte: Option<u64>,
1042    // Cost params for the Move native function `has_child_object(parent: address, id: address):
1043    // bool`
1044    dynamic_field_has_child_object_cost_base: Option<u64>,
1045    // Cost params for the Move native function `has_child_object_with_ty<Child: key>(parent:
1046    // address, id: address): bool`
1047    dynamic_field_has_child_object_with_ty_cost_base: Option<u64>,
1048    dynamic_field_has_child_object_with_ty_type_cost_per_byte: Option<u64>,
1049    dynamic_field_has_child_object_with_ty_type_tag_cost_per_byte: Option<u64>,
1050
1051    // `event` module
1052    // Cost params for the Move native function `event::emit<T: copy + drop>(event: T)`
1053    event_emit_cost_base: Option<u64>,
1054    event_emit_value_size_derivation_cost_per_byte: Option<u64>,
1055    event_emit_tag_size_derivation_cost_per_byte: Option<u64>,
1056    event_emit_output_cost_per_byte: Option<u64>,
1057
1058    //  `object` module
1059    // Cost params for the Move native function `borrow_uid<T: key>(obj: &T): &UID`
1060    object_borrow_uid_cost_base: Option<u64>,
1061    // Cost params for the Move native function `delete_impl(id: address)`
1062    object_delete_impl_cost_base: Option<u64>,
1063    // Cost params for the Move native function `record_new_uid(id: address)`
1064    object_record_new_uid_cost_base: Option<u64>,
1065
1066    // Transfer
1067    // Cost params for the Move native function `transfer_impl<T: key>(obj: T, recipient: address)`
1068    transfer_transfer_internal_cost_base: Option<u64>,
1069    // Cost params for the Move native function `freeze_object<T: key>(obj: T)`
1070    transfer_freeze_object_cost_base: Option<u64>,
1071    // Cost params for the Move native function `share_object<T: key>(obj: T)`
1072    transfer_share_object_cost_base: Option<u64>,
1073    // Cost params for the Move native function
1074    // `receive_object<T: key>(p: &mut UID, recv: Receiving<T>T)`
1075    transfer_receive_object_cost_base: Option<u64>,
1076
1077    // TxContext
1078    // Cost params for the Move native function `transfer_impl<T: key>(obj: T, recipient: address)`
1079    tx_context_derive_id_cost_base: Option<u64>,
1080    tx_context_fresh_id_cost_base: Option<u64>,
1081    tx_context_sender_cost_base: Option<u64>,
1082    tx_context_digest_cost_base: Option<u64>,
1083    tx_context_epoch_cost_base: Option<u64>,
1084    tx_context_epoch_timestamp_ms_cost_base: Option<u64>,
1085    tx_context_sponsor_cost_base: Option<u64>,
1086    tx_context_rgp_cost_base: Option<u64>,
1087    tx_context_gas_price_cost_base: Option<u64>,
1088    tx_context_gas_budget_cost_base: Option<u64>,
1089    tx_context_ids_created_cost_base: Option<u64>,
1090    tx_context_replace_cost_base: Option<u64>,
1091
1092    // Types
1093    // Cost params for the Move native function `is_one_time_witness<T: drop>(_: &T): bool`
1094    types_is_one_time_witness_cost_base: Option<u64>,
1095    types_is_one_time_witness_type_tag_cost_per_byte: Option<u64>,
1096    types_is_one_time_witness_type_cost_per_byte: Option<u64>,
1097
1098    // Validator
1099    // Cost params for the Move native function `validate_metadata_bcs(metadata: vector<u8>)`
1100    validator_validate_metadata_cost_base: Option<u64>,
1101    validator_validate_metadata_data_cost_per_byte: Option<u64>,
1102
1103    // Crypto natives
1104    crypto_invalid_arguments_cost: Option<u64>,
1105    // bls12381::bls12381_min_sig_verify
1106    bls12381_bls12381_min_sig_verify_cost_base: Option<u64>,
1107    bls12381_bls12381_min_sig_verify_msg_cost_per_byte: Option<u64>,
1108    bls12381_bls12381_min_sig_verify_msg_cost_per_block: Option<u64>,
1109
1110    // bls12381::bls12381_min_pk_verify
1111    bls12381_bls12381_min_pk_verify_cost_base: Option<u64>,
1112    bls12381_bls12381_min_pk_verify_msg_cost_per_byte: Option<u64>,
1113    bls12381_bls12381_min_pk_verify_msg_cost_per_block: Option<u64>,
1114
1115    // ecdsa_k1::ecrecover
1116    ecdsa_k1_ecrecover_keccak256_cost_base: Option<u64>,
1117    ecdsa_k1_ecrecover_keccak256_msg_cost_per_byte: Option<u64>,
1118    ecdsa_k1_ecrecover_keccak256_msg_cost_per_block: Option<u64>,
1119    ecdsa_k1_ecrecover_sha256_cost_base: Option<u64>,
1120    ecdsa_k1_ecrecover_sha256_msg_cost_per_byte: Option<u64>,
1121    ecdsa_k1_ecrecover_sha256_msg_cost_per_block: Option<u64>,
1122
1123    // ecdsa_k1::decompress_pubkey
1124    ecdsa_k1_decompress_pubkey_cost_base: Option<u64>,
1125
1126    // ecdsa_k1::secp256k1_verify
1127    ecdsa_k1_secp256k1_verify_keccak256_cost_base: Option<u64>,
1128    ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_byte: Option<u64>,
1129    ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_block: Option<u64>,
1130    ecdsa_k1_secp256k1_verify_sha256_cost_base: Option<u64>,
1131    ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_byte: Option<u64>,
1132    ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_block: Option<u64>,
1133
1134    // ecdsa_r1::ecrecover
1135    ecdsa_r1_ecrecover_keccak256_cost_base: Option<u64>,
1136    ecdsa_r1_ecrecover_keccak256_msg_cost_per_byte: Option<u64>,
1137    ecdsa_r1_ecrecover_keccak256_msg_cost_per_block: Option<u64>,
1138    ecdsa_r1_ecrecover_sha256_cost_base: Option<u64>,
1139    ecdsa_r1_ecrecover_sha256_msg_cost_per_byte: Option<u64>,
1140    ecdsa_r1_ecrecover_sha256_msg_cost_per_block: Option<u64>,
1141
1142    // ecdsa_r1::secp256k1_verify
1143    ecdsa_r1_secp256r1_verify_keccak256_cost_base: Option<u64>,
1144    ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_byte: Option<u64>,
1145    ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_block: Option<u64>,
1146    ecdsa_r1_secp256r1_verify_sha256_cost_base: Option<u64>,
1147    ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_byte: Option<u64>,
1148    ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_block: Option<u64>,
1149
1150    // ecvrf::verify
1151    ecvrf_ecvrf_verify_cost_base: Option<u64>,
1152    ecvrf_ecvrf_verify_alpha_string_cost_per_byte: Option<u64>,
1153    ecvrf_ecvrf_verify_alpha_string_cost_per_block: Option<u64>,
1154
1155    // ed25519
1156    ed25519_ed25519_verify_cost_base: Option<u64>,
1157    ed25519_ed25519_verify_msg_cost_per_byte: Option<u64>,
1158    ed25519_ed25519_verify_msg_cost_per_block: Option<u64>,
1159
1160    // groth16::prepare_verifying_key
1161    groth16_prepare_verifying_key_bls12381_cost_base: Option<u64>,
1162    groth16_prepare_verifying_key_bn254_cost_base: Option<u64>,
1163
1164    // groth16::verify_groth16_proof_internal
1165    groth16_verify_groth16_proof_internal_bls12381_cost_base: Option<u64>,
1166    groth16_verify_groth16_proof_internal_bls12381_cost_per_public_input: Option<u64>,
1167    groth16_verify_groth16_proof_internal_bn254_cost_base: Option<u64>,
1168    groth16_verify_groth16_proof_internal_bn254_cost_per_public_input: Option<u64>,
1169    groth16_verify_groth16_proof_internal_public_input_cost_per_byte: Option<u64>,
1170
1171    // hash::blake2b256
1172    hash_blake2b256_cost_base: Option<u64>,
1173    hash_blake2b256_data_cost_per_byte: Option<u64>,
1174    hash_blake2b256_data_cost_per_block: Option<u64>,
1175
1176    // hash::keccak256
1177    hash_keccak256_cost_base: Option<u64>,
1178    hash_keccak256_data_cost_per_byte: Option<u64>,
1179    hash_keccak256_data_cost_per_block: Option<u64>,
1180
1181    // poseidon::poseidon_bn254
1182    poseidon_bn254_cost_base: Option<u64>,
1183    poseidon_bn254_cost_per_block: Option<u64>,
1184
1185    // group_ops
1186    group_ops_bls12381_decode_scalar_cost: Option<u64>,
1187    group_ops_bls12381_decode_g1_cost: Option<u64>,
1188    group_ops_bls12381_decode_g2_cost: Option<u64>,
1189    group_ops_bls12381_decode_gt_cost: Option<u64>,
1190    group_ops_bls12381_scalar_add_cost: Option<u64>,
1191    group_ops_bls12381_g1_add_cost: Option<u64>,
1192    group_ops_bls12381_g2_add_cost: Option<u64>,
1193    group_ops_bls12381_gt_add_cost: Option<u64>,
1194    group_ops_bls12381_scalar_sub_cost: Option<u64>,
1195    group_ops_bls12381_g1_sub_cost: Option<u64>,
1196    group_ops_bls12381_g2_sub_cost: Option<u64>,
1197    group_ops_bls12381_gt_sub_cost: Option<u64>,
1198    group_ops_bls12381_scalar_mul_cost: Option<u64>,
1199    group_ops_bls12381_g1_mul_cost: Option<u64>,
1200    group_ops_bls12381_g2_mul_cost: Option<u64>,
1201    group_ops_bls12381_gt_mul_cost: Option<u64>,
1202    group_ops_bls12381_scalar_div_cost: Option<u64>,
1203    group_ops_bls12381_g1_div_cost: Option<u64>,
1204    group_ops_bls12381_g2_div_cost: Option<u64>,
1205    group_ops_bls12381_gt_div_cost: Option<u64>,
1206    group_ops_bls12381_g1_hash_to_base_cost: Option<u64>,
1207    group_ops_bls12381_g2_hash_to_base_cost: Option<u64>,
1208    group_ops_bls12381_g1_hash_to_cost_per_byte: Option<u64>,
1209    group_ops_bls12381_g2_hash_to_cost_per_byte: Option<u64>,
1210    group_ops_bls12381_g1_msm_base_cost: Option<u64>,
1211    group_ops_bls12381_g2_msm_base_cost: Option<u64>,
1212    group_ops_bls12381_g1_msm_base_cost_per_input: Option<u64>,
1213    group_ops_bls12381_g2_msm_base_cost_per_input: Option<u64>,
1214    group_ops_bls12381_msm_max_len: Option<u32>,
1215    group_ops_bls12381_pairing_cost: Option<u64>,
1216    group_ops_bls12381_g1_to_uncompressed_g1_cost: Option<u64>,
1217    group_ops_bls12381_uncompressed_g1_to_g1_cost: Option<u64>,
1218    group_ops_bls12381_uncompressed_g1_sum_base_cost: Option<u64>,
1219    group_ops_bls12381_uncompressed_g1_sum_cost_per_term: Option<u64>,
1220    group_ops_bls12381_uncompressed_g1_sum_max_terms: Option<u64>,
1221
1222    // hmac::hmac_sha3_256
1223    hmac_hmac_sha3_256_cost_base: Option<u64>,
1224    hmac_hmac_sha3_256_input_cost_per_byte: Option<u64>,
1225    hmac_hmac_sha3_256_input_cost_per_block: Option<u64>,
1226
1227    // zklogin::check_zklogin_id
1228    #[deprecated]
1229    check_zklogin_id_cost_base: Option<u64>,
1230    // zklogin::check_zklogin_issuer
1231    #[deprecated]
1232    check_zklogin_issuer_cost_base: Option<u64>,
1233
1234    vdf_verify_vdf_cost: Option<u64>,
1235    vdf_hash_to_input_cost: Option<u64>,
1236
1237    // Stdlib costs
1238    bcs_per_byte_serialized_cost: Option<u64>,
1239    bcs_legacy_min_output_size_cost: Option<u64>,
1240    bcs_failure_cost: Option<u64>,
1241
1242    hash_sha2_256_base_cost: Option<u64>,
1243    hash_sha2_256_per_byte_cost: Option<u64>,
1244    hash_sha2_256_legacy_min_input_len_cost: Option<u64>,
1245    hash_sha3_256_base_cost: Option<u64>,
1246    hash_sha3_256_per_byte_cost: Option<u64>,
1247    hash_sha3_256_legacy_min_input_len_cost: Option<u64>,
1248    type_name_get_base_cost: Option<u64>,
1249    type_name_get_per_byte_cost: Option<u64>,
1250
1251    string_check_utf8_base_cost: Option<u64>,
1252    string_check_utf8_per_byte_cost: Option<u64>,
1253    string_is_char_boundary_base_cost: Option<u64>,
1254    string_sub_string_base_cost: Option<u64>,
1255    string_sub_string_per_byte_cost: Option<u64>,
1256    string_index_of_base_cost: Option<u64>,
1257    string_index_of_per_byte_pattern_cost: Option<u64>,
1258    string_index_of_per_byte_searched_cost: Option<u64>,
1259
1260    vector_empty_base_cost: Option<u64>,
1261    vector_length_base_cost: Option<u64>,
1262    vector_push_back_base_cost: Option<u64>,
1263    vector_push_back_legacy_per_abstract_memory_unit_cost: Option<u64>,
1264    vector_borrow_base_cost: Option<u64>,
1265    vector_pop_back_base_cost: Option<u64>,
1266    vector_destroy_empty_base_cost: Option<u64>,
1267    vector_swap_base_cost: Option<u64>,
1268    debug_print_base_cost: Option<u64>,
1269    debug_print_stack_trace_base_cost: Option<u64>,
1270
1271    // === Execution Version ===
1272    execution_version: Option<u64>,
1273
1274    // Dictates the threshold (percentage of stake) that is used to calculate the "bad" nodes to be
1275    // swapped when creating the consensus schedule. The values should be of the range [0 - 33].
1276    // Anything above 33 (f) will not be allowed.
1277    consensus_bad_nodes_stake_threshold: Option<u64>,
1278
1279    #[deprecated]
1280    max_jwk_votes_per_validator_per_epoch: Option<u64>,
1281    // The maximum age of a JWK in epochs before it is removed from the AuthenticatorState object.
1282    // Applied at the end of an epoch as a delta from the new epoch value, so setting this to 1
1283    // will cause the new epoch to start with JWKs from the previous epoch still valid.
1284    #[deprecated]
1285    max_age_of_jwk_in_epochs: Option<u64>,
1286
1287    // === random beacon ===
1288    /// Maximum allowed precision loss when reducing voting weights for the
1289    /// random beacon protocol.
1290    random_beacon_reduction_allowed_delta: Option<u16>,
1291
1292    /// Minimum number of shares below which voting weights will not be reduced
1293    /// for the random beacon protocol.
1294    random_beacon_reduction_lower_bound: Option<u32>,
1295
1296    /// Consensus Round after which DKG should be aborted and randomness
1297    /// disabled for the epoch, if it hasn't already completed.
1298    random_beacon_dkg_timeout_round: Option<u32>,
1299
1300    /// Minimum interval between consecutive rounds of generated randomness.
1301    random_beacon_min_round_interval_ms: Option<u64>,
1302
1303    /// Version of the random beacon DKG protocol.
1304    /// 0 was deprecated (and currently not supported), 1 is the default
1305    /// version.
1306    random_beacon_dkg_version: Option<u64>,
1307
1308    /// The maximum serialized transaction size (in bytes) accepted by
1309    /// consensus. `consensus_max_transaction_size_bytes` should include
1310    /// space for additional metadata, on top of the `max_tx_size_bytes`
1311    /// value.
1312    consensus_max_transaction_size_bytes: Option<u64>,
1313    /// The maximum size of transactions included in a consensus block.
1314    consensus_max_transactions_in_block_bytes: Option<u64>,
1315    /// The maximum number of transactions included in a consensus block.
1316    consensus_max_num_transactions_in_block: Option<u64>,
1317
1318    /// The max number of consensus rounds a transaction can be deferred due to
1319    /// shared object congestion. Transactions will be cancelled after this
1320    /// many rounds.
1321    max_deferral_rounds_for_congestion_control: Option<u64>,
1322
1323    /// Minimum interval of commit timestamps between consecutive checkpoints.
1324    min_checkpoint_interval_ms: Option<u64>,
1325
1326    /// Version number to use for version_specific_data in `CheckpointSummary`.
1327    checkpoint_summary_version_specific_data: Option<u64>,
1328
1329    /// The max number of transactions that can be included in a single Soft
1330    /// Bundle.
1331    max_soft_bundle_size: Option<u64>,
1332
1333    /// Deprecated because of bridge removal.
1334    /// Whether to try to form bridge committee
1335    // Note: this is not a feature flag because we want to distinguish between
1336    // `None` and `Some(false)`, as committee was already finalized on Testnet.
1337    bridge_should_try_to_finalize_committee: Option<bool>,
1338
1339    /// The max accumulated txn execution cost per object in a mysticeti commit.
1340    /// Transactions in a commit will be deferred once their touch shared
1341    /// objects hit this limit. Note that if
1342    /// `max_congestion_limit_overshoot_per_commit` is set, this may be overshot
1343    /// within a single commit, but the limit will be enforced in the long run.
1344    max_accumulated_txn_cost_per_object_in_mysticeti_commit: Option<u64>,
1345
1346    /// Maximum number of committee (validators taking part in consensus)
1347    /// validators at any moment. We do not allow the number of committee
1348    /// validators in any epoch to go above this.
1349    max_committee_members_count: Option<u64>,
1350
1351    /// Configures the garbage collection depth for consensus. When is unset or
1352    /// `0` then the garbage collection is disabled.
1353    consensus_gc_depth: Option<u32>,
1354
1355    /// Configures the maximum number of acknowledgments to be included in a
1356    /// block. It must be reasonably larger than the number of validators
1357    /// because not all validators create their blocks at the same pace.
1358    /// Default value set to 400. (5 x expected committee size (80)).
1359    /// Applicable only to `starfish` consensus.
1360    consensus_max_acknowledgments_per_block: Option<u32>,
1361
1362    /// The maximum amount that is allowed to overshoot the congestion limit
1363    /// specified by 'max_accumulated_txn_cost_per_object_in_mysticeti_commit'
1364    /// for any single commit. Any overshoot is tracked as a debt that must
1365    /// be accounted for in subsequent commits.
1366    max_congestion_limit_overshoot_per_commit: Option<u64>,
1367
1368    /// Scorer version. When set to `None`, MisbehaviorReports are not sent nor
1369    /// considered valid. When set to `Some(version)`, scores are included in
1370    /// the MisbehaviorReports messages, where `version` determines the scoring
1371    /// formulas and metrics to be used. Even if set to None, the Scorer
1372    /// component is created, having access to metrics and being able to expose
1373    /// validator scores. Also gates the wire format of the
1374    /// `MisbehaviorReport` consensus transaction — scorer and report bump
1375    /// together.
1376    scorer_version: Option<u16>,
1377
1378    // `auth_context` module
1379    // Cost params for the Move native function `native_digest(): vector<u8>`
1380    auth_context_digest_cost_base: Option<u64>,
1381    // Cost params for the Move native function `native_tx_data_bytes(): &vector<u8>`
1382    auth_context_tx_data_bytes_cost_base: Option<u64>,
1383    auth_context_tx_data_bytes_cost_per_byte: Option<u64>,
1384    // Cost params for the Move native function `native_tx_commands<C>(): vector<C>`
1385    auth_context_tx_commands_cost_base: Option<u64>,
1386    auth_context_tx_commands_cost_per_byte: Option<u64>,
1387    // Cost params for the Move native function `native_tx_inputs<I>(): vector<I>`
1388    auth_context_tx_inputs_cost_base: Option<u64>,
1389    auth_context_tx_inputs_cost_per_byte: Option<u64>,
1390    // Cost params for the Move native function `fun native_replace<I, C>(auth_digest: vector<u8>,
1391    // tx_inputs: vector<I>, tx_commands: vector<C>, tx_data_bytes: vector<u8>)`
1392    auth_context_replace_cost_base: Option<u64>,
1393    auth_context_replace_cost_per_byte: Option<u64>,
1394    // Cost params for the Move native functions
1395    // `fun native_sender_authenticator_function_info_v1<F>(): &Option<F>`
1396    // `fun native_sponsor_authenticator_function_info_v1<F>(): &Option<F>`
1397    auth_context_authenticator_function_info_v1_cost_base: Option<u64>,
1398}
1399
1400// feature flags
1401impl ProtocolConfig {
1402    // Add checks for feature flag support here, e.g.:
1403    // pub fn check_new_protocol_feature_supported(&self) -> Result<(), Error> {
1404    //     if self.feature_flags.new_protocol_feature_supported {
1405    //         Ok(())
1406    //     } else {
1407    //         Err(Error(format!(
1408    //             "new_protocol_feature is not supported at {:?}",
1409    //             self.version
1410    //         )))
1411    //     }
1412    // }
1413
1414    pub fn disable_invariant_violation_check_in_swap_loc(&self) -> bool {
1415        self.feature_flags
1416            .disable_invariant_violation_check_in_swap_loc
1417    }
1418
1419    pub fn no_extraneous_module_bytes(&self) -> bool {
1420        self.feature_flags.no_extraneous_module_bytes
1421    }
1422
1423    pub fn consensus_transaction_ordering(&self) -> ConsensusTransactionOrdering {
1424        self.feature_flags.consensus_transaction_ordering
1425    }
1426
1427    pub fn dkg_version(&self) -> u64 {
1428        // Version 0 was deprecated and removed, the default is 1 if not set.
1429        self.random_beacon_dkg_version.unwrap_or(1)
1430    }
1431
1432    pub fn hardened_otw_check(&self) -> bool {
1433        self.feature_flags.hardened_otw_check
1434    }
1435
1436    pub fn enable_poseidon(&self) -> bool {
1437        self.feature_flags.enable_poseidon
1438    }
1439
1440    pub fn enable_group_ops_native_function_msm(&self) -> bool {
1441        self.feature_flags.enable_group_ops_native_function_msm
1442    }
1443
1444    pub fn per_object_congestion_control_mode(&self) -> PerObjectCongestionControlMode {
1445        self.feature_flags.per_object_congestion_control_mode
1446    }
1447
1448    pub fn consensus_choice(&self) -> ConsensusChoice {
1449        self.feature_flags.consensus_choice
1450    }
1451
1452    pub fn consensus_network(&self) -> ConsensusNetwork {
1453        self.feature_flags.consensus_network
1454    }
1455
1456    pub fn enable_vdf(&self) -> bool {
1457        self.feature_flags.enable_vdf
1458    }
1459
1460    pub fn passkey_auth(&self) -> bool {
1461        self.feature_flags.passkey_auth
1462    }
1463
1464    pub fn max_transaction_size_bytes(&self) -> u64 {
1465        // Provide a default value if protocol config version is too low.
1466        self.consensus_max_transaction_size_bytes
1467            .unwrap_or(256 * 1024)
1468    }
1469
1470    pub fn max_transactions_in_block_bytes(&self) -> u64 {
1471        if cfg!(msim) {
1472            256 * 1024
1473        } else {
1474            self.consensus_max_transactions_in_block_bytes
1475                .unwrap_or(512 * 1024)
1476        }
1477    }
1478
1479    pub fn max_num_transactions_in_block(&self) -> u64 {
1480        if cfg!(msim) {
1481            8
1482        } else {
1483            self.consensus_max_num_transactions_in_block.unwrap_or(512)
1484        }
1485    }
1486
1487    pub fn rethrow_serialization_type_layout_errors(&self) -> bool {
1488        self.feature_flags.rethrow_serialization_type_layout_errors
1489    }
1490
1491    pub fn relocate_event_module(&self) -> bool {
1492        self.feature_flags.relocate_event_module
1493    }
1494
1495    pub fn protocol_defined_base_fee(&self) -> bool {
1496        self.feature_flags.protocol_defined_base_fee
1497    }
1498
1499    pub fn uncompressed_g1_group_elements(&self) -> bool {
1500        self.feature_flags.uncompressed_g1_group_elements
1501    }
1502
1503    pub fn disallow_new_modules_in_deps_only_packages(&self) -> bool {
1504        self.feature_flags
1505            .disallow_new_modules_in_deps_only_packages
1506    }
1507
1508    pub fn native_charging_v2(&self) -> bool {
1509        self.feature_flags.native_charging_v2
1510    }
1511
1512    pub fn consensus_round_prober(&self) -> bool {
1513        self.feature_flags.consensus_round_prober
1514    }
1515
1516    pub fn consensus_distributed_vote_scoring_strategy(&self) -> bool {
1517        self.feature_flags
1518            .consensus_distributed_vote_scoring_strategy
1519    }
1520
1521    pub fn gc_depth(&self) -> u32 {
1522        if cfg!(msim) {
1523            // exercise a very low gc_depth
1524            min(5, self.consensus_gc_depth.unwrap_or(0))
1525        } else {
1526            self.consensus_gc_depth.unwrap_or(0)
1527        }
1528    }
1529
1530    pub fn consensus_linearize_subdag_v2(&self) -> bool {
1531        let res = self.feature_flags.consensus_linearize_subdag_v2;
1532        assert!(
1533            !res || self.gc_depth() > 0,
1534            "The consensus linearize sub dag V2 requires GC to be enabled"
1535        );
1536        res
1537    }
1538
1539    pub fn consensus_max_acknowledgments_per_block_or_default(&self) -> u32 {
1540        self.consensus_max_acknowledgments_per_block.unwrap_or(400)
1541    }
1542
1543    pub fn max_acknowledgments_per_block(&self, committee_size: usize) -> usize {
1544        if self.consensus_block_restrictions() {
1545            2 * committee_size
1546        } else {
1547            self.consensus_max_acknowledgments_per_block_or_default() as usize
1548        }
1549    }
1550
1551    pub fn max_commit_votes_per_block(&self, committee_size: usize) -> usize {
1552        if self.consensus_block_restrictions() {
1553            committee_size
1554        } else {
1555            100
1556        }
1557    }
1558
1559    pub fn variant_nodes(&self) -> bool {
1560        self.feature_flags.variant_nodes
1561    }
1562
1563    pub fn consensus_smart_ancestor_selection(&self) -> bool {
1564        self.feature_flags.consensus_smart_ancestor_selection
1565    }
1566
1567    pub fn consensus_round_prober_probe_accepted_rounds(&self) -> bool {
1568        self.feature_flags
1569            .consensus_round_prober_probe_accepted_rounds
1570    }
1571
1572    pub fn consensus_zstd_compression(&self) -> bool {
1573        self.feature_flags.consensus_zstd_compression
1574    }
1575
1576    pub fn congestion_control_min_free_execution_slot(&self) -> bool {
1577        self.feature_flags
1578            .congestion_control_min_free_execution_slot
1579    }
1580
1581    pub fn accept_passkey_in_multisig(&self) -> bool {
1582        self.feature_flags.accept_passkey_in_multisig
1583    }
1584
1585    pub fn consensus_batched_block_sync(&self) -> bool {
1586        self.feature_flags.consensus_batched_block_sync
1587    }
1588
1589    /// Check if the gas price feedback mechanism (which is used for
1590    /// transactions cancelled due to shared object congestion) is enabled
1591    pub fn congestion_control_gas_price_feedback_mechanism(&self) -> bool {
1592        self.feature_flags
1593            .congestion_control_gas_price_feedback_mechanism
1594    }
1595
1596    pub fn validate_identifier_inputs(&self) -> bool {
1597        self.feature_flags.validate_identifier_inputs
1598    }
1599
1600    pub fn minimize_child_object_mutations(&self) -> bool {
1601        self.feature_flags.minimize_child_object_mutations
1602    }
1603
1604    pub fn dependency_linkage_error(&self) -> bool {
1605        self.feature_flags.dependency_linkage_error
1606    }
1607
1608    pub fn additional_multisig_checks(&self) -> bool {
1609        self.feature_flags.additional_multisig_checks
1610    }
1611
1612    pub fn consensus_num_requested_prior_commits_at_startup(&self) -> u32 {
1613        // TODO: this will eventually be the max of some number of other
1614        // parameters.
1615        0
1616    }
1617
1618    pub fn normalize_ptb_arguments(&self) -> bool {
1619        self.feature_flags.normalize_ptb_arguments
1620    }
1621
1622    pub fn select_committee_from_eligible_validators(&self) -> bool {
1623        let res = self.feature_flags.select_committee_from_eligible_validators;
1624        assert!(
1625            !res || (self.protocol_defined_base_fee()
1626                && self.max_committee_members_count_as_option().is_some()),
1627            "select_committee_from_eligible_validators requires protocol_defined_base_fee and max_committee_members_count to be set"
1628        );
1629        res
1630    }
1631
1632    pub fn track_non_committee_eligible_validators(&self) -> bool {
1633        self.feature_flags.track_non_committee_eligible_validators
1634    }
1635
1636    pub fn select_committee_supporting_next_epoch_version(&self) -> bool {
1637        let res = self
1638            .feature_flags
1639            .select_committee_supporting_next_epoch_version;
1640        assert!(
1641            !res || (self.track_non_committee_eligible_validators()
1642                && self.select_committee_from_eligible_validators()),
1643            "select_committee_supporting_next_epoch_version requires select_committee_from_eligible_validators to be set"
1644        );
1645        res
1646    }
1647
1648    pub fn consensus_median_timestamp_with_checkpoint_enforcement(&self) -> bool {
1649        let res = self
1650            .feature_flags
1651            .consensus_median_timestamp_with_checkpoint_enforcement;
1652        assert!(
1653            !res || self.gc_depth() > 0,
1654            "The consensus median timestamp with checkpoint enforcement requires GC to be enabled"
1655        );
1656        res
1657    }
1658
1659    pub fn consensus_commit_transactions_only_for_traversed_headers(&self) -> bool {
1660        self.feature_flags
1661            .consensus_commit_transactions_only_for_traversed_headers
1662    }
1663
1664    /// Check whether congestion limit overshoot is enabled in the gas price
1665    /// feedback mechanism.
1666    pub fn congestion_limit_overshoot_in_gas_price_feedback_mechanism(&self) -> bool {
1667        self.feature_flags
1668            .congestion_limit_overshoot_in_gas_price_feedback_mechanism
1669    }
1670
1671    /// Check whether a separate gas price feedback mechanism is used for
1672    /// randomness transactions.
1673    pub fn separate_gas_price_feedback_mechanism_for_randomness(&self) -> bool {
1674        self.feature_flags
1675            .separate_gas_price_feedback_mechanism_for_randomness
1676    }
1677
1678    pub fn metadata_in_module_bytes(&self) -> bool {
1679        self.feature_flags.metadata_in_module_bytes
1680    }
1681
1682    pub fn publish_package_metadata(&self) -> bool {
1683        self.feature_flags.publish_package_metadata
1684    }
1685
1686    pub fn enable_move_authentication(&self) -> bool {
1687        self.feature_flags.enable_move_authentication
1688    }
1689
1690    pub fn additional_borrow_checks(&self) -> bool {
1691        self.feature_flags.additional_borrow_checks
1692    }
1693
1694    pub fn enable_move_authentication_for_sponsor(&self) -> bool {
1695        let enable_move_authentication_for_sponsor =
1696            self.feature_flags.enable_move_authentication_for_sponsor;
1697        assert!(
1698            !enable_move_authentication_for_sponsor || self.enable_move_authentication(),
1699            "enable_move_authentication_for_sponsor requires enable_move_authentication to be set"
1700        );
1701        enable_move_authentication_for_sponsor
1702    }
1703
1704    pub fn pass_validator_scores_to_advance_epoch(&self) -> bool {
1705        self.feature_flags.pass_validator_scores_to_advance_epoch
1706    }
1707
1708    pub fn calculate_validator_scores(&self) -> bool {
1709        let calculate_validator_scores = self.feature_flags.calculate_validator_scores;
1710        assert!(
1711            !calculate_validator_scores || self.scorer_version.is_some(),
1712            "calculate_validator_scores requires scorer_version to be set"
1713        );
1714        calculate_validator_scores
1715    }
1716
1717    pub fn adjust_rewards_by_score(&self) -> bool {
1718        let adjust = self.feature_flags.adjust_rewards_by_score;
1719        assert!(
1720            !adjust || (self.scorer_version.is_some() && self.calculate_validator_scores()),
1721            "adjust_rewards_by_score requires scorer_version to be set"
1722        );
1723        adjust
1724    }
1725
1726    pub fn pass_calculated_validator_scores_to_advance_epoch(&self) -> bool {
1727        let pass = self
1728            .feature_flags
1729            .pass_calculated_validator_scores_to_advance_epoch;
1730        assert!(
1731            !pass
1732                || (self.pass_validator_scores_to_advance_epoch()
1733                    && self.calculate_validator_scores()),
1734            "pass_calculated_validator_scores_to_advance_epoch requires pass_validator_scores_to_advance_epoch and calculate_validator_scores to be enabled"
1735        );
1736        pass
1737    }
1738    pub fn consensus_fast_commit_sync(&self) -> bool {
1739        let res = self.feature_flags.consensus_fast_commit_sync;
1740        assert!(
1741            !res || self.consensus_commit_transactions_only_for_traversed_headers(),
1742            "consensus_fast_commit_sync requires consensus_commit_transactions_only_for_traversed_headers to be enabled"
1743        );
1744        res
1745    }
1746
1747    pub fn consensus_block_restrictions(&self) -> bool {
1748        self.feature_flags.consensus_block_restrictions
1749    }
1750
1751    pub fn move_native_tx_context(&self) -> bool {
1752        self.feature_flags.move_native_tx_context
1753    }
1754
1755    pub fn pre_consensus_sponsor_only_move_authentication(&self) -> bool {
1756        let pre_consensus_sponsor_only_move_authentication = self
1757            .feature_flags
1758            .pre_consensus_sponsor_only_move_authentication;
1759        if pre_consensus_sponsor_only_move_authentication {
1760            assert!(
1761                self.enable_move_authentication(),
1762                "pre_consensus_sponsor_only_move_authentication requires enable_move_authentication to be set"
1763            );
1764            assert!(
1765                self.enable_move_authentication_for_sponsor(),
1766                "pre_consensus_sponsor_only_move_authentication requires enable_move_authentication_for_sponsor to be set"
1767            );
1768        }
1769        pre_consensus_sponsor_only_move_authentication
1770    }
1771
1772    pub fn consensus_starfish_speed(&self) -> bool {
1773        let res = self.feature_flags.consensus_starfish_speed;
1774        assert!(
1775            !res || self.consensus_fast_commit_sync(),
1776            "consensus_starfish_speed requires consensus_fast_commit_sync to be enabled"
1777        );
1778        res
1779    }
1780
1781    pub fn always_advance_dkg_to_resolution(&self) -> bool {
1782        self.feature_flags.always_advance_dkg_to_resolution
1783    }
1784
1785    pub fn enable_white_flag_flow(&self) -> bool {
1786        self.feature_flags.enable_white_flag_flow
1787    }
1788}
1789
1790#[cfg(not(msim))]
1791static POISON_VERSION_METHODS: AtomicBool = const { AtomicBool::new(false) };
1792
1793// Use a thread local in sim tests for test isolation.
1794#[cfg(msim)]
1795thread_local! {
1796    static POISON_VERSION_METHODS: AtomicBool = const { AtomicBool::new(false) };
1797}
1798
1799// Instantiations for each protocol version.
1800impl ProtocolConfig {
1801    /// Get the value ProtocolConfig that are in effect during the given
1802    /// protocol version.
1803    pub fn get_for_version(version: ProtocolVersion, chain: Chain) -> Self {
1804        // ProtocolVersion can be deserialized so we need to check it here as well.
1805        assert!(
1806            version >= ProtocolVersion::MIN,
1807            "Network protocol version is {:?}, but the minimum supported version by the binary is {:?}. Please upgrade the binary.",
1808            version,
1809            ProtocolVersion::MIN.0,
1810        );
1811        assert!(
1812            version <= ProtocolVersion::MAX_ALLOWED,
1813            "Network protocol version is {:?}, but the maximum supported version by the binary is {:?}. Please upgrade the binary.",
1814            version,
1815            ProtocolVersion::MAX_ALLOWED.0,
1816        );
1817
1818        let mut ret = Self::get_for_version_impl(version, chain);
1819        ret.version = version;
1820
1821        ret = CONFIG_OVERRIDE.with(|ovr| {
1822            if let Some(override_fn) = &*ovr.borrow() {
1823                warn!(
1824                    "overriding ProtocolConfig settings with custom settings (you should not see this log outside of tests)"
1825                );
1826                override_fn(version, ret)
1827            } else {
1828                ret
1829            }
1830        });
1831
1832        if std::env::var("IOTA_PROTOCOL_CONFIG_OVERRIDE_ENABLE").is_ok() {
1833            warn!(
1834                "overriding ProtocolConfig settings with custom settings; this may break non-local networks"
1835            );
1836
1837            // First, deserialize the top-level ProtocolConfig fields
1838            let overrides: ProtocolConfigOptional =
1839                serde_env::from_env_with_prefix("IOTA_PROTOCOL_CONFIG_OVERRIDE")
1840                    .expect("failed to parse ProtocolConfig override env variables");
1841            overrides.apply_to(&mut ret);
1842
1843            // Then, separately deserialize FeatureFlags fields
1844            let feature_flag_overrides: FeatureFlagsOptional =
1845                serde_env::from_env_with_prefix("IOTA_PROTOCOL_CONFIG_FEATURE_FLAGS_OVERRIDE")
1846                    .expect("failed to parse ProtocolConfig feature flags override env variables");
1847
1848            feature_flag_overrides.apply_to(&mut ret.feature_flags);
1849        }
1850
1851        ret
1852    }
1853
1854    /// Get the value ProtocolConfig that are in effect during the given
1855    /// protocol version. Or none if the version is not supported.
1856    pub fn get_for_version_if_supported(version: ProtocolVersion, chain: Chain) -> Option<Self> {
1857        if version.0 >= ProtocolVersion::MIN.0 && version.0 <= ProtocolVersion::MAX_ALLOWED.0 {
1858            let mut ret = Self::get_for_version_impl(version, chain);
1859            ret.version = version;
1860            Some(ret)
1861        } else {
1862            None
1863        }
1864    }
1865
1866    #[cfg(not(msim))]
1867    pub fn poison_get_for_min_version() {
1868        POISON_VERSION_METHODS.store(true, Ordering::Relaxed);
1869    }
1870
1871    #[cfg(not(msim))]
1872    fn load_poison_get_for_min_version() -> bool {
1873        POISON_VERSION_METHODS.load(Ordering::Relaxed)
1874    }
1875
1876    #[cfg(msim)]
1877    pub fn poison_get_for_min_version() {
1878        POISON_VERSION_METHODS.with(|p| p.store(true, Ordering::Relaxed));
1879    }
1880
1881    #[cfg(msim)]
1882    fn load_poison_get_for_min_version() -> bool {
1883        POISON_VERSION_METHODS.with(|p| p.load(Ordering::Relaxed))
1884    }
1885
1886    pub fn convert_type_argument_error(&self) -> bool {
1887        self.feature_flags.convert_type_argument_error
1888    }
1889
1890    /// Convenience to get the constants at the current minimum supported
1891    /// version. Mainly used by client code that may not yet be
1892    /// protocol-version aware.
1893    pub fn get_for_min_version() -> Self {
1894        if Self::load_poison_get_for_min_version() {
1895            panic!("get_for_min_version called on validator");
1896        }
1897        ProtocolConfig::get_for_version(ProtocolVersion::MIN, Chain::Unknown)
1898    }
1899
1900    /// CAREFUL! - You probably want to use `get_for_version` instead.
1901    ///
1902    /// Convenience to get the constants at the current maximum supported
1903    /// version. Mainly used by genesis. Note well that this function uses
1904    /// the max version supported locally by the node, which is not
1905    /// necessarily the current version of the network. ALSO, this function
1906    /// disregards chain specific config (by using Chain::Unknown), thereby
1907    /// potentially returning a protocol config that is incorrect for some
1908    /// feature flags. Definitely safe for testing and for protocol version
1909    /// 11 and prior.
1910    #[expect(non_snake_case)]
1911    pub fn get_for_max_version_UNSAFE() -> Self {
1912        if Self::load_poison_get_for_min_version() {
1913            panic!("get_for_max_version_UNSAFE called on validator");
1914        }
1915        ProtocolConfig::get_for_version(ProtocolVersion::MAX, Chain::Unknown)
1916    }
1917
1918    fn get_for_version_impl(version: ProtocolVersion, chain: Chain) -> Self {
1919        #[cfg(msim)]
1920        {
1921            // populate the fake simulator version # with a different base tx cost.
1922            if version > ProtocolVersion::MAX {
1923                let mut config = Self::get_for_version_impl(ProtocolVersion::MAX, Chain::Unknown);
1924                config.base_tx_cost_fixed = Some(config.base_tx_cost_fixed() + 1000);
1925                return config;
1926            }
1927        }
1928
1929        // IMPORTANT: Never modify the value of any constant for a pre-existing protocol
1930        // version. To change the values here you must create a new protocol
1931        // version with the new values!
1932        let mut cfg = Self {
1933            version,
1934
1935            feature_flags: Default::default(),
1936
1937            max_tx_size_bytes: Some(128 * 1024),
1938            // We need this number to be at least 100x less than
1939            // `max_serialized_tx_effects_size_bytes`otherwise effects can be huge
1940            max_input_objects: Some(2048),
1941            max_serialized_tx_effects_size_bytes: Some(512 * 1024),
1942            max_serialized_tx_effects_size_bytes_system_tx: Some(512 * 1024 * 16),
1943            max_gas_payment_objects: Some(256),
1944            max_modules_in_publish: Some(64),
1945            max_package_dependencies: Some(32),
1946            max_arguments: Some(512),
1947            max_type_arguments: Some(16),
1948            max_type_argument_depth: Some(16),
1949            max_pure_argument_size: Some(16 * 1024),
1950            max_programmable_tx_commands: Some(1024),
1951            move_binary_format_version: Some(7),
1952            min_move_binary_format_version: Some(6),
1953            binary_module_handles: Some(100),
1954            binary_struct_handles: Some(300),
1955            binary_function_handles: Some(1500),
1956            binary_function_instantiations: Some(750),
1957            binary_signatures: Some(1000),
1958            binary_constant_pool: Some(4000),
1959            binary_identifiers: Some(10000),
1960            binary_address_identifiers: Some(100),
1961            binary_struct_defs: Some(200),
1962            binary_struct_def_instantiations: Some(100),
1963            binary_function_defs: Some(1000),
1964            binary_field_handles: Some(500),
1965            binary_field_instantiations: Some(250),
1966            binary_friend_decls: Some(100),
1967            binary_enum_defs: None,
1968            binary_enum_def_instantiations: None,
1969            binary_variant_handles: None,
1970            binary_variant_instantiation_handles: None,
1971            max_move_object_size: Some(250 * 1024),
1972            max_move_package_size: Some(100 * 1024),
1973            max_publish_or_upgrade_per_ptb: Some(5),
1974            // max gas budget for an authentication is in NANOS
1975            max_auth_gas: None,
1976            // max gas budget is in NANOS and an absolute value 50IOTA
1977            max_tx_gas: Some(50_000_000_000),
1978            max_gas_price: Some(100_000),
1979            max_gas_computation_bucket: Some(5_000_000),
1980            max_loop_depth: Some(5),
1981            max_generic_instantiation_length: Some(32),
1982            max_function_parameters: Some(128),
1983            max_basic_blocks: Some(1024),
1984            max_value_stack_size: Some(1024),
1985            max_type_nodes: Some(256),
1986            max_push_size: Some(10000),
1987            max_struct_definitions: Some(200),
1988            max_function_definitions: Some(1000),
1989            max_fields_in_struct: Some(32),
1990            max_dependency_depth: Some(100),
1991            max_num_event_emit: Some(1024),
1992            max_num_new_move_object_ids: Some(2048),
1993            max_num_new_move_object_ids_system_tx: Some(2048 * 16),
1994            max_num_deleted_move_object_ids: Some(2048),
1995            max_num_deleted_move_object_ids_system_tx: Some(2048 * 16),
1996            max_num_transferred_move_object_ids: Some(2048),
1997            max_num_transferred_move_object_ids_system_tx: Some(2048 * 16),
1998            max_event_emit_size: Some(250 * 1024),
1999            max_move_vector_len: Some(256 * 1024),
2000            max_type_to_layout_nodes: None,
2001            max_ptb_value_size: None,
2002
2003            max_back_edges_per_function: Some(10_000),
2004            max_back_edges_per_module: Some(10_000),
2005
2006            max_verifier_meter_ticks_per_function: Some(16_000_000),
2007
2008            max_meter_ticks_per_module: Some(16_000_000),
2009            max_meter_ticks_per_package: Some(16_000_000),
2010
2011            object_runtime_max_num_cached_objects: Some(1000),
2012            object_runtime_max_num_cached_objects_system_tx: Some(1000 * 16),
2013            object_runtime_max_num_store_entries: Some(1000),
2014            object_runtime_max_num_store_entries_system_tx: Some(1000 * 16),
2015            // min gas budget is in NANOS and an absolute value 1000 NANOS or 0.000001IOTA
2016            base_tx_cost_fixed: Some(1_000),
2017            package_publish_cost_fixed: Some(1_000),
2018            base_tx_cost_per_byte: Some(0),
2019            package_publish_cost_per_byte: Some(80),
2020            obj_access_cost_read_per_byte: Some(15),
2021            obj_access_cost_mutate_per_byte: Some(40),
2022            obj_access_cost_delete_per_byte: Some(40),
2023            obj_access_cost_verify_per_byte: Some(200),
2024            obj_data_cost_refundable: Some(100),
2025            obj_metadata_cost_non_refundable: Some(50),
2026            gas_model_version: Some(1),
2027            storage_rebate_rate: Some(10000),
2028            // Change reward slashing rate to 100%.
2029            reward_slashing_rate: Some(10000),
2030            storage_gas_price: Some(76),
2031            base_gas_price: None,
2032            // The initial subsidy (target reward) for validators per epoch.
2033            // Refer to the IOTA tokenomics for the origin of this value.
2034            validator_target_reward: Some(767_000 * 1_000_000_000),
2035            max_transactions_per_checkpoint: Some(10_000),
2036            max_checkpoint_size_bytes: Some(30 * 1024 * 1024),
2037
2038            // For now, perform upgrades with a bare quorum of validators.
2039            buffer_stake_for_protocol_upgrade_bps: Some(5000),
2040
2041            // === Native Function Costs ===
2042            // `address` module
2043            // Cost params for the Move native function `address::from_bytes(bytes: vector<u8>)`
2044            address_from_bytes_cost_base: Some(52),
2045            // Cost params for the Move native function `address::to_u256(address): u256`
2046            address_to_u256_cost_base: Some(52),
2047            // Cost params for the Move native function `address::from_u256(u256): address`
2048            address_from_u256_cost_base: Some(52),
2049
2050            // `config` module
2051            // Cost params for the Move native function `read_setting_impl``
2052            config_read_setting_impl_cost_base: Some(100),
2053            config_read_setting_impl_cost_per_byte: Some(40),
2054
2055            // `dynamic_field` module
2056            // Cost params for the Move native function `hash_type_and_key<K: copy + drop +
2057            // store>(parent: address, k: K): address`
2058            dynamic_field_hash_type_and_key_cost_base: Some(100),
2059            dynamic_field_hash_type_and_key_type_cost_per_byte: Some(2),
2060            dynamic_field_hash_type_and_key_value_cost_per_byte: Some(2),
2061            dynamic_field_hash_type_and_key_type_tag_cost_per_byte: Some(2),
2062            // Cost params for the Move native function `add_child_object<Child: key>(parent:
2063            // address, child: Child)`
2064            dynamic_field_add_child_object_cost_base: Some(100),
2065            dynamic_field_add_child_object_type_cost_per_byte: Some(10),
2066            dynamic_field_add_child_object_value_cost_per_byte: Some(10),
2067            dynamic_field_add_child_object_struct_tag_cost_per_byte: Some(10),
2068            // Cost params for the Move native function `borrow_child_object_mut<Child: key>(parent:
2069            // &mut UID, id: address): &mut Child`
2070            dynamic_field_borrow_child_object_cost_base: Some(100),
2071            dynamic_field_borrow_child_object_child_ref_cost_per_byte: Some(10),
2072            dynamic_field_borrow_child_object_type_cost_per_byte: Some(10),
2073            // Cost params for the Move native function `remove_child_object<Child: key>(parent:
2074            // address, id: address): Child`
2075            dynamic_field_remove_child_object_cost_base: Some(100),
2076            dynamic_field_remove_child_object_child_cost_per_byte: Some(2),
2077            dynamic_field_remove_child_object_type_cost_per_byte: Some(2),
2078            // Cost params for the Move native function `has_child_object(parent: address, id:
2079            // address): bool`
2080            dynamic_field_has_child_object_cost_base: Some(100),
2081            // Cost params for the Move native function `has_child_object_with_ty<Child:
2082            // key>(parent: address, id: address): bool`
2083            dynamic_field_has_child_object_with_ty_cost_base: Some(100),
2084            dynamic_field_has_child_object_with_ty_type_cost_per_byte: Some(2),
2085            dynamic_field_has_child_object_with_ty_type_tag_cost_per_byte: Some(2),
2086
2087            // `event` module
2088            // Cost params for the Move native function `event::emit<T: copy + drop>(event: T)`
2089            event_emit_cost_base: Some(52),
2090            event_emit_value_size_derivation_cost_per_byte: Some(2),
2091            event_emit_tag_size_derivation_cost_per_byte: Some(5),
2092            event_emit_output_cost_per_byte: Some(10),
2093
2094            //  `object` module
2095            // Cost params for the Move native function `borrow_uid<T: key>(obj: &T): &UID`
2096            object_borrow_uid_cost_base: Some(52),
2097            // Cost params for the Move native function `delete_impl(id: address)`
2098            object_delete_impl_cost_base: Some(52),
2099            // Cost params for the Move native function `record_new_uid(id: address)`
2100            object_record_new_uid_cost_base: Some(52),
2101
2102            // `transfer` module
2103            // Cost params for the Move native function `transfer_impl<T: key>(obj: T, recipient:
2104            // address)`
2105            transfer_transfer_internal_cost_base: Some(52),
2106            // Cost params for the Move native function `freeze_object<T: key>(obj: T)`
2107            transfer_freeze_object_cost_base: Some(52),
2108            // Cost params for the Move native function `share_object<T: key>(obj: T)`
2109            transfer_share_object_cost_base: Some(52),
2110            transfer_receive_object_cost_base: Some(52),
2111
2112            // `tx_context` module
2113            // Cost params for the Move native function `transfer_impl<T: key>(obj: T, recipient:
2114            // address)`
2115            tx_context_derive_id_cost_base: Some(52),
2116            tx_context_fresh_id_cost_base: None,
2117            tx_context_sender_cost_base: None,
2118            tx_context_digest_cost_base: None,
2119            tx_context_epoch_cost_base: None,
2120            tx_context_epoch_timestamp_ms_cost_base: None,
2121            tx_context_sponsor_cost_base: None,
2122            tx_context_rgp_cost_base: None,
2123            tx_context_gas_price_cost_base: None,
2124            tx_context_gas_budget_cost_base: None,
2125            tx_context_ids_created_cost_base: None,
2126            tx_context_replace_cost_base: None,
2127
2128            // `types` module
2129            // Cost params for the Move native function `is_one_time_witness<T: drop>(_: &T): bool`
2130            types_is_one_time_witness_cost_base: Some(52),
2131            types_is_one_time_witness_type_tag_cost_per_byte: Some(2),
2132            types_is_one_time_witness_type_cost_per_byte: Some(2),
2133
2134            // `validator` module
2135            // Cost params for the Move native function `validate_metadata_bcs(metadata:
2136            // vector<u8>)`
2137            validator_validate_metadata_cost_base: Some(52),
2138            validator_validate_metadata_data_cost_per_byte: Some(2),
2139
2140            // Crypto
2141            crypto_invalid_arguments_cost: Some(100),
2142            // bls12381::bls12381_min_pk_verify
2143            bls12381_bls12381_min_sig_verify_cost_base: Some(52),
2144            bls12381_bls12381_min_sig_verify_msg_cost_per_byte: Some(2),
2145            bls12381_bls12381_min_sig_verify_msg_cost_per_block: Some(2),
2146
2147            // bls12381::bls12381_min_pk_verify
2148            bls12381_bls12381_min_pk_verify_cost_base: Some(52),
2149            bls12381_bls12381_min_pk_verify_msg_cost_per_byte: Some(2),
2150            bls12381_bls12381_min_pk_verify_msg_cost_per_block: Some(2),
2151
2152            // ecdsa_k1::ecrecover
2153            ecdsa_k1_ecrecover_keccak256_cost_base: Some(52),
2154            ecdsa_k1_ecrecover_keccak256_msg_cost_per_byte: Some(2),
2155            ecdsa_k1_ecrecover_keccak256_msg_cost_per_block: Some(2),
2156            ecdsa_k1_ecrecover_sha256_cost_base: Some(52),
2157            ecdsa_k1_ecrecover_sha256_msg_cost_per_byte: Some(2),
2158            ecdsa_k1_ecrecover_sha256_msg_cost_per_block: Some(2),
2159
2160            // ecdsa_k1::decompress_pubkey
2161            ecdsa_k1_decompress_pubkey_cost_base: Some(52),
2162
2163            // ecdsa_k1::secp256k1_verify
2164            ecdsa_k1_secp256k1_verify_keccak256_cost_base: Some(52),
2165            ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_byte: Some(2),
2166            ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_block: Some(2),
2167            ecdsa_k1_secp256k1_verify_sha256_cost_base: Some(52),
2168            ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_byte: Some(2),
2169            ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_block: Some(2),
2170
2171            // ecdsa_r1::ecrecover
2172            ecdsa_r1_ecrecover_keccak256_cost_base: Some(52),
2173            ecdsa_r1_ecrecover_keccak256_msg_cost_per_byte: Some(2),
2174            ecdsa_r1_ecrecover_keccak256_msg_cost_per_block: Some(2),
2175            ecdsa_r1_ecrecover_sha256_cost_base: Some(52),
2176            ecdsa_r1_ecrecover_sha256_msg_cost_per_byte: Some(2),
2177            ecdsa_r1_ecrecover_sha256_msg_cost_per_block: Some(2),
2178
2179            // ecdsa_r1::secp256k1_verify
2180            ecdsa_r1_secp256r1_verify_keccak256_cost_base: Some(52),
2181            ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_byte: Some(2),
2182            ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_block: Some(2),
2183            ecdsa_r1_secp256r1_verify_sha256_cost_base: Some(52),
2184            ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_byte: Some(2),
2185            ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_block: Some(2),
2186
2187            // ecvrf::verify
2188            ecvrf_ecvrf_verify_cost_base: Some(52),
2189            ecvrf_ecvrf_verify_alpha_string_cost_per_byte: Some(2),
2190            ecvrf_ecvrf_verify_alpha_string_cost_per_block: Some(2),
2191
2192            // ed25519
2193            ed25519_ed25519_verify_cost_base: Some(52),
2194            ed25519_ed25519_verify_msg_cost_per_byte: Some(2),
2195            ed25519_ed25519_verify_msg_cost_per_block: Some(2),
2196
2197            // groth16::prepare_verifying_key
2198            groth16_prepare_verifying_key_bls12381_cost_base: Some(52),
2199            groth16_prepare_verifying_key_bn254_cost_base: Some(52),
2200
2201            // groth16::verify_groth16_proof_internal
2202            groth16_verify_groth16_proof_internal_bls12381_cost_base: Some(52),
2203            groth16_verify_groth16_proof_internal_bls12381_cost_per_public_input: Some(2),
2204            groth16_verify_groth16_proof_internal_bn254_cost_base: Some(52),
2205            groth16_verify_groth16_proof_internal_bn254_cost_per_public_input: Some(2),
2206            groth16_verify_groth16_proof_internal_public_input_cost_per_byte: Some(2),
2207
2208            // hash::blake2b256
2209            hash_blake2b256_cost_base: Some(52),
2210            hash_blake2b256_data_cost_per_byte: Some(2),
2211            hash_blake2b256_data_cost_per_block: Some(2),
2212            // hash::keccak256
2213            hash_keccak256_cost_base: Some(52),
2214            hash_keccak256_data_cost_per_byte: Some(2),
2215            hash_keccak256_data_cost_per_block: Some(2),
2216
2217            poseidon_bn254_cost_base: None,
2218            poseidon_bn254_cost_per_block: None,
2219
2220            // hmac::hmac_sha3_256
2221            hmac_hmac_sha3_256_cost_base: Some(52),
2222            hmac_hmac_sha3_256_input_cost_per_byte: Some(2),
2223            hmac_hmac_sha3_256_input_cost_per_block: Some(2),
2224
2225            // group ops
2226            group_ops_bls12381_decode_scalar_cost: Some(52),
2227            group_ops_bls12381_decode_g1_cost: Some(52),
2228            group_ops_bls12381_decode_g2_cost: Some(52),
2229            group_ops_bls12381_decode_gt_cost: Some(52),
2230            group_ops_bls12381_scalar_add_cost: Some(52),
2231            group_ops_bls12381_g1_add_cost: Some(52),
2232            group_ops_bls12381_g2_add_cost: Some(52),
2233            group_ops_bls12381_gt_add_cost: Some(52),
2234            group_ops_bls12381_scalar_sub_cost: Some(52),
2235            group_ops_bls12381_g1_sub_cost: Some(52),
2236            group_ops_bls12381_g2_sub_cost: Some(52),
2237            group_ops_bls12381_gt_sub_cost: Some(52),
2238            group_ops_bls12381_scalar_mul_cost: Some(52),
2239            group_ops_bls12381_g1_mul_cost: Some(52),
2240            group_ops_bls12381_g2_mul_cost: Some(52),
2241            group_ops_bls12381_gt_mul_cost: Some(52),
2242            group_ops_bls12381_scalar_div_cost: Some(52),
2243            group_ops_bls12381_g1_div_cost: Some(52),
2244            group_ops_bls12381_g2_div_cost: Some(52),
2245            group_ops_bls12381_gt_div_cost: Some(52),
2246            group_ops_bls12381_g1_hash_to_base_cost: Some(52),
2247            group_ops_bls12381_g2_hash_to_base_cost: Some(52),
2248            group_ops_bls12381_g1_hash_to_cost_per_byte: Some(2),
2249            group_ops_bls12381_g2_hash_to_cost_per_byte: Some(2),
2250            group_ops_bls12381_g1_msm_base_cost: Some(52),
2251            group_ops_bls12381_g2_msm_base_cost: Some(52),
2252            group_ops_bls12381_g1_msm_base_cost_per_input: Some(52),
2253            group_ops_bls12381_g2_msm_base_cost_per_input: Some(52),
2254            group_ops_bls12381_msm_max_len: Some(32),
2255            group_ops_bls12381_pairing_cost: Some(52),
2256            group_ops_bls12381_g1_to_uncompressed_g1_cost: None,
2257            group_ops_bls12381_uncompressed_g1_to_g1_cost: None,
2258            group_ops_bls12381_uncompressed_g1_sum_base_cost: None,
2259            group_ops_bls12381_uncompressed_g1_sum_cost_per_term: None,
2260            group_ops_bls12381_uncompressed_g1_sum_max_terms: None,
2261
2262            // zklogin::check_zklogin_id
2263            #[allow(deprecated)]
2264            check_zklogin_id_cost_base: Some(200),
2265            #[allow(deprecated)]
2266            // zklogin::check_zklogin_issuer
2267            check_zklogin_issuer_cost_base: Some(200),
2268
2269            vdf_verify_vdf_cost: None,
2270            vdf_hash_to_input_cost: None,
2271
2272            bcs_per_byte_serialized_cost: Some(2),
2273            bcs_legacy_min_output_size_cost: Some(1),
2274            bcs_failure_cost: Some(52),
2275            hash_sha2_256_base_cost: Some(52),
2276            hash_sha2_256_per_byte_cost: Some(2),
2277            hash_sha2_256_legacy_min_input_len_cost: Some(1),
2278            hash_sha3_256_base_cost: Some(52),
2279            hash_sha3_256_per_byte_cost: Some(2),
2280            hash_sha3_256_legacy_min_input_len_cost: Some(1),
2281            type_name_get_base_cost: Some(52),
2282            type_name_get_per_byte_cost: Some(2),
2283            string_check_utf8_base_cost: Some(52),
2284            string_check_utf8_per_byte_cost: Some(2),
2285            string_is_char_boundary_base_cost: Some(52),
2286            string_sub_string_base_cost: Some(52),
2287            string_sub_string_per_byte_cost: Some(2),
2288            string_index_of_base_cost: Some(52),
2289            string_index_of_per_byte_pattern_cost: Some(2),
2290            string_index_of_per_byte_searched_cost: Some(2),
2291            vector_empty_base_cost: Some(52),
2292            vector_length_base_cost: Some(52),
2293            vector_push_back_base_cost: Some(52),
2294            vector_push_back_legacy_per_abstract_memory_unit_cost: Some(2),
2295            vector_borrow_base_cost: Some(52),
2296            vector_pop_back_base_cost: Some(52),
2297            vector_destroy_empty_base_cost: Some(52),
2298            vector_swap_base_cost: Some(52),
2299            debug_print_base_cost: Some(52),
2300            debug_print_stack_trace_base_cost: Some(52),
2301
2302            max_size_written_objects: Some(5 * 1000 * 1000),
2303            // max size of written objects during a system TXn to allow for larger writes
2304            // akin to `max_size_written_objects` but for system TXns
2305            max_size_written_objects_system_tx: Some(50 * 1000 * 1000),
2306
2307            // Limits the length of a Move identifier
2308            max_move_identifier_len: Some(128),
2309            max_move_value_depth: Some(128),
2310            max_move_enum_variants: None,
2311
2312            gas_rounding_step: Some(1_000),
2313
2314            execution_version: Some(1),
2315
2316            // We maintain the same total size limit for events, but increase the number of
2317            // events that can be emitted.
2318            max_event_emit_size_total: Some(
2319                256 /* former event count limit */ * 250 * 1024, // size limit per event
2320            ),
2321
2322            // Taking a baby step approach, we consider only 20% by stake as bad nodes so we
2323            // have a 80% by stake of nodes participating in the leader committee. That
2324            // allow us for more redundancy in case we have validators
2325            // under performing - since the responsibility is shared
2326            // amongst more nodes. We can increase that once we do have
2327            // higher confidence.
2328            consensus_bad_nodes_stake_threshold: Some(20),
2329
2330            // Max of 10 votes per hour.
2331            #[allow(deprecated)]
2332            max_jwk_votes_per_validator_per_epoch: Some(240),
2333
2334            #[allow(deprecated)]
2335            max_age_of_jwk_in_epochs: Some(1),
2336
2337            consensus_max_transaction_size_bytes: Some(256 * 1024), // 256KB
2338
2339            // Assume 1KB per transaction and 500 transactions per block.
2340            consensus_max_transactions_in_block_bytes: Some(512 * 1024),
2341
2342            random_beacon_reduction_allowed_delta: Some(800),
2343
2344            random_beacon_reduction_lower_bound: Some(1000),
2345            random_beacon_dkg_timeout_round: Some(3000),
2346            random_beacon_min_round_interval_ms: Some(500),
2347
2348            random_beacon_dkg_version: Some(1),
2349
2350            // Assume 20_000 TPS * 5% max stake per validator / (minimum) 4 blocks per round
2351            // = 250 transactions per block maximum Using a higher limit
2352            // that is 512, to account for bursty traffic and system transactions.
2353            consensus_max_num_transactions_in_block: Some(512),
2354
2355            max_deferral_rounds_for_congestion_control: Some(10),
2356
2357            min_checkpoint_interval_ms: Some(200),
2358
2359            checkpoint_summary_version_specific_data: Some(1),
2360
2361            max_soft_bundle_size: Some(5),
2362
2363            bridge_should_try_to_finalize_committee: None,
2364
2365            max_accumulated_txn_cost_per_object_in_mysticeti_commit: Some(10),
2366
2367            max_committee_members_count: None,
2368
2369            consensus_gc_depth: None,
2370
2371            consensus_max_acknowledgments_per_block: None,
2372
2373            max_congestion_limit_overshoot_per_commit: None,
2374
2375            scorer_version: None,
2376
2377            // `auth_context` module
2378            auth_context_digest_cost_base: None,
2379            auth_context_tx_data_bytes_cost_base: None,
2380            auth_context_tx_data_bytes_cost_per_byte: None,
2381            auth_context_tx_commands_cost_base: None,
2382            auth_context_tx_commands_cost_per_byte: None,
2383            auth_context_tx_inputs_cost_base: None,
2384            auth_context_tx_inputs_cost_per_byte: None,
2385            auth_context_replace_cost_base: None,
2386            auth_context_replace_cost_per_byte: None,
2387            auth_context_authenticator_function_info_v1_cost_base: None,
2388            // When adding a new constant, set it to None in the earliest version, like this:
2389            // new_constant: None,
2390        };
2391
2392        cfg.feature_flags.consensus_transaction_ordering = ConsensusTransactionOrdering::ByGasPrice;
2393
2394        // MoveVM related flags
2395        {
2396            cfg.feature_flags
2397                .disable_invariant_violation_check_in_swap_loc = true;
2398            cfg.feature_flags.no_extraneous_module_bytes = true;
2399            cfg.feature_flags.hardened_otw_check = true;
2400            cfg.feature_flags.rethrow_serialization_type_layout_errors = true;
2401        }
2402
2403        // zkLogin related flags
2404        {
2405            #[allow(deprecated)]
2406            {
2407                cfg.feature_flags.zklogin_max_epoch_upper_bound_delta = Some(30);
2408            }
2409        }
2410
2411        // Historical default: Mysticeti. Kept explicitly to match the
2412        // serialized form of pre-v14/v19/v24 configs. No runtime behavior
2413        // depends on this — Starfish is the only consensus protocol.
2414        #[expect(deprecated)]
2415        {
2416            cfg.feature_flags.consensus_choice = ConsensusChoice::MysticetiDeprecated;
2417        }
2418        // Use tonic networking for consensus.
2419        cfg.feature_flags.consensus_network = ConsensusNetwork::Tonic;
2420
2421        cfg.feature_flags.per_object_congestion_control_mode =
2422            PerObjectCongestionControlMode::TotalTxCount;
2423
2424        // Do not allow bridge committee to finalize on mainnet.
2425        cfg.bridge_should_try_to_finalize_committee = Some(chain != Chain::Mainnet);
2426
2427        // Devnet
2428        if chain != Chain::Mainnet && chain != Chain::Testnet {
2429            cfg.feature_flags.enable_poseidon = true;
2430            cfg.poseidon_bn254_cost_base = Some(260);
2431            cfg.poseidon_bn254_cost_per_block = Some(10);
2432
2433            cfg.feature_flags.enable_group_ops_native_function_msm = true;
2434
2435            cfg.feature_flags.enable_vdf = true;
2436            // Set to 30x and 2x the cost of a signature verification for now. This
2437            // should be updated along with other native crypto functions.
2438            cfg.vdf_verify_vdf_cost = Some(1500);
2439            cfg.vdf_hash_to_input_cost = Some(100);
2440
2441            cfg.feature_flags.passkey_auth = true;
2442        }
2443
2444        for cur in 2..=version.0 {
2445            match cur {
2446                1 => unreachable!(),
2447                // version 2 is a new framework version but with no config changes
2448                2 => {}
2449                3 => {
2450                    cfg.feature_flags.relocate_event_module = true;
2451                }
2452                4 => {
2453                    cfg.max_type_to_layout_nodes = Some(512);
2454                }
2455                5 => {
2456                    cfg.feature_flags.protocol_defined_base_fee = true;
2457                    cfg.base_gas_price = Some(1000);
2458
2459                    cfg.feature_flags.disallow_new_modules_in_deps_only_packages = true;
2460                    cfg.feature_flags.convert_type_argument_error = true;
2461                    cfg.feature_flags.native_charging_v2 = true;
2462
2463                    if chain != Chain::Mainnet && chain != Chain::Testnet {
2464                        cfg.feature_flags.uncompressed_g1_group_elements = true;
2465                    }
2466
2467                    cfg.gas_model_version = Some(2);
2468
2469                    cfg.poseidon_bn254_cost_per_block = Some(388);
2470
2471                    cfg.bls12381_bls12381_min_sig_verify_cost_base = Some(44064);
2472                    cfg.bls12381_bls12381_min_pk_verify_cost_base = Some(49282);
2473                    cfg.ecdsa_k1_secp256k1_verify_keccak256_cost_base = Some(1470);
2474                    cfg.ecdsa_k1_secp256k1_verify_sha256_cost_base = Some(1470);
2475                    cfg.ecdsa_r1_secp256r1_verify_sha256_cost_base = Some(4225);
2476                    cfg.ecdsa_r1_secp256r1_verify_keccak256_cost_base = Some(4225);
2477                    cfg.ecvrf_ecvrf_verify_cost_base = Some(4848);
2478                    cfg.ed25519_ed25519_verify_cost_base = Some(1802);
2479
2480                    // Manually changed to be "under cost"
2481                    cfg.ecdsa_r1_ecrecover_keccak256_cost_base = Some(1173);
2482                    cfg.ecdsa_r1_ecrecover_sha256_cost_base = Some(1173);
2483                    cfg.ecdsa_k1_ecrecover_keccak256_cost_base = Some(500);
2484                    cfg.ecdsa_k1_ecrecover_sha256_cost_base = Some(500);
2485
2486                    cfg.groth16_prepare_verifying_key_bls12381_cost_base = Some(53838);
2487                    cfg.groth16_prepare_verifying_key_bn254_cost_base = Some(82010);
2488                    cfg.groth16_verify_groth16_proof_internal_bls12381_cost_base = Some(72090);
2489                    cfg.groth16_verify_groth16_proof_internal_bls12381_cost_per_public_input =
2490                        Some(8213);
2491                    cfg.groth16_verify_groth16_proof_internal_bn254_cost_base = Some(115502);
2492                    cfg.groth16_verify_groth16_proof_internal_bn254_cost_per_public_input =
2493                        Some(9484);
2494
2495                    cfg.hash_keccak256_cost_base = Some(10);
2496                    cfg.hash_blake2b256_cost_base = Some(10);
2497
2498                    // group ops
2499                    cfg.group_ops_bls12381_decode_scalar_cost = Some(7);
2500                    cfg.group_ops_bls12381_decode_g1_cost = Some(2848);
2501                    cfg.group_ops_bls12381_decode_g2_cost = Some(3770);
2502                    cfg.group_ops_bls12381_decode_gt_cost = Some(3068);
2503
2504                    cfg.group_ops_bls12381_scalar_add_cost = Some(10);
2505                    cfg.group_ops_bls12381_g1_add_cost = Some(1556);
2506                    cfg.group_ops_bls12381_g2_add_cost = Some(3048);
2507                    cfg.group_ops_bls12381_gt_add_cost = Some(188);
2508
2509                    cfg.group_ops_bls12381_scalar_sub_cost = Some(10);
2510                    cfg.group_ops_bls12381_g1_sub_cost = Some(1550);
2511                    cfg.group_ops_bls12381_g2_sub_cost = Some(3019);
2512                    cfg.group_ops_bls12381_gt_sub_cost = Some(497);
2513
2514                    cfg.group_ops_bls12381_scalar_mul_cost = Some(11);
2515                    cfg.group_ops_bls12381_g1_mul_cost = Some(4842);
2516                    cfg.group_ops_bls12381_g2_mul_cost = Some(9108);
2517                    cfg.group_ops_bls12381_gt_mul_cost = Some(27490);
2518
2519                    cfg.group_ops_bls12381_scalar_div_cost = Some(91);
2520                    cfg.group_ops_bls12381_g1_div_cost = Some(5091);
2521                    cfg.group_ops_bls12381_g2_div_cost = Some(9206);
2522                    cfg.group_ops_bls12381_gt_div_cost = Some(27804);
2523
2524                    cfg.group_ops_bls12381_g1_hash_to_base_cost = Some(2962);
2525                    cfg.group_ops_bls12381_g2_hash_to_base_cost = Some(8688);
2526
2527                    cfg.group_ops_bls12381_g1_msm_base_cost = Some(62648);
2528                    cfg.group_ops_bls12381_g2_msm_base_cost = Some(131192);
2529                    cfg.group_ops_bls12381_g1_msm_base_cost_per_input = Some(1333);
2530                    cfg.group_ops_bls12381_g2_msm_base_cost_per_input = Some(3216);
2531
2532                    cfg.group_ops_bls12381_uncompressed_g1_to_g1_cost = Some(677);
2533                    cfg.group_ops_bls12381_g1_to_uncompressed_g1_cost = Some(2099);
2534                    cfg.group_ops_bls12381_uncompressed_g1_sum_base_cost = Some(77);
2535                    cfg.group_ops_bls12381_uncompressed_g1_sum_cost_per_term = Some(26);
2536                    cfg.group_ops_bls12381_uncompressed_g1_sum_max_terms = Some(1200);
2537
2538                    cfg.group_ops_bls12381_pairing_cost = Some(26897);
2539
2540                    cfg.validator_validate_metadata_cost_base = Some(20000);
2541
2542                    cfg.max_committee_members_count = Some(50);
2543                }
2544                6 => {
2545                    cfg.max_ptb_value_size = Some(1024 * 1024);
2546                }
2547                7 => {
2548                    // version 7 is a new framework version but with no config
2549                    // changes
2550                }
2551                8 => {
2552                    cfg.feature_flags.variant_nodes = true;
2553
2554                    if chain != Chain::Mainnet {
2555                        // Enable round prober in consensus.
2556                        cfg.feature_flags.consensus_round_prober = true;
2557                        // Enable distributed vote scoring.
2558                        cfg.feature_flags
2559                            .consensus_distributed_vote_scoring_strategy = true;
2560                        cfg.feature_flags.consensus_linearize_subdag_v2 = true;
2561                        // Enable smart ancestor selection for testnet
2562                        cfg.feature_flags.consensus_smart_ancestor_selection = true;
2563                        // Enable probing for accepted rounds in round prober for testnet
2564                        cfg.feature_flags
2565                            .consensus_round_prober_probe_accepted_rounds = true;
2566                        // Enable zstd compression for consensus in testnet
2567                        cfg.feature_flags.consensus_zstd_compression = true;
2568                        // Assuming a round rate of max 15/sec, then using a gc depth of 60 allow
2569                        // blocks within a window of ~4 seconds
2570                        // to be included before be considered garbage collected.
2571                        cfg.consensus_gc_depth = Some(60);
2572                    }
2573
2574                    // Enable min_free_execution_slot for the shared object congestion tracker in
2575                    // devnet.
2576                    if chain != Chain::Testnet && chain != Chain::Mainnet {
2577                        cfg.feature_flags.congestion_control_min_free_execution_slot = true;
2578                    }
2579                }
2580                9 => {
2581                    if chain != Chain::Mainnet {
2582                        // Disable smart ancestor selection in the testnet and devnet.
2583                        cfg.feature_flags.consensus_smart_ancestor_selection = false;
2584                    }
2585
2586                    // Enable zstd compression for consensus
2587                    cfg.feature_flags.consensus_zstd_compression = true;
2588
2589                    // Enable passkey in multisig in devnet.
2590                    if chain != Chain::Testnet && chain != Chain::Mainnet {
2591                        cfg.feature_flags.accept_passkey_in_multisig = true;
2592                    }
2593
2594                    // this flag is now deprecated because of the bridge removal.
2595                    cfg.bridge_should_try_to_finalize_committee = None;
2596                }
2597                10 => {
2598                    // Enable min_free_execution_slot for the shared object congestion tracker in
2599                    // all networks.
2600                    cfg.feature_flags.congestion_control_min_free_execution_slot = true;
2601
2602                    // Increase the committee size to 80 on all networks.
2603                    cfg.max_committee_members_count = Some(80);
2604
2605                    // Enable round prober in consensus.
2606                    cfg.feature_flags.consensus_round_prober = true;
2607                    // Enable probing for accepted rounds in round.
2608                    cfg.feature_flags
2609                        .consensus_round_prober_probe_accepted_rounds = true;
2610                    // Enable distributed vote scoring.
2611                    cfg.feature_flags
2612                        .consensus_distributed_vote_scoring_strategy = true;
2613                    // Enable the new consensus commit rule.
2614                    cfg.feature_flags.consensus_linearize_subdag_v2 = true;
2615
2616                    // Enable consensus garbage collection
2617                    // Assuming a round rate of max 15/sec, then using a gc depth of 60 allow
2618                    // blocks within a window of ~4 seconds
2619                    // to be included before be considered garbage collected.
2620                    cfg.consensus_gc_depth = Some(60);
2621
2622                    // Enable minimized child object mutation counting.
2623                    cfg.feature_flags.minimize_child_object_mutations = true;
2624
2625                    if chain != Chain::Mainnet {
2626                        // Enable batched block sync in devnet and testnet.
2627                        cfg.feature_flags.consensus_batched_block_sync = true;
2628                    }
2629
2630                    if chain != Chain::Testnet && chain != Chain::Mainnet {
2631                        // Enable the gas price feedback mechanism (which is used for
2632                        // transactions cancelled due to shared object congestion) in devnet
2633                        cfg.feature_flags
2634                            .congestion_control_gas_price_feedback_mechanism = true;
2635                    }
2636
2637                    cfg.feature_flags.validate_identifier_inputs = true;
2638                    cfg.feature_flags.dependency_linkage_error = true;
2639                    cfg.feature_flags.additional_multisig_checks = true;
2640                }
2641                11 => {
2642                    // version 11 is a new framework version but with no config
2643                    // changes
2644                }
2645                12 => {
2646                    // Enable the gas price feedback mechanism for transactions
2647                    // cancelled due to congestion in all networks
2648                    cfg.feature_flags
2649                        .congestion_control_gas_price_feedback_mechanism = true;
2650
2651                    // Enable normalization of PTB arguments in all networks.
2652                    cfg.feature_flags.normalize_ptb_arguments = true;
2653                }
2654                13 => {
2655                    // Enable selecting committee based on eligible active validators on all
2656                    // networks.
2657                    cfg.feature_flags.select_committee_from_eligible_validators = true;
2658                    // Enable tracking non-committee eligible active
2659                    // validators on all networks.
2660                    cfg.feature_flags.track_non_committee_eligible_validators = true;
2661
2662                    if chain != Chain::Testnet && chain != Chain::Mainnet {
2663                        // Enable selecting committee only from active validators that next epoch
2664                        // version and issued valid AuthorityCapabilities notification in devnet.
2665                        cfg.feature_flags
2666                            .select_committee_supporting_next_epoch_version = true;
2667                    }
2668                }
2669                14 => {
2670                    // Enable batched block sync for mainnet.
2671                    cfg.feature_flags.consensus_batched_block_sync = true;
2672
2673                    if chain != Chain::Mainnet {
2674                        // Enable median-based commit timestamp calculation in consensus and
2675                        // enforce checkpoint timestamp monotonicity for testnet.
2676                        cfg.feature_flags
2677                            .consensus_median_timestamp_with_checkpoint_enforcement = true;
2678                        // Enable selecting committee only from active validators that support the
2679                        // next epoch's version and issued valid AuthorityCapabilities notification
2680                        // in testnet.
2681                        cfg.feature_flags
2682                            .select_committee_supporting_next_epoch_version = true;
2683                    }
2684                    if chain != Chain::Testnet && chain != Chain::Mainnet {
2685                        // Switch consensus protocol to Starfish in devnet
2686                        cfg.feature_flags.consensus_choice = ConsensusChoice::Starfish;
2687                    }
2688                }
2689                15 => {
2690                    if chain != Chain::Mainnet && chain != Chain::Testnet {
2691                        // Enable overshoot of 100 in congestion control. This allows bursts of
2692                        // shared object transactions up to 10 times the average allowable
2693                        // load set by `max_accumulated_txn_cost_per_object_in_mysticeti_commit`.
2694                        cfg.max_congestion_limit_overshoot_per_commit = Some(100);
2695                    }
2696                }
2697                16 => {
2698                    // Enable selecting committee only from active validators that support the
2699                    // next epoch's version and issued valid AuthorityCapabilities notification.
2700                    cfg.feature_flags
2701                        .select_committee_supporting_next_epoch_version = true;
2702                    // Enable committing transactions only for traversed headers in Starfish
2703                    cfg.feature_flags
2704                        .consensus_commit_transactions_only_for_traversed_headers = true;
2705                }
2706                17 => {
2707                    // Increase the committee size to 100 on all networks.
2708                    cfg.max_committee_members_count = Some(100);
2709                }
2710                18 => {
2711                    if chain != Chain::Mainnet {
2712                        // Enable passkey authentication support in testnet.
2713                        cfg.feature_flags.passkey_auth = true;
2714                    }
2715                }
2716                19 => {
2717                    if chain != Chain::Testnet && chain != Chain::Mainnet {
2718                        // Enable congestion limit overshoot in the gas price feedback
2719                        // mechanism on devnet.
2720                        cfg.feature_flags
2721                            .congestion_limit_overshoot_in_gas_price_feedback_mechanism = true;
2722                        // Enable a separate gas price feedback mechanism for transactions using
2723                        // randomness on devnet.
2724                        cfg.feature_flags
2725                            .separate_gas_price_feedback_mechanism_for_randomness = true;
2726                        // Enable storing metadata in module bytes and then
2727                        // publishing package metadata in devnet
2728                        cfg.feature_flags.metadata_in_module_bytes = true;
2729                        cfg.feature_flags.publish_package_metadata = true;
2730                        // Enable Move authentication in devnet
2731                        cfg.feature_flags.enable_move_authentication = true;
2732                        // Max auth gas budget is in NANOS and an absolute value 0.25 IOTA
2733                        cfg.max_auth_gas = Some(250_000_000);
2734                        // Increase the base cost for transfer receive object in devnet, since the
2735                        // implementation now does check if parent is not an account.
2736                        cfg.transfer_receive_object_cost_base = Some(100);
2737                        // Enable adjustment of validator rewards based on score in devnet.
2738                        cfg.feature_flags.adjust_rewards_by_score = true;
2739                    }
2740
2741                    if chain != Chain::Mainnet {
2742                        // Switch consensus protocol to Starfish in testnet.
2743                        cfg.feature_flags.consensus_choice = ConsensusChoice::Starfish;
2744
2745                        // Enable validator score calculation on testnet
2746                        cfg.feature_flags.calculate_validator_scores = true;
2747                        cfg.scorer_version = Some(1);
2748                    }
2749
2750                    // Change epoch transaction will contain validator scores
2751                    cfg.feature_flags.pass_validator_scores_to_advance_epoch = true;
2752
2753                    // Enable passkey authentication support in mainnet
2754                    cfg.feature_flags.passkey_auth = true;
2755                }
2756                20 => {
2757                    if chain != Chain::Testnet && chain != Chain::Mainnet {
2758                        // Passes the calculated validator scores to advance epoch only on Devnet
2759                        cfg.feature_flags
2760                            .pass_calculated_validator_scores_to_advance_epoch = true;
2761                    }
2762                }
2763                21 => {
2764                    if chain != Chain::Testnet && chain != Chain::Mainnet {
2765                        // Enable fast commit syncer for faster recovery in devnet.
2766                        cfg.feature_flags.consensus_fast_commit_sync = true;
2767                    }
2768                    if chain != Chain::Mainnet {
2769                        // Enable overshoot of 100 in congestion control on testnet.
2770                        // This allows bursts of shared-object transactions
2771                        // up to 10 times the average allowable load set by
2772                        // `max_accumulated_txn_cost_per_object_in_mysticeti_commit`.
2773                        cfg.max_congestion_limit_overshoot_per_commit = Some(100);
2774                        // Enable congestion limit overshoot in the gas price feedback
2775                        // mechanism on testnet.
2776                        cfg.feature_flags
2777                            .congestion_limit_overshoot_in_gas_price_feedback_mechanism = true;
2778                        // Enable a separate gas price feedback mechanism for transactions using
2779                        // randomness on testnet.
2780                        cfg.feature_flags
2781                            .separate_gas_price_feedback_mechanism_for_randomness = true;
2782                    }
2783
2784                    cfg.auth_context_digest_cost_base = Some(30);
2785                    cfg.auth_context_tx_commands_cost_base = Some(30);
2786                    cfg.auth_context_tx_commands_cost_per_byte = Some(2);
2787                    cfg.auth_context_tx_inputs_cost_base = Some(30);
2788                    cfg.auth_context_tx_inputs_cost_per_byte = Some(2);
2789                    cfg.auth_context_replace_cost_base = Some(30);
2790                    cfg.auth_context_replace_cost_per_byte = Some(2);
2791
2792                    if chain != Chain::Testnet && chain != Chain::Mainnet {
2793                        // Decrease max_auth_gas to 0.00025 IOTA
2794                        cfg.max_auth_gas = Some(250_000);
2795                    }
2796                }
2797                22 => {
2798                    // Enable overshoot of 100 in congestion control on all networks.
2799                    // This allows bursts of shared-object transactions
2800                    // up to 10 times the average allowable load set by
2801                    // `max_accumulated_txn_cost_per_object_in_mysticeti_commit`.
2802                    cfg.max_congestion_limit_overshoot_per_commit = Some(100);
2803                    // Enable congestion limit overshoot in the gas price feedback
2804                    // mechanism on all networks.
2805                    cfg.feature_flags
2806                        .congestion_limit_overshoot_in_gas_price_feedback_mechanism = true;
2807                    // Enable a separate gas price feedback mechanism for transactions using
2808                    // randomness on all networks.
2809                    cfg.feature_flags
2810                        .separate_gas_price_feedback_mechanism_for_randomness = true;
2811
2812                    if chain != Chain::Mainnet {
2813                        // Enable storing metadata in module bytes and then
2814                        // publishing package metadata in testnet
2815                        cfg.feature_flags.metadata_in_module_bytes = true;
2816                        cfg.feature_flags.publish_package_metadata = true;
2817                        // Enable Move authentication in testnet
2818                        cfg.feature_flags.enable_move_authentication = true;
2819                        // Max_auth_gas is 0.00025 IOTA
2820                        cfg.max_auth_gas = Some(250_000);
2821                        // Increase the base cost for transfer receive object in testnet, since the
2822                        // implementation now does check if parent is not an account.
2823                        cfg.transfer_receive_object_cost_base = Some(100);
2824                    }
2825
2826                    if chain != Chain::Mainnet {
2827                        // Enable fast commit syncer for faster recovery on testnet.
2828                        cfg.feature_flags.consensus_fast_commit_sync = true;
2829                    }
2830                }
2831                23 => {
2832                    // Enable Move native context (TxContext via native functions) in all networks.
2833                    cfg.feature_flags.move_native_tx_context = true;
2834                    cfg.tx_context_fresh_id_cost_base = Some(52);
2835                    cfg.tx_context_sender_cost_base = Some(30);
2836                    cfg.tx_context_digest_cost_base = Some(30);
2837                    cfg.tx_context_epoch_cost_base = Some(30);
2838                    cfg.tx_context_epoch_timestamp_ms_cost_base = Some(30);
2839                    cfg.tx_context_sponsor_cost_base = Some(30);
2840                    cfg.tx_context_rgp_cost_base = Some(30);
2841                    cfg.tx_context_gas_price_cost_base = Some(30);
2842                    cfg.tx_context_gas_budget_cost_base = Some(30);
2843                    cfg.tx_context_ids_created_cost_base = Some(30);
2844                    cfg.tx_context_replace_cost_base = Some(30);
2845                }
2846                24 => {
2847                    // Switch consensus protocol to Starfish in all networks.
2848                    cfg.feature_flags.consensus_choice = ConsensusChoice::Starfish;
2849
2850                    if chain != Chain::Testnet && chain != Chain::Mainnet {
2851                        // Enable Move-based sponsor account authentication in devnet.
2852                        cfg.feature_flags.enable_move_authentication_for_sponsor = true;
2853                    }
2854
2855                    // Add tx_data_bytes to AuthContext for intent-based signature
2856                    // verification in account abstraction.
2857                    cfg.auth_context_tx_data_bytes_cost_base = Some(30);
2858                    cfg.auth_context_tx_data_bytes_cost_per_byte = Some(2);
2859
2860                    // Enable additional borrow checks.
2861                    cfg.feature_flags.additional_borrow_checks = true;
2862                }
2863                #[allow(deprecated)]
2864                25 => {
2865                    // Deprecate zkLogin related parameters since zkLogin is deprecated and was
2866                    // never enabled on IOTA.
2867                    cfg.feature_flags.zklogin_max_epoch_upper_bound_delta = None;
2868                    cfg.check_zklogin_id_cost_base = None;
2869                    cfg.check_zklogin_issuer_cost_base = None;
2870                    cfg.max_jwk_votes_per_validator_per_epoch = None;
2871                    cfg.max_age_of_jwk_in_epochs = None;
2872                }
2873                26 => {
2874                    // Introduce a module to allow Move code to query protocol
2875                    // feature flags at runtime.
2876                }
2877                27 => {
2878                    if chain != Chain::Mainnet {
2879                        // Enable consensus block restrictions on testnet/devnet to bound
2880                        // header size by committee size.
2881                        cfg.feature_flags.consensus_block_restrictions = true;
2882                    }
2883
2884                    if chain != Chain::Testnet && chain != Chain::Mainnet {
2885                        // Only sponsor Move authentication is performed pre-consensus in devnet.
2886                        cfg.feature_flags
2887                            .pre_consensus_sponsor_only_move_authentication = true;
2888                    }
2889                }
2890                28 => {
2891                    // AuthenticatorFunctionInfoV1 max BCS size:
2892                    // package (32) + module_name (128) + function_name (128) = 288 bytes = 9 ×
2893                    // digest. auth_context_digest_cost_base = 30 for 32 bytes →
2894                    // 9 × 30 = 270.
2895                    cfg.auth_context_authenticator_function_info_v1_cost_base = Some(270);
2896
2897                    // Enable storing metadata in module bytes and then
2898                    // publishing package metadata in mainnet.
2899                    cfg.feature_flags.metadata_in_module_bytes = true;
2900                    cfg.feature_flags.publish_package_metadata = true;
2901                    // Enable Move authentication in mainnet.
2902                    cfg.feature_flags.enable_move_authentication = true;
2903                    // Increase the base cost for transfer receive object in mainnet, since the
2904                    // implementation now does check if parent is not an account.
2905                    cfg.transfer_receive_object_cost_base = Some(100);
2906
2907                    if chain != Chain::Unknown {
2908                        // max_auth_gas is 0.00002 IOTA in testnet and mainnet.
2909                        cfg.max_auth_gas = Some(20_000);
2910                    }
2911
2912                    if chain != Chain::Mainnet {
2913                        // Enable Move-based sponsor account authentication in testnet.
2914                        cfg.feature_flags.enable_move_authentication_for_sponsor = true;
2915                        // Only sponsor Move authentication is performed pre-consensus in testnet.
2916                        cfg.feature_flags
2917                            .pre_consensus_sponsor_only_move_authentication = true;
2918                    }
2919                }
2920                29 => {
2921                    // Keep advancing the random beacon DKG state machine on every commit
2922                    // while it is still pending so DKG resolves from persisted state
2923                    // (completing, or failing once the timeout round passes) even with no
2924                    // fresh inbound traffic -- e.g. after a validator restart -- instead of
2925                    // staying pending forever and blocking epoch close.
2926                    cfg.feature_flags.always_advance_dkg_to_resolution = true;
2927
2928                    // Enable median-based commit timestamp calculation in consensus and
2929                    // enforce checkpoint timestamp monotonicity for mainnet.
2930                    cfg.feature_flags
2931                        .consensus_median_timestamp_with_checkpoint_enforcement = true;
2932                }
2933                // Use this template when making changes:
2934                //
2935                //     // modify an existing constant.
2936                //     move_binary_format_version: Some(7),
2937                //
2938                //     // Add a new constant (which is set to None in prior versions).
2939                //     new_constant: Some(new_value),
2940                //
2941                //     // Remove a constant (ensure that it is never accessed during this version).
2942                //     max_move_object_size: None,
2943                _ => panic!("unsupported version {version:?}"),
2944            }
2945        }
2946        cfg
2947    }
2948
2949    // Extract the bytecode verifier config from this protocol config.
2950    // If used during signing, `signing_limits` should be set.
2951    // The third limit configures`sanity_check_with_regex_reference_safety`,
2952    // which runs the new regex-based reference safety check to check that it is
2953    // strictly more permissive than the current implementation.
2954    pub fn verifier_config(&self, signing_limits: Option<(usize, usize, usize)>) -> VerifierConfig {
2955        let (
2956            max_back_edges_per_function,
2957            max_back_edges_per_module,
2958            sanity_check_with_regex_reference_safety,
2959        ) = if let Some((
2960            max_back_edges_per_function,
2961            max_back_edges_per_module,
2962            sanity_check_with_regex_reference_safety,
2963        )) = signing_limits
2964        {
2965            (
2966                Some(max_back_edges_per_function),
2967                Some(max_back_edges_per_module),
2968                Some(sanity_check_with_regex_reference_safety),
2969            )
2970        } else {
2971            (None, None, None)
2972        };
2973
2974        let additional_borrow_checks = if signing_limits.is_some() {
2975            // Always apply additional borrow checks during signing regardless of
2976            // protocol version, to prevent accepting potentially unsafe bytecode.
2977            true
2978        } else {
2979            self.additional_borrow_checks()
2980        };
2981
2982        VerifierConfig {
2983            max_loop_depth: Some(self.max_loop_depth() as usize),
2984            max_generic_instantiation_length: Some(self.max_generic_instantiation_length() as usize),
2985            max_function_parameters: Some(self.max_function_parameters() as usize),
2986            max_basic_blocks: Some(self.max_basic_blocks() as usize),
2987            max_value_stack_size: self.max_value_stack_size() as usize,
2988            max_type_nodes: Some(self.max_type_nodes() as usize),
2989            max_push_size: Some(self.max_push_size() as usize),
2990            max_dependency_depth: Some(self.max_dependency_depth() as usize),
2991            max_fields_in_struct: Some(self.max_fields_in_struct() as usize),
2992            max_function_definitions: Some(self.max_function_definitions() as usize),
2993            max_data_definitions: Some(self.max_struct_definitions() as usize),
2994            max_constant_vector_len: Some(self.max_move_vector_len()),
2995            max_back_edges_per_function,
2996            max_back_edges_per_module,
2997            max_basic_blocks_in_script: None,
2998            max_identifier_len: self.max_move_identifier_len_as_option(), /* Before protocol
2999                                                                           * version 9, there was
3000                                                                           * no limit */
3001            bytecode_version: self.move_binary_format_version(),
3002            max_variants_in_enum: self.max_move_enum_variants_as_option(),
3003            additional_borrow_checks,
3004            sanity_check_with_regex_reference_safety: sanity_check_with_regex_reference_safety
3005                .map(|limit| limit as u128),
3006        }
3007    }
3008
3009    /// Override one or more settings in the config, for testing.
3010    /// This must be called at the beginning of the test, before
3011    /// get_for_(min|max)_version is called, since those functions cache
3012    /// their return value.
3013    pub fn apply_overrides_for_testing(
3014        override_fn: impl Fn(ProtocolVersion, Self) -> Self + Send + Sync + 'static,
3015    ) -> OverrideGuard {
3016        CONFIG_OVERRIDE.with(|ovr| {
3017            let mut cur = ovr.borrow_mut();
3018            assert!(cur.is_none(), "config override already present");
3019            *cur = Some(Box::new(override_fn));
3020            OverrideGuard
3021        })
3022    }
3023}
3024
3025// Setters for tests.
3026// This is only needed for feature_flags. Please suffix each setter with
3027// `_for_testing`. Non-feature_flags should already have test setters defined
3028// through macros.
3029impl ProtocolConfig {
3030    pub fn set_per_object_congestion_control_mode_for_testing(
3031        &mut self,
3032        val: PerObjectCongestionControlMode,
3033    ) {
3034        self.feature_flags.per_object_congestion_control_mode = val;
3035    }
3036
3037    pub fn set_consensus_choice_for_testing(&mut self, val: ConsensusChoice) {
3038        self.feature_flags.consensus_choice = val;
3039    }
3040
3041    pub fn set_consensus_network_for_testing(&mut self, val: ConsensusNetwork) {
3042        self.feature_flags.consensus_network = val;
3043    }
3044
3045    pub fn set_passkey_auth_for_testing(&mut self, val: bool) {
3046        self.feature_flags.passkey_auth = val
3047    }
3048
3049    pub fn set_disallow_new_modules_in_deps_only_packages_for_testing(&mut self, val: bool) {
3050        self.feature_flags
3051            .disallow_new_modules_in_deps_only_packages = val;
3052    }
3053
3054    pub fn set_consensus_round_prober_for_testing(&mut self, val: bool) {
3055        self.feature_flags.consensus_round_prober = val;
3056    }
3057
3058    pub fn set_consensus_distributed_vote_scoring_strategy_for_testing(&mut self, val: bool) {
3059        self.feature_flags
3060            .consensus_distributed_vote_scoring_strategy = val;
3061    }
3062
3063    pub fn set_gc_depth_for_testing(&mut self, val: u32) {
3064        self.consensus_gc_depth = Some(val);
3065    }
3066
3067    pub fn set_consensus_linearize_subdag_v2_for_testing(&mut self, val: bool) {
3068        self.feature_flags.consensus_linearize_subdag_v2 = val;
3069    }
3070
3071    pub fn set_consensus_round_prober_probe_accepted_rounds(&mut self, val: bool) {
3072        self.feature_flags
3073            .consensus_round_prober_probe_accepted_rounds = val;
3074    }
3075
3076    pub fn set_accept_passkey_in_multisig_for_testing(&mut self, val: bool) {
3077        self.feature_flags.accept_passkey_in_multisig = val;
3078    }
3079
3080    pub fn set_consensus_smart_ancestor_selection_for_testing(&mut self, val: bool) {
3081        self.feature_flags.consensus_smart_ancestor_selection = val;
3082    }
3083
3084    pub fn set_consensus_batched_block_sync_for_testing(&mut self, val: bool) {
3085        self.feature_flags.consensus_batched_block_sync = val;
3086    }
3087
3088    pub fn set_congestion_control_min_free_execution_slot_for_testing(&mut self, val: bool) {
3089        self.feature_flags
3090            .congestion_control_min_free_execution_slot = val;
3091    }
3092
3093    pub fn set_congestion_control_gas_price_feedback_mechanism_for_testing(&mut self, val: bool) {
3094        self.feature_flags
3095            .congestion_control_gas_price_feedback_mechanism = val;
3096    }
3097
3098    pub fn set_select_committee_from_eligible_validators_for_testing(&mut self, val: bool) {
3099        self.feature_flags.select_committee_from_eligible_validators = val;
3100    }
3101
3102    pub fn set_track_non_committee_eligible_validators_for_testing(&mut self, val: bool) {
3103        self.feature_flags.track_non_committee_eligible_validators = val;
3104    }
3105
3106    pub fn set_select_committee_supporting_next_epoch_version(&mut self, val: bool) {
3107        self.feature_flags
3108            .select_committee_supporting_next_epoch_version = val;
3109    }
3110
3111    pub fn set_consensus_median_timestamp_with_checkpoint_enforcement_for_testing(
3112        &mut self,
3113        val: bool,
3114    ) {
3115        self.feature_flags
3116            .consensus_median_timestamp_with_checkpoint_enforcement = val;
3117    }
3118
3119    pub fn set_consensus_commit_transactions_only_for_traversed_headers_for_testing(
3120        &mut self,
3121        val: bool,
3122    ) {
3123        self.feature_flags
3124            .consensus_commit_transactions_only_for_traversed_headers = val;
3125    }
3126
3127    pub fn set_congestion_limit_overshoot_in_gas_price_feedback_mechanism_for_testing(
3128        &mut self,
3129        val: bool,
3130    ) {
3131        self.feature_flags
3132            .congestion_limit_overshoot_in_gas_price_feedback_mechanism = val;
3133    }
3134
3135    pub fn set_separate_gas_price_feedback_mechanism_for_randomness_for_testing(
3136        &mut self,
3137        val: bool,
3138    ) {
3139        self.feature_flags
3140            .separate_gas_price_feedback_mechanism_for_randomness = val;
3141    }
3142
3143    pub fn set_metadata_in_module_bytes_for_testing(&mut self, val: bool) {
3144        self.feature_flags.metadata_in_module_bytes = val;
3145    }
3146
3147    pub fn set_publish_package_metadata_for_testing(&mut self, val: bool) {
3148        self.feature_flags.publish_package_metadata = val;
3149    }
3150
3151    pub fn set_enable_move_authentication_for_testing(&mut self, val: bool) {
3152        self.feature_flags.enable_move_authentication = val;
3153    }
3154
3155    pub fn set_enable_move_authentication_for_sponsor_for_testing(&mut self, val: bool) {
3156        self.feature_flags.enable_move_authentication_for_sponsor = val;
3157    }
3158
3159    pub fn set_consensus_fast_commit_sync_for_testing(&mut self, val: bool) {
3160        self.feature_flags.consensus_fast_commit_sync = val;
3161    }
3162
3163    pub fn set_consensus_block_restrictions_for_testing(&mut self, val: bool) {
3164        self.feature_flags.consensus_block_restrictions = val;
3165    }
3166
3167    pub fn set_pre_consensus_sponsor_only_move_authentication_for_testing(&mut self, val: bool) {
3168        self.feature_flags
3169            .pre_consensus_sponsor_only_move_authentication = val;
3170    }
3171
3172    pub fn set_consensus_starfish_speed_for_testing(&mut self, val: bool) {
3173        self.feature_flags.consensus_starfish_speed = val;
3174    }
3175
3176    pub fn set_always_advance_dkg_to_resolution_for_testing(&mut self, val: bool) {
3177        self.feature_flags.always_advance_dkg_to_resolution = val;
3178    }
3179
3180    pub fn set_enable_white_flag_flow_for_testing(&mut self, val: bool) {
3181        self.feature_flags.enable_white_flag_flow = val;
3182    }
3183}
3184
3185type OverrideFn = dyn Fn(ProtocolVersion, ProtocolConfig) -> ProtocolConfig + Send + Sync;
3186
3187thread_local! {
3188    static CONFIG_OVERRIDE: RefCell<Option<Box<OverrideFn>>> = const { RefCell::new(None) };
3189}
3190
3191#[must_use]
3192pub struct OverrideGuard;
3193
3194impl Drop for OverrideGuard {
3195    fn drop(&mut self) {
3196        info!("restoring override fn");
3197        CONFIG_OVERRIDE.with(|ovr| {
3198            *ovr.borrow_mut() = None;
3199        });
3200    }
3201}
3202
3203/// Defines which limit got crossed.
3204/// The value which crossed the limit and value of the limit crossed are
3205/// embedded
3206#[derive(PartialEq, Eq)]
3207pub enum LimitThresholdCrossed {
3208    None,
3209    Soft(u128, u128),
3210    Hard(u128, u128),
3211}
3212
3213/// Convenience function for comparing limit ranges
3214/// V::MAX must be at >= U::MAX and T::MAX
3215pub fn check_limit_in_range<T: Into<V>, U: Into<V>, V: PartialOrd + Into<u128>>(
3216    x: T,
3217    soft_limit: U,
3218    hard_limit: V,
3219) -> LimitThresholdCrossed {
3220    let x: V = x.into();
3221    let soft_limit: V = soft_limit.into();
3222
3223    debug_assert!(soft_limit <= hard_limit);
3224
3225    // It is important to preserve this comparison order because if soft_limit ==
3226    // hard_limit we want LimitThresholdCrossed::Hard
3227    if x >= hard_limit {
3228        LimitThresholdCrossed::Hard(x.into(), hard_limit.into())
3229    } else if x < soft_limit {
3230        LimitThresholdCrossed::None
3231    } else {
3232        LimitThresholdCrossed::Soft(x.into(), soft_limit.into())
3233    }
3234}
3235
3236#[macro_export]
3237macro_rules! check_limit {
3238    ($x:expr, $hard:expr) => {
3239        check_limit!($x, $hard, $hard)
3240    };
3241    ($x:expr, $soft:expr, $hard:expr) => {
3242        check_limit_in_range($x as u64, $soft, $hard)
3243    };
3244}
3245
3246/// Used to check which limits were crossed if the TX is metered (not system tx)
3247/// Args are: is_metered, value_to_check, metered_limit, unmetered_limit
3248/// metered_limit is always less than or equal to unmetered_hard_limit
3249#[macro_export]
3250macro_rules! check_limit_by_meter {
3251    ($is_metered:expr, $x:expr, $metered_limit:expr, $unmetered_hard_limit:expr, $metric:expr) => {{
3252        // If this is metered, we use the metered_limit limit as the upper bound
3253        let (h, metered_str) = if $is_metered {
3254            ($metered_limit, "metered")
3255        } else {
3256            // Unmetered gets more headroom
3257            ($unmetered_hard_limit, "unmetered")
3258        };
3259        use iota_protocol_config::check_limit_in_range;
3260        let result = check_limit_in_range($x as u64, $metered_limit, h);
3261        match result {
3262            LimitThresholdCrossed::None => {}
3263            LimitThresholdCrossed::Soft(_, _) => {
3264                $metric.with_label_values(&[metered_str, "soft"]).inc();
3265            }
3266            LimitThresholdCrossed::Hard(_, _) => {
3267                $metric.with_label_values(&[metered_str, "hard"]).inc();
3268            }
3269        };
3270        result
3271    }};
3272}
3273
3274#[cfg(all(test, not(msim)))]
3275mod test {
3276    use insta::assert_yaml_snapshot;
3277
3278    use super::*;
3279
3280    #[test]
3281    fn snapshot_tests() {
3282        println!("\n============================================================================");
3283        println!("!                                                                          !");
3284        println!("! IMPORTANT: never update snapshots from this test. only add new versions! !");
3285        println!("!                                                                          !");
3286        println!("============================================================================\n");
3287        for chain_id in &[Chain::Unknown, Chain::Mainnet, Chain::Testnet] {
3288            // make Chain::Unknown snapshots compatible with pre-chain-id snapshots so that
3289            // we don't break the release-time compatibility tests. Once Chain
3290            // Id configs have been released everywhere, we can remove this and
3291            // only test Mainnet and Testnet
3292            let chain_str = match chain_id {
3293                Chain::Unknown => "".to_string(),
3294                _ => format!("{chain_id:?}_"),
3295            };
3296            for i in MIN_PROTOCOL_VERSION..=MAX_PROTOCOL_VERSION {
3297                let cur = ProtocolVersion::new(i);
3298                assert_yaml_snapshot!(
3299                    format!("{}version_{}", chain_str, cur.as_u64()),
3300                    ProtocolConfig::get_for_version(cur, *chain_id)
3301                );
3302            }
3303        }
3304    }
3305
3306    #[test]
3307    fn test_getters() {
3308        let prot: ProtocolConfig =
3309            ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
3310        assert_eq!(
3311            prot.max_arguments(),
3312            prot.max_arguments_as_option().unwrap()
3313        );
3314    }
3315
3316    #[test]
3317    fn test_setters() {
3318        let mut prot: ProtocolConfig =
3319            ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
3320        prot.set_max_arguments_for_testing(123);
3321        assert_eq!(prot.max_arguments(), 123);
3322
3323        prot.set_max_arguments_from_str_for_testing("321".to_string());
3324        assert_eq!(prot.max_arguments(), 321);
3325
3326        prot.disable_max_arguments_for_testing();
3327        assert_eq!(prot.max_arguments_as_option(), None);
3328
3329        prot.set_attr_for_testing("max_arguments".to_string(), "456".to_string());
3330        assert_eq!(prot.max_arguments(), 456);
3331    }
3332
3333    #[test]
3334    #[should_panic(expected = "unsupported version")]
3335    fn max_version_test() {
3336        // When this does not panic, version higher than MAX_PROTOCOL_VERSION exists.
3337        // To fix, bump MAX_PROTOCOL_VERSION or disable this check for the version.
3338        let _ = ProtocolConfig::get_for_version_impl(
3339            ProtocolVersion::new(MAX_PROTOCOL_VERSION + 1),
3340            Chain::Unknown,
3341        );
3342    }
3343
3344    #[test]
3345    fn lookup_by_string_test() {
3346        let prot: ProtocolConfig =
3347            ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Mainnet);
3348        // Does not exist
3349        assert!(prot.lookup_attr("some random string".to_string()).is_none());
3350
3351        assert!(
3352            prot.lookup_attr("max_arguments".to_string())
3353                == Some(ProtocolConfigValue::u32(prot.max_arguments())),
3354        );
3355
3356        // We didnt have this in version 1 on Mainnet
3357        assert!(
3358            prot.lookup_attr("poseidon_bn254_cost_base".to_string())
3359                .is_none()
3360        );
3361        assert!(
3362            prot.attr_map()
3363                .get("poseidon_bn254_cost_base")
3364                .unwrap()
3365                .is_none()
3366        );
3367
3368        // But we did in version 1 on Devnet
3369        let prot: ProtocolConfig =
3370            ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
3371
3372        assert!(
3373            prot.lookup_attr("poseidon_bn254_cost_base".to_string())
3374                == Some(ProtocolConfigValue::u64(prot.poseidon_bn254_cost_base()))
3375        );
3376        assert!(
3377            prot.attr_map().get("poseidon_bn254_cost_base").unwrap()
3378                == &Some(ProtocolConfigValue::u64(prot.poseidon_bn254_cost_base()))
3379        );
3380
3381        // Check feature flags
3382        let prot: ProtocolConfig =
3383            ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Mainnet);
3384        // Does not exist
3385        assert!(
3386            prot.feature_flags
3387                .lookup_attr("some random string".to_owned())
3388                .is_none()
3389        );
3390        assert!(
3391            !prot
3392                .feature_flags
3393                .attr_map()
3394                .contains_key("some random string")
3395        );
3396
3397        // Was false in v1 on Mainnet
3398        assert!(prot.feature_flags.lookup_attr("enable_poseidon".to_owned()) == Some(false));
3399        assert!(
3400            prot.feature_flags
3401                .attr_map()
3402                .get("enable_poseidon")
3403                .unwrap()
3404                == &false
3405        );
3406        let prot: ProtocolConfig =
3407            ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
3408        // Was true from v1 and up on Devnet
3409        assert!(prot.feature_flags.lookup_attr("enable_poseidon".to_owned()) == Some(true));
3410        assert!(
3411            prot.feature_flags
3412                .attr_map()
3413                .get("enable_poseidon")
3414                .unwrap()
3415                == &true
3416        );
3417    }
3418
3419    #[test]
3420    fn limit_range_fn_test() {
3421        let low = 100u32;
3422        let high = 10000u64;
3423
3424        assert!(check_limit!(1u8, low, high) == LimitThresholdCrossed::None);
3425        assert!(matches!(
3426            check_limit!(255u16, low, high),
3427            LimitThresholdCrossed::Soft(255u128, 100)
3428        ));
3429        // This wont compile because lossy
3430        // assert!(check_limit!(100000000u128, low, high) ==
3431        // LimitThresholdCrossed::None); This wont compile because lossy
3432        // assert!(check_limit!(100000000usize, low, high) ==
3433        // LimitThresholdCrossed::None);
3434
3435        assert!(matches!(
3436            check_limit!(2550000u64, low, high),
3437            LimitThresholdCrossed::Hard(2550000, 10000)
3438        ));
3439
3440        assert!(matches!(
3441            check_limit!(2550000u64, high, high),
3442            LimitThresholdCrossed::Hard(2550000, 10000)
3443        ));
3444
3445        assert!(matches!(
3446            check_limit!(1u8, high),
3447            LimitThresholdCrossed::None
3448        ));
3449
3450        assert!(check_limit!(255u16, high) == LimitThresholdCrossed::None);
3451
3452        assert!(matches!(
3453            check_limit!(2550000u64, high),
3454            LimitThresholdCrossed::Hard(2550000, 10000)
3455        ));
3456    }
3457}