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
//! Governance Account
use {
    crate::{
        error::GovernanceError,
        state::{
            enums::{GovernanceAccountType, VoteThreshold, VoteTipping},
            legacy::{is_governance_v1_account_type, GovernanceV1},
            realm::{assert_is_valid_realm, RealmV2},
            vote_record::VoteKind,
        },
        tools::structs::Reserved119,
    },
    borsh::{maybestd::io::Write, BorshDeserialize, BorshSchema, BorshSerialize},
    solana_program::{
        account_info::AccountInfo, program_error::ProgramError, program_pack::IsInitialized,
        pubkey::Pubkey, rent::Rent,
    },
    spl_governance_tools::{
        account::{
            assert_is_valid_account_of_types, extend_account_size, get_account_data,
            get_account_type, AccountMaxSize,
        },
        error::GovernanceToolsError,
    },
};

/// Governance config
#[derive(Clone, Debug, PartialEq, Eq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct GovernanceConfig {
    /// The type of the vote threshold used for community vote
    /// Note: In the current version only YesVotePercentage and Disabled
    /// thresholds are supported
    pub community_vote_threshold: VoteThreshold,

    /// Minimum community weight a governance token owner must possess to be
    /// able to create a proposal
    pub min_community_weight_to_create_proposal: u64,

    /// Minimum waiting time in seconds for a transaction to be executed after
    /// proposal is voted on
    pub min_transaction_hold_up_time: u32,

    /// The base voting time in seconds for proposal to be open for voting
    /// Voting is unrestricted during the base voting time and any vote types
    /// can be cast The base voting time can be extend by optional cool off
    /// time when only negative votes (Veto and Deny) are allowed
    pub voting_base_time: u32,

    /// Conditions under which a Community vote will complete early
    pub community_vote_tipping: VoteTipping,

    /// The type of the vote threshold used for council vote
    /// Note: In the current version only YesVotePercentage and Disabled
    /// thresholds are supported
    pub council_vote_threshold: VoteThreshold,

    /// The threshold for Council Veto votes
    pub council_veto_vote_threshold: VoteThreshold,

    /// Minimum council weight a governance token owner must possess to be able
    /// to create a proposal
    pub min_council_weight_to_create_proposal: u64,

    /// Conditions under which a Council vote will complete early
    pub council_vote_tipping: VoteTipping,

    /// The threshold for Community Veto votes
    pub community_veto_vote_threshold: VoteThreshold,

    /// Voting cool of time
    pub voting_cool_off_time: u32,

    /// The number of active proposals exempt from the Proposal security deposit
    pub deposit_exempt_proposal_count: u8,
}

/// The default number of active proposals exempt from security deposit
pub const DEFAULT_DEPOSIT_EXEMPT_PROPOSAL_COUNT: u8 = 10;

/// Security deposit is paid when a Proposal is created and can be refunded
/// after voting ends or the Proposals is cancelled
pub const SECURITY_DEPOSIT_BASE_LAMPORTS: u64 = 100_000_000; // 0.1 SOL

/// Governance Account
#[derive(Clone, Debug, PartialEq, Eq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct GovernanceV2 {
    /// Account type. It can be Uninitialized, Governance, ProgramGovernance,
    /// TokenGovernance or MintGovernance
    pub account_type: GovernanceAccountType,

    /// Governance Realm
    pub realm: Pubkey,

    /// Account governed by this Governance and/or PDA identity seed
    /// It can be Program account, Mint account, Token account or any other
    /// account
    ///
    /// Note: The account doesn't have to exist. In that case the field is only
    /// a PDA seed
    ///
    /// Note: Setting governed_account doesn't give any authority over the
    /// governed account The relevant authorities for specific account types
    /// must still be transferred to the Governance PDA Ex: mint_authority/
    /// freeze_authority for a Mint account or upgrade_authority for a
    /// Program account should be transferred to the Governance PDA
    pub governed_account: Pubkey,

    /// Reserved space for future versions
    pub reserved1: u32,

    /// Governance config
    pub config: GovernanceConfig,

    /// Reserved space for versions v2 and onwards
    /// Note 1: V1 accounts must be resized before using this space
    /// Note 2: The reserved space should be used from the end to also allow the
    /// config to grow if needed
    pub reserved_v2: Reserved119,

    /// The number of required signatories for proposals in the Governance
    pub required_signatories_count: u8,

    /// The number of active proposals where active means Draft, SigningOff or
    /// Voting state
    ///
    /// Note: The counter was introduced in program V3 and didn't exist in
    /// program V1 & V2 If the program is upgraded from program V1 or V2
    /// while there are any outstanding active proposals the counter won't
    /// be accurate until all proposals are transitioned to an inactive final
    /// state and the counter reset
    pub active_proposal_count: u64,
}

