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

perf: Refactoring the double check in start-flashloan instruction #260

Closed
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
61 changes: 34 additions & 27 deletions programs/marginfi/src/instructions/marginfi_account/flashloan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,32 @@ pub fn check_flashloan_can_start(
) -> MarginfiResult<()> {
// Note: FLASHLOAN_ENABLED_FLAG is now deprecated.
// Any non-disabled account can initiate a flash loan.
check!(
!marginfi_account.load()?.get_flag(DISABLED_FLAG),
MarginfiError::AccountDisabled
);

let marginfi_account_data = marginfi_account.load()?;
check_account_status(&marginfi_account_data)?;

let current_ix_idx: usize = instructions::load_current_index_checked(sysvar_ixs)?.into();

check!(current_ix_idx < end_fl_idx, MarginfiError::IllegalFlashloan);

// Check current ix is not a CPI
let current_ix = instructions::load_instruction_at_checked(current_ix_idx, sysvar_ixs)?;
check_current_instruction(sysvar_ixs, current_ix_idx)?;

// Will error if ix doesn't exist
check_end_flashloan_instruction(sysvar_ixs, end_fl_idx, marginfi_account.key())?;

Ok(())
}

fn check_account_status(marginfi_account: &MarginfiAccount) -> MarginfiResult<()> {
check!(!marginfi_account.get_flag(DISABLED_FLAG), MarginfiError::AccountDisabled);
check!(!marginfi_account.get_flag(IN_FLASHLOAN_FLAG), MarginfiError::IllegalFlashloan);
Ok(())
}

fn check_current_instruction(sysvar_ixs: &AccountInfo, current_ix_idx: usize) -> MarginfiResult<()> {
let current_ix = instructions::load_instruction_at_checked(
current_ix_idx,
sysvar_ixs
)?;

check!(
get_stack_height() == TRANSACTION_LEVEL_STACK_HEIGHT,
Expand All @@ -79,41 +94,33 @@ pub fn check_flashloan_can_start(
"Start flashloan ix should not be in CPI"
);

// Will error if ix doesn't exist
let unchecked_end_fl_ix = instructions::load_instruction_at_checked(end_fl_idx, sysvar_ixs)?;
Ok(())
}

fn check_end_flashloan_instruction(
sysvar_ixs: &AccountInfo,
end_fl_idx: usize,
marginfi_account_key: Pubkey
) -> MarginfiResult<()> {
let end_fl_ix = instructions::load_instruction_at_checked(end_fl_idx, sysvar_ixs)?;

check!(
unchecked_end_fl_ix.data[..8]
.eq(&crate::instruction::LendingAccountEndFlashloan::DISCRIMINATOR),
end_fl_ix.data[..8].eq(&crate::instruction::LendingAccountEndFlashloan::DISCRIMINATOR),
MarginfiError::IllegalFlashloan
);

check!(
unchecked_end_fl_ix.program_id.eq(&crate::id()),
end_fl_ix.program_id.eq(&crate::id()),
MarginfiError::IllegalFlashloan
);

let end_fl_ix = unchecked_end_fl_ix;

let end_fl_marginfi_account = end_fl_ix
.accounts
.get(END_FL_IX_MARGINFI_ACCOUNT_AI_IDX)
.ok_or(MarginfiError::IllegalFlashloan)?;

check!(
end_fl_marginfi_account.pubkey.eq(&marginfi_account.key()),
MarginfiError::IllegalFlashloan
);

let marginf_account = marginfi_account.load()?;

check!(
!marginf_account.get_flag(DISABLED_FLAG),
MarginfiError::AccountDisabled
);

check!(
!marginf_account.get_flag(IN_FLASHLOAN_FLAG),
end_fl_marginfi_account.pubkey.eq(&marginfi_account_key),
MarginfiError::IllegalFlashloan
);

Expand Down