1use fuel_types::{
2 Address,
3 AssetId,
4 Bytes32,
5 ChainId,
6 bytes::WORD_SIZE,
7};
8
9pub mod gas;
10
11pub use gas::{
12 DependentCost,
13 GasCostNotDefined,
14 GasCosts,
15 GasCostsValues,
16};
17
18use crate::consts::BALANCE_ENTRY_SIZE;
19
20#[cfg(feature = "test-helpers")]
21const MAX_GAS: u64 = 100_000_000;
22#[cfg(feature = "test-helpers")]
23const MAX_SIZE: u64 = 110 * 1024;
24
25#[derive(Debug, derive_more::Display)]
26#[display("setting block transaction size limit is not supported")]
27pub struct SettingBlockTransactionSizeLimitNotSupported;
28#[cfg(feature = "std")]
29impl std::error::Error for SettingBlockTransactionSizeLimitNotSupported {}
30
31#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
33pub enum ConsensusParameters {
34 V1(ConsensusParametersV1),
36 V2(ConsensusParametersV2),
37}
38
39#[cfg(feature = "test-helpers")]
40impl Default for ConsensusParameters {
41 fn default() -> Self {
42 Self::standard()
43 }
44}
45
46impl ConsensusParameters {
47 #[cfg(feature = "test-helpers")]
48 pub fn standard() -> Self {
50 ConsensusParametersV2::standard().into()
51 }
52
53 #[cfg(feature = "test-helpers")]
54 pub fn standard_with_id(chain_id: ChainId) -> Self {
56 ConsensusParametersV2::standard_with_id(chain_id).into()
57 }
58
59 pub const fn new(
61 tx_params: TxParameters,
62 predicate_params: PredicateParameters,
63 script_params: ScriptParameters,
64 contract_params: ContractParameters,
65 fee_params: FeeParameters,
66 chain_id: ChainId,
67 gas_costs: GasCosts,
68 base_asset_id: AssetId,
69 block_gas_limit: u64,
70 block_transaction_size_limit: u64,
71 privileged_address: Address,
72 ) -> Self {
73 Self::V2(ConsensusParametersV2 {
74 tx_params,
75 predicate_params,
76 script_params,
77 contract_params,
78 fee_params,
79 chain_id,
80 gas_costs,
81 base_asset_id,
82 block_gas_limit,
83 block_transaction_size_limit,
84 privileged_address,
85 })
86 }
87
88 pub const fn tx_params(&self) -> &TxParameters {
90 match self {
91 Self::V1(params) => ¶ms.tx_params,
92 Self::V2(params) => ¶ms.tx_params,
93 }
94 }
95
96 pub const fn predicate_params(&self) -> &PredicateParameters {
98 match self {
99 Self::V1(params) => ¶ms.predicate_params,
100 Self::V2(params) => ¶ms.predicate_params,
101 }
102 }
103
104 pub const fn script_params(&self) -> &ScriptParameters {
106 match self {
107 Self::V1(params) => ¶ms.script_params,
108 Self::V2(params) => ¶ms.script_params,
109 }
110 }
111
112 pub const fn contract_params(&self) -> &ContractParameters {
114 match self {
115 Self::V1(params) => ¶ms.contract_params,
116 Self::V2(params) => ¶ms.contract_params,
117 }
118 }
119
120 pub const fn fee_params(&self) -> &FeeParameters {
122 match self {
123 Self::V1(params) => ¶ms.fee_params,
124 Self::V2(params) => ¶ms.fee_params,
125 }
126 }
127
128 pub const fn chain_id(&self) -> ChainId {
130 match self {
131 Self::V1(params) => params.chain_id,
132 Self::V2(params) => params.chain_id,
133 }
134 }
135
136 pub const fn gas_costs(&self) -> &GasCosts {
138 match self {
139 Self::V1(params) => ¶ms.gas_costs,
140 Self::V2(params) => ¶ms.gas_costs,
141 }
142 }
143
144 pub const fn base_asset_id(&self) -> &AssetId {
146 match self {
147 Self::V1(params) => ¶ms.base_asset_id,
148 Self::V2(params) => ¶ms.base_asset_id,
149 }
150 }
151
152 pub const fn block_gas_limit(&self) -> u64 {
154 match self {
155 Self::V1(params) => params.block_gas_limit,
156 Self::V2(params) => params.block_gas_limit,
157 }
158 }
159
160 pub fn block_transaction_size_limit(&self) -> u64 {
162 match self {
163 Self::V1(_) => {
164 u64::MAX
167 }
168 Self::V2(params) => params.block_transaction_size_limit,
169 }
170 }
171
172 pub const fn privileged_address(&self) -> &Address {
174 match self {
175 Self::V1(params) => ¶ms.privileged_address,
176 Self::V2(params) => ¶ms.privileged_address,
177 }
178 }
179}
180
181impl ConsensusParameters {
182 pub fn set_tx_params(&mut self, tx_params: TxParameters) {
184 match self {
185 Self::V1(params) => params.tx_params = tx_params,
186 Self::V2(params) => params.tx_params = tx_params,
187 }
188 }
189
190 pub fn set_predicate_params(&mut self, predicate_params: PredicateParameters) {
192 match self {
193 Self::V1(params) => params.predicate_params = predicate_params,
194 Self::V2(params) => params.predicate_params = predicate_params,
195 }
196 }
197
198 pub fn set_script_params(&mut self, script_params: ScriptParameters) {
200 match self {
201 Self::V1(params) => params.script_params = script_params,
202 Self::V2(params) => params.script_params = script_params,
203 }
204 }
205
206 pub fn set_contract_params(&mut self, contract_params: ContractParameters) {
208 match self {
209 Self::V1(params) => params.contract_params = contract_params,
210 Self::V2(params) => params.contract_params = contract_params,
211 }
212 }
213
214 pub fn set_fee_params(&mut self, fee_params: FeeParameters) {
216 match self {
217 Self::V1(params) => params.fee_params = fee_params,
218 Self::V2(params) => params.fee_params = fee_params,
219 }
220 }
221
222 pub fn set_chain_id(&mut self, chain_id: ChainId) {
224 match self {
225 Self::V1(params) => params.chain_id = chain_id,
226 Self::V2(params) => params.chain_id = chain_id,
227 }
228 }
229
230 pub fn set_gas_costs(&mut self, gas_costs: GasCosts) {
232 match self {
233 Self::V1(params) => params.gas_costs = gas_costs,
234 Self::V2(params) => params.gas_costs = gas_costs,
235 }
236 }
237
238 pub fn set_base_asset_id(&mut self, base_asset_id: AssetId) {
240 match self {
241 Self::V1(params) => params.base_asset_id = base_asset_id,
242 Self::V2(params) => params.base_asset_id = base_asset_id,
243 }
244 }
245
246 pub fn set_block_gas_limit(&mut self, block_gas_limit: u64) {
248 match self {
249 Self::V1(params) => params.block_gas_limit = block_gas_limit,
250 Self::V2(params) => params.block_gas_limit = block_gas_limit,
251 }
252 }
253
254 pub fn set_block_transaction_size_limit(
256 &mut self,
257 block_transaction_size_limit: u64,
258 ) -> Result<(), SettingBlockTransactionSizeLimitNotSupported> {
259 match self {
260 Self::V1(_) => Err(SettingBlockTransactionSizeLimitNotSupported),
261 Self::V2(params) => {
262 params.block_transaction_size_limit = block_transaction_size_limit;
263 Ok(())
264 }
265 }
266 }
267
268 pub fn set_privileged_address(&mut self, privileged_address: Address) {
270 match self {
271 Self::V1(params) => params.privileged_address = privileged_address,
272 Self::V2(params) => params.privileged_address = privileged_address,
273 }
274 }
275}
276
277#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
279pub struct ConsensusParametersV1 {
280 pub tx_params: TxParameters,
281 pub predicate_params: PredicateParameters,
282 pub script_params: ScriptParameters,
283 pub contract_params: ContractParameters,
284 pub fee_params: FeeParameters,
285 pub chain_id: ChainId,
286 pub gas_costs: GasCosts,
287 pub base_asset_id: AssetId,
288 pub block_gas_limit: u64,
289 pub privileged_address: Address,
292}
293
294#[cfg(feature = "test-helpers")]
295impl ConsensusParametersV1 {
296 pub fn standard() -> Self {
298 Self::standard_with_id(ChainId::default())
299 }
300
301 pub fn standard_with_id(chain_id: ChainId) -> Self {
303 Self {
304 tx_params: TxParameters::DEFAULT,
305 predicate_params: PredicateParameters::DEFAULT,
306 script_params: ScriptParameters::DEFAULT,
307 contract_params: ContractParameters::DEFAULT,
308 fee_params: FeeParameters::DEFAULT,
309 chain_id,
310 gas_costs: GasCosts::default(),
311 base_asset_id: Default::default(),
312 block_gas_limit: TxParameters::DEFAULT.max_gas_per_tx(),
313 privileged_address: Default::default(),
314 }
315 }
316}
317
318#[cfg(feature = "test-helpers")]
319impl Default for ConsensusParametersV1 {
320 fn default() -> Self {
321 Self::standard()
322 }
323}
324
325impl From<ConsensusParametersV1> for ConsensusParameters {
326 fn from(params: ConsensusParametersV1) -> Self {
327 Self::V1(params)
328 }
329}
330
331#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
335pub struct ConsensusParametersV2 {
336 pub tx_params: TxParameters,
337 pub predicate_params: PredicateParameters,
338 pub script_params: ScriptParameters,
339 pub contract_params: ContractParameters,
340 pub fee_params: FeeParameters,
341 pub chain_id: ChainId,
342 pub gas_costs: GasCosts,
343 pub base_asset_id: AssetId,
344 pub block_gas_limit: u64,
345 pub block_transaction_size_limit: u64,
346 pub privileged_address: Address,
349}
350
351#[cfg(feature = "test-helpers")]
352impl ConsensusParametersV2 {
353 const DEFAULT_BLOCK_TRANSACTION_SIZE_LIMIT: u64 = 126 * 1024;
354
355 pub fn standard() -> Self {
357 Self::standard_with_id(ChainId::default())
358 }
359
360 pub fn standard_with_id(chain_id: ChainId) -> Self {
362 Self {
363 tx_params: TxParameters::DEFAULT,
364 predicate_params: PredicateParameters::DEFAULT,
365 script_params: ScriptParameters::DEFAULT,
366 contract_params: ContractParameters::DEFAULT,
367 fee_params: FeeParameters::DEFAULT,
368 chain_id,
369 gas_costs: GasCosts::default(),
370 base_asset_id: Default::default(),
371 block_gas_limit: TxParameters::DEFAULT.max_gas_per_tx(),
372 block_transaction_size_limit: Self::DEFAULT_BLOCK_TRANSACTION_SIZE_LIMIT,
373 privileged_address: Default::default(),
374 }
375 }
376}
377
378#[cfg(feature = "test-helpers")]
379impl Default for ConsensusParametersV2 {
380 fn default() -> Self {
381 Self::standard()
382 }
383}
384
385impl From<ConsensusParametersV2> for ConsensusParameters {
386 fn from(params: ConsensusParametersV2) -> Self {
387 Self::V2(params)
388 }
389}
390
391#[derive(
393 Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
394)]
395pub enum FeeParameters {
396 V1(FeeParametersV1),
397}
398
399impl FeeParameters {
400 #[cfg(feature = "test-helpers")]
401 pub const DEFAULT: Self = Self::V1(FeeParametersV1::DEFAULT);
403
404 pub const fn with_gas_price_factor(self, gas_price_factor: u64) -> Self {
406 match self {
407 Self::V1(mut params) => {
408 params.gas_price_factor = gas_price_factor;
409 Self::V1(params)
410 }
411 }
412 }
413
414 pub const fn with_gas_per_byte(self, gas_per_byte: u64) -> Self {
415 match self {
416 Self::V1(mut params) => {
417 params.gas_per_byte = gas_per_byte;
418 Self::V1(params)
419 }
420 }
421 }
422}
423
424impl FeeParameters {
425 pub const fn gas_price_factor(&self) -> u64 {
427 match self {
428 Self::V1(params) => params.gas_price_factor,
429 }
430 }
431
432 pub const fn gas_per_byte(&self) -> u64 {
434 match self {
435 Self::V1(params) => params.gas_per_byte,
436 }
437 }
438}
439
440#[cfg(feature = "test-helpers")]
441impl Default for FeeParameters {
442 fn default() -> Self {
443 Self::DEFAULT
444 }
445}
446
447impl From<FeeParametersV1> for FeeParameters {
448 fn from(params: FeeParametersV1) -> Self {
449 Self::V1(params)
450 }
451}
452
453#[derive(
455 Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
456)]
457pub struct FeeParametersV1 {
458 pub gas_price_factor: u64,
460 pub gas_per_byte: u64,
462}
463
464#[cfg(feature = "test-helpers")]
465impl FeeParametersV1 {
466 pub const DEFAULT: Self = FeeParametersV1 {
468 gas_price_factor: 1_000_000_000,
469 gas_per_byte: 4,
470 };
471}
472
473#[cfg(feature = "test-helpers")]
474impl Default for FeeParametersV1 {
475 fn default() -> Self {
476 Self::DEFAULT
477 }
478}
479
480#[derive(
482 Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
483)]
484pub enum PredicateParameters {
485 V1(PredicateParametersV1),
486}
487
488impl PredicateParameters {
489 #[cfg(feature = "test-helpers")]
490 pub const DEFAULT: Self = Self::V1(PredicateParametersV1::DEFAULT);
492
493 pub const fn with_max_predicate_length(self, max_predicate_length: u64) -> Self {
495 match self {
496 Self::V1(mut params) => {
497 params.max_predicate_length = max_predicate_length;
498 Self::V1(params)
499 }
500 }
501 }
502
503 pub const fn with_max_predicate_data_length(
505 self,
506 max_predicate_data_length: u64,
507 ) -> Self {
508 match self {
509 Self::V1(mut params) => {
510 params.max_predicate_data_length = max_predicate_data_length;
511 Self::V1(params)
512 }
513 }
514 }
515
516 pub const fn with_max_message_data_length(
518 self,
519 max_message_data_length: u64,
520 ) -> Self {
521 match self {
522 Self::V1(mut params) => {
523 params.max_message_data_length = max_message_data_length;
524 Self::V1(params)
525 }
526 }
527 }
528
529 pub const fn with_max_gas_per_predicate(self, max_gas_per_predicate: u64) -> Self {
531 match self {
532 Self::V1(mut params) => {
533 params.max_gas_per_predicate = max_gas_per_predicate;
534 Self::V1(params)
535 }
536 }
537 }
538}
539
540impl PredicateParameters {
541 pub const fn max_predicate_length(&self) -> u64 {
543 match self {
544 Self::V1(params) => params.max_predicate_length,
545 }
546 }
547
548 pub const fn max_predicate_data_length(&self) -> u64 {
550 match self {
551 Self::V1(params) => params.max_predicate_data_length,
552 }
553 }
554
555 pub const fn max_message_data_length(&self) -> u64 {
557 match self {
558 Self::V1(params) => params.max_message_data_length,
559 }
560 }
561
562 pub const fn max_gas_per_predicate(&self) -> u64 {
564 match self {
565 Self::V1(params) => params.max_gas_per_predicate,
566 }
567 }
568}
569
570impl From<PredicateParametersV1> for PredicateParameters {
571 fn from(params: PredicateParametersV1) -> Self {
572 Self::V1(params)
573 }
574}
575
576#[cfg(feature = "test-helpers")]
577impl Default for PredicateParameters {
578 fn default() -> Self {
579 Self::DEFAULT
580 }
581}
582
583#[derive(
585 Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
586)]
587pub struct PredicateParametersV1 {
588 pub max_predicate_length: u64,
590 pub max_predicate_data_length: u64,
592 pub max_message_data_length: u64,
594 pub max_gas_per_predicate: u64,
596}
597
598#[cfg(feature = "test-helpers")]
599impl PredicateParametersV1 {
600 pub const DEFAULT: Self = Self {
602 max_predicate_length: 1024 * 1024,
603 max_predicate_data_length: 1024 * 1024,
604 max_message_data_length: 1024 * 1024,
605 max_gas_per_predicate: MAX_GAS,
606 };
607}
608
609#[cfg(feature = "test-helpers")]
610impl Default for PredicateParametersV1 {
611 fn default() -> Self {
612 Self::DEFAULT
613 }
614}
615
616#[derive(
618 Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
619)]
620pub enum TxParameters {
621 V1(TxParametersV1),
623}
624
625impl TxParameters {
626 #[cfg(feature = "test-helpers")]
627 pub const DEFAULT: Self = Self::V1(TxParametersV1::DEFAULT);
629
630 pub const fn tx_offset(&self) -> usize {
632 let Some(balances_size) =
633 (self.max_inputs() as usize).checked_mul(BALANCE_ENTRY_SIZE)
634 else {
635 panic!(
636 "Consensus parameters shouldn't allow max_inputs to cause overflow here"
637 );
638 };
639
640 balances_size.saturating_add(
641 Bytes32::LEN + WORD_SIZE + AssetId::LEN, )
645 }
646
647 pub const fn with_max_inputs(self, max_inputs: u16) -> Self {
649 match self {
650 Self::V1(mut params) => {
651 params.max_inputs = max_inputs;
652 Self::V1(params)
653 }
654 }
655 }
656
657 pub const fn with_max_outputs(self, max_outputs: u16) -> Self {
659 match self {
660 Self::V1(mut params) => {
661 params.max_outputs = max_outputs;
662 Self::V1(params)
663 }
664 }
665 }
666
667 pub const fn with_max_witnesses(self, max_witnesses: u32) -> Self {
669 match self {
670 Self::V1(mut params) => {
671 params.max_witnesses = max_witnesses;
672 Self::V1(params)
673 }
674 }
675 }
676
677 pub const fn with_max_gas_per_tx(self, max_gas_per_tx: u64) -> Self {
679 match self {
680 Self::V1(mut params) => {
681 params.max_gas_per_tx = max_gas_per_tx;
682 Self::V1(params)
683 }
684 }
685 }
686
687 pub const fn with_max_size(self, max_size: u64) -> Self {
689 match self {
690 Self::V1(mut params) => {
691 params.max_size = max_size;
692 Self::V1(params)
693 }
694 }
695 }
696
697 pub const fn with_max_bytecode_subsections(
699 self,
700 max_bytecode_subsections: u16,
701 ) -> Self {
702 match self {
703 Self::V1(mut params) => {
704 params.max_bytecode_subsections = max_bytecode_subsections;
705 Self::V1(params)
706 }
707 }
708 }
709}
710
711impl TxParameters {
712 pub const fn max_inputs(&self) -> u16 {
714 match self {
715 Self::V1(params) => params.max_inputs,
716 }
717 }
718
719 pub const fn max_outputs(&self) -> u16 {
721 match self {
722 Self::V1(params) => params.max_outputs,
723 }
724 }
725
726 pub const fn max_witnesses(&self) -> u32 {
728 match self {
729 Self::V1(params) => params.max_witnesses,
730 }
731 }
732
733 pub const fn max_gas_per_tx(&self) -> u64 {
735 match self {
736 Self::V1(params) => params.max_gas_per_tx,
737 }
738 }
739
740 pub const fn max_size(&self) -> u64 {
742 match self {
743 Self::V1(params) => params.max_size,
744 }
745 }
746
747 pub const fn max_bytecode_subsections(&self) -> u16 {
749 match self {
750 Self::V1(params) => params.max_bytecode_subsections,
751 }
752 }
753}
754
755#[cfg(feature = "test-helpers")]
756impl Default for TxParameters {
757 fn default() -> Self {
758 Self::DEFAULT
759 }
760}
761
762#[cfg(feature = "test-helpers")]
763impl TxParameters {
764 pub fn set_max_size(&mut self, max_size: u64) {
765 match self {
766 Self::V1(params) => params.max_size = max_size,
767 }
768 }
769}
770
771impl From<TxParametersV1> for TxParameters {
772 fn from(params: TxParametersV1) -> Self {
773 Self::V1(params)
774 }
775}
776
777#[derive(
778 Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
779)]
780pub struct TxParametersV1 {
781 pub max_inputs: u16,
783 pub max_outputs: u16,
785 pub max_witnesses: u32,
787 pub max_gas_per_tx: u64,
789 pub max_size: u64,
791 pub max_bytecode_subsections: u16,
793}
794
795#[cfg(feature = "test-helpers")]
796impl TxParametersV1 {
797 pub const DEFAULT: Self = Self {
799 max_inputs: 255,
800 max_outputs: 255,
801 max_witnesses: 255,
802 max_gas_per_tx: MAX_GAS,
803 max_size: MAX_SIZE,
804 max_bytecode_subsections: 255,
805 };
806}
807
808#[cfg(feature = "test-helpers")]
809impl Default for TxParametersV1 {
810 fn default() -> Self {
811 Self::DEFAULT
812 }
813}
814
815#[derive(
817 Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
818)]
819pub enum ScriptParameters {
820 V1(ScriptParametersV1),
821 V2(ScriptParametersV2),
822}
823
824impl ScriptParameters {
825 #[cfg(feature = "test-helpers")]
826 pub const DEFAULT: Self = Self::V2(ScriptParametersV2::DEFAULT);
828
829 #[cfg(feature = "test-helpers")]
831 pub const fn with_max_script_length(self, max_script_length: u64) -> Self {
832 match self {
833 Self::V1(mut params) => {
834 params.max_script_length = max_script_length;
835 Self::V1(params)
836 }
837 Self::V2(mut params) => {
838 params.max_script_length = max_script_length;
839 Self::V2(params)
840 }
841 }
842 }
843
844 #[cfg(feature = "test-helpers")]
846 pub const fn with_max_script_data_length(self, max_script_data_length: u64) -> Self {
847 match self {
848 Self::V1(mut params) => {
849 params.max_script_data_length = max_script_data_length;
850 Self::V1(params)
851 }
852 Self::V2(mut params) => {
853 params.max_script_data_length = max_script_data_length;
854 Self::V2(params)
855 }
856 }
857 }
858
859 #[cfg(feature = "test-helpers")]
861 pub const fn with_max_storage_slot_length(
862 self,
863 max_storage_slot_length: u64,
864 ) -> Self {
865 match self {
866 Self::V1(_) => {
867 panic!("ScriptParametersV1 does not support max_storage_slot_length");
868 }
869 Self::V2(mut params) => {
870 params.max_storage_slot_length = max_storage_slot_length;
871 Self::V2(params)
872 }
873 }
874 }
875
876 pub const fn max_script_length(&self) -> u64 {
878 match self {
879 Self::V1(params) => params.max_script_length,
880 Self::V2(params) => params.max_script_length,
881 }
882 }
883
884 pub const fn max_script_data_length(&self) -> u64 {
886 match self {
887 Self::V1(params) => params.max_script_data_length,
888 Self::V2(params) => params.max_script_data_length,
889 }
890 }
891
892 pub const fn max_storage_slot_length(&self) -> u64 {
894 match self {
895 Self::V1(_) => 32, Self::V2(params) => params.max_storage_slot_length,
897 }
898 }
899}
900
901#[cfg(feature = "test-helpers")]
902impl Default for ScriptParameters {
903 fn default() -> Self {
904 Self::DEFAULT
905 }
906}
907
908#[derive(
909 Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
910)]
911pub struct ScriptParametersV1 {
912 pub max_script_length: u64,
914 pub max_script_data_length: u64,
916}
917
918impl From<ScriptParametersV1> for ScriptParameters {
919 fn from(params: ScriptParametersV1) -> Self {
920 Self::V1(params)
921 }
922}
923
924#[cfg(feature = "test-helpers")]
925impl ScriptParametersV1 {
926 pub const DEFAULT: Self = Self {
928 max_script_length: 1024 * 1024,
929 max_script_data_length: 1024 * 1024,
930 };
931}
932
933#[cfg(feature = "test-helpers")]
934impl Default for ScriptParametersV1 {
935 fn default() -> Self {
936 Self::DEFAULT
937 }
938}
939
940#[derive(
941 Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
942)]
943pub struct ScriptParametersV2 {
944 pub max_script_length: u64,
946 pub max_script_data_length: u64,
948 pub max_storage_slot_length: u64,
952}
953
954impl From<ScriptParametersV2> for ScriptParameters {
955 fn from(params: ScriptParametersV2) -> Self {
956 Self::V2(params)
957 }
958}
959
960#[cfg(feature = "test-helpers")]
961impl ScriptParametersV2 {
962 pub const DEFAULT: Self = Self {
964 max_script_length: 1024 * 1024,
965 max_script_data_length: 1024 * 1024,
966 max_storage_slot_length: 1024 * 1024,
967 };
968}
969
970#[cfg(feature = "test-helpers")]
971impl Default for ScriptParametersV2 {
972 fn default() -> Self {
973 Self::DEFAULT
974 }
975}
976
977#[derive(
979 Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
980)]
981pub enum ContractParameters {
982 V1(ContractParametersV1),
983}
984
985impl ContractParameters {
986 #[cfg(feature = "test-helpers")]
987 pub const DEFAULT: Self = Self::V1(ContractParametersV1::DEFAULT);
989
990 pub const fn with_contract_max_size(self, contract_max_size: u64) -> Self {
992 match self {
993 Self::V1(mut params) => {
994 params.contract_max_size = contract_max_size;
995 Self::V1(params)
996 }
997 }
998 }
999
1000 pub const fn with_max_storage_slots(self, max_storage_slots: u64) -> Self {
1002 match self {
1003 Self::V1(mut params) => {
1004 params.max_storage_slots = max_storage_slots;
1005 Self::V1(params)
1006 }
1007 }
1008 }
1009}
1010
1011impl ContractParameters {
1012 pub const fn contract_max_size(&self) -> u64 {
1014 match self {
1015 Self::V1(params) => params.contract_max_size,
1016 }
1017 }
1018
1019 pub const fn max_storage_slots(&self) -> u64 {
1021 match self {
1022 Self::V1(params) => params.max_storage_slots,
1023 }
1024 }
1025}
1026
1027impl From<ContractParametersV1> for ContractParameters {
1028 fn from(params: ContractParametersV1) -> Self {
1029 Self::V1(params)
1030 }
1031}
1032
1033#[cfg(feature = "test-helpers")]
1034impl Default for ContractParameters {
1035 fn default() -> Self {
1036 Self::DEFAULT
1037 }
1038}
1039
1040#[derive(
1041 Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
1042)]
1043pub struct ContractParametersV1 {
1044 pub contract_max_size: u64,
1046
1047 pub max_storage_slots: u64,
1049}
1050
1051#[cfg(feature = "test-helpers")]
1052impl ContractParametersV1 {
1053 pub const DEFAULT: Self = Self {
1055 contract_max_size: 100 * 1024,
1056 max_storage_slots: 255,
1057 };
1058}
1059
1060#[cfg(feature = "test-helpers")]
1061impl Default for ContractParametersV1 {
1062 fn default() -> Self {
1063 Self::DEFAULT
1064 }
1065}
1066
1067#[cfg(feature = "typescript")]
1068pub mod typescript {
1069 use wasm_bindgen::prelude::*;
1070
1071 use super::{
1072 PredicateParameters as PredicateParametersRust,
1073 PredicateParametersV1,
1074 };
1075
1076 #[derive(Clone, Debug, PartialEq, Eq, Hash)]
1077 #[cfg_attr(feature = "typescript", wasm_bindgen::prelude::wasm_bindgen)]
1078 pub struct PredicateParameters(alloc::boxed::Box<PredicateParametersRust>);
1079
1080 impl AsRef<PredicateParametersRust> for PredicateParameters {
1081 fn as_ref(&self) -> &PredicateParametersRust {
1082 &self.0
1083 }
1084 }
1085
1086 #[wasm_bindgen]
1087 impl PredicateParameters {
1088 #[wasm_bindgen(constructor)]
1089 pub fn typescript_new(
1090 max_predicate_length: u64,
1091 max_predicate_data_length: u64,
1092 max_message_data_length: u64,
1093 max_gas_per_predicate: u64,
1094 ) -> Self {
1095 let params: PredicateParametersRust = PredicateParametersV1 {
1096 max_predicate_length,
1097 max_predicate_data_length,
1098 max_message_data_length,
1099 max_gas_per_predicate,
1100 }
1101 .into();
1102
1103 PredicateParameters(params.into())
1104 }
1105 }
1106}
1107
1108#[cfg(test)]
1109mod tests {
1110 use crate::consensus_parameters::{
1111 ConsensusParametersV2,
1112 SettingBlockTransactionSizeLimitNotSupported,
1113 };
1114
1115 use super::{
1116 ConsensusParameters,
1117 ConsensusParametersV1,
1118 };
1119
1120 #[test]
1121 fn error_when_setting_block_size_limit_in_consensus_parameters_v1() {
1122 let mut consensus_params: ConsensusParameters =
1123 ConsensusParametersV1::default().into();
1124
1125 let result = consensus_params.set_block_transaction_size_limit(0);
1126
1127 assert!(matches!(
1128 result,
1129 Err(SettingBlockTransactionSizeLimitNotSupported)
1130 ))
1131 }
1132
1133 #[test]
1134 fn ok_when_setting_block_size_limit_in_consensus_parameters_v2() {
1135 let mut consensus_params: ConsensusParameters =
1136 ConsensusParametersV2::default().into();
1137
1138 let result = consensus_params.set_block_transaction_size_limit(0);
1139
1140 assert!(matches!(result, Ok(())))
1141 }
1142}