@@ -84,10 +84,8 @@ fn finish_create_account(
84
84
}
85
85
86
86
// 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 ) {
91
89
debug ! (
92
90
"CreateAccount: invalid argument; account {} already in use" ,
93
91
to. unsigned_key( )
@@ -116,7 +114,7 @@ fn finish_create_account(
116
114
return Err ( SystemError :: InvalidAccountDataLength . into ( ) ) ;
117
115
}
118
116
119
- // guard against sysvars being assigned
117
+ // guard against sysvars being made
120
118
if sysvar:: check_id ( & program_id) {
121
119
debug ! ( "Assign: program id {} invalid" , program_id) ;
122
120
return Err ( SystemError :: InvalidProgramId . into ( ) ) ;
@@ -141,7 +139,7 @@ fn assign_account_to_program(
141
139
return Err ( InstructionError :: MissingRequiredSignature ) ;
142
140
}
143
141
144
- // guard against sysvars being assigned
142
+ // guard against sysvars being made
145
143
if sysvar:: check_id ( & program_id) {
146
144
debug ! ( "Assign: program id {} invalid" , program_id) ;
147
145
return Err ( SystemError :: InvalidProgramId . into ( ) ) ;
@@ -515,6 +513,7 @@ mod tests {
515
513
516
514
#[ test]
517
515
fn test_create_already_in_use ( ) {
516
+ solana_logger:: setup ( ) ;
518
517
// Attempt to create system account in account already owned by another program
519
518
let new_program_owner = Pubkey :: new ( & [ 9 ; 32 ] ) ;
520
519
let from = Pubkey :: new_rand ( ) ;
@@ -538,7 +537,8 @@ mod tests {
538
537
assert_eq ! ( from_lamports, 100 ) ;
539
538
assert_eq ! ( owned_account, unchanged_account) ;
540
539
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 ( ) ) ;
542
542
let unchanged_account = owned_account. clone ( ) ;
543
543
let result = create_account (
544
544
& mut KeyedAccount :: new ( & from, true , & mut from_account) ,
@@ -551,6 +551,19 @@ mod tests {
551
551
let from_lamports = from_account. lamports ;
552
552
assert_eq ! ( from_lamports, 100 ) ;
553
553
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 ) ;
554
567
}
555
568
556
569
#[ test]
0 commit comments