Files
solana/programs/bpf/rust/noop/src/lib.rs

69 lines
1.6 KiB
Rust
Raw Normal View History

//! @brief Example Rust-based BPF program that prints out the parameters passed to it
2019-06-20 07:43:31 -07:00
#![allow(unreachable_code)]
extern crate solana_sdk;
2019-09-06 17:32:14 -07:00
use solana_sdk::{
account_info::AccountInfo, entrypoint, entrypoint::SUCCESS, info, log::*, pubkey::Pubkey,
};
#[derive(Debug, PartialEq)]
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);
2019-09-06 17:32:14 -07:00
fn process_instruction(program_id: &Pubkey, accounts: &mut [AccountInfo], data: &[u8]) -> u32 {
2019-06-20 16:07:12 -07:00
info!("Program identifier:");
program_id.log();
// 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:");
2019-09-06 17:32:14 -07:00
sol_log_params(accounts, data);
{
2019-09-04 16:00:11 -07:00
// Test - use std methods, unwrap
// 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();
assert_eq!(4, result_str.len());
assert_eq!("💖", result_str);
2019-06-20 16:07:12 -07:00
info!(result_str);
}
{
// Test - struct return
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!();
}
SUCCESS
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_return_sstruct() {
assert_eq!(SStruct { x: 1, y: 2, z: 3 }, return_sstruct());
}
}