Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Feb 26, 2022
1 parent 2786320 commit 07ce3de
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16523,6 +16523,19 @@ pub(crate) mod tests {
)
.unwrap();

// Dummy message to determine fee amount
let dummy_message = SanitizedMessage::try_from(Message::new_with_blockhash(
&[system_instruction::transfer(
&rent_exempt_fee_payer.pubkey(),
&recipient,
sol_to_lamports(1.),
)],
Some(&rent_exempt_fee_payer.pubkey()),
&recent_blockhash,
))
.unwrap();
let fee = bank.get_fee_for_message(&dummy_message).unwrap();

// RentPaying fee-payer can remain RentPaying
let tx = Transaction::new(
&[&rent_paying_fee_payer, &mint_keypair],
Expand Down Expand Up @@ -16570,6 +16583,23 @@ pub(crate) mod tests {
&rent_paying_fee_payer.pubkey()
));

// RentPaying fee-payer can be emptied with fee and transaction
let tx = Transaction::new(
&[&rent_paying_fee_payer],
Message::new(
&[system_instruction::transfer(
&rent_paying_fee_payer.pubkey(),
&recipient,
bank.get_balance(&rent_paying_fee_payer.pubkey()) - fee,
)],
Some(&rent_paying_fee_payer.pubkey()),
),
recent_blockhash,
);
let result = bank.process_transaction(&tx);
assert!(result.is_ok());
assert_eq!(0, bank.get_balance(&rent_paying_fee_payer.pubkey()));

// RentExempt fee-payer cannot become RentPaying via rent
let tx = Transaction::new(
&[&rent_exempt_fee_payer, &mint_keypair],
Expand Down Expand Up @@ -16613,6 +16643,86 @@ pub(crate) mod tests {
assert!(check_account_is_rent_exempt(
&rent_exempt_fee_payer.pubkey()
));

// For good measure, show that a RentExempt fee-payer that is also debited by a transaction
// cannot become RentPaying by that debit, but can still be charged for the fee
bank.transfer(fee, &mint_keypair, &rent_exempt_fee_payer.pubkey())
.unwrap();
let fee_payer_balance = bank.get_balance(&rent_exempt_fee_payer.pubkey());
assert_eq!(fee_payer_balance, rent_exempt_minimum + fee);
let tx = Transaction::new(
&[&rent_exempt_fee_payer],
Message::new(
&[system_instruction::transfer(
&rent_exempt_fee_payer.pubkey(),
&recipient,
fee,
)],
Some(&rent_exempt_fee_payer.pubkey()),
),
recent_blockhash,
);
let result = bank.process_transaction(&tx);
assert_eq!(
result.unwrap_err(),
TransactionError::InvalidRentPayingAccount
);
assert_eq!(
fee_payer_balance - fee,
bank.get_balance(&rent_exempt_fee_payer.pubkey())
);
assert!(check_account_is_rent_exempt(
&rent_exempt_fee_payer.pubkey()
));

// Also show that a RentExempt fee-payer can be completely emptied via fee and transaction
bank.transfer(fee + 1, &mint_keypair, &rent_exempt_fee_payer.pubkey())
.unwrap();
assert!(bank.get_balance(&rent_exempt_fee_payer.pubkey()) > rent_exempt_minimum + fee);
let tx = Transaction::new(
&[&rent_exempt_fee_payer],
Message::new(
&[system_instruction::transfer(
&rent_exempt_fee_payer.pubkey(),
&recipient,
bank.get_balance(&rent_exempt_fee_payer.pubkey()) - fee,
)],
Some(&rent_exempt_fee_payer.pubkey()),
),
recent_blockhash,
);
let result = bank.process_transaction(&tx);
assert!(result.is_ok());
assert_eq!(0, bank.get_balance(&rent_exempt_fee_payer.pubkey()));

// ... but not if the fee alone would make it RentPaying
bank.transfer(
rent_exempt_minimum + 1,
&mint_keypair,
&rent_exempt_fee_payer.pubkey(),
)
.unwrap();
assert!(bank.get_balance(&rent_exempt_fee_payer.pubkey()) < rent_exempt_minimum + fee);
let tx = Transaction::new(
&[&rent_exempt_fee_payer],
Message::new(
&[system_instruction::transfer(
&rent_exempt_fee_payer.pubkey(),
&recipient,
bank.get_balance(&rent_exempt_fee_payer.pubkey()) - fee,
)],
Some(&rent_exempt_fee_payer.pubkey()),
),
recent_blockhash,
);
let result = bank.process_transaction(&tx);
assert_eq!(
result.unwrap_err(),
TransactionError::InvalidRentPayingAccount
);
assert!(check_account_is_rent_exempt(
&rent_exempt_fee_payer.pubkey()
));
}

#[test]
Expand Down

0 comments on commit 07ce3de

Please sign in to comment.