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