namada_apps_lib 0.251.4

Namada CLI apps library code
Documentation
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
//! The templates for balances, parameters and VPs used for a chain's genesis.

use std::collections::{BTreeMap, BTreeSet};
use std::marker::PhantomData;
use std::path::Path;

use namada_macros::BorshDeserializer;
#[cfg(feature = "migrations")]
use namada_migrations::*;
use namada_sdk::address::Address;
use namada_sdk::borsh::{BorshDeserialize, BorshSerialize};
use namada_sdk::dec::Dec;
use namada_sdk::eth_bridge::storage::parameters::{
    Contracts, Erc20WhitelistEntry, MinimumConfirmations,
};
use namada_sdk::parameters::ProposalBytes;
use namada_sdk::token::{
    Amount, DenominatedAmount, Denomination, NATIVE_MAX_DECIMAL_PLACES,
};
use namada_sdk::{ethereum_structs, token};
use serde::{Deserialize, Serialize};

use super::transactions::{self, Transactions};
use super::utils::{read_toml, write_toml};
use crate::config::genesis::chain::DeriveEstablishedAddress;
use crate::config::genesis::transactions::{BondTx, SignedBondTx};
use crate::wallet::Alias;

pub const BALANCES_FILE_NAME: &str = "balances.toml";
pub const PARAMETERS_FILE_NAME: &str = "parameters.toml";
pub const VPS_FILE_NAME: &str = "validity-predicates.toml";
pub const TOKENS_FILE_NAME: &str = "tokens.toml";
pub const TRANSACTIONS_FILE_NAME: &str = "transactions.toml";

const MAX_TOKEN_BALANCE_SUM: u64 = i64::MAX as u64;

/// Note that these balances must be crossed-checked with the token configs
/// to correctly represent the underlying amounts.
pub fn read_balances(path: &Path) -> eyre::Result<UndenominatedBalances> {
    read_toml(path, "Balances")
}

pub fn read_parameters(path: &Path) -> eyre::Result<Parameters<Unvalidated>> {
    read_toml(path, "Parameters")
}

pub fn read_validity_predicates(
    path: &Path,
) -> eyre::Result<ValidityPredicates> {
    read_toml(path, "Validity predicates")
}

pub fn read_tokens(path: &Path) -> eyre::Result<Tokens> {
    read_toml(path, "Tokens")
}

pub fn read_transactions(
    path: &Path,
) -> eyre::Result<Transactions<Unvalidated>> {
    read_toml(path, "Transactions")
}

/// Genesis balances of all tokens
#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct UndenominatedBalances {
    pub token: BTreeMap<Alias, RawTokenBalances>,
}

impl UndenominatedBalances {
    /// Use the denom in `TokenConfig` to correctly interpret the balances
    /// to the right denomination.
    pub fn denominate(
        self,
        tokens: &Tokens,
    ) -> eyre::Result<DenominatedBalances> {
        let mut balances = DenominatedBalances {
            token: BTreeMap::new(),
        };
        for (alias, bals) in self.token {
            let denom = tokens
                .token
                .get(&alias)
                .ok_or_else(|| {
                    eyre::eyre!(
                        "A balance of token {} was found, but this token was \
                         not found in the `tokens.toml` file",
                        alias
                    )
                })?
                .denom;
            let mut denominated_bals = BTreeMap::new();
            for (addr, bal) in bals.0.into_iter() {
                let denominated = bal.increase_precision(denom)?;
                denominated_bals.insert(addr, denominated);
            }
            balances
                .token
                .insert(alias, TokenBalances(denominated_bals));
        }
        Ok(balances)
    }
}

/// Genesis balances of all tokens
#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct DenominatedBalances {
    pub token: BTreeMap<Alias, TokenBalances>,
}

/// Genesis balances for a given token
#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct RawTokenBalances(pub BTreeMap<Address, token::DenominatedAmount>);

