diff --git a/core/src/sigverify.rs b/core/src/sigverify.rs index 9255f67cc69cee..8ac0f1fe0858b3 100644 --- a/core/src/sigverify.rs +++ b/core/src/sigverify.rs @@ -33,7 +33,24 @@ pub type TxOffset = PinnedVec; type TxOffsets = (TxOffset, TxOffset, TxOffset, TxOffset, Vec>); -type PacketOffsets = (u32, u32, u32, u32); +#[derive(Debug, PartialEq, Eq)] +struct PacketOffsets { + pub sig_len: u32, + pub sig_start: u32, + pub msg_start: u32, + pub pubkey_start: u32, +} + +impl PacketOffsets { + pub fn new(sig_len: u32, sig_start: u32, msg_start: u32, pubkey_start: u32) -> Self { + Self { + sig_len, + sig_start, + msg_start, + pubkey_start, + } + } +} type UnsanitizedPacketOffsets = (bool, u32, u32, u32, u32); @@ -50,12 +67,12 @@ pub fn init() { } fn verify_packet(packet: &Packet) -> u8 { - let (sig_len, sig_start, msg_start, pubkey_start) = get_packet_offsets(packet, 0); - let mut sig_start = sig_start as usize; - let mut pubkey_start = pubkey_start as usize; - let msg_start = msg_start as usize; + let packet_offsets = get_packet_offsets(packet, 0); + let mut sig_start = packet_offsets.sig_start as usize; + let mut pubkey_start = packet_offsets.pubkey_start as usize; + let msg_start = packet_offsets.msg_start as usize; - if sig_len == 0 { + if packet_offsets.sig_len == 0 { return 0; } @@ -64,7 +81,7 @@ fn verify_packet(packet: &Packet) -> u8 { } let msg_end = packet.meta.size; - for _ in 0..sig_len { + for _ in 0..packet_offsets.sig_len { let pubkey_end = pubkey_start as usize + size_of::(); let sig_end = sig_start as usize + size_of::(); @@ -130,13 +147,13 @@ fn do_get_packet_offsets(packet: &Packet, current_offset: u32) -> UnsanitizedPac } } -pub fn get_packet_offsets(packet: &Packet, current_offset: u32) -> PacketOffsets { +fn get_packet_offsets(packet: &Packet, current_offset: u32) -> PacketOffsets { let ret = do_get_packet_offsets(packet, current_offset); if ret.0 { - (ret.1, ret.2, ret.3, ret.4) + PacketOffsets::new(ret.1, ret.2, ret.3, ret.4) } else { // force sigverify to fail by returning zeros - (0, 0, 0, 0) + PacketOffsets::new(0, 0, 0, 0) } } @@ -157,25 +174,26 @@ pub fn generate_offsets(batches: &[Packets], recycler: &Recycler) -> R p.packets.iter().for_each(|packet| { let current_offset = current_packet as u32 * size_of::() as u32; - let (sig_len, sig_start, msg_start_offset, pubkey_offset) = - get_packet_offsets(packet, current_offset); + let packet_offsets = get_packet_offsets(packet, current_offset); - sig_lens.push(sig_len); + sig_lens.push(packet_offsets.sig_len); - trace!("pubkey_offset: {}", pubkey_offset); - if sig_len > 0 { - let mut pubkey_offset = pubkey_offset; - let mut sig_offset = sig_start; - for _ in 0..sig_len { + trace!("pubkey_offset: {}", packet_offsets.pubkey_start); + if packet_offsets.sig_len > 0 { + let mut pubkey_offset = packet_offsets.pubkey_start; + let mut sig_offset = packet_offsets.sig_start; + for _ in 0..packet_offsets.sig_len { signature_offsets.push(sig_offset); sig_offset += size_of::() as u32; pubkey_offsets.push(pubkey_offset); pubkey_offset += size_of::() as u32; - msg_start_offsets.push(msg_start_offset); + msg_start_offsets.push(packet_offsets.msg_start); - msg_sizes.push(current_offset + (packet.meta.size as u32) - msg_start_offset); + msg_sizes.push( + current_offset + (packet.meta.size as u32) - packet_offsets.msg_start, + ); } } current_packet += 1; @@ -333,6 +351,7 @@ mod tests { use crate::packet::{Packet, Packets}; use crate::recycler::Recycler; use crate::sigverify; + use crate::sigverify::PacketOffsets; use crate::test_tx::{test_multisig_tx, test_tx}; use bincode::{deserialize, serialize, serialized_size}; use solana_sdk::hash::Hash; @@ -370,8 +389,7 @@ mod tests { let message_data = tx.message_data(); let packet = sigverify::make_packet_from_transaction(tx.clone()); - let (sig_len, sig_start, msg_start_offset, pubkey_offset) = - sigverify::get_packet_offsets(&packet, 0); + let packet_offsets = sigverify::get_packet_offsets(&packet, 0); assert_eq!( memfind(&tx_bytes, &tx.signatures[0].as_ref()), @@ -379,17 +397,17 @@ mod tests { ); assert_eq!( memfind(&tx_bytes, &tx.message().account_keys[0].as_ref()), - Some(pubkey_offset as usize) + Some(packet_offsets.pubkey_start as usize) ); assert_eq!( memfind(&tx_bytes, &message_data), - Some(msg_start_offset as usize) + Some(packet_offsets.msg_start as usize) ); assert_eq!( memfind(&tx_bytes, &tx.signatures[0].as_ref()), - Some(sig_start as usize) + Some(packet_offsets.sig_start as usize) ); - assert_eq!(sig_len, 1); + assert_eq!(packet_offsets.sig_len, 1); } #[test] @@ -476,33 +494,38 @@ mod tests { } // Just like get_packet_offsets, but not returning redundant information. - fn get_packet_offsets_from_tx(tx: Transaction, current_offset: u32) -> sigverify::PacketOffsets { + fn get_packet_offsets_from_tx(tx: Transaction, current_offset: u32) -> PacketOffsets { let packet = sigverify::make_packet_from_transaction(tx); - let (sig_len, sig_start, msg_start_offset, pubkey_offset) = - sigverify::get_packet_offsets(&packet, current_offset); - ( - sig_len, - sig_start - current_offset, - msg_start_offset - sig_start, - pubkey_offset - msg_start_offset, + let packet_offsets = sigverify::get_packet_offsets(&packet, current_offset); + PacketOffsets::new( + packet_offsets.sig_len - packet_offsets.sig_len, + packet_offsets.sig_start - current_offset, + packet_offsets.msg_start - packet_offsets.sig_start, + packet_offsets.pubkey_start - packet_offsets.msg_start, ) } #[test] fn test_get_packet_offsets() { - assert_eq!(get_packet_offsets_from_tx(test_tx(), 0), (1, 1, 64, 4)); - assert_eq!(get_packet_offsets_from_tx(test_tx(), 100), (1, 1, 64, 4)); + assert_eq!( + get_packet_offsets_from_tx(test_tx(), 0), + PacketOffsets::new(1, 1, 64, 4) + ); + assert_eq!( + get_packet_offsets_from_tx(test_tx(), 100), + PacketOffsets::new(1, 1, 64, 4) + ); // Ensure we're not indexing packet by the `current_offset` parameter. assert_eq!( get_packet_offsets_from_tx(test_tx(), 1_000_000), - (1, 1, 64, 4) + PacketOffsets::new(1, 1, 64, 4) ); // Ensure we're returning sig_len, not sig_size. assert_eq!( get_packet_offsets_from_tx(test_multisig_tx(), 0), - (2, 1, 128, 4) + PacketOffsets::new(2, 1, 128, 4) ); }