| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  | //! The `genesis_config` module is a library for generating the chain's genesis config.
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-08 22:34:26 -07:00
										 |  |  | use crate::{
 | 
					
						
							|  |  |  |     account::Account,
 | 
					
						
							| 
									
										
										
										
											2020-05-14 18:22:47 -07:00
										 |  |  |     clock::{UnixTimestamp, DEFAULT_TICKS_PER_SLOT},
 | 
					
						
							| 
									
										
										
										
											2019-10-08 22:34:26 -07:00
										 |  |  |     epoch_schedule::EpochSchedule,
 | 
					
						
							| 
									
										
										
										
											2020-02-28 13:27:01 -07:00
										 |  |  |     fee_calculator::FeeRateGovernor,
 | 
					
						
							| 
									
										
										
										
											2019-10-08 22:34:26 -07:00
										 |  |  |     hash::{hash, Hash},
 | 
					
						
							|  |  |  |     inflation::Inflation,
 | 
					
						
							| 
									
										
										
										
											2020-02-24 10:18:08 -07:00
										 |  |  |     native_token::lamports_to_sol,
 | 
					
						
							| 
									
										
										
										
											2019-10-08 22:34:26 -07:00
										 |  |  |     poh_config::PohConfig,
 | 
					
						
							|  |  |  |     pubkey::Pubkey,
 | 
					
						
							| 
									
										
										
										
											2019-10-30 16:25:12 -07:00
										 |  |  |     rent::Rent,
 | 
					
						
							| 
									
										
										
										
											2020-02-24 10:18:08 -07:00
										 |  |  |     shred_version::compute_shred_version,
 | 
					
						
							| 
									
										
										
										
											2020-02-20 14:28:55 -07:00
										 |  |  |     signature::{Keypair, Signer},
 | 
					
						
							| 
									
										
										
										
											2020-04-27 21:05:12 -07:00
										 |  |  |     system_program,
 | 
					
						
							| 
									
										
										
										
											2019-10-08 22:34:26 -07:00
										 |  |  | };
 | 
					
						
							| 
									
										
										
										
											2019-06-14 14:22:52 -07:00
										 |  |  | use bincode::{deserialize, serialize};
 | 
					
						
							| 
									
										
										
										
											2020-02-24 10:18:08 -07:00
										 |  |  | use chrono::{TimeZone, Utc};
 | 
					
						
							| 
									
										
										
										
											2019-06-14 14:22:52 -07:00
										 |  |  | use memmap::Mmap;
 | 
					
						
							| 
									
										
										
										
											2019-10-08 22:34:26 -07:00
										 |  |  | use std::{
 | 
					
						
							| 
									
										
										
										
											2019-11-26 19:23:37 -08:00
										 |  |  |     collections::BTreeMap,
 | 
					
						
							| 
									
										
										
										
											2020-02-24 10:18:08 -07:00
										 |  |  |     fmt,
 | 
					
						
							| 
									
										
										
										
											2019-10-08 22:34:26 -07:00
										 |  |  |     fs::{File, OpenOptions},
 | 
					
						
							|  |  |  |     io::Write,
 | 
					
						
							| 
									
										
										
										
											2019-11-12 10:24:49 -07:00
										 |  |  |     path::{Path, PathBuf},
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:03:43 -08:00
										 |  |  |     time::{SystemTime, UNIX_EPOCH},
 | 
					
						
							| 
									
										
										
										
											2019-10-08 22:34:26 -07:00
										 |  |  | };
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 18:22:47 -07:00
										 |  |  | // deprecated default that is no longer used
 | 
					
						
							|  |  |  | pub const UNUSED_DEFAULT: u64 = 1024;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 13:58:01 -07:00
										 |  |  | #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
 | 
					
						
							| 
									
										
										
										
											2019-10-23 11:50:10 -07:00
										 |  |  | pub enum OperatingMode {
 | 
					
						
							| 
									
										
										
										
											2020-02-19 17:48:58 -07:00
										 |  |  |     Preview,     // Next set of cluster features to be promoted to Stable
 | 
					
						
							| 
									
										
										
										
											2020-02-20 17:23:29 -07:00
										 |  |  |     Stable,      // Stable cluster features
 | 
					
						
							| 
									
										
										
										
											2020-02-19 17:48:58 -07:00
										 |  |  |     Development, // All features (including experimental features)
 | 
					
						
							| 
									
										
										
										
											2019-10-23 11:50:10 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 15:40:39 -07:00
										 |  |  | #[derive(Serialize, Deserialize, Debug, Clone)]
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  | pub struct GenesisConfig {
 | 
					
						
							| 
									
										
										
										
											2020-01-22 09:22:09 -07:00
										 |  |  |     /// when the network (bootstrap validator) was started relative to the UNIX Epoch
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:03:43 -08:00
										 |  |  |     pub creation_time: UnixTimestamp,
 | 
					
						
							|  |  |  |     /// initial accounts
 | 
					
						
							| 
									
										
										
										
											2019-11-26 19:23:37 -08:00
										 |  |  |     pub accounts: BTreeMap<Pubkey, Account>,
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:03:43 -08:00
										 |  |  |     /// built-in programs
 | 
					
						
							| 
									
										
										
										
											2020-04-08 14:36:18 -07:00
										 |  |  |     pub native_instruction_processors: Vec<(String, Pubkey)>,
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:03:43 -08:00
										 |  |  |     /// accounts for network rewards, these do not count towards capitalization
 | 
					
						
							| 
									
										
										
										
											2019-11-26 19:23:37 -08:00
										 |  |  |     pub rewards_pools: BTreeMap<Pubkey, Account>,
 | 
					
						
							| 
									
										
										
										
											2019-06-19 15:40:39 -07:00
										 |  |  |     pub ticks_per_slot: u64,
 | 
					
						
							| 
									
										
										
										
											2020-05-14 18:22:47 -07:00
										 |  |  |     pub unused: u64,
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:03:43 -08:00
										 |  |  |     /// network speed configuration
 | 
					
						
							| 
									
										
										
										
											2019-05-18 14:01:36 -07:00
										 |  |  |     pub poh_config: PohConfig,
 | 
					
						
							| 
									
										
										
										
											2020-03-04 01:04:44 -07:00
										 |  |  |     /// this field exists only to ensure that the binary layout of GenesisConfig remains compatible
 | 
					
						
							|  |  |  |     /// with the Solana v0.23 release line
 | 
					
						
							|  |  |  |     pub __backwards_compat_with_v0_23: u64,
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:03:43 -08:00
										 |  |  |     /// transaction fee config
 | 
					
						
							| 
									
										
										
										
											2020-02-28 13:27:01 -07:00
										 |  |  |     pub fee_rate_governor: FeeRateGovernor,
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:03:43 -08:00
										 |  |  |     /// rent config
 | 
					
						
							| 
									
										
										
										
											2019-10-30 16:25:12 -07:00
										 |  |  |     pub rent: Rent,
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:03:43 -08:00
										 |  |  |     /// inflation config
 | 
					
						
							| 
									
										
										
										
											2019-06-11 21:42:31 -07:00
										 |  |  |     pub inflation: Inflation,
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:03:43 -08:00
										 |  |  |     /// how slots map to epochs
 | 
					
						
							| 
									
										
										
										
											2019-10-08 22:34:26 -07:00
										 |  |  |     pub epoch_schedule: EpochSchedule,
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:03:43 -08:00
										 |  |  |     /// network runlevel
 | 
					
						
							| 
									
										
										
										
											2019-10-23 11:50:10 -07:00
										 |  |  |     pub operating_mode: OperatingMode,
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  | // useful for basic tests
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  | pub fn create_genesis_config(lamports: u64) -> (GenesisConfig, Keypair) {
 | 
					
						
							| 
									
										
										
										
											2019-11-15 14:50:26 -07:00
										 |  |  |     let faucet_keypair = Keypair::new();
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  |     (
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  |         GenesisConfig::new(
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  |             &[(
 | 
					
						
							| 
									
										
										
										
											2019-11-15 14:50:26 -07:00
										 |  |  |                 faucet_keypair.pubkey(),
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  |                 Account::new(lamports, 0, &system_program::id()),
 | 
					
						
							|  |  |  |             )],
 | 
					
						
							| 
									
										
										
										
											2020-04-27 21:05:12 -07:00
										 |  |  |             &[],
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  |         ),
 | 
					
						
							| 
									
										
										
										
											2019-11-15 14:50:26 -07:00
										 |  |  |         faucet_keypair,
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  |     )
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  | impl Default for GenesisConfig {
 | 
					
						
							| 
									
										
										
										
											2019-06-11 17:04:13 -07:00
										 |  |  |     fn default() -> Self {
 | 
					
						
							|  |  |  |         Self {
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:03:43 -08:00
										 |  |  |             creation_time: SystemTime::now()
 | 
					
						
							|  |  |  |                 .duration_since(UNIX_EPOCH)
 | 
					
						
							|  |  |  |                 .unwrap()
 | 
					
						
							|  |  |  |                 .as_secs() as UnixTimestamp,
 | 
					
						
							| 
									
										
										
										
											2019-11-26 19:23:37 -08:00
										 |  |  |             accounts: BTreeMap::default(),
 | 
					
						
							|  |  |  |             native_instruction_processors: Vec::default(),
 | 
					
						
							|  |  |  |             rewards_pools: BTreeMap::default(),
 | 
					
						
							| 
									
										
										
										
											2019-06-11 17:04:13 -07:00
										 |  |  |             ticks_per_slot: DEFAULT_TICKS_PER_SLOT,
 | 
					
						
							| 
									
										
										
										
											2020-05-14 18:22:47 -07:00
										 |  |  |             unused: UNUSED_DEFAULT,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 17:04:13 -07:00
										 |  |  |             poh_config: PohConfig::default(),
 | 
					
						
							| 
									
										
										
										
											2019-06-11 21:42:31 -07:00
										 |  |  |             inflation: Inflation::default(),
 | 
					
						
							| 
									
										
										
										
											2020-03-04 01:04:44 -07:00
										 |  |  |             __backwards_compat_with_v0_23: 0,
 | 
					
						
							| 
									
										
										
										
											2020-02-28 13:27:01 -07:00
										 |  |  |             fee_rate_governor: FeeRateGovernor::default(),
 | 
					
						
							| 
									
										
										
										
											2019-10-30 16:25:12 -07:00
										 |  |  |             rent: Rent::default(),
 | 
					
						
							| 
									
										
										
										
											2019-10-08 22:34:26 -07:00
										 |  |  |             epoch_schedule: EpochSchedule::default(),
 | 
					
						
							| 
									
										
										
										
											2019-10-23 11:50:10 -07:00
										 |  |  |             operating_mode: OperatingMode::Development,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 17:04:13 -07:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  | impl GenesisConfig {
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  |     pub fn new(
 | 
					
						
							|  |  |  |         accounts: &[(Pubkey, Account)],
 | 
					
						
							| 
									
										
										
										
											2020-04-08 14:36:18 -07:00
										 |  |  |         native_instruction_processors: &[(String, Pubkey)],
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  |     ) -> Self {
 | 
					
						
							|  |  |  |         Self {
 | 
					
						
							| 
									
										
										
										
											2019-11-26 19:23:37 -08:00
										 |  |  |             accounts: accounts
 | 
					
						
							|  |  |  |                 .iter()
 | 
					
						
							|  |  |  |                 .cloned()
 | 
					
						
							|  |  |  |                 .collect::<BTreeMap<Pubkey, Account>>(),
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  |             native_instruction_processors: native_instruction_processors.to_vec(),
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  |             ..GenesisConfig::default()
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-01 09:49:37 -08:00
										 |  |  |     pub fn hash(&self) -> Hash {
 | 
					
						
							| 
									
										
										
										
											2019-11-26 19:23:37 -08:00
										 |  |  |         let serialized = serialize(&self).unwrap();
 | 
					
						
							|  |  |  |         hash(&serialized)
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-12 10:24:49 -07:00
										 |  |  |     fn genesis_filename(ledger_path: &Path) -> PathBuf {
 | 
					
						
							|  |  |  |         Path::new(ledger_path).join("genesis.bin")
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 15:53:41 -07:00
										 |  |  |     pub fn load(ledger_path: &Path) -> Result<Self, std::io::Error> {
 | 
					
						
							| 
									
										
										
										
											2019-11-12 10:24:49 -07:00
										 |  |  |         let filename = Self::genesis_filename(&ledger_path);
 | 
					
						
							| 
									
										
										
										
											2019-06-14 14:22:52 -07:00
										 |  |  |         let file = OpenOptions::new()
 | 
					
						
							|  |  |  |             .read(true)
 | 
					
						
							| 
									
										
										
										
											2019-11-12 10:24:49 -07:00
										 |  |  |             .open(&filename)
 | 
					
						
							|  |  |  |             .map_err(|err| {
 | 
					
						
							|  |  |  |                 std::io::Error::new(
 | 
					
						
							|  |  |  |                     std::io::ErrorKind::Other,
 | 
					
						
							|  |  |  |                     format!("Unable to open {:?}: {:?}", filename, err),
 | 
					
						
							|  |  |  |                 )
 | 
					
						
							|  |  |  |             })?;
 | 
					
						
							| 
									
										
										
										
											2019-06-14 14:22:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //UNSAFE: Required to create a Mmap
 | 
					
						
							| 
									
										
										
										
											2019-11-12 10:24:49 -07:00
										 |  |  |         let mem = unsafe { Mmap::map(&file) }.map_err(|err| {
 | 
					
						
							|  |  |  |             std::io::Error::new(
 | 
					
						
							|  |  |  |                 std::io::ErrorKind::Other,
 | 
					
						
							|  |  |  |                 format!("Unable to map {:?}: {:?}", filename, err),
 | 
					
						
							|  |  |  |             )
 | 
					
						
							|  |  |  |         })?;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let genesis_config = deserialize(&mem).map_err(|err| {
 | 
					
						
							|  |  |  |             std::io::Error::new(
 | 
					
						
							|  |  |  |                 std::io::ErrorKind::Other,
 | 
					
						
							|  |  |  |                 format!("Unable to deserialize {:?}: {:?}", filename, err),
 | 
					
						
							|  |  |  |             )
 | 
					
						
							|  |  |  |         })?;
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  |         Ok(genesis_config)
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 15:53:41 -07:00
										 |  |  |     pub fn write(&self, ledger_path: &Path) -> Result<(), std::io::Error> {
 | 
					
						
							| 
									
										
										
										
											2019-11-12 10:24:49 -07:00
										 |  |  |         let serialized = serialize(&self).map_err(|err| {
 | 
					
						
							|  |  |  |             std::io::Error::new(
 | 
					
						
							|  |  |  |                 std::io::ErrorKind::Other,
 | 
					
						
							|  |  |  |                 format!("Unable to serialize: {:?}", err),
 | 
					
						
							|  |  |  |             )
 | 
					
						
							|  |  |  |         })?;
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 15:53:41 -07:00
										 |  |  |         std::fs::create_dir_all(&ledger_path)?;
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-12 10:24:49 -07:00
										 |  |  |         let mut file = File::create(Self::genesis_filename(&ledger_path))?;
 | 
					
						
							| 
									
										
										
										
											2019-06-14 14:22:52 -07:00
										 |  |  |         file.write_all(&serialized)
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2019-10-15 13:52:44 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     pub fn add_account(&mut self, pubkey: Pubkey, account: Account) {
 | 
					
						
							| 
									
										
										
										
											2019-11-26 19:23:37 -08:00
										 |  |  |         self.accounts.insert(pubkey, account);
 | 
					
						
							| 
									
										
										
										
											2019-10-15 13:52:44 -06:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 14:36:18 -07:00
										 |  |  |     pub fn add_native_instruction_processor(&mut self, name: String, program_id: Pubkey) {
 | 
					
						
							|  |  |  |         self.native_instruction_processors.push((name, program_id));
 | 
					
						
							| 
									
										
										
										
											2019-10-15 13:52:44 -06:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pub fn add_rewards_pool(&mut self, pubkey: Pubkey, account: Account) {
 | 
					
						
							| 
									
										
										
										
											2019-11-26 19:23:37 -08:00
										 |  |  |         self.rewards_pools.insert(pubkey, account);
 | 
					
						
							| 
									
										
										
										
											2019-10-15 13:52:44 -06:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-24 10:18:08 -07:00
										 |  |  | impl fmt::Display for GenesisConfig {
 | 
					
						
							|  |  |  |     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 | 
					
						
							|  |  |  |         write!(
 | 
					
						
							|  |  |  |             f,
 | 
					
						
							|  |  |  |             "\
 | 
					
						
							| 
									
										
										
										
											2020-02-28 13:27:01 -07:00
										 |  |  |              Creation time: {}\n\
 | 
					
						
							|  |  |  |              Operating mode: {:?}\n\
 | 
					
						
							|  |  |  |              Genesis hash: {}\n\
 | 
					
						
							|  |  |  |              Shred version: {}\n\
 | 
					
						
							|  |  |  |              Hashes per tick: {:?}\n\
 | 
					
						
							|  |  |  |              Slots per epoch: {}\n\
 | 
					
						
							|  |  |  |              Warmup epochs: {}abled\n\
 | 
					
						
							|  |  |  |              {:?}\n\
 | 
					
						
							|  |  |  |              {:?}\n\
 | 
					
						
							| 
									
										
										
										
											2020-05-28 14:35:19 +09:00
										 |  |  |              {:?}\n\
 | 
					
						
							| 
									
										
										
										
											2020-02-28 13:27:01 -07:00
										 |  |  |              Capitalization: {} SOL in {} accounts\n\
 | 
					
						
							|  |  |  |              ",
 | 
					
						
							| 
									
										
										
										
											2020-02-24 10:18:08 -07:00
										 |  |  |             Utc.timestamp(self.creation_time, 0).to_rfc3339(),
 | 
					
						
							|  |  |  |             self.operating_mode,
 | 
					
						
							|  |  |  |             self.hash(),
 | 
					
						
							|  |  |  |             compute_shred_version(&self.hash(), None),
 | 
					
						
							|  |  |  |             self.poh_config.hashes_per_tick,
 | 
					
						
							|  |  |  |             self.epoch_schedule.slots_per_epoch,
 | 
					
						
							|  |  |  |             if self.epoch_schedule.warmup {
 | 
					
						
							|  |  |  |                 "en"
 | 
					
						
							|  |  |  |             } else {
 | 
					
						
							|  |  |  |                 "dis"
 | 
					
						
							|  |  |  |             },
 | 
					
						
							| 
									
										
										
										
											2020-05-28 14:35:19 +09:00
										 |  |  |             self.inflation,
 | 
					
						
							| 
									
										
										
										
											2020-02-24 10:18:08 -07:00
										 |  |  |             self.rent,
 | 
					
						
							| 
									
										
										
										
											2020-02-28 13:27:01 -07:00
										 |  |  |             self.fee_rate_governor,
 | 
					
						
							| 
									
										
										
										
											2020-02-24 10:18:08 -07:00
										 |  |  |             lamports_to_sol(
 | 
					
						
							|  |  |  |                 self.accounts
 | 
					
						
							|  |  |  |                     .iter()
 | 
					
						
							|  |  |  |                     .map(|(pubkey, account)| {
 | 
					
						
							|  |  |  |                         if account.lamports == 0 {
 | 
					
						
							|  |  |  |                             panic!("{:?}", (pubkey, account));
 | 
					
						
							|  |  |  |                         }
 | 
					
						
							|  |  |  |                         account.lamports
 | 
					
						
							|  |  |  |                     })
 | 
					
						
							|  |  |  |                     .sum::<u64>()
 | 
					
						
							|  |  |  |             ),
 | 
					
						
							|  |  |  |             self.accounts.len(),
 | 
					
						
							|  |  |  |         )
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  | #[cfg(test)]
 | 
					
						
							|  |  |  | mod tests {
 | 
					
						
							|  |  |  |     use super::*;
 | 
					
						
							| 
									
										
										
										
											2020-02-20 14:28:55 -07:00
										 |  |  |     use crate::signature::{Keypair, Signer};
 | 
					
						
							| 
									
										
										
										
											2019-07-30 15:53:41 -07:00
										 |  |  |     use std::path::PathBuf;
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 15:53:41 -07:00
										 |  |  |     fn make_tmp_path(name: &str) -> PathBuf {
 | 
					
						
							| 
									
										
										
										
											2019-07-17 14:27:58 -07:00
										 |  |  |         let out_dir = std::env::var("FARF_DIR").unwrap_or_else(|_| "farf".to_string());
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  |         let keypair = Keypair::new();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 15:53:41 -07:00
										 |  |  |         let path = [
 | 
					
						
							|  |  |  |             out_dir,
 | 
					
						
							|  |  |  |             "tmp".to_string(),
 | 
					
						
							|  |  |  |             format!("{}-{}", name, keypair.pubkey()),
 | 
					
						
							|  |  |  |         ]
 | 
					
						
							|  |  |  |         .iter()
 | 
					
						
							|  |  |  |         .collect();
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // whack any possible collision
 | 
					
						
							|  |  |  |         let _ignored = std::fs::remove_dir_all(&path);
 | 
					
						
							|  |  |  |         // whack any possible collision
 | 
					
						
							|  |  |  |         let _ignored = std::fs::remove_file(&path);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         path
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     #[test]
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  |     fn test_genesis_config() {
 | 
					
						
							| 
									
										
										
										
											2019-11-15 14:50:26 -07:00
										 |  |  |         let faucet_keypair = Keypair::new();
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  |         let mut config = GenesisConfig::default();
 | 
					
						
							|  |  |  |         config.add_account(
 | 
					
						
							| 
									
										
										
										
											2019-11-15 14:50:26 -07:00
										 |  |  |             faucet_keypair.pubkey(),
 | 
					
						
							| 
									
										
										
										
											2019-10-15 13:52:44 -06:00
										 |  |  |             Account::new(10_000, 0, &Pubkey::default()),
 | 
					
						
							|  |  |  |         );
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  |         config.add_account(Pubkey::new_rand(), Account::new(1, 0, &Pubkey::default()));
 | 
					
						
							| 
									
										
										
										
											2020-04-08 14:36:18 -07:00
										 |  |  |         config.add_native_instruction_processor("hi".to_string(), Pubkey::new_rand());
 | 
					
						
							| 
									
										
										
										
											2019-06-19 15:40:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  |         assert_eq!(config.accounts.len(), 2);
 | 
					
						
							| 
									
										
										
										
											2019-11-15 14:50:26 -07:00
										 |  |  |         assert!(config
 | 
					
						
							|  |  |  |             .accounts
 | 
					
						
							|  |  |  |             .iter()
 | 
					
						
							|  |  |  |             .any(|(pubkey, account)| *pubkey == faucet_keypair.pubkey()
 | 
					
						
							|  |  |  |                 && account.lamports == 10_000));
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  |         let path = &make_tmp_path("genesis_config");
 | 
					
						
							|  |  |  |         config.write(&path).expect("write");
 | 
					
						
							|  |  |  |         let loaded_config = GenesisConfig::load(&path).expect("load");
 | 
					
						
							|  |  |  |         assert_eq!(config.hash(), loaded_config.hash());
 | 
					
						
							| 
									
										
										
										
											2019-05-07 11:16:22 -07:00
										 |  |  |         let _ignored = std::fs::remove_file(&path);
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:04:04 -08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | }
 |