everscale_types/models/config/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
//! Blockchain config and params.

use crate::cell::*;
use crate::dict::{Dict, DictKey};
use crate::error::Error;
use crate::num::Tokens;

use crate::models::currency::ExtraCurrencyCollection;
use crate::models::global_version::GlobalVersion;

pub use self::params::*;

mod params;

#[cfg(test)]
mod tests;

/// Blockchain config.
#[derive(Debug, Clone, Eq, PartialEq, Store, Load)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockchainConfig {
    /// Configuration contract address.
    pub address: HashBytes,
    /// Configuration parameters.
    pub params: BlockchainConfigParams,
}

impl BlockchainConfig {
    /// Creates a new blockchain config with only the address set.
    pub fn new_empty(address: HashBytes) -> Self {
        let mut params = BlockchainConfigParams(Dict::new());
        params
            .set_raw(ConfigParam0::ID, CellBuilder::build_from(address).unwrap())
            .unwrap();

        Self { address, params }
    }
}

impl std::ops::Deref for BlockchainConfig {
    type Target = BlockchainConfigParams;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.params
    }
}

impl std::ops::DerefMut for BlockchainConfig {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.params
    }
}

/// A non-empty dictionary with blockchain config params.
#[derive(Debug, Clone, Eq, PartialEq)]
#[repr(transparent)]
pub struct BlockchainConfigParams(Dict<u32, Cell>);

impl BlockchainConfigParams {
    /// Returns the elector account address (in masterchain).
    ///
    /// Uses [`ConfigParam1`].
    pub fn get_elector_address(&self) -> Result<HashBytes, Error> {
        ok!(self.get::<ConfigParam1>()).ok_or(Error::CellUnderflow)
    }

    /// Updates the elector account address (in masterchain).
    ///
    /// Uses [`ConfigParam1`].
    pub fn set_elector_address(&mut self, address: &HashBytes) -> Result<bool, Error> {
        self.set_raw(ConfigParam1::ID, ok!(CellBuilder::build_from(address)))
    }

    /// Returns the minter account address (in masterchain).
    ///
    /// Uses [`ConfigParam2`] with a fallback to [`ConfigParam0`] (config).
    pub fn get_minter_address(&self) -> Result<HashBytes, Error> {
        match ok!(self.get::<ConfigParam2>()) {
            Some(address) => Ok(address),
            None => ok!(self.get::<ConfigParam0>()).ok_or(Error::CellUnderflow),
        }
    }

    /// Updates the minter account address (in masterchain).
    ///
    /// Uses [`ConfigParam2`].
    pub fn set_minter_address(&mut self, address: &HashBytes) -> Result<bool, Error> {
        self.set_raw(ConfigParam2::ID, ok!(CellBuilder::build_from(address)))
    }

    /// Returns the fee collector account address (in masterchain).
    ///
    /// Uses [`ConfigParam3`] with a fallback to [`ConfigParam1`] (elector).
    pub fn get_fee_collector_address(&self) -> Result<HashBytes, Error> {
        match ok!(self.get::<ConfigParam3>()) {
            Some(address) => Ok(address),
            None => ok!(self.get::<ConfigParam1>()).ok_or(Error::CellUnderflow),
        }
    }

    /// Updates the fee collector address (in masterchain).
    ///
    /// Uses [`ConfigParam3`].
    pub fn set_fee_collector_address(&mut self, address: &HashBytes) -> Result<bool, Error> {
        self.set_raw(ConfigParam3::ID, ok!(CellBuilder::build_from(address)))
    }

    /// Returns the lowest supported block version and required capabilities.
    ///
    /// Uses [`ConfigParam8`].
    pub fn get_global_version(&self) -> Result<GlobalVersion, Error> {
        ok!(self.get::<ConfigParam8>()).ok_or(Error::CellUnderflow)
    }

    /// Updates the global version.
    ///
    /// Uses [`ConfigParam8`].
    pub fn set_global_version(&mut self, version: &GlobalVersion) -> Result<bool, Error> {
        self.set_raw(ConfigParam8::ID, ok!(CellBuilder::build_from(version)))
    }

    /// Returns a list of params that must be present in config.
    ///
    /// Uses [`ConfigParam9`].
    pub fn get_mandatory_params(&self) -> Result<Dict<u32, ()>, Error> {
        ok!(self.get::<ConfigParam9>()).ok_or(Error::CellUnderflow)
    }

