diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index e0e6c0c5b72957..f7132e403f1c1a 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -141,7 +141,9 @@ solana-frozen-abi = { workspace = true, optional = true, features = [ solana-frozen-abi-macro = { workspace = true, optional = true, features = [ "frozen-abi", ] } -solana-genesis-config = { workspace = true, optional = true } +solana-genesis-config = { workspace = true, features = [ + "serde" +], optional = true } solana-hard-forks = { workspace = true, features = [ "serde", ], optional = true } diff --git a/sdk/genesis-config/Cargo.toml b/sdk/genesis-config/Cargo.toml index 21b656eda7a275..16da57dd0bd209 100644 --- a/sdk/genesis-config/Cargo.toml +++ b/sdk/genesis-config/Cargo.toml @@ -13,23 +13,23 @@ edition = { workspace = true } bincode = { workspace = true } chrono = { workspace = true, features = ["alloc"] } memmap2 = { workspace = true } -serde = { workspace = true } -serde_derive = { workspace = true } -solana-account = { workspace = true, features = ["serde"] } -solana-clock = { workspace = true, features = ["serde"] } -solana-cluster-type = { workspace = true, features = ["serde"] } -solana-epoch-schedule = { workspace = true, features = ["serde"] } -solana-fee-calculator = { workspace = true, features = ["serde"] } +serde = { workspace = true, optional = true } +serde_derive = { workspace = true, optional = true } +solana-account = { workspace = true } +solana-clock = { workspace = true } +solana-cluster-type = { workspace = true } +solana-epoch-schedule = { workspace = true } +solana-fee-calculator = { workspace = true } solana-frozen-abi = { workspace = true, optional = true } solana-frozen-abi-macro = { workspace = true, optional = true } solana-hash = { workspace = true } -solana-inflation = { workspace = true, features = ["serde"] } +solana-inflation = { workspace = true } solana-keypair = { workspace = true } solana-logger = { workspace = true } solana-native-token = { workspace = true } -solana-poh-config = { workspace = true, features = ["serde"] } +solana-poh-config = { workspace = true } solana-pubkey = { workspace = true } -solana-rent = { workspace = true, features = ["serde"] } +solana-rent = { workspace = true } solana-sdk-ids = { workspace = true } solana-sha256-hasher = { workspace = true } solana-shred-version = { workspace = true } @@ -41,6 +41,18 @@ solana-pubkey = { workspace = true, features = ["rand"] } [features] frozen-abi = ["dep:solana-frozen-abi", "dep:solana-frozen-abi-macro"] +serde = [ + "dep:serde", + "dep:serde_derive", + "solana-account/serde", + "solana-clock/serde", + "solana-cluster-type/serde", + "solana-epoch-schedule/serde", + "solana-fee-calculator/serde", + "solana-inflation/serde", + "solana-poh-config/serde", + "solana-rent/serde", +] [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/sdk/genesis-config/src/lib.rs b/sdk/genesis-config/src/lib.rs index a407223ba95493..6fd738a6c6d798 100644 --- a/sdk/genesis-config/src/lib.rs +++ b/sdk/genesis-config/src/lib.rs @@ -9,33 +9,37 @@ pub use solana_cluster_type::ClusterType; #[cfg(feature = "frozen-abi")] use solana_frozen_abi_macro::{frozen_abi, AbiExample}; +#[cfg(feature = "serde")] use { bincode::{deserialize, serialize}, chrono::{TimeZone, Utc}, memmap2::Mmap, - serde_derive::{Deserialize, Serialize}, + solana_hash::Hash, + solana_native_token::lamports_to_sol, + solana_sha256_hasher::hash, + solana_shred_version::compute_shred_version, + std::{ + fmt, + fs::{File, OpenOptions}, + io::Write, + path::{Path, PathBuf}, + }, +}; +use { solana_account::{Account, AccountSharedData}, solana_clock::{UnixTimestamp, DEFAULT_TICKS_PER_SLOT}, solana_epoch_schedule::EpochSchedule, solana_fee_calculator::FeeRateGovernor, - solana_hash::Hash, solana_inflation::Inflation, solana_keypair::Keypair, - solana_native_token::lamports_to_sol, solana_poh_config::PohConfig, solana_pubkey::Pubkey, solana_rent::Rent, solana_sdk_ids::system_program, - solana_sha256_hasher::hash, - solana_shred_version::compute_shred_version, solana_signer::Signer, solana_time_utils::years_as_slots, std::{ collections::BTreeMap, - fmt, - fs::{File, OpenOptions}, - io::Write, - path::{Path, PathBuf}, time::{SystemTime, UNIX_EPOCH}, }, }; @@ -52,7 +56,11 @@ pub const UNUSED_DEFAULT: u64 = 1024; derive(AbiExample), frozen_abi(digest = "D9VFRSj4fodCuKFC9omQY2zY2Uw8wo6SzJFLeMJaVigm") )] -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[cfg_attr( + feature = "serde", + derive(serde_derive::Deserialize, serde_derive::Serialize) +)] +#[derive(Clone, Debug, PartialEq)] pub struct GenesisConfig { /// when the network (bootstrap validator) was started relative to the UNIX Epoch pub creation_time: UnixTimestamp, @@ -135,15 +143,18 @@ impl GenesisConfig { } } + #[cfg(feature = "serde")] pub fn hash(&self) -> Hash { let serialized = serialize(&self).unwrap(); hash(&serialized) } + #[cfg(feature = "serde")] fn genesis_filename(ledger_path: &Path) -> PathBuf { Path::new(ledger_path).join(DEFAULT_GENESIS_FILE) } + #[cfg(feature = "serde")] pub fn load(ledger_path: &Path) -> Result { let filename = Self::genesis_filename(ledger_path); let file = OpenOptions::new() @@ -173,6 +184,7 @@ impl GenesisConfig { Ok(genesis_config) } + #[cfg(feature = "serde")] pub fn write(&self, ledger_path: &Path) -> Result<(), std::io::Error> { let serialized = serialize(&self).map_err(|err| { std::io::Error::new( @@ -219,6 +231,7 @@ impl GenesisConfig { } } +#[cfg(feature = "serde")] impl fmt::Display for GenesisConfig { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( @@ -276,7 +289,7 @@ impl fmt::Display for GenesisConfig { } } -#[cfg(test)] +#[cfg(all(feature = "serde", test))] mod tests { use {super::*, solana_signer::Signer, std::path::PathBuf};