Skip to content

Commit c3f238d

Browse files
zsfelfoldikaralabe
authored andcommitted
les: limit LES peer count and improve peer configuration logic (#16010)
* les: limit number of LES connections * eth, cmd/utils: light vs max peer configuration logic
1 parent bc0666f commit c3f238d

7 files changed

Lines changed: 43 additions & 11 deletions

File tree

‎cmd/utils/flags.go‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ var (
179179
LightPeersFlag = cli.IntFlag{
180180
Name: "lightpeers",
181181
Usage: "Maximum number of LES client peers",
182-
Value: 20,
182+
Value: eth.DefaultConfig.LightPeers,
183183
}
184184
LightKDFFlag = cli.BoolFlag{
185185
Name: "lightkdf",
@@ -791,20 +791,40 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) {
791791
setBootstrapNodes(ctx, cfg)
792792
setBootstrapNodesV5(ctx, cfg)
793793

794+
lightClient := ctx.GlobalBool(LightModeFlag.Name) || ctx.GlobalString(SyncModeFlag.Name) == "light"
795+
lightServer := ctx.GlobalInt(LightServFlag.Name) != 0
796+
lightPeers := ctx.GlobalInt(LightPeersFlag.Name)
797+
794798
if ctx.GlobalIsSet(MaxPeersFlag.Name) {
795799
cfg.MaxPeers = ctx.GlobalInt(MaxPeersFlag.Name)
800+
} else {
801+
if lightServer {
802+
cfg.MaxPeers += lightPeers
803+
}
804+
if lightClient && ctx.GlobalIsSet(LightPeersFlag.Name) && cfg.MaxPeers < lightPeers {
805+
cfg.MaxPeers = lightPeers
806+
}
796807
}
808+
if !(lightClient || lightServer) {
809+
lightPeers = 0
810+
}
811+
ethPeers := cfg.MaxPeers - lightPeers
812+
if lightClient {
813+
ethPeers = 0
814+
}
815+
log.Info("Maximum peer count", "ETH", ethPeers, "LES", lightPeers, "total", cfg.MaxPeers)
816+
797817
if ctx.GlobalIsSet(MaxPendingPeersFlag.Name) {
798818
cfg.MaxPendingPeers = ctx.GlobalInt(MaxPendingPeersFlag.Name)
799819
}
800-
if ctx.GlobalIsSet(NoDiscoverFlag.Name) || ctx.GlobalBool(LightModeFlag.Name) {
820+
if ctx.GlobalIsSet(NoDiscoverFlag.Name) || lightClient {
801821
cfg.NoDiscovery = true
802822
}
803823

804824
// if we're running a light client or server, force enable the v5 peer discovery
805825
// unless it is explicitly disabled with --nodiscover note that explicitly specifying
806826
// --v5disc overrides --nodiscover, in which case the later only disables v4 discovery
807-
forceV5Discovery := (ctx.GlobalBool(LightModeFlag.Name) || ctx.GlobalInt(LightServFlag.Name) > 0) && !ctx.GlobalBool(NoDiscoverFlag.Name)
827+
forceV5Discovery := (lightClient || lightServer) && !ctx.GlobalBool(NoDiscoverFlag.Name)
808828
if ctx.GlobalIsSet(DiscoveryV5Flag.Name) {
809829
cfg.DiscoveryV5 = ctx.GlobalBool(DiscoveryV5Flag.Name)
810830
} else if forceV5Discovery {

‎eth/backend.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,10 @@ func (s *Ethereum) Start(srvr *p2p.Server) error {
393393
// Figure out a max peers count based on the server limits
394394
maxPeers := srvr.MaxPeers
395395
if s.config.LightServ > 0 {
396-
maxPeers -= s.config.LightPeers
397-
if maxPeers < srvr.MaxPeers/2 {
398-
maxPeers = srvr.MaxPeers / 2
396+
if s.config.LightPeers >= srvr.MaxPeers {
397+
return fmt.Errorf("invalid peer config: light peer count (%d) >= total peer count (%d)", s.config.LightPeers, srvr.MaxPeers)
399398
}
399+
maxPeers -= s.config.LightPeers
400400
}
401401
// Start the networking layer and the light server if requested
402402
s.protocolManager.Start(maxPeers)

‎eth/config.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var DefaultConfig = Config{
4343
DatasetsOnDisk: 2,
4444
},
4545
NetworkId: 1,
46-
LightPeers: 20,
46+
LightPeers: 100,
4747
DatabaseCache: 128,
4848
GasPrice: big.NewInt(18 * params.Shannon),
4949

‎les/backend.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import (
4646
)
4747

4848
type LightEthereum struct {
49+
config *eth.Config
50+
4951
odr *LesOdr
5052
relay *LesTxRelay
5153
chainConfig *params.ChainConfig
@@ -92,6 +94,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
9294
quitSync := make(chan struct{})
9395

9496
leth := &LightEthereum{
97+
config: config,
9598
chainConfig: chainConfig,
9699
chainDb: chainDb,
97100
eventMux: ctx.EventMux,
@@ -224,7 +227,7 @@ func (s *LightEthereum) Start(srvr *p2p.Server) error {
224227
// clients are searching for the first advertised protocol in the list
225228
protocolVersion := AdvertiseProtocolVersions[0]
226229
s.serverPool.start(srvr, lesTopic(s.blockchain.Genesis().Hash(), protocolVersion))
227-
s.protocolManager.Start()
230+
s.protocolManager.Start(s.config.LightPeers)
228231
return nil
229232
}
230233

‎les/handler.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ type ProtocolManager struct {
109109
downloader *downloader.Downloader
110110
fetcher *lightFetcher
111111
peers *peerSet
112+
maxPeers int
112113

113114
SubProtocols []p2p.Protocol
114115

@@ -216,7 +217,9 @@ func (pm *ProtocolManager) removePeer(id string) {
216217
pm.peers.Unregister(id)
217218
}
218219

219-
func (pm *ProtocolManager) Start() {
220+
func (pm *ProtocolManager) Start(maxPeers int) {
221+
pm.maxPeers = maxPeers
222+
220223
if pm.lightSync {
221224
go pm.syncer()
222225
} else {
@@ -257,6 +260,10 @@ func (pm *ProtocolManager) newPeer(pv int, nv uint64, p *p2p.Peer, rw p2p.MsgRea
257260
// handle is the callback invoked to manage the life cycle of a les peer. When
258261
// this function terminates, the peer is disconnected.
259262
func (pm *ProtocolManager) handle(p *peer) error {
263+
if pm.peers.Len() >= pm.maxPeers {
264+
return p2p.DiscTooManyPeers
265+
}
266+
260267
p.Log().Debug("Light Ethereum peer connected", "name", p.Name())
261268

262269
// Execute the LES handshake

‎les/helper_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func newTestProtocolManager(lightSync bool, blocks int, generator func(int, *cor
176176
srv.fcManager = flowcontrol.NewClientManager(50, 10, 1000000000)
177177
srv.fcCostStats = newCostStats(nil)
178178
}
179-
pm.Start()
179+
pm.Start(1000)
180180
return pm, nil
181181
}
182182

‎les/server.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
)
3939

4040
type LesServer struct {
41+
config *eth.Config
4142
protocolManager *ProtocolManager
4243
fcManager *flowcontrol.ClientManager // nil if our node is client only
4344
fcCostStats *requestCostStats
@@ -62,6 +63,7 @@ func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
6263
}
6364

6465
srv := &LesServer{
66+
config: config,
6567
protocolManager: pm,
6668
quitSync: quitSync,
6769
lesTopics: lesTopics,
@@ -108,7 +110,7 @@ func (s *LesServer) Protocols() []p2p.Protocol {
108110

109111
// Start starts the LES server
110112
func (s *LesServer) Start(srvr *p2p.Server) {
111-
s.protocolManager.Start()
113+
s.protocolManager.Start(s.config.LightPeers)
112114
for _, topic := range s.lesTopics {
113115
topic := topic
114116
go func() {

0 commit comments

Comments
 (0)