Cleanup and feature gate instruction processing (#12359)

This commit is contained in:
sakridge
2020-09-20 10:58:12 -07:00
committed by GitHub
parent 65b247a922
commit 22d8b3c3f8
5 changed files with 43 additions and 19 deletions

View File

@ -47,7 +47,7 @@ fn bench_manual_instruction_deserialize(b: &mut Bencher) {
let serialized = message.serialize_instructions();
b.iter(|| {
for i in 0..instructions.len() {
test::black_box(instructions::get_instruction(i, &serialized).unwrap());
test::black_box(instructions::load_instruction_at(i, &serialized).unwrap());
}
});
}
@ -58,6 +58,6 @@ fn bench_manual_instruction_deserialize_single(b: &mut Bencher) {
let message = Message::new(&instructions, None);
let serialized = message.serialize_instructions();
b.iter(|| {
test::black_box(instructions::get_instruction(3, &serialized).unwrap());
test::black_box(instructions::load_instruction_at(3, &serialized).unwrap());
});
}

View File

@ -7,7 +7,7 @@ use crate::sysvar::Sysvar;
pub type Instructions = Vec<Instruction>;
crate::declare_sysvar_id!("instructions1111111111111111111111111111111", Instructions);
crate::declare_sysvar_id!("Sysvar1nstructions1111111111111111111111111", Instructions);
impl Sysvar for Instructions {}
@ -21,19 +21,19 @@ pub fn is_enabled(_epoch: Epoch, cluster_type: ClusterType) -> bool {
cluster_type == ClusterType::Development
}
pub fn get_current_instruction(data: &[u8]) -> u16 {
pub fn load_current_index(data: &[u8]) -> u16 {
let mut instr_fixed_data = [0u8; 2];
let len = data.len();
instr_fixed_data.copy_from_slice(&data[len - 2..len]);
u16::from_le_bytes(instr_fixed_data)
}
pub fn store_current_instruction(data: &mut [u8], instruction_index: u16) {
pub fn store_current_index(data: &mut [u8], instruction_index: u16) {
let last_index = data.len() - 2;
data[last_index..last_index + 2].copy_from_slice(&instruction_index.to_le_bytes());
}
pub fn get_instruction(index: usize, data: &[u8]) -> Result<Instruction, SanitizeError> {
pub fn load_instruction_at(index: usize, data: &[u8]) -> Result<Instruction, SanitizeError> {
solana_sdk::message::Message::deserialize_instruction(index, data)
}
@ -42,10 +42,10 @@ mod tests {
use super::*;
#[test]
fn test_get_store_instruction() {
fn test_load_store_instruction() {
let mut data = [4u8; 10];
store_current_instruction(&mut data, 3);
assert_eq!(get_current_instruction(&data), 3);
store_current_index(&mut data, 3);
assert_eq!(load_current_index(&data), 3);
assert_eq!([4u8; 8], data[0..8]);
}
}