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