les: limit number of LES connections

This commit is contained in:
Zsolt Felfoldi 2018-02-02 13:15:00 +01:00
parent 4dedde7beb
commit 49a3d034d9
4 changed files with 16 additions and 4 deletions

View file

@ -46,6 +46,8 @@ import (
) )
type LightEthereum struct { type LightEthereum struct {
config *eth.Config
odr *LesOdr odr *LesOdr
relay *LesTxRelay relay *LesTxRelay
chainConfig *params.ChainConfig chainConfig *params.ChainConfig
@ -92,6 +94,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
quitSync := make(chan struct{}) quitSync := make(chan struct{})
leth := &LightEthereum{ leth := &LightEthereum{
config: config,
chainConfig: chainConfig, chainConfig: chainConfig,
chainDb: chainDb, chainDb: chainDb,
eventMux: ctx.EventMux, eventMux: ctx.EventMux,
@ -224,7 +227,7 @@ func (s *LightEthereum) Start(srvr *p2p.Server) error {
// clients are searching for the first advertised protocol in the list // clients are searching for the first advertised protocol in the list
protocolVersion := AdvertiseProtocolVersions[0] protocolVersion := AdvertiseProtocolVersions[0]
s.serverPool.start(srvr, lesTopic(s.blockchain.Genesis().Hash(), protocolVersion)) s.serverPool.start(srvr, lesTopic(s.blockchain.Genesis().Hash(), protocolVersion))
s.protocolManager.Start() s.protocolManager.Start(s.config.LightPeers)
return nil return nil
} }

View file

@ -109,6 +109,7 @@ type ProtocolManager struct {
downloader *downloader.Downloader downloader *downloader.Downloader
fetcher *lightFetcher fetcher *lightFetcher
peers *peerSet peers *peerSet
maxPeers int
SubProtocols []p2p.Protocol SubProtocols []p2p.Protocol
@ -216,7 +217,9 @@ func (pm *ProtocolManager) removePeer(id string) {
pm.peers.Unregister(id) pm.peers.Unregister(id)
} }
func (pm *ProtocolManager) Start() { func (pm *ProtocolManager) Start(maxPeers int) {
pm.maxPeers = maxPeers
if pm.lightSync { if pm.lightSync {
go pm.syncer() go pm.syncer()
} else { } else {
@ -257,6 +260,10 @@ func (pm *ProtocolManager) newPeer(pv int, nv uint64, p *p2p.Peer, rw p2p.MsgRea
// handle is the callback invoked to manage the life cycle of a les peer. When // handle is the callback invoked to manage the life cycle of a les peer. When
// this function terminates, the peer is disconnected. // this function terminates, the peer is disconnected.
func (pm *ProtocolManager) handle(p *peer) error { func (pm *ProtocolManager) handle(p *peer) error {
if pm.peers.Len() >= pm.maxPeers {
return p2p.DiscTooManyPeers
}
p.Log().Debug("Light Ethereum peer connected", "name", p.Name()) p.Log().Debug("Light Ethereum peer connected", "name", p.Name())
// Execute the LES handshake // Execute the LES handshake

View file

@ -176,7 +176,7 @@ func newTestProtocolManager(lightSync bool, blocks int, generator func(int, *cor
srv.fcManager = flowcontrol.NewClientManager(50, 10, 1000000000) srv.fcManager = flowcontrol.NewClientManager(50, 10, 1000000000)
srv.fcCostStats = newCostStats(nil) srv.fcCostStats = newCostStats(nil)
} }
pm.Start() pm.Start(1000)
return pm, nil return pm, nil
} }

View file

@ -38,6 +38,7 @@ import (
) )
type LesServer struct { type LesServer struct {
config *eth.Config
protocolManager *ProtocolManager protocolManager *ProtocolManager
fcManager *flowcontrol.ClientManager // nil if our node is client only fcManager *flowcontrol.ClientManager // nil if our node is client only
fcCostStats *requestCostStats fcCostStats *requestCostStats
@ -62,6 +63,7 @@ func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
} }
srv := &LesServer{ srv := &LesServer{
config: config,
protocolManager: pm, protocolManager: pm,
quitSync: quitSync, quitSync: quitSync,
lesTopics: lesTopics, lesTopics: lesTopics,
@ -108,7 +110,7 @@ func (s *LesServer) Protocols() []p2p.Protocol {
// Start starts the LES server // Start starts the LES server
func (s *LesServer) Start(srvr *p2p.Server) { func (s *LesServer) Start(srvr *p2p.Server) {
s.protocolManager.Start() s.protocolManager.Start(s.config.LightPeers)
for _, topic := range s.lesTopics { for _, topic := range s.lesTopics {
topic := topic topic := topic
go func() { go func() {