Skip to content

Commit

Permalink
AKI-364: Do not store negative account balances
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Oct 28, 2019
1 parent 14e268c commit 970a33e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ public BigInteger getBalance(AionAddress address) {
* @param amount The amount.
*/
@Override
public void addBalance(AionAddress address, BigInteger amount) {
public void addBalance(AionAddress address, BigInteger amount)
{
if (!this.isLocalCall && getBalance(address).add(amount).signum() < 0) {
throw new IllegalArgumentException("This balance adjustment leads to a negative balance!");
}
this.repository.addBalance(address, amount);
}

Expand Down Expand Up @@ -376,6 +380,9 @@ public boolean accountBalanceIsAtLeast(AionAddress address, BigInteger balance)
@Override
public void deductEnergyCost(AionAddress address, BigInteger energyCost) {
if (!this.isLocalCall) {
if (getBalance(address).subtract(energyCost).signum() < 0) {
throw new IllegalArgumentException("This energy cost deduction leads to a negative balance!");
}
this.repository.addBalance(address, energyCost.negate());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ public BigInteger getBalance(AionAddress address) {

@Override
public void adjustBalance(AionAddress address, BigInteger delta) {
if (!this.isLocalCall && getBalance(address).add(delta).signum() < 0) {
throw new IllegalArgumentException("This balance adjustment leads to a negative balance!");
}
this.repositoryCache.addBalance(address, delta);
}

Expand All @@ -176,6 +179,9 @@ public void incrementNonce(AionAddress address) {
@Override
public void refundAccount(AionAddress address, BigInteger amount) {
if (!this.isLocalCall) {
if (getBalance(address).add(amount).signum() < 0) {
throw new IllegalArgumentException("This refund leads to a negative balance!");
}
this.repositoryCache.addBalance(address, amount);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ public BigInteger getBalance(AionAddress address) {

@Override
public void adjustBalance(AionAddress address, BigInteger delta) {
this.repositoryCache.addBalance(address, delta);
if (!this.isLocalCall && getBalance(address).add(delta).signum() < 0) {
throw new IllegalArgumentException("This balance adjustment leads to a negative balance!");
}
this.repositoryCache.addBalance(address, delta);
}

@Override
Expand All @@ -177,6 +180,9 @@ public void incrementNonce(AionAddress address) {
@Override
public void refundAccount(AionAddress address, BigInteger amount) {
if (!this.isLocalCall) {
if (getBalance(address).add(amount).signum() < 0) {
throw new IllegalArgumentException("This refund leads to a negative balance!");
}
this.repositoryCache.addBalance(address, amount);
}
}
Expand Down

0 comments on commit 970a33e

Please sign in to comment.