Specialize GenericInstruction

This commit is contained in:
Greg Fitzgerald 2019-03-27 17:06:50 -06:00
parent 50b0a5ae83
commit 4bca60861e
1 changed files with 32 additions and 12 deletions

View File

@ -69,19 +69,22 @@ impl InstructionError {
} }
} }
/// An instruction to execute a program #[derive(Debug, PartialEq)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub struct Instruction {
pub struct GenericInstruction<P, Q> { /// Pubkey of the instruction processor that executes this instruction
/// Index into the transaction program ids array indicating the program account that executes this instruction pub program_ids_index: Pubkey,
pub program_ids_index: P, /// Metadata for what accounts should be passed to the instruction processor
/// Ordered indices into the transaction keys array indicating which accounts to pass to the program pub accounts: Vec<AccountMeta>,
pub accounts: Vec<Q>, /// Opaque data passed to the instruction processor
/// The program input data
pub data: Vec<u8>, pub data: Vec<u8>,
} }
impl<P, Q> GenericInstruction<P, Q> { impl Instruction {
pub fn new<T: Serialize>(program_ids_index: P, data: &T, accounts: Vec<Q>) -> Self { pub fn new<T: Serialize>(
program_ids_index: Pubkey,
data: &T,
accounts: Vec<AccountMeta>,
) -> Self {
let data = serialize(data).unwrap(); let data = serialize(data).unwrap();
Self { Self {
program_ids_index, program_ids_index,
@ -106,10 +109,27 @@ impl AccountMeta {
} }
} }
pub type Instruction = GenericInstruction<Pubkey, AccountMeta>; /// An instruction to execute a program
pub type CompiledInstruction = GenericInstruction<u8, u8>; #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct CompiledInstruction {
/// Index into the transaction program ids array indicating the program account that executes this instruction
pub program_ids_index: u8,
/// Ordered indices into the transaction keys array indicating which accounts to pass to the program
pub accounts: Vec<u8>,
/// The program input data
pub data: Vec<u8>,
}
impl CompiledInstruction { impl CompiledInstruction {
pub fn new<T: Serialize>(program_ids_index: u8, data: &T, accounts: Vec<u8>) -> Self {
let data = serialize(data).unwrap();
Self {
program_ids_index,
data,
accounts,
}
}
pub fn serialize_with(mut writer: &mut Cursor<&mut [u8]>, ix: &Self) -> Result<(), Error> { pub fn serialize_with(mut writer: &mut Cursor<&mut [u8]>, ix: &Self) -> Result<(), Error> {
writer.write_all(&[ix.program_ids_index])?; writer.write_all(&[ix.program_ids_index])?;
serialize_vec_bytes(&mut writer, &ix.accounts[..])?; serialize_vec_bytes(&mut writer, &ix.accounts[..])?;