Skip to content

Commit

Permalink
ordinal rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Pyattaev committed Feb 11, 2025
1 parent 0975a9f commit c2790d2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions gossip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ solana-version = { workspace = true }
solana-vote = { workspace = true }
solana-vote-program = { workspace = true }
static_assertions = { workspace = true }
strum = { workspace = true, features = ["derive"] }
thiserror = { workspace = true }

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions gossip/src/cluster_info_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ pub(crate) fn submit_gossip_stats(
i64
),
);

datapoint_info!(
"cluster_info_crds_stats",
("LegacyContactInfo-push", crds_stats.push.counts[0], i64),
Expand Down
21 changes: 3 additions & 18 deletions gossip/src/crds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use {
ops::{Bound, Index, IndexMut},
sync::Mutex,
},
strum::EnumCount,
};

const CRDS_SHARDS_BITS: u32 = 12;
Expand Down Expand Up @@ -99,7 +100,7 @@ pub enum GossipRoute<'a> {
PushMessage(/*from:*/ &'a Pubkey),
}

type CrdsCountsArray = [usize; 14];
type CrdsCountsArray = [usize; CrdsData::COUNT];

pub(crate) struct CrdsDataStats {
pub(crate) counts: CrdsCountsArray,
Expand Down Expand Up @@ -728,23 +729,7 @@ impl CrdsDataStats {
}

fn ordinal(entry: &VersionedCrdsValue) -> usize {
match entry.value.data() {
CrdsData::LegacyContactInfo(_) => 0,
CrdsData::Vote(_, _) => 1,
CrdsData::LowestSlot(_, _) => 2,
CrdsData::LegacySnapshotHashes(_) => 3,
CrdsData::AccountsHashes(_) => 4,
CrdsData::EpochSlots(_, _) => 5,
CrdsData::LegacyVersion(_) => 6,
CrdsData::Version(_) => 7,
CrdsData::NodeInstance(_) => 8,
CrdsData::DuplicateShred(_, _) => 9,
CrdsData::SnapshotHashes(_) => 10,
CrdsData::ContactInfo(_) => 11,
CrdsData::RestartLastVotedForkSlots(_) => 12,
CrdsData::RestartHeaviestFork(_) => 13,
// Update CrdsCountsArray if new items are added here.
}
entry.value.data().ordinal() as usize
}
}

Expand Down
35 changes: 34 additions & 1 deletion gossip/src/crds_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use {
},
solana_vote::vote_parser,
std::{cmp::Ordering, collections::BTreeSet},
strum::EnumCount,
};

pub(crate) const MAX_WALLCLOCK: u64 = 1_000_000_000_000_000;
Expand All @@ -41,7 +42,7 @@ pub(crate) const MAX_EPOCH_SLOTS: EpochSlotsIndex = 255;
/// * LowestSlot index is deprecated
#[allow(clippy::large_enum_variant)]
#[cfg_attr(feature = "frozen-abi", derive(AbiExample, AbiEnumVisitor))]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, EnumCount)]
pub enum CrdsData {
#[allow(private_interfaces)]
LegacyContactInfo(LegacyContactInfo),
Expand Down Expand Up @@ -176,6 +177,26 @@ impl CrdsData {
}
}

/// Returns the index of the CrdsData in the enum that matches wire format byte
pub fn ordinal(&self) -> usize {
match self {
CrdsData::LegacyContactInfo(_) => 0,
CrdsData::Vote(_, _) => 1,
CrdsData::LowestSlot(_, _) => 2,
CrdsData::LegacySnapshotHashes(_) => 3,
CrdsData::AccountsHashes(_) => 4,
CrdsData::EpochSlots(_, _) => 5,
CrdsData::LegacyVersion(_) => 6,
CrdsData::Version(_) => 7,
CrdsData::NodeInstance(_) => 8,
CrdsData::DuplicateShred(_, _) => 9,
CrdsData::SnapshotHashes(_) => 10,
CrdsData::ContactInfo(_) => 11,
CrdsData::RestartLastVotedForkSlots(_) => 12,
CrdsData::RestartHeaviestFork(_) => 13,
}
}

#[inline]
#[must_use]
pub(crate) fn is_deprecated(&self) -> bool {
Expand Down Expand Up @@ -511,6 +532,18 @@ mod test {
solana_vote_program::{vote_instruction, vote_state},
};

#[test]
fn test_to_ordinal() {
let mut rng = rand::thread_rng();
for _ in 0..100 {
let value = CrdsData::new_rand(&mut rng, None);
let bytes = bincode::serialize(&value).unwrap();
assert!(value.ordinal() < CrdsData::COUNT);
let discriminant = bytes[0];
assert_eq!(discriminant as usize, value.ordinal());
}
}

#[test]
fn test_lowest_slot_sanitize() {
let ls = LowestSlot::new(Pubkey::default(), 0, 0);
Expand Down

0 comments on commit c2790d2

Please sign in to comment.