Make instruction data opaque to runtime (#6470)

This commit is contained in:
Jack May
2019-10-24 22:38:57 -07:00
committed by GitHub
parent 28d3af6f35
commit 6eeca9c6f1
10 changed files with 94 additions and 114 deletions

View File

@ -1,6 +1,8 @@
use crate::instruction::{AccountMeta, Instruction};
use crate::pubkey::Pubkey;
use crate::sysvar::rent;
use bincode::serialize;
use serde::Serialize;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum LoaderInstruction {
@ -34,6 +36,7 @@ pub enum LoaderInstruction {
},
}
/// Create an instruction to write program data into an account
pub fn write(
account_pubkey: &Pubkey,
program_id: &Pubkey,
@ -48,6 +51,7 @@ pub fn write(
)
}
/// Create an instruction to finalize a program data account, once finalized it can no longer be modified
pub fn finalize(account_pubkey: &Pubkey, program_id: &Pubkey) -> Instruction {
let account_metas = vec![
AccountMeta::new(*account_pubkey, true),
@ -55,3 +59,15 @@ pub fn finalize(account_pubkey: &Pubkey, program_id: &Pubkey) -> Instruction {
];
Instruction::new(*program_id, &LoaderInstruction::Finalize, account_metas)
}
// Create an instruction to Invoke a program's "main" entrypoint with the given data
pub fn invoke_main<T: Serialize>(
program_id: &Pubkey,
data: &T,
account_metas: Vec<AccountMeta>,
) -> Instruction {
let ix_data = LoaderInstruction::InvokeMain {
data: serialize(data).unwrap().to_vec(),
};
Instruction::new(*program_id, &ix_data, account_metas)
}