Skip to content

Commit

Permalink
Add hello-solana example source files to SVM folder (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasSte authored Mar 21, 2024
1 parent dff99d0 commit 4d838d5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ members = [
"zk-token-sdk",
]

exclude = ["programs/sbf"]
exclude = ["programs/sbf", "svm/tests/example-programs"]

# This prevents a Travis CI error when building for Windows.
resolver = "2"
Expand Down
37 changes: 13 additions & 24 deletions svm/src/transaction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,19 +1195,26 @@ mod tests {
}
}

#[test]
fn test_load_program_from_bytes() {
fn load_test_program() -> Vec<u8> {
let mut dir = env::current_dir().unwrap();
dir.push("tests");
dir.push("example-programs");
dir.push("hello-solana");
dir.push("hello_solana_program.so");
let mut file = File::open(dir.clone()).expect("file not found");
let metadata = fs::metadata(dir).expect("Unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
file.read_exact(&mut buffer).expect("Buffer overflow");
buffer
}

#[test]
fn test_load_program_from_bytes() {
let buffer = load_test_program();

let mut metrics = LoadProgramMetrics::default();
let loader = bpf_loader_upgradeable::id();
let size = metadata.len() as usize;
let size = buffer.len();
let slot = 2;
let environment = ProgramRuntimeEnvironment::new(BuiltinProgram::new_mock());

Expand Down Expand Up @@ -1303,13 +1310,7 @@ mod tests {
);
assert_eq!(result, Arc::new(loaded_program));

let mut dir = env::current_dir().unwrap();
dir.push("tests");
dir.push("hello_solana_program.so");
let mut file = File::open(dir.clone()).expect("file not found");
let metadata = fs::metadata(dir).expect("Unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
file.read_exact(&mut buffer).expect("buffer overflow");
let buffer = load_test_program();
account_data.set_data(buffer);

mock_bank
Expand Down Expand Up @@ -1376,13 +1377,7 @@ mod tests {
);
assert_eq!(result, Arc::new(loaded_program));

let mut dir = env::current_dir().unwrap();
dir.push("tests");
dir.push("hello_solana_program.so");
let mut file = File::open(dir.clone()).expect("file not found");
let metadata = fs::metadata(dir).expect("Unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
file.read_exact(&mut buffer).expect("buffer overflow");
let mut buffer = load_test_program();
let mut header = bincode::serialize(&state).unwrap();
let mut complement = vec![
0;
Expand Down Expand Up @@ -1461,13 +1456,7 @@ mod tests {
vec![0; std::cmp::max(0, LoaderV4State::program_data_offset() - header.len())];
header.append(&mut complement);

let mut dir = env::current_dir().unwrap();
dir.push("tests");
dir.push("hello_solana_program.so");
let mut file = File::open(dir.clone()).expect("file not found");
let metadata = fs::metadata(dir).expect("Unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
file.read_exact(&mut buffer).expect("buffer overflow");
let mut buffer = load_test_program();
header.append(&mut buffer);

account_data.set_data(header);
Expand Down
12 changes: 12 additions & 0 deletions svm/tests/example-programs/hello-solana/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "hello-solana-program"
version = "2.0.0"
edition = "2021"

[dependencies]
solana-program = { path = "../../../../sdk/program", version = "=2.0.0" }

[lib]
crate-type = ["cdylib", "rlib"]

[workspace]
File renamed without changes.
15 changes: 15 additions & 0 deletions svm/tests/example-programs/hello-solana/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use solana_program::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey,
};

entrypoint!(process_instruction);

fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
msg!("Hello, Solana!");

Ok(())
}
4 changes: 2 additions & 2 deletions svm/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ fn prepare_transactions(
// Loading the program file
let mut dir = env::current_dir().unwrap();
dir.push("tests");
// File compiled from
// https://github.com/solana-developers/program-examples/blob/feb82f254a4633ce2107d06060f2d0558dc987f5/basics/hello-solana/native/program/src/lib.rs
dir.push("example-programs");
dir.push("hello-solana");
dir.push("hello_solana_program.so");
let mut file = File::open(dir.clone()).expect("file not found");
let metadata = fs::metadata(dir).expect("Unable to read metadata");
Expand Down

0 comments on commit 4d838d5

Please sign in to comment.