-
Notifications
You must be signed in to change notification settings - Fork 338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Maintain originated balance #1870
Conversation
87534c0
to
956fd6e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 6 of 6 files at r1.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @metacertain)
pkg/accounting/accounting.go, line 29 at r1 (raw file):
balancesPrefix string = "accounting_balance_" balancesSurplusPrefix string = "accounting_surplusbalance_" balancesOriginatedPrefix string = "accounting_originatedbalance_"
the word originated is enigmatic. can we use a descriptive term?
2021c8e
to
850464b
Compare
2a6ed76
to
e1f77a9
Compare
850464b
to
bd9bcaf
Compare
e1f77a9
to
b406e1e
Compare
bd9bcaf
to
01b4264
Compare
b406e1e
to
ff99f01
Compare
01b4264
to
1ae4d84
Compare
ff99f01
to
0943d59
Compare
1ae4d84
to
a383b72
Compare
we concluded with @ralph-pichler this is a descriptive term, as this is the balance representing originated traffic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 3 of 3 files at r2.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @metacertain)
0943d59
to
1e85784
Compare
a383b72
to
ea6a38e
Compare
1e85784
to
ae5f4c8
Compare
ae5f4c8
to
e18113d
Compare
ea6a38e
to
0f5c704
Compare
@metacertain still not really clear to me what |
updated pr description with explanation of changes |
e18113d
to
25ea02f
Compare
0f5c704
to
0a10f1d
Compare
25ea02f
to
aa06a82
Compare
0a10f1d
to
ebc208a
Compare
aa06a82
to
150d81f
Compare
ebc208a
to
09f9c73
Compare
150d81f
to
695324e
Compare
89f551e
to
fad6396
Compare
09f9c73
to
2d7d213
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 6 files at r1, 4 of 4 files at r3.
Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @metacertain)
pkg/accounting/accounting.go, line 27 at r3 (raw file):
var ( _ Interface = (*Accounting)(nil) balancesPrefix string = "accounting_balance_"
The string
type can be omitted (also for the vars below) and the related variables grouped.
pkg/accounting/accounting.go, line 275 at r3 (raw file):
// if originated {
This can be rewritten in early return which would make the code more readable:
if !originated {
return nil
}
...
pkg/accounting/accounting.go, line 277 at r3 (raw file):
if originated { originBalance, err := a.OriginatedBalance(peer) if err != nil {
No need for double if
, just if err != nil && !errors.Is(err, ErrPeerNoBalance)...
will do.
pkg/accounting/accounting.go, line 360 at r3 (raw file):
err = a.decreaseOriginatedBalanceTo(peer, oldBalance) if err != nil { a.logger.Warningf("settle: failed to decrease originated balance: %w", err)
The %w
wouldn't work in this context, switch to %v
pkg/accounting/accounting.go, line 468 at r3 (raw file):
func originatedBalanceKey(peer swarm.Address) string { return fmt.Sprintf("%s%s", balancesOriginatedPrefix, peer.String())
The String()
method call on the peer
is unnecessary since %s
formatting directive will do the same.
pkg/accounting/accounting.go, line 704 at r3 (raw file):
err = a.decreaseOriginatedBalanceBy(peer, amount) if err != nil { a.logger.Warningf("accounting: notifypaymentsent failed to decrease originated balance: %w", err)
As above, replace the %w
with %v
.
pkg/accounting/accounting.go, line 904 at r3 (raw file):
err = a.decreaseOriginatedBalanceTo(peer, nextBalance) if err != nil { a.logger.Warningf("increase balance: failed to decrease originated balance: %w", err)
As above, replace the %w
with %v
.
pkg/accounting/accounting.go, line 953 at r3 (raw file):
func (a *Accounting) decreaseOriginatedBalanceTo(peer swarm.Address, limit *big.Int) error { zero := big.NewInt(0)
This is unnecessary since you've already declared the zero
value above.
pkg/accounting/accounting.go, line 958 at r3 (raw file):
originatedBalance, err := a.OriginatedBalance(peer) if err != nil {
As above, this can be simplified into just one if
statement.
pkg/accounting/accounting.go, line 984 at r3 (raw file):
originatedBalance, err := a.OriginatedBalance(peer) if err != nil {
As above, this can be simplified into just one if
statement.
pkg/accounting/metrics.go, line 43 at r3 (raw file):
Help: "Amount of BZZ credited to peers (potential cost of the node)", }), TotalOriginatedCreditedAmount: prometheus.NewCounter(prometheus.CounterOpts{
This can follow the position defined in the struct and so be moved to the penultimate place.
pkg/accounting/metrics.go, line 61 at r3 (raw file):
Help: "Number of occurrences of BZZ credit events towards peers", }), OriginatedCreditEventsCount: prometheus.NewCounter(prometheus.CounterOpts{
This can follow the position defined in the struct and so be moved to the last place.
fad6396
to
f934c85
Compare
2d7d213
to
fe4ef3f
Compare
attended all comments as suggested, except
this lead to unexplained symptoms in the past, wouldn't try it at the moment
we removed global zero as a risk factor, it could be changed by functions receiving it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 2 files at r4.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @metacertain)
pkg/accounting/accounting.go
Outdated
@@ -42,7 +43,7 @@ type Interface interface { | |||
// Release releases the reserved funds. | |||
Release(peer swarm.Address, price uint64) | |||
// Credit increases the balance the peer has with us (we "pay" the peer). | |||
Credit(peer swarm.Address, price uint64) error | |||
Credit(peer swarm.Address, price uint64, orignated bool) error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
fdc98a1
to
a3e7050
Compare
The aim of this change is to introduce originated balances, that keep track of credit operations done for the purpose of requests originated by the node itself, and to limit monetary payments to the debt showed by the originated balance.
In accounting, the following changes implement this:
When credit is done, if the originated argument is true, we deduct the price from the originated balance
When settle (time based part) or debit decreases negative balance, originated balance is reduced towards zero along with the normal balance if the negative balance would become closer to zero.
When notifyPaymentSent decreases negative balance, the value of the settlement is added to the originated balance, even increasing it's value above zero (as the payment was solely intended to move the originated balance).
When sending monetary payments limiting amount to current outstanding originated debt
The PR also includes the use of originated parameter to credit in protocols
This change is