Move shred_version module to sdk/

This commit is contained in:
Michael Vines 2020-02-24 10:18:08 -07:00
parent 90240bf11d
commit 73063544bd
14 changed files with 89 additions and 82 deletions

1
Cargo.lock generated
View File

@ -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)",

View File

@ -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,
};

View File

@ -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<dyn error::Error>> {
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::<u64>()
),
genesis_config.accounts.len(),
);
println!("{}", genesis_config);
Ok(())
}

View File

@ -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::{

View File

@ -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");

View File

@ -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;

View File

@ -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");

View File

@ -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,

View File

@ -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;

View File

@ -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"

View File

@ -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::<u64>()
),
self.accounts.len(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -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;

View File

@ -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)]