Skip to content

Commit

Permalink
tests that Vec<CrdsValue> (de)serialization round trips (#3710)
Browse files Browse the repository at this point in the history
#3575
reworks CrdsValue bincode (de)serialization.

In order to maintain compatibility and correctness, the commit adds a
round trip test for Vec<CrdsValue> (de)serialization and also verifies
the serialized bytes against a hard-coded hash.
  • Loading branch information
behzadnouri authored Nov 20, 2024
1 parent a8fd26f commit fe7d37a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
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
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions gossip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
serial_test = { workspace = true }
solana-perf = { workspace = true, features = ["dev-context-only-utils"] }
solana-runtime = { workspace = true, features = ["dev-context-only-utils"] }
Expand Down
90 changes: 90 additions & 0 deletions gossip/src/crds_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
);
}
}

0 comments on commit fe7d37a

Please sign in to comment.