Migrate loader to high-level instructions

This commit is contained in:
Greg Fitzgerald 2019-03-21 06:05:32 -06:00
parent 148e08a8a5
commit 58f071b7a0
3 changed files with 44 additions and 19 deletions

View File

@ -1,3 +1,6 @@
use crate::pubkey::Pubkey;
use crate::transaction::{AccountMeta, Instruction};
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum LoaderInstruction {
/// Write program data into an account
@ -16,3 +19,24 @@ pub enum LoaderInstruction {
/// The transaction must be signed by key[0]
Finalize,
}
impl LoaderInstruction {
pub fn new_write(
account_id: &Pubkey,
program_id: &Pubkey,
offset: u32,
bytes: Vec<u8>,
) -> Instruction {
let account_metas = vec![AccountMeta::new(*account_id, true)];
Instruction::new(
*program_id,
&LoaderInstruction::Write { offset, bytes },
account_metas,
)
}
pub fn new_finalize(account_id: &Pubkey, program_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta::new(*account_id, true)];
Instruction::new(*program_id, &LoaderInstruction::Finalize, account_metas)
}
}

View File

@ -3,7 +3,7 @@
use crate::hash::Hash;
use crate::loader_instruction::LoaderInstruction;
use crate::pubkey::Pubkey;
use crate::signature::Keypair;
use crate::signature::{Keypair, KeypairUtil};
use crate::transaction::Transaction;
pub struct LoaderTransaction {}
@ -17,15 +17,10 @@ impl LoaderTransaction {
recent_blockhash: Hash,
fee: u64,
) -> Transaction {
let instruction = LoaderInstruction::Write { offset, bytes };
Transaction::new_signed(
from_keypair,
&[],
loader,
&instruction,
recent_blockhash,
fee,
)
let write_instruction =
LoaderInstruction::new_write(&from_keypair.pubkey(), loader, offset, bytes);
let instructions = vec![write_instruction];
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash, fee)
}
pub fn new_finalize(
@ -34,14 +29,8 @@ impl LoaderTransaction {
recent_blockhash: Hash,
fee: u64,
) -> Transaction {
let instruction = LoaderInstruction::Finalize;
Transaction::new_signed(
from_keypair,
&[],
loader,
&instruction,
recent_blockhash,
fee,
)
let finalize_instruction = LoaderInstruction::new_finalize(&from_keypair.pubkey(), loader);
let instructions = vec![finalize_instruction];
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash, fee)
}
}

View File

@ -218,6 +218,18 @@ impl Transaction {
Script::new(instructions).compile()
}
pub fn new_signed_instructions<T: KeypairUtil>(
from_keypairs: &[&T],
instructions: Vec<Instruction>,
recent_blockhash: Hash,
fee: u64,
) -> Transaction {
let mut tx = Self::new(instructions);
tx.fee = fee;
tx.sign(from_keypairs, recent_blockhash);
tx
}
pub fn new_with_blockhash_and_fee<T: Serialize>(
from_pubkey: &Pubkey,
transaction_keys: &[Pubkey],