Skip to content

Commit

Permalink
Feat : SetMaxPeers (maticnetwork#726)
Browse files Browse the repository at this point in the history
* init : add admin.setMaxPeers and getMaxPeers

* lint : fix lint

* add : some comments
  • Loading branch information
0xsharma authored and thogard785 committed Apr 5, 2023
1 parent 0e4a8a7 commit ecf9c65
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ web3._extend({
name: 'stopWS',
call: 'admin_stopWS'
}),
new web3._extend.Method({
name: 'getMaxPeers',
call: 'admin_getMaxPeers'
}),
new web3._extend.Method({
name: 'setMaxPeers',
call: 'admin_setMaxPeers',
params: 1
}),
],
properties: [
new web3._extend.Property({
Expand Down
24 changes: 24 additions & 0 deletions node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ type privateAdminAPI struct {
node *Node // Node interfaced by this API
}

// This function sets the param maxPeers for the node. If there are excess peers attached to the node, it will remove the difference.
func (api *privateAdminAPI) SetMaxPeers(maxPeers int) (bool, error) {
// Make sure the server is running, fail otherwise
server := api.node.Server()
if server == nil {
return false, ErrNodeStopped
}

server.SetMaxPeers(maxPeers)

return true, nil
}

// This function gets the maxPeers param for the node.
func (api *privateAdminAPI) GetMaxPeers() (int, error) {
// Make sure the server is running, fail otherwise
server := api.node.Server()
if server == nil {
return 0, ErrNodeStopped
}

return server.MaxPeers, nil
}

// AddPeer requests connecting to a remote node, and also maintaining the new
// connection at all times, even reconnecting if it is lost.
func (api *privateAdminAPI) AddPeer(url string) (bool, error) {
Expand Down
31 changes: 31 additions & 0 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,35 @@ func (srv *Server) Peers() []*Peer {
return ps
}

// This function retrieves the peers that are not trusted-peers
func (srv *Server) getNonTrustedPeers() []*Peer {
allPeers := srv.Peers()

nontrustedPeers := []*Peer{}

for _, peer := range allPeers {
if !peer.Info().Network.Trusted {
nontrustedPeers = append(nontrustedPeers, peer)
}
}

return nontrustedPeers
}

// SetMaxPeers sets the maximum number of peers that can be connected
func (srv *Server) SetMaxPeers(maxPeers int) {
currentPeers := srv.getNonTrustedPeers()
if len(currentPeers) > maxPeers {
peersToDrop := currentPeers[maxPeers:]
for _, peer := range peersToDrop {
log.Warn("CurrentPeers more than MaxPeers", "removing", peer.ID())
srv.RemovePeer(peer.Node())
}
}

srv.MaxPeers = maxPeers
}

// PeerCount returns the number of connected peers.
func (srv *Server) PeerCount() int {
var count int
Expand Down Expand Up @@ -372,6 +401,8 @@ func (srv *Server) RemoveTrustedPeer(node *enode.Node) {
case srv.removetrusted <- node:
case <-srv.quit:
}
// Disconnect the peer if maxPeers is breached.
srv.SetMaxPeers(srv.MaxPeers)
}

// SubscribeEvents subscribes the given channel to peer events
Expand Down

0 comments on commit ecf9c65

Please sign in to comment.