remove Pack/Seal traits for Instruction

This commit is contained in:
De Facto 2021-01-20 12:08:59 +08:00
parent 00fc0b24ec
commit f8f304b5b2
2 changed files with 11 additions and 43 deletions

View File

@ -6,8 +6,6 @@
use solana_program::{
sysvar,
pubkey::Pubkey,
program_error::ProgramError,
program_pack::{Pack, Sealed},
instruction::{AccountMeta, Instruction as SolInstruction},
};
@ -95,27 +93,6 @@ pub enum Instruction {
},
}
impl Sealed for Instruction {}
impl Pack for Instruction {
const LEN: usize = 54;
fn pack_into_slice(&self, dst: &mut [u8]) {
let data = self.pack_into_vec();
dst[..data.len()].copy_from_slice(&data);
}
fn unpack_from_slice(src: &[u8]) -> Result<Self, ProgramError> {
let mut mut_src: &[u8] = src;
Self::deserialize(&mut mut_src).map_err(|_| ProgramError::InvalidInstructionData)
}
}
impl Instruction {
fn pack_into_vec(&self) -> Vec<u8> {
self.try_to_vec().expect("try_to_vec")
}
}
/// Below is for test
/// Creates a `intialize` instruction.
@ -145,8 +122,7 @@ pub fn initialize(
max_submission_value,
submission_decimals,
description,
}
.pack_into_vec(),
}.try_to_vec().expect("try_to_vec")
}
}
@ -173,8 +149,7 @@ pub fn add_oracle(
accounts,
data: Instruction::AddOracle {
description,
}
.pack_into_vec(),
}.try_to_vec().expect("try_to_vec"),
}
}
@ -196,8 +171,7 @@ pub fn remove_oracle(
accounts,
data: Instruction::RemoveOracle {
pubkey: pubkey.to_bytes(),
}
.pack_into_vec(),
}.try_to_vec().expect("try_to_vec"),
}
}
@ -222,8 +196,7 @@ pub fn submit(
accounts,
data: Instruction::Submit {
submission,
}
.pack_into_vec(),
}.try_to_vec().expect("try_to_vec"),
}
}
@ -235,14 +208,6 @@ mod tests {
use crate::borsh_utils;
use anyhow::Result;
#[test]
fn test_get_packed_len() {
assert_eq!(
Instruction::get_packed_len(),
borsh_utils::get_packed_len::<Instruction>()
)
}
#[test]
fn test_serialize_bytes() -> Result<()> {
let test_instruction = Instruction::Initialize {
@ -265,15 +230,17 @@ mod tests {
#[test]
fn state_deserialize_invalid() -> Result<()> {
let instruction = Instruction::try_from_slice(&hex::decode("0022112211bbaabbaabbaabbaaddccddccddccddcc06ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")?)?;
assert_eq!(
Instruction::unpack_from_slice(&hex::decode("0022112211bbaabbaabbaabbaaddccddccddccddcc06ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")?),
Ok(Instruction::Initialize {
instruction,
Instruction::Initialize {
submit_interval: 0x11221122,
min_submission_value: 0xaabbaabbaabbaabb,
max_submission_value: 0xccddccddccddccdd,
submission_decimals: 6,
description: [0xff; 32],
}),
},
);
// assert_eq!(

View File

@ -6,6 +6,7 @@ use crate::{
state::{Aggregator, Oracle},
};
use borsh::BorshDeserialize;
use solana_program::{
account_info::{next_account_info, AccountInfo},
clock::Clock,
@ -24,7 +25,7 @@ pub struct Processor {}
impl Processor {
/// Processes an [Instruction](enum.Instruction.html).
pub fn process(program_id: &Pubkey, accounts: &[AccountInfo], input: &[u8]) -> ProgramResult {
let instruction = Instruction::unpack_from_slice(input)?;
let instruction = Instruction::try_from_slice(input).map_err(|_err| ProgramError::InvalidInstructionData )?;
match instruction {
Instruction::Initialize {