This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
token-2022: Add Reallocate instruction #2864
Merged
CriesofCarrots
merged 6 commits into
solana-labs:master
from
CriesofCarrots:realloc-token
Feb 4, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ed63010
Pass by ref
0742930
Dedupe ExtensionTypes in length apis
78f2937
Depend on deduplication in ExtensionType::get_account_len
3a3c182
Add Reallocate instruction
a0f2321
Remove unneeded api
e5a3f77
Add set_account_type helper and remove unneeded StateWithExtensionsMu…
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
Large diffs are not rendered by default.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| use { | ||
| crate::{ | ||
| error::TokenError, | ||
| extension::{set_account_type, AccountType, ExtensionType, StateWithExtensions}, | ||
| processor::Processor, | ||
| state::Account, | ||
| }, | ||
| solana_program::{ | ||
| account_info::{next_account_info, AccountInfo}, | ||
| entrypoint::ProgramResult, | ||
| msg, | ||
| program::invoke, | ||
| pubkey::Pubkey, | ||
| system_instruction, | ||
| sysvar::{rent::Rent, Sysvar}, | ||
| }, | ||
| }; | ||
|
|
||
| /// Processes a [Reallocate](enum.TokenInstruction.html) instruction | ||
| pub fn process_reallocate( | ||
| program_id: &Pubkey, | ||
| accounts: &[AccountInfo], | ||
| new_extension_types: Vec<ExtensionType>, | ||
| ) -> ProgramResult { | ||
| let account_info_iter = &mut accounts.iter(); | ||
| let token_account_info = next_account_info(account_info_iter)?; | ||
| let payer_info = next_account_info(account_info_iter)?; | ||
| let system_program_info = next_account_info(account_info_iter)?; | ||
| let authority_info = next_account_info(account_info_iter)?; | ||
| let authority_info_data_len = authority_info.data_len(); | ||
|
|
||
| // check that account is the right type and validate owner | ||
| let mut current_extension_types = { | ||
| let token_account = token_account_info.data.borrow(); | ||
| let account = StateWithExtensions::<Account>::unpack(&token_account)?; | ||
|
CriesofCarrots marked this conversation as resolved.
|
||
| Processor::validate_owner( | ||
| program_id, | ||
| &account.base.owner, | ||
| authority_info, | ||
| authority_info_data_len, | ||
| account_info_iter.as_slice(), | ||
| )?; | ||
| account.get_extension_types()? | ||
| }; | ||
|
|
||
| // check that all desired extensions are for the right account type | ||
| if new_extension_types | ||
| .iter() | ||
| .any(|extension_type| extension_type.get_account_type() != AccountType::Account) | ||
| { | ||
| return Err(TokenError::InvalidState.into()); | ||
| } | ||
| // ExtensionType::get_account_len() dedupes types, so just a dumb concatenation is fine here | ||
| current_extension_types.extend_from_slice(&new_extension_types); | ||
| let needed_account_len = ExtensionType::get_account_len::<Account>(¤t_extension_types); | ||
|
|
||
| // if account is already large enough, return early | ||
| if token_account_info.data_len() >= needed_account_len { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| // reallocate | ||
| msg!( | ||
| "account needs realloc, +{:?} bytes", | ||
| needed_account_len - token_account_info.data_len() | ||
| ); | ||
| token_account_info.realloc(needed_account_len, false)?; | ||
|
|
||
| // if additional lamports needed to remain rent-exempt, transfer them | ||
| let rent = Rent::get()?; | ||
| let new_minimum_balance = rent.minimum_balance(needed_account_len); | ||
| let lamports_diff = new_minimum_balance.saturating_sub(token_account_info.lamports()); | ||
| invoke( | ||
|
CriesofCarrots marked this conversation as resolved.
|
||
| &system_instruction::transfer(payer_info.key, token_account_info.key, lamports_diff), | ||
| &[ | ||
| payer_info.clone(), | ||
| token_account_info.clone(), | ||
| system_program_info.clone(), | ||
| ], | ||
| )?; | ||
|
|
||
| // unpack to set account_type, if needed | ||
| let mut token_account = token_account_info.data.borrow_mut(); | ||
| set_account_type::<Account>(&mut token_account)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.