2019-03-21 08:14:14 -06:00
|
|
|
use crate::bank_client::BankClient;
|
|
|
|
use serde::Serialize;
|
2019-03-23 21:12:27 -06:00
|
|
|
use solana_sdk::instruction::{AccountMeta, Instruction};
|
2019-04-03 09:45:57 -06:00
|
|
|
use solana_sdk::loader_instruction;
|
2019-03-02 21:03:36 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2019-04-03 09:45:57 -06:00
|
|
|
use solana_sdk::system_instruction;
|
2019-03-02 21:03:36 -07:00
|
|
|
|
2019-03-21 08:14:14 -06:00
|
|
|
pub fn load_program(
|
2019-03-27 07:34:01 -06:00
|
|
|
bank_client: &BankClient,
|
|
|
|
from_keypair: &Keypair,
|
2019-03-21 08:14:14 -06:00
|
|
|
loader_id: &Pubkey,
|
|
|
|
program: Vec<u8>,
|
|
|
|
) -> Pubkey {
|
|
|
|
let program_keypair = Keypair::new();
|
|
|
|
let program_pubkey = program_keypair.pubkey();
|
2019-03-02 21:03:36 -07:00
|
|
|
|
2019-04-03 09:45:57 -06:00
|
|
|
let instruction = system_instruction::create_account(
|
2019-03-27 07:34:01 -06:00
|
|
|
&from_keypair.pubkey(),
|
2019-03-21 08:14:14 -06:00
|
|
|
&program_pubkey,
|
2019-03-02 21:03:36 -07:00
|
|
|
1,
|
|
|
|
program.len() as u64,
|
|
|
|
loader_id,
|
|
|
|
);
|
2019-03-27 07:34:01 -06:00
|
|
|
bank_client
|
|
|
|
.process_instruction(&from_keypair, instruction)
|
|
|
|
.unwrap();
|
2019-03-02 21:03:36 -07:00
|
|
|
|
|
|
|
let chunk_size = 256; // Size of chunk just needs to fit into tx
|
|
|
|
let mut offset = 0;
|
|
|
|
for chunk in program.chunks(chunk_size) {
|
2019-03-21 08:14:14 -06:00
|
|
|
let instruction =
|
2019-04-03 09:45:57 -06:00
|
|
|
loader_instruction::write(&program_pubkey, loader_id, offset, chunk.to_vec());
|
2019-03-27 07:34:01 -06:00
|
|
|
bank_client
|
|
|
|
.process_instruction(&program_keypair, instruction)
|
|
|
|
.unwrap();
|
2019-03-02 21:03:36 -07:00
|
|
|
offset += chunk_size as u32;
|
|
|
|
}
|
|
|
|
|
2019-04-03 09:45:57 -06:00
|
|
|
let instruction = loader_instruction::finalize(&program_pubkey, loader_id);
|
2019-03-27 07:34:01 -06:00
|
|
|
bank_client
|
|
|
|
.process_instruction(&program_keypair, instruction)
|
|
|
|
.unwrap();
|
2019-03-21 08:14:14 -06:00
|
|
|
|
|
|
|
program_pubkey
|
|
|
|
}
|
2019-03-02 21:03:36 -07:00
|
|
|
|
2019-03-21 08:14:14 -06:00
|
|
|
// Return an Instruction that invokes `program_id` with `data` and required
|
|
|
|
// a signature from `from_pubkey`.
|
|
|
|
pub fn create_invoke_instruction<T: Serialize>(
|
|
|
|
from_pubkey: Pubkey,
|
|
|
|
program_id: Pubkey,
|
|
|
|
data: &T,
|
|
|
|
) -> Instruction {
|
|
|
|
let account_metas = vec![AccountMeta::new(from_pubkey, true)];
|
|
|
|
Instruction::new(program_id, data, account_metas)
|
2019-03-02 21:03:36 -07:00
|
|
|
}
|