Files
solana/web3.js/examples/bpf-rust-noop/src/lib.rs

73 lines
1.7 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
2019-06-17 15:09:32 -07:00
#![allow(unreachable_code)]
2019-05-10 14:16:35 -07:00
2019-09-13 12:36:08 -07:00
extern crate solana_sdk;
use solana_sdk::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, info, log::*, pubkey::Pubkey,
2019-09-13 12:36:08 -07:00
};
2019-05-10 14:16:35 -07:00
2019-09-13 12:36:08 -07:00
#[derive(Debug, PartialEq)]
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(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
info!("Program identifier:");
2019-09-13 12:36:08 -07:00
program_id.log();
2019-05-10 14:16:35 -07: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.
info!("Account keys and instruction input data:");
sol_log_params(accounts, instruction_data);
2019-05-10 14:16:35 -07:00
2019-06-17 15:09:32 -07:00
{
2019-09-13 12:36:08 -07:00
// Test - use std methods, unwrap
2019-05-10 14:16:35 -07:00
// valid bytes, in a stack-allocated array
let sparkle_heart = [240, 159, 146, 150];
2019-09-13 12:36:08 -07:00
let result_str = std::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);
info!(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);
}
2019-09-13 12:36:08 -07:00
{
// Test - arch config
#[cfg(not(target_arch = "bpf"))]
panic!();
}
Ok(())
2019-09-13 12:36:08 -07:00
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_return_sstruct() {
assert_eq!(SStruct { x: 1, y: 2, z: 3 }, return_sstruct());
}
2019-05-10 14:16:35 -07:00
}