    /// Updates a list of params that must be present in config.
    ///
    /// Uses [`ConfigParam9`].
    pub fn set_mandatory_params(&mut self, params: &[u32]) -> Result<bool, Error> {
        let mut dict = Dict::new();
        for id in params {
            ok!(dict.set(*id, ()));
        }
        self.set_raw(
            ConfigParam9::ID,
            ok!(CellBuilder::build_from(NonEmptyDict(dict))),
        )
    }

    /// Returns a list of params that have a different set of update requirements.
    ///
    /// Uses [`ConfigParam10`].
    pub fn get_critical_params(&self) -> Result<Dict<u32, ()>, Error> {
        ok!(self.get::<ConfigParam10>()).ok_or(Error::CellUnderflow)
    }

    /// Updates a list of params that have a different set of update requirements.
    ///
    /// Uses [`ConfigParam10`].
    pub fn set_critical_params(&mut self, params: &[u32]) -> Result<bool, Error> {
        let mut dict = Dict::new();
        for id in params {
            ok!(dict.set(*id, ()));
        }
        self.set_raw(
            ConfigParam10::ID,
            ok!(CellBuilder::build_from(NonEmptyDict(dict))),
        )
    }

    /// Returns a dictionary with workchain descriptions.
    ///
    /// Uses [`ConfigParam12`].
    pub fn get_workchains(&self) -> Result<Dict<i32, WorkchainDescription>, Error> {
        ok!(self.get::<ConfigParam12>()).ok_or(Error::CellUnderflow)
    }

    /// Updates a list of workchain descriptions.
    ///
    /// Uses [`ConfigParam12`].
    pub fn set_workchains(
        &mut self,
        workchains: &Dict<i32, WorkchainDescription>,
    ) -> Result<bool, Error> {
        self.set_raw(ConfigParam12::ID, ok!(CellBuilder::build_from(workchains)))
    }

    /// Returns a block creation reward for the specified workchain in tokens.
    ///
    /// Uses [`ConfigParam14`].
    pub fn get_block_creation_reward(&self, masterchain: bool) -> Result<Tokens, Error> {
        let rewards = ok!(self.get_block_creation_rewards());
        Ok(if masterchain {
            rewards.masterchain_block_fee
        } else {
            rewards.basechain_block_fee
        })
    }

    /// Returns a block creation rewards in tokens.
    ///
    /// Uses [`ConfigParam14`].
    pub fn get_block_creation_rewards(&self) -> Result<BlockCreationRewards, Error> {
        ok!(self.get::<ConfigParam14>()).ok_or(Error::CellUnderflow)
    }

    /// Updates a block creation rewards in tokens.
    ///
    /// Uses [`ConfigParam14`].
    pub fn set_block_creation_rewards(
        &mut self,
        rewards: &BlockCreationRewards,
    ) -> Result<bool, Error> {
        self.set_raw(ConfigParam14::ID, ok!(CellBuilder::build_from(rewards)))
    }

    /// Returns election timings.
    ///
    /// Uses [`ConfigParam15`].
    pub fn get_election_timings(&self) -> Result<ElectionTimings, Error> {
        ok!(self.get::<ConfigParam15>()).ok_or(Error::CellUnderflow)
    }

    /// Updates election timings.
    ///
    /// Uses [`ConfigParam15`].
    pub fn set_election_timings(&mut self, timings: &ElectionTimings) -> Result<bool, Error> {
        self.set_raw(ConfigParam15::ID, ok!(CellBuilder::build_from(timings)))
    }

    /// Returns possible validator count.
    ///
    /// Uses [`ConfigParam16`].
    pub fn get_validator_count_params(&self) -> Result<ValidatorCountParams, Error> {
        ok!(self.get::<ConfigParam16>()).ok_or(Error::CellUnderflow)
    }

    /// Updates possible validator count.
    ///
    /// Uses [`ConfigParam16`].
    pub fn set_validator_count_params(
        &mut self,
        params: &ValidatorCountParams,
    ) -> Result<bool, Error> {
        self.set_raw(ConfigParam16::ID, ok!(CellBuilder::build_from(params)))
    }

    /// Returns validator stake range and factor.
    ///
    /// Uses [`ConfigParam17`].
    pub fn get_validator_stake_params(&self) -> Result<ValidatorStakeParams, Error> {
        ok!(self.get::<ConfigParam17>()).ok_or(Error::CellUnderflow)
    }

