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