2020-09-14 23:44:20 +03:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/les/utils"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2021-04-06 21:42:50 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
2020-09-14 23:44:20 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/nodestate"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
posThreshold = 1000000 // minimum positive balance that is persisted in the database
|
|
|
|
negThreshold = 1000000 // minimum negative balance that is persisted in the database
|
|
|
|
persistExpirationRefresh = time.Minute * 5 // refresh period of the token expiration persistence
|
|
|
|
)
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
// balanceTracker tracks positive and negative balances for connected nodes.
|
|
|
|
// After clientField is set externally, a nodeBalance is created and previous
|
2020-09-14 23:44:20 +03:00
|
|
|
// balance values are loaded from the database. Both balances are exponentially expired
|
|
|
|
// values. Costs are deducted from the positive balance if present, otherwise added to
|
|
|
|
// the negative balance. If the capacity is non-zero then a time cost is applied
|
|
|
|
// continuously while individual request costs are applied immediately.
|
|
|
|
// The two balances are translated into a single priority value that also depends
|
|
|
|
// on the actual capacity.
|
2021-04-06 21:42:50 +03:00
|
|
|
type balanceTracker struct {
|
|
|
|
setup *serverSetup
|
|
|
|
clock mclock.Clock
|
|
|
|
lock sync.Mutex
|
|
|
|
ns *nodestate.NodeStateMachine
|
|
|
|
ndb *nodeDB
|
|
|
|
posExp, negExp utils.ValueExpirer
|
|
|
|
|
|
|
|
posExpTC, negExpTC uint64
|
|
|
|
defaultPosFactors, defaultNegFactors PriceFactors
|
2020-09-14 23:44:20 +03:00
|
|
|
|
|
|
|
active, inactive utils.ExpiredValue
|
|
|
|
balanceTimer *utils.UpdateTimer
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
// newBalanceTracker creates a new balanceTracker
|
|
|
|
func newBalanceTracker(ns *nodestate.NodeStateMachine, setup *serverSetup, db ethdb.KeyValueStore, clock mclock.Clock, posExp, negExp utils.ValueExpirer) *balanceTracker {
|
2020-09-14 23:44:20 +03:00
|
|
|
ndb := newNodeDB(db, clock)
|
2021-04-06 21:42:50 +03:00
|
|
|
bt := &balanceTracker{
|
|
|
|
ns: ns,
|
|
|
|
setup: setup,
|
|
|
|
ndb: ndb,
|
|
|
|
clock: clock,
|
|
|
|
posExp: posExp,
|
|
|
|
negExp: negExp,
|
|
|
|
balanceTimer: utils.NewUpdateTimer(clock, time.Second*10),
|
|
|
|
quit: make(chan struct{}),
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
2021-02-19 17:53:12 +03:00
|
|
|
posOffset, negOffset := bt.ndb.getExpiration()
|
|
|
|
posExp.SetLogOffset(clock.Now(), posOffset)
|
|
|
|
negExp.SetLogOffset(clock.Now(), negOffset)
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
// Load all persisted balance entries of priority nodes,
|
|
|
|
// calculate the total number of issued service tokens.
|
2020-09-14 23:44:20 +03:00
|
|
|
bt.ndb.forEachBalance(false, func(id enode.ID, balance utils.ExpiredValue) bool {
|
|
|
|
bt.inactive.AddExp(balance)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
ns.SubscribeField(bt.setup.capacityField, func(node *enode.Node, state nodestate.Flags, oldValue, newValue interface{}) {
|
|
|
|
n, _ := ns.GetField(node, bt.setup.balanceField).(*nodeBalance)
|
2020-09-14 23:44:20 +03:00
|
|
|
if n == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ov, _ := oldValue.(uint64)
|
|
|
|
nv, _ := newValue.(uint64)
|
|
|
|
if ov == 0 && nv != 0 {
|
|
|
|
n.activate()
|
|
|
|
}
|
|
|
|
if nv != 0 {
|
|
|
|
n.setCapacity(nv)
|
|
|
|
}
|
|
|
|
if ov != 0 && nv == 0 {
|
|
|
|
n.deactivate()
|
|
|
|
}
|
|
|
|
})
|
2021-04-06 21:42:50 +03:00
|
|
|
ns.SubscribeField(bt.setup.clientField, func(node *enode.Node, state nodestate.Flags, oldValue, newValue interface{}) {
|
|
|
|
type peer interface {
|
|
|
|
FreeClientId() string
|
|
|
|
}
|
2020-09-14 23:44:20 +03:00
|
|
|
if newValue != nil {
|
2021-04-06 21:42:50 +03:00
|
|
|
n := bt.newNodeBalance(node, newValue.(peer).FreeClientId(), true)
|
|
|
|
bt.lock.Lock()
|
|
|
|
n.SetPriceFactors(bt.defaultPosFactors, bt.defaultNegFactors)
|
|
|
|
bt.lock.Unlock()
|
|
|
|
ns.SetFieldSub(node, bt.setup.balanceField, n)
|
2020-09-14 23:44:20 +03:00
|
|
|
} else {
|
2021-04-06 21:42:50 +03:00
|
|
|
ns.SetStateSub(node, nodestate.Flags{}, bt.setup.priorityFlag, 0)
|
|
|
|
if b, _ := ns.GetField(node, bt.setup.balanceField).(*nodeBalance); b != nil {
|
2020-09-14 23:44:20 +03:00
|
|
|
b.deactivate()
|
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
ns.SetFieldSub(node, bt.setup.balanceField, nil)
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// The positive and negative balances of clients are stored in database
|
|
|
|
// and both of these decay exponentially over time. Delete them if the
|
|
|
|
// value is small enough.
|
|
|
|
bt.ndb.evictCallBack = bt.canDropBalance
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-clock.After(persistExpirationRefresh):
|
|
|
|
now := clock.Now()
|
|
|
|
bt.ndb.setExpiration(posExp.LogOffset(now), negExp.LogOffset(now))
|
|
|
|
case <-bt.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return bt
|
|
|
|
}
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
// Stop saves expiration offset and unsaved node balances and shuts balanceTracker down
|
|
|
|
func (bt *balanceTracker) stop() {
|
2020-09-14 23:44:20 +03:00
|
|
|
now := bt.clock.Now()
|
|
|
|
bt.ndb.setExpiration(bt.posExp.LogOffset(now), bt.negExp.LogOffset(now))
|
|
|
|
close(bt.quit)
|
|
|
|
bt.ns.ForEach(nodestate.Flags{}, nodestate.Flags{}, func(node *enode.Node, state nodestate.Flags) {
|
2021-04-06 21:42:50 +03:00
|
|
|
if n, ok := bt.ns.GetField(node, bt.setup.balanceField).(*nodeBalance); ok {
|
2020-09-14 23:44:20 +03:00
|
|
|
n.lock.Lock()
|
|
|
|
n.storeBalance(true, true)
|
|
|
|
n.lock.Unlock()
|
2021-04-06 21:42:50 +03:00
|
|
|
bt.ns.SetField(node, bt.setup.balanceField, nil)
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
bt.ndb.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalTokenAmount returns the current total amount of service tokens in existence
|
2021-04-06 21:42:50 +03:00
|
|
|
func (bt *balanceTracker) TotalTokenAmount() uint64 {
|
2020-09-14 23:44:20 +03:00
|
|
|
bt.lock.Lock()
|
|
|
|
defer bt.lock.Unlock()
|
|
|
|
|
|
|
|
bt.balanceTimer.Update(func(_ time.Duration) bool {
|
|
|
|
bt.active = utils.ExpiredValue{}
|
|
|
|
bt.ns.ForEach(nodestate.Flags{}, nodestate.Flags{}, func(node *enode.Node, state nodestate.Flags) {
|
2021-04-06 21:42:50 +03:00
|
|
|
if n, ok := bt.ns.GetField(node, bt.setup.balanceField).(*nodeBalance); ok && n.active {
|
2020-09-14 23:44:20 +03:00
|
|
|
pos, _ := n.GetRawBalance()
|
|
|
|
bt.active.AddExp(pos)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
total := bt.active
|
|
|
|
total.AddExp(bt.inactive)
|
|
|
|
return total.Value(bt.posExp.LogOffset(bt.clock.Now()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPosBalanceIDs lists node IDs with an associated positive balance
|
2021-04-06 21:42:50 +03:00
|
|
|
func (bt *balanceTracker) GetPosBalanceIDs(start, stop enode.ID, maxCount int) (result []enode.ID) {
|
2020-09-14 23:44:20 +03:00
|
|
|
return bt.ndb.getPosBalanceIDs(start, stop, maxCount)
|
|
|
|
}
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
// SetDefaultFactors sets the default price factors applied to subsequently connected clients
|
|
|
|
func (bt *balanceTracker) SetDefaultFactors(posFactors, negFactors PriceFactors) {
|
|
|
|
bt.lock.Lock()
|
|
|
|
bt.defaultPosFactors = posFactors
|
|
|
|
bt.defaultNegFactors = negFactors
|
|
|
|
bt.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-09-14 23:44:20 +03:00
|
|
|
// SetExpirationTCs sets positive and negative token expiration time constants.
|
|
|
|
// Specified in seconds, 0 means infinite (no expiration).
|
2021-04-06 21:42:50 +03:00
|
|
|
func (bt *balanceTracker) SetExpirationTCs(pos, neg uint64) {
|
2020-09-14 23:44:20 +03:00
|
|
|
bt.lock.Lock()
|
|
|
|
defer bt.lock.Unlock()
|
|
|
|
|
|
|
|
bt.posExpTC, bt.negExpTC = pos, neg
|
|
|
|
now := bt.clock.Now()
|
|
|
|
if pos > 0 {
|
|
|
|
bt.posExp.SetRate(now, 1/float64(pos*uint64(time.Second)))
|
|
|
|
} else {
|
|
|
|
bt.posExp.SetRate(now, 0)
|
|
|
|
}
|
|
|
|
if neg > 0 {
|
|
|
|
bt.negExp.SetRate(now, 1/float64(neg*uint64(time.Second)))
|
|
|
|
} else {
|
|
|
|
bt.negExp.SetRate(now, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetExpirationTCs returns the current positive and negative token expiration
|
|
|
|
// time constants
|
2021-04-06 21:42:50 +03:00
|
|
|
func (bt *balanceTracker) GetExpirationTCs() (pos, neg uint64) {
|
2020-09-14 23:44:20 +03:00
|
|
|
bt.lock.Lock()
|
|
|
|
defer bt.lock.Unlock()
|
|
|
|
|
|
|
|
return bt.posExpTC, bt.negExpTC
|
|
|
|
}
|
|
|
|
|
2021-04-06 21:42:50 +03:00
|
|
|
// BalanceOperation allows atomic operations on the balance of a node regardless of whether
|
|
|
|
// it is currently connected or not
|
|
|
|
func (bt *balanceTracker) BalanceOperation(id enode.ID, connAddress string, cb func(AtomicBalanceOperator)) {
|
|
|
|
bt.ns.Operation(func() {
|
|
|
|
var nb *nodeBalance
|
|
|
|
if node := bt.ns.GetNode(id); node != nil {
|
|
|
|
nb, _ = bt.ns.GetField(node, bt.setup.balanceField).(*nodeBalance)
|
|
|
|
} else {
|
|
|
|
node = enode.SignNull(&enr.Record{}, id)
|
|
|
|
nb = bt.newNodeBalance(node, connAddress, false)
|
|
|
|
}
|
|
|
|
cb(nb)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// newNodeBalance loads balances from the database and creates a nodeBalance instance
|
|
|
|
// for the given node. It also sets the priorityFlag and adds balanceCallbackZero if
|
2020-09-14 23:44:20 +03:00
|
|
|
// the node has a positive balance.
|
|
|
|
// Note: this function should run inside a NodeStateMachine operation
|
2021-04-06 21:42:50 +03:00
|
|
|
func (bt *balanceTracker) newNodeBalance(node *enode.Node, connAddress string, setFlags bool) *nodeBalance {
|
2020-09-14 23:44:20 +03:00
|
|
|
pb := bt.ndb.getOrNewBalance(node.ID().Bytes(), false)
|
2021-04-06 21:42:50 +03:00
|
|
|
nb := bt.ndb.getOrNewBalance([]byte(connAddress), true)
|
|
|
|
n := &nodeBalance{
|
2020-09-14 23:44:20 +03:00
|
|
|
bt: bt,
|
|
|
|
node: node,
|
2021-04-06 21:42:50 +03:00
|
|
|
setFlags: setFlags,
|
|
|
|
connAddress: connAddress,
|
|
|
|
balance: balance{pos: pb, neg: nb, posExp: bt.posExp, negExp: bt.negExp},
|
2020-09-14 23:44:20 +03:00
|
|
|
initTime: bt.clock.Now(),
|
|
|
|
lastUpdate: bt.clock.Now(),
|
|
|
|
}
|
|
|
|
for i := range n.callbackIndex {
|
|
|
|
n.callbackIndex[i] = -1
|
|
|
|
}
|
2021-04-06 21:42:50 +03:00
|
|
|
if setFlags && n.checkPriorityStatus() {
|
|
|
|
n.bt.ns.SetStateSub(n.node, n.bt.setup.priorityFlag, nodestate.Flags{}, 0)
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// storeBalance stores either a positive or a negative balance in the database
|
2021-04-06 21:42:50 +03:00
|
|
|
func (bt *balanceTracker) storeBalance(id []byte, neg bool, value utils.ExpiredValue) {
|
2020-09-14 23:44:20 +03:00
|
|
|
if bt.canDropBalance(bt.clock.Now(), neg, value) {
|
|
|
|
bt.ndb.delBalance(id, neg) // balance is small enough, drop it directly.
|
|
|
|
} else {
|
|
|
|
bt.ndb.setBalance(id, neg, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// canDropBalance tells whether a positive or negative balance is below the threshold
|
|
|
|
// and therefore can be dropped from the database
|
2021-04-06 21:42:50 +03:00
|
|
|
func (bt *balanceTracker) canDropBalance(now mclock.AbsTime, neg bool, b utils.ExpiredValue) bool {
|
2020-09-14 23:44:20 +03:00
|
|
|
if neg {
|
|
|
|
return b.Value(bt.negExp.LogOffset(now)) <= negThreshold
|
|
|
|
}
|
2020-11-25 11:24:50 +03:00
|
|
|
return b.Value(bt.posExp.LogOffset(now)) <= posThreshold
|
2020-09-14 23:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// updateTotalBalance adjusts the total balance after executing given callback.
|
2021-04-06 21:42:50 +03:00
|
|
|
func (bt *balanceTracker) updateTotalBalance(n *nodeBalance, callback func() bool) {
|
2020-09-14 23:44:20 +03:00
|
|
|
bt.lock.Lock()
|
|
|
|
defer bt.lock.Unlock()
|
|
|
|
|
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
|
|
|
|
|
|
|
original, active := n.balance.pos, n.active
|
|
|
|
if !callback() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if active {
|
|
|
|
bt.active.SubExp(original)
|
|
|
|
} else {
|
|
|
|
bt.inactive.SubExp(original)
|
|
|
|
}
|
|
|
|
if n.active {
|
|
|
|
bt.active.AddExp(n.balance.pos)
|
|
|
|
} else {
|
|
|
|
bt.inactive.AddExp(n.balance.pos)
|
|
|
|
}
|
|
|
|
}
|