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