impl AccountMaxSize for GovernanceV2 {
    fn get_max_size(&self) -> Option<usize> {
        Some(236)
    }
}

/// Checks if the given account type is one of the Governance V2 account types
pub fn is_governance_v2_account_type(account_type: &GovernanceAccountType) -> bool {
    match account_type {
        GovernanceAccountType::GovernanceV2
        | GovernanceAccountType::ProgramGovernanceV2
        | GovernanceAccountType::MintGovernanceV2
        | GovernanceAccountType::TokenGovernanceV2 => true,
        GovernanceAccountType::Uninitialized
        | GovernanceAccountType::RealmV1
        | GovernanceAccountType::RealmV2
        | GovernanceAccountType::RealmConfig
        | GovernanceAccountType::TokenOwnerRecordV1
        | GovernanceAccountType::TokenOwnerRecordV2
        | GovernanceAccountType::GovernanceV1
        | GovernanceAccountType::ProgramGovernanceV1
        | GovernanceAccountType::MintGovernanceV1
        | GovernanceAccountType::TokenGovernanceV1
        | GovernanceAccountType::ProposalV1
        | GovernanceAccountType::ProposalV2
        | GovernanceAccountType::SignatoryRecordV1
        | GovernanceAccountType::SignatoryRecordV2
        | GovernanceAccountType::ProposalInstructionV1
        | GovernanceAccountType::ProposalTransactionV2
        | GovernanceAccountType::VoteRecordV1
        | GovernanceAccountType::VoteRecordV2
        | GovernanceAccountType::ProgramMetadata
        | GovernanceAccountType::ProposalDeposit
        | GovernanceAccountType::RequiredSignatory => false,
    }
}

/// Returns GovernanceV2 type for given GovernanceV1 type or None if the given
/// account type is not GovernanceV1
pub fn try_get_governance_v2_type_for_v1(
    account_type: &GovernanceAccountType,
) -> Option<GovernanceAccountType> {
    match account_type {
        GovernanceAccountType::GovernanceV1 => Some(GovernanceAccountType::GovernanceV2),
        GovernanceAccountType::ProgramGovernanceV1 => {
            Some(GovernanceAccountType::ProgramGovernanceV2)
        }
        GovernanceAccountType::MintGovernanceV1 => Some(GovernanceAccountType::MintGovernanceV2),
        GovernanceAccountType::TokenGovernanceV1 => Some(GovernanceAccountType::TokenGovernanceV2),
        GovernanceAccountType::Uninitialized
        | GovernanceAccountType::RealmV1
        | GovernanceAccountType::RealmV2
        | GovernanceAccountType::RealmConfig
        | GovernanceAccountType::TokenOwnerRecordV1
        | GovernanceAccountType::TokenOwnerRecordV2
        | GovernanceAccountType::GovernanceV2
        | GovernanceAccountType::ProgramGovernanceV2
        | GovernanceAccountType::MintGovernanceV2
        | GovernanceAccountType::TokenGovernanceV2
        | GovernanceAccountType::ProposalV1
        | GovernanceAccountType::ProposalV2
        | GovernanceAccountType::SignatoryRecordV1
        | GovernanceAccountType::SignatoryRecordV2
        | GovernanceAccountType::ProposalInstructionV1
        | GovernanceAccountType::ProposalTransactionV2
        | GovernanceAccountType::VoteRecordV1
        | GovernanceAccountType::VoteRecordV2
        | GovernanceAccountType::ProgramMetadata
        | GovernanceAccountType::ProposalDeposit
        | GovernanceAccountType::RequiredSignatory => None,
    }
}

/// Checks if the given account type is on of the Governance account types of
/// any version
pub fn is_governance_account_type(account_type: &GovernanceAccountType) -> bool {
    is_governance_v1_account_type(account_type) || is_governance_v2_account_type(account_type)
}

impl IsInitialized for GovernanceV2 {
    fn is_initialized(&self) -> bool {
        is_governance_v2_account_type(&self.account_type)
    }
}

