gemachain_runtime/
genesis_utils.rs

1use gemachain_sdk::{
2    account::Account,
3    account::AccountSharedData,
4    feature::{self, Feature},
5    feature_set::FeatureSet,
6    fee_calculator::FeeRateGovernor,
7    genesis_config::{ClusterType, GenesisConfig},
8    pubkey::Pubkey,
9    rent::Rent,
10    signature::{Keypair, Signer},
11    stake::state::StakeState,
12    system_program,
13};
14use gemachain_stake_program::stake_state;
15use gemachain_vote_program::vote_state;
16use std::borrow::Borrow;
17
18// Default amount received by the validator
19const VALIDATOR_CARATS: u64 = 42;
20
21// fun fact: rustc is very close to make this const fn.
22pub fn bootstrap_validator_stake_carats() -> u64 {
23    StakeState::get_rent_exempt_reserve(&Rent::default())
24}
25
26pub struct ValidatorVoteKeypairs {
27    pub node_keypair: Keypair,
28    pub vote_keypair: Keypair,
29    pub stake_keypair: Keypair,
30}
31
32impl ValidatorVoteKeypairs {
33    pub fn new(node_keypair: Keypair, vote_keypair: Keypair, stake_keypair: Keypair) -> Self {
34        Self {
35            node_keypair,
36            vote_keypair,
37            stake_keypair,
38        }
39    }
40
41    pub fn new_rand() -> Self {
42        Self {
43            node_keypair: Keypair::new(),
44            vote_keypair: Keypair::new(),
45            stake_keypair: Keypair::new(),
46        }
47    }
48}
49
50pub struct GenesisConfigInfo {
51    pub genesis_config: GenesisConfig,
52    pub mint_keypair: Keypair,
53    pub voting_keypair: Keypair,
54}
55
56pub fn create_genesis_config(mint_carats: u64) -> GenesisConfigInfo {
57    create_genesis_config_with_leader(mint_carats, &gemachain_sdk::pubkey::new_rand(), 0)
58}
59
60pub fn create_genesis_config_with_vote_accounts(
61    mint_carats: u64,
62    voting_keypairs: &[impl Borrow<ValidatorVoteKeypairs>],
63    stakes: Vec<u64>,
64) -> GenesisConfigInfo {
65    create_genesis_config_with_vote_accounts_and_cluster_type(
66        mint_carats,
67        voting_keypairs,
68        stakes,
69        ClusterType::Development,
70    )
71}
72
73pub fn create_genesis_config_with_vote_accounts_and_cluster_type(
74    mint_carats: u64,
75    voting_keypairs: &[impl Borrow<ValidatorVoteKeypairs>],
76    stakes: Vec<u64>,
77    cluster_type: ClusterType,
78) -> GenesisConfigInfo {
79    assert!(!voting_keypairs.is_empty());
80    assert_eq!(voting_keypairs.len(), stakes.len());
81
82    let mint_keypair = Keypair::new();
83    let voting_keypair =
84        Keypair::from_bytes(&voting_keypairs[0].borrow().vote_keypair.to_bytes()).unwrap();
85
86    let genesis_config = create_genesis_config_with_leader_ex(
87        mint_carats,
88        &mint_keypair.pubkey(),
89        &voting_keypairs[0].borrow().node_keypair.pubkey(),
90        &voting_keypairs[0].borrow().vote_keypair.pubkey(),
91        &voting_keypairs[0].borrow().stake_keypair.pubkey(),
92        stakes[0],
93        VALIDATOR_CARATS,
94        FeeRateGovernor::new(0, 0), // most tests can't handle transaction fees
95        Rent::free(),               // most tests don't expect rent
96        cluster_type,
97        vec![],
98    );
99
100    let mut genesis_config_info = GenesisConfigInfo {
101        genesis_config,
102        mint_keypair,
103        voting_keypair,
104    };
105
106    for (validator_voting_keypairs, stake) in voting_keypairs[1..].iter().zip(&stakes[1..]) {
107        let node_pubkey = validator_voting_keypairs.borrow().node_keypair.pubkey();
108        let vote_pubkey = validator_voting_keypairs.borrow().vote_keypair.pubkey();
109        let stake_pubkey = validator_voting_keypairs.borrow().stake_keypair.pubkey();
110
111        // Create accounts
112        let node_account = Account::new(VALIDATOR_CARATS, 0, &system_program::id());
113        let vote_account = vote_state::create_account(&vote_pubkey, &node_pubkey, 0, *stake);
114        let stake_account = Account::from(stake_state::create_account(
115            &stake_pubkey,
116            &vote_pubkey,
117            &vote_account,
118            &genesis_config_info.genesis_config.rent,
119            *stake,
120        ));
121
122        let vote_account = Account::from(vote_account);
123
124        // Put newly created accounts into genesis
125        genesis_config_info.genesis_config.accounts.extend(vec![
126            (node_pubkey, node_account),
127            (vote_pubkey, vote_account),
128            (stake_pubkey, stake_account),
129        ]);
130    }
131
132    genesis_config_info
133}
134
135pub fn create_genesis_config_with_leader(
136    mint_carats: u64,
137    validator_pubkey: &Pubkey,
138    validator_stake_carats: u64,
139) -> GenesisConfigInfo {
140    let mint_keypair = Keypair::new();
141    let voting_keypair = Keypair::new();
142
143    let genesis_config = create_genesis_config_with_leader_ex(
144        mint_carats,
145        &mint_keypair.pubkey(),
146        validator_pubkey,
147        &voting_keypair.pubkey(),
148        &gemachain_sdk::pubkey::new_rand(),
149        validator_stake_carats,
150        VALIDATOR_CARATS,
151        FeeRateGovernor::new(0, 0), // most tests can't handle transaction fees
152        Rent::free(),               // most tests don't expect rent
153        ClusterType::Development,
154        vec![],
155    );
156
157    GenesisConfigInfo {
158        genesis_config,
159        mint_keypair,
160        voting_keypair,
161    }
162}
163
164pub fn activate_all_features(genesis_config: &mut GenesisConfig) {
165    // Activate all features at genesis in development mode
166    for feature_id in FeatureSet::default().inactive {
167        genesis_config.accounts.insert(
168            feature_id,
169            Account::from(feature::create_account(
170                &Feature {
171                    activated_at: Some(0),
172                },
173                std::cmp::max(genesis_config.rent.minimum_balance(Feature::size_of()), 1),
174            )),
175        );
176    }
177}
178
179#[allow(clippy::too_many_arguments)]
180pub fn create_genesis_config_with_leader_ex(
181    mint_carats: u64,
182    mint_pubkey: &Pubkey,
183    validator_pubkey: &Pubkey,
184    validator_vote_account_pubkey: &Pubkey,
185    validator_stake_account_pubkey: &Pubkey,
186    validator_stake_carats: u64,
187    validator_carats: u64,
188    fee_rate_governor: FeeRateGovernor,
189    rent: Rent,
190    cluster_type: ClusterType,
191    mut initial_accounts: Vec<(Pubkey, AccountSharedData)>,
192) -> GenesisConfig {
193    let validator_vote_account = vote_state::create_account(
194        validator_vote_account_pubkey,
195        validator_pubkey,
196        0,
197        validator_stake_carats,
198    );
199
200    let validator_stake_account = stake_state::create_account(
201        validator_stake_account_pubkey,
202        validator_vote_account_pubkey,
203        &validator_vote_account,
204        &rent,
205        validator_stake_carats,
206    );
207
208    initial_accounts.push((
209        *mint_pubkey,
210        AccountSharedData::new(mint_carats, 0, &system_program::id()),
211    ));
212    initial_accounts.push((
213        *validator_pubkey,
214        AccountSharedData::new(validator_carats, 0, &system_program::id()),
215    ));
216    initial_accounts.push((*validator_vote_account_pubkey, validator_vote_account));
217    initial_accounts.push((*validator_stake_account_pubkey, validator_stake_account));
218
219    let mut genesis_config = GenesisConfig {
220        accounts: initial_accounts
221            .iter()
222            .cloned()
223            .map(|(key, account)| (key, Account::from(account)))
224            .collect(),
225        fee_rate_governor,
226        rent,
227        cluster_type,
228        ..GenesisConfig::default()
229    };
230
231    gemachain_stake_program::add_genesis_accounts(&mut genesis_config);
232    if genesis_config.cluster_type == ClusterType::Development {
233        activate_all_features(&mut genesis_config);
234    }
235
236    genesis_config
237}