Skip to content

Commit

Permalink
fix: limiting behaviour in originated credit
Browse files Browse the repository at this point in the history
  • Loading branch information
metacertain committed May 27, 2021
1 parent fe93196 commit 1ae4d84
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions pkg/accounting/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
// fraction of the refresh rate that is the minimum for monetary settlement
// this value is chosen so that tiny payments are prevented while still allowing small payments in environments with lower payment thresholds
minimumPaymentDivisor = int64(5)
zero = big.NewInt(0)
)

// Interface is the Accounting interface.
Expand Down Expand Up @@ -282,6 +283,17 @@ func (a *Accounting) Credit(peer swarm.Address, price uint64, originated bool) e

a.logger.Tracef("crediting peer %v with price %d, new originated balance is %d", peer, price, nextOriginBalance)

// only consider negative balance for limiting originated balance
if nextBalance.Cmp(zero) > 0 {
nextBalance.Set(zero)
}

// If originated balance is more into the negative domain, set it to balance
if nextOriginBalance.Cmp(nextBalance) < 0 {
nextOriginBalance.Set(nextBalance)
a.logger.Tracef("decreasing originated balance to peer %v to current balance %d", peer, nextOriginBalance)
}

err = a.store.Put(originatedBalanceKey(peer), nextOriginBalance)
if err != nil {
return fmt.Errorf("failed to persist originated balance: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/accounting/accounting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestAccountingAddOriginatedBalance(t *testing.T) {
// inconsequential debit because originated balance is in the positive domain
{peer: peer1Addr, price: 100, expectedBalance: 200, originatedBalance: 100},
// originated credit moving the originated balance back into the negative domain, should be limited to the expectedbalance
{peer: peer1Addr, price: -300, expectedBalance: -100, originatedBalance: -200, originatedCredit: true},
{peer: peer1Addr, price: -300, expectedBalance: -100, originatedBalance: -100, originatedCredit: true},
}

for i, booking := range bookings {
Expand Down

0 comments on commit 1ae4d84

Please sign in to comment.