impl GovernanceV2 {
    /// Returns Governance PDA seeds
    pub fn get_governance_address_seeds(&self) -> Result<[&[u8]; 3], ProgramError> {
        let seeds = match self.account_type {
            GovernanceAccountType::GovernanceV1 | GovernanceAccountType::GovernanceV2 => {
                get_governance_address_seeds(&self.realm, &self.governed_account)
            }
            GovernanceAccountType::ProgramGovernanceV1
            | GovernanceAccountType::ProgramGovernanceV2 => {
                get_program_governance_address_seeds(&self.realm, &self.governed_account)
            }
            GovernanceAccountType::MintGovernanceV1 | GovernanceAccountType::MintGovernanceV2 => {
                get_mint_governance_address_seeds(&self.realm, &self.governed_account)
            }
            GovernanceAccountType::TokenGovernanceV1 | GovernanceAccountType::TokenGovernanceV2 => {
                get_token_governance_address_seeds(&self.realm, &self.governed_account)
            }
            GovernanceAccountType::Uninitialized
            | GovernanceAccountType::RealmV1
            | GovernanceAccountType::TokenOwnerRecordV1
            | GovernanceAccountType::ProposalV1
            | GovernanceAccountType::SignatoryRecordV1
            | GovernanceAccountType::VoteRecordV1
            | GovernanceAccountType::ProposalInstructionV1
            | GovernanceAccountType::RealmConfig
            | GovernanceAccountType::VoteRecordV2
            | GovernanceAccountType::ProposalTransactionV2
            | GovernanceAccountType::ProposalV2
            | GovernanceAccountType::ProgramMetadata
            | GovernanceAccountType::ProposalDeposit
            | GovernanceAccountType::RealmV2
            | GovernanceAccountType::TokenOwnerRecordV2
            | GovernanceAccountType::SignatoryRecordV2
            | GovernanceAccountType::RequiredSignatory => {
                return Err(GovernanceToolsError::InvalidAccountType.into())
            }
        };

        Ok(seeds)
    }

    /// Serializes account into the target buffer
    pub fn serialize<W: Write>(self, writer: W) -> Result<(), ProgramError> {
        if is_governance_v2_account_type(&self.account_type) {
            borsh::to_writer(writer, &self)?
        } else if is_governance_v1_account_type(&self.account_type) {
            // V1 account can't be resized and we have to translate it back to the original
            // format

            // If reserved_v2 is used it must be individually assessed for GovernanceV1
            // account backward compatibility impact
            if self.reserved_v2 != Reserved119::default() {
                panic!("Extended data not supported by GovernanceV1")
            }

            // Note: active_proposal_count is not preserved on GovernanceV1 account until
            // it's migrated to GovernanceV2 during Proposal creation

            let governance_data_v1 = GovernanceV1 {
                account_type: self.account_type,
                realm: self.realm,
                governed_account: self.governed_account,
                proposals_count: 0,
                config: self.config,
            };

            borsh::to_writer(writer, &governance_data_v1)?
        }

        Ok(())
    }

