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