    /// Updates validator stake range and factor.
    ///
    /// Uses [`ConfigParam17`].
    pub fn set_validator_stake_params(
        &mut self,
        params: &ValidatorStakeParams,
    ) -> Result<bool, Error> {
        self.set_raw(ConfigParam17::ID, ok!(CellBuilder::build_from(params)))
    }

    /// Returns a list with a history of all storage prices.
    ///
    /// Uses [`ConfigParam18`].
    pub fn get_storage_prices(&self) -> Result<Dict<u32, StoragePrices>, Error> {
        ok!(self.get::<ConfigParam18>()).ok_or(Error::CellUnderflow)
    }

    /// Updates a list with a history of all storage prices.
    ///
    /// Uses [`ConfigParam18`].
    pub fn set_storage_prices(&mut self, prices: &[StoragePrices]) -> Result<bool, Error> {
        let mut dict = Dict::<u32, StoragePrices>::new();
        for (i, prices) in prices.iter().enumerate() {
            ok!(dict.set(i as u32, *prices));
        }
        self.set_raw(
            ConfigParam18::ID,
            ok!(CellBuilder::build_from(NonEmptyDict(dict))),
        )
    }

    /// Returns gas limits and prices.
    ///
    /// Uses [`ConfigParam20`] (for masterchain) or [`ConfigParam21`] (for other workchains).
    pub fn get_gas_prices(&self, masterchain: bool) -> Result<GasLimitsPrices, Error> {
        ok!(if masterchain {
            self.get::<ConfigParam20>()
        } else {
            self.get::<ConfigParam21>()
        })
        .ok_or(Error::CellUnderflow)
    }

    /// Updates gas limits and prices.
    ///
    /// Uses [`ConfigParam20`] (for masterchain) or [`ConfigParam21`] (for other workchains).
    pub fn set_gas_prices(
        &mut self,
        masterchain: bool,
        prices: &GasLimitsPrices,
    ) -> Result<bool, Error> {
        let id = if masterchain {
            ConfigParam20::ID
        } else {
            ConfigParam21::ID
        };
        self.set_raw(id, ok!(CellBuilder::build_from(prices)))
    }

    /// Returns block limits.
    ///
    /// Uses [`ConfigParam22`] (for masterchain) or [`ConfigParam23`] (for other workchains).
    pub fn get_block_limits(&self, masterchain: bool) -> Result<BlockLimits, Error> {
        ok!(if masterchain {
            self.get::<ConfigParam22>()
        } else {
            self.get::<ConfigParam23>()
        })
        .ok_or(Error::CellUnderflow)
    }

    /// Updates block limits.
    ///
    /// Uses [`ConfigParam22`] (for masterchain) or [`ConfigParam23`] (for other workchains).
    pub fn set_block_limits(
        &mut self,
        masterchain: bool,
        limits: &BlockLimits,
    ) -> Result<bool, Error> {
        let id = if masterchain {
            ConfigParam22::ID
        } else {
            ConfigParam23::ID
        };
        self.set_raw(id, ok!(CellBuilder::build_from(limits)))
    }

    /// Returns message forwarding prices.
    ///
    /// Uses [`ConfigParam24`] (for masterchain) or [`ConfigParam25`] (for other workchains).
    pub fn get_msg_forward_prices(&self, masterchain: bool) -> Result<MsgForwardPrices, Error> {
        ok!(if masterchain {
            self.get::<ConfigParam24>()
        } else {
            self.get::<ConfigParam25>()
        })
        .ok_or(Error::CellUnderflow)
    }

    /// Updates message forwarding prices.
    ///
    /// Uses [`ConfigParam24`] (for masterchain) or [`ConfigParam25`] (for other workchains).
    pub fn set_msg_forward_prices(
        &mut self,
        masterchain: bool,
        prices: &MsgForwardPrices,
    ) -> Result<bool, Error> {
        let id = if masterchain {
            ConfigParam24::ID
        } else {
            ConfigParam25::ID
        };
        self.set_raw(id, ok!(CellBuilder::build_from(prices)))
    }

    /// Returns a catchain config.
    ///
    /// Uses [`ConfigParam28`].
    #[cfg(not(feature = "tycho"))]
    pub fn get_catchain_config(&self) -> Result<CatchainConfig, Error> {
        ok!(self.get::<ConfigParam28>()).ok_or(Error::CellUnderflow)
    }

    /// Updates a catchain config.
    ///
    /// Uses [`ConfigParam28`].
    #[cfg(not(feature = "tycho"))]
    pub fn set_catchain_config(&mut self, config: &CatchainConfig) -> Result<bool, Error> {
        self.set_raw(ConfigParam28::ID, ok!(CellBuilder::build_from(config)))
    }