    /// Serializes Governance accounts as GovernanceV2
    /// If the account is GovernanceV1 then it changes its type to GovernanceV2
    /// and resizes account data Note: It supports all the specialized
    /// Governance account types (Governance, ProgramGovernance, MintGovernance
    /// and TokenGovernance)
    pub fn serialize_as_governance_v2<'a>(
        mut self,
        governance_info: &AccountInfo<'a>,
        payer_info: &AccountInfo<'a>,
        system_info: &AccountInfo<'a>,
        rent: &Rent,
    ) -> Result<(), ProgramError> {
        // If the Governance account is GovernanceV1 reallocate its size and change type
        // to GovernanceV2
        if let Some(governance_v2_type) = try_get_governance_v2_type_for_v1(&self.account_type) {
            // Change type to GovernanceV2
            // Note: Only type change is required because the account data was translated to
            // GovernanceV2 during deserialisation
            self.account_type = governance_v2_type;

            extend_account_size(
                governance_info,
                payer_info,
                self.get_max_size().unwrap(),
                rent,
                system_info,
            )?;
        }

        self.serialize(&mut governance_info.data.borrow_mut()[..])
    }

    /// Asserts the provided voting population represented by the given
    /// governing_token_mint can cast the given vote type on proposals for
    /// the Governance
    pub fn assert_governing_token_mint_can_vote(
        &self,
        realm_data: &RealmV2,
        vote_governing_token_mint: &Pubkey,
        vote_kind: &VoteKind,
    ) -> Result<(), ProgramError> {
        // resolve_vote_threshold() asserts the vote threshold exists for the given
        // governing_token_mint and is not disabled
        let _ = self.resolve_vote_threshold(realm_data, vote_governing_token_mint, vote_kind)?;

        Ok(())
    }

    /// Resolves VoteThreshold for the given realm, governing token and Vote
    /// kind
    pub fn resolve_vote_threshold(
        &self,
        realm_data: &RealmV2,
        vote_governing_token_mint: &Pubkey,
        vote_kind: &VoteKind,
    ) -> Result<VoteThreshold, ProgramError> {
        let vote_threshold = if realm_data.community_mint == *vote_governing_token_mint {
            match vote_kind {
                VoteKind::Electorate => &self.config.community_vote_threshold,
                VoteKind::Veto => &self.config.community_veto_vote_threshold,
            }
        } else if realm_data.config.council_mint == Some(*vote_governing_token_mint) {
            match vote_kind {
                VoteKind::Electorate => &self.config.council_vote_threshold,
                VoteKind::Veto => &self.config.council_veto_vote_threshold,
            }
        } else {
            return Err(GovernanceError::InvalidGoverningTokenMint.into());
        };

        if *vote_threshold == VoteThreshold::Disabled {
            return Err(GovernanceError::GoverningTokenMintNotAllowedToVote.into());
        }

        Ok(vote_threshold.clone())
    }

    /// Returns VoteTipping for the given governing_token_mint
    pub fn get_vote_tipping(
        &self,
        realm_data: &RealmV2,
        governing_token_mint: &Pubkey,
    ) -> Result<&VoteTipping, ProgramError> {
        let vote_tipping = if *governing_token_mint == realm_data.community_mint {
            &self.config.community_vote_tipping
        } else if Some(*governing_token_mint) == realm_data.config.council_mint {
            &self.config.council_vote_tipping
        } else {
            return Err(GovernanceError::InvalidGoverningTokenMint.into());
        };

        Ok(vote_tipping)
    }

    /// Returns the required deposit amount for creating Nth Proposal based on
    /// the number of active proposals where N equals to
    /// active_proposal_count - deposit_exempt_proposal_count The deposit is
    /// not payed unless there are more active Proposal than the exempt amount
    ///
    /// Note: The exact deposit payed for Nth Proposal is
    /// N*SECURITY_DEPOSIT_BASE_LAMPORTS + min_rent_for(ProposalDeposit)
    ///
    /// Note: Although the deposit amount payed for Nth proposal is linear the
    /// total deposit amount required to create N proposals is sum of arithmetic
    /// series Dn = N*r + d*N*(N+1)/2
    // where:
    // Dn - The total deposit amount required to create N proposals
    // N = active_proposal_count - deposit_exempt_proposal_count
    // d = SECURITY_DEPOSIT_BASE_LAMPORTS
    // r = min rent amount for ProposalDeposit
    pub fn get_proposal_deposit_amount(&self) -> u64 {
        self.active_proposal_count
            .saturating_sub(self.config.deposit_exempt_proposal_count as u64)
            .saturating_mul(SECURITY_DEPOSIT_BASE_LAMPORTS)
    }
}

/// Deserializes Governance account and checks owner program
pub fn get_governance_data(
    program_id: &Pubkey,
    governance_info: &AccountInfo,
) -> Result<GovernanceV2, ProgramError> {
    let account_type: GovernanceAccountType = get_account_type(program_id, governance_info)?;

    // If the account is V1 version then translate to V2
    let mut governance_data = if is_governance_v1_account_type(&account_type) {
        let governance_data_v1 = get_account_data::<GovernanceV1>(program_id, governance_info)?;

        GovernanceV2 {
            account_type,
            realm: governance_data_v1.realm,
            governed_account: governance_data_v1.governed_account,
            reserved1: 0,
            config: governance_data_v1.config,
            reserved_v2: Reserved119::default(),
            required_signatories_count: 0,
            // GovernanceV1 layout doesn't support active_proposal_count
            // For any legacy GovernanceV1 account it's not preserved until the account layout is
            // migrated to GovernanceV2 in CreateProposal
            active_proposal_count: 0,
        }
    } else {
        get_account_data::<GovernanceV2>(program_id, governance_info)?
    };

    // In previous versions of spl-gov (< 3) we had
    // config.proposal_cool_off_time:u32 which was unused and always 0
    // In version 3.0.0 proposal_cool_off_time was replaced with
    // council_vote_threshold:VoteThreshold and
    // council_veto_vote_threshold:VoteThreshold If we read a legacy account
    // then council_vote_threshold == VoteThreshold::YesVotePercentage(0)
    //
    // Note: assert_is_valid_governance_config() prevents setting
    // council_vote_threshold to VoteThreshold::YesVotePercentage(0) which gives
    // as guarantee that it is a legacy account layout set with
    // proposal_cool_off_time = 0
    //
    // Note: All the settings below are one time config migration from program V1 &
    // V2 account data to V3
    if governance_data.config.council_vote_threshold == VoteThreshold::YesVotePercentage(0) {
        // Set council_vote_threshold to community_vote_threshold which was used for
        // both council and community thresholds before
        governance_data.config.council_vote_threshold =
            governance_data.config.community_vote_threshold.clone();

        // The assumption here is that council should have Veto vote enabled by default
        // and equal to council_vote_threshold
        governance_data.config.council_veto_vote_threshold =
            governance_data.config.council_vote_threshold.clone();

        // For legacy accounts default Council VoteTipping to the Community
        governance_data.config.council_vote_tipping =
            governance_data.config.community_vote_tipping.clone();

        // For legacy accounts set the community Veto threshold to Disabled
        governance_data.config.community_veto_vote_threshold = VoteThreshold::Disabled;

        // Reset voting_cool_off_time and deposit_exempt_proposal_count  previously used
        // for voting_proposal_count
        governance_data.config.voting_cool_off_time = 0;
        governance_data.config.deposit_exempt_proposal_count =
            DEFAULT_DEPOSIT_EXEMPT_PROPOSAL_COUNT;

        // Reset reserved space previously used for proposal_count
        governance_data.reserved1 = 0;
    }

    Ok(governance_data)
}