/// Genesis balances for a given token
#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct TokenBalances(pub BTreeMap<Address, token::DenominatedAmount>);

/// Genesis validity predicates
#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct ValidityPredicates {
    // Wasm definitions
    pub wasm: BTreeMap<String, WasmVpConfig>,
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct WasmVpConfig {
    pub filename: String,
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct Tokens {
    pub token: BTreeMap<Alias, TokenConfig>,
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct TokenConfig {
    pub denom: Denomination,
    pub masp_params: Option<token::ShieldedParams>,
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct Parameters<T: TemplateValidation> {
    pub parameters: ChainParams<T>,
    pub pos_params: PosParams,
    pub gov_params: GovernanceParams,
    pub pgf_params: PgfParams<T>,
    pub eth_bridge_params: Option<EthBridgeParams>,
    pub ibc_params: IbcParams,
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct ChainParams<T: TemplateValidation> {
    /// Max payload size, in bytes, for a tx decided through
    /// the consensus protocol.
    pub max_tx_bytes: u32,
    /// Name of the native token - this must one of the tokens from
    /// `tokens.toml` file
    pub native_token: Alias,
    /// Enable the native token transfer if it is true
    pub is_native_token_transferable: bool,
    /// Minimum number of blocks per epoch.
    // NB: u64 only works with values up to i64::MAX with toml-rs!
    pub min_num_of_blocks: u64,
    /// Max payload size, in bytes, for a tx batch proposal.
    ///
    /// Block proposers may never return a `PrepareProposal`
    /// response containing `txs` with a byte length greater
    /// than whatever is configured through this parameter.
    ///
    /// Note that this parameter's value will always be strictly
    /// smaller than a Tendermint block's `MaxBytes` consensus
    /// parameter. Currently, we hard cap `max_proposal_bytes`
    /// at 90 MiB in Namada, which leaves at least 10 MiB of
    /// room for header data, evidence and protobuf
    /// serialization overhead in Tendermint blocks.
    pub max_proposal_bytes: ProposalBytes,
    /// Hashes of allowed vps array. `None` value or an empty array
    /// disables allowlisting.
    #[serde(alias = "vp_whitelist")] // old name for compat
    pub vp_allowlist: Option<Vec<String>>,
    /// Hashes of allowed txs array. `None` value or an empty array
    /// disables allowlisting.
    #[serde(alias = "tx_whitelist")] // old name for compat
    pub tx_allowlist: Option<Vec<String>>,
    /// Filename of implicit accounts validity predicate WASM code
    pub implicit_vp: String,
    /// Expected number of epochs per year
    pub epochs_per_year: u64,
    /// How many epochs it takes to transition to the next masp epoch
    pub masp_epoch_multiplier: u64,
    /// Max gas for block
    pub max_block_gas: u64,
    /// Gas limit of a masp transaction paying fees
    pub masp_fee_payment_gas_limit: u64,
    /// Gas scale
    pub gas_scale: u64,
    /// Map of the cost per gas unit for every token allowed for fee payment
    pub minimum_gas_price: T::GasMinimums,
}

impl ChainParams<Unvalidated> {
    pub fn denominate(
        self,
        tokens: &Tokens,
    ) -> eyre::Result<ChainParams<Validated>> {
        let ChainParams {
            max_tx_bytes,
            native_token,
            is_native_token_transferable,
            min_num_of_blocks,
            max_proposal_bytes,
            vp_allowlist,
            tx_allowlist,
            implicit_vp,
            epochs_per_year,
            masp_epoch_multiplier,
            max_block_gas,
            masp_fee_payment_gas_limit,
            gas_scale,
            minimum_gas_price,
        } = self;
        let mut min_gas_prices = BTreeMap::default();
        for (token, amount) in minimum_gas_price.into_iter() {
            let denom = if let Some(TokenConfig { denom, .. }) =
                tokens.token.get(&token)
            {
                *denom
            } else {
                eprintln!(
                    "Genesis files contained minimum gas amount  of token {}, \
                     which is not in the `tokens.toml` file",
                    token
                );
                return Err(eyre::eyre!(
                    "Genesis files contained minimum gas amount of token {}, \
                     which is not in the `tokens.toml` file",
                    token
                ));
            };
            let amount = amount.increase_precision(denom).map_err(|e| {
                eprintln!(
                    "A minimum gas amount in the parameters.toml file was \
                     incorrectly formatted:\n{}",
                    e
                );
                e
            })?;
            min_gas_prices.insert(token, amount);
        }

        Ok(ChainParams {
            max_tx_bytes,
            native_token,
            is_native_token_transferable,
            min_num_of_blocks,
            max_proposal_bytes,
            vp_allowlist,
            tx_allowlist,
            implicit_vp,
            epochs_per_year,
            masp_epoch_multiplier,
            max_block_gas,
            masp_fee_payment_gas_limit,
            gas_scale,
            minimum_gas_price: min_gas_prices,
        })
    }
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct PosParams {
    /// Maximum number of active validators.
    pub max_validator_slots: u64,
    /// Pipeline length (in epochs).
    pub pipeline_len: u64,
    /// Unbonding length (in epochs).
    pub unbonding_len: u64,
    /// Votes per token.
    pub tm_votes_per_token: Dec,
    /// Reward for proposing a block.
    pub block_proposer_reward: Dec,
    /// Reward for voting on a block.
    pub block_vote_reward: Dec,
    /// Maximum staking APY
    pub max_inflation_rate: Dec,
    /// Target ratio of staked NAM tokens to total NAM tokens
    pub target_staked_ratio: Dec,
    /// Portion of a validator's stake that should be slashed on a
    /// duplicate vote.
    pub duplicate_vote_min_slash_rate: Dec,
    /// Portion of a validator's stake that should be slashed on a
    /// light client attack.
    pub light_client_attack_min_slash_rate: Dec,
    /// Number of epochs above and below (separately) the current epoch to
    /// consider when doing cubic slashing
    pub cubic_slashing_window_length: u64,
    /// The minimum amount of bonded tokens that a validator needs to be in
    /// either the `consensus` or `below_capacity` validator sets
    pub validator_stake_threshold: token::Amount,
    /// The length, in blocks, of the sliding window for consensus validators
    /// inactivity verification
    pub liveness_window_check: u64,
    /// The minimum required activity of consensus validators, in percentage,
    /// over the `liveness_window_check`
    pub liveness_threshold: Dec,
    /// PoS gain p (read only)
    pub rewards_gain_p: Dec,
    /// PoS gain d (read only)
    pub rewards_gain_d: Dec,
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct GovernanceParams {
    /// Min funds to stake to submit a proposal
    pub min_proposal_fund: u64,
    /// Maximum size of proposal in bytes
    pub max_proposal_code_size: u64,
    /// Minimum number of epochs between the proposal end epoch and start epoch
    pub min_proposal_voting_period: u64,
    /// Maximum number of epochs between the proposal activation epoch and
    /// start epoch
    pub max_proposal_period: u64,
    /// Maximum number of characters in the proposal content
    pub max_proposal_content_size: u64,
    /// Minimum number of epochs between the end and activation epochs
    pub min_proposal_grace_epochs: u64,
    /// Maximum number of epochs between current epoch and start epochs
    pub max_proposal_latency: u64,
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct PgfParams<T: TemplateValidation> {
    /// The set of stewards
    pub stewards: BTreeSet<Address>,
    /// The PGF funding inflation rate
    pub pgf_inflation_rate: Dec,
    /// The PGF stewards inflation rate
    pub stewards_inflation_rate: Dec,
    /// The maximum allowed number of PGF stewards at any time
    pub maximum_number_of_stewards: u64,
    #[serde(default)]
    #[serde(skip_serializing)]
    #[cfg(test)]
    pub valid: PhantomData<T>,
    #[serde(default)]
    #[serde(skip_serializing)]
    #[cfg(not(test))]
    valid: PhantomData<T>,
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct EthBridgeParams {
    /// Initial Ethereum block height when events will first be extracted from.
    pub eth_start_height: ethereum_structs::BlockHeight,
    /// Minimum number of confirmations needed to trust an Ethereum branch.
    /// This must be at least one.
    pub min_confirmations: MinimumConfirmations,
    /// List of ERC20 token types whitelisted at genesis time.
    pub erc20_whitelist: Vec<Erc20WhitelistEntry>,
    /// The addresses of the Ethereum contracts that need to be directly known
    /// by validators.
    pub contracts: Contracts,
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct IbcParams {
    /// Default supply limit of each token
    pub default_mint_limit: token::Amount,
    /// Default per-epoch throughput limit of each token
    pub default_per_epoch_throughput_limit: token::Amount,
}

impl TokenBalances {
    pub fn get(&self, addr: &Address) -> Option<token::Amount> {
        self.0.get(addr).map(|amt| amt.amount())
    }
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
)]
pub struct Unvalidated {}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshDeserializer,
    BorshSerialize,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
)]
pub struct Validated {}

pub trait TemplateValidation: Serialize {
    type Amount: for<'a> Deserialize<'a>
        + Serialize
        + Into<token::DenominatedAmount>
        + Clone
        + std::fmt::Debug
        + BorshSerialize
        + BorshDeserialize
        + PartialEq
        + Eq
        + PartialOrd
        + Ord;
    type Balances: for<'a> Deserialize<'a>
        + Serialize
        + Clone
        + std::fmt::Debug
        + BorshSerialize
        + BorshDeserialize
        + PartialEq
        + Eq;
    type BondTx: for<'a> Deserialize<'a>
        + Serialize
        + Clone
        + std::fmt::Debug
        + BorshSerialize
        + BorshDeserialize
        + PartialEq
        + Eq
        + PartialOrd
        + Ord;
    type GasMinimums: for<'a> Deserialize<'a>
        + Serialize
        + Clone
        + std::fmt::Debug
        + BorshSerialize
        + BorshDeserialize
        + PartialEq
        + Eq;
}

impl TemplateValidation for Unvalidated {
    type Amount = token::DenominatedAmount;
    type Balances = UndenominatedBalances;
    type BondTx = SignedBondTx<Unvalidated>;
    type GasMinimums = BTreeMap<Alias, DenominatedAmount>;
}

impl TemplateValidation for Validated {
    type Amount = token::DenominatedAmount;
    type Balances = DenominatedBalances;
    type BondTx = BondTx<Validated>;
    type GasMinimums = BTreeMap<Alias, DenominatedAmount>;
}

#[derive(
    Clone,
    Debug,
    Deserialize,
    Serialize,
    BorshDeserialize,
    BorshSerialize,
    PartialEq,
    Eq,
)]
pub struct All<T: TemplateValidation> {
    pub vps: ValidityPredicates,
    pub tokens: Tokens,
    pub balances: T::Balances,
    pub parameters: Parameters<T>,
    pub transactions: Transactions<T>,
}

impl<T: TemplateValidation> All<T> {
    pub fn write_toml_files(&self, output_dir: &Path) -> eyre::Result<()> {
        let All {
            vps,
            tokens,
            balances,
            parameters,
            transactions,
        } = self;

        let vps_file = output_dir.join(VPS_FILE_NAME);
        let tokens_file = output_dir.join(TOKENS_FILE_NAME);
        let balances_file = output_dir.join(BALANCES_FILE_NAME);
        let parameters_file = output_dir.join(PARAMETERS_FILE_NAME);
        let transactions_file = output_dir.join(TRANSACTIONS_FILE_NAME);

        write_toml(vps, &vps_file, "Validity predicates")?;
        write_toml(tokens, &tokens_file, "Tokens")?;
        write_toml(balances, &balances_file, "Balances")?;
        write_toml(parameters, &parameters_file, "Parameters")?;
        write_toml(transactions, &transactions_file, "Transactions")?;
        Ok(())
    }
}

impl All<Unvalidated> {
    pub fn read_toml_files(input_dir: &Path) -> eyre::Result<Self> {
        let vps_file = input_dir.join(VPS_FILE_NAME);
        let tokens_file = input_dir.join(TOKENS_FILE_NAME);
        let balances_file = input_dir.join(BALANCES_FILE_NAME);
        let parameters_file = input_dir.join(PARAMETERS_FILE_NAME);
        let transactions_file = input_dir.join(TRANSACTIONS_FILE_NAME);

        let vps = read_toml(&vps_file, "Validity predicates")?;
        let tokens = read_toml(&tokens_file, "Tokens")?;
        let balances = read_toml(&balances_file, "Balances")?;
        let parameters = read_toml(&parameters_file, "Parameters")?;
        let transactions = read_toml(&transactions_file, "Transactions")?;
        Ok(Self {
            vps,
            tokens,
            balances,
            parameters,
            transactions,
        })
    }
}

/// Load genesis templates from the given directory and validate them. Returns
/// `None` when there are some validation issues.
///
/// Note that the validation rules for these templates won't enforce that there
/// is at least one validator with positive voting power. This must be checked
/// when the templates are being used to `init-network`.
pub fn load_and_validate(templates_dir: &Path) -> Option<All<Validated>> {
    let mut is_valid = true;
    // We don't reuse `All::read_toml_files` here to allow to validate config
    // without all files present.
    let vps_file = templates_dir.join(VPS_FILE_NAME);
    let tokens_file = templates_dir.join(TOKENS_FILE_NAME);
    let balances_file = templates_dir.join(BALANCES_FILE_NAME);
    let parameters_file = templates_dir.join(PARAMETERS_FILE_NAME);
    let transactions_file = templates_dir.join(TRANSACTIONS_FILE_NAME);

    // Check that all required files are present
    let mut check_file_exists = |file: &Path, name: &str| {
        if !file.exists() {
            is_valid = false;
            eprintln!("{name} file is missing at {}", file.to_string_lossy());
        }
    };
    check_file_exists(&vps_file, "Validity predicates");
    check_file_exists(&tokens_file, "Tokens");
    check_file_exists(&balances_file, "Balances");
    check_file_exists(&parameters_file, "Parameters");
    check_file_exists(&transactions_file, "Transactions");

    // Load and parse the files
    let vps = read_validity_predicates(&vps_file);
    let tokens = read_tokens(&tokens_file);
    let balances = read_balances(&balances_file);
    let parameters = read_parameters(&parameters_file);
    let transactions = read_transactions(&transactions_file);

    let eprintln_invalid_file = |err: &eyre::Report, name: &str| {
        eprintln!("{name} file is NOT valid. Failed to read with: {err:?}");
    };

    // Check the parsing results
    let vps = vps.map_or_else(
        |err| {
            eprintln_invalid_file(&err, "Validity predicates");
            None
        },
        Some,
    );
    let tokens = tokens.map_or_else(
        |err| {
            eprintln_invalid_file(&err, "Tokens");
            None
        },
        Some,
    );
    let balances = balances.map_or_else(
        |err| {
            eprintln_invalid_file(&err, "Balances");
            None
        },
        Some,
    );
    let parameters = parameters.map_or_else(
        |err| {
            eprintln_invalid_file(&err, "Parameters");
            None
        },
        Some,
    );
    let transactions = transactions.map_or_else(
        |err| {
            eprintln_invalid_file(&err, "Transactions");
            None
        },
        Some,
    );

    // Validate each file that could be loaded
    if let Some(vps) = vps.as_ref() {
        if validate_vps(vps) {
            println!("Validity predicates file is valid.");
        } else {
            is_valid = false;
        }
    }

    let (parameters, native_token) = if let Some(parameters) = parameters {
        let params = validate_parameters(
            parameters,
            &tokens,
            &transactions,
            vps.as_ref(),
        );
        if let Some(validate_params) = params.clone() {
            println!("Parameters file is valid.");
            (params, Some(validate_params.parameters.native_token))
        } else {
            is_valid = false;
            (params, None)
        }
    } else {
        (None, None)
    };

    let balances = if let Some(tokens) = tokens.as_ref() {
        if tokens.token.is_empty() {
            is_valid = false;
            eprintln!(
                "Tokens file is invalid. There has to be at least one token."
            );
        }
        println!("Tokens file is valid.");
        balances
            .and_then(|raw| raw.denominate(tokens).ok())
            .and_then(|balances| {
                validate_balances(&balances, Some(tokens), native_token).then(
                    || {
                        println!("Balances file is valid.");
                        balances
                    },
                )
            })
    } else {
        None
    };
    if balances.is_none() {
        is_valid = false;
    }

    let txs = if let Some(txs) = transactions.and_then(|txs| {
        transactions::validate(
            txs,
            vps.as_ref(),
            balances.as_ref(),
            parameters.as_ref(),
        )
    }) {
        println!("Transactions file is valid.");
        Some(txs)
    } else {
        is_valid = false;
        None
    };

    match vps {
        Some(vps) if is_valid => Some(All {
            vps,
            tokens: tokens.unwrap(),
            balances: balances.unwrap(),
            parameters: parameters.unwrap(),
            transactions: txs.unwrap(),
        }),
        _ => None,
    }
}

pub fn validate_vps(vps: &ValidityPredicates) -> bool {
    let mut is_valid = true;
    vps.wasm.iter().for_each(|(name, config)| {
        if !config.filename.ends_with(".wasm") {
            eprintln!(
                "Invalid validity predicate \"{name}\" configuration. Only \
                 \".wasm\" filenames are currently supported."
            );
            is_valid = false;
        }
    });
    is_valid
}

pub fn validate_parameters(
    parameters: Parameters<Unvalidated>,
    tokens: &Option<Tokens>,
    transactions: &Option<Transactions<Unvalidated>>,
    vps: Option<&ValidityPredicates>,
) -> Option<Parameters<Validated>> {
    let tokens = tokens.as_ref()?;
    let txs = transactions.as_ref()?;
    let mut is_valid = true;
    let implicit_vp = &parameters.parameters.implicit_vp;
    if !vps
        .map(|vps| vps.wasm.contains_key(implicit_vp))
        .unwrap_or_default()
    {
        eprintln!(
            "Implicit VP \"{implicit_vp}\" not found in the Validity \
             predicates files."
        );
        is_valid = false;
    }
    // check that each PGF steward has an established account
    for steward in &parameters.pgf_params.stewards {
        let mut found_steward = false;
        if let Some(accs) = &txs.established_account {
            if accs.iter().any(|acct| {
                let addr = acct.derive_address();
                &addr == steward
            }) {
                found_steward = true;
            }
        }

        is_valid = found_steward && is_valid;
        if !is_valid {
            eprintln!(
                "Could not find an established or validator account \
                 associated with the PGF steward {}",
                steward
            );
        }
    }
    let Parameters {
        parameters,
        pos_params,
        gov_params,
        pgf_params,
        eth_bridge_params,
        ibc_params,
    } = parameters;
    match parameters.denominate(tokens) {
        Err(e) => {
            eprintln!("{}", e);
            None
        }
        Ok(parameters) => is_valid.then(|| Parameters {
            parameters,
            pos_params,
            gov_params,
            pgf_params: PgfParams {
                stewards: pgf_params.stewards,
                pgf_inflation_rate: pgf_params.pgf_inflation_rate,
                stewards_inflation_rate: pgf_params.stewards_inflation_rate,
                maximum_number_of_stewards: pgf_params
                    .maximum_number_of_stewards,
                valid: Default::default(),
            },
            eth_bridge_params,
            ibc_params,
        }),
    }
}

pub fn validate_balances(
    balances: &DenominatedBalances,
    tokens: Option<&Tokens>,
    native_alias: Option<Alias>,
) -> bool {
    let mut is_valid = true;

    balances.token.iter().for_each(|(token, next)| {
        // Every token alias used in Balances file must be present in
        // the Tokens file
        if !tokens
            .as_ref()
            .map(|tokens| tokens.token.contains_key(token))
            .unwrap_or_default()
        {
            is_valid = false;
            eprintln!(
                "Token \"{token}\" from the Balances file is not present in \
                 the Tokens file."
            )
        }

        // Check the sum of balances
        let sum = next.0.values().try_fold(
            token::Amount::default(),
            |acc, amount| {
                let res = acc.checked_add(amount.amount());
                if res.as_ref().is_none() {
                    is_valid = false;
                    eprintln!(
                        "Balances for token {token} overflow `token::Amount`"
                    );
                }
                res
            },
        );
        if Some(token) == native_alias.as_ref() {
            match sum {
                Some(sum) if sum.is_positive() => {
                    if sum
                        > Amount::from_uint(
                            MAX_TOKEN_BALANCE_SUM,
                            NATIVE_MAX_DECIMAL_PLACES,
                        )
                        .unwrap()
                    {
                        eprintln!(
                            "The sum of balances for token {token} is greater \
                             than {MAX_TOKEN_BALANCE_SUM}"
                        );
                        is_valid = false;
                    }
                }
                _ => {
                    eprintln!(
                        "Native token {token} balance is zero at genesis."
                    );
                    is_valid = false;
                }
            }
        }
    });
    is_valid
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::path::PathBuf;

    use namada_sdk::key;
    use namada_sdk::key::RefTo;
    use tempfile::tempdir;

    use super::*;

    /// Validate the `genesis/localnet` genesis templates.
    #[test]
    fn test_validate_localnet_genesis_templates() {
        let templates_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .unwrap()
            .parent()
            .unwrap()
            .join("genesis/localnet");
        assert!(
            load_and_validate(&templates_dir).is_some(),
            "Localnet genesis templates must be valid"
        );
    }

    /// Validate the `genesis/starter` genesis templates.
    #[test]
    fn test_validate_starter_genesis_templates() {
        let templates_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .unwrap()
            .parent()
            .unwrap()
            .join("genesis/starter");
        assert!(
            load_and_validate(&templates_dir).is_some(),
            "Starter genesis templates must be valid"
        );
    }

    /// Validate the `genesis/hardware` genesis templates.
    #[test]
    fn test_validate_hardware_genesis_templates() {
        let templates_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .unwrap()
            .parent()
            .unwrap()
            .join("genesis/hardware");
        assert!(
            load_and_validate(&templates_dir).is_some(),
            "Hardware genesis templates must be valid"
        );
    }

    #[test]
    fn test_read_balances() {
        let test_dir = tempdir().unwrap();
        let path = test_dir.path().join(BALANCES_FILE_NAME);
        let sk = key::testing::keypair_1();
        let pk = sk.ref_to();
        let address: Address = (&pk).into();
        let balance = token::Amount::from(101_000_001);
        let token_alias = Alias::from("Some_token".to_string());
        let contents = format!(
            r#"
		[token.{token_alias}]
		{address} = "{}"
	    "#,
            balance.to_string_native()
        );
        fs::write(&path, contents).unwrap();

        let balances = read_balances(&path).unwrap();
        let example_balance = balances.token.get(&token_alias).unwrap();
        assert_eq!(balance, example_balance.0.get(&address).unwrap().amount());
    }
}