Skip to content

Commit

Permalink
sdk: Make Packet::meta private, use accessor functions (solana-labs#2…
Browse files Browse the repository at this point in the history
…9092)

sdk: Make packet meta private
  • Loading branch information
joncinque authored Dec 6, 2022
1 parent c106c7e commit b1340d7
Show file tree
Hide file tree
Showing 39 changed files with 270 additions and 249 deletions.
8 changes: 4 additions & 4 deletions bench-streamer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ fn producer(addr: &SocketAddr, exit: Arc<AtomicBool>) -> JoinHandle<()> {
let mut packet_batch = PacketBatch::with_capacity(batch_size);
packet_batch.resize(batch_size, Packet::default());
for w in packet_batch.iter_mut() {
w.meta.size = PACKET_DATA_SIZE;
w.meta.set_socket_addr(addr);
w.meta_mut().size = PACKET_DATA_SIZE;
w.meta_mut().set_socket_addr(addr);
}
let packet_batch = Arc::new(packet_batch);
spawn(move || loop {
Expand All @@ -35,8 +35,8 @@ fn producer(addr: &SocketAddr, exit: Arc<AtomicBool>) -> JoinHandle<()> {
}
let mut num = 0;
for p in packet_batch.iter() {
let a = p.meta.socket_addr();
assert!(p.meta.size <= PACKET_DATA_SIZE);
let a = p.meta().socket_addr();
assert!(p.meta().size <= PACKET_DATA_SIZE);
let data = p.data(..).unwrap_or_default();
send.send_to(data, a).unwrap();
num += 1;
Expand Down
2 changes: 1 addition & 1 deletion core/benches/banking_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ fn bench_banking(bencher: &mut Bencher, tx_type: TransactionType) {
let mut packet_batches = to_packet_batches(&vote_txs, PACKETS_PER_BATCH);
for batch in packet_batches.iter_mut() {
for packet in batch.iter_mut() {
packet.meta.set_simple_vote(true);
packet.meta_mut().set_simple_vote(true);
}
}
packet_batches
Expand Down
14 changes: 7 additions & 7 deletions core/benches/sigverify_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn run_bench_packet_discard(num_ips: usize, bencher: &mut Bencher) {
total += batch.len();
for p in batch.iter_mut() {
let ip_index = thread_rng().gen_range(0, ips.len());
p.meta.addr = ips[ip_index];
p.meta_mut().addr = ips[ip_index];
}
}
info!("total packets: {}", total);
Expand All @@ -62,10 +62,10 @@ fn run_bench_packet_discard(num_ips: usize, bencher: &mut Bencher) {
let mut num_packets = 0;
for batch in batches.iter_mut() {
for p in batch.iter_mut() {
if !p.meta.discard() {
if !p.meta().discard() {
num_packets += 1;
}
p.meta.set_discard(false);
p.meta_mut().set_discard(false);
}
}
assert_eq!(num_packets, 10_000);
Expand Down Expand Up @@ -97,7 +97,7 @@ fn bench_packet_discard_mixed_senders(bencher: &mut Bencher) {
for batch in batches.iter_mut() {
for packet in batch.iter_mut() {
// One spam address, ~1000 unique addresses.
packet.meta.addr = if rng.gen_ratio(1, 30) {
packet.meta_mut().addr = if rng.gen_ratio(1, 30) {
new_rand_addr(&mut rng)
} else {
spam_addr
Expand All @@ -109,10 +109,10 @@ fn bench_packet_discard_mixed_senders(bencher: &mut Bencher) {
let mut num_packets = 0;
for batch in batches.iter_mut() {
for packet in batch.iter_mut() {
if !packet.meta.discard() {
if !packet.meta().discard() {
num_packets += 1;
}
packet.meta.set_discard(false);
packet.meta_mut().set_discard(false);
}
}
assert_eq!(num_packets, 10_000);
Expand Down Expand Up @@ -215,7 +215,7 @@ fn prepare_batches(discard_factor: i32) -> (Vec<PacketBatch>, usize) {
batch.iter_mut().for_each(|p| {
let throw = die.sample(&mut rng);
if throw < discard_factor {
p.meta.set_discard(true);
p.meta_mut().set_discard(true);
c += 1;
}
})
Expand Down
8 changes: 4 additions & 4 deletions core/benches/unprocessed_packet_batches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn build_packet_batch(
recent_blockhash.unwrap_or_else(Hash::new_unique),
);
let mut packet = Packet::from_data(None, tx).unwrap();
packet.meta.sender_stake = sender_stake as u64;
packet.meta_mut().sender_stake = sender_stake as u64;
packet
})
.collect(),
Expand Down Expand Up @@ -66,7 +66,7 @@ fn build_randomized_packet_batch(
);
let mut packet = Packet::from_data(None, tx).unwrap();
let sender_stake = distribution.sample(&mut rng);
packet.meta.sender_stake = sender_stake as u64;
packet.meta_mut().sender_stake = sender_stake as u64;
packet
})
.collect(),
Expand Down Expand Up @@ -120,8 +120,8 @@ fn bench_packet_clone(bencher: &mut Bencher) {
let mut timer = Measure::start("insert_batch");
packet_batch.iter().for_each(|packet| {
let mut packet = packet.clone();
packet.meta.sender_stake *= 2;
if packet.meta.sender_stake > 2 {
packet.meta_mut().sender_stake *= 2;
if packet.meta().sender_stake > 2 {
outer_packet = packet;
}
});
Expand Down
12 changes: 8 additions & 4 deletions core/src/ancestor_hashes_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl AncestorHashesService {
keypair: &Keypair,
ancestor_socket: &UdpSocket,
) -> Option<(Slot, DuplicateAncestorDecision)> {
let from_addr = packet.meta.socket_addr();
let from_addr = packet.meta().socket_addr();
let packet_data = match packet.data(..) {
Some(data) => data,
None => {
Expand Down Expand Up @@ -1205,7 +1205,9 @@ mod test {
.recv_timeout(Duration::from_millis(10_000))
.unwrap();
let packet = &mut response_packet[0];
packet.meta.set_socket_addr(&responder_info.serve_repair);
packet
.meta_mut()
.set_socket_addr(&responder_info.serve_repair);
let decision = AncestorHashesService::verify_and_process_ancestor_response(
packet,
&ancestor_hashes_request_statuses,
Expand Down Expand Up @@ -1465,7 +1467,7 @@ mod test {

// Create invalid packet with fewer bytes than the size of the nonce
let mut packet = Packet::default();
packet.meta.size = 0;
packet.meta_mut().size = 0;

assert!(AncestorHashesService::verify_and_process_ancestor_response(
&packet,
Expand Down Expand Up @@ -1573,7 +1575,9 @@ mod test {
.recv_timeout(Duration::from_millis(10_000))
.unwrap();
let packet = &mut response_packet[0];
packet.meta.set_socket_addr(&responder_info.serve_repair);
packet
.meta_mut()
.set_socket_addr(&responder_info.serve_repair);
let decision = AncestorHashesService::verify_and_process_ancestor_response(
packet,
&ancestor_hashes_request_statuses,
Expand Down
8 changes: 4 additions & 4 deletions core/src/banking_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ impl BankingStage {

let packet_vec: Vec<_> = forwardable_packets
.filter_map(|p| {
if !p.meta.forwarded() && data_budget.take(p.meta.size) {
if !p.meta().forwarded() && data_budget.take(p.meta().size) {
Some(p.data(..)?.to_vec())
} else {
None
Expand Down Expand Up @@ -2122,7 +2122,7 @@ mod tests {
with_vers.iter_mut().for_each(|(b, v)| {
b.iter_mut()
.zip(v)
.for_each(|(p, f)| p.meta.set_discard(*f == 0))
.for_each(|(p, f)| p.meta_mut().set_discard(*f == 0))
});
with_vers.into_iter().map(|(b, _)| b).collect()
}
Expand Down Expand Up @@ -3925,7 +3925,7 @@ mod tests {
let forwarded_packet = {
let transaction = system_transaction::transfer(&keypair, &pubkey, 1, fwd_block_hash);
let mut packet = Packet::from_data(None, transaction).unwrap();
packet.meta.flags |= PacketFlags::FORWARDED;
packet.meta_mut().flags |= PacketFlags::FORWARDED;
DeserializedPacket::new(packet).unwrap()
};

Expand Down Expand Up @@ -4005,7 +4005,7 @@ mod tests {
let num_received = recv_mmsg(recv_socket, &mut packets[..]).unwrap_or_default();
assert_eq!(num_received, expected_ids.len(), "{}", name);
for (i, expected_id) in expected_ids.iter().enumerate() {
assert_eq!(packets[i].meta.size, 215);
assert_eq!(packets[i].meta().size, 215);
let recv_transaction: VersionedTransaction =
packets[i].deserialize_slice(..).unwrap();
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion core/src/cluster_info_vote_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl ClusterInfoVoteListener {
.filter(|(_, packet_batch)| {
// to_packet_batches() above splits into 1 packet long batches
assert_eq!(packet_batch.len(), 1);
!packet_batch[0].meta.discard()
!packet_batch[0].meta().discard()
})
.filter_map(|(tx, packet_batch)| {
let (vote_account_key, vote, ..) = vote_parser::parse_vote_transaction(&tx)?;
Expand Down
2 changes: 1 addition & 1 deletion core/src/fetch_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl FetchStage {
poh_recorder: &Arc<RwLock<PohRecorder>>,
) -> Result<()> {
let mark_forwarded = |packet: &mut Packet| {
packet.meta.flags |= PacketFlags::FORWARDED;
packet.meta_mut().flags |= PacketFlags::FORWARDED;
};

let mut packet_batch = recvr.recv()?;
Expand Down
4 changes: 2 additions & 2 deletions core/src/find_packet_sender_stake_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ impl FindPacketSenderStakeStage {
.iter_mut()
.flat_map(|batch| batch.iter_mut())
.for_each(|packet| {
packet.meta.sender_stake = ip_to_stake
.get(&packet.meta.addr)
packet.meta_mut().sender_stake = ip_to_stake
.get(&packet.meta().addr)
.copied()
.unwrap_or_default();
});
Expand Down
2 changes: 1 addition & 1 deletion core/src/immutable_deserialized_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl ImmutableDeserializedPacket {
let sanitized_transaction = SanitizedVersionedTransaction::try_from(versioned_transaction)?;
let message_bytes = packet_message(&packet)?;
let message_hash = Message::hash_raw_message(message_bytes);
let is_simple_vote = packet.meta.is_simple_vote_tx();
let is_simple_vote = packet.meta().is_simple_vote_tx();

// drop transaction if prioritization fails.
let mut priority_details = priority_details
Expand Down
15 changes: 9 additions & 6 deletions core/src/latest_unprocessed_votes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct LatestValidatorVotePacket {

impl LatestValidatorVotePacket {
pub fn new(packet: Packet, vote_source: VoteSource) -> Result<Self, DeserializedPacketError> {
if !packet.meta.is_simple_vote_tx() {
if !packet.meta().is_simple_vote_tx() {
return Err(DeserializedPacketError::VoteTransactionError);
}

Expand Down Expand Up @@ -347,7 +347,10 @@ mod tests {
None,
);
let mut packet = Packet::from_data(None, vote_tx).unwrap();
packet.meta.flags.set(PacketFlags::SIMPLE_VOTE_TX, true);
packet
.meta_mut()
.flags
.set(PacketFlags::SIMPLE_VOTE_TX, true);
LatestValidatorVotePacket::new(packet, vote_source).unwrap()
}

Expand Down Expand Up @@ -380,7 +383,7 @@ mod tests {
),
)
.unwrap();
vote.meta.flags.set(PacketFlags::SIMPLE_VOTE_TX, true);
vote.meta_mut().flags.set(PacketFlags::SIMPLE_VOTE_TX, true);
let mut vote_switch = Packet::from_data(
None,
new_vote_transaction(
Expand All @@ -395,7 +398,7 @@ mod tests {
)
.unwrap();
vote_switch
.meta
.meta_mut()
.flags
.set(PacketFlags::SIMPLE_VOTE_TX, true);
let mut vote_state_update = Packet::from_data(
Expand All @@ -411,7 +414,7 @@ mod tests {
)
.unwrap();
vote_state_update
.meta
.meta_mut()
.flags
.set(PacketFlags::SIMPLE_VOTE_TX, true);
let mut vote_state_update_switch = Packet::from_data(
Expand All @@ -427,7 +430,7 @@ mod tests {
)
.unwrap();
vote_state_update_switch
.meta
.meta_mut()
.flags
.set(PacketFlags::SIMPLE_VOTE_TX, true);
let random_transaction = Packet::from_data(
Expand Down
4 changes: 2 additions & 2 deletions core/src/packet_deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl PacketDeserializer {
packet_batch
.iter()
.enumerate()
.filter(|(_, pkt)| !pkt.meta.discard())
.filter(|(_, pkt)| !pkt.meta().discard())
.map(|(index, _)| index)
.collect()
}
Expand Down Expand Up @@ -179,7 +179,7 @@ mod tests {
let transactions = vec![random_transfer(), random_transfer()];
let mut packet_batches = to_packet_batches(&transactions, 1);
assert_eq!(packet_batches.len(), 2);
packet_batches[0][0].meta.set_discard(true);
packet_batches[0][0].meta_mut().set_discard(true);

let results = PacketDeserializer::deserialize_and_collect_packets(&packet_batches, None);
assert_eq!(results.deserialized_packets.len(), 1);
Expand Down
6 changes: 3 additions & 3 deletions core/src/repair_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub fn repair_response_packet_from_bytes(
if size > packet.buffer_mut().len() {
return None;
}
packet.meta.size = size;
packet.meta.set_socket_addr(dest);
packet.meta_mut().size = size;
packet.meta_mut().set_socket_addr(dest);
packet.buffer_mut()[..bytes.len()].copy_from_slice(&bytes);
let mut wr = io::Cursor::new(&mut packet.buffer_mut()[bytes.len()..]);
bincode::serialize_into(&mut wr, &nonce).expect("Buffer not large enough to fit nonce");
Expand Down Expand Up @@ -90,7 +90,7 @@ mod test {
nonce,
)
.unwrap();
packet.meta.flags |= PacketFlags::REPAIR;
packet.meta_mut().flags |= PacketFlags::REPAIR;

let leader_slots = [(slot, keypair.pubkey().to_bytes())]
.iter()
Expand Down
16 changes: 8 additions & 8 deletions core/src/serve_repair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ impl ServeRepair {
}
};

let from_addr = packet.meta.socket_addr();
let from_addr = packet.meta().socket_addr();
if !ContactInfo::is_valid_address(&from_addr, &socket_addr_space) {
stats.err_malformed += 1;
continue;
Expand Down Expand Up @@ -807,7 +807,7 @@ impl ServeRepair {
Some(rsp) => rsp,
};
let num_response_packets = rsp.len();
let num_response_bytes = rsp.iter().map(|p| p.meta.size).sum();
let num_response_bytes = rsp.iter().map(|p| p.meta().size).sum();
if data_budget.take(num_response_bytes) && response_sender.send(rsp).is_ok() {
stats.total_response_packets += num_response_packets;
match stake > 0 {
Expand Down Expand Up @@ -984,7 +984,7 @@ impl ServeRepair {
) {
let mut pending_pongs = Vec::default();
for packet in packet_batch.iter_mut() {
if packet.meta.size != REPAIR_RESPONSE_SERIALIZED_PING_BYTES {
if packet.meta().size != REPAIR_RESPONSE_SERIALIZED_PING_BYTES {
continue;
}
if let Ok(RepairResponse::Ping(ping)) = packet.deserialize_slice(..) {
Expand All @@ -998,12 +998,12 @@ impl ServeRepair {
stats.ping_err_verify_count += 1;
continue;
}
packet.meta.set_discard(true);
packet.meta_mut().set_discard(true);
stats.ping_count += 1;
if let Ok(pong) = Pong::new(&ping, keypair) {
let pong = RepairProtocol::Pong(pong);
if let Ok(pong_bytes) = serialize(&pong) {
let from_addr = packet.meta.socket_addr();
let from_addr = packet.meta().socket_addr();
pending_pongs.push((pong_bytes, from_addr));
}
}
Expand Down Expand Up @@ -1210,7 +1210,7 @@ mod tests {
let ping = Ping::new_rand(&mut rng, &keypair).unwrap();
let ping = RepairResponse::Ping(ping);
let pkt = Packet::from_data(None, ping).unwrap();
assert_eq!(pkt.meta.size, REPAIR_RESPONSE_SERIALIZED_PING_BYTES);
assert_eq!(pkt.meta().size, REPAIR_RESPONSE_SERIALIZED_PING_BYTES);
}

#[test]
Expand All @@ -1230,7 +1230,7 @@ mod tests {
shred.sign(&keypair);
let mut pkt = Packet::default();
shred.copy_to_packet(&mut pkt);
pkt.meta.size = REPAIR_RESPONSE_SERIALIZED_PING_BYTES;
pkt.meta_mut().size = REPAIR_RESPONSE_SERIALIZED_PING_BYTES;
let res = pkt.deserialize_slice::<RepairResponse, _>(..);
if let Ok(RepairResponse::Ping(ping)) = res {
assert!(!ping.verify());
Expand Down Expand Up @@ -1870,7 +1870,7 @@ mod tests {
fn test_run_ancestor_hashes() {
fn deserialize_ancestor_hashes_response(packet: &Packet) -> AncestorHashesResponse {
packet
.deserialize_slice(..packet.meta.size - SIZE_OF_NONCE)
.deserialize_slice(..packet.meta().size - SIZE_OF_NONCE)
.unwrap()
}

Expand Down
Loading

0 comments on commit b1340d7

Please sign in to comment.