spl-associated-token-account: Add feature for new program (#22648)

* spl-associated-token-account: Add feature for new program

* Address feedback
This commit is contained in:
Jon Cinque
2022-01-24 22:54:41 +01:00
committed by GitHub
parent 0562426661
commit fc21af4e6e
4 changed files with 58 additions and 45 deletions

View File

@ -49,7 +49,7 @@ use {
builtins::{self, ActivationType, Builtin, Builtins}, builtins::{self, ActivationType, Builtin, Builtins},
cost_tracker::CostTracker, cost_tracker::CostTracker,
epoch_stakes::{EpochStakes, NodeVoteAccounts}, epoch_stakes::{EpochStakes, NodeVoteAccounts},
inline_spl_token, inline_spl_associated_token_account, inline_spl_token,
message_processor::MessageProcessor, message_processor::MessageProcessor,
rent_collector::{CollectedInfo, RentCollector}, rent_collector::{CollectedInfo, RentCollector},
stake_weighted_timestamp::{ stake_weighted_timestamp::{
@ -6092,7 +6092,20 @@ impl Bank {
} }
if new_feature_activations.contains(&feature_set::spl_token_v3_3_0_release::id()) { if new_feature_activations.contains(&feature_set::spl_token_v3_3_0_release::id()) {
self.apply_spl_token_v3_3_0_release(); self.replace_program_account(
&inline_spl_token::id(),
&inline_spl_token::new_token_program::id(),
"bank-apply_spl_token_v3_3_0_release",
);
}
if new_feature_activations.contains(&feature_set::spl_associated_token_account_v1_0_4::id())
{
self.replace_program_account(
&inline_spl_associated_token_account::id(),
&inline_spl_associated_token_account::program_v1_0_4::id(),
"bank-apply_spl_associated_token_account_v1_4_0",
);
} }
if !debug_do_not_add_builtins { if !debug_do_not_add_builtins {
@ -6190,33 +6203,31 @@ impl Bank {
} }
} }
fn apply_spl_token_v3_3_0_release(&mut self) { fn replace_program_account(
if let Some(old_account) = self.get_account_with_fixed_root(&inline_spl_token::id()) { &mut self,
if let Some(new_account) = old_address: &Pubkey,
self.get_account_with_fixed_root(&inline_spl_token::new_token_program::id()) new_address: &Pubkey,
{ datapoint_name: &'static str,
datapoint_info!( ) {
"bank-apply_spl_token_v3_3_0_release", if let Some(old_account) = self.get_account_with_fixed_root(old_address) {
("slot", self.slot, i64), if let Some(new_account) = self.get_account_with_fixed_root(new_address) {
); datapoint_info!(datapoint_name, ("slot", self.slot, i64));
// Burn lamports in the old token account // Burn lamports in the old account
self.capitalization self.capitalization
.fetch_sub(old_account.lamports(), Relaxed); .fetch_sub(old_account.lamports(), Relaxed);
// Transfer new token account to old token account // Transfer new account to old account
self.store_account(&inline_spl_token::id(), &new_account); self.store_account(old_address, &new_account);
// Clear new token account // Clear new account
self.store_account( self.store_account(new_address, &AccountSharedData::default());
&inline_spl_token::new_token_program::id(),
&AccountSharedData::default(),
);
self.remove_executor(&inline_spl_token::id()); self.remove_executor(old_address);
} }
} }
} }
fn reconfigure_token2_native_mint(&mut self) { fn reconfigure_token2_native_mint(&mut self) {
let reconfigure_token2_native_mint = match self.cluster_type() { let reconfigure_token2_native_mint = match self.cluster_type() {
ClusterType::Development => true, ClusterType::Development => true,
@ -13173,49 +13184,39 @@ pub(crate) mod tests {
} }
#[test] #[test]
fn test_spl_token_replacement() { fn test_program_replacement() {
let (genesis_config, _mint_keypair) = create_genesis_config(0); let (genesis_config, _mint_keypair) = create_genesis_config(0);
let mut bank = Bank::new_for_tests(&genesis_config); let mut bank = Bank::new_for_tests(&genesis_config);
// Setup original token account // Setup original program account
let old_address = Pubkey::new_unique();
let new_address = Pubkey::new_unique();
bank.store_account_and_update_capitalization( bank.store_account_and_update_capitalization(
&inline_spl_token::id(), &old_address,
&AccountSharedData::from(Account { &AccountSharedData::from(Account {
lamports: 100, lamports: 100,
..Account::default() ..Account::default()
}), }),
); );
assert_eq!(bank.get_balance(&inline_spl_token::id()), 100); assert_eq!(bank.get_balance(&old_address), 100);
// Setup new token account // Setup new program account
let new_token_account = AccountSharedData::from(Account { let new_program_account = AccountSharedData::from(Account {
lamports: 123, lamports: 123,
..Account::default() ..Account::default()
}); });
bank.store_account_and_update_capitalization( bank.store_account_and_update_capitalization(&new_address, &new_program_account);
&inline_spl_token::new_token_program::id(), assert_eq!(bank.get_balance(&new_address), 123);
&new_token_account,
);
assert_eq!(
bank.get_balance(&inline_spl_token::new_token_program::id()),
123
);
let original_capitalization = bank.capitalization(); let original_capitalization = bank.capitalization();
bank.apply_spl_token_v3_3_0_release(); bank.replace_program_account(&old_address, &new_address, "bank-apply_program_replacement");
// New token account is now empty // New program account is now empty
assert_eq!( assert_eq!(bank.get_balance(&new_address), 0);
bank.get_balance(&inline_spl_token::new_token_program::id()),
0
);
// Old token account holds the new token account // Old program account holds the new program account
assert_eq!( assert_eq!(bank.get_account(&old_address), Some(new_program_account));
bank.get_account(&inline_spl_token::id()),
Some(new_token_account)
);
// Lamports in the old token account were burnt // Lamports in the old token account were burnt
assert_eq!(bank.capitalization(), original_capitalization - 100); assert_eq!(bank.capitalization(), original_capitalization - 100);

View File

@ -0,0 +1,6 @@
// Partial SPL Associated Token Account declarations inlined to avoid an external dependency on the spl-associated-token-account crate
solana_sdk::declare_id!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
pub(crate) mod program_v1_0_4 {
solana_sdk::declare_id!("nata4apBRD9S9256v3X8RxDQ7jmK7wLEsGNHhRNWFq3");
}

View File

@ -33,6 +33,7 @@ pub mod execute_cost_table;
pub mod genesis_utils; pub mod genesis_utils;
pub mod hardened_unpack; pub mod hardened_unpack;
pub mod in_mem_accounts_index; pub mod in_mem_accounts_index;
pub mod inline_spl_associated_token_account;
pub mod inline_spl_token; pub mod inline_spl_token;
pub mod loader_utils; pub mod loader_utils;
pub mod message_processor; pub mod message_processor;

View File

@ -303,6 +303,10 @@ pub mod vote_withdraw_authority_may_change_authorized_voter {
solana_sdk::declare_id!("AVZS3ZsN4gi6Rkx2QUibYuSJG3S6QHib7xCYhG6vGJxU"); solana_sdk::declare_id!("AVZS3ZsN4gi6Rkx2QUibYuSJG3S6QHib7xCYhG6vGJxU");
} }
pub mod spl_associated_token_account_v1_0_4 {
solana_sdk::declare_id!("FaTa4SpiaSNH44PGC4z8bnGVTkSRYaWvrBs3KTu8XQQq");
}
lazy_static! { lazy_static! {
/// Map of feature identifiers to user-visible description /// Map of feature identifiers to user-visible description
pub static ref FEATURE_NAMES: HashMap<Pubkey, &'static str> = [ pub static ref FEATURE_NAMES: HashMap<Pubkey, &'static str> = [
@ -373,6 +377,7 @@ lazy_static! {
(filter_votes_outside_slot_hashes::id(), "filter vote slots older than the slot hashes history"), (filter_votes_outside_slot_hashes::id(), "filter vote slots older than the slot hashes history"),
(update_syscall_base_costs::id(), "Update syscall base costs"), (update_syscall_base_costs::id(), "Update syscall base costs"),
(vote_withdraw_authority_may_change_authorized_voter::id(), "vote account withdraw authority may change the authorized voter #22521"), (vote_withdraw_authority_may_change_authorized_voter::id(), "vote account withdraw authority may change the authorized voter #22521"),
(spl_associated_token_account_v1_0_4::id(), "SPL Associated Token Account Program release version 1.0.4, tied to token 3.3.0 #22648"),
/*************** ADD NEW FEATURES HERE ***************/ /*************** ADD NEW FEATURES HERE ***************/
] ]
.iter() .iter()