mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
les: updateBalance fixed, added detailed info
This commit is contained in:
parent
9117084a44
commit
6614683246
2 changed files with 26 additions and 19 deletions
|
|
@ -225,8 +225,12 @@ loop:
|
||||||
|
|
||||||
// UpdateBalance updates the balance of a client (either overwrites it or adds to it).
|
// UpdateBalance updates the balance of a client (either overwrites it or adds to it).
|
||||||
// It also updates the balance meta info string.
|
// It also updates the balance meta info string.
|
||||||
func (api *PrivateLightServerAPI) UpdateBalance(id enode.ID, value int64, meta string) error {
|
func (api *PrivateLightServerAPI) UpdateBalance(id enode.ID, value int64, meta string) (map[string]uint64, error) {
|
||||||
return api.server.clientPool.updateBalance(id, value, meta)
|
oldBalance, newBalance, err := api.server.clientPool.updateBalance(id, value, meta)
|
||||||
|
m := make(map[string]uint64)
|
||||||
|
m["old"] = oldBalance
|
||||||
|
m["new"] = newBalance
|
||||||
|
return m, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetClientParams sets client parameters for all clients listed in the ids list
|
// SetClientParams sets client parameters for all clients listed in the ids list
|
||||||
|
|
|
||||||
|
|
@ -565,31 +565,20 @@ func (f *clientPool) getPosBalance(id enode.ID) posBalance {
|
||||||
|
|
||||||
// updateBalance updates the balance of a client (either overwrites it or adds to it).
|
// updateBalance updates the balance of a client (either overwrites it or adds to it).
|
||||||
// It also updates the balance meta info string.
|
// It also updates the balance meta info string.
|
||||||
func (f *clientPool) updateBalance(id enode.ID, amount int64, meta string) error {
|
func (f *clientPool) updateBalance(id enode.ID, amount int64, meta string) (uint64, uint64, error) {
|
||||||
f.lock.Lock()
|
f.lock.Lock()
|
||||||
defer f.lock.Unlock()
|
defer f.lock.Unlock()
|
||||||
|
|
||||||
pb := f.ndb.getOrNewPB(id)
|
pb := f.ndb.getOrNewPB(id)
|
||||||
|
var negBalance uint64
|
||||||
c := f.connectedMap[id]
|
c := f.connectedMap[id]
|
||||||
if c != nil {
|
if c != nil {
|
||||||
posBalance, negBalance := c.balanceTracker.getBalance(f.clock.Now())
|
pb.value, negBalance = c.balanceTracker.getBalance(f.clock.Now())
|
||||||
pb.value = posBalance
|
|
||||||
defer func() {
|
|
||||||
c.balanceTracker.setBalance(pb.value, negBalance)
|
|
||||||
if !c.priority && pb.value > 0 {
|
|
||||||
// The capacity should be adjusted based on the requirement,
|
|
||||||
// but we have no idea about the new capacity, need a second
|
|
||||||
// call to udpate it.
|
|
||||||
c.priority = true
|
|
||||||
f.priorityConnected += c.capacity
|
|
||||||
c.balanceTracker.addCallback(balanceCallbackZero, 0, func() { f.balanceExhausted(id) })
|
|
||||||
}
|
|
||||||
c.balanceMetaInfo = meta
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
oldBalance := pb.value
|
||||||
if amount > 0 {
|
if amount > 0 {
|
||||||
if amount > maxBalance || pb.value > maxBalance-uint64(amount) {
|
if amount > maxBalance || pb.value > maxBalance-uint64(amount) {
|
||||||
return errBalanceOverflow
|
return oldBalance, oldBalance, errBalanceOverflow
|
||||||
}
|
}
|
||||||
pb.value += uint64(amount)
|
pb.value += uint64(amount)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -601,7 +590,21 @@ func (f *clientPool) updateBalance(id enode.ID, amount int64, meta string) error
|
||||||
}
|
}
|
||||||
pb.meta = meta
|
pb.meta = meta
|
||||||
f.ndb.setPB(id, pb)
|
f.ndb.setPB(id, pb)
|
||||||
return nil
|
if c != nil {
|
||||||
|
c.balanceTracker.setBalance(pb.value, negBalance)
|
||||||
|
if !c.priority && pb.value > 0 {
|
||||||
|
// The capacity should be adjusted based on the requirement,
|
||||||
|
// but we have no idea about the new capacity, need a second
|
||||||
|
// call to udpate it.
|
||||||
|
c.priority = true
|
||||||
|
f.priorityConnected += c.capacity
|
||||||
|
c.balanceTracker.addCallback(balanceCallbackZero, 0, func() { f.balanceExhausted(id) })
|
||||||
|
}
|
||||||
|
// if balance is set to zero then reverting to non-priority status
|
||||||
|
// is handled by the balanceExhausted callback
|
||||||
|
c.balanceMetaInfo = meta
|
||||||
|
}
|
||||||
|
return oldBalance, pb.value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// posBalance represents a recently accessed positive balance entry
|
// posBalance represents a recently accessed positive balance entry
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue