Skip to content

Commit

Permalink
signed repair request test fixes/cleanup (#28691)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbiseda authored Nov 16, 2022
1 parent ad24e37 commit e10d958
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
15 changes: 10 additions & 5 deletions core/src/ancestor_hashes_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,10 @@ mod test {
},
solana_ledger::{blockstore::make_many_slot_entries, get_tmp_ledger_path},
solana_runtime::{accounts_background_service::AbsRequestSender, bank_forks::BankForks},
solana_sdk::{hash::Hash, signature::Keypair},
solana_sdk::{
hash::Hash,
signature::{Keypair, Signer},
},
solana_streamer::socket::SocketAddrSpace,
std::collections::HashMap,
trees::tr,
Expand Down Expand Up @@ -959,10 +962,11 @@ mod test {
fn new(slot_to_query: Slot) -> Self {
assert!(slot_to_query >= MAX_ANCESTOR_RESPONSES as Slot);
let vote_simulator = VoteSimulator::new(3);
let responder_node = Node::new_localhost();
let keypair = Keypair::new();
let responder_node = Node::new_localhost_with_pubkey(&keypair.pubkey());
let cluster_info = ClusterInfo::new(
responder_node.info.clone(),
Arc::new(Keypair::new()),
Arc::new(keypair),
SocketAddrSpace::Unspecified,
);
let responder_serve_repair =
Expand Down Expand Up @@ -1044,9 +1048,10 @@ mod test {
let ancestor_hashes_request_statuses = Arc::new(DashMap::new());
let ancestor_hashes_request_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").unwrap());
let epoch_schedule = *bank_forks.read().unwrap().root_bank().epoch_schedule();
let keypair = Keypair::new();
let requester_cluster_info = Arc::new(ClusterInfo::new(
Node::new_localhost().info,
Arc::new(Keypair::new()),
Node::new_localhost_with_pubkey(&keypair.pubkey()).info,
Arc::new(keypair),
SocketAddrSpace::Unspecified,
));
let requester_serve_repair =
Expand Down
55 changes: 41 additions & 14 deletions core/src/serve_repair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1375,10 +1375,8 @@ mod tests {

#[test]
fn test_verify_signed_packet() {
let keypair = Keypair::new();
let my_keypair = Keypair::new();
let other_keypair = Keypair::new();
let my_id = Pubkey::new_unique();
let other_id = Pubkey::new_unique();

fn sign_packet(packet: &mut Packet, keypair: &Keypair) {
let signable_data = [
Expand All @@ -1392,58 +1390,84 @@ mod tests {

// well formed packet
let packet = {
let header = RepairRequestHeader::new(keypair.pubkey(), my_id, timestamp(), 678);
let header = RepairRequestHeader::new(
my_keypair.pubkey(),
other_keypair.pubkey(),
timestamp(),
678,
);
let slot = 239847;
let request = RepairProtocol::Orphan { header, slot };
let mut packet = Packet::from_data(None, &request).unwrap();
sign_packet(&mut packet, &keypair);
sign_packet(&mut packet, &my_keypair);
packet
};
let request: RepairProtocol = packet.deserialize_slice(..).unwrap();
assert!(ServeRepair::verify_signed_packet(
&my_id,
&other_keypair.pubkey(),
&packet,
&request,
&mut ServeRepairStats::default(),
));

// recipient mismatch
let packet = {
let header = RepairRequestHeader::new(keypair.pubkey(), other_id, timestamp(), 678);
let header = RepairRequestHeader::new(
my_keypair.pubkey(),
other_keypair.pubkey(),
timestamp(),
678,
);
let slot = 239847;
let request = RepairProtocol::Orphan { header, slot };
let mut packet = Packet::from_data(None, &request).unwrap();
sign_packet(&mut packet, &keypair);
sign_packet(&mut packet, &my_keypair);
packet
};
let request: RepairProtocol = packet.deserialize_slice(..).unwrap();
let mut stats = ServeRepairStats::default();
assert!(!ServeRepair::verify_signed_packet(
&my_id, &packet, &request, &mut stats,
&my_keypair.pubkey(),
&packet,
&request,
&mut stats,
));
assert_eq!(stats.err_id_mismatch, 1);

// outside time window
let packet = {
let time_diff_ms = u64::try_from(SIGNED_REPAIR_TIME_WINDOW.as_millis() * 2).unwrap();
let old_timestamp = timestamp().saturating_sub(time_diff_ms);
let header = RepairRequestHeader::new(keypair.pubkey(), my_id, old_timestamp, 678);
let header = RepairRequestHeader::new(
my_keypair.pubkey(),
other_keypair.pubkey(),
old_timestamp,
678,
);
let slot = 239847;
let request = RepairProtocol::Orphan { header, slot };
let mut packet = Packet::from_data(None, &request).unwrap();
sign_packet(&mut packet, &keypair);
sign_packet(&mut packet, &my_keypair);
packet
};
let request: RepairProtocol = packet.deserialize_slice(..).unwrap();
let mut stats = ServeRepairStats::default();
assert!(!ServeRepair::verify_signed_packet(
&my_id, &packet, &request, &mut stats,
&other_keypair.pubkey(),
&packet,
&request,
&mut stats,
));
assert_eq!(stats.err_time_skew, 1);

// bad signature
let packet = {
let header = RepairRequestHeader::new(keypair.pubkey(), my_id, timestamp(), 678);
let header = RepairRequestHeader::new(
my_keypair.pubkey(),
other_keypair.pubkey(),
timestamp(),
678,
);
let slot = 239847;
let request = RepairProtocol::Orphan { header, slot };
let mut packet = Packet::from_data(None, &request).unwrap();
Expand All @@ -1453,7 +1477,10 @@ mod tests {
let request: RepairProtocol = packet.deserialize_slice(..).unwrap();
let mut stats = ServeRepairStats::default();
assert!(!ServeRepair::verify_signed_packet(
&my_id, &packet, &request, &mut stats,
&other_keypair.pubkey(),
&packet,
&request,
&mut stats,
));
assert_eq!(stats.err_sig_verify, 1);
}
Expand Down

0 comments on commit e10d958

Please sign in to comment.