2019-06-26 10:13:21 -07:00
|
|
|
use solana_sdk::{
|
|
|
|
account::Account,
|
2021-03-09 13:06:07 -08:00
|
|
|
account::AccountSharedData,
|
2020-10-30 13:40:55 -07:00
|
|
|
feature::{self, Feature},
|
|
|
|
feature_set::FeatureSet,
|
2020-02-28 12:27:01 -08:00
|
|
|
fee_calculator::FeeRateGovernor,
|
2020-09-21 14:03:35 -07:00
|
|
|
genesis_config::{ClusterType, GenesisConfig},
|
2019-06-26 10:13:21 -07:00
|
|
|
pubkey::Pubkey,
|
2019-11-12 12:33:40 -08:00
|
|
|
rent::Rent,
|
2020-02-20 13:28:55 -08:00
|
|
|
signature::{Keypair, Signer},
|
2021-06-15 09:04:00 -07:00
|
|
|
stake::state::StakeState,
|
2020-04-27 21:05:12 -07:00
|
|
|
system_program,
|
2019-06-26 10:13:21 -07:00
|
|
|
};
|
2019-11-20 10:12:43 -08:00
|
|
|
use solana_stake_program::stake_state;
|
|
|
|
use solana_vote_program::vote_state;
|
2020-08-19 22:04:38 -07:00
|
|
|
use std::borrow::Borrow;
|
2019-05-07 22:22:43 -07:00
|
|
|
|
2020-11-25 17:00:47 -08:00
|
|
|
// Default amount received by the validator
|
|
|
|
const VALIDATOR_LAMPORTS: u64 = 42;
|
2019-05-07 22:22:43 -07:00
|
|
|
|
2020-11-20 00:21:03 -08:00
|
|
|
// fun fact: rustc is very close to make this const fn.
|
|
|
|
pub fn bootstrap_validator_stake_lamports() -> u64 {
|
|
|
|
StakeState::get_rent_exempt_reserve(&Rent::default())
|
|
|
|
}
|
|
|
|
|
2020-02-18 02:39:47 -08:00
|
|
|
pub struct ValidatorVoteKeypairs {
|
2020-08-19 22:04:38 -07:00
|
|
|
pub node_keypair: Keypair,
|
|
|
|
pub vote_keypair: Keypair,
|
|
|
|
pub stake_keypair: Keypair,
|
2020-02-18 02:39:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ValidatorVoteKeypairs {
|
2020-08-19 22:04:38 -07:00
|
|
|
pub fn new(node_keypair: Keypair, vote_keypair: Keypair, stake_keypair: Keypair) -> Self {
|
2020-02-18 02:39:47 -08:00
|
|
|
Self {
|
|
|
|
node_keypair,
|
|
|
|
vote_keypair,
|
|
|
|
stake_keypair,
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 14:07:21 -07:00
|
|
|
|
|
|
|
pub fn new_rand() -> Self {
|
|
|
|
Self {
|
2020-08-19 22:04:38 -07:00
|
|
|
node_keypair: Keypair::new(),
|
|
|
|
vote_keypair: Keypair::new(),
|
|
|
|
stake_keypair: Keypair::new(),
|
2020-05-05 14:07:21 -07:00
|
|
|
}
|
|
|
|
}
|
2020-02-18 02:39:47 -08:00
|
|
|
}
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
pub struct GenesisConfigInfo {
|
|
|
|
pub genesis_config: GenesisConfig,
|
2019-05-22 20:39:00 -07:00
|
|
|
pub mint_keypair: Keypair,
|
2020-08-19 22:04:38 -07:00
|
|
|
pub voting_keypair: Keypair,
|
2019-05-22 20:39:00 -07:00
|
|
|
}
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
pub fn create_genesis_config(mint_lamports: u64) -> GenesisConfigInfo {
|
2020-10-19 12:12:08 -07:00
|
|
|
create_genesis_config_with_leader(mint_lamports, &solana_sdk::pubkey::new_rand(), 0)
|
2019-06-11 10:27:22 -07:00
|
|
|
}
|
|
|
|
|
2020-02-18 02:39:47 -08:00
|
|
|
pub fn create_genesis_config_with_vote_accounts(
|
|
|
|
mint_lamports: u64,
|
|
|
|
voting_keypairs: &[impl Borrow<ValidatorVoteKeypairs>],
|
2020-07-23 18:50:42 -07:00
|
|
|
stakes: Vec<u64>,
|
2020-09-24 12:23:09 -07:00
|
|
|
) -> 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,
|
2020-02-18 02:39:47 -08:00
|
|
|
) -> GenesisConfigInfo {
|
2020-08-19 22:04:38 -07:00
|
|
|
assert!(!voting_keypairs.is_empty());
|
2020-07-23 18:50:42 -07:00
|
|
|
assert_eq!(voting_keypairs.len(), stakes.len());
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
let mint_keypair = Keypair::new();
|
|
|
|
let voting_keypair =
|
|
|
|
Keypair::from_bytes(&voting_keypairs[0].borrow().vote_keypair.to_bytes()).unwrap();
|
|
|
|
|
|
|
|
let genesis_config = create_genesis_config_with_leader_ex(
|
2020-07-23 18:50:42 -07:00
|
|
|
mint_lamports,
|
2020-12-08 23:18:27 -08:00
|
|
|
&mint_keypair.pubkey(),
|
2020-07-23 18:50:42 -07:00
|
|
|
&voting_keypairs[0].borrow().node_keypair.pubkey(),
|
2020-12-08 23:18:27 -08:00
|
|
|
&voting_keypairs[0].borrow().vote_keypair.pubkey(),
|
2020-08-19 22:04:38 -07:00
|
|
|
&voting_keypairs[0].borrow().stake_keypair.pubkey(),
|
2020-07-23 18:50:42 -07:00
|
|
|
stakes[0],
|
2020-11-25 17:00:47 -08:00
|
|
|
VALIDATOR_LAMPORTS,
|
2020-12-08 23:18:27 -08:00
|
|
|
FeeRateGovernor::new(0, 0), // most tests can't handle transaction fees
|
|
|
|
Rent::free(), // most tests don't expect rent
|
2020-09-24 12:23:09 -07:00
|
|
|
cluster_type,
|
2020-12-08 23:18:27 -08:00
|
|
|
vec![],
|
2020-07-23 18:50:42 -07:00
|
|
|
);
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
let mut genesis_config_info = GenesisConfigInfo {
|
|
|
|
genesis_config,
|
|
|
|
mint_keypair,
|
2021-04-18 10:27:36 -07:00
|
|
|
voting_keypair,
|
2020-12-08 23:18:27 -08:00
|
|
|
};
|
|
|
|
|
2020-08-19 22:04:38 -07:00
|
|
|
for (validator_voting_keypairs, stake) in voting_keypairs[1..].iter().zip(&stakes[1..]) {
|
2020-02-18 02:39:47 -08:00
|
|
|
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();
|
|
|
|
|
|
|
|
// Create accounts
|
2020-11-25 17:00:47 -08:00
|
|
|
let node_account = Account::new(VALIDATOR_LAMPORTS, 0, &system_program::id());
|
2020-08-19 22:04:38 -07:00
|
|
|
let vote_account = vote_state::create_account(&vote_pubkey, &node_pubkey, 0, *stake);
|
2021-03-09 13:06:07 -08:00
|
|
|
let stake_account = Account::from(stake_state::create_account(
|
2020-02-18 02:39:47 -08:00
|
|
|
&stake_pubkey,
|
|
|
|
&vote_pubkey,
|
|
|
|
&vote_account,
|
|
|
|
&genesis_config_info.genesis_config.rent,
|
2020-08-19 22:04:38 -07:00
|
|
|
*stake,
|
2021-03-09 13:06:07 -08:00
|
|
|
));
|
|
|
|
|
|
|
|
let vote_account = Account::from(vote_account);
|
2020-02-18 02:39:47 -08:00
|
|
|
|
|
|
|
// Put newly created accounts into genesis
|
|
|
|
genesis_config_info.genesis_config.accounts.extend(vec![
|
2020-07-23 18:50:42 -07:00
|
|
|
(node_pubkey, node_account),
|
|
|
|
(vote_pubkey, vote_account),
|
2020-02-18 02:39:47 -08:00
|
|
|
(stake_pubkey, stake_account),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
genesis_config_info
|
|
|
|
}
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
pub fn create_genesis_config_with_leader(
|
2019-05-07 22:22:43 -07:00
|
|
|
mint_lamports: u64,
|
2020-11-25 17:00:47 -08:00
|
|
|
validator_pubkey: &Pubkey,
|
|
|
|
validator_stake_lamports: u64,
|
2020-02-07 08:14:27 -08:00
|
|
|
) -> GenesisConfigInfo {
|
2020-12-08 23:18:27 -08:00
|
|
|
let mint_keypair = Keypair::new();
|
|
|
|
let voting_keypair = Keypair::new();
|
|
|
|
|
|
|
|
let genesis_config = create_genesis_config_with_leader_ex(
|
2020-02-07 08:14:27 -08:00
|
|
|
mint_lamports,
|
2020-12-08 23:18:27 -08:00
|
|
|
&mint_keypair.pubkey(),
|
2020-11-25 17:00:47 -08:00
|
|
|
validator_pubkey,
|
2020-12-08 23:18:27 -08:00
|
|
|
&voting_keypair.pubkey(),
|
2020-10-19 12:12:08 -07:00
|
|
|
&solana_sdk::pubkey::new_rand(),
|
2020-11-25 17:00:47 -08:00
|
|
|
validator_stake_lamports,
|
|
|
|
VALIDATOR_LAMPORTS,
|
2020-12-08 23:18:27 -08:00
|
|
|
FeeRateGovernor::new(0, 0), // most tests can't handle transaction fees
|
|
|
|
Rent::free(), // most tests don't expect rent
|
2020-09-24 12:23:09 -07:00
|
|
|
ClusterType::Development,
|
2020-12-08 23:18:27 -08:00
|
|
|
vec![],
|
|
|
|
);
|
|
|
|
|
|
|
|
GenesisConfigInfo {
|
|
|
|
genesis_config,
|
|
|
|
mint_keypair,
|
2021-04-18 10:27:36 -07:00
|
|
|
voting_keypair,
|
2020-12-08 23:18:27 -08:00
|
|
|
}
|
2020-02-07 08:14:27 -08:00
|
|
|
}
|
|
|
|
|
2020-09-24 12:23:09 -07:00
|
|
|
pub fn activate_all_features(genesis_config: &mut GenesisConfig) {
|
|
|
|
// Activate all features at genesis in development mode
|
|
|
|
for feature_id in FeatureSet::default().inactive {
|
|
|
|
genesis_config.accounts.insert(
|
|
|
|
feature_id,
|
2021-03-09 13:06:07 -08:00
|
|
|
Account::from(feature::create_account(
|
2020-10-30 13:40:55 -07:00
|
|
|
&Feature {
|
|
|
|
activated_at: Some(0),
|
|
|
|
},
|
|
|
|
std::cmp::max(genesis_config.rent.minimum_balance(Feature::size_of()), 1),
|
2021-03-09 13:06:07 -08:00
|
|
|
)),
|
2020-09-24 12:23:09 -07:00
|
|
|
);
|
2020-09-21 14:03:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-02-07 08:14:27 -08:00
|
|
|
pub fn create_genesis_config_with_leader_ex(
|
|
|
|
mint_lamports: u64,
|
2020-12-08 23:18:27 -08:00
|
|
|
mint_pubkey: &Pubkey,
|
2020-11-25 17:00:47 -08:00
|
|
|
validator_pubkey: &Pubkey,
|
2020-12-08 23:18:27 -08:00
|
|
|
validator_vote_account_pubkey: &Pubkey,
|
2020-11-25 17:00:47 -08:00
|
|
|
validator_stake_account_pubkey: &Pubkey,
|
|
|
|
validator_stake_lamports: u64,
|
|
|
|
validator_lamports: u64,
|
2020-12-08 23:18:27 -08:00
|
|
|
fee_rate_governor: FeeRateGovernor,
|
|
|
|
rent: Rent,
|
2020-09-24 12:23:09 -07:00
|
|
|
cluster_type: ClusterType,
|
2021-03-09 13:06:07 -08:00
|
|
|
mut initial_accounts: Vec<(Pubkey, AccountSharedData)>,
|
2020-12-08 23:18:27 -08:00
|
|
|
) -> GenesisConfig {
|
2020-11-25 17:00:47 -08:00
|
|
|
let validator_vote_account = vote_state::create_account(
|
2020-12-08 23:18:27 -08:00
|
|
|
&validator_vote_account_pubkey,
|
2020-11-25 17:00:47 -08:00
|
|
|
&validator_pubkey,
|
2019-05-07 22:22:43 -07:00
|
|
|
0,
|
2020-11-25 17:00:47 -08:00
|
|
|
validator_stake_lamports,
|
2019-05-07 22:22:43 -07:00
|
|
|
);
|
|
|
|
|
2020-11-25 17:00:47 -08:00
|
|
|
let validator_stake_account = stake_state::create_account(
|
|
|
|
validator_stake_account_pubkey,
|
2020-12-08 23:18:27 -08:00
|
|
|
&validator_vote_account_pubkey,
|
2020-11-25 17:00:47 -08:00
|
|
|
&validator_vote_account,
|
2019-11-12 12:33:40 -08:00
|
|
|
&rent,
|
2020-11-25 17:00:47 -08:00
|
|
|
validator_stake_lamports,
|
2019-08-15 18:58:46 -07:00
|
|
|
);
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
initial_accounts.push((
|
|
|
|
*mint_pubkey,
|
2021-03-09 13:06:07 -08:00
|
|
|
AccountSharedData::new(mint_lamports, 0, &system_program::id()),
|
2020-12-08 23:18:27 -08:00
|
|
|
));
|
|
|
|
initial_accounts.push((
|
|
|
|
*validator_pubkey,
|
2021-03-09 13:06:07 -08:00
|
|
|
AccountSharedData::new(validator_lamports, 0, &system_program::id()),
|
2020-12-08 23:18:27 -08:00
|
|
|
));
|
|
|
|
initial_accounts.push((*validator_vote_account_pubkey, validator_vote_account));
|
|
|
|
initial_accounts.push((*validator_stake_account_pubkey, validator_stake_account));
|
2019-06-20 12:22:29 -07:00
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let mut genesis_config = GenesisConfig {
|
2021-03-09 13:06:07 -08:00
|
|
|
accounts: initial_accounts
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.map(|(key, account)| (key, Account::from(account)))
|
|
|
|
.collect(),
|
2020-02-28 12:27:01 -08:00
|
|
|
fee_rate_governor,
|
2019-11-12 12:33:40 -08:00
|
|
|
rent,
|
2020-09-24 12:23:09 -07:00
|
|
|
cluster_type,
|
2019-11-08 20:56:57 -08:00
|
|
|
..GenesisConfig::default()
|
2019-10-15 12:52:44 -07:00
|
|
|
};
|
|
|
|
|
2019-11-20 10:12:43 -08:00
|
|
|
solana_stake_program::add_genesis_accounts(&mut genesis_config);
|
2020-09-24 12:23:09 -07:00
|
|
|
if genesis_config.cluster_type == ClusterType::Development {
|
|
|
|
activate_all_features(&mut genesis_config);
|
|
|
|
}
|
2019-05-07 22:22:43 -07:00
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
genesis_config
|
2019-05-07 22:22:43 -07:00
|
|
|
}
|