1use 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
20const MIN_PROTOCOL_VERSION: u64 = 1;
22pub const MAX_PROTOCOL_VERSION: u64 = 11;
23
24#[derive(Copy, Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
76pub struct ProtocolVersion(u64);
77
78impl ProtocolVersion {
79 pub const MIN: Self = Self(MIN_PROTOCOL_VERSION);
85
86 pub const MAX: Self = Self(MAX_PROTOCOL_VERSION);
87
88 #[cfg(not(msim))]
89 const MAX_ALLOWED: Self = Self::MAX;
90
91 #[cfg(msim)]
94 pub const MAX_ALLOWED: Self = Self(MAX_PROTOCOL_VERSION + 1);
95
96 pub fn new(v: u64) -> Self {
97 Self(v)
98 }
99
100 pub const fn as_u64(&self) -> u64 {
101 self.0
102 }
103
104 pub fn max() -> Self {
107 Self::MAX
108 }
109}
110
111impl From<u64> for ProtocolVersion {
112 fn from(v: u64) -> Self {
113 Self::new(v)
114 }
115}
116
117impl std::ops::Sub<u64> for ProtocolVersion {
118 type Output = Self;
119 fn sub(self, rhs: u64) -> Self::Output {
120 Self::new(self.0 - rhs)
121 }
122}
123
124impl std::ops::Add<u64> for ProtocolVersion {
125 type Output = Self;
126 fn add(self, rhs: u64) -> Self::Output {
127 Self::new(self.0 + rhs)
128 }
129}
130
131#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Copy, PartialOrd, Ord, Eq, ValueEnum)]
132pub enum Chain {
133 Mainnet,
134 Testnet,
135 Unknown,
136}
137
138impl Default for Chain {
139 fn default() -> Self {
140 Self::Unknown
141 }
142}
143
144impl Chain {
145 pub fn as_str(self) -> &'static str {
146 match self {
147 Chain::Mainnet => "mainnet",
148 Chain::Testnet => "testnet",
149 Chain::Unknown => "unknown",
150 }
151 }
152}
153
154pub struct Error(pub String);
155
156#[derive(Default, Clone, Serialize, Deserialize, Debug, ProtocolConfigFeatureFlagsGetters)]
160struct FeatureFlags {
161 #[serde(skip_serializing_if = "is_true")]
167 disable_invariant_violation_check_in_swap_loc: bool,
168
169 #[serde(skip_serializing_if = "is_true")]
172 no_extraneous_module_bytes: bool,
173
174 #[serde(skip_serializing_if = "is_false")]
176 zklogin_auth: bool,
177
178 #[serde(skip_serializing_if = "ConsensusTransactionOrdering::is_none")]
180 consensus_transaction_ordering: ConsensusTransactionOrdering,
181
182 #[serde(skip_serializing_if = "is_false")]
183 enable_jwk_consensus_updates: bool,
184
185 #[serde(skip_serializing_if = "is_false")]
187 accept_zklogin_in_multisig: bool,
188
189 #[serde(skip_serializing_if = "is_true")]
192 hardened_otw_check: bool,
193
194 #[serde(skip_serializing_if = "is_false")]
196 enable_poseidon: bool,
197
198 #[serde(skip_serializing_if = "is_false")]
200 enable_group_ops_native_function_msm: bool,
201
202 #[serde(skip_serializing_if = "PerObjectCongestionControlMode::is_none")]
204 per_object_congestion_control_mode: PerObjectCongestionControlMode,
205
206 #[serde(skip_serializing_if = "ConsensusChoice::is_mysticeti")]
208 consensus_choice: ConsensusChoice,
209
210 #[serde(skip_serializing_if = "ConsensusNetwork::is_tonic")]
212 consensus_network: ConsensusNetwork,
213
214 #[serde(skip_serializing_if = "Option::is_none")]
216 zklogin_max_epoch_upper_bound_delta: Option<u64>,
217
218 #[serde(skip_serializing_if = "is_false")]
220 enable_vdf: bool,
221
222 #[serde(skip_serializing_if = "is_false")]
224 passkey_auth: bool,
225
226 #[serde(skip_serializing_if = "is_true")]
229 rethrow_serialization_type_layout_errors: bool,
230
231 #[serde(skip_serializing_if = "is_false")]
233 relocate_event_module: bool,
234
235 #[serde(skip_serializing_if = "is_false")]
237 protocol_defined_base_fee: bool,
238
239 #[serde(skip_serializing_if = "is_false")]
241 uncompressed_g1_group_elements: bool,
242
243 #[serde(skip_serializing_if = "is_false")]
245 disallow_new_modules_in_deps_only_packages: bool,
246
247 #[serde(skip_serializing_if = "is_false")]
249 native_charging_v2: bool,
250
251 #[serde(skip_serializing_if = "is_false")]
253 convert_type_argument_error: bool,
254
255 #[serde(skip_serializing_if = "is_false")]
257 consensus_round_prober: bool,
258
259 #[serde(skip_serializing_if = "is_false")]
261 consensus_distributed_vote_scoring_strategy: bool,
262
263 #[serde(skip_serializing_if = "is_false")]
267 consensus_linearize_subdag_v2: bool,
268
269 #[serde(skip_serializing_if = "is_false")]
271 variant_nodes: bool,
272
273 #[serde(skip_serializing_if = "is_false")]
275 consensus_smart_ancestor_selection: bool,
276
277 #[serde(skip_serializing_if = "is_false")]
279 consensus_round_prober_probe_accepted_rounds: bool,
280
281 #[serde(skip_serializing_if = "is_false")]
283 consensus_zstd_compression: bool,
284
285 #[serde(skip_serializing_if = "is_false")]
288 congestion_control_min_free_execution_slot: bool,
289
290 #[serde(skip_serializing_if = "is_false")]
292 accept_passkey_in_multisig: bool,
293
294 #[serde(skip_serializing_if = "is_false")]
296 consensus_batched_block_sync: bool,
297
298 #[serde(skip_serializing_if = "is_false")]
301 congestion_control_gas_price_feedback_mechanism: bool,
302
303 #[serde(skip_serializing_if = "is_false")]
305 validate_identifier_inputs: bool,
306
307 #[serde(skip_serializing_if = "is_false")]
310 minimize_child_object_mutations: bool,
311
312 #[serde(skip_serializing_if = "is_false")]
314 dependency_linkage_error: bool,
315
316 #[serde(skip_serializing_if = "is_false")]
318 additional_multisig_checks: bool,
319}
320
321fn is_true(b: &bool) -> bool {
322 *b
323}
324
325fn is_false(b: &bool) -> bool {
326 !b
327}
328
329#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
331pub enum ConsensusTransactionOrdering {
332 #[default]
335 None,
336 ByGasPrice,
338}
339
340impl ConsensusTransactionOrdering {
341 pub fn is_none(&self) -> bool {
342 matches!(self, ConsensusTransactionOrdering::None)
343 }
344}
345
346#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
348pub enum PerObjectCongestionControlMode {
349 #[default]
350 None, TotalGasBudget, TotalTxCount, }
354
355impl PerObjectCongestionControlMode {
356 pub fn is_none(&self) -> bool {
357 matches!(self, PerObjectCongestionControlMode::None)
358 }
359}
360
361#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
363pub enum ConsensusChoice {
364 #[default]
365 Mysticeti,
366}
367
368impl ConsensusChoice {
369 pub fn is_mysticeti(&self) -> bool {
370 matches!(self, ConsensusChoice::Mysticeti)
371 }
372}
373
374#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
376pub enum ConsensusNetwork {
377 #[default]
378 Tonic,
379}
380
381impl ConsensusNetwork {
382 pub fn is_tonic(&self) -> bool {
383 matches!(self, ConsensusNetwork::Tonic)
384 }
385}
386
387#[skip_serializing_none]
421#[derive(Clone, Serialize, Debug, ProtocolConfigAccessors, ProtocolConfigOverride)]
422pub struct ProtocolConfig {
423 pub version: ProtocolVersion,
424
425 feature_flags: FeatureFlags,
426
427 max_tx_size_bytes: Option<u64>,
432
433 max_input_objects: Option<u64>,
436
437 max_size_written_objects: Option<u64>,
442 max_size_written_objects_system_tx: Option<u64>,
446
447 max_serialized_tx_effects_size_bytes: Option<u64>,
449
450 max_serialized_tx_effects_size_bytes_system_tx: Option<u64>,
452
453 max_gas_payment_objects: Option<u32>,
455
456 max_modules_in_publish: Option<u32>,
458
459 max_package_dependencies: Option<u32>,
461
462 max_arguments: Option<u32>,
465
466 max_type_arguments: Option<u32>,
468
469 max_type_argument_depth: Option<u32>,
471
472 max_pure_argument_size: Option<u32>,
474
475 max_programmable_tx_commands: Option<u32>,
477
478 move_binary_format_version: Option<u32>,
484 min_move_binary_format_version: Option<u32>,
485
486 binary_module_handles: Option<u16>,
488 binary_struct_handles: Option<u16>,
489 binary_function_handles: Option<u16>,
490 binary_function_instantiations: Option<u16>,
491 binary_signatures: Option<u16>,
492 binary_constant_pool: Option<u16>,
493 binary_identifiers: Option<u16>,
494 binary_address_identifiers: Option<u16>,
495 binary_struct_defs: Option<u16>,
496 binary_struct_def_instantiations: Option<u16>,
497 binary_function_defs: Option<u16>,
498 binary_field_handles: Option<u16>,
499 binary_field_instantiations: Option<u16>,
500 binary_friend_decls: Option<u16>,
501 binary_enum_defs: Option<u16>,
502 binary_enum_def_instantiations: Option<u16>,
503 binary_variant_handles: Option<u16>,
504 binary_variant_instantiation_handles: Option<u16>,
505
506 max_move_object_size: Option<u64>,
509
510 max_move_package_size: Option<u64>,
515
516 max_publish_or_upgrade_per_ptb: Option<u64>,
519
520 max_tx_gas: Option<u64>,
522
523 max_gas_price: Option<u64>,
526
527 max_gas_computation_bucket: Option<u64>,
530
531 gas_rounding_step: Option<u64>,
533
534 max_loop_depth: Option<u64>,
536
537 max_generic_instantiation_length: Option<u64>,
540
541 max_function_parameters: Option<u64>,
544
545 max_basic_blocks: Option<u64>,
548
549 max_value_stack_size: Option<u64>,
551
552 max_type_nodes: Option<u64>,
556
557 max_push_size: Option<u64>,
560
561 max_struct_definitions: Option<u64>,
564
565 max_function_definitions: Option<u64>,
568
569 max_fields_in_struct: Option<u64>,
572
573 max_dependency_depth: Option<u64>,
576
577 max_num_event_emit: Option<u64>,
580
581 max_num_new_move_object_ids: Option<u64>,
584
585 max_num_new_move_object_ids_system_tx: Option<u64>,
588
589 max_num_deleted_move_object_ids: Option<u64>,
592
593 max_num_deleted_move_object_ids_system_tx: Option<u64>,
596
597 max_num_transferred_move_object_ids: Option<u64>,
600
601 max_num_transferred_move_object_ids_system_tx: Option<u64>,
604
605 max_event_emit_size: Option<u64>,
607
608 max_event_emit_size_total: Option<u64>,
610
611 max_move_vector_len: Option<u64>,
614
615 max_move_identifier_len: Option<u64>,
618
619 max_move_value_depth: Option<u64>,
621
622 max_move_enum_variants: Option<u64>,
625
626 max_back_edges_per_function: Option<u64>,
629
630 max_back_edges_per_module: Option<u64>,
633
634 max_verifier_meter_ticks_per_function: Option<u64>,
637
638 max_meter_ticks_per_module: Option<u64>,
641
642 max_meter_ticks_per_package: Option<u64>,
645
646 object_runtime_max_num_cached_objects: Option<u64>,
653
654 object_runtime_max_num_cached_objects_system_tx: Option<u64>,
657
658 object_runtime_max_num_store_entries: Option<u64>,
661
662 object_runtime_max_num_store_entries_system_tx: Option<u64>,
665
666 base_tx_cost_fixed: Option<u64>,
671
672 package_publish_cost_fixed: Option<u64>,
676
677 base_tx_cost_per_byte: Option<u64>,
681
682 package_publish_cost_per_byte: Option<u64>,
684
685 obj_access_cost_read_per_byte: Option<u64>,
687
688 obj_access_cost_mutate_per_byte: Option<u64>,
690
691 obj_access_cost_delete_per_byte: Option<u64>,
693
694 obj_access_cost_verify_per_byte: Option<u64>,
704
705 max_type_to_layout_nodes: Option<u64>,
707
708 max_ptb_value_size: Option<u64>,
710
711 gas_model_version: Option<u64>,
716
717 obj_data_cost_refundable: Option<u64>,
723
724 obj_metadata_cost_non_refundable: Option<u64>,
728
729 storage_rebate_rate: Option<u64>,
735
736 reward_slashing_rate: Option<u64>,
739
740 storage_gas_price: Option<u64>,
742
743 base_gas_price: Option<u64>,
745
746 validator_target_reward: Option<u64>,
748
749 max_transactions_per_checkpoint: Option<u64>,
756
757 max_checkpoint_size_bytes: Option<u64>,
761
762 buffer_stake_for_protocol_upgrade_bps: Option<u64>,
768
769 address_from_bytes_cost_base: Option<u64>,
774 address_to_u256_cost_base: Option<u64>,
776 address_from_u256_cost_base: Option<u64>,
778
779 config_read_setting_impl_cost_base: Option<u64>,
784 config_read_setting_impl_cost_per_byte: Option<u64>,
785
786 dynamic_field_hash_type_and_key_cost_base: Option<u64>,
790 dynamic_field_hash_type_and_key_type_cost_per_byte: Option<u64>,
791 dynamic_field_hash_type_and_key_value_cost_per_byte: Option<u64>,
792 dynamic_field_hash_type_and_key_type_tag_cost_per_byte: Option<u64>,
793 dynamic_field_add_child_object_cost_base: Option<u64>,
796 dynamic_field_add_child_object_type_cost_per_byte: Option<u64>,
797 dynamic_field_add_child_object_value_cost_per_byte: Option<u64>,
798 dynamic_field_add_child_object_struct_tag_cost_per_byte: Option<u64>,
799 dynamic_field_borrow_child_object_cost_base: Option<u64>,
802 dynamic_field_borrow_child_object_child_ref_cost_per_byte: Option<u64>,
803 dynamic_field_borrow_child_object_type_cost_per_byte: Option<u64>,
804 dynamic_field_remove_child_object_cost_base: Option<u64>,
807 dynamic_field_remove_child_object_child_cost_per_byte: Option<u64>,
808 dynamic_field_remove_child_object_type_cost_per_byte: Option<u64>,
809 dynamic_field_has_child_object_cost_base: Option<u64>,
812 dynamic_field_has_child_object_with_ty_cost_base: Option<u64>,
815 dynamic_field_has_child_object_with_ty_type_cost_per_byte: Option<u64>,
816 dynamic_field_has_child_object_with_ty_type_tag_cost_per_byte: Option<u64>,
817
818 event_emit_cost_base: Option<u64>,
821 event_emit_value_size_derivation_cost_per_byte: Option<u64>,
822 event_emit_tag_size_derivation_cost_per_byte: Option<u64>,
823 event_emit_output_cost_per_byte: Option<u64>,
824
825 object_borrow_uid_cost_base: Option<u64>,
828 object_delete_impl_cost_base: Option<u64>,
830 object_record_new_uid_cost_base: Option<u64>,
832
833 transfer_transfer_internal_cost_base: Option<u64>,
836 transfer_freeze_object_cost_base: Option<u64>,
838 transfer_share_object_cost_base: Option<u64>,
840 transfer_receive_object_cost_base: Option<u64>,
843
844 tx_context_derive_id_cost_base: Option<u64>,
847
848 types_is_one_time_witness_cost_base: Option<u64>,
851 types_is_one_time_witness_type_tag_cost_per_byte: Option<u64>,
852 types_is_one_time_witness_type_cost_per_byte: Option<u64>,
853
854 validator_validate_metadata_cost_base: Option<u64>,
857 validator_validate_metadata_data_cost_per_byte: Option<u64>,
858
859 crypto_invalid_arguments_cost: Option<u64>,
861 bls12381_bls12381_min_sig_verify_cost_base: Option<u64>,
863 bls12381_bls12381_min_sig_verify_msg_cost_per_byte: Option<u64>,
864 bls12381_bls12381_min_sig_verify_msg_cost_per_block: Option<u64>,
865
866 bls12381_bls12381_min_pk_verify_cost_base: Option<u64>,
868 bls12381_bls12381_min_pk_verify_msg_cost_per_byte: Option<u64>,
869 bls12381_bls12381_min_pk_verify_msg_cost_per_block: Option<u64>,
870
871 ecdsa_k1_ecrecover_keccak256_cost_base: Option<u64>,
873 ecdsa_k1_ecrecover_keccak256_msg_cost_per_byte: Option<u64>,
874 ecdsa_k1_ecrecover_keccak256_msg_cost_per_block: Option<u64>,
875 ecdsa_k1_ecrecover_sha256_cost_base: Option<u64>,
876 ecdsa_k1_ecrecover_sha256_msg_cost_per_byte: Option<u64>,
877 ecdsa_k1_ecrecover_sha256_msg_cost_per_block: Option<u64>,
878
879 ecdsa_k1_decompress_pubkey_cost_base: Option<u64>,
881
882 ecdsa_k1_secp256k1_verify_keccak256_cost_base: Option<u64>,
884 ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_byte: Option<u64>,
885 ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_block: Option<u64>,
886 ecdsa_k1_secp256k1_verify_sha256_cost_base: Option<u64>,
887 ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_byte: Option<u64>,
888 ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_block: Option<u64>,
889
890 ecdsa_r1_ecrecover_keccak256_cost_base: Option<u64>,
892 ecdsa_r1_ecrecover_keccak256_msg_cost_per_byte: Option<u64>,
893 ecdsa_r1_ecrecover_keccak256_msg_cost_per_block: Option<u64>,
894 ecdsa_r1_ecrecover_sha256_cost_base: Option<u64>,
895 ecdsa_r1_ecrecover_sha256_msg_cost_per_byte: Option<u64>,
896 ecdsa_r1_ecrecover_sha256_msg_cost_per_block: Option<u64>,
897
898 ecdsa_r1_secp256r1_verify_keccak256_cost_base: Option<u64>,
900 ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_byte: Option<u64>,
901 ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_block: Option<u64>,
902 ecdsa_r1_secp256r1_verify_sha256_cost_base: Option<u64>,
903 ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_byte: Option<u64>,
904 ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_block: Option<u64>,
905
906 ecvrf_ecvrf_verify_cost_base: Option<u64>,
908 ecvrf_ecvrf_verify_alpha_string_cost_per_byte: Option<u64>,
909 ecvrf_ecvrf_verify_alpha_string_cost_per_block: Option<u64>,
910
911 ed25519_ed25519_verify_cost_base: Option<u64>,
913 ed25519_ed25519_verify_msg_cost_per_byte: Option<u64>,
914 ed25519_ed25519_verify_msg_cost_per_block: Option<u64>,
915
916 groth16_prepare_verifying_key_bls12381_cost_base: Option<u64>,
918 groth16_prepare_verifying_key_bn254_cost_base: Option<u64>,
919
920 groth16_verify_groth16_proof_internal_bls12381_cost_base: Option<u64>,
922 groth16_verify_groth16_proof_internal_bls12381_cost_per_public_input: Option<u64>,
923 groth16_verify_groth16_proof_internal_bn254_cost_base: Option<u64>,
924 groth16_verify_groth16_proof_internal_bn254_cost_per_public_input: Option<u64>,
925 groth16_verify_groth16_proof_internal_public_input_cost_per_byte: Option<u64>,
926
927 hash_blake2b256_cost_base: Option<u64>,
929 hash_blake2b256_data_cost_per_byte: Option<u64>,
930 hash_blake2b256_data_cost_per_block: Option<u64>,
931
932 hash_keccak256_cost_base: Option<u64>,
934 hash_keccak256_data_cost_per_byte: Option<u64>,
935 hash_keccak256_data_cost_per_block: Option<u64>,
936
937 poseidon_bn254_cost_base: Option<u64>,
939 poseidon_bn254_cost_per_block: Option<u64>,
940
941 group_ops_bls12381_decode_scalar_cost: Option<u64>,
943 group_ops_bls12381_decode_g1_cost: Option<u64>,
944 group_ops_bls12381_decode_g2_cost: Option<u64>,
945 group_ops_bls12381_decode_gt_cost: Option<u64>,
946 group_ops_bls12381_scalar_add_cost: Option<u64>,
947 group_ops_bls12381_g1_add_cost: Option<u64>,
948 group_ops_bls12381_g2_add_cost: Option<u64>,
949 group_ops_bls12381_gt_add_cost: Option<u64>,
950 group_ops_bls12381_scalar_sub_cost: Option<u64>,
951 group_ops_bls12381_g1_sub_cost: Option<u64>,
952 group_ops_bls12381_g2_sub_cost: Option<u64>,
953 group_ops_bls12381_gt_sub_cost: Option<u64>,
954 group_ops_bls12381_scalar_mul_cost: Option<u64>,
955 group_ops_bls12381_g1_mul_cost: Option<u64>,
956 group_ops_bls12381_g2_mul_cost: Option<u64>,
957 group_ops_bls12381_gt_mul_cost: Option<u64>,
958 group_ops_bls12381_scalar_div_cost: Option<u64>,
959 group_ops_bls12381_g1_div_cost: Option<u64>,
960 group_ops_bls12381_g2_div_cost: Option<u64>,
961 group_ops_bls12381_gt_div_cost: Option<u64>,
962 group_ops_bls12381_g1_hash_to_base_cost: Option<u64>,
963 group_ops_bls12381_g2_hash_to_base_cost: Option<u64>,
964 group_ops_bls12381_g1_hash_to_cost_per_byte: Option<u64>,
965 group_ops_bls12381_g2_hash_to_cost_per_byte: Option<u64>,
966 group_ops_bls12381_g1_msm_base_cost: Option<u64>,
967 group_ops_bls12381_g2_msm_base_cost: Option<u64>,
968 group_ops_bls12381_g1_msm_base_cost_per_input: Option<u64>,
969 group_ops_bls12381_g2_msm_base_cost_per_input: Option<u64>,
970 group_ops_bls12381_msm_max_len: Option<u32>,
971 group_ops_bls12381_pairing_cost: Option<u64>,
972 group_ops_bls12381_g1_to_uncompressed_g1_cost: Option<u64>,
973 group_ops_bls12381_uncompressed_g1_to_g1_cost: Option<u64>,
974 group_ops_bls12381_uncompressed_g1_sum_base_cost: Option<u64>,
975 group_ops_bls12381_uncompressed_g1_sum_cost_per_term: Option<u64>,
976 group_ops_bls12381_uncompressed_g1_sum_max_terms: Option<u64>,
977
978 hmac_hmac_sha3_256_cost_base: Option<u64>,
980 hmac_hmac_sha3_256_input_cost_per_byte: Option<u64>,
981 hmac_hmac_sha3_256_input_cost_per_block: Option<u64>,
982
983 check_zklogin_id_cost_base: Option<u64>,
985 check_zklogin_issuer_cost_base: Option<u64>,
987
988 vdf_verify_vdf_cost: Option<u64>,
989 vdf_hash_to_input_cost: Option<u64>,
990
991 bcs_per_byte_serialized_cost: Option<u64>,
993 bcs_legacy_min_output_size_cost: Option<u64>,
994 bcs_failure_cost: Option<u64>,
995
996 hash_sha2_256_base_cost: Option<u64>,
997 hash_sha2_256_per_byte_cost: Option<u64>,
998 hash_sha2_256_legacy_min_input_len_cost: Option<u64>,
999 hash_sha3_256_base_cost: Option<u64>,
1000 hash_sha3_256_per_byte_cost: Option<u64>,
1001 hash_sha3_256_legacy_min_input_len_cost: Option<u64>,
1002 type_name_get_base_cost: Option<u64>,
1003 type_name_get_per_byte_cost: Option<u64>,
1004
1005 string_check_utf8_base_cost: Option<u64>,
1006 string_check_utf8_per_byte_cost: Option<u64>,
1007 string_is_char_boundary_base_cost: Option<u64>,
1008 string_sub_string_base_cost: Option<u64>,
1009 string_sub_string_per_byte_cost: Option<u64>,
1010 string_index_of_base_cost: Option<u64>,
1011 string_index_of_per_byte_pattern_cost: Option<u64>,
1012 string_index_of_per_byte_searched_cost: Option<u64>,
1013
1014 vector_empty_base_cost: Option<u64>,
1015 vector_length_base_cost: Option<u64>,
1016 vector_push_back_base_cost: Option<u64>,
1017 vector_push_back_legacy_per_abstract_memory_unit_cost: Option<u64>,
1018 vector_borrow_base_cost: Option<u64>,
1019 vector_pop_back_base_cost: Option<u64>,
1020 vector_destroy_empty_base_cost: Option<u64>,
1021 vector_swap_base_cost: Option<u64>,
1022 debug_print_base_cost: Option<u64>,
1023 debug_print_stack_trace_base_cost: Option<u64>,
1024
1025 execution_version: Option<u64>,
1027
1028 consensus_bad_nodes_stake_threshold: Option<u64>,
1032
1033 max_jwk_votes_per_validator_per_epoch: Option<u64>,
1034 max_age_of_jwk_in_epochs: Option<u64>,
1038
1039 random_beacon_reduction_allowed_delta: Option<u16>,
1043
1044 random_beacon_reduction_lower_bound: Option<u32>,
1047
1048 random_beacon_dkg_timeout_round: Option<u32>,
1051
1052 random_beacon_min_round_interval_ms: Option<u64>,
1054
1055 random_beacon_dkg_version: Option<u64>,
1059
1060 consensus_max_transaction_size_bytes: Option<u64>,
1065 consensus_max_transactions_in_block_bytes: Option<u64>,
1067 consensus_max_num_transactions_in_block: Option<u64>,
1069
1070 max_deferral_rounds_for_congestion_control: Option<u64>,
1074
1075 min_checkpoint_interval_ms: Option<u64>,
1077
1078 checkpoint_summary_version_specific_data: Option<u64>,
1080
1081 max_soft_bundle_size: Option<u64>,
1084
1085 bridge_should_try_to_finalize_committee: Option<bool>,
1090
1091 max_accumulated_txn_cost_per_object_in_mysticeti_commit: Option<u64>,
1095
1096 max_committee_members_count: Option<u64>,
1100
1101 consensus_gc_depth: Option<u32>,
1104}
1105
1106impl ProtocolConfig {
1108 pub fn disable_invariant_violation_check_in_swap_loc(&self) -> bool {
1121 self.feature_flags
1122 .disable_invariant_violation_check_in_swap_loc
1123 }
1124
1125 pub fn no_extraneous_module_bytes(&self) -> bool {
1126 self.feature_flags.no_extraneous_module_bytes
1127 }
1128
1129 pub fn zklogin_auth(&self) -> bool {
1130 self.feature_flags.zklogin_auth
1131 }
1132
1133 pub fn consensus_transaction_ordering(&self) -> ConsensusTransactionOrdering {
1134 self.feature_flags.consensus_transaction_ordering
1135 }
1136
1137 pub fn enable_jwk_consensus_updates(&self) -> bool {
1138 self.feature_flags.enable_jwk_consensus_updates
1139 }
1140
1141 pub fn create_authenticator_state_in_genesis(&self) -> bool {
1143 self.enable_jwk_consensus_updates()
1144 }
1145
1146 pub fn dkg_version(&self) -> u64 {
1147 self.random_beacon_dkg_version.unwrap_or(1)
1149 }
1150
1151 pub fn accept_zklogin_in_multisig(&self) -> bool {
1152 self.feature_flags.accept_zklogin_in_multisig
1153 }
1154
1155 pub fn zklogin_max_epoch_upper_bound_delta(&self) -> Option<u64> {
1156 self.feature_flags.zklogin_max_epoch_upper_bound_delta
1157 }
1158
1159 pub fn hardened_otw_check(&self) -> bool {
1160 self.feature_flags.hardened_otw_check
1161 }
1162
1163 pub fn enable_poseidon(&self) -> bool {
1164 self.feature_flags.enable_poseidon
1165 }
1166
1167 pub fn enable_group_ops_native_function_msm(&self) -> bool {
1168 self.feature_flags.enable_group_ops_native_function_msm
1169 }
1170
1171 pub fn per_object_congestion_control_mode(&self) -> PerObjectCongestionControlMode {
1172 self.feature_flags.per_object_congestion_control_mode
1173 }
1174
1175 pub fn consensus_choice(&self) -> ConsensusChoice {
1176 self.feature_flags.consensus_choice
1177 }
1178
1179 pub fn consensus_network(&self) -> ConsensusNetwork {
1180 self.feature_flags.consensus_network
1181 }
1182
1183 pub fn enable_vdf(&self) -> bool {
1184 self.feature_flags.enable_vdf
1185 }
1186
1187 pub fn passkey_auth(&self) -> bool {
1188 self.feature_flags.passkey_auth
1189 }
1190
1191 pub fn max_transaction_size_bytes(&self) -> u64 {
1192 self.consensus_max_transaction_size_bytes
1194 .unwrap_or(256 * 1024)
1195 }
1196
1197 pub fn max_transactions_in_block_bytes(&self) -> u64 {
1198 if cfg!(msim) {
1199 256 * 1024
1200 } else {
1201 self.consensus_max_transactions_in_block_bytes
1202 .unwrap_or(512 * 1024)
1203 }
1204 }
1205
1206 pub fn max_num_transactions_in_block(&self) -> u64 {
1207 if cfg!(msim) {
1208 8
1209 } else {
1210 self.consensus_max_num_transactions_in_block.unwrap_or(512)
1211 }
1212 }
1213
1214 pub fn rethrow_serialization_type_layout_errors(&self) -> bool {
1215 self.feature_flags.rethrow_serialization_type_layout_errors
1216 }
1217
1218 pub fn relocate_event_module(&self) -> bool {
1219 self.feature_flags.relocate_event_module
1220 }
1221
1222 pub fn protocol_defined_base_fee(&self) -> bool {
1223 self.feature_flags.protocol_defined_base_fee
1224 }
1225
1226 pub fn uncompressed_g1_group_elements(&self) -> bool {
1227 self.feature_flags.uncompressed_g1_group_elements
1228 }
1229
1230 pub fn disallow_new_modules_in_deps_only_packages(&self) -> bool {
1231 self.feature_flags
1232 .disallow_new_modules_in_deps_only_packages
1233 }
1234
1235 pub fn native_charging_v2(&self) -> bool {
1236 self.feature_flags.native_charging_v2
1237 }
1238
1239 pub fn consensus_round_prober(&self) -> bool {
1240 self.feature_flags.consensus_round_prober
1241 }
1242
1243 pub fn consensus_distributed_vote_scoring_strategy(&self) -> bool {
1244 self.feature_flags
1245 .consensus_distributed_vote_scoring_strategy
1246 }
1247
1248 pub fn gc_depth(&self) -> u32 {
1249 if cfg!(msim) {
1250 min(5, self.consensus_gc_depth.unwrap_or(0))
1252 } else {
1253 self.consensus_gc_depth.unwrap_or(0)
1254 }
1255 }
1256
1257 pub fn consensus_linearize_subdag_v2(&self) -> bool {
1258 let res = self.feature_flags.consensus_linearize_subdag_v2;
1259 assert!(
1260 !res || self.gc_depth() > 0,
1261 "The consensus linearize sub dag V2 requires GC to be enabled"
1262 );
1263 res
1264 }
1265
1266 pub fn variant_nodes(&self) -> bool {
1267 self.feature_flags.variant_nodes
1268 }
1269
1270 pub fn consensus_smart_ancestor_selection(&self) -> bool {
1271 self.feature_flags.consensus_smart_ancestor_selection
1272 }
1273
1274 pub fn consensus_round_prober_probe_accepted_rounds(&self) -> bool {
1275 self.feature_flags
1276 .consensus_round_prober_probe_accepted_rounds
1277 }
1278
1279 pub fn consensus_zstd_compression(&self) -> bool {
1280 self.feature_flags.consensus_zstd_compression
1281 }
1282
1283 pub fn congestion_control_min_free_execution_slot(&self) -> bool {
1284 self.feature_flags
1285 .congestion_control_min_free_execution_slot
1286 }
1287
1288 pub fn accept_passkey_in_multisig(&self) -> bool {
1289 self.feature_flags.accept_passkey_in_multisig
1290 }
1291
1292 pub fn consensus_batched_block_sync(&self) -> bool {
1293 self.feature_flags.consensus_batched_block_sync
1294 }
1295
1296 pub fn congestion_control_gas_price_feedback_mechanism(&self) -> bool {
1299 self.feature_flags
1300 .congestion_control_gas_price_feedback_mechanism
1301 }
1302
1303 pub fn validate_identifier_inputs(&self) -> bool {
1304 self.feature_flags.validate_identifier_inputs
1305 }
1306
1307 pub fn minimize_child_object_mutations(&self) -> bool {
1308 self.feature_flags.minimize_child_object_mutations
1309 }
1310
1311 pub fn dependency_linkage_error(&self) -> bool {
1312 self.feature_flags.dependency_linkage_error
1313 }
1314
1315 pub fn additional_multisig_checks(&self) -> bool {
1316 self.feature_flags.additional_multisig_checks
1317 }
1318}
1319
1320#[cfg(not(msim))]
1321static POISON_VERSION_METHODS: AtomicBool = const { AtomicBool::new(false) };
1322
1323#[cfg(msim)]
1325thread_local! {
1326 static POISON_VERSION_METHODS: AtomicBool = const { AtomicBool::new(false) };
1327}
1328
1329impl ProtocolConfig {
1331 pub fn get_for_version(version: ProtocolVersion, chain: Chain) -> Self {
1334 assert!(
1336 version >= ProtocolVersion::MIN,
1337 "Network protocol version is {:?}, but the minimum supported version by the binary is {:?}. Please upgrade the binary.",
1338 version,
1339 ProtocolVersion::MIN.0,
1340 );
1341 assert!(
1342 version <= ProtocolVersion::MAX_ALLOWED,
1343 "Network protocol version is {:?}, but the maximum supported version by the binary is {:?}. Please upgrade the binary.",
1344 version,
1345 ProtocolVersion::MAX_ALLOWED.0,
1346 );
1347
1348 let mut ret = Self::get_for_version_impl(version, chain);
1349 ret.version = version;
1350
1351 ret = CONFIG_OVERRIDE.with(|ovr| {
1352 if let Some(override_fn) = &*ovr.borrow() {
1353 warn!(
1354 "overriding ProtocolConfig settings with custom settings (you should not see this log outside of tests)"
1355 );
1356 override_fn(version, ret)
1357 } else {
1358 ret
1359 }
1360 });
1361
1362 if std::env::var("IOTA_PROTOCOL_CONFIG_OVERRIDE_ENABLE").is_ok() {
1363 warn!(
1364 "overriding ProtocolConfig settings with custom settings; this may break non-local networks"
1365 );
1366 let overrides: ProtocolConfigOptional =
1367 serde_env::from_env_with_prefix("IOTA_PROTOCOL_CONFIG_OVERRIDE")
1368 .expect("failed to parse ProtocolConfig override env variables");
1369 overrides.apply_to(&mut ret);
1370 }
1371
1372 ret
1373 }
1374
1375 pub fn get_for_version_if_supported(version: ProtocolVersion, chain: Chain) -> Option<Self> {
1378 if version.0 >= ProtocolVersion::MIN.0 && version.0 <= ProtocolVersion::MAX_ALLOWED.0 {
1379 let mut ret = Self::get_for_version_impl(version, chain);
1380 ret.version = version;
1381 Some(ret)
1382 } else {
1383 None
1384 }
1385 }
1386
1387 #[cfg(not(msim))]
1388 pub fn poison_get_for_min_version() {
1389 POISON_VERSION_METHODS.store(true, Ordering::Relaxed);
1390 }
1391
1392 #[cfg(not(msim))]
1393 fn load_poison_get_for_min_version() -> bool {
1394 POISON_VERSION_METHODS.load(Ordering::Relaxed)
1395 }
1396
1397 #[cfg(msim)]
1398 pub fn poison_get_for_min_version() {
1399 POISON_VERSION_METHODS.with(|p| p.store(true, Ordering::Relaxed));
1400 }
1401
1402 #[cfg(msim)]
1403 fn load_poison_get_for_min_version() -> bool {
1404 POISON_VERSION_METHODS.with(|p| p.load(Ordering::Relaxed))
1405 }
1406
1407 pub fn convert_type_argument_error(&self) -> bool {
1408 self.feature_flags.convert_type_argument_error
1409 }
1410
1411 pub fn get_for_min_version() -> Self {
1415 if Self::load_poison_get_for_min_version() {
1416 panic!("get_for_min_version called on validator");
1417 }
1418 ProtocolConfig::get_for_version(ProtocolVersion::MIN, Chain::Unknown)
1419 }
1420
1421 #[expect(non_snake_case)]
1432 pub fn get_for_max_version_UNSAFE() -> Self {
1433 if Self::load_poison_get_for_min_version() {
1434 panic!("get_for_max_version_UNSAFE called on validator");
1435 }
1436 ProtocolConfig::get_for_version(ProtocolVersion::MAX, Chain::Unknown)
1437 }
1438
1439 fn get_for_version_impl(version: ProtocolVersion, chain: Chain) -> Self {
1440 #[cfg(msim)]
1441 {
1442 if version > ProtocolVersion::MAX {
1444 let mut config = Self::get_for_version_impl(ProtocolVersion::MAX, Chain::Unknown);
1445 config.base_tx_cost_fixed = Some(config.base_tx_cost_fixed() + 1000);
1446 return config;
1447 }
1448 }
1449
1450 let mut cfg = Self {
1454 version,
1455
1456 feature_flags: Default::default(),
1457
1458 max_tx_size_bytes: Some(128 * 1024),
1459 max_input_objects: Some(2048),
1462 max_serialized_tx_effects_size_bytes: Some(512 * 1024),
1463 max_serialized_tx_effects_size_bytes_system_tx: Some(512 * 1024 * 16),
1464 max_gas_payment_objects: Some(256),
1465 max_modules_in_publish: Some(64),
1466 max_package_dependencies: Some(32),
1467 max_arguments: Some(512),
1468 max_type_arguments: Some(16),
1469 max_type_argument_depth: Some(16),
1470 max_pure_argument_size: Some(16 * 1024),
1471 max_programmable_tx_commands: Some(1024),
1472 move_binary_format_version: Some(7),
1473 min_move_binary_format_version: Some(6),
1474 binary_module_handles: Some(100),
1475 binary_struct_handles: Some(300),
1476 binary_function_handles: Some(1500),
1477 binary_function_instantiations: Some(750),
1478 binary_signatures: Some(1000),
1479 binary_constant_pool: Some(4000),
1480 binary_identifiers: Some(10000),
1481 binary_address_identifiers: Some(100),
1482 binary_struct_defs: Some(200),
1483 binary_struct_def_instantiations: Some(100),
1484 binary_function_defs: Some(1000),
1485 binary_field_handles: Some(500),
1486 binary_field_instantiations: Some(250),
1487 binary_friend_decls: Some(100),
1488 binary_enum_defs: None,
1489 binary_enum_def_instantiations: None,
1490 binary_variant_handles: None,
1491 binary_variant_instantiation_handles: None,
1492 max_move_object_size: Some(250 * 1024),
1493 max_move_package_size: Some(100 * 1024),
1494 max_publish_or_upgrade_per_ptb: Some(5),
1495 max_tx_gas: Some(50_000_000_000),
1497 max_gas_price: Some(100_000),
1498 max_gas_computation_bucket: Some(5_000_000),
1499 max_loop_depth: Some(5),
1500 max_generic_instantiation_length: Some(32),
1501 max_function_parameters: Some(128),
1502 max_basic_blocks: Some(1024),
1503 max_value_stack_size: Some(1024),
1504 max_type_nodes: Some(256),
1505 max_push_size: Some(10000),
1506 max_struct_definitions: Some(200),
1507 max_function_definitions: Some(1000),
1508 max_fields_in_struct: Some(32),
1509 max_dependency_depth: Some(100),
1510 max_num_event_emit: Some(1024),
1511 max_num_new_move_object_ids: Some(2048),
1512 max_num_new_move_object_ids_system_tx: Some(2048 * 16),
1513 max_num_deleted_move_object_ids: Some(2048),
1514 max_num_deleted_move_object_ids_system_tx: Some(2048 * 16),
1515 max_num_transferred_move_object_ids: Some(2048),
1516 max_num_transferred_move_object_ids_system_tx: Some(2048 * 16),
1517 max_event_emit_size: Some(250 * 1024),
1518 max_move_vector_len: Some(256 * 1024),
1519 max_type_to_layout_nodes: None,
1520 max_ptb_value_size: None,
1521
1522 max_back_edges_per_function: Some(10_000),
1523 max_back_edges_per_module: Some(10_000),
1524
1525 max_verifier_meter_ticks_per_function: Some(16_000_000),
1526
1527 max_meter_ticks_per_module: Some(16_000_000),
1528 max_meter_ticks_per_package: Some(16_000_000),
1529
1530 object_runtime_max_num_cached_objects: Some(1000),
1531 object_runtime_max_num_cached_objects_system_tx: Some(1000 * 16),
1532 object_runtime_max_num_store_entries: Some(1000),
1533 object_runtime_max_num_store_entries_system_tx: Some(1000 * 16),
1534 base_tx_cost_fixed: Some(1_000),
1536 package_publish_cost_fixed: Some(1_000),
1537 base_tx_cost_per_byte: Some(0),
1538 package_publish_cost_per_byte: Some(80),
1539 obj_access_cost_read_per_byte: Some(15),
1540 obj_access_cost_mutate_per_byte: Some(40),
1541 obj_access_cost_delete_per_byte: Some(40),
1542 obj_access_cost_verify_per_byte: Some(200),
1543 obj_data_cost_refundable: Some(100),
1544 obj_metadata_cost_non_refundable: Some(50),
1545 gas_model_version: Some(1),
1546 storage_rebate_rate: Some(10000),
1547 reward_slashing_rate: Some(10000),
1549 storage_gas_price: Some(76),
1550 base_gas_price: None,
1551 validator_target_reward: Some(767_000 * 1_000_000_000),
1554 max_transactions_per_checkpoint: Some(10_000),
1555 max_checkpoint_size_bytes: Some(30 * 1024 * 1024),
1556
1557 buffer_stake_for_protocol_upgrade_bps: Some(5000),
1559
1560 address_from_bytes_cost_base: Some(52),
1564 address_to_u256_cost_base: Some(52),
1566 address_from_u256_cost_base: Some(52),
1568
1569 config_read_setting_impl_cost_base: Some(100),
1572 config_read_setting_impl_cost_per_byte: Some(40),
1573
1574 dynamic_field_hash_type_and_key_cost_base: Some(100),
1578 dynamic_field_hash_type_and_key_type_cost_per_byte: Some(2),
1579 dynamic_field_hash_type_and_key_value_cost_per_byte: Some(2),
1580 dynamic_field_hash_type_and_key_type_tag_cost_per_byte: Some(2),
1581 dynamic_field_add_child_object_cost_base: Some(100),
1584 dynamic_field_add_child_object_type_cost_per_byte: Some(10),
1585 dynamic_field_add_child_object_value_cost_per_byte: Some(10),
1586 dynamic_field_add_child_object_struct_tag_cost_per_byte: Some(10),
1587 dynamic_field_borrow_child_object_cost_base: Some(100),
1590 dynamic_field_borrow_child_object_child_ref_cost_per_byte: Some(10),
1591 dynamic_field_borrow_child_object_type_cost_per_byte: Some(10),
1592 dynamic_field_remove_child_object_cost_base: Some(100),
1595 dynamic_field_remove_child_object_child_cost_per_byte: Some(2),
1596 dynamic_field_remove_child_object_type_cost_per_byte: Some(2),
1597 dynamic_field_has_child_object_cost_base: Some(100),
1600 dynamic_field_has_child_object_with_ty_cost_base: Some(100),
1603 dynamic_field_has_child_object_with_ty_type_cost_per_byte: Some(2),
1604 dynamic_field_has_child_object_with_ty_type_tag_cost_per_byte: Some(2),
1605
1606 event_emit_cost_base: Some(52),
1609 event_emit_value_size_derivation_cost_per_byte: Some(2),
1610 event_emit_tag_size_derivation_cost_per_byte: Some(5),
1611 event_emit_output_cost_per_byte: Some(10),
1612
1613 object_borrow_uid_cost_base: Some(52),
1616 object_delete_impl_cost_base: Some(52),
1618 object_record_new_uid_cost_base: Some(52),
1620
1621 transfer_transfer_internal_cost_base: Some(52),
1625 transfer_freeze_object_cost_base: Some(52),
1627 transfer_share_object_cost_base: Some(52),
1629 transfer_receive_object_cost_base: Some(52),
1630
1631 tx_context_derive_id_cost_base: Some(52),
1635
1636 types_is_one_time_witness_cost_base: Some(52),
1639 types_is_one_time_witness_type_tag_cost_per_byte: Some(2),
1640 types_is_one_time_witness_type_cost_per_byte: Some(2),
1641
1642 validator_validate_metadata_cost_base: Some(52),
1646 validator_validate_metadata_data_cost_per_byte: Some(2),
1647
1648 crypto_invalid_arguments_cost: Some(100),
1650 bls12381_bls12381_min_sig_verify_cost_base: Some(52),
1652 bls12381_bls12381_min_sig_verify_msg_cost_per_byte: Some(2),
1653 bls12381_bls12381_min_sig_verify_msg_cost_per_block: Some(2),
1654
1655 bls12381_bls12381_min_pk_verify_cost_base: Some(52),
1657 bls12381_bls12381_min_pk_verify_msg_cost_per_byte: Some(2),
1658 bls12381_bls12381_min_pk_verify_msg_cost_per_block: Some(2),
1659
1660 ecdsa_k1_ecrecover_keccak256_cost_base: Some(52),
1662 ecdsa_k1_ecrecover_keccak256_msg_cost_per_byte: Some(2),
1663 ecdsa_k1_ecrecover_keccak256_msg_cost_per_block: Some(2),
1664 ecdsa_k1_ecrecover_sha256_cost_base: Some(52),
1665 ecdsa_k1_ecrecover_sha256_msg_cost_per_byte: Some(2),
1666 ecdsa_k1_ecrecover_sha256_msg_cost_per_block: Some(2),
1667
1668 ecdsa_k1_decompress_pubkey_cost_base: Some(52),
1670
1671 ecdsa_k1_secp256k1_verify_keccak256_cost_base: Some(52),
1673 ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_byte: Some(2),
1674 ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_block: Some(2),
1675 ecdsa_k1_secp256k1_verify_sha256_cost_base: Some(52),
1676 ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_byte: Some(2),
1677 ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_block: Some(2),
1678
1679 ecdsa_r1_ecrecover_keccak256_cost_base: Some(52),
1681 ecdsa_r1_ecrecover_keccak256_msg_cost_per_byte: Some(2),
1682 ecdsa_r1_ecrecover_keccak256_msg_cost_per_block: Some(2),
1683 ecdsa_r1_ecrecover_sha256_cost_base: Some(52),
1684 ecdsa_r1_ecrecover_sha256_msg_cost_per_byte: Some(2),
1685 ecdsa_r1_ecrecover_sha256_msg_cost_per_block: Some(2),
1686
1687 ecdsa_r1_secp256r1_verify_keccak256_cost_base: Some(52),
1689 ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_byte: Some(2),
1690 ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_block: Some(2),
1691 ecdsa_r1_secp256r1_verify_sha256_cost_base: Some(52),
1692 ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_byte: Some(2),
1693 ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_block: Some(2),
1694
1695 ecvrf_ecvrf_verify_cost_base: Some(52),
1697 ecvrf_ecvrf_verify_alpha_string_cost_per_byte: Some(2),
1698 ecvrf_ecvrf_verify_alpha_string_cost_per_block: Some(2),
1699
1700 ed25519_ed25519_verify_cost_base: Some(52),
1702 ed25519_ed25519_verify_msg_cost_per_byte: Some(2),
1703 ed25519_ed25519_verify_msg_cost_per_block: Some(2),
1704
1705 groth16_prepare_verifying_key_bls12381_cost_base: Some(52),
1707 groth16_prepare_verifying_key_bn254_cost_base: Some(52),
1708
1709 groth16_verify_groth16_proof_internal_bls12381_cost_base: Some(52),
1711 groth16_verify_groth16_proof_internal_bls12381_cost_per_public_input: Some(2),
1712 groth16_verify_groth16_proof_internal_bn254_cost_base: Some(52),
1713 groth16_verify_groth16_proof_internal_bn254_cost_per_public_input: Some(2),
1714 groth16_verify_groth16_proof_internal_public_input_cost_per_byte: Some(2),
1715
1716 hash_blake2b256_cost_base: Some(52),
1718 hash_blake2b256_data_cost_per_byte: Some(2),
1719 hash_blake2b256_data_cost_per_block: Some(2),
1720 hash_keccak256_cost_base: Some(52),
1722 hash_keccak256_data_cost_per_byte: Some(2),
1723 hash_keccak256_data_cost_per_block: Some(2),
1724
1725 poseidon_bn254_cost_base: None,
1726 poseidon_bn254_cost_per_block: None,
1727
1728 hmac_hmac_sha3_256_cost_base: Some(52),
1730 hmac_hmac_sha3_256_input_cost_per_byte: Some(2),
1731 hmac_hmac_sha3_256_input_cost_per_block: Some(2),
1732
1733 group_ops_bls12381_decode_scalar_cost: Some(52),
1735 group_ops_bls12381_decode_g1_cost: Some(52),
1736 group_ops_bls12381_decode_g2_cost: Some(52),
1737 group_ops_bls12381_decode_gt_cost: Some(52),
1738 group_ops_bls12381_scalar_add_cost: Some(52),
1739 group_ops_bls12381_g1_add_cost: Some(52),
1740 group_ops_bls12381_g2_add_cost: Some(52),
1741 group_ops_bls12381_gt_add_cost: Some(52),
1742 group_ops_bls12381_scalar_sub_cost: Some(52),
1743 group_ops_bls12381_g1_sub_cost: Some(52),
1744 group_ops_bls12381_g2_sub_cost: Some(52),
1745 group_ops_bls12381_gt_sub_cost: Some(52),
1746 group_ops_bls12381_scalar_mul_cost: Some(52),
1747 group_ops_bls12381_g1_mul_cost: Some(52),
1748 group_ops_bls12381_g2_mul_cost: Some(52),
1749 group_ops_bls12381_gt_mul_cost: Some(52),
1750 group_ops_bls12381_scalar_div_cost: Some(52),
1751 group_ops_bls12381_g1_div_cost: Some(52),
1752 group_ops_bls12381_g2_div_cost: Some(52),
1753 group_ops_bls12381_gt_div_cost: Some(52),
1754 group_ops_bls12381_g1_hash_to_base_cost: Some(52),
1755 group_ops_bls12381_g2_hash_to_base_cost: Some(52),
1756 group_ops_bls12381_g1_hash_to_cost_per_byte: Some(2),
1757 group_ops_bls12381_g2_hash_to_cost_per_byte: Some(2),
1758 group_ops_bls12381_g1_msm_base_cost: Some(52),
1759 group_ops_bls12381_g2_msm_base_cost: Some(52),
1760 group_ops_bls12381_g1_msm_base_cost_per_input: Some(52),
1761 group_ops_bls12381_g2_msm_base_cost_per_input: Some(52),
1762 group_ops_bls12381_msm_max_len: Some(32),
1763 group_ops_bls12381_pairing_cost: Some(52),
1764 group_ops_bls12381_g1_to_uncompressed_g1_cost: None,
1765 group_ops_bls12381_uncompressed_g1_to_g1_cost: None,
1766 group_ops_bls12381_uncompressed_g1_sum_base_cost: None,
1767 group_ops_bls12381_uncompressed_g1_sum_cost_per_term: None,
1768 group_ops_bls12381_uncompressed_g1_sum_max_terms: None,
1769
1770 check_zklogin_id_cost_base: Some(200),
1772 check_zklogin_issuer_cost_base: Some(200),
1774
1775 vdf_verify_vdf_cost: None,
1776 vdf_hash_to_input_cost: None,
1777
1778 bcs_per_byte_serialized_cost: Some(2),
1779 bcs_legacy_min_output_size_cost: Some(1),
1780 bcs_failure_cost: Some(52),
1781 hash_sha2_256_base_cost: Some(52),
1782 hash_sha2_256_per_byte_cost: Some(2),
1783 hash_sha2_256_legacy_min_input_len_cost: Some(1),
1784 hash_sha3_256_base_cost: Some(52),
1785 hash_sha3_256_per_byte_cost: Some(2),
1786 hash_sha3_256_legacy_min_input_len_cost: Some(1),
1787 type_name_get_base_cost: Some(52),
1788 type_name_get_per_byte_cost: Some(2),
1789 string_check_utf8_base_cost: Some(52),
1790 string_check_utf8_per_byte_cost: Some(2),
1791 string_is_char_boundary_base_cost: Some(52),
1792 string_sub_string_base_cost: Some(52),
1793 string_sub_string_per_byte_cost: Some(2),
1794 string_index_of_base_cost: Some(52),
1795 string_index_of_per_byte_pattern_cost: Some(2),
1796 string_index_of_per_byte_searched_cost: Some(2),
1797 vector_empty_base_cost: Some(52),
1798 vector_length_base_cost: Some(52),
1799 vector_push_back_base_cost: Some(52),
1800 vector_push_back_legacy_per_abstract_memory_unit_cost: Some(2),
1801 vector_borrow_base_cost: Some(52),
1802 vector_pop_back_base_cost: Some(52),
1803 vector_destroy_empty_base_cost: Some(52),
1804 vector_swap_base_cost: Some(52),
1805 debug_print_base_cost: Some(52),
1806 debug_print_stack_trace_base_cost: Some(52),
1807
1808 max_size_written_objects: Some(5 * 1000 * 1000),
1809 max_size_written_objects_system_tx: Some(50 * 1000 * 1000),
1812
1813 max_move_identifier_len: Some(128),
1815 max_move_value_depth: Some(128),
1816 max_move_enum_variants: None,
1817
1818 gas_rounding_step: Some(1_000),
1819
1820 execution_version: Some(1),
1821
1822 max_event_emit_size_total: Some(
1825 256 * 250 * 1024, ),
1827
1828 consensus_bad_nodes_stake_threshold: Some(20),
1835
1836 max_jwk_votes_per_validator_per_epoch: Some(240),
1838
1839 max_age_of_jwk_in_epochs: Some(1),
1840
1841 consensus_max_transaction_size_bytes: Some(256 * 1024), consensus_max_transactions_in_block_bytes: Some(512 * 1024),
1845
1846 random_beacon_reduction_allowed_delta: Some(800),
1847
1848 random_beacon_reduction_lower_bound: Some(1000),
1849 random_beacon_dkg_timeout_round: Some(3000),
1850 random_beacon_min_round_interval_ms: Some(500),
1851
1852 random_beacon_dkg_version: Some(1),
1853
1854 consensus_max_num_transactions_in_block: Some(512),
1858
1859 max_deferral_rounds_for_congestion_control: Some(10),
1860
1861 min_checkpoint_interval_ms: Some(200),
1862
1863 checkpoint_summary_version_specific_data: Some(1),
1864
1865 max_soft_bundle_size: Some(5),
1866
1867 bridge_should_try_to_finalize_committee: None,
1868
1869 max_accumulated_txn_cost_per_object_in_mysticeti_commit: Some(10),
1870
1871 max_committee_members_count: None,
1872
1873 consensus_gc_depth: None,
1874 };
1877
1878 cfg.feature_flags.consensus_transaction_ordering = ConsensusTransactionOrdering::ByGasPrice;
1879
1880 {
1882 cfg.feature_flags
1883 .disable_invariant_violation_check_in_swap_loc = true;
1884 cfg.feature_flags.no_extraneous_module_bytes = true;
1885 cfg.feature_flags.hardened_otw_check = true;
1886 cfg.feature_flags.rethrow_serialization_type_layout_errors = true;
1887 }
1888
1889 {
1891 cfg.feature_flags.zklogin_max_epoch_upper_bound_delta = Some(30);
1892 }
1893
1894 cfg.feature_flags.consensus_choice = ConsensusChoice::Mysticeti;
1896 cfg.feature_flags.consensus_network = ConsensusNetwork::Tonic;
1898
1899 cfg.feature_flags.per_object_congestion_control_mode =
1900 PerObjectCongestionControlMode::TotalTxCount;
1901
1902 cfg.bridge_should_try_to_finalize_committee = Some(chain != Chain::Mainnet);
1904
1905 if chain != Chain::Mainnet && chain != Chain::Testnet {
1907 cfg.feature_flags.enable_poseidon = true;
1908 cfg.poseidon_bn254_cost_base = Some(260);
1909 cfg.poseidon_bn254_cost_per_block = Some(10);
1910
1911 cfg.feature_flags.enable_group_ops_native_function_msm = true;
1912
1913 cfg.feature_flags.enable_vdf = true;
1914 cfg.vdf_verify_vdf_cost = Some(1500);
1917 cfg.vdf_hash_to_input_cost = Some(100);
1918
1919 cfg.feature_flags.passkey_auth = true;
1920 }
1921
1922 for cur in 2..=version.0 {
1923 match cur {
1924 1 => unreachable!(),
1925 2 => {}
1927 3 => {
1928 cfg.feature_flags.relocate_event_module = true;
1929 }
1930 4 => {
1931 cfg.max_type_to_layout_nodes = Some(512);
1932 }
1933 5 => {
1934 cfg.feature_flags.protocol_defined_base_fee = true;
1935 cfg.base_gas_price = Some(1000);
1936
1937 cfg.feature_flags.disallow_new_modules_in_deps_only_packages = true;
1938 cfg.feature_flags.convert_type_argument_error = true;
1939 cfg.feature_flags.native_charging_v2 = true;
1940
1941 if chain != Chain::Mainnet && chain != Chain::Testnet {
1942 cfg.feature_flags.uncompressed_g1_group_elements = true;
1943 }
1944
1945 cfg.gas_model_version = Some(2);
1946
1947 cfg.poseidon_bn254_cost_per_block = Some(388);
1948
1949 cfg.bls12381_bls12381_min_sig_verify_cost_base = Some(44064);
1950 cfg.bls12381_bls12381_min_pk_verify_cost_base = Some(49282);
1951 cfg.ecdsa_k1_secp256k1_verify_keccak256_cost_base = Some(1470);
1952 cfg.ecdsa_k1_secp256k1_verify_sha256_cost_base = Some(1470);
1953 cfg.ecdsa_r1_secp256r1_verify_sha256_cost_base = Some(4225);
1954 cfg.ecdsa_r1_secp256r1_verify_keccak256_cost_base = Some(4225);
1955 cfg.ecvrf_ecvrf_verify_cost_base = Some(4848);
1956 cfg.ed25519_ed25519_verify_cost_base = Some(1802);
1957
1958 cfg.ecdsa_r1_ecrecover_keccak256_cost_base = Some(1173);
1960 cfg.ecdsa_r1_ecrecover_sha256_cost_base = Some(1173);
1961 cfg.ecdsa_k1_ecrecover_keccak256_cost_base = Some(500);
1962 cfg.ecdsa_k1_ecrecover_sha256_cost_base = Some(500);
1963
1964 cfg.groth16_prepare_verifying_key_bls12381_cost_base = Some(53838);
1965 cfg.groth16_prepare_verifying_key_bn254_cost_base = Some(82010);
1966 cfg.groth16_verify_groth16_proof_internal_bls12381_cost_base = Some(72090);
1967 cfg.groth16_verify_groth16_proof_internal_bls12381_cost_per_public_input =
1968 Some(8213);
1969 cfg.groth16_verify_groth16_proof_internal_bn254_cost_base = Some(115502);
1970 cfg.groth16_verify_groth16_proof_internal_bn254_cost_per_public_input =
1971 Some(9484);
1972
1973 cfg.hash_keccak256_cost_base = Some(10);
1974 cfg.hash_blake2b256_cost_base = Some(10);
1975
1976 cfg.group_ops_bls12381_decode_scalar_cost = Some(7);
1978 cfg.group_ops_bls12381_decode_g1_cost = Some(2848);
1979 cfg.group_ops_bls12381_decode_g2_cost = Some(3770);
1980 cfg.group_ops_bls12381_decode_gt_cost = Some(3068);
1981
1982 cfg.group_ops_bls12381_scalar_add_cost = Some(10);
1983 cfg.group_ops_bls12381_g1_add_cost = Some(1556);
1984 cfg.group_ops_bls12381_g2_add_cost = Some(3048);
1985 cfg.group_ops_bls12381_gt_add_cost = Some(188);
1986
1987 cfg.group_ops_bls12381_scalar_sub_cost = Some(10);
1988 cfg.group_ops_bls12381_g1_sub_cost = Some(1550);
1989 cfg.group_ops_bls12381_g2_sub_cost = Some(3019);
1990 cfg.group_ops_bls12381_gt_sub_cost = Some(497);
1991
1992 cfg.group_ops_bls12381_scalar_mul_cost = Some(11);
1993 cfg.group_ops_bls12381_g1_mul_cost = Some(4842);
1994 cfg.group_ops_bls12381_g2_mul_cost = Some(9108);
1995 cfg.group_ops_bls12381_gt_mul_cost = Some(27490);
1996
1997 cfg.group_ops_bls12381_scalar_div_cost = Some(91);
1998 cfg.group_ops_bls12381_g1_div_cost = Some(5091);
1999 cfg.group_ops_bls12381_g2_div_cost = Some(9206);
2000 cfg.group_ops_bls12381_gt_div_cost = Some(27804);
2001
2002 cfg.group_ops_bls12381_g1_hash_to_base_cost = Some(2962);
2003 cfg.group_ops_bls12381_g2_hash_to_base_cost = Some(8688);
2004
2005 cfg.group_ops_bls12381_g1_msm_base_cost = Some(62648);
2006 cfg.group_ops_bls12381_g2_msm_base_cost = Some(131192);
2007 cfg.group_ops_bls12381_g1_msm_base_cost_per_input = Some(1333);
2008 cfg.group_ops_bls12381_g2_msm_base_cost_per_input = Some(3216);
2009
2010 cfg.group_ops_bls12381_uncompressed_g1_to_g1_cost = Some(677);
2011 cfg.group_ops_bls12381_g1_to_uncompressed_g1_cost = Some(2099);
2012 cfg.group_ops_bls12381_uncompressed_g1_sum_base_cost = Some(77);
2013 cfg.group_ops_bls12381_uncompressed_g1_sum_cost_per_term = Some(26);
2014 cfg.group_ops_bls12381_uncompressed_g1_sum_max_terms = Some(1200);
2015
2016 cfg.group_ops_bls12381_pairing_cost = Some(26897);
2017
2018 cfg.validator_validate_metadata_cost_base = Some(20000);
2019
2020 cfg.max_committee_members_count = Some(50);
2021 }
2022 6 => {
2023 cfg.max_ptb_value_size = Some(1024 * 1024);
2024 }
2025 7 => {
2026 }
2029 8 => {
2030 cfg.feature_flags.variant_nodes = true;
2031
2032 if chain != Chain::Mainnet {
2033 cfg.feature_flags.consensus_round_prober = true;
2035 cfg.feature_flags
2037 .consensus_distributed_vote_scoring_strategy = true;
2038 cfg.feature_flags.consensus_linearize_subdag_v2 = true;
2039 cfg.feature_flags.consensus_smart_ancestor_selection = true;
2041 cfg.feature_flags
2043 .consensus_round_prober_probe_accepted_rounds = true;
2044 cfg.feature_flags.consensus_zstd_compression = true;
2046 cfg.consensus_gc_depth = Some(60);
2050 }
2051
2052 if chain != Chain::Testnet && chain != Chain::Mainnet {
2055 cfg.feature_flags.congestion_control_min_free_execution_slot = true;
2056 }
2057 }
2058 9 => {
2059 if chain != Chain::Mainnet {
2060 cfg.feature_flags.consensus_smart_ancestor_selection = false;
2062 }
2063
2064 cfg.feature_flags.consensus_zstd_compression = true;
2066
2067 if chain != Chain::Testnet && chain != Chain::Mainnet {
2069 cfg.feature_flags.accept_passkey_in_multisig = true;
2070 }
2071
2072 cfg.bridge_should_try_to_finalize_committee = None;
2074 }
2075 10 => {
2076 cfg.feature_flags.congestion_control_min_free_execution_slot = true;
2079
2080 cfg.max_committee_members_count = Some(80);
2082
2083 cfg.feature_flags.consensus_round_prober = true;
2085 cfg.feature_flags
2087 .consensus_round_prober_probe_accepted_rounds = true;
2088 cfg.feature_flags
2090 .consensus_distributed_vote_scoring_strategy = true;
2091 cfg.feature_flags.consensus_linearize_subdag_v2 = true;
2093
2094 cfg.consensus_gc_depth = Some(60);
2099
2100 cfg.feature_flags.minimize_child_object_mutations = true;
2102
2103 if chain != Chain::Mainnet {
2104 cfg.feature_flags.consensus_batched_block_sync = true;
2106 }
2107
2108 if chain != Chain::Testnet && chain != Chain::Mainnet {
2109 cfg.feature_flags
2112 .congestion_control_gas_price_feedback_mechanism = true;
2113 }
2114
2115 cfg.feature_flags.validate_identifier_inputs = true;
2116 cfg.feature_flags.dependency_linkage_error = true;
2117 cfg.feature_flags.additional_multisig_checks = true;
2118 }
2119 11 => {
2120 }
2123 _ => panic!("unsupported version {version:?}"),
2134 }
2135 }
2136 cfg
2137 }
2138
2139 pub fn verifier_config(&self, signing_limits: Option<(usize, usize)>) -> VerifierConfig {
2143 let (max_back_edges_per_function, max_back_edges_per_module) = if let Some((
2144 max_back_edges_per_function,
2145 max_back_edges_per_module,
2146 )) = signing_limits
2147 {
2148 (
2149 Some(max_back_edges_per_function),
2150 Some(max_back_edges_per_module),
2151 )
2152 } else {
2153 (None, None)
2154 };
2155
2156 VerifierConfig {
2157 max_loop_depth: Some(self.max_loop_depth() as usize),
2158 max_generic_instantiation_length: Some(self.max_generic_instantiation_length() as usize),
2159 max_function_parameters: Some(self.max_function_parameters() as usize),
2160 max_basic_blocks: Some(self.max_basic_blocks() as usize),
2161 max_value_stack_size: self.max_value_stack_size() as usize,
2162 max_type_nodes: Some(self.max_type_nodes() as usize),
2163 max_push_size: Some(self.max_push_size() as usize),
2164 max_dependency_depth: Some(self.max_dependency_depth() as usize),
2165 max_fields_in_struct: Some(self.max_fields_in_struct() as usize),
2166 max_function_definitions: Some(self.max_function_definitions() as usize),
2167 max_data_definitions: Some(self.max_struct_definitions() as usize),
2168 max_constant_vector_len: Some(self.max_move_vector_len()),
2169 max_back_edges_per_function,
2170 max_back_edges_per_module,
2171 max_basic_blocks_in_script: None,
2172 max_identifier_len: self.max_move_identifier_len_as_option(), bytecode_version: self.move_binary_format_version(),
2176 max_variants_in_enum: self.max_move_enum_variants_as_option(),
2177 }
2178 }
2179
2180 pub fn apply_overrides_for_testing(
2185 override_fn: impl Fn(ProtocolVersion, Self) -> Self + Send + Sync + 'static,
2186 ) -> OverrideGuard {
2187 CONFIG_OVERRIDE.with(|ovr| {
2188 let mut cur = ovr.borrow_mut();
2189 assert!(cur.is_none(), "config override already present");
2190 *cur = Some(Box::new(override_fn));
2191 OverrideGuard
2192 })
2193 }
2194}
2195
2196impl ProtocolConfig {
2201 pub fn set_zklogin_auth_for_testing(&mut self, val: bool) {
2202 self.feature_flags.zklogin_auth = val
2203 }
2204 pub fn set_enable_jwk_consensus_updates_for_testing(&mut self, val: bool) {
2205 self.feature_flags.enable_jwk_consensus_updates = val
2206 }
2207
2208 pub fn set_accept_zklogin_in_multisig_for_testing(&mut self, val: bool) {
2209 self.feature_flags.accept_zklogin_in_multisig = val
2210 }
2211
2212 pub fn set_per_object_congestion_control_mode_for_testing(
2213 &mut self,
2214 val: PerObjectCongestionControlMode,
2215 ) {
2216 self.feature_flags.per_object_congestion_control_mode = val;
2217 }
2218
2219 pub fn set_consensus_choice_for_testing(&mut self, val: ConsensusChoice) {
2220 self.feature_flags.consensus_choice = val;
2221 }
2222
2223 pub fn set_consensus_network_for_testing(&mut self, val: ConsensusNetwork) {
2224 self.feature_flags.consensus_network = val;
2225 }
2226
2227 pub fn set_zklogin_max_epoch_upper_bound_delta_for_testing(&mut self, val: Option<u64>) {
2228 self.feature_flags.zklogin_max_epoch_upper_bound_delta = val
2229 }
2230
2231 pub fn set_passkey_auth_for_testing(&mut self, val: bool) {
2232 self.feature_flags.passkey_auth = val
2233 }
2234
2235 pub fn set_disallow_new_modules_in_deps_only_packages_for_testing(&mut self, val: bool) {
2236 self.feature_flags
2237 .disallow_new_modules_in_deps_only_packages = val;
2238 }
2239
2240 pub fn set_consensus_round_prober_for_testing(&mut self, val: bool) {
2241 self.feature_flags.consensus_round_prober = val;
2242 }
2243
2244 pub fn set_consensus_distributed_vote_scoring_strategy_for_testing(&mut self, val: bool) {
2245 self.feature_flags
2246 .consensus_distributed_vote_scoring_strategy = val;
2247 }
2248
2249 pub fn set_gc_depth_for_testing(&mut self, val: u32) {
2250 self.consensus_gc_depth = Some(val);
2251 }
2252
2253 pub fn set_consensus_linearize_subdag_v2_for_testing(&mut self, val: bool) {
2254 self.feature_flags.consensus_linearize_subdag_v2 = val;
2255 }
2256
2257 pub fn set_consensus_round_prober_probe_accepted_rounds(&mut self, val: bool) {
2258 self.feature_flags
2259 .consensus_round_prober_probe_accepted_rounds = val;
2260 }
2261
2262 pub fn set_accept_passkey_in_multisig_for_testing(&mut self, val: bool) {
2263 self.feature_flags.accept_passkey_in_multisig = val;
2264 }
2265
2266 pub fn set_consensus_smart_ancestor_selection_for_testing(&mut self, val: bool) {
2267 self.feature_flags.consensus_smart_ancestor_selection = val;
2268 }
2269
2270 pub fn set_consensus_batched_block_sync_for_testing(&mut self, val: bool) {
2271 self.feature_flags.consensus_batched_block_sync = val;
2272 }
2273
2274 pub fn set_congestion_control_min_free_execution_slot_for_testing(&mut self, val: bool) {
2275 self.feature_flags
2276 .congestion_control_min_free_execution_slot = val;
2277 }
2278
2279 pub fn set_congestion_control_gas_price_feedback_mechanism_for_testing(&mut self, val: bool) {
2280 self.feature_flags
2281 .congestion_control_gas_price_feedback_mechanism = val;
2282 }
2283}
2284
2285type OverrideFn = dyn Fn(ProtocolVersion, ProtocolConfig) -> ProtocolConfig + Send + Sync;
2286
2287thread_local! {
2288 static CONFIG_OVERRIDE: RefCell<Option<Box<OverrideFn>>> = const { RefCell::new(None) };
2289}
2290
2291#[must_use]
2292pub struct OverrideGuard;
2293
2294impl Drop for OverrideGuard {
2295 fn drop(&mut self) {
2296 info!("restoring override fn");
2297 CONFIG_OVERRIDE.with(|ovr| {
2298 *ovr.borrow_mut() = None;
2299 });
2300 }
2301}
2302
2303#[derive(PartialEq, Eq)]
2307pub enum LimitThresholdCrossed {
2308 None,
2309 Soft(u128, u128),
2310 Hard(u128, u128),
2311}
2312
2313pub fn check_limit_in_range<T: Into<V>, U: Into<V>, V: PartialOrd + Into<u128>>(
2316 x: T,
2317 soft_limit: U,
2318 hard_limit: V,
2319) -> LimitThresholdCrossed {
2320 let x: V = x.into();
2321 let soft_limit: V = soft_limit.into();
2322
2323 debug_assert!(soft_limit <= hard_limit);
2324
2325 if x >= hard_limit {
2328 LimitThresholdCrossed::Hard(x.into(), hard_limit.into())
2329 } else if x < soft_limit {
2330 LimitThresholdCrossed::None
2331 } else {
2332 LimitThresholdCrossed::Soft(x.into(), soft_limit.into())
2333 }
2334}
2335
2336#[macro_export]
2337macro_rules! check_limit {
2338 ($x:expr, $hard:expr) => {
2339 check_limit!($x, $hard, $hard)
2340 };
2341 ($x:expr, $soft:expr, $hard:expr) => {
2342 check_limit_in_range($x as u64, $soft, $hard)
2343 };
2344}
2345
2346#[macro_export]
2350macro_rules! check_limit_by_meter {
2351 ($is_metered:expr, $x:expr, $metered_limit:expr, $unmetered_hard_limit:expr, $metric:expr) => {{
2352 let (h, metered_str) = if $is_metered {
2354 ($metered_limit, "metered")
2355 } else {
2356 ($unmetered_hard_limit, "unmetered")
2358 };
2359 use iota_protocol_config::check_limit_in_range;
2360 let result = check_limit_in_range($x as u64, $metered_limit, h);
2361 match result {
2362 LimitThresholdCrossed::None => {}
2363 LimitThresholdCrossed::Soft(_, _) => {
2364 $metric.with_label_values(&[metered_str, "soft"]).inc();
2365 }
2366 LimitThresholdCrossed::Hard(_, _) => {
2367 $metric.with_label_values(&[metered_str, "hard"]).inc();
2368 }
2369 };
2370 result
2371 }};
2372}
2373
2374#[cfg(all(test, not(msim)))]
2375mod test {
2376 use insta::assert_yaml_snapshot;
2377
2378 use super::*;
2379
2380 #[test]
2381 fn snapshot_tests() {
2382 println!("\n============================================================================");
2383 println!("! !");
2384 println!("! IMPORTANT: never update snapshots from this test. only add new versions! !");
2385 println!("! !");
2386 println!("============================================================================\n");
2387 for chain_id in &[Chain::Unknown, Chain::Mainnet, Chain::Testnet] {
2388 let chain_str = match chain_id {
2393 Chain::Unknown => "".to_string(),
2394 _ => format!("{chain_id:?}_"),
2395 };
2396 for i in MIN_PROTOCOL_VERSION..=MAX_PROTOCOL_VERSION {
2397 let cur = ProtocolVersion::new(i);
2398 assert_yaml_snapshot!(
2399 format!("{}version_{}", chain_str, cur.as_u64()),
2400 ProtocolConfig::get_for_version(cur, *chain_id)
2401 );
2402 }
2403 }
2404 }
2405
2406 #[test]
2407 fn test_getters() {
2408 let prot: ProtocolConfig =
2409 ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
2410 assert_eq!(
2411 prot.max_arguments(),
2412 prot.max_arguments_as_option().unwrap()
2413 );
2414 }
2415
2416 #[test]
2417 fn test_setters() {
2418 let mut prot: ProtocolConfig =
2419 ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
2420 prot.set_max_arguments_for_testing(123);
2421 assert_eq!(prot.max_arguments(), 123);
2422
2423 prot.set_max_arguments_from_str_for_testing("321".to_string());
2424 assert_eq!(prot.max_arguments(), 321);
2425
2426 prot.disable_max_arguments_for_testing();
2427 assert_eq!(prot.max_arguments_as_option(), None);
2428
2429 prot.set_attr_for_testing("max_arguments".to_string(), "456".to_string());
2430 assert_eq!(prot.max_arguments(), 456);
2431 }
2432
2433 #[test]
2434 #[should_panic(expected = "unsupported version")]
2435 fn max_version_test() {
2436 let _ = ProtocolConfig::get_for_version_impl(
2439 ProtocolVersion::new(MAX_PROTOCOL_VERSION + 1),
2440 Chain::Unknown,
2441 );
2442 }
2443
2444 #[test]
2445 fn lookup_by_string_test() {
2446 let prot: ProtocolConfig =
2447 ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Mainnet);
2448 assert!(prot.lookup_attr("some random string".to_string()).is_none());
2450
2451 assert!(
2452 prot.lookup_attr("max_arguments".to_string())
2453 == Some(ProtocolConfigValue::u32(prot.max_arguments())),
2454 );
2455
2456 assert!(
2458 prot.lookup_attr("poseidon_bn254_cost_base".to_string())
2459 .is_none()
2460 );
2461 assert!(
2462 prot.attr_map()
2463 .get("poseidon_bn254_cost_base")
2464 .unwrap()
2465 .is_none()
2466 );
2467
2468 let prot: ProtocolConfig =
2470 ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
2471
2472 assert!(
2473 prot.lookup_attr("poseidon_bn254_cost_base".to_string())
2474 == Some(ProtocolConfigValue::u64(prot.poseidon_bn254_cost_base()))
2475 );
2476 assert!(
2477 prot.attr_map().get("poseidon_bn254_cost_base").unwrap()
2478 == &Some(ProtocolConfigValue::u64(prot.poseidon_bn254_cost_base()))
2479 );
2480
2481 let prot: ProtocolConfig =
2483 ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Mainnet);
2484 assert!(
2486 prot.feature_flags
2487 .lookup_attr("some random string".to_owned())
2488 .is_none()
2489 );
2490 assert!(
2491 !prot
2492 .feature_flags
2493 .attr_map()
2494 .contains_key("some random string")
2495 );
2496
2497 assert!(prot.feature_flags.lookup_attr("enable_poseidon".to_owned()) == Some(false));
2499 assert!(
2500 prot.feature_flags
2501 .attr_map()
2502 .get("enable_poseidon")
2503 .unwrap()
2504 == &false
2505 );
2506 let prot: ProtocolConfig =
2507 ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
2508 assert!(prot.feature_flags.lookup_attr("enable_poseidon".to_owned()) == Some(true));
2510 assert!(
2511 prot.feature_flags
2512 .attr_map()
2513 .get("enable_poseidon")
2514 .unwrap()
2515 == &true
2516 );
2517 }
2518
2519 #[test]
2520 fn limit_range_fn_test() {
2521 let low = 100u32;
2522 let high = 10000u64;
2523
2524 assert!(check_limit!(1u8, low, high) == LimitThresholdCrossed::None);
2525 assert!(matches!(
2526 check_limit!(255u16, low, high),
2527 LimitThresholdCrossed::Soft(255u128, 100)
2528 ));
2529 assert!(matches!(
2536 check_limit!(2550000u64, low, high),
2537 LimitThresholdCrossed::Hard(2550000, 10000)
2538 ));
2539
2540 assert!(matches!(
2541 check_limit!(2550000u64, high, high),
2542 LimitThresholdCrossed::Hard(2550000, 10000)
2543 ));
2544
2545 assert!(matches!(
2546 check_limit!(1u8, high),
2547 LimitThresholdCrossed::None
2548 ));
2549
2550 assert!(check_limit!(255u16, high) == LimitThresholdCrossed::None);
2551
2552 assert!(matches!(
2553 check_limit!(2550000u64, high),
2554 LimitThresholdCrossed::Hard(2550000, 10000)
2555 ));
2556 }
2557}