Skip to content

Commit

Permalink
fix: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
metacertain committed Jun 11, 2021
1 parent 78898ef commit fe4ef3f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 55 deletions.
78 changes: 35 additions & 43 deletions pkg/accounting/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ import (

var (
_ Interface = (*Accounting)(nil)
balancesPrefix string = "accounting_balance_"
balancesSurplusPrefix string = "accounting_surplusbalance_"
balancesOriginatedPrefix string = "accounting_originatedbalance_"
balancesPrefix = "accounting_balance_"
balancesSurplusPrefix = "accounting_surplusbalance_"
balancesOriginatedPrefix = "accounting_originatedbalance_"
// 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 @@ -270,42 +269,39 @@ func (a *Accounting) Credit(peer swarm.Address, price uint64, originated bool) e
a.metrics.TotalCreditedAmount.Add(float64(price))
a.metrics.CreditEventsCount.Inc()

//

if originated {
originBalance, err := a.OriginatedBalance(peer)
if err != nil {
if !errors.Is(err, ErrPeerNoBalance) {
return fmt.Errorf("failed to load originated balance: %w", err)
}
}
if !originated {
return nil
}

// Calculate next balance by decreasing current balance with the price we credit
nextOriginBalance := new(big.Int).Sub(originBalance, new(big.Int).SetUint64(price))
originBalance, err := a.OriginatedBalance(peer)
if err != nil && !errors.Is(err, ErrPeerNoBalance) {
return fmt.Errorf("failed to load originated balance: %w", err)
}

a.logger.Tracef("crediting peer %v with price %d, new originated balance is %d", peer, price, nextOriginBalance)
// Calculate next balance by decreasing current balance with the price we credit
nextOriginBalance := new(big.Int).Sub(originBalance, new(big.Int).SetUint64(price))

// only consider negative balance for limiting originated balance
if nextBalance.Cmp(zero) > 0 {
nextBalance.Set(zero)
}
a.logger.Tracef("crediting peer %v with price %d, new originated balance is %d", peer, price, nextOriginBalance)

// 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)
}
zero := big.NewInt(0)
// only consider negative balance for limiting originated balance
if nextBalance.Cmp(zero) > 0 {
nextBalance.Set(zero)
}

err = a.store.Put(originatedBalanceKey(peer), nextOriginBalance)
if err != nil {
return fmt.Errorf("failed to persist originated balance: %w", err)
}
// 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)
}

a.metrics.TotalOriginatedCreditedAmount.Add(float64(price))
a.metrics.OriginatedCreditEventsCount.Inc()
err = a.store.Put(originatedBalanceKey(peer), nextOriginBalance)
if err != nil {
return fmt.Errorf("failed to persist originated balance: %w", err)
}

//
a.metrics.TotalOriginatedCreditedAmount.Add(float64(price))
a.metrics.OriginatedCreditEventsCount.Inc()

return nil
}
Expand Down Expand Up @@ -357,7 +353,7 @@ func (a *Accounting) settle(peer swarm.Address, balance *accountingPeer) error {

err = a.decreaseOriginatedBalanceTo(peer, oldBalance)
if err != nil {
a.logger.Warningf("settle: failed to decrease originated balance: %w", err)
a.logger.Warningf("settle: failed to decrease originated balance: %v", err)
}
}

Expand Down Expand Up @@ -701,7 +697,7 @@ func (a *Accounting) NotifyPaymentSent(peer swarm.Address, amount *big.Int, rece

err = a.decreaseOriginatedBalanceBy(peer, amount)
if err != nil {
a.logger.Warningf("accounting: notifypaymentsent failed to decrease originated balance: %w", err)
a.logger.Warningf("accounting: notifypaymentsent failed to decrease originated balance: %v", err)
}

}
Expand Down Expand Up @@ -901,7 +897,7 @@ func (a *Accounting) increaseBalance(peer swarm.Address, accountingPeer *account

err = a.decreaseOriginatedBalanceTo(peer, nextBalance)
if err != nil {
a.logger.Warningf("increase balance: failed to decrease originated balance: %w", err)
a.logger.Warningf("increase balance: failed to decrease originated balance: %v", err)
}

return nextBalance, nil
Expand Down Expand Up @@ -955,10 +951,8 @@ func (a *Accounting) decreaseOriginatedBalanceTo(peer swarm.Address, limit *big.
toSet := new(big.Int).Set(limit)

originatedBalance, err := a.OriginatedBalance(peer)
if err != nil {
if !errors.Is(err, ErrPeerNoBalance) {
return fmt.Errorf("failed to load originated balance: %w", err)
}
if err != nil && !errors.Is(err, ErrPeerNoBalance) {
return fmt.Errorf("failed to load originated balance: %w", err)
}

if toSet.Cmp(zero) > 0 {
Expand All @@ -981,10 +975,8 @@ func (a *Accounting) decreaseOriginatedBalanceTo(peer swarm.Address, limit *big.
func (a *Accounting) decreaseOriginatedBalanceBy(peer swarm.Address, amount *big.Int) error {

originatedBalance, err := a.OriginatedBalance(peer)
if err != nil {
if !errors.Is(err, ErrPeerNoBalance) {
return fmt.Errorf("failed to load balance: %w", err)
}
if err != nil && !errors.Is(err, ErrPeerNoBalance) {
return fmt.Errorf("failed to load balance: %w", err)
}

// Move originated balance into the positive domain by amount
Expand Down
24 changes: 12 additions & 12 deletions pkg/accounting/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ func newMetrics() metrics {
Name: "total_credited_amount",
Help: "Amount of BZZ credited to peers (potential cost of the node)",
}),
TotalOriginatedCreditedAmount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "total_originated_credited_amount",
Help: "Amount of BZZ credited to peers (potential cost of the node) for originated traffic",
}),
DebitEventsCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Expand All @@ -58,12 +52,6 @@ func newMetrics() metrics {
Name: "credit_events_count",
Help: "Number of occurrences of BZZ credit events towards peers",
}),
OriginatedCreditEventsCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "originated_credit_events_count",
Help: "Number of occurrences of BZZ credit events as originator towards peers",
}),
AccountingDisconnectsCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Expand All @@ -82,6 +70,18 @@ func newMetrics() metrics {
Name: "accounting_reserve_count",
Help: "Number of reserve calls",
}),
TotalOriginatedCreditedAmount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "total_originated_credited_amount",
Help: "Amount of BZZ credited to peers (potential cost of the node) for originated traffic",
}),
OriginatedCreditEventsCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "originated_credit_events_count",
Help: "Number of occurrences of BZZ credit events as originator towards peers",
}),
}
}

Expand Down

0 comments on commit fe4ef3f

Please sign in to comment.