Skip to content

Commit b8cfdee

Browse files
authored
chore(version): cosmeticly reorganize the version crate (#10326)
1 parent 421241b commit b8cfdee

File tree

6 files changed

+188
-176
lines changed

6 files changed

+188
-176
lines changed

gossip/src/crds_data.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ impl<'de> Deserialize<'de> for Vote {
427427
pub(crate) struct LegacyVersion {
428428
from: Pubkey,
429429
wallclock: u64,
430-
version: solana_version::LegacyVersion1,
430+
version: solana_version::v1::Version,
431431
}
432432
reject_deserialize!(LegacyVersion, "LegacyVersion is deprecated");
433433

@@ -444,7 +444,7 @@ impl Sanitize for LegacyVersion {
444444
pub(crate) struct Version {
445445
from: Pubkey,
446446
wallclock: u64,
447-
version: solana_version::LegacyVersion2,
447+
version: solana_version::v2::Version,
448448
}
449449
reject_deserialize!(Version, "Version is deprecated");
450450

@@ -612,7 +612,7 @@ mod test {
612612
commit: Option<u32>,
613613
}
614614

615-
let legacy_v1: solana_version::LegacyVersion1 = {
615+
let legacy_v1: solana_version::v1::Version = {
616616
let bytes = bincode::serialize(&LegacyVersion1Mirror {
617617
major: 0,
618618
minor: 0,
@@ -636,7 +636,7 @@ mod test {
636636
let version = CrdsData::Version(Version {
637637
from: keypair.pubkey(),
638638
wallclock: timestamp(),
639-
version: solana_version::LegacyVersion2::default(),
639+
version: solana_version::v2::Version::default(),
640640
});
641641
let bytes = bincode::serialize(&version).unwrap();
642642
assert!(bincode::deserialize::<CrdsData>(&bytes[..]).is_err());

version/src/client_ids.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#[derive(Debug, Eq, PartialEq)]
2+
pub enum ClientId {
3+
SolanaLabs,
4+
JitoLabs,
5+
Frankendancer,
6+
Agave,
7+
AgavePaladin,
8+
Firedancer,
9+
AgaveBam,
10+
Sig,
11+
// If new variants are added, update From<u16> and TryFrom<ClientId>.
12+
Unknown(u16),
13+
}
14+
15+
impl From<u16> for ClientId {
16+
fn from(client: u16) -> Self {
17+
match client {
18+
0u16 => Self::SolanaLabs,
19+
1u16 => Self::JitoLabs,
20+
2u16 => Self::Frankendancer,
21+
3u16 => Self::Agave,
22+
4u16 => Self::AgavePaladin,
23+
5u16 => Self::Firedancer,
24+
6u16 => Self::AgaveBam,
25+
7u16 => Self::Sig,
26+
_ => Self::Unknown(client),
27+
}
28+
}
29+
}
30+
31+
impl TryFrom<ClientId> for u16 {
32+
type Error = String;
33+
34+
fn try_from(client: ClientId) -> Result<Self, Self::Error> {
35+
match client {
36+
ClientId::SolanaLabs => Ok(0u16),
37+
ClientId::JitoLabs => Ok(1u16),
38+
ClientId::Frankendancer => Ok(2u16),
39+
ClientId::Agave => Ok(3u16),
40+
ClientId::AgavePaladin => Ok(4u16),
41+
ClientId::Firedancer => Ok(5u16),
42+
ClientId::AgaveBam => Ok(6u16),
43+
ClientId::Sig => Ok(7u16),
44+
ClientId::Unknown(client @ 0u16..=7u16) => Err(format!("Invalid client: {client}")),
45+
ClientId::Unknown(client) => Ok(client),
46+
}
47+
}
48+
}
49+
50+
#[cfg(test)]
51+
mod test {
52+
use super::*;
53+
54+
#[test]
55+
fn test_client_id() {
56+
assert_eq!(ClientId::from(0u16), ClientId::SolanaLabs);
57+
assert_eq!(ClientId::from(1u16), ClientId::JitoLabs);
58+
assert_eq!(ClientId::from(2u16), ClientId::Frankendancer);
59+
assert_eq!(ClientId::from(3u16), ClientId::Agave);
60+
assert_eq!(ClientId::from(4u16), ClientId::AgavePaladin);
61+
assert_eq!(ClientId::from(5u16), ClientId::Firedancer);
62+
assert_eq!(ClientId::from(6u16), ClientId::AgaveBam);
63+
assert_eq!(ClientId::from(7u16), ClientId::Sig);
64+
for client in 8u16..=u16::MAX {
65+
assert_eq!(ClientId::from(client), ClientId::Unknown(client));
66+
}
67+
assert_eq!(u16::try_from(ClientId::SolanaLabs), Ok(0u16));
68+
assert_eq!(u16::try_from(ClientId::JitoLabs), Ok(1u16));
69+
assert_eq!(u16::try_from(ClientId::Frankendancer), Ok(2u16));
70+
assert_eq!(u16::try_from(ClientId::Agave), Ok(3u16));
71+
assert_eq!(u16::try_from(ClientId::AgavePaladin), Ok(4u16));
72+
assert_eq!(u16::try_from(ClientId::Firedancer), Ok(5u16));
73+
assert_eq!(u16::try_from(ClientId::AgaveBam), Ok(6u16));
74+
assert_eq!(u16::try_from(ClientId::Sig), Ok(7u16));
75+
for client in 0..=7u16 {
76+
assert_eq!(
77+
u16::try_from(ClientId::Unknown(client)),
78+
Err(format!("Invalid client: {client}"))
79+
);
80+
}
81+
for client in 8u16..=u16::MAX {
82+
assert_eq!(u16::try_from(ClientId::Unknown(client)), Ok(client));
83+
}
84+
}
85+
}

version/src/lib.rs

Lines changed: 6 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,21 @@
11
#![cfg(feature = "agave-unstable-api")]
22
#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
33

4-
pub use self::legacy::{LegacyVersion1, LegacyVersion2};
5-
use {
6-
rand::{rng, Rng},
7-
serde::{Deserialize, Serialize},
8-
solana_sanitize::Sanitize,
9-
solana_serde_varint as serde_varint,
10-
std::{convert::TryInto, fmt},
11-
};
124
#[cfg_attr(feature = "frozen-abi", macro_use)]
135
#[cfg(feature = "frozen-abi")]
146
extern crate solana_frozen_abi_macro;
157

16-
mod legacy;
8+
mod client_ids;
9+
pub mod v1;
10+
pub mod v2;
11+
pub mod v3;
1712

18-
#[derive(Debug, Eq, PartialEq)]
19-
pub enum ClientId {
20-
SolanaLabs,
21-
JitoLabs,
22-
Frankendancer,
23-
Agave,
24-
AgavePaladin,
25-
Firedancer,
26-
AgaveBam,
27-
Sig,
28-
// If new variants are added, update From<u16> and TryFrom<ClientId>.
29-
Unknown(u16),
30-
}
31-
32-
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
33-
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
34-
pub struct Version {
35-
#[serde(with = "serde_varint")]
36-
pub major: u16,
37-
#[serde(with = "serde_varint")]
38-
pub minor: u16,
39-
#[serde(with = "serde_varint")]
40-
pub patch: u16,
41-
pub commit: u32, // first 4 bytes of the sha1 commit hash
42-
pub feature_set: u32, // first 4 bytes of the FeatureSet identifier
43-
#[serde(with = "serde_varint")]
44-
client: u16,
45-
}
46-
47-
impl Version {
48-
pub fn as_semver_version(&self) -> semver::Version {
49-
semver::Version::new(self.major as u64, self.minor as u64, self.patch as u64)
50-
}
51-
52-
pub fn client(&self) -> ClientId {
53-
ClientId::from(self.client)
54-
}
55-
}
13+
pub use {client_ids::*, v3::*};
5614

57-
fn compute_commit(sha1: Option<&'static str>) -> Option<u32> {
15+
pub(crate) fn compute_commit(sha1: Option<&'static str>) -> Option<u32> {
5816
u32::from_str_radix(sha1?.get(..8)?, /*radix:*/ 16).ok()
5917
}
6018

61-
impl Default for Version {
62-
fn default() -> Self {
63-
let feature_set =
64-
u32::from_le_bytes(agave_feature_set::ID.as_ref()[..4].try_into().unwrap());
65-
Self {
66-
major: env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(),
67-
minor: env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(),
68-
patch: env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(),
69-
commit: compute_commit(option_env!("CI_COMMIT"))
70-
.or(compute_commit(option_env!("AGAVE_GIT_COMMIT_HASH")))
71-
.unwrap_or_else(|| rng().random::<u32>()),
72-
feature_set,
73-
// Other client implementations need to modify this line.
74-
client: u16::try_from(ClientId::Agave).unwrap(),
75-
}
76-
}
77-
}
78-
79-
impl fmt::Display for Version {
80-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81-
write!(f, "{}.{}.{}", self.major, self.minor, self.patch,)
82-
}
83-
}
84-
85-
impl fmt::Debug for Version {
86-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87-
write!(
88-
f,
89-
"{}.{}.{} (src:{:08x}; feat:{}, client:{:?})",
90-
self.major,
91-
self.minor,
92-
self.patch,
93-
self.commit,
94-
self.feature_set,
95-
self.client(),
96-
)
97-
}
98-
}
99-
100-
impl Sanitize for Version {}
101-
102-
impl From<u16> for ClientId {
103-
fn from(client: u16) -> Self {
104-
match client {
105-
0u16 => Self::SolanaLabs,
106-
1u16 => Self::JitoLabs,
107-
2u16 => Self::Frankendancer,
108-
3u16 => Self::Agave,
109-
4u16 => Self::AgavePaladin,
110-
5u16 => Self::Firedancer,
111-
6u16 => Self::AgaveBam,
112-
7u16 => Self::Sig,
113-
_ => Self::Unknown(client),
114-
}
115-
}
116-
}
117-
118-
impl TryFrom<ClientId> for u16 {
119-
type Error = String;
120-
121-
fn try_from(client: ClientId) -> Result<Self, Self::Error> {
122-
match client {
123-
ClientId::SolanaLabs => Ok(0u16),
124-
ClientId::JitoLabs => Ok(1u16),
125-
ClientId::Frankendancer => Ok(2u16),
126-
ClientId::Agave => Ok(3u16),
127-
ClientId::AgavePaladin => Ok(4u16),
128-
ClientId::Firedancer => Ok(5u16),
129-
ClientId::AgaveBam => Ok(6u16),
130-
ClientId::Sig => Ok(7u16),
131-
ClientId::Unknown(client @ 0u16..=7u16) => Err(format!("Invalid client: {client}")),
132-
ClientId::Unknown(client) => Ok(client),
133-
}
134-
}
135-
}
136-
13719
#[macro_export]
13820
macro_rules! semver {
13921
() => {
@@ -159,36 +41,4 @@ mod test {
15941
assert_eq!(compute_commit(Some("HEAD")), None);
16042
assert_eq!(compute_commit(Some("garbagein")), None);
16143
}
162-
163-
#[test]
164-
fn test_client_id() {
165-
assert_eq!(ClientId::from(0u16), ClientId::SolanaLabs);
166-
assert_eq!(ClientId::from(1u16), ClientId::JitoLabs);
167-
assert_eq!(ClientId::from(2u16), ClientId::Frankendancer);
168-
assert_eq!(ClientId::from(3u16), ClientId::Agave);
169-
assert_eq!(ClientId::from(4u16), ClientId::AgavePaladin);
170-
assert_eq!(ClientId::from(5u16), ClientId::Firedancer);
171-
assert_eq!(ClientId::from(6u16), ClientId::AgaveBam);
172-
assert_eq!(ClientId::from(7u16), ClientId::Sig);
173-
for client in 8u16..=u16::MAX {
174-
assert_eq!(ClientId::from(client), ClientId::Unknown(client));
175-
}
176-
assert_eq!(u16::try_from(ClientId::SolanaLabs), Ok(0u16));
177-
assert_eq!(u16::try_from(ClientId::JitoLabs), Ok(1u16));
178-
assert_eq!(u16::try_from(ClientId::Frankendancer), Ok(2u16));
179-
assert_eq!(u16::try_from(ClientId::Agave), Ok(3u16));
180-
assert_eq!(u16::try_from(ClientId::AgavePaladin), Ok(4u16));
181-
assert_eq!(u16::try_from(ClientId::Firedancer), Ok(5u16));
182-
assert_eq!(u16::try_from(ClientId::AgaveBam), Ok(6u16));
183-
assert_eq!(u16::try_from(ClientId::Sig), Ok(7u16));
184-
for client in 0..=7u16 {
185-
assert_eq!(
186-
u16::try_from(ClientId::Unknown(client)),
187-
Err(format!("Invalid client: {client}"))
188-
);
189-
}
190-
for client in 8u16..=u16::MAX {
191-
assert_eq!(u16::try_from(ClientId::Unknown(client)), Ok(client));
192-
}
193-
}
19444
}

version/src/v1.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use {
2+
serde::{Deserialize, Serialize},
3+
solana_sanitize::Sanitize,
4+
};
5+
6+
// Older version structure used earlier 1.3.x releases
7+
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
8+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
9+
pub struct Version {
10+
major: u16,
11+
minor: u16,
12+
patch: u16,
13+
commit: Option<u32>, // first 4 bytes of the sha1 commit hash
14+
}
15+
16+
impl Sanitize for Version {}
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,17 @@ use {
55
std::{convert::TryInto, fmt},
66
};
77

8-
// Older version structure used earlier 1.3.x releases
9-
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
10-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
11-
pub struct LegacyVersion1 {
12-
major: u16,
13-
minor: u16,
14-
patch: u16,
15-
commit: Option<u32>, // first 4 bytes of the sha1 commit hash
16-
}
17-
18-
impl Sanitize for LegacyVersion1 {}
19-
208
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
219
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
22-
pub struct LegacyVersion2 {
10+
pub struct Version {
2311
pub major: u16,
2412
pub minor: u16,
2513
pub patch: u16,
2614
pub commit: Option<u32>, // first 4 bytes of the sha1 commit hash
2715
pub feature_set: u32, // first 4 bytes of the FeatureSet identifier
2816
}
2917

30-
impl Default for LegacyVersion2 {
18+
impl Default for Version {
3119
fn default() -> Self {
3220
let feature_set =
3321
u32::from_le_bytes(agave_feature_set::ID.as_ref()[..4].try_into().unwrap());
@@ -41,7 +29,7 @@ impl Default for LegacyVersion2 {
4129
}
4230
}
4331

44-
impl fmt::Debug for LegacyVersion2 {
32+
impl fmt::Debug for Version {
4533
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4634
write!(
4735
f,
@@ -58,4 +46,4 @@ impl fmt::Debug for LegacyVersion2 {
5846
}
5947
}
6048

61-
impl Sanitize for LegacyVersion2 {}
49+
impl Sanitize for Version {}

0 commit comments

Comments
 (0)