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
use {
solana_sdk::{
account::{Account, AccountSharedData},
feature::{self, Feature},
feature_set::FeatureSet,
fee_calculator::FeeRateGovernor,
genesis_config::{ClusterType, GenesisConfig},
pubkey::Pubkey,
rent::Rent,
signature::{Keypair, Signer},
stake::state::StakeState,
system_program,
},
solana_stake_program::stake_state,
solana_vote_program::vote_state,
std::borrow::Borrow,
};
const VALIDATOR_LAMPORTS: u64 = 42;
pub fn bootstrap_validator_stake_lamports() -> u64 {
Rent::default().minimum_balance(StakeState::size_of())
}
pub const fn genesis_sysvar_and_builtin_program_lamports() -> u64 {
const NUM_BUILTIN_PROGRAMS: u64 = 4;
const FEES_SYSVAR_MIN_BALANCE: u64 = 946_560;
const STAKE_HISTORY_MIN_BALANCE: u64 = 114_979_200;
const CLOCK_SYSVAR_MIN_BALANCE: u64 = 1_169_280;
const RENT_SYSVAR_MIN_BALANCE: u64 = 1_009_200;
const EPOCH_SCHEDULE_SYSVAR_MIN_BALANCE: u64 = 1_120_560;
const RECENT_BLOCKHASHES_SYSVAR_MIN_BALANCE: u64 = 42_706_560;
FEES_SYSVAR_MIN_BALANCE
+ STAKE_HISTORY_MIN_BALANCE
+ CLOCK_SYSVAR_MIN_BALANCE
+ RENT_SYSVAR_MIN_BALANCE
+ EPOCH_SCHEDULE_SYSVAR_MIN_BALANCE
+ RECENT_BLOCKHASHES_SYSVAR_MIN_BALANCE
+ NUM_BUILTIN_PROGRAMS
}
pub struct ValidatorVoteKeypairs {
pub node_keypair: Keypair,
pub vote_keypair: Keypair,
pub stake_keypair: Keypair,
}
impl ValidatorVoteKeypairs {
pub fn new(node_keypair: Keypair, vote_keypair: Keypair, stake_keypair: Keypair) -> Self {
Self {
node_keypair,
vote_keypair,
stake_keypair,
}
}
pub fn new_rand() -> Self {
Self {
node_keypair: Keypair::new(),
vote_keypair: Keypair::new(),
stake_keypair: Keypair::new(),
}
}
}
pub struct GenesisConfigInfo {
pub genesis_config: GenesisConfig,
pub mint_keypair: Keypair,
pub voting_keypair: Keypair,
pub validator_pubkey: Pubkey,
}
pub fn create_genesis_config(mint_lamports: u64) -> GenesisConfigInfo {
create_genesis_config_with_leader(
mint_lamports,
&solana_sdk::pubkey::new_rand(), 0, )
}
pub fn create_genesis_config_with_vote_accounts(
mint_lamports: u64,
voting_keypairs: &[impl Borrow<ValidatorVoteKeypairs>],
stakes: Vec<u64>,
) -> GenesisConfigInfo {
create_genesis_config_with_vote_accounts_and_cluster_type(
mint_lamports,
voting_keypairs,
stakes,
ClusterType::Development,
)
}
pub fn create_genesis_config_with_vote_accounts_and_cluster_type(
mint_lamports: u64,
voting_keypairs: &[impl Borrow<ValidatorVoteKeypairs>],
stakes: Vec<u64>,
cluster_type: ClusterType,
) -> GenesisConfigInfo {
assert!(!voting_keypairs.is_empty());
assert_eq!(voting_keypairs.len(), stakes.len());
let mint_keypair = Keypair::new();
let voting_keypair =
Keypair::from_bytes(&voting_keypairs[0].borrow().vote_keypair.to_bytes()).unwrap();
let validator_pubkey = voting_keypairs[0].borrow().node_keypair.pubkey();
let genesis_config = create_genesis_config_with_leader_ex(
mint_lamports,
&mint_keypair.pubkey(),
&validator_pubkey,
&voting_keypairs[0].borrow().vote_keypair.pubkey(),
&voting_keypairs[0].borrow().stake_keypair.pubkey(),
stakes[0],
VALIDATOR_LAMPORTS,
FeeRateGovernor::new(0, 0), Rent::free(), cluster_type,
vec![],
);
let mut genesis_config_info = GenesisConfigInfo {
genesis_config,
mint_keypair,
voting_keypair,
validator_pubkey,
};
for (validator_voting_keypairs, stake) in voting_keypairs[1..].iter().zip(&stakes[1..]) {
let node_pubkey = validator_voting_keypairs.borrow().node_keypair.pubkey();
let vote_pubkey = validator_voting_keypairs.borrow().vote_keypair.pubkey();
let stake_pubkey = validator_voting_keypairs.borrow().stake_keypair.pubkey();
let node_account = Account::new(VALIDATOR_LAMPORTS, 0, &system_program::id());
let vote_account = vote_state::create_account(&vote_pubkey, &node_pubkey, 0, *stake);
let stake_account = Account::from(stake_state::create_account(
&stake_pubkey,
&vote_pubkey,
&vote_account,
&genesis_config_info.genesis_config.rent,
*stake,
));
let vote_account = Account::from(vote_account);
genesis_config_info.genesis_config.accounts.extend(vec![
(node_pubkey, node_account),
(vote_pubkey, vote_account),
(stake_pubkey, stake_account),
]);
}
genesis_config_info
}
pub fn create_genesis_config_with_leader(
mint_lamports: u64,
validator_pubkey: &Pubkey,
validator_stake_lamports: u64,
) -> GenesisConfigInfo {
let mint_keypair = Keypair::new();
let voting_keypair = Keypair::new();
let genesis_config = create_genesis_config_with_leader_ex(
mint_lamports,
&mint_keypair.pubkey(),
validator_pubkey,
&voting_keypair.pubkey(),
&solana_sdk::pubkey::new_rand(),
validator_stake_lamports,
VALIDATOR_LAMPORTS,
FeeRateGovernor::new(0, 0), Rent::free(), ClusterType::Development,
vec![],
);
GenesisConfigInfo {
genesis_config,
mint_keypair,
voting_keypair,
validator_pubkey: *validator_pubkey,
}
}
pub fn activate_all_features(genesis_config: &mut GenesisConfig) {
for feature_id in FeatureSet::default().inactive {
activate_feature(genesis_config, feature_id);
}
}
pub fn activate_feature(genesis_config: &mut GenesisConfig, feature_id: Pubkey) {
genesis_config.accounts.insert(
feature_id,
Account::from(feature::create_account(
&Feature {
activated_at: Some(0),
},
std::cmp::max(genesis_config.rent.minimum_balance(Feature::size_of()), 1),
)),
);
}
#[allow(clippy::too_many_arguments)]
pub fn create_genesis_config_with_leader_ex(
mint_lamports: u64,
mint_pubkey: &Pubkey,
validator_pubkey: &Pubkey,
validator_vote_account_pubkey: &Pubkey,
validator_stake_account_pubkey: &Pubkey,
validator_stake_lamports: u64,
validator_lamports: u64,
fee_rate_governor: FeeRateGovernor,
rent: Rent,
cluster_type: ClusterType,
mut initial_accounts: Vec<(Pubkey, AccountSharedData)>,
) -> GenesisConfig {
let validator_vote_account = vote_state::create_account(
validator_vote_account_pubkey,
validator_pubkey,
0,
validator_stake_lamports,
);
let validator_stake_account = stake_state::create_account(
validator_stake_account_pubkey,
validator_vote_account_pubkey,
&validator_vote_account,
&rent,
validator_stake_lamports,
);
initial_accounts.push((
*mint_pubkey,
AccountSharedData::new(mint_lamports, 0, &system_program::id()),
));
initial_accounts.push((
*validator_pubkey,
AccountSharedData::new(validator_lamports, 0, &system_program::id()),
));
initial_accounts.push((*validator_vote_account_pubkey, validator_vote_account));
initial_accounts.push((*validator_stake_account_pubkey, validator_stake_account));
let mut genesis_config = GenesisConfig {
accounts: initial_accounts
.iter()
.cloned()
.map(|(key, account)| (key, Account::from(account)))
.collect(),
fee_rate_governor,
rent,
cluster_type,
..GenesisConfig::default()
};
solana_stake_program::add_genesis_accounts(&mut genesis_config);
if genesis_config.cluster_type == ClusterType::Development {
activate_all_features(&mut genesis_config);
}
genesis_config
}