Skip to content

Commit

Permalink
Trun PacketOffsets into a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoqun committed Oct 16, 2019
1 parent 021d597 commit f10a36d
Showing 1 changed file with 61 additions and 38 deletions.
99 changes: 61 additions & 38 deletions core/src/sigverify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,24 @@ pub type TxOffset = PinnedVec<u32>;

type TxOffsets = (TxOffset, TxOffset, TxOffset, TxOffset, Vec<Vec<u32>>);

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);

Expand All @@ -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;
}

Expand All @@ -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::<Pubkey>();
let sig_end = sig_start as usize + size_of::<Signature>();

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -157,25 +174,26 @@ pub fn generate_offsets(batches: &[Packets], recycler: &Recycler<TxOffset>) -> R
p.packets.iter().for_each(|packet| {
let current_offset = current_packet as u32 * size_of::<Packet>() 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::<Signature>() as u32;

pubkey_offsets.push(pubkey_offset);
pubkey_offset += size_of::<Pubkey>() 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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -370,26 +389,25 @@ 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()),
Some(SIG_OFFSET)
);
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]
Expand Down Expand Up @@ -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)
);
}

Expand Down

0 comments on commit f10a36d

Please sign in to comment.