les: remove clientPeerSet and serverSet (#21566)
* les: move NodeStateMachine from clientPool to LesServer * les: new header broadcaster * les: peerCommons.headInfo always contains last announced head * les: remove clientPeerSet and serverSet * les: fixed panic * les: fixed --nodiscover option * les: disconnect all peers at ns.Stop() * les: added comments and fixed signed broadcasts * les: removed unused parameter, fixed tests
This commit is contained in:
@ -18,7 +18,6 @@ package les
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -46,19 +45,6 @@ const (
|
||||
inactiveTimeout = time.Second * 10
|
||||
)
|
||||
|
||||
var (
|
||||
clientPoolSetup = &nodestate.Setup{}
|
||||
clientField = clientPoolSetup.NewField("clientInfo", reflect.TypeOf(&clientInfo{}))
|
||||
connAddressField = clientPoolSetup.NewField("connAddr", reflect.TypeOf(""))
|
||||
balanceTrackerSetup = lps.NewBalanceTrackerSetup(clientPoolSetup)
|
||||
priorityPoolSetup = lps.NewPriorityPoolSetup(clientPoolSetup)
|
||||
)
|
||||
|
||||
func init() {
|
||||
balanceTrackerSetup.Connect(connAddressField, priorityPoolSetup.CapacityField)
|
||||
priorityPoolSetup.Connect(balanceTrackerSetup.BalanceField, balanceTrackerSetup.UpdateFlag) // NodeBalance implements nodePriority
|
||||
}
|
||||
|
||||
// clientPool implements a client database that assigns a priority to each client
|
||||
// based on a positive and negative balance. Positive balance is externally assigned
|
||||
// to prioritized clients and is decreased with connection time and processed
|
||||
@ -119,8 +105,7 @@ type clientInfo struct {
|
||||
}
|
||||
|
||||
// newClientPool creates a new client pool
|
||||
func newClientPool(lespayDb ethdb.Database, minCap uint64, connectedBias time.Duration, clock mclock.Clock, removePeer func(enode.ID)) *clientPool {
|
||||
ns := nodestate.NewNodeStateMachine(nil, nil, clock, clientPoolSetup)
|
||||
func newClientPool(ns *nodestate.NodeStateMachine, lespayDb ethdb.Database, minCap uint64, connectedBias time.Duration, clock mclock.Clock, removePeer func(enode.ID)) *clientPool {
|
||||
pool := &clientPool{
|
||||
ns: ns,
|
||||
BalanceTrackerSetup: balanceTrackerSetup,
|
||||
@ -147,7 +132,7 @@ func newClientPool(lespayDb ethdb.Database, minCap uint64, connectedBias time.Du
|
||||
})
|
||||
|
||||
ns.SubscribeState(pool.ActiveFlag.Or(pool.PriorityFlag), func(node *enode.Node, oldState, newState nodestate.Flags) {
|
||||
c, _ := ns.GetField(node, clientField).(*clientInfo)
|
||||
c, _ := ns.GetField(node, clientInfoField).(*clientInfo)
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
@ -172,7 +157,7 @@ func newClientPool(lespayDb ethdb.Database, minCap uint64, connectedBias time.Du
|
||||
if oldState.Equals(pool.ActiveFlag) && newState.Equals(pool.InactiveFlag) {
|
||||
clientDeactivatedMeter.Mark(1)
|
||||
log.Debug("Client deactivated", "id", node.ID())
|
||||
c, _ := ns.GetField(node, clientField).(*clientInfo)
|
||||
c, _ := ns.GetField(node, clientInfoField).(*clientInfo)
|
||||
if c == nil || !c.peer.allowInactive() {
|
||||
pool.removePeer(node.ID())
|
||||
}
|
||||
@ -190,13 +175,11 @@ func newClientPool(lespayDb ethdb.Database, minCap uint64, connectedBias time.Du
|
||||
newCap, _ := newValue.(uint64)
|
||||
totalConnected += newCap - oldCap
|
||||
totalConnectedGauge.Update(int64(totalConnected))
|
||||
c, _ := ns.GetField(node, clientField).(*clientInfo)
|
||||
c, _ := ns.GetField(node, clientInfoField).(*clientInfo)
|
||||
if c != nil {
|
||||
c.peer.updateCapacity(newCap)
|
||||
}
|
||||
})
|
||||
|
||||
ns.Start()
|
||||
return pool
|
||||
}
|
||||
|
||||
@ -210,7 +193,6 @@ func (f *clientPool) stop() {
|
||||
f.disconnectNode(node)
|
||||
})
|
||||
f.bt.Stop()
|
||||
f.ns.Stop()
|
||||
}
|
||||
|
||||
// connect should be called after a successful handshake. If the connection was
|
||||
@ -225,7 +207,7 @@ func (f *clientPool) connect(peer clientPoolPeer) (uint64, error) {
|
||||
}
|
||||
// Dedup connected peers.
|
||||
node, freeID := peer.Node(), peer.freeClientId()
|
||||
if f.ns.GetField(node, clientField) != nil {
|
||||
if f.ns.GetField(node, clientInfoField) != nil {
|
||||
log.Debug("Client already connected", "address", freeID, "id", node.ID().String())
|
||||
return 0, fmt.Errorf("Client already connected address=%s id=%s", freeID, node.ID().String())
|
||||
}
|
||||
@ -237,7 +219,7 @@ func (f *clientPool) connect(peer clientPoolPeer) (uint64, error) {
|
||||
connected: true,
|
||||
connectedAt: now,
|
||||
}
|
||||
f.ns.SetField(node, clientField, c)
|
||||
f.ns.SetField(node, clientInfoField, c)
|
||||
f.ns.SetField(node, connAddressField, freeID)
|
||||
if c.balance, _ = f.ns.GetField(node, f.BalanceField).(*lps.NodeBalance); c.balance == nil {
|
||||
f.disconnect(peer)
|
||||
@ -280,7 +262,7 @@ func (f *clientPool) disconnect(p clientPoolPeer) {
|
||||
// disconnectNode removes node fields and flags related to connected status
|
||||
func (f *clientPool) disconnectNode(node *enode.Node) {
|
||||
f.ns.SetField(node, connAddressField, nil)
|
||||
f.ns.SetField(node, clientField, nil)
|
||||
f.ns.SetField(node, clientInfoField, nil)
|
||||
}
|
||||
|
||||
// setDefaultFactors sets the default price factors applied to subsequently connected clients
|
||||
@ -299,7 +281,8 @@ func (f *clientPool) capacityInfo() (uint64, uint64, uint64) {
|
||||
defer f.lock.Unlock()
|
||||
|
||||
// total priority active cap will be supported when the token issuer module is added
|
||||
return f.capLimit, f.pp.ActiveCapacity(), 0
|
||||
_, activeCap := f.pp.Active()
|
||||
return f.capLimit, activeCap, 0
|
||||
}
|
||||
|
||||
// setLimits sets the maximum number and total capacity of connected clients,
|
||||
@ -314,13 +297,13 @@ func (f *clientPool) setLimits(totalConn int, totalCap uint64) {
|
||||
|
||||
// setCapacity sets the assigned capacity of a connected client
|
||||
func (f *clientPool) setCapacity(node *enode.Node, freeID string, capacity uint64, bias time.Duration, setCap bool) (uint64, error) {
|
||||
c, _ := f.ns.GetField(node, clientField).(*clientInfo)
|
||||
c, _ := f.ns.GetField(node, clientInfoField).(*clientInfo)
|
||||
if c == nil {
|
||||
if setCap {
|
||||
return 0, fmt.Errorf("client %064x is not connected", node.ID())
|
||||
}
|
||||
c = &clientInfo{node: node}
|
||||
f.ns.SetField(node, clientField, c)
|
||||
f.ns.SetField(node, clientInfoField, c)
|
||||
f.ns.SetField(node, connAddressField, freeID)
|
||||
if c.balance, _ = f.ns.GetField(node, f.BalanceField).(*lps.NodeBalance); c.balance == nil {
|
||||
log.Error("BalanceField is missing", "node", node.ID())
|
||||
@ -328,7 +311,7 @@ func (f *clientPool) setCapacity(node *enode.Node, freeID string, capacity uint6
|
||||
}
|
||||
defer func() {
|
||||
f.ns.SetField(node, connAddressField, nil)
|
||||
f.ns.SetField(node, clientField, nil)
|
||||
f.ns.SetField(node, clientInfoField, nil)
|
||||
}()
|
||||
}
|
||||
var (
|
||||
@ -370,7 +353,7 @@ func (f *clientPool) forClients(ids []enode.ID, cb func(client *clientInfo)) {
|
||||
|
||||
if len(ids) == 0 {
|
||||
f.ns.ForEach(nodestate.Flags{}, nodestate.Flags{}, func(node *enode.Node, state nodestate.Flags) {
|
||||
c, _ := f.ns.GetField(node, clientField).(*clientInfo)
|
||||
c, _ := f.ns.GetField(node, clientInfoField).(*clientInfo)
|
||||
if c != nil {
|
||||
cb(c)
|
||||
}
|
||||
@ -381,12 +364,12 @@ func (f *clientPool) forClients(ids []enode.ID, cb func(client *clientInfo)) {
|
||||
if node == nil {
|
||||
node = enode.SignNull(&enr.Record{}, id)
|
||||
}
|
||||
c, _ := f.ns.GetField(node, clientField).(*clientInfo)
|
||||
c, _ := f.ns.GetField(node, clientInfoField).(*clientInfo)
|
||||
if c != nil {
|
||||
cb(c)
|
||||
} else {
|
||||
c = &clientInfo{node: node}
|
||||
f.ns.SetField(node, clientField, c)
|
||||
f.ns.SetField(node, clientInfoField, c)
|
||||
f.ns.SetField(node, connAddressField, "")
|
||||
if c.balance, _ = f.ns.GetField(node, f.BalanceField).(*lps.NodeBalance); c.balance != nil {
|
||||
cb(c)
|
||||
@ -394,7 +377,7 @@ func (f *clientPool) forClients(ids []enode.ID, cb func(client *clientInfo)) {
|
||||
log.Error("BalanceField is missing")
|
||||
}
|
||||
f.ns.SetField(node, connAddressField, nil)
|
||||
f.ns.SetField(node, clientField, nil)
|
||||
f.ns.SetField(node, clientInfoField, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user