65 lines
1.5 KiB
Rust
Raw Normal View History

2019-05-10 14:16:35 -07:00
//! @brief Example Rust-based BPF program that prints out the parameters passed to it
#![no_std]
2019-06-17 15:09:32 -07:00
#![allow(unreachable_code)]
2019-05-10 14:16:35 -07:00
2019-06-17 15:09:32 -07:00
extern crate solana_sdk_bpf_utils;
2019-05-10 14:16:35 -07:00
2019-06-17 15:09:32 -07:00
use solana_sdk_bpf_utils::entrypoint;
use solana_sdk_bpf_utils::entrypoint::*;
use solana_sdk_bpf_utils::log::*;
2019-05-10 14:16:35 -07:00
struct SStruct {
x: u64,
y: u64,
z: u64,
}
#[inline(never)]
fn return_sstruct() -> SStruct {
SStruct { x: 1, y: 2, z: 3 }
}
2019-06-17 15:09:32 -07:00
entrypoint!(process_instruction);
fn process_instruction(
ka: &mut [Option<SolKeyedAccount>; MAX_ACCOUNTS],
info: &SolClusterInfo,
data: &[u8],
) -> bool {
2019-05-10 14:16:35 -07:00
sol_log("Program identifier:");
sol_log_key(&info.program_id);
// 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.
sol_log("Account keys and instruction input data:");
sol_log_params(ka, data);
2019-06-17 15:09:32 -07:00
{
// Test - arch config
#[cfg(not(target_arch = "bpf"))]
panic!();
}
2019-05-10 14:16:35 -07:00
{
// Test - use core methods, unwrap
// valid bytes, in a stack-allocated array
let sparkle_heart = [240, 159, 146, 150];
let result_str = core::str::from_utf8(&sparkle_heart).unwrap();
2019-06-17 15:09:32 -07:00
assert_eq!(4, result_str.len());
2019-05-10 14:16:35 -07:00
assert_eq!("💖", result_str);
2019-06-17 15:09:32 -07:00
sol_log(result_str);
2019-05-10 14:16:35 -07:00
}
{
// Test - struct return
2019-06-17 15:09:32 -07:00
2019-05-10 14:16:35 -07:00
let s = return_sstruct();
assert_eq!(s.x + s.y + s.z, 6);
}
sol_log("Success");
true
}