Skip to content

Commit

Permalink
remove non-data swap
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Aug 17, 2023
1 parent 70e265b commit ea6b7dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
18 changes: 3 additions & 15 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
36 changes: 21 additions & 15 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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 {
Expand Down

0 comments on commit ea6b7dd

Please sign in to comment.