2022-10-28 11:30:31 -07:00
|
|
|
//! Example Rust-based SBF sanity program that prints out the parameters passed to it
|
2020-09-11 18:49:00 -07:00
|
|
|
|
|
|
|
#![allow(unreachable_code)]
|
|
|
|
|
2020-10-23 17:22:10 -07:00
|
|
|
extern crate solana_program;
|
|
|
|
use solana_program::{
|
2022-03-30 08:28:49 -07:00
|
|
|
account_info::AccountInfo, bpf_loader, entrypoint::ProgramResult, log::*, msg,
|
|
|
|
program::check_type_assumptions, pubkey::Pubkey,
|
2020-09-11 18:49:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
struct SStruct {
|
|
|
|
x: u64,
|
|
|
|
y: u64,
|
|
|
|
z: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
fn return_sstruct() -> SStruct {
|
|
|
|
SStruct { x: 1, y: 2, z: 3 }
|
|
|
|
}
|
|
|
|
|
2022-02-21 19:59:06 -08:00
|
|
|
solana_program::entrypoint!(process_instruction);
|
2020-12-13 17:26:34 -08:00
|
|
|
#[allow(clippy::unnecessary_wraps)]
|
2021-08-11 12:07:15 -07:00
|
|
|
pub fn process_instruction(
|
2020-09-11 18:49:00 -07:00
|
|
|
program_id: &Pubkey,
|
|
|
|
accounts: &[AccountInfo],
|
|
|
|
instruction_data: &[u8],
|
|
|
|
) -> ProgramResult {
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("Program identifier:");
|
2020-09-11 18:49:00 -07:00
|
|
|
program_id.log();
|
|
|
|
|
|
|
|
assert!(!bpf_loader::check_id(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.
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("Account keys and instruction input data:");
|
2020-09-11 18:49:00 -07:00
|
|
|
sol_log_params(accounts, instruction_data);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test - use std methods, unwrap
|
|
|
|
|
|
|
|
// valid bytes, in a stack-allocated array
|
|
|
|
let sparkle_heart = [240, 159, 146, 150];
|
|
|
|
let result_str = std::str::from_utf8(&sparkle_heart).unwrap();
|
|
|
|
assert_eq!(4, result_str.len());
|
|
|
|
assert_eq!("💖", result_str);
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!(result_str);
|
2020-09-11 18:49:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test - struct return
|
|
|
|
|
|
|
|
let s = return_sstruct();
|
|
|
|
assert_eq!(s.x + s.y + s.z, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test - arch config
|
2022-05-18 18:17:29 -07:00
|
|
|
#[cfg(not(target_os = "solana"))]
|
2020-09-11 18:49:00 -07:00
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:09:58 -07:00
|
|
|
{
|
|
|
|
// Test - float math functions
|
|
|
|
let zero = accounts[0].try_borrow_mut_data()?.len() as f64;
|
|
|
|
let num = zero + 8.0f64;
|
|
|
|
let num = num.powf(0.333f64);
|
|
|
|
// check that the result is in a correct interval close to 1.998614185980905
|
|
|
|
assert!(1.9986f64 < num && num < 2.0f64);
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:28:49 -07:00
|
|
|
check_type_assumptions();
|
|
|
|
|
2020-10-28 12:39:48 -07:00
|
|
|
sol_log_compute_units();
|
2020-09-11 18:49:00 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_return_sstruct() {
|
|
|
|
assert_eq!(SStruct { x: 1, y: 2, z: 3 }, return_sstruct());
|
|
|
|
}
|
|
|
|
}
|