Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,14 @@ impl Accounts {
}

fn store(&mut self, pubkey: &Pubkey, account: &Account) {
// purge if balance is 0 and no checkpoints
if account.tokens == 0 && self.checkpoints.is_empty() {
self.accounts.remove(pubkey);
if account.tokens == 0 {
if self.checkpoints.is_empty() {
// purge if balance is 0 and no checkpoints
self.accounts.remove(pubkey);
} else {
// store default account if balance is 0 and there's a checkpoint
self.accounts.insert(pubkey.clone(), Account::default());
}
} else {
self.accounts.insert(pubkey.clone(), account.clone());
}
Expand Down Expand Up @@ -2210,6 +2215,37 @@ mod tests {
assert_eq!(bank.checkpoint_depth(), 0);
}

#[test]
fn test_bank_checkpoint_zero_balance() {
let alice = Mint::new(1_000);
let bank = Bank::new(&alice);
let bob = Keypair::new();
let charlie = Keypair::new();

// bob should have 500
bank.transfer(500, &alice.keypair(), bob.pubkey(), alice.last_id())
.unwrap();
assert_eq!(bank.get_balance(&bob.pubkey()), 500);
assert_eq!(bank.checkpoint_depth(), 0);

bank.checkpoint();
assert_eq!(bank.checkpoint_depth(), 1);

// charlie should have 500, alice should have 0
bank.transfer(500, &alice.keypair(), charlie.pubkey(), alice.last_id())
.unwrap();
assert_eq!(bank.get_balance(&charlie.pubkey()), 500);
assert_eq!(bank.get_balance(&alice.pubkey()), 0);

let account = bank.get_account(&alice.pubkey()).unwrap();
let default_account = Account::default();
assert_eq!(account.tokens, default_account.tokens);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these assertions should also apply to a brand new bank, too, right?

can you compare bank.get_account() and Default::default() directly, instead of comparing each field?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, assertions should apply to fresh bank accounts.
I tried comparing get_account() to default(). It's not implemented.

assert_eq!(account.userdata, default_account.userdata);
assert_eq!(account.owner, default_account.owner);
assert_eq!(account.executable, default_account.executable);
assert_eq!(account.loader, default_account.loader);
}

#[test]
fn test_bank_checkpoint_rollback() {
let alice = Mint::new(10_000);
Expand Down