Purge empty accounts

This commit is contained in:
Greg Fitzgerald
2018-07-02 17:23:06 -06:00
committed by Greg Fitzgerald
parent b4dc180592
commit d2bb4dc14a

View File

@@ -208,6 +208,8 @@ impl Bank {
/// funds and isn't a duplicate. /// funds and isn't a duplicate.
fn apply_debits(&self, tx: &Transaction) -> Result<()> { fn apply_debits(&self, tx: &Transaction) -> Result<()> {
let mut bals = self.balances.write().unwrap(); let mut bals = self.balances.write().unwrap();
let mut purge = false;
{
let option = bals.get_mut(&tx.from); let option = bals.get_mut(&tx.from);
if option.is_none() { if option.is_none() {
return Err(BankError::AccountNotFound(tx.from)); return Err(BankError::AccountNotFound(tx.from));
@@ -224,10 +226,18 @@ impl Bank {
if *bal < contract.tokens { if *bal < contract.tokens {
self.forget_signature_with_last_id(&tx.sig, &tx.last_id); self.forget_signature_with_last_id(&tx.sig, &tx.last_id);
return Err(BankError::InsufficientFunds(tx.from)); return Err(BankError::InsufficientFunds(tx.from));
} else if *bal == contract.tokens {
purge = true;
} else {
*bal -= contract.tokens;
}
};
}
if purge {
bals.remove(&tx.from);
} }
*bal -= contract.tokens;
};
Ok(()) Ok(())
} }
@@ -602,7 +612,7 @@ mod tests {
.unwrap(); .unwrap();
// Mint's balance will be zero because all funds are locked up. // Mint's balance will be zero because all funds are locked up.
assert_eq!(bank.get_balance(&mint.pubkey()), Some(0)); assert_eq!(bank.get_balance(&mint.pubkey()), None);
// tx count is 1, because debits were applied. // tx count is 1, because debits were applied.
assert_eq!(bank.transaction_count(), 1); assert_eq!(bank.transaction_count(), 1);
@@ -636,7 +646,7 @@ mod tests {
bank.transfer_on_date(1, &mint.keypair(), pubkey, dt, mint.last_id()) bank.transfer_on_date(1, &mint.keypair(), pubkey, dt, mint.last_id())
.unwrap(); .unwrap();
assert_eq!(bank.get_balance(&mint.pubkey()), Some(0)); assert_eq!(bank.get_balance(&mint.pubkey()), None);
assert_eq!(bank.get_balance(&pubkey), Some(1)); assert_eq!(bank.get_balance(&pubkey), Some(1));
} }
@@ -653,7 +663,7 @@ mod tests {
assert_eq!(bank.transaction_count(), 1); assert_eq!(bank.transaction_count(), 1);
// Mint's balance will be zero because all funds are locked up. // Mint's balance will be zero because all funds are locked up.
assert_eq!(bank.get_balance(&mint.pubkey()), Some(0)); assert_eq!(bank.get_balance(&mint.pubkey()), None);
// pubkey's balance will be None because the funds have not been // pubkey's balance will be None because the funds have not been
// sent. // sent.