Skip to content

Commit

Permalink
extract cluster-type crate
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinheavey committed Oct 29, 2024
1 parent e9616d0 commit 8961ab5
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 45 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ members = [
"sdk/cargo-test-sbf",
"sdk/clock",
"sdk/cpi",
"sdk/cluster-type",
"sdk/decode-error",
"sdk/derivation-path",
"sdk/epoch-schedule",
Expand Down Expand Up @@ -410,6 +411,7 @@ solana-cli-config = { path = "cli-config", version = "=2.2.0" }
solana-cli-output = { path = "cli-output", version = "=2.2.0" }
solana-client = { path = "client", version = "=2.2.0" }
solana-clock = { path = "sdk/clock", version = "=2.2.0" }
solana-cluster-type = { path = "sdk/cluster-type", version = "=2.2.0" }
solana-compute-budget = { path = "compute-budget", version = "=2.2.0" }
solana-compute-budget-program = { path = "programs/compute-budget", version = "=2.2.0" }
solana-config-program = { path = "programs/config", version = "=2.2.0" }
Expand Down
10 changes: 10 additions & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ full = [
"sha3",
"digest",
"solana-pubkey/rand",
"dep:solana-cluster-type",
"dep:solana-precompile-error",
"dep:solana-transaction-error"
]
Expand All @@ -44,6 +45,7 @@ frozen-abi = [
"dep:solana-frozen-abi-macro",
"solana-feature-set/frozen-abi",
"solana-account/frozen-abi",
"solana-cluster-type/frozen-abi",
"solana-inflation/frozen-abi",
"solana-program/frozen-abi",
"solana-short-vec/frozen-abi",
Expand Down Expand Up @@ -91,6 +93,9 @@ sha3 = { workspace = true, optional = true }
siphasher = { workspace = true }
solana-account = { workspace = true, features = ["bincode"] }
solana-bn254 = { workspace = true }
solana-cluster-type = { workspace = true, features = [
"serde",
], optional = true }
solana-decode-error = { workspace = true }
solana-derivation-path = { workspace = true }
solana-feature-set = { workspace = true }
Expand Down
26 changes: 26 additions & 0 deletions sdk/cluster-type/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "solana-cluster-type"
description = "Solana ClusterType enum"
documentation = "https://docs.rs/solana-cluster-type"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-frozen-abi = { workspace = true, optional = true }
solana-frozen-abi-macro = { workspace = true, optional = true }
solana-hash = { workspace = true, default-features = false }

[features]
frozen-abi = ["dep:solana-frozen-abi", "dep:solana-frozen-abi-macro"]
serde = ["dep:serde", "dep:serde_derive"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
all-features = true
rustdoc-args = ["--cfg=docsrs"]
51 changes: 51 additions & 0 deletions sdk/cluster-type/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
use {solana_hash::Hash, std::str::FromStr};

// The order can't align with release lifecycle only to remain ABI-compatible...
#[cfg_attr(feature = "frozen-abi", derive(AbiExample, AbiEnumVisitor))]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Deserialize, serde_derive::Serialize)
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClusterType {
Testnet,
MainnetBeta,
Devnet,
Development,
}

impl ClusterType {
pub const STRINGS: [&'static str; 4] = ["development", "devnet", "testnet", "mainnet-beta"];

/// Get the known genesis hash for this ClusterType
pub fn get_genesis_hash(&self) -> Option<Hash> {
match self {
Self::MainnetBeta => {
Some(Hash::from_str("5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d").unwrap())
}
Self::Testnet => {
Some(Hash::from_str("4uhcVJyU9pJkvQyS88uRDiswHXSCkY3zQawwpjk2NsNY").unwrap())
}
Self::Devnet => {
Some(Hash::from_str("EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG").unwrap())
}
Self::Development => None,
}
}
}

impl FromStr for ClusterType {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"development" => Ok(ClusterType::Development),
"devnet" => Ok(ClusterType::Devnet),
"testnet" => Ok(ClusterType::Testnet),
"mainnet-beta" => Ok(ClusterType::MainnetBeta),
_ => Err(format!("{s} is unrecognized for cluster type")),
}
}
}
50 changes: 5 additions & 45 deletions sdk/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#![cfg(feature = "full")]

#[deprecated(
since = "2.2.0",
note = "Use `solana_cluster_type::ClusterType` instead."
)]
pub use solana_cluster_type::ClusterType;
use {
crate::{
clock::{UnixTimestamp, DEFAULT_TICKS_PER_SLOT},
Expand All @@ -28,7 +33,6 @@ use {
fs::{File, OpenOptions},
io::Write,
path::{Path, PathBuf},
str::FromStr,
time::{SystemTime, UNIX_EPOCH},
},
};
Expand All @@ -40,50 +44,6 @@ pub const DEFAULT_GENESIS_DOWNLOAD_PATH: &str = "/genesis.tar.bz2";
// deprecated default that is no longer used
pub const UNUSED_DEFAULT: u64 = 1024;

// The order can't align with release lifecycle only to remain ABI-compatible...
#[cfg_attr(feature = "frozen-abi", derive(AbiExample, AbiEnumVisitor))]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClusterType {
Testnet,
MainnetBeta,
Devnet,
Development,
}

impl ClusterType {
pub const STRINGS: [&'static str; 4] = ["development", "devnet", "testnet", "mainnet-beta"];

/// Get the known genesis hash for this ClusterType
pub fn get_genesis_hash(&self) -> Option<Hash> {
match self {
Self::MainnetBeta => {
Some(Hash::from_str("5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d").unwrap())
}
Self::Testnet => {
Some(Hash::from_str("4uhcVJyU9pJkvQyS88uRDiswHXSCkY3zQawwpjk2NsNY").unwrap())
}
Self::Devnet => {
Some(Hash::from_str("EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG").unwrap())
}
Self::Development => None,
}
}
}

impl FromStr for ClusterType {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"development" => Ok(ClusterType::Development),
"devnet" => Ok(ClusterType::Devnet),
"testnet" => Ok(ClusterType::Testnet),
"mainnet-beta" => Ok(ClusterType::MainnetBeta),
_ => Err(format!("{s} is unrecognized for cluster type")),
}
}
}

#[cfg_attr(
feature = "frozen-abi",
derive(AbiExample),
Expand Down

0 comments on commit 8961ab5

Please sign in to comment.