Skip to content

Commit

Permalink
Forbid direct invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
kstepanovdev committed Oct 10, 2024
1 parent 67f6cca commit d1ff3a2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions programs/rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ path = "tests/rewards/tests.rs"

[features]
no-entrypoint = []
testing = []
8 changes: 8 additions & 0 deletions programs/rewards/src/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
//! Program entrypoint

Check warning on line 1 in programs/rewards/src/entrypoint.rs

View workflow job for this annotation

GitHub Actions / Linter

Diff in /home/runner/work/aura-rewards/aura-rewards/programs/rewards/src/entrypoint.rs
use crate::{error::MplxRewardsError, instructions::process_instruction};
use solana_program::instruction::get_stack_height;
use solana_program::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult,
program_error::PrintProgramError, pubkey::Pubkey,
};

entrypoint!(program_entrypoint);

pub const TRANSACTION_LEVEL_STACK_HEIGHT: usize = 1;

fn program_entrypoint<'a>(
program_id: &Pubkey,
accounts: &'a [AccountInfo<'a>],
instruction_data: &[u8],
) -> ProgramResult {
if let Err(error) = process_instruction(program_id, accounts, instruction_data) {
#[cfg(not(feature = "testing"))]
if get_stack_height() == TRANSACTION_LEVEL_STACK_HEIGHT {
return Err(MplxRewardsError::ForbiddenInvocation.into());
}
// Catch the error so we can print it
error.print::<MplxRewardsError>();
return Err(error);
Expand Down
4 changes: 4 additions & 0 deletions programs/rewards/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ pub enum MplxRewardsError {
/// 19
#[error("Account addres derivation has failed")]
AccountDerivationAddresFailed,

/// 20
#[error("This contract is supposed to be called only from the staking contract")]
ForbiddenInvocation,
}

impl PrintProgramError for MplxRewardsError {
Expand Down

0 comments on commit d1ff3a2

Please sign in to comment.