Skip to content

Commit

Permalink
serde is back now
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Jan 15, 2025
1 parent d53d761 commit a085001
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
4 changes: 3 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
32 changes: 22 additions & 10 deletions sdk/genesis-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"]
Expand Down
35 changes: 24 additions & 11 deletions sdk/genesis-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
};
Expand All @@ -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,
Expand Down Expand Up @@ -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<Self, std::io::Error> {
let filename = Self::genesis_filename(ledger_path);
let file = OpenOptions::new()
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -219,6 +231,7 @@ impl GenesisConfig {
}
}

#[cfg(feature = "serde")]
impl fmt::Display for GenesisConfig {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
Expand Down Expand Up @@ -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};

Expand Down

0 comments on commit a085001

Please sign in to comment.