/// Deserializes Governance account, checks owner program and asserts governance
/// belongs to the given ream
pub fn get_governance_data_for_realm(
    program_id: &Pubkey,
    governance_info: &AccountInfo,
    realm: &Pubkey,
) -> Result<GovernanceV2, ProgramError> {
    let governance_data = get_governance_data(program_id, governance_info)?;

    if governance_data.realm != *realm {
        return Err(GovernanceError::InvalidRealmForGovernance.into());
    }

    Ok(governance_data)
}

/// Checks the given account is a governance account and belongs to the given
/// realm
pub fn assert_governance_for_realm(
    program_id: &Pubkey,
    governance_info: &AccountInfo,
    realm: &Pubkey,
) -> Result<(), ProgramError> {
    get_governance_data_for_realm(program_id, governance_info, realm)?;
    Ok(())
}

/// Returns ProgramGovernance PDA seeds
pub fn get_program_governance_address_seeds<'a>(
    realm: &'a Pubkey,
    governed_program: &'a Pubkey,
) -> [&'a [u8]; 3] {
    // 'program-governance' prefix ensures uniqueness of the PDA
    // Note: Only the current program upgrade authority can create an account with
    // this PDA using CreateProgramGovernance instruction
    [
        b"program-governance",
        realm.as_ref(),
        governed_program.as_ref(),
    ]
}

/// Returns ProgramGovernance PDA address
pub fn get_program_governance_address<'a>(
    program_id: &Pubkey,
    realm: &'a Pubkey,
    governed_program: &'a Pubkey,
) -> Pubkey {
    Pubkey::find_program_address(
        &get_program_governance_address_seeds(realm, governed_program),
        program_id,
    )
    .0
}

/// Returns MintGovernance PDA seeds
pub fn get_mint_governance_address_seeds<'a>(
    realm: &'a Pubkey,
    governed_mint: &'a Pubkey,
) -> [&'a [u8]; 3] {
    // 'mint-governance' prefix ensures uniqueness of the PDA
    // Note: Only the current mint authority can create an account with this PDA
    // using CreateMintGovernance instruction
    [b"mint-governance", realm.as_ref(), governed_mint.as_ref()]
}

/// Returns MintGovernance PDA address
pub fn get_mint_governance_address<'a>(
    program_id: &Pubkey,
    realm: &'a Pubkey,
    governed_mint: &'a Pubkey,
) -> Pubkey {
    Pubkey::find_program_address(
        &get_mint_governance_address_seeds(realm, governed_mint),
        program_id,
    )
    .0
}

/// Returns TokenGovernance PDA seeds
pub fn get_token_governance_address_seeds<'a>(
    realm: &'a Pubkey,
    governed_token: &'a Pubkey,
) -> [&'a [u8]; 3] {
    // 'token-governance' prefix ensures uniqueness of the PDA
    // Note: Only the current token account owner can create an account with this
    // PDA using CreateTokenGovernance instruction
    [b"token-governance", realm.as_ref(), governed_token.as_ref()]
}