    /// Returns a collation config.
    ///
    /// Uses [`ConfigParam28`].
    #[cfg(feature = "tycho")]
    pub fn get_collation_config(&self) -> Result<CollationConfig, Error> {
        ok!(self.get::<ConfigParam28>()).ok_or(Error::CellUnderflow)
    }

    /// Updates a collation config.
    ///
    /// Uses [`ConfigParam28`].
    #[cfg(feature = "tycho")]
    pub fn set_collation_config(&mut self, config: &CollationConfig) -> Result<bool, Error> {
        self.set_raw(ConfigParam28::ID, ok!(CellBuilder::build_from(config)))
    }

    /// Returns a consensus config.
    ///
    /// Uses [`ConfigParam29`].
    pub fn get_consensus_config(&self) -> Result<ConsensusConfig, Error> {
        ok!(self.get::<ConfigParam29>()).ok_or(Error::CellUnderflow)
    }

    /// Updates a consensus config.
    ///
    /// Uses [`ConfigParam29`].
    pub fn set_consensus_config(&mut self, config: &ConsensusConfig) -> Result<bool, Error> {
        self.set_raw(ConfigParam29::ID, ok!(CellBuilder::build_from(config)))
    }

    /// Returns a list of fundamental account addresses (in masterchain).
    ///
    /// Uses [`ConfigParam31`].
    pub fn get_fundamental_addresses(&self) -> Result<Dict<HashBytes, ()>, Error> {
        ok!(self.get::<ConfigParam31>()).ok_or(Error::CellUnderflow)
    }

    /// Updates a list of fundamental account addresses (in masterchain).
    ///
    /// Uses [`ConfigParam31`].
    pub fn set_fundamental_addresses(&mut self, addresses: &[HashBytes]) -> Result<bool, Error> {
        let mut dict = Dict::<HashBytes, ()>::new();
        for address in addresses {
            ok!(dict.set(*address, ()));
        }
        self.set_raw(ConfigParam31::ID, ok!(CellBuilder::build_from(dict)))
    }

    /// Returns `true` if the config contains info about the previous validator set.
    ///
    /// Uses [`ConfigParam32`] or [`ConfigParam33`].
    pub fn contains_prev_validator_set(&self) -> Result<bool, Error> {
        Ok(ok!(self.contains::<ConfigParam32>()) || ok!(self.contains::<ConfigParam33>()))
    }

    /// Returns `true` if the config contains info about the next validator set.
    ///
    /// Uses [`ConfigParam36`] or [`ConfigParam37`].
    pub fn contains_next_validator_set(&self) -> Result<bool, Error> {
        Ok(ok!(self.contains::<ConfigParam36>()) || ok!(self.contains::<ConfigParam37>()))
    }

    /// Returns the previous validator set.
    ///
    /// Uses [`ConfigParam33`] (temp prev validators) or [`ConfigParam32`] (prev validators).
    pub fn get_previous_validator_set(&self) -> Result<Option<ValidatorSet>, Error> {
        match ok!(self.get::<ConfigParam33>()) {
            None => self.get::<ConfigParam32>(),
            set => Ok(set),
        }
    }

    /// Returns the current validator set.
    ///
    /// Uses [`ConfigParam35`] (temp validators) or [`ConfigParam34`] (current validators).
    pub fn get_current_validator_set(&self) -> Result<ValidatorSet, Error> {
        match ok!(self.get::<ConfigParam35>()) {
            Some(set) => Ok(set),
            None => ok!(self.get::<ConfigParam34>()).ok_or(Error::CellUnderflow),
        }
    }

    /// Returns the next validator set.
    ///
    /// Uses [`ConfigParam37`] (temp next validators) or [`ConfigParam36`] (next validators).
    pub fn get_next_validator_set(&self) -> Result<Option<ValidatorSet>, Error> {
        match ok!(self.get::<ConfigParam37>()) {
            None => self.get::<ConfigParam36>(),
            set => Ok(set),
        }
    }

    /// Returns `true` if the config contains a param for the specified id.
    pub fn contains<'a, T: KnownConfigParam<'a>>(&'a self) -> Result<bool, Error> {
        self.0.contains_key(T::ID)
    }

    /// Returns `true` if the config contains a param for the specified id.
    pub fn contains_raw(&self, id: u32) -> Result<bool, Error> {
        self.0.contains_key(id)
    }

