Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit a6d083d

Browse files
authored
Remove create_account bandaid now that to's signature is required (#7776)
* Remove create account bandaid now that requires signature * shrink scope of this PR to bandaid
1 parent 91bae9d commit a6d083d

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

runtime/src/system_instruction_processor.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@ fn finish_create_account(
8484
}
8585

8686
// if it looks like the `to` account is already in use, bail
87-
if to.account.lamports != 0
88-
|| !to.account.data.is_empty()
89-
|| !system_program::check_id(&to.account.owner)
90-
{
87+
// (note that the id check is also enforced by message_processor)
88+
if !to.account.data.is_empty() || !system_program::check_id(&to.account.owner) {
9189
debug!(
9290
"CreateAccount: invalid argument; account {} already in use",
9391
to.unsigned_key()
@@ -116,7 +114,7 @@ fn finish_create_account(
116114
return Err(SystemError::InvalidAccountDataLength.into());
117115
}
118116

119-
// guard against sysvars being assigned
117+
// guard against sysvars being made
120118
if sysvar::check_id(&program_id) {
121119
debug!("Assign: program id {} invalid", program_id);
122120
return Err(SystemError::InvalidProgramId.into());
@@ -141,7 +139,7 @@ fn assign_account_to_program(
141139
return Err(InstructionError::MissingRequiredSignature);
142140
}
143141

144-
// guard against sysvars being assigned
142+
// guard against sysvars being made
145143
if sysvar::check_id(&program_id) {
146144
debug!("Assign: program id {} invalid", program_id);
147145
return Err(SystemError::InvalidProgramId.into());
@@ -515,6 +513,7 @@ mod tests {
515513

516514
#[test]
517515
fn test_create_already_in_use() {
516+
solana_logger::setup();
518517
// Attempt to create system account in account already owned by another program
519518
let new_program_owner = Pubkey::new(&[9; 32]);
520519
let from = Pubkey::new_rand();
@@ -538,7 +537,8 @@ mod tests {
538537
assert_eq!(from_lamports, 100);
539538
assert_eq!(owned_account, unchanged_account);
540539

541-
let mut owned_account = Account::new(10, 0, &Pubkey::default());
540+
// Attempt to create system account in account that already has data
541+
let mut owned_account = Account::new(0, 1, &Pubkey::default());
542542
let unchanged_account = owned_account.clone();
543543
let result = create_account(
544544
&mut KeyedAccount::new(&from, true, &mut from_account),
@@ -551,6 +551,19 @@ mod tests {
551551
let from_lamports = from_account.lamports;
552552
assert_eq!(from_lamports, 100);
553553
assert_eq!(owned_account, unchanged_account);
554+
555+
// Verify that create_account works even if `to` has a non-zero balance
556+
let mut owned_account = Account::new(1, 0, &Pubkey::default());
557+
let result = create_account(
558+
&mut KeyedAccount::new(&from, true, &mut from_account),
559+
&mut KeyedAccount::new(&owned_key, true, &mut owned_account),
560+
50,
561+
2,
562+
&new_program_owner,
563+
);
564+
assert_eq!(result, Ok(()));
565+
assert_eq!(from_account.lamports, from_lamports - 50);
566+
assert_eq!(owned_account.lamports, 1 + 50);
554567
}
555568

556569
#[test]

0 commit comments

Comments
 (0)