From ea6b7dd96bc1f30b87c1a41cc08281f11615d66c Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 16 Aug 2023 21:23:06 -0600 Subject: [PATCH] remove non-data swap --- runtime/src/bank.rs | 18 +++--------------- runtime/src/bank/tests.rs | 36 +++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 568653c491dabc..c89b4c2e99b074 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -8191,22 +8191,10 @@ impl Bank { self.capitalization .fetch_sub(new_account.lamports(), Relaxed); self.store_account(new_address, &AccountSharedData::default()); - } else if let Some(old_data_account) = - self.get_account_with_fixed_root(&old_data_address) + } else if self + .get_account_with_fixed_root(&old_data_address) + .is_none() { - // A data account exists for the old program, but not the - // new program - // Swap program accounts and delete the old data account - self.replace_account( - old_address, - new_address, - Some(&old_account), - &new_account, - ); - self.capitalization - .fetch_sub(old_data_account.lamports(), Relaxed); - self.store_account(&old_data_address, &AccountSharedData::default()); - } else { // A data account does not exist for the new program // Swap program accounts only self.replace_account( diff --git a/runtime/src/bank/tests.rs b/runtime/src/bank/tests.rs index 9ec80c46054beb..4ad4355960101e 100644 --- a/runtime/src/bank/tests.rs +++ b/runtime/src/bank/tests.rs @@ -8173,15 +8173,17 @@ fn test_upgradable_program_replace_with_no_data() { // - OldData: [Old program data] // - New: [*New program data] // - // Should replace the program account and delete the data account: - // - Old: [*New program data] + // This is NOT allowed and should leave the program unchanged: + // - Old: PDA(OldData) + // - OldData: [Old program data] let bpf_id = bpf_loader_upgradeable::id(); let mut bank = create_simple_test_bank(0); let old = Pubkey::new_unique(); let (old_data, _) = Pubkey::find_program_address(&[old.as_ref()], &bpf_id); + let old_program_bytes = vec![0, 1, 2, 3, 4, 5, 6]; set_up_account_with_bank(&mut bank, &old, 100, old_data.to_bytes().to_vec()); - set_up_account_with_bank(&mut bank, &old_data, 102, vec![0, 1, 2, 3, 4, 5, 6]); + set_up_account_with_bank(&mut bank, &old_data, 102, old_program_bytes.clone()); let new = Pubkey::new_unique(); let new_program_bytes = vec![6, 5, 4, 3, 2, 1, 0]; @@ -8191,22 +8193,26 @@ fn test_upgradable_program_replace_with_no_data() { bank.replace_program_account(&old, &new, "bank-apply_program_replacement"); - // Old program account balance is unchanged + // All balances are unchanged assert_eq!(bank.get_balance(&old), 100); + assert_eq!(bank.get_balance(&old_data), 102); + assert_eq!(bank.get_balance(&new), 100); - // Old data account is now empty - assert_eq!(bank.get_balance(&old_data), 0); - - // New program account is now empty - assert_eq!(bank.get_balance(&new), 0); - - // Old program account now holds the new program bytes - // - Old: [*New program data] + // Old program accounts' data are unchanged + // - Old: PDA(OldData) + // - OldData: [Old program data] let old_account = bank.get_account(&old).unwrap(); - assert_eq!(old_account.data(), &new_program_bytes,); + assert_eq!(old_account.data(), &old_data.to_bytes().to_vec()); + let old_data_account = bank.get_account(&old_data).unwrap(); + assert_eq!(old_data_account.data(), &old_program_bytes); - // Lamports in the old token accounts were burnt - assert_eq!(bank.capitalization(), original_capitalization - 100 - 102); + // New program account is unchanged + // - New: [*New program data] + let new_account = bank.get_account(&new).unwrap(); + assert_eq!(new_account.data(), &new_program_bytes); + + // Lamports were unchanged across the board + assert_eq!(bank.capitalization(), original_capitalization); } fn min_rent_exempt_balance_for_sysvars(bank: &Bank, sysvar_ids: &[Pubkey]) -> u64 {