Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : SetMaxPeers #726

Merged
merged 3 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -307,6 +307,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 @@ -368,6 +397,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