Skip to content

Commit b0ca1b6

Browse files
committed
eth: use maxpeers from p2p layer instead of extra config
1 parent 03d0036 commit b0ca1b6

8 files changed

Lines changed: 24 additions & 33 deletions

File tree

‎cmd/utils/flags.go‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -949,10 +949,6 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
949949
cfg.NetworkId = ctx.GlobalUint64(NetworkIdFlag.Name)
950950
}
951951

952-
// Ethereum needs to know maxPeers to calculate the light server peer ratio.
953-
// TODO(fjl): ensure Ethereum can get MaxPeers from node.
954-
cfg.MaxPeers = ctx.GlobalInt(MaxPeersFlag.Name)
955-
956952
if ctx.GlobalIsSet(CacheFlag.Name) {
957953
cfg.DatabaseCache = ctx.GlobalInt(CacheFlag.Name)
958954
}

‎eth/backend.go‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,19 @@ type LesServer interface {
5757

5858
// Ethereum implements the Ethereum full node service.
5959
type Ethereum struct {
60+
config *Config
6061
chainConfig *params.ChainConfig
62+
6163
// Channel for shutting down the service
6264
shutdownChan chan bool // Channel for shutting down the ethereum
6365
stopDbUpgrade func() error // stop chain db sequential key upgrade
66+
6467
// Handlers
6568
txPool *core.TxPool
6669
blockchain *core.BlockChain
6770
protocolManager *ProtocolManager
6871
lesServer LesServer
72+
6973
// DB interfaces
7074
chainDb ethdb.Database // Block chain database
7175

@@ -98,7 +102,6 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
98102
if !config.SyncMode.IsValid() {
99103
return nil, fmt.Errorf("invalid sync mode %d", config.SyncMode)
100104
}
101-
102105
chainDb, err := CreateDB(ctx, config, "chaindata")
103106
if err != nil {
104107
return nil, err
@@ -111,6 +114,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
111114
log.Info("Initialised chain configuration", "config", chainConfig)
112115

113116
eth := &Ethereum{
117+
config: config,
114118
chainDb: chainDb,
115119
chainConfig: chainConfig,
116120
eventMux: ctx.EventMux,
@@ -153,21 +157,9 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
153157
}
154158
eth.txPool = core.NewTxPool(config.TxPool, eth.chainConfig, eth.blockchain)
155159

156-
maxPeers := config.MaxPeers
157-
if config.LightServ > 0 {
158-
// if we are running a light server, limit the number of ETH peers so that we reserve some space for incoming LES connections
159-
// temporary solution until the new peer connectivity API is finished
160-
halfPeers := maxPeers / 2
161-
maxPeers -= config.LightPeers
162-
if maxPeers < halfPeers {
163-
maxPeers = halfPeers
164-
}
165-
}
166-
167-
if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.SyncMode, config.NetworkId, maxPeers, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb); err != nil {
160+
if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb); err != nil {
168161
return nil, err
169162
}
170-
171163
eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine)
172164
eth.miner.SetExtra(makeExtraData(config.ExtraData))
173165

@@ -376,7 +368,15 @@ func (s *Ethereum) Protocols() []p2p.Protocol {
376368
func (s *Ethereum) Start(srvr *p2p.Server) error {
377369
s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.NetVersion())
378370

379-
s.protocolManager.Start()
371+
// Figure out a max peers count based on the server limits
372+
maxPeers := srvr.MaxPeers
373+
if s.config.LightServ > 0 {
374+
maxPeers -= s.config.LightPeers
375+
if maxPeers < srvr.MaxPeers/2 {
376+
maxPeers = srvr.MaxPeers / 2
377+
}
378+
}
379+
s.protocolManager.Start(maxPeers)
380380
if s.lesServer != nil {
381381
s.lesServer.Start(srvr)
382382
}

‎eth/config.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ type Config struct {
7979
// Light client options
8080
LightServ int `toml:",omitempty"` // Maximum percentage of time allowed for serving LES requests
8181
LightPeers int `toml:",omitempty"` // Maximum number of LES client peers
82-
MaxPeers int `toml:"-"` // Maximum number of global peers
8382

8483
// Database options
8584
SkipBcVersionCheck bool `toml:"-"`

‎eth/gen_config.go‎

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎eth/handler.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ type ProtocolManager struct {
9999

100100
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
101101
// with the ethereum network.
102-
func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, networkId uint64, maxPeers int, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database) (*ProtocolManager, error) {
102+
func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, networkId uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database) (*ProtocolManager, error) {
103103
// Create the protocol manager with the base fields
104104
manager := &ProtocolManager{
105105
networkId: networkId,
@@ -108,7 +108,6 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
108108
blockchain: blockchain,
109109
chaindb: chaindb,
110110
chainconfig: config,
111-
maxPeers: maxPeers,
112111
peers: newPeerSet(),
113112
newPeerCh: make(chan *peer),
114113
noMorePeers: make(chan struct{}),
@@ -203,11 +202,14 @@ func (pm *ProtocolManager) removePeer(id string) {
203202
}
204203
}
205204

206-
func (pm *ProtocolManager) Start() {
205+
func (pm *ProtocolManager) Start(maxPeers int) {
206+
pm.maxPeers = maxPeers
207+
207208
// broadcast transactions
208209
pm.txCh = make(chan core.TxPreEvent, txChanSize)
209210
pm.txSub = pm.txpool.SubscribeTxPreEvent(pm.txCh)
210211
go pm.txBroadcastLoop()
212+
211213
// broadcast mined blocks
212214
pm.minedBlockSub = pm.eventMux.Subscribe(core.NewMinedBlockEvent{})
213215
go pm.minedBroadcastLoop()

‎eth/handler_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,11 @@ func testDAOChallenge(t *testing.T, localForked, remoteForked bool, timeout bool
476476
genesis = gspec.MustCommit(db)
477477
blockchain, _ = core.NewBlockChain(db, config, pow, vm.Config{})
478478
)
479-
pm, err := NewProtocolManager(config, downloader.FullSync, DefaultConfig.NetworkId, 1000, evmux, new(testTxPool), pow, blockchain, db)
479+
pm, err := NewProtocolManager(config, downloader.FullSync, DefaultConfig.NetworkId, evmux, new(testTxPool), pow, blockchain, db)
480480
if err != nil {
481481
t.Fatalf("failed to start test protocol manager: %v", err)
482482
}
483-
pm.Start()
483+
pm.Start(1000)
484484
defer pm.Stop()
485485

486486
// Connect a new peer and check that we receive the DAO challenge

‎eth/helper_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ func newTestProtocolManager(mode downloader.SyncMode, blocks int, generator func
6666
panic(err)
6767
}
6868

69-
pm, err := NewProtocolManager(gspec.Config, mode, DefaultConfig.NetworkId, 1000, evmux, &testTxPool{added: newtx}, engine, blockchain, db)
69+
pm, err := NewProtocolManager(gspec.Config, mode, DefaultConfig.NetworkId, evmux, &testTxPool{added: newtx}, engine, blockchain, db)
7070
if err != nil {
7171
return nil, err
7272
}
73-
pm.Start()
73+
pm.Start(1000)
7474
return pm, nil
7575
}
7676

‎params/bootnodes.go‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package params
1919
// MainnetBootnodes are the enode URLs of the P2P bootstrap nodes running on
2020
// the main Ethereum network.
2121
var MainnetBootnodes = []string{
22-
2322
// Ethereum Foundation Go Bootnodes
2423
"enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303", // IE
2524
"enode://3f1d12044546b76342d59d4a05532c14b85aa669704bfe1f864fe079415aa2c02d743e03218e57a33fb94523adb54032871a6c51b2cc5514cb7c7e35b3ed0a99@13.93.211.84:30303", // US-WEST
@@ -29,7 +28,6 @@ var MainnetBootnodes = []string{
2928

3029
// Ethereum Foundation Cpp Bootnodes
3130
"enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9@5.1.83.226:30303", // DE
32-
3331
}
3432

3533
// TestnetBootnodes are the enode URLs of the P2P bootstrap nodes running on the

0 commit comments

Comments
 (0)