Update rust example to use BPF enabled infrastructure (#2974)
This commit is contained in:
@@ -7,6 +7,17 @@ mod solana_sdk;
|
||||
|
||||
use solana_sdk::*;
|
||||
|
||||
struct SStruct {
|
||||
x: u64,
|
||||
y: u64,
|
||||
z: u64,
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
fn return_sstruct() -> SStruct {
|
||||
SStruct { x: 1, y: 2, z: 3 }
|
||||
}
|
||||
|
||||
fn process(ka: &mut [SolKeyedAccount], data: &[u8], info: &SolClusterInfo) -> bool {
|
||||
sol_log("Tick height:");
|
||||
sol_log_64(info.tick_height, 0, 0, 0, 0);
|
||||
@@ -18,5 +29,27 @@ fn process(ka: &mut [SolKeyedAccount], data: &[u8], info: &SolClusterInfo) -> bo
|
||||
// programs will have specific requirements so they can do their work.
|
||||
sol_log("Account keys and instruction input data:");
|
||||
sol_log_params(ka, data);
|
||||
|
||||
{
|
||||
// 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();
|
||||
|
||||
sol_log_64(0, 0, 0, 0, result_str.len() as u64);
|
||||
sol_log(result_str);
|
||||
assert_eq!("💖", result_str);
|
||||
}
|
||||
|
||||
{
|
||||
// Test - struct return
|
||||
let s = return_sstruct();
|
||||
sol_log_64(0, 0, s.x, s.y, s.z);
|
||||
assert_eq!(s.x + s.y + s.z, 6);
|
||||
}
|
||||
|
||||
sol_log("Success");
|
||||
true
|
||||
}
|
||||
|
@@ -1,39 +1,80 @@
|
||||
//! @brief Solana Rust-based BPF program utility functions and types
|
||||
|
||||
extern crate heapless;
|
||||
// extern crate heapless;
|
||||
|
||||
use self::heapless::consts::*;
|
||||
use self::heapless::String; // fixed capacity `std::Vec` // type level integer used to specify capacity
|
||||
// use self::heapless::consts::*;
|
||||
// use self::heapless::String; // fixed capacity `std::Vec` // type level integer used to specify capacity
|
||||
#[cfg(test)]
|
||||
use self::tests::process;
|
||||
use core::mem::size_of;
|
||||
use core::panic::PanicInfo;
|
||||
use core::slice::from_raw_parts;
|
||||
|
||||
#[cfg(not(test))]
|
||||
use process;
|
||||
|
||||
// Panic handling
|
||||
extern "C" {
|
||||
pub fn sol_panic_() -> !;
|
||||
}
|
||||
#[panic_handler]
|
||||
fn panic(_info: &PanicInfo) -> ! {
|
||||
sol_log("Panic!");
|
||||
// TODO rashes! sol_log(_info.payload().downcast_ref::<&str>().unwrap());
|
||||
if let Some(location) = _info.location() {
|
||||
if !location.file().is_empty() {
|
||||
// TODO location.file() returns empty str, if we get here its been fixed
|
||||
sol_log(location.file());
|
||||
sol_log("location.file() is fixed!!");
|
||||
unsafe {
|
||||
sol_panic_();
|
||||
}
|
||||
}
|
||||
sol_log_64(0, 0, 0, location.line() as u64, location.column() as u64);
|
||||
} else {
|
||||
sol_log("Panic! but could not get location information");
|
||||
}
|
||||
unsafe {
|
||||
sol_panic_();
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn sol_log_(message: *const u8);
|
||||
}
|
||||
/// Helper function that prints a string to stdout
|
||||
#[inline(never)] // stack intensive, block inline so everyone does not incur
|
||||
pub fn sol_log(message: &str) {
|
||||
let mut c_string: String<U256> = String::new();
|
||||
if message.len() < 256 {
|
||||
if c_string.push_str(message).is_err() {
|
||||
c_string
|
||||
.push_str("Attempted to log a malformed string\0")
|
||||
.is_ok();
|
||||
// TODO This is extremely slow, do something better
|
||||
let mut buf: [u8; 128] = [0; 128];
|
||||
for (i, b) in message.as_bytes().iter().enumerate() {
|
||||
if i >= 126 {
|
||||
break;
|
||||
}
|
||||
if c_string.push('\0').is_err() {
|
||||
c_string.push_str("Failed to log string\0").is_ok();
|
||||
};
|
||||
} else {
|
||||
c_string
|
||||
.push_str("Attempted to log a string that is too long\0")
|
||||
.is_ok();
|
||||
buf[i] = *b;
|
||||
}
|
||||
unsafe {
|
||||
sol_log_(c_string.as_bytes().as_ptr());
|
||||
sol_log_(buf.as_ptr());
|
||||
}
|
||||
|
||||
// let mut c_string: String<U256> = String::new();
|
||||
// if message.len() < 256 {
|
||||
// if c_string.push_str(message).is_err() {
|
||||
// c_string
|
||||
// .push_str("Attempted to log a malformed string\0")
|
||||
// .is_ok();
|
||||
// }
|
||||
// if c_string.push('\0').is_err() {
|
||||
// c_string.push_str("Failed to log string\0").is_ok();
|
||||
// };
|
||||
// } else {
|
||||
// c_string
|
||||
// .push_str("Attempted to log a string that is too long\0")
|
||||
// .is_ok();
|
||||
// }
|
||||
// unsafe {
|
||||
// sol_log_(message.as_ptr());
|
||||
// }
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
Reference in New Issue
Block a user