    /// Tries to get a parameter from the blockchain config.
    pub fn get<'a, T: KnownConfigParam<'a>>(&'a self) -> Result<Option<T::Value>, Error> {
        let Some(mut slice) = ok!(self.get_raw(T::ID)) else {
            return Ok(None);
        };
        match <T::Wrapper as Load<'a>>::load_from(&mut slice) {
            Ok(wrapped) => Ok(Some(wrapped.into_inner())),
            Err(e) => Err(e),
        }
    }

    /// Tries to update a parameter in the blockchain config.
    pub fn set<'a, T: KnownConfigParam<'a>>(&'a mut self, value: &T::Value) -> Result<bool, Error> {
        let value = ok!(CellBuilder::build_from(T::Wrapper::wrap_inner(value)));
        self.set_raw(T::ID, value)
    }

    /// Tries to get a raw parameter from the blockchain config.
    pub fn get_raw(&self, id: u32) -> Result<Option<CellSlice<'_>>, Error> {
        match ok!(self.0.get_raw(id)) {
            Some(slice) => match slice.get_reference_as_slice(0) {
                Ok(slice) => Ok(Some(slice)),
                Err(e) => Err(e),
            },
            None => Ok(None),
        }
    }

    /// Tries to set a parameter in the blockchain config.
    ///
    /// NOTE: Use with caution, as it doesn't check the value structure.
    pub fn set_raw(&mut self, id: u32, value: Cell) -> Result<bool, Error> {
        self.0.set(id, value)
    }

    /// Removes a parameter from the blockchain config.
    ///
    /// NOTE: Removing a zero parameter successfully does nothing
    /// and returns `None` as it is required and externaly managed.
    pub fn remove(&mut self, id: u32) -> Result<Option<Cell>, Error> {
        if id == 0 {
            return Ok(None);
        }
        self.0.remove(id)
    }

    /// Returns a reference to the underlying dictionary.
    pub fn as_dict(&self) -> &Dict<u32, Cell> {
        &self.0
    }
}

impl Store for BlockchainConfigParams {
    fn store_into(&self, builder: &mut CellBuilder, _: &mut dyn CellContext) -> Result<(), Error> {
        match self.0.root() {
            Some(root) => builder.store_reference(root.clone()),
            None => Err(Error::InvalidData),
        }
    }
}

impl<'a> Load<'a> for BlockchainConfigParams {
    fn load_from(slice: &mut CellSlice<'a>) -> Result<Self, Error> {
        let root = ok!(slice.load_reference_cloned());
        Ok(Self(Dict::from(Some(root))))
    }
}

/// Marker trait which is implemented for known config params.
pub trait KnownConfigParam<'a> {
    /// Parameter index in a configuration dictionary.
    const ID: u32;

    /// Associated value type.
    type Value;

    /// Value wrapper.
    type Wrapper: ConfigParamWrapper<Self::Value> + Store + Load<'a>;
}

/// Trait to customize config param representation.
pub trait ConfigParamWrapper<T> {
    /// Converts this wrapper into an underlying type.
    fn into_inner(self) -> T;

    /// Converts an inner type into a wrapper.
    fn wrap_inner(inner: &T) -> &Self;
}

/// Identity wrapper for [`ConfigParamWrapper`].
#[repr(transparent)]
pub struct ParamIdentity<T>(T);

impl<T> ConfigParamWrapper<T> for ParamIdentity<T> {
    #[inline]
    fn into_inner(self) -> T {
        self.0
    }

    #[inline]
    fn wrap_inner(inner: &T) -> &Self {
        // SAFETY: `ParamIdentity` is a transparent wrapper.
        unsafe { &*(inner as *const T).cast() }
    }
}

impl<'a, T: Load<'a>> Load<'a> for ParamIdentity<T> {
    #[inline]
    fn load_from(slice: &mut CellSlice<'a>) -> Result<Self, Error> {
        match T::load_from(slice) {
            Ok(value) => Ok(Self(value)),
            Err(e) => Err(e),
        }
    }
}

impl<T: Store> Store for ParamIdentity<T> {
    #[inline]
    fn store_into(&self, builder: &mut CellBuilder, cx: &mut dyn CellContext) -> Result<(), Error> {
        self.0.store_into(builder, cx)
    }
}

#[cfg(feature = "serde")]
impl<T: serde::Serialize> serde::Serialize for ParamIdentity<T> {
    #[inline]
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.0.serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for ParamIdentity<T> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        T::deserialize(deserializer).map(Self)
    }
}

