2018-10-16 09:43:49 -07:00
|
|
|
use solana::bank::Bank;
|
2019-01-24 12:04:04 -08:00
|
|
|
use solana::genesis_block::GenesisBlock;
|
2018-12-04 15:18:35 -08:00
|
|
|
use solana_sdk::loader_transaction::LoaderTransaction;
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2018-12-04 15:37:11 -08:00
|
|
|
use solana_sdk::system_transaction::SystemTransaction;
|
2018-11-29 16:18:47 -08:00
|
|
|
use solana_sdk::transaction::Transaction;
|
2018-10-18 14:19:25 -07:00
|
|
|
|
2019-02-01 09:00:34 -08:00
|
|
|
fn load_program(bank: &Bank, from: &Keypair, loader_id: Pubkey, program: Vec<u8>) -> Pubkey {
|
|
|
|
let program_account = Keypair::new();
|
2018-10-18 14:19:25 -07:00
|
|
|
|
2019-02-01 09:00:34 -08:00
|
|
|
let tx = SystemTransaction::new_program_account(
|
|
|
|
from,
|
|
|
|
program_account.pubkey(),
|
|
|
|
bank.last_id(),
|
|
|
|
1,
|
|
|
|
program.len() as u64,
|
|
|
|
loader_id,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
bank.process_transaction(&tx).unwrap();
|
2018-10-18 14:19:25 -07:00
|
|
|
|
2019-02-01 09:00:34 -08: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-02-01 07:36:35 -08:00
|
|
|
let tx = LoaderTransaction::new_write(
|
2019-02-01 09:00:34 -08:00
|
|
|
&program_account,
|
|
|
|
loader_id,
|
|
|
|
offset,
|
|
|
|
chunk.to_vec(),
|
|
|
|
bank.last_id(),
|
2019-01-24 12:04:04 -08:00
|
|
|
0,
|
|
|
|
);
|
2019-02-01 09:00:34 -08:00
|
|
|
bank.process_transaction(&tx).unwrap();
|
|
|
|
offset += chunk_size as u32;
|
2018-10-18 14:19:25 -07:00
|
|
|
}
|
|
|
|
|
2019-02-01 09:00:34 -08:00
|
|
|
let tx = LoaderTransaction::new_finalize(&program_account, loader_id, bank.last_id(), 0);
|
|
|
|
bank.process_transaction(&tx).unwrap();
|
2018-10-23 14:44:41 -07:00
|
|
|
|
2019-02-01 09:00:34 -08:00
|
|
|
let tx = SystemTransaction::new_spawn(&program_account, bank.last_id(), 0);
|
|
|
|
bank.process_transaction(&tx).unwrap();
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2019-02-01 09:00:34 -08:00
|
|
|
program_account.pubkey()
|
2018-10-18 14:19:25 -07:00
|
|
|
}
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2018-10-18 14:19:25 -07:00
|
|
|
#[test]
|
2018-10-19 18:28:38 -07:00
|
|
|
fn test_program_native_noop() {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2019-02-01 09:00:34 -08:00
|
|
|
let (genesis_block, mint_keypair) = GenesisBlock::new(50);
|
|
|
|
let bank = Bank::new(&genesis_block);
|
|
|
|
|
|
|
|
let program = "noop".as_bytes().to_vec();
|
|
|
|
let program_id = load_program(&bank, &mint_keypair, solana_native_loader::id(), program);
|
2018-10-18 10:33:30 -07:00
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
// Call user program
|
2019-02-01 09:00:34 -08:00
|
|
|
let tx = Transaction::new(&mint_keypair, &[], program_id, &1u8, bank.last_id(), 0);
|
|
|
|
bank.process_transaction(&tx).unwrap();
|
2018-09-28 11:17:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-10-18 14:19:25 -07:00
|
|
|
fn test_program_lua_move_funds() {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2019-02-01 09:00:34 -08:00
|
|
|
let (genesis_block, mint_keypair) = GenesisBlock::new(50);
|
|
|
|
let bank = Bank::new(&genesis_block);
|
|
|
|
let loader_id = load_program(
|
|
|
|
&bank,
|
|
|
|
&mint_keypair,
|
|
|
|
solana_native_loader::id(),
|
|
|
|
"solana_lua_loader".as_bytes().to_vec(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let program = r#"
|
2018-10-16 09:43:49 -07:00
|
|
|
print("Lua Script!")
|
|
|
|
local tokens, _ = string.unpack("I", data)
|
|
|
|
accounts[1].tokens = accounts[1].tokens - tokens
|
|
|
|
accounts[2].tokens = accounts[2].tokens + tokens
|
2018-12-07 19:01:28 -08:00
|
|
|
"#
|
|
|
|
.as_bytes()
|
2018-10-16 09:43:49 -07:00
|
|
|
.to_vec();
|
2019-02-01 09:00:34 -08:00
|
|
|
let program_id = load_program(&bank, &mint_keypair, loader_id, program);
|
2018-10-18 14:19:25 -07:00
|
|
|
let from = Keypair::new();
|
|
|
|
let to = Keypair::new().pubkey();
|
2018-10-18 10:33:30 -07:00
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
// Call user program with two accounts
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = SystemTransaction::new_program_account(
|
2019-02-01 09:00:34 -08:00
|
|
|
&mint_keypair,
|
2018-10-16 09:43:49 -07:00
|
|
|
from.pubkey(),
|
2019-02-01 09:00:34 -08:00
|
|
|
bank.last_id(),
|
2018-10-16 09:43:49 -07:00
|
|
|
10,
|
|
|
|
0,
|
2019-02-01 09:00:34 -08:00
|
|
|
program_id,
|
2018-10-16 09:43:49 -07:00
|
|
|
0,
|
|
|
|
);
|
2019-02-01 09:00:34 -08:00
|
|
|
bank.process_transaction(&tx).unwrap();
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = SystemTransaction::new_program_account(
|
2019-02-01 09:00:34 -08:00
|
|
|
&mint_keypair,
|
2018-10-16 09:43:49 -07:00
|
|
|
to,
|
2019-02-01 09:00:34 -08:00
|
|
|
bank.last_id(),
|
2018-10-16 09:43:49 -07:00
|
|
|
1,
|
|
|
|
0,
|
2019-02-01 09:00:34 -08:00
|
|
|
program_id,
|
2018-10-16 09:43:49 -07:00
|
|
|
0,
|
|
|
|
);
|
2019-02-01 09:00:34 -08:00
|
|
|
bank.process_transaction(&tx).unwrap();
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2019-02-01 09:00:34 -08:00
|
|
|
let tx = Transaction::new(&from, &[to], program_id, &10, bank.last_id(), 0);
|
|
|
|
bank.process_transaction(&tx).unwrap();
|
|
|
|
assert_eq!(bank.get_balance(&from.pubkey()), 0);
|
|
|
|
assert_eq!(bank.get_balance(&to), 11);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "bpf_c")]
|
|
|
|
use solana_sdk::bpf_loader;
|
|
|
|
#[cfg(any(feature = "bpf_c", feature = "bpf_rust"))]
|
|
|
|
use std::env;
|
|
|
|
#[cfg(any(feature = "bpf_c", feature = "bpf_rust"))]
|
|
|
|
use std::fs::File;
|
|
|
|
#[cfg(any(feature = "bpf_c", feature = "bpf_rust"))]
|
|
|
|
use std::io::Read;
|
|
|
|
#[cfg(any(feature = "bpf_c", feature = "bpf_rust"))]
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
/// BPF program file extension
|
|
|
|
#[cfg(any(feature = "bpf_c", feature = "bpf_rust"))]
|
|
|
|
const PLATFORM_FILE_EXTENSION_BPF: &str = "so";
|
|
|
|
/// Create a BPF program file name
|
|
|
|
#[cfg(any(feature = "bpf_c", feature = "bpf_rust"))]
|
|
|
|
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
|
2018-10-04 09:44:44 -07:00
|
|
|
}
|
|
|
|
|
2018-10-23 14:44:41 -07:00
|
|
|
#[cfg(feature = "bpf_c")]
|
|
|
|
#[test]
|
2019-02-01 09:00:34 -08:00
|
|
|
fn test_program_bpf_c_noop() {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-10-23 14:44:41 -07:00
|
|
|
|
2018-11-06 14:28:46 -08:00
|
|
|
let mut file = File::open(create_bpf_path("noop")).expect("file open failed");
|
|
|
|
let mut elf = Vec::new();
|
|
|
|
file.read_to_end(&mut elf).unwrap();
|
|
|
|
|
2019-02-01 09:00:34 -08:00
|
|
|
let (genesis_block, mint_keypair) = GenesisBlock::new(50);
|
|
|
|
let bank = Bank::new(&genesis_block);
|
2018-10-23 14:44:41 -07:00
|
|
|
|
|
|
|
// Call user program
|
2019-02-01 09:00:34 -08:00
|
|
|
let program_id = load_program(&bank, &mint_keypair, bpf_loader::id(), elf);
|
2018-10-23 14:44:41 -07:00
|
|
|
let tx = Transaction::new(
|
2019-02-01 09:00:34 -08:00
|
|
|
&mint_keypair,
|
2018-10-23 14:44:41 -07:00
|
|
|
&[],
|
2019-02-01 09:00:34 -08:00
|
|
|
program_id,
|
2018-11-13 17:19:10 -08:00
|
|
|
&vec![1u8],
|
2019-02-01 09:00:34 -08:00
|
|
|
bank.last_id(),
|
2018-10-23 14:44:41 -07:00
|
|
|
0,
|
|
|
|
);
|
2019-02-01 09:00:34 -08:00
|
|
|
bank.process_transaction(&tx).unwrap();
|
2018-10-23 14:44:41 -07:00
|
|
|
}
|
|
|
|
|
2018-10-04 09:44:44 -07:00
|
|
|
#[cfg(feature = "bpf_c")]
|
|
|
|
#[test]
|
2018-12-11 09:03:37 -08:00
|
|
|
fn test_program_bpf_c() {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2018-12-12 08:41:45 -08:00
|
|
|
let programs = [
|
2019-01-11 19:33:08 -08:00
|
|
|
"bpf_to_bpf",
|
2019-01-11 15:33:21 -08:00
|
|
|
"multiple_static",
|
2018-12-12 08:41:45 -08:00
|
|
|
"noop",
|
|
|
|
"noop++",
|
2019-01-11 19:33:08 -08:00
|
|
|
"relative_call",
|
2018-12-12 08:41:45 -08:00
|
|
|
"struct_pass",
|
|
|
|
"struct_ret",
|
|
|
|
];
|
2018-12-11 09:03:37 -08:00
|
|
|
for program in programs.iter() {
|
|
|
|
println!("Test program: {:?}", program);
|
|
|
|
let mut file = File::open(create_bpf_path(program)).expect("file open failed");
|
|
|
|
let mut elf = Vec::new();
|
|
|
|
file.read_to_end(&mut elf).unwrap();
|
|
|
|
|
2019-02-01 09:00:34 -08:00
|
|
|
let (genesis_block, mint_keypair) = GenesisBlock::new(50);
|
|
|
|
let bank = Bank::new(&genesis_block);
|
|
|
|
|
|
|
|
let loader_id = load_program(
|
|
|
|
&bank,
|
|
|
|
&mint_keypair,
|
|
|
|
solana_native_loader::id(),
|
|
|
|
"solana_bpf_loader".as_bytes().to_vec(),
|
|
|
|
);
|
2018-12-11 09:03:37 -08:00
|
|
|
|
|
|
|
// Call user program
|
2019-02-01 09:00:34 -08:00
|
|
|
let program_id = load_program(&bank, &mint_keypair, loader_id, elf);
|
2018-12-11 09:03:37 -08:00
|
|
|
let tx = Transaction::new(
|
2019-02-01 09:00:34 -08:00
|
|
|
&mint_keypair,
|
2018-12-11 09:03:37 -08:00
|
|
|
&[],
|
2019-02-01 09:00:34 -08:00
|
|
|
program_id,
|
2018-12-11 09:03:37 -08:00
|
|
|
&vec![1u8],
|
2019-02-01 09:00:34 -08:00
|
|
|
bank.last_id(),
|
2018-12-11 09:03:37 -08:00
|
|
|
0,
|
|
|
|
);
|
2019-02-01 09:00:34 -08:00
|
|
|
bank.process_transaction(&tx).unwrap();
|
2018-12-11 09:03:37 -08:00
|
|
|
}
|
2018-10-18 14:19:25 -07:00
|
|
|
}
|
2019-01-02 15:12:42 -08:00
|
|
|
|
|
|
|
// Cannot currently build the Rust BPF program as part
|
|
|
|
// of the rest of the build due to recursive `cargo build` causing
|
|
|
|
// a build deadlock. Therefore you must build the Rust programs
|
|
|
|
// yourself first by calling `make all` in the Rust BPF program's directory
|
|
|
|
#[cfg(feature = "bpf_rust")]
|
|
|
|
#[test]
|
|
|
|
fn test_program_bpf_rust() {
|
|
|
|
solana_logger::setup();
|
|
|
|
|
|
|
|
let programs = ["solana_bpf_rust_noop"];
|
|
|
|
for program in programs.iter() {
|
|
|
|
println!("Test program: {:?}", program);
|
|
|
|
let mut file = File::open(create_bpf_path(program)).expect("file open failed");
|
|
|
|
let mut elf = Vec::new();
|
|
|
|
file.read_to_end(&mut elf).unwrap();
|
|
|
|
|
2019-02-01 09:00:34 -08:00
|
|
|
let (genesis_block, mint_keypair) = GenesisBlock::new(50);
|
|
|
|
let bank = Bank::new(&genesis_block);
|
|
|
|
let loader_id = load_program(
|
|
|
|
&bank,
|
|
|
|
&mint_keypair,
|
|
|
|
solana_native_loader::id(),
|
|
|
|
"solana_bpf_loader".as_bytes().to_vec(),
|
|
|
|
);
|
2019-01-02 15:12:42 -08:00
|
|
|
|
|
|
|
// Call user program
|
2019-02-01 09:00:34 -08:00
|
|
|
let program_id = load_program(&bank, &mint_keypair, loader_id, elf);
|
2019-01-02 15:12:42 -08:00
|
|
|
let tx = Transaction::new(
|
2019-02-01 09:00:34 -08:00
|
|
|
&mint_keypair,
|
2019-01-02 15:12:42 -08:00
|
|
|
&[],
|
2019-02-01 09:00:34 -08:00
|
|
|
program_id,
|
2019-01-02 15:12:42 -08:00
|
|
|
&vec![1u8],
|
2019-02-01 09:00:34 -08:00
|
|
|
bank.last_id(),
|
2019-01-02 15:12:42 -08:00
|
|
|
0,
|
|
|
|
);
|
2019-02-01 09:00:34 -08:00
|
|
|
bank.process_transaction(&tx).unwrap();
|
2019-01-02 15:12:42 -08:00
|
|
|
}
|
|
|
|
}
|