/// Returns TokenGovernance PDA address
pub fn get_token_governance_address<'a>(
    program_id: &Pubkey,
    realm: &'a Pubkey,
    governed_token: &'a Pubkey,
) -> Pubkey {
    Pubkey::find_program_address(
        &get_token_governance_address_seeds(realm, governed_token),
        program_id,
    )
    .0
}

/// Returns Governance PDA seeds
pub fn get_governance_address_seeds<'a>(
    realm: &'a Pubkey,
    governed_account: &'a Pubkey,
) -> [&'a [u8]; 3] {
    [
        b"account-governance",
        realm.as_ref(),
        governed_account.as_ref(),
    ]
}

/// Returns Governance PDA address
pub fn get_governance_address<'a>(
    program_id: &Pubkey,
    realm: &'a Pubkey,
    governed_account: &'a Pubkey,
) -> Pubkey {
    Pubkey::find_program_address(
        &get_governance_address_seeds(realm, governed_account),
        program_id,
    )
    .0
}

/// Checks whether the Governance account exists, is initialized and owned by
/// the Governance program
pub fn assert_is_valid_governance(
    program_id: &Pubkey,
    governance_info: &AccountInfo,
) -> Result<(), ProgramError> {
    assert_is_valid_account_of_types(program_id, governance_info, is_governance_account_type)
}

/// Validates args supplied to create governance account
pub fn assert_valid_create_governance_args(
    program_id: &Pubkey,
    governance_config: &GovernanceConfig,
    realm_info: &AccountInfo,
) -> Result<(), ProgramError> {
    assert_is_valid_realm(program_id, realm_info)?;

    assert_is_valid_governance_config(governance_config)?;

    Ok(())
}

/// Validates governance config parameters
pub fn assert_is_valid_governance_config(
    governance_config: &GovernanceConfig,
) -> Result<(), ProgramError> {
    assert_is_valid_vote_threshold(&governance_config.community_vote_threshold)?;
    assert_is_valid_vote_threshold(&governance_config.community_veto_vote_threshold)?;

    assert_is_valid_vote_threshold(&governance_config.council_vote_threshold)?;
    assert_is_valid_vote_threshold(&governance_config.council_veto_vote_threshold)?;

    // Setting both thresholds to Disabled is not allowed, however we might
    // reconsider it as a way to disable Governance permanently
    if governance_config.community_vote_threshold == VoteThreshold::Disabled
        && governance_config.council_vote_threshold == VoteThreshold::Disabled
    {
        return Err(GovernanceError::AtLeastOneVoteThresholdRequired.into());
    }

    // Make u8::MAX invalid value in case we would like to use the magic number as
    // Disabled value in the future
    if governance_config.deposit_exempt_proposal_count == u8::MAX {
        return Err(GovernanceError::InvalidDepositExemptProposalCount.into());
    }

    Ok(())
}

/// Asserts the provided vote_threshold is valid
pub fn assert_is_valid_vote_threshold(vote_threshold: &VoteThreshold) -> Result<(), ProgramError> {
    match *vote_threshold {
        VoteThreshold::YesVotePercentage(yes_vote_threshold_percentage) => {
            if !(1..=100).contains(&yes_vote_threshold_percentage) {
                return Err(GovernanceError::InvalidVoteThresholdPercentage.into());
            }
        }
        VoteThreshold::QuorumPercentage(_) => {
            return Err(GovernanceError::VoteThresholdTypeNotSupported.into());
        }
        VoteThreshold::Disabled => {}
    }

    Ok(())
}

#[cfg(test)]
mod test {
    use {super::*, solana_program::clock::Epoch};

    fn create_test_governance_config() -> GovernanceConfig {
        GovernanceConfig {
            community_vote_threshold: VoteThreshold::YesVotePercentage(60),
            min_community_weight_to_create_proposal: 5,
            min_transaction_hold_up_time: 10,
            voting_base_time: 5,
            community_vote_tipping: VoteTipping::Strict,
            council_vote_threshold: VoteThreshold::YesVotePercentage(60),
            council_veto_vote_threshold: VoteThreshold::YesVotePercentage(50),
            min_council_weight_to_create_proposal: 1,
            council_vote_tipping: VoteTipping::Strict,
            community_veto_vote_threshold: VoteThreshold::YesVotePercentage(40),
            voting_cool_off_time: 2,
            deposit_exempt_proposal_count: 0,
        }
    }

    fn create_test_governance() -> GovernanceV2 {
        GovernanceV2 {
            account_type: GovernanceAccountType::GovernanceV2,
            realm: Pubkey::new_unique(),
            governed_account: Pubkey::new_unique(),
            reserved1: 0,
            config: create_test_governance_config(),
            reserved_v2: Reserved119::default(),
            active_proposal_count: 10,
            required_signatories_count: 0,
        }
    }

