Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience function to make it easier for programs to retrieve the result of GetMinimumDelegation #24175

Merged
merged 7 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions programs/bpf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/bpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ members = [
"rust/error_handling",
"rust/log_data",
"rust/external_spend",
"rust/get_minimum_delegation",
"rust/finalize",
"rust/instruction_introspection",
"rust/invoke",
Expand Down
1 change: 1 addition & 0 deletions programs/bpf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn main() {
"log_data",
"external_spend",
"finalize",
"get_minimum_delegation",
"instruction_introspection",
"invoke",
"invoke_and_error",
Expand Down
19 changes: 19 additions & 0 deletions programs/bpf/rust/get_minimum_delegation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "solana-bpf-rust-get-minimum-delegation"
version = "1.11.0"
description = "Solana BPF test program written in Rust"
authors = ["Solana Maintainers <[email protected]>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"
documentation = "https://docs.rs/solana-bpf-rust-get-minimum-delegation"
edition = "2021"

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

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

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
19 changes: 19 additions & 0 deletions programs/bpf/rust/get_minimum_delegation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Example/test program for calling GetMinimumDelegation and then the
//! helper function to return the minimum delegation value.

#![allow(unreachable_code)]

extern crate solana_program;
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey, stake};

solana_program::entrypoint!(process_instruction);
#[allow(clippy::unnecessary_wraps)]
fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
let minimum_delegation = stake::tools::get_minimum_delegation();
assert!(minimum_delegation.is_some());
Ok(())
}
30 changes: 30 additions & 0 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use {
pubkey::Pubkey,
rent::Rent,
signature::{keypair_from_seed, Keypair, Signer},
stake,
system_instruction::{self, MAX_PERMITTED_DATA_LENGTH},
system_program,
sysvar::{self, clock, rent},
Expand Down Expand Up @@ -3524,3 +3525,32 @@ fn test_program_fees() {
let post_balance = bank_client.get_balance(&mint_keypair.pubkey()).unwrap();
assert_eq!(pre_balance - post_balance, expected_min_fee);
}

#[test]
#[cfg(feature = "bpf_rust")]
fn test_get_minimum_delegation() {
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(100_123_456_789);
let mut bank = Bank::new_for_tests(&genesis_config);
bank.feature_set = Arc::new(FeatureSet::all_enabled());

let (name, id, entrypoint) = solana_bpf_loader_program!();
bank.add_builtin(&name, &id, entrypoint);
brooksprumo marked this conversation as resolved.
Show resolved Hide resolved
let bank = Arc::new(bank);
let bank_client = BankClient::new_shared(&bank);

let program_id = load_bpf_program(
&bank_client,
&bpf_loader::id(),
&mint_keypair,
"solana_bpf_rust_get_minimum_delegation",
);

let account_metas = vec![AccountMeta::new_readonly(stake::program::id(), false)];
let instruction = Instruction::new_with_bytes(program_id, &[], account_metas);
let result = bank_client.send_and_confirm_instruction(&mint_keypair, instruction);
assert!(result.is_ok());
}
7 changes: 6 additions & 1 deletion sdk/program/src/stake/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@ pub enum StakeInstruction {
/// None
///
/// The minimum delegation will be returned via the transaction context's returndata.
/// Use `get_return_data()` to retrieve the result.
/// Use [`get_return_data()`] to retrieve the result. Alternatively, use the
/// [`get_minimum_delegation_return_data()`] or [`get_minimum_delegation()`] helper functions.
///
/// [`get_return_data()`]: crate::program::get_return_data
/// [`get_minimum_delegation_return_data()`]: super::tools::get_minimum_delegation_return_data
/// [`get_minimum_delegation()`]: super::tools::get_minimum_delegation
brooksprumo marked this conversation as resolved.
Show resolved Hide resolved
GetMinimumDelegation,
}

Expand Down
1 change: 1 addition & 0 deletions sdk/program/src/stake/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod config;
pub mod instruction;
pub mod state;
pub mod tools;

pub mod program {
crate::declare_id!("Stake11111111111111111111111111111111111111");
Expand Down
29 changes: 29 additions & 0 deletions sdk/program/src/stake/tools.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Utility functions

/// Helper function for programs to call [`GetMinimumDelegation`] and then fetch the return data
///
/// This fn handles performing the CPI to call the [`GetMinimumDelegation`] function, and then
/// calls [`get_minimum_delegation_return_data()`] to fetch the return data.
///
/// [`GetMinimumDelegation`]: super::instruction::StakeInstruction::GetMinimumDelegation
pub fn get_minimum_delegation() -> Option<u64> {
brooksprumo marked this conversation as resolved.
Show resolved Hide resolved
let instruction = super::instruction::get_minimum_delegation();
crate::program::invoke(&instruction, &[]).ok()?;
brooksprumo marked this conversation as resolved.
Show resolved Hide resolved
get_minimum_delegation_return_data()
}

/// Helper function for programs to get the actual data after calling [`GetMinimumDelegation`]
///
/// This fn handles calling [`get_return_data()`], ensures the result is from the correct
/// program, and returns the correct type. Returns `None` otherwise.
///
/// [`GetMinimumDelegation`]: super::instruction::StakeInstruction::GetMinimumDelegation
/// [`get_return_data()`]: crate::program::get_return_data
pub fn get_minimum_delegation_return_data() -> Option<u64> {
brooksprumo marked this conversation as resolved.
Show resolved Hide resolved
crate::program::get_return_data()
.and_then(|(program_id, return_data)| {
(program_id == super::program::id()).then(|| return_data)
})
.and_then(|return_data| return_data.try_into().ok())
.map(u64::from_le_bytes)
}