Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements From<ContactInfo> for CrdsData #4405

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions gossip/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1261,10 +1261,7 @@ impl ClusterInfo {
Vec<(SocketAddr, Protocol)>, // Pull requests
) {
let now = timestamp();
let self_info = CrdsValue::new(
CrdsData::ContactInfo(self.my_contact_info()),
&self.keypair(),
);
let self_info = CrdsValue::new(CrdsData::from(self.my_contact_info()), &self.keypair());
let max_bloom_filter_bytes = get_max_bloom_filter_bytes(&self_info);
let mut pings = Vec::new();
let mut pulls = {
Expand Down Expand Up @@ -3337,7 +3334,7 @@ mod tests {

fn test_crds_values(pubkey: Pubkey) -> Vec<CrdsValue> {
let entrypoint = ContactInfo::new_localhost(&pubkey, timestamp());
let entrypoint_crdsvalue = CrdsValue::new_unsigned(CrdsData::ContactInfo(entrypoint));
let entrypoint_crdsvalue = CrdsValue::new_unsigned(CrdsData::from(entrypoint));
vec![entrypoint_crdsvalue]
}

Expand Down Expand Up @@ -3806,7 +3803,7 @@ mod tests {
node.set_shred_version(42);
let epoch_slots = EpochSlots::new_rand(&mut rng, Some(node_pubkey));
let entries = vec![
CrdsValue::new_unsigned(CrdsData::ContactInfo(node)),
CrdsValue::new_unsigned(CrdsData::from(node)),
CrdsValue::new_unsigned(CrdsData::EpochSlots(0, epoch_slots)),
];
{
Expand Down Expand Up @@ -3862,8 +3859,7 @@ mod tests {
}
}
// now add this message back to the table and make sure after the next pull, the entrypoint is unset
let entrypoint_crdsvalue =
CrdsValue::new_unsigned(CrdsData::ContactInfo(entrypoint.clone()));
let entrypoint_crdsvalue = CrdsValue::new_unsigned(CrdsData::from(&entrypoint));
let cluster_info = Arc::new(cluster_info);
let stakes = HashMap::from([(Pubkey::new_unique(), 1u64)]);
let timeouts = cluster_info.gossip.make_timeouts(
Expand Down Expand Up @@ -4178,7 +4174,7 @@ mod tests {
let mut rand_ci = ContactInfo::new_rand(&mut rng, Some(keypair.pubkey()));
rand_ci.set_shred_version(shred_version);
rand_ci.set_wallclock(timestamp());
CrdsValue::new(CrdsData::ContactInfo(rand_ci), &keypair)
CrdsValue::new(CrdsData::from(rand_ci), &keypair)
})
.take(NO_ENTRIES)
.collect();
Expand Down Expand Up @@ -4291,7 +4287,7 @@ mod tests {
let mut slots = RestartLastVotedForkSlots::new_rand(&mut rng, Some(node_pubkey));
slots.shred_version = 42;
let entries = vec![
CrdsValue::new_unsigned(CrdsData::ContactInfo(node)),
CrdsValue::new_unsigned(CrdsData::from(node)),
CrdsValue::new_unsigned(CrdsData::RestartLastVotedForkSlots(slots)),
];
{
Expand Down Expand Up @@ -4361,7 +4357,7 @@ mod tests {
let hash2 = Hash::new_unique();
let stake2 = 23_000_000;
let entries = vec![
CrdsValue::new_unsigned(CrdsData::ContactInfo(new_node)),
CrdsValue::new_unsigned(CrdsData::from(new_node)),
CrdsValue::new_unsigned(CrdsData::RestartHeaviestFork(RestartHeaviestFork {
from: pubkey2,
wallclock: timestamp(),
Expand Down
40 changes: 20 additions & 20 deletions gossip/src/crds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ mod tests {
#[test]
fn test_insert() {
let mut crds = Crds::default();
let val = CrdsValue::new_unsigned(CrdsData::ContactInfo(ContactInfo::default()));
let val = CrdsValue::new_unsigned(CrdsData::from(ContactInfo::default()));
assert_eq!(
crds.insert(val.clone(), 0, GossipRoute::LocalMessage),
Ok(())
Expand All @@ -807,7 +807,7 @@ mod tests {
#[test]
fn test_update_old() {
let mut crds = Crds::default();
let val = CrdsValue::new_unsigned(CrdsData::ContactInfo(ContactInfo::default()));
let val = CrdsValue::new_unsigned(CrdsData::from(ContactInfo::default()));
assert_eq!(
crds.insert(val.clone(), 0, GossipRoute::LocalMessage),
Ok(())
Expand All @@ -822,13 +822,13 @@ mod tests {
#[test]
fn test_update_new() {
let mut crds = Crds::default();
let original = CrdsValue::new_unsigned(CrdsData::ContactInfo(ContactInfo::new_localhost(
let original = CrdsValue::new_unsigned(CrdsData::from(ContactInfo::new_localhost(
&Pubkey::default(),
0,
)));
let value_hash = *original.hash();
assert_matches!(crds.insert(original, 0, GossipRoute::LocalMessage), Ok(()));
let val = CrdsValue::new_unsigned(CrdsData::ContactInfo(ContactInfo::new_localhost(
let val = CrdsValue::new_unsigned(CrdsData::from(ContactInfo::new_localhost(
&Pubkey::default(),
1,
)));
Expand All @@ -842,7 +842,7 @@ mod tests {
#[test]
fn test_update_timestamp() {
let mut crds = Crds::default();
let val1 = CrdsValue::new_unsigned(CrdsData::ContactInfo(ContactInfo::new_localhost(
let val1 = CrdsValue::new_unsigned(CrdsData::from(ContactInfo::new_localhost(
&Pubkey::default(),
0,
)));
Expand All @@ -855,7 +855,7 @@ mod tests {
assert_eq!(crds.table[&val1.label()].ordinal, 0);

// `val2` is expected to overwrite `val1` based on the `wallclock` value.
let val2 = CrdsValue::new_unsigned(CrdsData::ContactInfo(ContactInfo::new_localhost(
let val2 = CrdsValue::new_unsigned(CrdsData::from(ContactInfo::new_localhost(
&Pubkey::default(),
1,
)));
Expand Down Expand Up @@ -939,7 +939,7 @@ mod tests {
let mut crds = Crds::default();
let val = {
let node = ContactInfo::new_localhost(&Pubkey::default(), /*now:*/ 1);
CrdsValue::new_unsigned(CrdsData::ContactInfo(node))
CrdsValue::new_unsigned(CrdsData::from(node))
};
assert_eq!(
crds.insert(val.clone(), 1, GossipRoute::LocalMessage),
Expand Down Expand Up @@ -1036,7 +1036,7 @@ mod tests {
fn test_remove_default() {
let thread_pool = ThreadPoolBuilder::new().build().unwrap();
let mut crds = Crds::default();
let val = CrdsValue::new_unsigned(CrdsData::ContactInfo(ContactInfo::default()));
let val = CrdsValue::new_unsigned(CrdsData::from(ContactInfo::default()));
assert_matches!(
crds.insert(val.clone(), 1, GossipRoute::LocalMessage),
Ok(_)
Expand All @@ -1061,7 +1061,7 @@ mod tests {
let mut crds = Crds::default();
let val = {
let node = ContactInfo::new_localhost(&Pubkey::default(), /*now:*/ 1);
CrdsValue::new_unsigned(CrdsData::ContactInfo(node))
CrdsValue::new_unsigned(CrdsData::from(node))
};
assert_eq!(
crds.insert(val.clone(), 1, GossipRoute::LocalMessage),
Expand Down Expand Up @@ -1337,7 +1337,7 @@ mod tests {
let wallclock = node.wallclock();
node.set_shred_version(42);
{
let node = CrdsData::ContactInfo(node.clone());
let node = CrdsData::from(&node);
let node = CrdsValue::new_unsigned(node);
assert_eq!(
crds.insert(node, timestamp(), GossipRoute::LocalMessage),
Expand All @@ -1349,7 +1349,7 @@ mod tests {
let mut node = node.clone();
node.set_wallclock(wallclock - 1); // outdated.
node.set_shred_version(8);
let node = CrdsData::ContactInfo(node);
let node = CrdsData::from(node);
let node = CrdsValue::new_unsigned(node);
assert_eq!(
crds.insert(node, timestamp(), GossipRoute::LocalMessage),
Expand All @@ -1360,7 +1360,7 @@ mod tests {
let mut node = ContactInfo::new_rand(&mut rng, Some(pubkey));
node.set_wallclock(wallclock + 1); // so that it overrides the prev one.
node.set_shred_version(8);
let node = CrdsData::ContactInfo(node);
let node = CrdsData::from(node);
let node = CrdsValue::new_unsigned(node);
assert_eq!(
crds.insert(node, timestamp(), GossipRoute::LocalMessage),
Expand Down Expand Up @@ -1450,7 +1450,7 @@ mod tests {
fn test_remove_staked() {
let thread_pool = ThreadPoolBuilder::new().build().unwrap();
let mut crds = Crds::default();
let val = CrdsValue::new_unsigned(CrdsData::ContactInfo(ContactInfo::default()));
let val = CrdsValue::new_unsigned(CrdsData::from(ContactInfo::default()));
assert_matches!(
crds.insert(val.clone(), 1, GossipRoute::LocalMessage),
Ok(_)
Expand All @@ -1473,7 +1473,7 @@ mod tests {
#[test]
#[allow(clippy::neg_cmp_op_on_partial_ord)]
fn test_equal() {
let val = CrdsValue::new_unsigned(CrdsData::ContactInfo(ContactInfo::default()));
let val = CrdsValue::new_unsigned(CrdsData::from(ContactInfo::default()));
let v1 =
VersionedCrdsValue::new(val.clone(), Cursor::default(), 1, GossipRoute::LocalMessage);
let v2 = VersionedCrdsValue::new(val, Cursor::default(), 1, GossipRoute::LocalMessage);
Expand All @@ -1487,15 +1487,15 @@ mod tests {
fn test_hash_order() {
let mut node = ContactInfo::new_localhost(&Pubkey::default(), 0);
let v1 = VersionedCrdsValue::new(
CrdsValue::new_unsigned(CrdsData::ContactInfo(node.clone())),
CrdsValue::new_unsigned(CrdsData::from(&node)),
Cursor::default(),
1, // local_timestamp
GossipRoute::LocalMessage,
);
let v2 = VersionedCrdsValue::new(
{
node.set_rpc((Ipv4Addr::LOCALHOST, 1244)).unwrap();
CrdsValue::new_unsigned(CrdsData::ContactInfo(node))
CrdsValue::new_unsigned(CrdsData::from(node))
},
Cursor::default(),
1, // local_timestamp
Expand All @@ -1520,14 +1520,14 @@ mod tests {
fn test_wallclock_order() {
let mut node = ContactInfo::new_localhost(&Pubkey::default(), 1);
let v1 = VersionedCrdsValue::new(
CrdsValue::new_unsigned(CrdsData::ContactInfo(node.clone())),
CrdsValue::new_unsigned(CrdsData::from(&node)),
Cursor::default(),
1, // local_timestamp
GossipRoute::LocalMessage,
);
node.set_wallclock(0);
let v2 = VersionedCrdsValue::new(
CrdsValue::new_unsigned(CrdsData::ContactInfo(node)),
CrdsValue::new_unsigned(CrdsData::from(node)),
Cursor::default(),
1, // local_timestamp
GossipRoute::LocalMessage,
Expand All @@ -1543,7 +1543,7 @@ mod tests {
#[allow(clippy::neg_cmp_op_on_partial_ord)]
fn test_label_order() {
let v1 = VersionedCrdsValue::new(
CrdsValue::new_unsigned(CrdsData::ContactInfo(ContactInfo::new_localhost(
CrdsValue::new_unsigned(CrdsData::from(ContactInfo::new_localhost(
&solana_pubkey::new_rand(),
0,
))),
Expand All @@ -1552,7 +1552,7 @@ mod tests {
GossipRoute::LocalMessage,
);
let v2 = VersionedCrdsValue::new(
CrdsValue::new_unsigned(CrdsData::ContactInfo(ContactInfo::new_localhost(
CrdsValue::new_unsigned(CrdsData::from(ContactInfo::new_localhost(
&solana_pubkey::new_rand(),
0,
))),
Expand Down
16 changes: 15 additions & 1 deletion gossip/src/crds_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl CrdsData {
// TODO: Assign ranges to each arm proportional to their frequency in
// the mainnet crds table.
match kind {
0 => CrdsData::ContactInfo(ContactInfo::new_rand(rng, pubkey)),
0 => CrdsData::from(ContactInfo::new_rand(rng, pubkey)),
// Index for LowestSlot is deprecated and should be zero.
1 => CrdsData::LowestSlot(0, LowestSlot::new_rand(rng, pubkey)),
2 => CrdsData::LegacySnapshotHashes(LegacySnapshotHashes::new_rand(rng, pubkey)),
Expand Down Expand Up @@ -200,6 +200,20 @@ impl CrdsData {
}
}

impl From<ContactInfo> for CrdsData {
#[inline]
fn from(node: ContactInfo) -> Self {
Self::ContactInfo(node)
}
}

impl From<&ContactInfo> for CrdsData {
#[inline]
fn from(node: &ContactInfo) -> Self {
Self::ContactInfo(node.clone())
}
}

#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub(crate) struct AccountsHashes {
Expand Down
2 changes: 1 addition & 1 deletion gossip/src/crds_gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ mod test {
.write()
.unwrap()
.insert(
CrdsValue::new_unsigned(CrdsData::ContactInfo(ci.clone())),
CrdsValue::new_unsigned(CrdsData::from(&ci)),
0,
GossipRoute::LocalMessage,
)
Expand Down
Loading
Loading