fix: update examples (#485)

This commit is contained in:
Jack May 2019-09-13 12:36:08 -07:00 committed by Michael Vines
parent 82aaa8af47
commit ee3acbf1ba
7 changed files with 56 additions and 44 deletions

View File

@ -5,15 +5,15 @@
#include <solana_sdk.h>
extern bool entrypoint(const uint8_t *input) {
extern uint32_t entrypoint(const uint8_t *input) {
SolKeyedAccount ka[1];
SolParameters params = (SolParameters) { .ka = ka };
sol_log("Hello World");
if (!sol_deserialize(input, &params, SOL_ARRAY_SIZE(ka))) {
return false;
return 1;
}
sol_log_params(&params);
return true;
return SUCCESS;
}

View File

@ -3,7 +3,7 @@
[package]
name = "solana-bpf-rust-noop"
version = "0.16.0"
version = "0.19.0-pre0"
description = "Solana BPF noop program written in Rust"
authors = ["Solana Maintainers <maintainers@solana.com>"]
repository = "https://github.com/solana-labs/solana"
@ -12,11 +12,11 @@ homepage = "https://solana.com/"
edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../bpf-sdk/rust/rust-utils" }
solana-sdk-bpf-no-std = { path = "../../bpf-sdk/rust/rust-no-std" }
solana-sdk = { git = "https://github.com/solana-labs/solana", default-features = false }
[dev_dependencies]
solana-sdk-bpf-test = { path = "../../bpf-sdk/rust/rust-test"}
[features]
program = ["solana-sdk/program"]
default = ["program"]
[workspace]
members = []

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../bpf-sdk/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../bpf-sdk/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -20,26 +20,37 @@ Supported actions:
EOF
}
sdkDir=../../bpf-sdk
targetDir="$PWD"/target
profile=bpfel-unknown-unknown/release
perform_action() {
set -e
case "$1" in
build)
../../bpf-sdk/rust/build.sh "$PWD"
"$sdkDir"/rust/build.sh "$PWD"
so_path="$targetDir/$profile"
so_name="solana_bpf_rust_noop"
if [ -f "$so_path/${so_name}.so" ]; then
cp "$so_path/${so_name}.so" "$so_path/${so_name}_debug.so"
"$sdkDir"/dependencies/llvm-native/bin/llvm-objcopy --strip-all "$so_path/${so_name}.so" "$so_path/$so_name.so"
fi
;;
clean)
../../bpf-sdk/rust/clean.sh "$PWD"
"$sdkDir"/rust/clean.sh "$PWD"
;;
test)
echo "test $2"
cargo +nightly test
echo "test"
cargo +nightly test
;;
clippy)
echo "clippy $2"
cargo +nightly clippy
echo "clippy"
cargo +nightly clippy
;;
fmt)
echo "formatting $2"
cargo fmt
echo "formatting"
cargo fmt
;;
help)
usage

View File

@ -1,17 +1,13 @@
//! @brief Example Rust-based BPF program that prints out the parameters passed to it
#![no_std]
#![allow(unreachable_code)]
#![allow(unused_attributes)]
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
use solana_sdk_bpf_utils::entrypoint::*;
use solana_sdk_bpf_utils::log::*;
use solana_sdk_bpf_utils::{entrypoint, info};
extern crate solana_sdk;
use solana_sdk::{
account_info::AccountInfo, entrypoint, entrypoint::SUCCESS, info, log::*, pubkey::Pubkey,
};
#[derive(Debug, PartialEq)]
struct SStruct {
x: u64,
y: u64,
@ -24,28 +20,22 @@ fn return_sstruct() -> SStruct {
}
entrypoint!(process_instruction);
fn process_instruction(ka: &mut [SolKeyedAccount], info: &SolClusterInfo, data: &[u8]) -> bool {
fn process_instruction(program_id: &Pubkey, accounts: &mut [AccountInfo], data: &[u8]) -> u32 {
info!("Program identifier:");
sol_log_key(&info.program_id);
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.
info!("Account keys and instruction input data:");
sol_log_params(ka, data);
sol_log_params(accounts, data);
{
// Test - arch config
#[cfg(not(target_arch = "bpf"))]
panic!();
}
{
// Test - use core methods, unwrap
// Test - use std 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();
let result_str = std::str::from_utf8(&sparkle_heart).unwrap();
assert_eq!(4, result_str.len());
assert_eq!("💖", result_str);
info!(result_str);
@ -58,6 +48,21 @@ fn process_instruction(ka: &mut [SolKeyedAccount], info: &SolClusterInfo, data:
assert_eq!(s.x + s.y + s.z, 6);
}
info!("Success");
true
{
// 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());
}
}

Binary file not shown.