2019-02-01 09:00:34 -08:00
|
|
|
#[cfg(any(feature = "bpf_c", feature = "bpf_rust"))]
|
2019-03-02 21:33:02 -08:00
|
|
|
mod bpf {
|
|
|
|
use solana_runtime::bank::Bank;
|
2019-03-21 08:14:14 -06:00
|
|
|
use solana_runtime::bank_client::BankClient;
|
2019-06-11 10:27:22 -07:00
|
|
|
use solana_runtime::genesis_utils::{create_genesis_block, GenesisBlockInfo};
|
2019-05-21 11:22:33 -07:00
|
|
|
use solana_runtime::loader_utils::load_program;
|
2019-03-02 21:33:02 -08:00
|
|
|
use std::env;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::path::PathBuf;
|
2019-02-01 09:00:34 -08:00
|
|
|
|
2019-03-02 21:33:02 -08:00
|
|
|
/// BPF program file extension
|
|
|
|
const PLATFORM_FILE_EXTENSION_BPF: &str = "so";
|
2018-10-04 09:44:44 -07:00
|
|
|
|
2019-03-02 21:33:02 -08:00
|
|
|
/// Create a BPF program file name
|
|
|
|
fn create_bpf_path(name: &str) -> PathBuf {
|
|
|
|
let mut pathbuf = {
|
|
|
|
let current_exe = env::current_exe().unwrap();
|
|
|
|
PathBuf::from(current_exe.parent().unwrap().parent().unwrap())
|
|
|
|
};
|
|
|
|
pathbuf.push("bpf/");
|
|
|
|
pathbuf.push(name);
|
|
|
|
pathbuf.set_extension(PLATFORM_FILE_EXTENSION_BPF);
|
|
|
|
pathbuf
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "bpf_c")]
|
|
|
|
mod bpf_c {
|
|
|
|
use super::*;
|
2019-05-21 11:22:33 -07:00
|
|
|
use solana_runtime::loader_utils::create_invoke_instruction;
|
2019-03-02 21:33:02 -08:00
|
|
|
use solana_sdk::bpf_loader;
|
2019-04-11 00:25:14 -07:00
|
|
|
use solana_sdk::client::SyncClient;
|
2019-03-27 07:34:01 -06:00
|
|
|
use solana_sdk::signature::KeypairUtil;
|
2019-03-02 21:33:02 -08:00
|
|
|
use std::io::Read;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_program_bpf_c() {
|
|
|
|
solana_logger::setup();
|
|
|
|
|
|
|
|
let programs = [
|
2019-06-20 19:10:03 -07:00
|
|
|
("bpf_to_bpf", true),
|
|
|
|
("multiple_static", true),
|
|
|
|
("noop", true),
|
|
|
|
("noop++", true),
|
|
|
|
("panic", false),
|
|
|
|
("relative_call", true),
|
|
|
|
("struct_pass", true),
|
|
|
|
("struct_ret", true),
|
2019-03-02 21:33:02 -08:00
|
|
|
];
|
|
|
|
for program in programs.iter() {
|
2019-06-20 19:10:03 -07:00
|
|
|
println!("Test program: {:?}", program.0);
|
|
|
|
let mut file = File::open(create_bpf_path(program.0)).expect("file open failed");
|
2019-03-02 21:33:02 -08:00
|
|
|
let mut elf = Vec::new();
|
|
|
|
file.read_to_end(&mut elf).unwrap();
|
|
|
|
|
2019-06-11 10:27:22 -07:00
|
|
|
let GenesisBlockInfo {
|
|
|
|
genesis_block,
|
|
|
|
mint_keypair,
|
|
|
|
..
|
|
|
|
} = create_genesis_block(50);
|
2019-03-02 21:33:02 -08:00
|
|
|
let bank = Bank::new(&genesis_block);
|
2019-04-11 11:29:59 -07:00
|
|
|
let bank_client = BankClient::new(bank);
|
2019-03-02 21:33:02 -08:00
|
|
|
|
|
|
|
// Call user program
|
2019-06-12 08:49:59 -07:00
|
|
|
let program_id = load_program(&bank_client, &mint_keypair, &bpf_loader::id(), elf);
|
2019-03-21 08:14:14 -06:00
|
|
|
let instruction =
|
2019-06-10 11:00:15 -07:00
|
|
|
create_invoke_instruction(mint_keypair.pubkey(), program_id, &1u8);
|
2019-06-20 19:10:03 -07:00
|
|
|
let result = bank_client.send_instruction(&mint_keypair, instruction);
|
|
|
|
if program.1 {
|
|
|
|
assert!(result.is_ok());
|
|
|
|
} else {
|
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
2019-03-02 21:33:02 -08:00
|
|
|
}
|
|
|
|
}
|
2018-12-11 09:03:37 -08:00
|
|
|
}
|
2019-01-02 15:12:42 -08:00
|
|
|
|
2019-03-02 21:33:02 -08:00
|
|
|
#[cfg(feature = "bpf_rust")]
|
|
|
|
mod bpf_rust {
|
|
|
|
use super::*;
|
2019-06-12 08:49:59 -07:00
|
|
|
use solana_sdk::bpf_loader;
|
2019-04-11 00:25:14 -07:00
|
|
|
use solana_sdk::client::SyncClient;
|
2019-06-18 15:56:24 -07:00
|
|
|
use solana_sdk::hash;
|
2019-05-21 11:22:33 -07:00
|
|
|
use solana_sdk::instruction::{AccountMeta, Instruction};
|
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2019-03-02 21:33:02 -08:00
|
|
|
use std::io::Read;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_program_bpf_rust() {
|
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-05-21 11:22:33 -07:00
|
|
|
let programs = [
|
2019-06-21 02:43:50 -07:00
|
|
|
("solana_bpf_rust_128bit", true),
|
2019-06-10 11:00:15 -07:00
|
|
|
("solana_bpf_rust_alloc", true),
|
2019-06-20 16:07:12 -07:00
|
|
|
("solana_bpf_rust_dep_crate", true),
|
2019-06-10 11:00:15 -07:00
|
|
|
("solana_bpf_rust_iter", true),
|
2019-07-11 14:27:18 -08:00
|
|
|
("solana_bpf_rust_many_args", true),
|
2019-07-08 15:52:25 -08:00
|
|
|
("solana_bpf_rust_external_spend", false),
|
2019-06-10 11:00:15 -07:00
|
|
|
("solana_bpf_rust_noop", true),
|
|
|
|
("solana_bpf_rust_panic", false),
|
2019-05-21 11:22:33 -07:00
|
|
|
];
|
2019-03-02 21:33:02 -08:00
|
|
|
for program in programs.iter() {
|
2019-06-10 11:00:15 -07:00
|
|
|
let filename = create_bpf_path(program.0);
|
|
|
|
println!("Test program: {:?} from {:?}", program.0, filename);
|
2019-03-02 21:33:02 -08:00
|
|
|
let mut file = File::open(filename).unwrap();
|
|
|
|
let mut elf = Vec::new();
|
|
|
|
file.read_to_end(&mut elf).unwrap();
|
|
|
|
|
2019-06-11 10:27:22 -07:00
|
|
|
let GenesisBlockInfo {
|
|
|
|
genesis_block,
|
|
|
|
mint_keypair,
|
|
|
|
..
|
|
|
|
} = create_genesis_block(50);
|
2019-03-02 21:33:02 -08:00
|
|
|
let bank = Bank::new(&genesis_block);
|
2019-07-08 15:52:25 -08:00
|
|
|
|
2019-06-18 15:56:24 -07:00
|
|
|
// register some ticks, used by solana_bpf_rust_tick_height
|
|
|
|
for i in 0..10 {
|
|
|
|
bank.register_tick(&hash::hash(format!("hashing {}", i).as_bytes()));
|
|
|
|
}
|
2019-04-11 11:29:59 -07:00
|
|
|
let bank_client = BankClient::new(bank);
|
2019-03-21 08:14:14 -06:00
|
|
|
|
2019-03-02 21:33:02 -08:00
|
|
|
// Call user program
|
2019-06-12 08:49:59 -07:00
|
|
|
let program_id = load_program(&bank_client, &mint_keypair, &bpf_loader::id(), elf);
|
2019-05-21 11:22:33 -07:00
|
|
|
let account_metas = vec![
|
2019-06-10 11:00:15 -07:00
|
|
|
AccountMeta::new(mint_keypair.pubkey(), true),
|
2019-05-21 11:22:33 -07:00
|
|
|
AccountMeta::new(Keypair::new().pubkey(), false),
|
|
|
|
];
|
|
|
|
let instruction = Instruction::new(program_id, &1u8, account_metas);
|
2019-06-10 11:00:15 -07:00
|
|
|
let result = bank_client.send_instruction(&mint_keypair, instruction);
|
|
|
|
if program.1 {
|
|
|
|
assert!(result.is_ok());
|
|
|
|
} else {
|
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
2019-03-02 21:33:02 -08:00
|
|
|
}
|
|
|
|
}
|
2019-01-02 15:12:42 -08:00
|
|
|
}
|
|
|
|
}
|