    fn create_test_v1_governance() -> GovernanceV1 {
        GovernanceV1 {
            account_type: GovernanceAccountType::GovernanceV1,
            realm: Pubkey::new_unique(),
            governed_account: Pubkey::new_unique(),
            proposals_count: 10,
            config: create_test_governance_config(),
        }
    }

    #[test]
    fn test_max_governance_size() {
        // Arrange
        let governance_data = create_test_governance();

        // Act
        let size = governance_data.try_to_vec().unwrap().len();

        // Assert
        assert_eq!(governance_data.get_max_size(), Some(size));
    }

    #[test]
    fn test_v1_governance_size() {
        // Arrange
        let governance = create_test_v1_governance();

        // Act
        let size = governance.try_to_vec().unwrap().len();

        // Assert
        assert_eq!(108, size);
    }

    #[test]
    fn test_deserialize_legacy_governance_account_without_council_vote_thresholds() {
        // Arrange

        // Legacy GovernanceV2 with
        // 1) config.community_vote_threshold = YesVotePercentage(10)
        // 2) config.proposal_cool_off_time = 0
        let mut account_data = [
            18, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 100, 0,
            0, 0, 1, //
            // proposal_cool_off_time:
            0, 0, 0, 0, // ---------
            1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        ];

        let program_id = Pubkey::new_unique();

        let info_key = Pubkey::new_unique();
        let mut lamports = 10u64;

        let governance_info = AccountInfo::new(
            &info_key,
            false,
            false,
            &mut lamports,
            &mut account_data[..],
            &program_id,
            false,
            Epoch::default(),
        );

        // Act
        let governance = get_governance_data(&program_id, &governance_info).unwrap();

        // Assert
        assert_eq!(
            governance.config.community_vote_threshold,
            governance.config.council_vote_threshold
        );

        assert_eq!(
            governance.config.council_vote_threshold,
            governance.config.council_veto_vote_threshold
        );

        assert_eq!(
            governance.config.council_vote_tipping,
            governance.config.community_vote_tipping
        );

        assert_eq!(
            governance.config.deposit_exempt_proposal_count,
            DEFAULT_DEPOSIT_EXEMPT_PROPOSAL_COUNT
        );
        assert_eq!(governance.config.voting_cool_off_time, 0);
    }

    #[test]
    fn test_assert_config_invalid_with_council_zero_yes_vote_threshold() {
        // Arrange
        let mut governance_config = create_test_governance_config();
        governance_config.council_vote_threshold = VoteThreshold::YesVotePercentage(0);

        // Act
        let err = assert_is_valid_governance_config(&governance_config)
            .err()
            .unwrap();

        // Assert
        assert_eq!(err, GovernanceError::InvalidVoteThresholdPercentage.into());
    }

    #[test]
    fn test_migrate_governance_config_from_legacy_data_to_program_v3() {
        // Arrange
        let mut governance_legacy_data = create_test_governance();

        governance_legacy_data.config.community_vote_threshold =
            VoteThreshold::YesVotePercentage(60);

        // council_vote_threshold == YesVotePercentage(0) indicates legacy account from
        // V1 & V2 program versions
        governance_legacy_data.config.council_vote_threshold = VoteThreshold::YesVotePercentage(0);

        governance_legacy_data.config.council_veto_vote_threshold =
            VoteThreshold::YesVotePercentage(0);
        governance_legacy_data.config.council_vote_tipping = VoteTipping::Disabled;
        governance_legacy_data.config.community_veto_vote_threshold =
            VoteThreshold::YesVotePercentage(0);
        governance_legacy_data.config.voting_cool_off_time = 1;
        governance_legacy_data.config.voting_base_time = 36000;

        let mut legacy_data = vec![];
        governance_legacy_data.serialize(&mut legacy_data).unwrap();

        let program_id = Pubkey::new_unique();

        let info_key = Pubkey::new_unique();
        let mut lamports = 10u64;

        let legacy_account_info = AccountInfo::new(
            &info_key,
            false,
            false,
            &mut lamports,
            &mut legacy_data[..],
            &program_id,
            false,
            Epoch::default(),
        );
        // Act
        let governance_program_v3 = get_governance_data(&program_id, &legacy_account_info).unwrap();

        // Assert
        assert_eq!(
            governance_program_v3.config.council_vote_threshold,
            VoteThreshold::YesVotePercentage(60)
        );

        assert_eq!(
            governance_program_v3.config.council_veto_vote_threshold,
            VoteThreshold::YesVotePercentage(60)
        );

        assert_eq!(
            governance_program_v3.config.community_veto_vote_threshold,
            VoteThreshold::Disabled
        );

        assert_eq!(
            governance_program_v3.config.council_vote_tipping,
            VoteTipping::Strict
        );

        assert_eq!(governance_program_v3.config.voting_cool_off_time, 0);

        assert_eq!(
            governance_program_v3.config.deposit_exempt_proposal_count,
            DEFAULT_DEPOSIT_EXEMPT_PROPOSAL_COUNT
        );
    }

