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

tests that Vec<CrdsValue> (de)serialization round trips #3710

Merged
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
3 changes: 3 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -377,6 +377,7 @@ quote = "1.0"
rand = "0.8.5"
rand0-7 = { package = "rand", version = "0.7" }
rand_chacha = "0.3.1"
rand_chacha0-2 = { package = "rand_chacha", version = "0.2.2" }
rayon = "1.10.0"
reed-solomon-erasure = "6.0.0"
regex = "1.11.1"
3 changes: 3 additions & 0 deletions gossip/Cargo.toml
Original file line number Diff line number Diff line change
@@ -61,7 +61,10 @@ static_assertions = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
bs58 = { workspace = true }
num_cpus = { workspace = true }
rand0-7 = { workspace = true }
rand_chacha0-2 = { workspace = true }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need rand_cha_cha v2 here? Can we not get away with v3?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will not compile unless I use older versions for both rand and rand_chacha because Keypair::generate requires a rand 0.7 CryptoRng and rand_chacha 0.3 implements rand 0.8 CryptoRng which is not the same as 0.7 version.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love dependency hell lol. sounds good!

serial_test = { workspace = true }
solana-perf = { workspace = true, features = ["dev-context-only-utils"] }
solana-runtime = { workspace = true, features = ["dev-context-only-utils"] }
90 changes: 90 additions & 0 deletions gossip/src/crds_value.rs
Original file line number Diff line number Diff line change
@@ -243,11 +243,16 @@ mod test {
super::*,
crate::crds_data::{LowestSlot, NodeInstance, Vote},
bincode::deserialize,
rand0_7::{Rng, SeedableRng},
rand_chacha0_2::ChaChaRng,
solana_perf::test_tx::new_test_vote_tx,
solana_sdk::{
signature::{Keypair, Signer},
timing::timestamp,
vote::state::TowerSync,
},
solana_vote_program::{vote_state::Lockout, vote_transaction::new_tower_sync_transaction},
std::str::FromStr,
};

#[test]
@@ -348,4 +353,89 @@ mod test {
assert!(node.should_force_push(&pubkey));
assert!(!node.should_force_push(&Pubkey::new_unique()));
}

#[test]
fn test_serialize_round_trip() {
let mut rng = ChaChaRng::from_seed(
bs58::decode("4nHgVgCvVaHnsrg4dYggtvWYYgV3JbeyiRBWupPMt3EG")
.into_vec()
.map(<[u8; 32]>::try_from)
.unwrap()
.unwrap(),
);
let values: Vec<CrdsValue> = vec![
{
let keypair = Keypair::generate(&mut rng);
let lockouts: [Lockout; 4] = [
Lockout::new_with_confirmation_count(302_388_991, 11),
Lockout::new_with_confirmation_count(302_388_995, 7),
Lockout::new_with_confirmation_count(302_389_001, 3),
Lockout::new_with_confirmation_count(302_389_005, 1),
];
let tower_sync = TowerSync {
lockouts: lockouts.into_iter().collect(),
root: Some(302_388_989),
hash: Hash::new_from_array(rng.gen()),
timestamp: Some(1_732_044_716_167),
block_id: Hash::new_from_array(rng.gen()),
};
let vote = new_tower_sync_transaction(
tower_sync,
Hash::new_from_array(rng.gen()), // blockhash
&keypair, // node_keypair
&Keypair::generate(&mut rng), // vote_keypair
&Keypair::generate(&mut rng), // authorized_voter_keypair
None, // switch_proof_hash
);
let vote = Vote::new(
keypair.pubkey(),
vote,
1_732_045_236_371, // wallclock
)
.unwrap();
CrdsValue::new(CrdsData::Vote(5, vote), &keypair)
},
{
let keypair = Keypair::generate(&mut rng);
let lockouts: [Lockout; 3] = [
Lockout::new_with_confirmation_count(302_410_500, 9),
Lockout::new_with_confirmation_count(302_410_505, 5),
Lockout::new_with_confirmation_count(302_410_517, 1),
];
let tower_sync = TowerSync {
lockouts: lockouts.into_iter().collect(),
root: Some(302_410_499),
hash: Hash::new_from_array(rng.gen()),
timestamp: Some(1_732_053_615_237),
block_id: Hash::new_from_array(rng.gen()),
};
let vote = new_tower_sync_transaction(
tower_sync,
Hash::new_from_array(rng.gen()), // blockhash
&keypair, // node_keypair
&Keypair::generate(&mut rng), // vote_keypair
&Keypair::generate(&mut rng), // authorized_voter_keypair
None, // switch_proof_hash
);
let vote = Vote::new(
keypair.pubkey(),
vote,
1_732_053_639_350, // wallclock
)
.unwrap();
CrdsValue::new(CrdsData::Vote(5, vote), &keypair)
},
];
let bytes = bincode::serialize(&values).unwrap();
// Serialized bytes are fixed and should never change.
assert_eq!(
solana_sdk::hash::hash(&bytes),
Hash::from_str("7gtcoafccWE964njbs2bA1QuVFeV34RaoY781yLx2A8N").unwrap()
);
// serialize -> deserialize should round trip.
assert_eq!(
bincode::deserialize::<Vec<CrdsValue>>(&bytes).unwrap(),
values
);
}
}