diff --git a/Cargo.lock b/Cargo.lock index 3fdb0bb2bf..a6eddc43d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4445,6 +4445,7 @@ dependencies = [ "bs58 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "bv 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "ed25519-dalek 1.0.0-pre.1 (registry+https://github.com/rust-lang/crates.io-index)", "generic-array 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/core/src/validator.rs b/core/src/validator.rs index 62b80d5c54..e78face1f0 100644 --- a/core/src/validator.rs +++ b/core/src/validator.rs @@ -30,7 +30,6 @@ use solana_ledger::{ create_new_tmp_ledger, leader_schedule::FixedSchedule, leader_schedule_cache::LeaderScheduleCache, - shred_version::compute_shred_version, }; use solana_metrics::datapoint_info; use solana_runtime::bank::Bank; @@ -39,6 +38,7 @@ use solana_sdk::{ genesis_config::GenesisConfig, hash::Hash, pubkey::Pubkey, + shred_version::compute_shred_version, signature::{Keypair, Signer}, timing::timestamp, }; diff --git a/genesis/src/main.rs b/genesis/src/main.rs index 23004cd8f4..4c7b4993eb 100644 --- a/genesis/src/main.rs +++ b/genesis/src/main.rs @@ -1,23 +1,18 @@ //! A command-line executable for generating the chain's genesis config. -use chrono::{TimeZone, Utc}; use clap::{crate_description, crate_name, value_t, value_t_or_exit, App, Arg, ArgMatches}; use solana_clap_utils::{ input_parsers::{pubkey_of, unix_timestamp_from_rfc3339_datetime}, input_validators::{is_rfc3339_datetime, is_valid_percentage}, }; use solana_genesis::{genesis_accounts::add_genesis_accounts, Base64Account}; -use solana_ledger::{ - blockstore::create_new_ledger, poh::compute_hashes_per_tick, - shred_version::compute_shred_version, -}; +use solana_ledger::{blockstore::create_new_ledger, poh::compute_hashes_per_tick}; use solana_sdk::{ account::Account, clock, epoch_schedule::EpochSchedule, fee_calculator::FeeCalculator, genesis_config::{GenesisConfig, OperatingMode}, - native_token::lamports_to_sol, native_token::sol_to_lamports, poh_config::PohConfig, pubkey::Pubkey, @@ -557,47 +552,7 @@ fn main() -> Result<(), Box> { create_new_ledger(&ledger_path, &genesis_config)?; - println!( - "\ - 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\ - Capitalization: {} SOL in {} accounts\n\ - ", - Utc.timestamp(genesis_config.creation_time, 0).to_rfc3339(), - genesis_config.operating_mode, - genesis_config.hash(), - compute_shred_version(&genesis_config.hash(), None), - genesis_config.poh_config.hashes_per_tick, - genesis_config.epoch_schedule.slots_per_epoch, - if genesis_config.epoch_schedule.warmup { - "en" - } else { - "dis" - }, - genesis_config.rent, - genesis_config.fee_calculator, - lamports_to_sol( - genesis_config - .accounts - .iter() - .map(|(pubkey, account)| { - if account.lamports == 0 { - panic!("{:?}", (pubkey, account)); - } - account.lamports - }) - .sum::() - ), - genesis_config.accounts.len(), - ); - + println!("{}", genesis_config); Ok(()) } diff --git a/ledger-tool/src/main.rs b/ledger-tool/src/main.rs index 475aedd7e6..56d0fd351e 100644 --- a/ledger-tool/src/main.rs +++ b/ledger-tool/src/main.rs @@ -11,12 +11,11 @@ use solana_ledger::{ blockstore_db::{self, Column, Database}, blockstore_processor::{BankForksInfo, ProcessOptions}, rooted_slot_iterator::RootedSlotIterator, - shred_version::compute_shred_version, snapshot_utils, }; use solana_sdk::{ clock::Slot, genesis_config::GenesisConfig, native_token::lamports_to_sol, - program_utils::limited_deserialize, pubkey::Pubkey, + program_utils::limited_deserialize, pubkey::Pubkey, shred_version::compute_shred_version, }; use solana_vote_program::vote_state::VoteState; use std::{ diff --git a/ledger/src/blockstore.rs b/ledger/src/blockstore.rs index c40b3758b5..66de705df2 100644 --- a/ledger/src/blockstore.rs +++ b/ledger/src/blockstore.rs @@ -2264,7 +2264,7 @@ pub fn create_new_ledger(ledger_path: &Path, genesis_config: &GenesisConfig) -> let hashes_per_tick = genesis_config.poh_config.hashes_per_tick.unwrap_or(0); let entries = create_ticks(ticks_per_slot, hashes_per_tick, genesis_config.hash()); let last_hash = entries.last().unwrap().hash; - let version = Shred::version_from_hash(&last_hash); + let version = solana_sdk::shred_version::version_from_hash(&last_hash); let shredder = Shredder::new(0, 0, 0.0, Arc::new(Keypair::new()), 0, version) .expect("Failed to create entry shredder"); diff --git a/ledger/src/lib.rs b/ledger/src/lib.rs index 8350381a7e..dde02ecdb8 100644 --- a/ledger/src/lib.rs +++ b/ledger/src/lib.rs @@ -15,7 +15,6 @@ pub mod leader_schedule_utils; pub mod poh; pub mod rooted_slot_iterator; pub mod shred; -pub mod shred_version; pub mod sigverify_shreds; pub mod snapshot_package; pub mod snapshot_utils; diff --git a/ledger/src/shred.rs b/ledger/src/shred.rs index 48fe9f56d9..a09a655ce2 100644 --- a/ledger/src/shred.rs +++ b/ledger/src/shred.rs @@ -392,22 +392,6 @@ impl Shred { self.signature() .verify(pubkey.as_ref(), &self.payload[SIZE_OF_SIGNATURE..]) } - - pub fn version_from_hash(hash: &Hash) -> u16 { - let hash = hash.as_ref(); - let mut accum = [0u8; 2]; - hash.chunks(2).for_each(|seed| { - accum - .iter_mut() - .zip(seed) - .for_each(|(accum, seed)| *accum ^= *seed) - }); - // convert accum into a u16 - let version = ((accum[0] as u16) << 8) | accum[1] as u16; - - // ensure version is never zero, to avoid looking like an uninitialized version - version.saturating_add(1) - } } #[derive(Debug)] @@ -919,8 +903,7 @@ pub mod tests { use super::*; use bincode::serialized_size; use matches::assert_matches; - use solana_sdk::hash::hash; - use solana_sdk::system_transaction; + use solana_sdk::{hash::hash, shred_version, system_transaction}; use std::collections::HashSet; use std::convert::TryInto; @@ -1451,7 +1434,7 @@ pub mod tests { fn test_shred_version() { let keypair = Arc::new(Keypair::new()); let hash = hash(Hash::default().as_ref()); - let version = Shred::version_from_hash(&hash); + let version = shred_version::version_from_hash(&hash); assert_ne!(version, 0); let shredder = Shredder::new(0, 0, 1.0, keypair, 0, version).expect("Failed in creating shredder"); @@ -1481,19 +1464,19 @@ pub mod tests { 0x5a, 0x5a, 0xa5, 0xa5, 0x5a, 0x5a, 0xa5, 0xa5, 0x5a, 0x5a, 0xa5, 0xa5, 0x5a, 0x5a, 0xa5, 0xa5, 0x5a, 0x5a, ]; - let version = Shred::version_from_hash(&Hash::new(&hash)); + let version = shred_version::version_from_hash(&Hash::new(&hash)); assert_eq!(version, 1); let hash = [ 0xa5u8, 0xa5, 0x5a, 0x5a, 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 version = Shred::version_from_hash(&Hash::new(&hash)); + let version = shred_version::version_from_hash(&Hash::new(&hash)); assert_eq!(version, 0xffff); let hash = [ 0xa5u8, 0xa5, 0x5a, 0x5a, 0xa5, 0xa5, 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 version = Shred::version_from_hash(&Hash::new(&hash)); + let version = shred_version::version_from_hash(&Hash::new(&hash)); assert_eq!(version, 0x5a5b); } @@ -1501,7 +1484,7 @@ pub mod tests { fn test_shred_fec_set_index() { let keypair = Arc::new(Keypair::new()); let hash = hash(Hash::default().as_ref()); - let version = Shred::version_from_hash(&hash); + let version = shred_version::version_from_hash(&hash); assert_ne!(version, 0); let shredder = Shredder::new(0, 0, 0.5, keypair, 0, version).expect("Failed in creating shredder"); @@ -1539,7 +1522,7 @@ pub mod tests { fn test_max_coding_shreds() { let keypair = Arc::new(Keypair::new()); let hash = hash(Hash::default().as_ref()); - let version = Shred::version_from_hash(&hash); + let version = shred_version::version_from_hash(&hash); assert_ne!(version, 0); let shredder = Shredder::new(0, 0, 1.0, keypair, 0, version).expect("Failed in creating shredder"); diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 5761d237f2..da1709985c 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -8,7 +8,6 @@ use crate::{ AccountsDBSerialize, AppendVecId, ErrorCounters, SnapshotStorage, SnapshotStorages, }, blockhash_queue::BlockhashQueue, - hard_forks::HardForks, message_processor::{MessageProcessor, ProcessInstruction}, nonce_utils, rent_collector::RentCollector, @@ -38,6 +37,7 @@ use solana_sdk::{ epoch_schedule::EpochSchedule, fee_calculator::FeeCalculator, genesis_config::GenesisConfig, + hard_forks::HardForks, hash::{extend_and_hash, hashv, Hash}, inflation::Inflation, native_loader, diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index c1647f8663..197c3f27c8 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -7,7 +7,6 @@ pub mod bank_client; mod blockhash_queue; pub mod bloom; pub mod genesis_utils; -pub mod hard_forks; pub mod loader_utils; pub mod message_processor; mod native_loader; diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 6f8c957c62..ace15de7b7 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -30,6 +30,7 @@ bincode = "1.2.1" bs58 = "0.3.0" bv = { version = "0.11.0", features = ["serde"] } byteorder = { version = "1.3.2", optional = true } +chrono = "0.4" generic-array = { version = "0.13.2", default-features = false, features = ["serde", "more_lengths"] } hex = "0.4.0" hmac = "0.7.0" diff --git a/sdk/src/genesis_config.rs b/sdk/src/genesis_config.rs index ef469e7a00..b465213cce 100644 --- a/sdk/src/genesis_config.rs +++ b/sdk/src/genesis_config.rs @@ -7,16 +7,20 @@ use crate::{ fee_calculator::FeeCalculator, hash::{hash, Hash}, inflation::Inflation, + native_token::lamports_to_sol, poh_config::PohConfig, pubkey::Pubkey, rent::Rent, + shred_version::compute_shred_version, signature::{Keypair, Signer}, system_program::{self, solana_system_program}, }; use bincode::{deserialize, serialize}; +use chrono::{TimeZone, Utc}; use memmap::Mmap; use std::{ collections::BTreeMap, + fmt, fs::{File, OpenOptions}, io::Write, path::{Path, PathBuf}, @@ -173,6 +177,51 @@ impl GenesisConfig { } } +impl fmt::Display for GenesisConfig { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "\ + 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\ + Capitalization: {} SOL in {} accounts\n\ + ", + 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" + }, + self.rent, + self.fee_calculator, + lamports_to_sol( + self.accounts + .iter() + .map(|(pubkey, account)| { + if account.lamports == 0 { + panic!("{:?}", (pubkey, account)); + } + account.lamports + }) + .sum::() + ), + self.accounts.len(), + ) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/runtime/src/hard_forks.rs b/sdk/src/hard_forks.rs similarity index 100% rename from runtime/src/hard_forks.rs rename to sdk/src/hard_forks.rs diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index 50bb355757..fd7466875e 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -68,6 +68,10 @@ pub mod client; #[cfg(not(feature = "program"))] pub mod genesis_config; #[cfg(not(feature = "program"))] +pub mod hard_forks; +#[cfg(not(feature = "program"))] +pub mod shred_version; +#[cfg(not(feature = "program"))] pub mod signature; #[cfg(not(feature = "program"))] pub mod signers; diff --git a/ledger/src/shred_version.rs b/sdk/src/shred_version.rs similarity index 65% rename from ledger/src/shred_version.rs rename to sdk/src/shred_version.rs index 2149637085..5406fd57c3 100644 --- a/ledger/src/shred_version.rs +++ b/sdk/src/shred_version.rs @@ -1,6 +1,23 @@ -use crate::shred::Shred; -use solana_runtime::hard_forks::HardForks; -use solana_sdk::hash::{extend_and_hash, Hash}; +use solana_sdk::{ + hard_forks::HardForks, + hash::{extend_and_hash, Hash}, +}; + +pub fn version_from_hash(hash: &Hash) -> u16 { + let hash = hash.as_ref(); + let mut accum = [0u8; 2]; + hash.chunks(2).for_each(|seed| { + accum + .iter_mut() + .zip(seed) + .for_each(|(accum, seed)| *accum ^= *seed) + }); + // convert accum into a u16 + let version = ((accum[0] as u16) << 8) | accum[1] as u16; + + // ensure version is never zero, to avoid looking like an uninitialized version + version.saturating_add(1) +} pub fn compute_shred_version(genesis_hash: &Hash, hard_forks: Option<&HardForks>) -> u16 { use byteorder::{ByteOrder, LittleEndian}; @@ -15,7 +32,7 @@ pub fn compute_shred_version(genesis_hash: &Hash, hard_forks: Option<&HardForks> } } - Shred::version_from_hash(&hash) + version_from_hash(&hash) } #[cfg(test)]