Generalize instruction

For serialization: Instruction<u8, u8>
For users:         Instruction<Pubkey, (Pubkey, bool)>
For programs:      Instruction<Pubkey, (Pubkey, bool, Account)>
This commit is contained in:
Greg Fitzgerald 2019-02-28 02:19:04 -07:00
parent 8e4cd6fcc3
commit 4610706d9f
1 changed files with 12 additions and 13 deletions

View File

@ -17,29 +17,28 @@ use std::mem::size_of;
/// An instruction to execute a program /// An instruction to execute a program
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Instruction { pub struct Instruction<P, Q> {
/// Index into the transaction program ids array indicating the program account that executes this 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 /// Ordered indices into the transaction keys array indicating which accounts to pass to the program
pub accounts: Vec<u8>, pub accounts: Vec<Q>,
/// The program input data /// The program input data
pub userdata: Vec<u8>, pub userdata: Vec<u8>,
} }
impl Instruction { impl<P, Q> Instruction<P, Q> {
pub fn new<T: Serialize>(program_ids_index: u8, userdata: &T, accounts: Vec<u8>) -> Self { pub fn new<T: Serialize>(program_ids_index: P, userdata: &T, accounts: Vec<Q>) -> Self {
let userdata = serialize(userdata).unwrap(); let userdata = serialize(userdata).unwrap();
Instruction { Self {
program_ids_index, program_ids_index,
userdata, userdata,
accounts, accounts,
} }
} }
}
pub fn serialize_with( impl Instruction<u8, u8> {
mut writer: &mut Cursor<&mut [u8]>, pub fn serialize_with(mut writer: &mut Cursor<&mut [u8]>, ix: &Self) -> Result<(), Error> {
ix: &Instruction,
) -> 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[..])?;
serialize_vec_bytes(&mut writer, &ix.userdata[..])?; serialize_vec_bytes(&mut writer, &ix.userdata[..])?;
@ -93,7 +92,7 @@ pub struct Transaction {
pub program_ids: Vec<Pubkey>, pub program_ids: Vec<Pubkey>,
/// Programs that will be executed in sequence and committed in one atomic transaction if all /// Programs that will be executed in sequence and committed in one atomic transaction if all
/// succeed. /// succeed.
pub instructions: Vec<Instruction>, pub instructions: Vec<Instruction<u8, u8>>,
} }
impl Transaction { impl Transaction {
@ -153,7 +152,7 @@ impl Transaction {
last_id: Hash, last_id: Hash,
fee: u64, fee: u64,
program_ids: Vec<Pubkey>, program_ids: Vec<Pubkey>,
instructions: Vec<Instruction>, instructions: Vec<Instruction<u8, u8>>,
) -> Self { ) -> Self {
let mut account_keys: Vec<_> = from_keypairs let mut account_keys: Vec<_> = from_keypairs
.iter() .iter()
@ -371,7 +370,7 @@ impl<'a> serde::de::Visitor<'a> for TransactionVisitor {
let program_ids: Vec<Pubkey> = let program_ids: Vec<Pubkey> =
deserialize_vec_with(&mut rd, Transaction::deserialize_pubkey) deserialize_vec_with(&mut rd, Transaction::deserialize_pubkey)
.map_err(Error::custom)?; .map_err(Error::custom)?;
let instructions: Vec<Instruction> = let instructions: Vec<Instruction<u8, u8>> =
deserialize_vec_with(&mut rd, Instruction::deserialize_from).map_err(Error::custom)?; deserialize_vec_with(&mut rd, Instruction::deserialize_from).map_err(Error::custom)?;
Ok(Transaction { Ok(Transaction {
signatures, signatures,