2019-01-02 15:12:42 -08:00
|
|
|
//! @brief Example Rust-based BPF program that prints out the parameters passed to it
|
2018-10-04 09:44:44 -07:00
|
|
|
|
2019-06-20 07:43:31 -07:00
|
|
|
#![allow(unreachable_code)]
|
2018-10-04 09:44:44 -07:00
|
|
|
|
2019-09-06 09:20:14 -07:00
|
|
|
extern crate solana_sdk;
|
2019-09-06 17:32:14 -07:00
|
|
|
use solana_sdk::{
|
2020-03-05 10:57:35 -08:00
|
|
|
account_info::AccountInfo, bpf_loader, entrypoint, entrypoint::ProgramResult, info, log::*,
|
|
|
|
pubkey::Pubkey,
|
2019-09-06 17:32:14 -07:00
|
|
|
};
|
2019-01-02 15:12:42 -08:00
|
|
|
|
2019-07-08 20:28:05 -08:00
|
|
|
#[derive(Debug, PartialEq)]
|
2019-02-28 22:05:11 -08:00
|
|
|
struct SStruct {
|
|
|
|
x: u64,
|
|
|
|
y: u64,
|
|
|
|
z: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
fn return_sstruct() -> SStruct {
|
|
|
|
SStruct { x: 1, y: 2, z: 3 }
|
|
|
|
}
|
|
|
|
|
2019-05-21 13:39:27 -07:00
|
|
|
entrypoint!(process_instruction);
|
2020-01-24 13:41:14 -08:00
|
|
|
fn process_instruction(
|
|
|
|
program_id: &Pubkey,
|
2020-01-27 18:27:44 -08:00
|
|
|
accounts: &[AccountInfo],
|
2020-01-24 13:41:14 -08:00
|
|
|
instruction_data: &[u8],
|
2020-02-04 12:25:42 -08:00
|
|
|
) -> ProgramResult {
|
2019-06-20 16:07:12 -07:00
|
|
|
info!("Program identifier:");
|
2019-09-06 14:44:41 -07:00
|
|
|
program_id.log();
|
2019-01-02 15:12:42 -08:00
|
|
|
|
2020-03-05 10:57:35 -08:00
|
|
|
assert!(!bpf_loader::check_id(program_id));
|
|
|
|
|
2019-01-02 15:12:42 -08:00
|
|
|
// Log the provided account keys and instruction input data. In the case of
|
|
|
|
// the no-op program, no account keys or input data are expected but real
|
|
|
|
// programs will have specific requirements so they can do their work.
|
2019-06-20 16:07:12 -07:00
|
|
|
info!("Account keys and instruction input data:");
|
2020-01-24 13:41:14 -08:00
|
|
|
sol_log_params(accounts, instruction_data);
|
2019-02-28 22:05:11 -08:00
|
|
|
|
2019-05-17 11:04:29 -07:00
|
|
|
{
|
2019-09-04 16:00:11 -07:00
|
|
|
// Test - use std methods, unwrap
|
2019-02-28 22:05:11 -08:00
|
|
|
|
|
|
|
// valid bytes, in a stack-allocated array
|
|
|
|
let sparkle_heart = [240, 159, 146, 150];
|
2019-09-04 16:00:11 -07:00
|
|
|
let result_str = std::str::from_utf8(&sparkle_heart).unwrap();
|
2019-05-17 11:04:29 -07:00
|
|
|
assert_eq!(4, result_str.len());
|
2019-02-28 22:05:11 -08:00
|
|
|
assert_eq!("💖", result_str);
|
2019-06-20 16:07:12 -07:00
|
|
|
info!(result_str);
|
2019-02-28 22:05:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test - struct return
|
2019-05-17 11:04:29 -07:00
|
|
|
|
2019-02-28 22:05:11 -08:00
|
|
|
let s = return_sstruct();
|
|
|
|
assert_eq!(s.x + s.y + s.z, 6);
|
|
|
|
}
|
|
|
|
|
2019-09-04 16:00:11 -07:00
|
|
|
{
|
|
|
|
// Test - arch config
|
|
|
|
#[cfg(not(target_arch = "bpf"))]
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
2020-01-30 09:47:22 -08:00
|
|
|
Ok(())
|
2018-10-04 09:44:44 -07:00
|
|
|
}
|
2019-07-08 20:28:05 -08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2020-07-01 13:15:30 -07:00
|
|
|
// Pull in syscall stubs when building for non-BPF targets
|
2020-06-18 23:23:28 -07:00
|
|
|
solana_sdk::program_stubs!();
|
2019-07-08 20:28:05 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_return_sstruct() {
|
|
|
|
assert_eq!(SStruct { x: 1, y: 2, z: 3 }, return_sstruct());
|
|
|
|
}
|
|
|
|
}
|