Repository navigation
perf(svm): reuse constant V5 transfer delegates #1573
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
Open
Reinis-FRP
wants to merge
4
commits into
reinis/svm-cu-benchmarks
Choose a base branch
from
reinis/svm-constant-transfer-delegates
base: reinis/svm-cu-benchmarks
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−70
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
76a871c
perf(svm): reuse constant V5 transfer delegates
Reinis-FRP 79e8980
refactor(svm): colocate V5 transfer helper and delegate
Reinis-FRP 236ec1a
test(svm): pin transfer delegate mappings and trim benchmark history
Reinis-FRP c9dc5a2
test(svm): refresh constant-delegate CU measurements after restack
Reinis-FRP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| //! Transfers authorized by the V5 adapter's fixed deposit and fill delegates. | ||
|
|
||
| use crate::{ | ||
| constants::{ | ||
| V5_DEPOSIT_DELEGATE, V5_DEPOSIT_DELEGATE_BUMP, V5_DEPOSIT_DELEGATE_SEED, V5_FILL_DELEGATE, | ||
| V5_FILL_DELEGATE_BUMP, V5_FILL_DELEGATE_SEED, | ||
| }, | ||
| error::SvmError, | ||
| }; | ||
| use anchor_lang::prelude::*; | ||
| use anchor_spl::token_interface::{transfer_checked, TransferChecked}; | ||
|
|
||
| /// Select a trusted address/seed/bump combination; callers cannot mix delegate components. | ||
| #[derive(Clone, Copy)] | ||
| pub(crate) enum V5TransferDelegate { | ||
| Deposit, | ||
| Fill, | ||
| } | ||
|
|
||
| impl V5TransferDelegate { | ||
| fn pda(self) -> (Pubkey, &'static [u8], u8) { | ||
| match self { | ||
| Self::Deposit => (V5_DEPOSIT_DELEGATE, V5_DEPOSIT_DELEGATE_SEED, V5_DEPOSIT_DELEGATE_BUMP), | ||
| Self::Fill => (V5_FILL_DELEGATE, V5_FILL_DELEGATE_SEED, V5_FILL_DELEGATE_BUMP), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn transfer_from<'info>( | ||
| accounts: TransferChecked<'info>, | ||
| token_program: AccountInfo<'info>, | ||
| amount: u64, | ||
| mint_decimals: u8, | ||
| delegate: V5TransferDelegate, | ||
| ) -> Result<()> { | ||
| let (delegate, delegate_seed, bump) = delegate.pda(); | ||
| if delegate != accounts.authority.key() { | ||
| return err!(SvmError::InvalidDelegatePda); | ||
| } | ||
|
|
||
| let bump_seed = [bump]; | ||
| let signer_seeds: &[&[u8]] = &[delegate_seed, &bump_seed]; | ||
| let signer_seeds = [signer_seeds]; | ||
|
|
||
| transfer_checked(CpiContext::new_with_signer(token_program, accounts, &signer_seeds), amount, mint_decimals) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn transfer_delegate_variants_select_their_expected_pdas() { | ||
| assert_eq!( | ||
| V5TransferDelegate::Deposit.pda(), | ||
| (V5_DEPOSIT_DELEGATE, V5_DEPOSIT_DELEGATE_SEED, V5_DEPOSIT_DELEGATE_BUMP) | ||
| ); | ||
| assert_eq!(V5TransferDelegate::Fill.pda(), (V5_FILL_DELEGATE, V5_FILL_DELEGATE_SEED, V5_FILL_DELEGATE_BUMP)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn wrong_transfer_authority_is_rejected_before_cpi() { | ||
| for delegate in [V5TransferDelegate::Deposit, V5TransferDelegate::Fill] { | ||
| // Include the other canonical delegate: a valid Spoke PDA is insufficient for the wrong operation. | ||
| for authority in [Pubkey::new_unique(), V5_DEPOSIT_DELEGATE, V5_FILL_DELEGATE] { | ||
| if authority == delegate.pda().0 { | ||
| continue; | ||
| } | ||
| let mut lamports = 0; | ||
| let owner = Pubkey::default(); | ||
| let info = AccountInfo::new(&authority, false, false, &mut lamports, &mut [], &owner, false, 0); | ||
| let accounts = TransferChecked { | ||
| from: info.clone(), | ||
| mint: info.clone(), | ||
| to: info.clone(), | ||
| authority: info.clone(), | ||
| }; | ||
| assert_eq!(transfer_from(accounts, info, 1, 6, delegate), Err(error!(SvmError::InvalidDelegatePda))); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about something like
Seems marginally cleaner