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 extra program id checks to instruction helpers #220

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions dex/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::matching::{OrderType, Side};
use bytemuck::cast;
use serde::{Deserialize, Serialize};
use solana_program::{
declare_id,
entrypoint::ProgramResult,
instruction::{AccountMeta, Instruction},
program_error::ProgramError,
pubkey::Pubkey,
sysvar::rent,
};
Expand All @@ -19,6 +22,8 @@ use proptest::prelude::*;
#[cfg(test)]
use proptest_derive::Arbitrary;

declare_id!("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin");

pub mod srm_token {
use solana_program::declare_id;
declare_id!("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt");
Expand Down Expand Up @@ -603,6 +608,7 @@ pub fn initialize_market(
vault_signer_nonce: u64,
pc_dust_threshold: u64,
) -> Result<solana_program::instruction::Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::InitializeMarket(InitializeMarketInstruction {
coin_lot_size,
pc_lot_size,
Expand Down Expand Up @@ -685,6 +691,7 @@ pub fn new_order(
limit: u16,
max_native_pc_qty_including_fees: NonZeroU64,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::NewOrderV3(NewOrderInstructionV3 {
side,
limit_price,
Expand Down Expand Up @@ -731,6 +738,7 @@ pub fn match_orders(
pc_fee_receivable_account: &Pubkey,
limit: u16,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::MatchOrders(limit).pack();
let accounts: Vec<AccountMeta> = vec![
AccountMeta::new(*market, false),
Expand All @@ -757,6 +765,7 @@ pub fn consume_events(
pc_fee_receivable_account: &Pubkey,
limit: u16,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::ConsumeEvents(limit).pack();
let mut accounts: Vec<AccountMeta> = open_orders_accounts
.iter()
Expand All @@ -783,6 +792,7 @@ pub fn consume_events_permissioned(
consume_events_authority: &Pubkey,
limit: u16,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::ConsumeEventsPermissioned(limit).pack();
let mut accounts: Vec<AccountMeta> = open_orders_accounts
.iter()
Expand Down Expand Up @@ -811,6 +821,7 @@ pub fn cancel_order(
side: Side,
order_id: u128,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::CancelOrderV2(CancelOrderInstructionV2 { side, order_id }).pack();
let accounts: Vec<AccountMeta> = vec![
AccountMeta::new(*market, false),
Expand Down Expand Up @@ -840,6 +851,7 @@ pub fn settle_funds(
referrer_pc_wallet: Option<&Pubkey>,
vault_signer: &Pubkey,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::SettleFunds.pack();
let mut accounts: Vec<AccountMeta> = vec![
AccountMeta::new(*market, false),
Expand Down Expand Up @@ -872,6 +884,7 @@ pub fn cancel_order_by_client_order_id(
event_queue: &Pubkey,
client_order_id: u64,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::CancelOrderByClientIdV2(client_order_id).pack();
let accounts: Vec<AccountMeta> = vec![
AccountMeta::new(*market, false),
Expand All @@ -893,6 +906,7 @@ pub fn disable_market(
market: &Pubkey,
disable_authority_key: &Pubkey,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::DisableMarket.pack();
let accounts: Vec<AccountMeta> = vec![
AccountMeta::new(*market, false),
Expand All @@ -914,6 +928,7 @@ pub fn sweep_fees(
vault_signer: &Pubkey,
spl_token_program_id: &Pubkey,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::SweepFees.pack();
let accounts: Vec<AccountMeta> = vec![
AccountMeta::new(*market, false),
Expand All @@ -937,6 +952,7 @@ pub fn close_open_orders(
destination: &Pubkey,
market: &Pubkey,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::CloseOpenOrders.pack();
let accounts: Vec<AccountMeta> = vec![
AccountMeta::new(*open_orders, false),
Expand All @@ -958,6 +974,7 @@ pub fn init_open_orders(
market: &Pubkey,
market_authority: Option<&Pubkey>,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::InitOpenOrders.pack();
let mut accounts: Vec<AccountMeta> = vec![
AccountMeta::new(*open_orders, false),
Expand Down Expand Up @@ -986,6 +1003,7 @@ pub fn prune(
event_q: &Pubkey,
limit: u16,
) -> Result<Instruction, DexError> {
check_program_account(program_id)?;
let data = MarketInstruction::Prune(limit).pack();
let accounts: Vec<AccountMeta> = vec![
AccountMeta::new(*market, false),
Expand Down Expand Up @@ -1210,3 +1228,10 @@ mod fuzzing {
arbitrary_impl!(NewOrderInstructionV2, NewOrderInstructionU64);
arbitrary_impl!(NewOrderInstructionV1, NewOrderInstructionU64);
}

fn check_program_account(program_id: &Pubkey) -> ProgramResult {
if program_id != &ID {
return Err(ProgramError::IncorrectProgramId);
}
Ok(())
}
4 changes: 2 additions & 2 deletions dex/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn new_spl_token_program<'bump>(bump: &'bump Bump) -> AccountInfo<'bump> {
}

fn setup_market<'bump, R: Rng>(rng: &mut R, bump: &'bump Bump) -> MarketAccounts<'bump> {
let program_id = random_pubkey(rng, bump);
let program_id = &crate::instruction::ID;
let market = new_dex_owned_account(rng, size_of::<MarketState>(), program_id, bump);
let bids = new_dex_owned_account(rng, 1 << 23, program_id, bump);
let asks = new_dex_owned_account(rng, 1 << 23, program_id, bump);
Expand Down Expand Up @@ -260,7 +260,7 @@ fn test_new_order() {

let accounts = setup_market(&mut rng, &bump);

let dex_program_id = accounts.market.owner;
let dex_program_id = &crate::instruction::ID;

let owner = new_sol_account(&mut rng, 1_000_000_000, &bump);
let orders_account_buyer =
Expand Down