    #[test]
    fn test_assert_config_invalid_with_community_zero_yes_vote_threshold() {
        // Arrange
        let mut governance_config = create_test_governance_config();
        governance_config.community_vote_threshold = VoteThreshold::YesVotePercentage(0);

        // Act
        let err = assert_is_valid_governance_config(&governance_config)
            .err()
            .unwrap();

        // Assert
        assert_eq!(err, GovernanceError::InvalidVoteThresholdPercentage.into());
    }

    #[test]
    fn test_assert_config_invalid_with_all_vote_thresholds_disabled() {
        // Arrange
        let mut governance_config = create_test_governance_config();
        governance_config.community_vote_threshold = VoteThreshold::Disabled;
        governance_config.council_vote_threshold = VoteThreshold::Disabled;

        // Act
        let err = assert_is_valid_governance_config(&governance_config)
            .err()
            .unwrap();

        // Assert
        assert_eq!(err, GovernanceError::AtLeastOneVoteThresholdRequired.into());
    }

    #[test]
    fn test_assert_config_invalid_with_council_zero_yes_veto_vote_threshold() {
        // Arrange
        let mut governance_config = create_test_governance_config();
        governance_config.council_veto_vote_threshold = VoteThreshold::YesVotePercentage(0);

        // Act
        let err = assert_is_valid_governance_config(&governance_config)
            .err()
            .unwrap();

        // Assert
        assert_eq!(err, GovernanceError::InvalidVoteThresholdPercentage.into());
    }

    #[test]
    fn test_assert_config_invalid_with_community_zero_yes_veto_vote_threshold() {
        // Arrange
        let mut governance_config = create_test_governance_config();
        governance_config.community_veto_vote_threshold = VoteThreshold::YesVotePercentage(0);

        // Act
        let err = assert_is_valid_governance_config(&governance_config)
            .err()
            .unwrap();

        // Assert
        assert_eq!(err, GovernanceError::InvalidVoteThresholdPercentage.into());
    }

    #[test]
    fn test_get_proposal_deposit_amount_for_exempt_proposal() {
        // Arrange
        let mut governance_data = create_test_governance();

        governance_data.active_proposal_count = 10;
        governance_data.config.deposit_exempt_proposal_count = 10;

        // Act
        let deposit_amount = governance_data.get_proposal_deposit_amount();

        // Assert
        assert_eq!(deposit_amount, 0);
    }

    #[test]
    fn test_get_proposal_deposit_amount_for_non_exempt_proposal() {
        // Arrange
        let mut governance_data = create_test_governance();

        governance_data.active_proposal_count = 100;
        governance_data.config.deposit_exempt_proposal_count = 10;

        // Act
        let deposit_amount = governance_data.get_proposal_deposit_amount();

        // Assert
        assert_eq!(deposit_amount, SECURITY_DEPOSIT_BASE_LAMPORTS * 90);
    }

    #[test]
    fn test_get_proposal_deposit_amount_without_exempt_proposal() {
        // Arrange
        let mut governance_data = create_test_governance();

        governance_data.active_proposal_count = 10;
        governance_data.config.deposit_exempt_proposal_count = 0;

        // Act
        let deposit_amount = governance_data.get_proposal_deposit_amount();

        // Assert
        assert_eq!(deposit_amount, SECURITY_DEPOSIT_BASE_LAMPORTS * 10);
    }

    #[test]
    fn test_assert_config_invalid_with_max_deposit_exempt_proposal_count() {
        // Arrange
        let mut governance_config = create_test_governance_config();
        governance_config.deposit_exempt_proposal_count = u8::MAX;

        // Act
        let err = assert_is_valid_governance_config(&governance_config)
            .err()
            .unwrap();

        // Assert
        assert_eq!(
            err,
            GovernanceError::InvalidDepositExemptProposalCount.into()
        );
    }
}