/// Dict wrapper for [`ConfigParamWrapper`] for parsing non-empty dictionaries.
#[repr(transparent)]
pub struct NonEmptyDict<T>(T);

impl<T> ConfigParamWrapper<T> for NonEmptyDict<T> {
    fn into_inner(self) -> T {
        self.0
    }

    #[inline]
    fn wrap_inner(inner: &T) -> &Self {
        // SAFETY: `NonEmptyDict` is a transparent wrapper.
        unsafe { &*(inner as *const T).cast() }
    }
}

impl<'a, K, V> Load<'a> for NonEmptyDict<Dict<K, V>>
where
    K: DictKey,
{
    #[inline]
    fn load_from(slice: &mut CellSlice<'a>) -> Result<Self, Error> {
        match Dict::load_from_root_ext(slice, &mut Cell::empty_context()) {
            Ok(value) => Ok(Self(value)),
            Err(e) => Err(e),
        }
    }
}

impl<K, V> Store for NonEmptyDict<Dict<K, V>> {
    fn store_into(&self, builder: &mut CellBuilder, _: &mut dyn CellContext) -> Result<(), Error> {
        match self.0.root() {
            Some(root) => builder.store_slice(ok!(root.as_slice())),
            None => Err(Error::InvalidData),
        }
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for NonEmptyDict<Dict<u32, ()>> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeSeq;

        let mut seq = serializer.serialize_seq(None)?;
        for entry in self.0.iter() {
            match entry {
                Ok((key, _)) => ok!(seq.serialize_element(&key)),
                Err(e) => return Err(serde::ser::Error::custom(e)),
            }
        }
        seq.end()
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for NonEmptyDict<Dict<u32, ()>> {
    #[inline]
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        use serde::de::Error;

        let parsed = ok!(Vec::<u32>::deserialize(deserializer));
        if parsed.is_empty() {
            return Err(Error::custom("dictionary is empty"));
        }

        let mut res = Dict::new();
        for item in parsed {
            ok!(res.set(item, ()).map_err(Error::custom));
        }

        Ok(Self(res))
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for NonEmptyDict<Dict<u32, StoragePrices>> {
    #[inline]
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.0.serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for NonEmptyDict<Dict<u32, StoragePrices>> {
    #[inline]
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let res = ok!(Dict::deserialize(deserializer));
        if res.is_empty() {
            return Err(serde::de::Error::custom("dictionary is empty"));
        }
        Ok(Self(res))
    }
}

macro_rules! define_config_params {
    ($(
        $(#[doc = $doc:expr])*
        $(#[cfg($($cfg_attrs:tt)*)])?
        $(#[serde($($serde_attrs:tt)*)])?
        $id:literal => $ident:ident($($ty:tt)*)
    ),*$(,)?) => {
        $(
            $(#[cfg($($cfg_attrs)*)])?
            $(#[doc = $doc])*
            pub struct $ident;

            $(#[cfg($($cfg_attrs)*)])?
            impl<'a> KnownConfigParam<'a> for $ident {
                const ID: u32 = $id;

                define_config_params!(@wrapper $($ty)*);
            }
        )*

        #[cfg(feature = "serde")]
        impl serde::Serialize for BlockchainConfigParams {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                use serde::ser::{Error, SerializeMap};

                let dict = &self.0;
                if !serializer.is_human_readable() {
                    return crate::boc::BocRepr::serialize(dict, serializer);
                }

                let mut map = serializer.serialize_map(None)?;

                for entry in dict.iter() {
                    let (key, value) = match entry {
                        Ok(entry) => entry,
                        Err(e) => return Err(Error::custom(e)),
                    };

                    {
                        match key {
                            $(
                                $(#[cfg($($cfg_attrs)*)])?
                                $($id => {
                                    let value = define_config_params!(
                                        @ser
                                        $ident,
                                        value,
                                        $($serde_attrs)*
                                    );
                                    ok!(map.serialize_entry(&key, &value));
                                },)?
                            )*
                            _ => ok!(map.serialize_entry(&key, value.as_ref())),
                        }
                    }
                }

                map.end()
            }
        }

        #[cfg(feature = "serde")]
        impl<'de> serde::Deserialize<'de> for BlockchainConfigParams {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                use serde::de::{Error, Visitor, MapAccess};

                #[derive(serde::Deserialize)]
                struct RawValue(#[serde(with = "crate::boc::Boc")] Cell);

                struct MapVisitor;

                impl<'de> Visitor<'de> for MapVisitor {
                    type Value = BlockchainConfigParams;

                    fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                        f.write_str("a config params map")
                    }

                    fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
                    where
                        M: MapAccess<'de>,
                    {
                        let mut dict = Dict::new();

                        while let Some(key) = access.next_key::<u32>()? {
                            let value = match key {
                                $(
                                    $(#[cfg($($cfg_attrs)*)])?
                                    $($id => define_config_params!(
                                        @de
                                        $ident,
                                        access,
                                        $($serde_attrs)*
                                    ),)?
                                )*
                                _ => {
                                    let RawValue(cell) = ok!(access.next_value());
                                    cell
                                }
                            };
                            ok!(dict.set(key, value).map_err(Error::custom));
                        }

                        Ok(BlockchainConfigParams(dict))
                    }
                }

                if deserializer.is_human_readable() {
                    deserializer.deserialize_map(MapVisitor)
                } else {
                    crate::boc::BocRepr::deserialize(deserializer)
                }
            }
        }
    };

    (@wrapper $wrapper:ident => $($ty:tt)*) => {
        type Value = $($ty)*;
        type Wrapper = $wrapper<Self::Value>;
    };
    (@wrapper $($ty:tt)*) => {
        type Value = $($ty)*;
        type Wrapper = ParamIdentity<Self::Value>;
    };

    (@ser $ident:ident, $value:ident, transparent) => {{
        ok!($value.parse::<<$ident as KnownConfigParam>::Wrapper>().map_err(Error::custom))
    }};
    (@ser $ident:ident, $value:ident, remote = $remote:ident) => {{
        let value = ok!($value.parse::<<$ident as KnownConfigParam>::Wrapper>().map_err(Error::custom));
        $remote(<<$ident as KnownConfigParam>::Wrapper>::into_inner(value))
    }};

    (@de $ident:ident, $access:ident, transparent) => {{
        let parsed = ok!($access.next_value::<<$ident as KnownConfigParam>::Wrapper>());
        ok!(CellBuilder::build_from(&parsed).map_err(Error::custom))
    }};
    (@de $ident:ident, $access:ident, remote = $remote:ident) => {{
        let parsed = ok!($access.next_value::<$remote>());
        ok!(CellBuilder::build_from(&parsed.0).map_err(Error::custom))
    }};
}

define_config_params! {
    /// Configuration account address (in masterchain).
    #[serde(transparent)]
    0 => ConfigParam0(HashBytes),

    /// Elector account address (in masterchain).
    #[serde(transparent)]
    1 => ConfigParam1(HashBytes),

    /// Minter account address (in masterchain).
    #[serde(transparent)]
    2 => ConfigParam2(HashBytes),

    /// Fee collector account address (in masterchain).
    #[serde(transparent)]
    3 => ConfigParam3(HashBytes),

    /// DNS root account address (in masterchain).
    #[serde(transparent)]
    4 => ConfigParam4(HashBytes),

    /// Mint new price and mint add price (unused).
    6 => ConfigParam6(CellSlice<'a>),

    /// Target amount of minted extra currencies.
    #[serde(transparent)]
    7 => ConfigParam7(ExtraCurrencyCollection),

    /// The lowest supported block version and required capabilities.
    ///
    /// Contains a [`GlobalVersion`].
    #[serde(transparent)]
    8 => ConfigParam8(GlobalVersion),

    /// Params that must be present in config.
    #[serde(transparent)]
    9 => ConfigParam9(NonEmptyDict => Dict<u32, ()>),

    /// Params that have a different set of update requirements.
    #[serde(transparent)]
    10 => ConfigParam10(NonEmptyDict => Dict<u32, ()>),

    /// Config voting setup params.
    ///
    /// Contains a [`ConfigVotingSetup`].
    #[serde(transparent)]
    11 => ConfigParam11(ConfigVotingSetup),

    /// Known workchain descriptions.
    ///
    /// Contains a dictionary with workchain id as key and [`WorkchainDescription`] as value.
    #[serde(transparent)]
    12 => ConfigParam12(Dict<i32, WorkchainDescription>),

    /// Complaint pricing.
    13 => ConfigParam13(CellSlice<'a>),

    /// Block creation reward for masterchain and basechain.
    ///
    /// Contains a [`BlockCreationRewards`].
    #[serde(transparent)]
    14 => ConfigParam14(BlockCreationRewards),

    /// Validators election timings.
    ///
    /// Contains [`ElectionTimings`].
    #[serde(transparent)]
    15 => ConfigParam15(ElectionTimings),

    /// Range of number of validators.
    ///
    /// Contains a [`ValidatorCountParams`].
    #[serde(transparent)]
    16 => ConfigParam16(ValidatorCountParams),

    /// Validator stake range and factor.
    ///
    /// Contains [`ValidatorStakeParams`]
    #[serde(transparent)]
    17 => ConfigParam17(ValidatorStakeParams),

    /// Storage prices for different intervals of time.
    ///
    /// Contains a dictionary with a history of all [`StoragePrices`].
    #[serde(transparent)]
    18 => ConfigParam18(NonEmptyDict => Dict<u32, StoragePrices>),

    /// Masterchain gas limits and prices.
    ///
    /// Contains [`GasLimitsPrices`].
    #[serde(transparent)]
    20 => ConfigParam20(GasLimitsPrices),

    /// Base workchain gas limits and prices.
    ///
    /// Contains [`GasLimitsPrices`].
    #[serde(transparent)]
    21 => ConfigParam21(GasLimitsPrices),

    /// Masterchain block limits.
    ///
    /// Contains [`BlockLimits`].
    #[serde(transparent)]
    22 => ConfigParam22(BlockLimits),

    /// Base workchain block limits.
    ///
    /// Contains [`BlockLimits`].
    #[serde(transparent)]
    23 => ConfigParam23(BlockLimits),

    /// Message forwarding prices for masterchain.
    ///
    /// Contains [`MsgForwardPrices`].
    #[serde(transparent)]
    24 => ConfigParam24(MsgForwardPrices),

    /// Message forwarding prices for base workchain.
    ///
    /// Contains [`MsgForwardPrices`].
    #[serde(transparent)]
    25 => ConfigParam25(MsgForwardPrices),

    /// Catchain configuration params.
    ///
    /// Contains a [`CatchainConfig`].
    #[cfg(not(feature = "tycho"))]
    #[serde(transparent)]
    28 => ConfigParam28(CatchainConfig),

    /// Collation configuration params.
    ///
    /// Contains a [`CollationConfig`].
    #[cfg(feature = "tycho")]
    #[serde(transparent)]
    28 => ConfigParam28(CollationConfig),

    /// Consensus configuration params.
    ///
    /// Contains a [`ConsensusConfig`].
    #[serde(transparent)]
    29 => ConfigParam29(ConsensusConfig),

    /// Delector configuration params.
    30 => ConfigParam30(CellSlice<'a>),

    /// Fundamental smartcontract addresses.
    ///
    /// Contains a dictionary with addresses (in masterchain) of fundamental contracts as keys.
    #[serde(remote = HashBytesList)]
    31 => ConfigParam31(Dict<HashBytes, ()>),

    /// Previous validator set.
    ///
    /// Contains a [`ValidatorSet`].
    #[serde(transparent)]
    32 => ConfigParam32(ValidatorSet),

    /// Previous temporary validator set.
    ///
    /// Contains a [`ValidatorSet`].
    #[serde(transparent)]
    33 => ConfigParam33(ValidatorSet),

    /// Current validator set.
    ///
    /// Contains a [`ValidatorSet`].
    #[serde(transparent)]
    34 => ConfigParam34(ValidatorSet),

    /// Current temporary validator set.
    ///
    /// Contains a [`ValidatorSet`].
    #[serde(transparent)]
    35 => ConfigParam35(ValidatorSet),

    /// Next validator set.
    ///
    /// Contains a [`ValidatorSet`].
    #[serde(transparent)]
    36 => ConfigParam36(ValidatorSet),

    /// Next temporary validator set.
    ///
    /// Contains a [`ValidatorSet`].
    #[serde(transparent)]
    37 => ConfigParam37(ValidatorSet),
}

#[cfg(feature = "serde")]
#[repr(transparent)]
struct HashBytesList(Dict<HashBytes, ()>);

#[cfg(feature = "serde")]
impl serde::Serialize for HashBytesList {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeSeq;

        let mut seq = serializer.serialize_seq(None)?;
        for entry in self.0.iter() {
            match entry {
                Ok((ref key, _)) => ok!(seq.serialize_element(key)),
                Err(e) => return Err(serde::ser::Error::custom(e)),
            }
        }
        seq.end()
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for HashBytesList {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::Error;

        let parsed = ok!(Vec::<HashBytes>::deserialize(deserializer));

        let mut res = Dict::new();
        for item in parsed {
            ok!(res.set(item, ()).map_err(Error::custom));
        }

        Ok(Self(res))
    }
}