-
Notifications
You must be signed in to change notification settings - Fork 9
fix: prevent undelegation of closed delegated accounts #128
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
Closed
JkrishnaD
wants to merge
2
commits into
magicblock-labs:main
from
JkrishnaD:undelegate-close-accounts
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| use dlp::pda::{ | ||
| delegation_metadata_pda_from_delegated_account, delegation_record_pda_from_delegated_account, | ||
| fees_vault_pda, validator_fees_vault_pda_from_validator, | ||
| }; | ||
| use solana_program::{hash::Hash, native_token::LAMPORTS_PER_SOL, rent::Rent, system_program}; | ||
| use solana_program_test::{read_file, BanksClient, ProgramTest}; | ||
| use solana_sdk::{ | ||
| account::Account, | ||
| signature::{Keypair, Signer}, | ||
| transaction::Transaction, | ||
| }; | ||
|
|
||
| use crate::fixtures::{ | ||
| get_delegation_metadata_data, get_delegation_record_data, DELEGATED_PDA_ID, | ||
| DELEGATED_PDA_OWNER_ID, TEST_AUTHORITY, | ||
| }; | ||
|
|
||
| mod fixtures; | ||
|
|
||
| #[tokio::test] | ||
| async fn test_undelegate_close_account() { | ||
| // Setup | ||
| let (banks, _, authority, blockhash) = setup_program_test_env().await; | ||
|
|
||
| // Create the undelegate tx | ||
| let ix_undelegate = dlp::instruction_builder::undelegate( | ||
| authority.pubkey(), | ||
| DELEGATED_PDA_ID, | ||
| DELEGATED_PDA_OWNER_ID, | ||
| authority.pubkey(), | ||
| ); | ||
|
|
||
| // Submit the transaction | ||
| let tx = Transaction::new_signed_with_payer( | ||
| &[ix_undelegate], | ||
| Some(&authority.pubkey()), | ||
| &[&authority], | ||
| blockhash, | ||
| ); | ||
| let res = banks.process_transaction(tx).await; | ||
| assert!(res.is_err(), "Delegate account is already closed"); | ||
| } | ||
|
|
||
| async fn setup_program_test_env() -> (BanksClient, Keypair, Keypair, Hash) { | ||
| let mut program_test = ProgramTest::new("dlp", dlp::ID, None); | ||
| program_test.prefer_bpf(true); | ||
| let authority = Keypair::from_bytes(&TEST_AUTHORITY).unwrap(); | ||
|
|
||
| program_test.add_account( | ||
| authority.pubkey(), | ||
| Account { | ||
| lamports: LAMPORTS_PER_SOL, | ||
| data: vec![], | ||
| owner: system_program::id(), | ||
| executable: false, | ||
| rent_epoch: 0, | ||
| }, | ||
| ); | ||
|
|
||
| // Setup a Closed delegated PDA | ||
| program_test.add_account( | ||
| DELEGATED_PDA_ID, | ||
| Account { | ||
| lamports: 0, | ||
| data: vec![], | ||
| owner: dlp::id(), | ||
| executable: false, | ||
| rent_epoch: 0, | ||
| }, | ||
| ); | ||
|
|
||
| // Setup the delegated record PDA | ||
| let delegation_record_data = get_delegation_record_data(authority.pubkey(), None); | ||
| program_test.add_account( | ||
| delegation_record_pda_from_delegated_account(&DELEGATED_PDA_ID), | ||
| Account { | ||
| lamports: Rent::default().minimum_balance(delegation_record_data.len()), | ||
| data: delegation_record_data, | ||
| owner: dlp::id(), | ||
| executable: false, | ||
| rent_epoch: 0, | ||
| }, | ||
| ); | ||
|
|
||
| // Setup the delegated metadata PDA | ||
| let delegation_metadata_data = get_delegation_metadata_data(authority.pubkey(), Some(true)); | ||
| program_test.add_account( | ||
| delegation_metadata_pda_from_delegated_account(&DELEGATED_PDA_ID), | ||
| Account { | ||
| lamports: Rent::default().minimum_balance(delegation_metadata_data.len()), | ||
| data: delegation_metadata_data, | ||
| owner: dlp::id(), | ||
| executable: false, | ||
| rent_epoch: 0, | ||
| }, | ||
| ); | ||
|
|
||
| // Setup program to test undelegation | ||
| let data = read_file("tests/buffers/test_delegation.so"); | ||
| program_test.add_account( | ||
| DELEGATED_PDA_OWNER_ID, | ||
| Account { | ||
| lamports: Rent::default().minimum_balance(data.len()), | ||
| data, | ||
| owner: solana_sdk::bpf_loader::id(), | ||
| executable: true, | ||
| rent_epoch: 0, | ||
| }, | ||
| ); | ||
|
|
||
| // Setup the protocol fees vault | ||
| program_test.add_account( | ||
| fees_vault_pda(), | ||
| Account { | ||
| lamports: Rent::default().minimum_balance(0), | ||
| data: vec![], | ||
| owner: dlp::id(), | ||
| executable: false, | ||
| rent_epoch: 0, | ||
| }, | ||
| ); | ||
|
|
||
| // Setup the validator fees vault | ||
| program_test.add_account( | ||
| validator_fees_vault_pda_from_validator(&authority.pubkey()), | ||
| Account { | ||
| lamports: LAMPORTS_PER_SOL, | ||
| data: vec![], | ||
| owner: dlp::id(), | ||
| executable: false, | ||
| rent_epoch: 0, | ||
| }, | ||
| ); | ||
|
|
||
| let (banks, payer, blockhash) = program_test.start().await; | ||
| (banks, payer, authority, blockhash) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.