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