Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianBor committed Apr 12, 2021
0 parents commit 4ddb860
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*-dump.txt
/*.so
/target/
21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "bpf-program-template"
version = "0.1.0"
edition = "2018"
license = "WTFPL"
publish = false

[dependencies]
solana-program = "1.6.2"

[features]
test-bpf = []

[dev-dependencies]
assert_matches = "1.4.0"
solana-program-test = "1.6.2"
solana-sdk = "1.6.2"
solana-validator = "1.6.2"

[lib]
crate-type = ["cdylib", "lib"]
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
### Environment Setup
1. Install Rust from https://rustup.rs/
2. Install Solana v1.6.2 or later from https://docs.solana.com/cli/install-solana-cli-tools#use-solanas-install-tool

### Build and test for program compiled natively
```
$ cargo build
$ cargo test
```

### Build and test the program compiled for BPF
```
$ cargo build-bpf
$ cargo test-bpf
```
2 changes: 2 additions & 0 deletions Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
59 changes: 59 additions & 0 deletions scripts/patch.crates-io.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash
#
# Patches the SPL crates for developing against a local solana monorepo
#

here="$(dirname "$0")"

solana_dir=$1
if [[ -z $solana_dir ]]; then
echo "Usage: $0 <path-to-solana-monorepo>"
exit 1
fi

workspace_crates=(
"$here"/../Cargo.toml
)

if [[ ! -r "$solana_dir"/scripts/read-cargo-variable.sh ]]; then
echo "$solana_dir is not a path to the solana monorepo"
exit 1
fi

set -e

solana_dir=$(cd "$solana_dir" && pwd)

source "$solana_dir"/scripts/read-cargo-variable.sh
solana_ver=$(readCargoVariable version "$solana_dir"/sdk/Cargo.toml)

echo "Patching in $solana_ver from $solana_dir"

if ! git diff --quiet && [[ -z $DIRTY_OK ]]; then
echo "Error: dirty tree"
exit 1
fi
export DIRTY_OK=1

for crate in "${workspace_crates[@]}"; do
if grep -q '\[patch.crates-io\]' "$crate"; then
echo "* $crate is already patched"
else
echo "* patched $crate"
cat >> "$crate" <<PATCH
[patch.crates-io]
solana-clap-utils = {path = "$solana_dir/clap-utils" }
solana-cli-config = {path = "$solana_dir/cli-config" }
solana-client = { path = "$solana_dir/client"}
solana-logger = { path = "$solana_dir/logger"}
solana-program = { path = "$solana_dir/sdk/program" }
solana-program-test = { path = "$solana_dir/program-test" }
solana-remote-wallet = { path = "$solana_dir/remote-wallet"}
solana-sdk = { path = "$solana_dir/sdk" }
solana-validator = { path = "$solana_dir/validator"}
PATCH
fi
done

"$here"/update-solana-dependencies.sh "$solana_ver"

47 changes: 47 additions & 0 deletions scripts/update-solana-dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
#
# Updates the solana version in all the SPL crates
#

here="$(dirname "$0")"

solana_ver=$1
if [[ -z $solana_ver ]]; then
echo "Usage: $0 <new-solana-version>"
exit 1
fi

if [[ $solana_ver =~ ^v ]]; then
# Drop `v` from v1.2.3...
solana_ver=${solana_ver:1}
fi

cd "$here"/..

echo "Updating Solana version to $solana_ver in $PWD"

if ! git diff --quiet && [[ -z $DIRTY_OK ]]; then
echo "Error: dirty tree"
exit 1
fi

declare tomls=()
while IFS='' read -r line; do tomls+=("$line"); done < <(find . -name Cargo.toml)

crates=(
solana-clap-utils
solana-cli-config
solana-client
solana-logger
solana-program
solana-program-test
solana-remote-wallet
solana-sdk
solana-validator
)

set -x
for crate in "${crates[@]}"; do
sed -i -e "s#\(${crate} = \"\).*\(\"\)#\1$solana_ver\2#g" "${tomls[@]}"
done

54 changes: 54 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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!(
"process_instruction: {}: {} accounts, data={:?}",
program_id,
accounts.len(),
instruction_data
);
Ok(())
}

#[cfg(test)]
mod test {
use {
super::*,
assert_matches::*,
solana_program::instruction::{AccountMeta, Instruction},
solana_program_test::*,
solana_sdk::{signature::Signer, transaction::Transaction},
};

#[tokio::test]
async fn test_transaction() {
let program_id = Pubkey::new_unique();

let (mut banks_client, payer, recent_blockhash) = ProgramTest::new(
"bpf_program_template",
program_id,
processor!(process_instruction),
)
.start()
.await;

let mut transaction = Transaction::new_with_payer(
&[Instruction {
program_id,
accounts: vec![AccountMeta::new(payer.pubkey(), false)],
data: vec![1, 2, 3],
}],
Some(&payer.pubkey()),
);
transaction.sign(&[&payer], recent_blockhash);

assert_matches!(banks_client.process_transaction(transaction).await, Ok(()));
}
}
33 changes: 33 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![cfg(feature = "test-bpf")]

use {
assert_matches::*,
solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
},
solana_sdk::{signature::Signer, transaction::Transaction},
solana_validator::test_validator::*,
};

#[test]
fn test_validator_transaction() {
let program_id = Pubkey::new_unique();

let (test_validator, payer) = TestValidatorGenesis::default()
.add_program("bpf_program_template", program_id)
.start();
let (rpc_client, recent_blockhash, _fee_calculator) = test_validator.rpc_client();

let mut transaction = Transaction::new_with_payer(
&[Instruction {
program_id,
accounts: vec![AccountMeta::new(payer.pubkey(), false)],
data: vec![1, 2, 3],
}],
Some(&payer.pubkey()),
);
transaction.sign(&[&payer], recent_blockhash);

assert_matches!(rpc_client.send_and_confirm_transaction(&transaction), Ok(_));
}

0 comments on commit 4ddb860

Please sign in to comment.