From 4610706d9f5e44a251b7505fc31d88e3b0d95c88 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Thu, 28 Feb 2019 02:19:04 -0700 Subject: [PATCH] Generalize instruction For serialization: Instruction For users: Instruction For programs: Instruction --- sdk/src/transaction.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/sdk/src/transaction.rs b/sdk/src/transaction.rs index 07bf0edcc3..3a488f86a8 100644 --- a/sdk/src/transaction.rs +++ b/sdk/src/transaction.rs @@ -17,29 +17,28 @@ use std::mem::size_of; /// An instruction to execute a program #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] -pub struct Instruction { +pub struct Instruction { /// Index into the transaction program ids array indicating the program account that executes this instruction - pub program_ids_index: u8, + pub program_ids_index: P, /// Ordered indices into the transaction keys array indicating which accounts to pass to the program - pub accounts: Vec, + pub accounts: Vec, /// The program input data pub userdata: Vec, } -impl Instruction { - pub fn new(program_ids_index: u8, userdata: &T, accounts: Vec) -> Self { +impl Instruction { + pub fn new(program_ids_index: P, userdata: &T, accounts: Vec) -> Self { let userdata = serialize(userdata).unwrap(); - Instruction { + Self { program_ids_index, userdata, accounts, } } +} - pub fn serialize_with( - mut writer: &mut Cursor<&mut [u8]>, - ix: &Instruction, - ) -> Result<(), Error> { +impl Instruction { + pub fn serialize_with(mut writer: &mut Cursor<&mut [u8]>, ix: &Self) -> Result<(), Error> { writer.write_all(&[ix.program_ids_index])?; serialize_vec_bytes(&mut writer, &ix.accounts[..])?; serialize_vec_bytes(&mut writer, &ix.userdata[..])?; @@ -93,7 +92,7 @@ pub struct Transaction { pub program_ids: Vec, /// Programs that will be executed in sequence and committed in one atomic transaction if all /// succeed. - pub instructions: Vec, + pub instructions: Vec>, } impl Transaction { @@ -153,7 +152,7 @@ impl Transaction { last_id: Hash, fee: u64, program_ids: Vec, - instructions: Vec, + instructions: Vec>, ) -> Self { let mut account_keys: Vec<_> = from_keypairs .iter() @@ -371,7 +370,7 @@ impl<'a> serde::de::Visitor<'a> for TransactionVisitor { let program_ids: Vec = deserialize_vec_with(&mut rd, Transaction::deserialize_pubkey) .map_err(Error::custom)?; - let instructions: Vec = + let instructions: Vec> = deserialize_vec_with(&mut rd, Instruction::deserialize_from).map_err(Error::custom)?; Ok(Transaction { signatures,