Skip to content

Commit b8ba80b

Browse files
authored
Merge pull request bnb-chain#2740 from Firescar96/removepeer
node, p2p, internal: Add ability to remove peers via admin interface
2 parents 8ec6ccc + 4c3da0f commit b8ba80b

5 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎internal/web3ext/web3ext.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ web3._extend({
3939
call: 'admin_addPeer',
4040
params: 1
4141
}),
42+
new web3._extend.Method({
43+
name: 'removePeer',
44+
call: 'admin_removePeer',
45+
params: 1
46+
}),
4247
new web3._extend.Method({
4348
name: 'exportChain',
4449
call: 'admin_exportChain',

‎node/api.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ func (api *PrivateAdminAPI) AddPeer(url string) (bool, error) {
5858
return true, nil
5959
}
6060

61+
// RemovePeer disconnects from a a remote node if the connection exists
62+
func (api *PrivateAdminAPI) RemovePeer(url string) (bool, error) {
63+
// Make sure the server is running, fail otherwise
64+
server := api.node.Server()
65+
if server == nil {
66+
return false, ErrNodeStopped
67+
}
68+
// Try to remove the url as a static peer and return
69+
node, err := discover.ParseNode(url)
70+
if err != nil {
71+
return false, fmt.Errorf("invalid enode: %v", err)
72+
}
73+
server.RemovePeer(node)
74+
return true, nil
75+
}
76+
6177
// StartRPC starts the HTTP RPC API server.
6278
func (api *PrivateAdminAPI) StartRPC(host *string, port *rpc.HexNumber, cors *string, apis *string) (bool, error) {
6379
api.node.lock.Lock()

‎p2p/dial.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ func (s *dialstate) addStatic(n *discover.Node) {
121121
s.static[n.ID] = &dialTask{flags: staticDialedConn, dest: n}
122122
}
123123

124+
func (s *dialstate) removeStatic(n *discover.Node) {
125+
// This removes a task so future attempts to connect will not be made.
126+
delete(s.static, n.ID)
127+
}
128+
124129
func (s *dialstate) newTasks(nRunning int, peers map[discover.NodeID]*Peer, now time.Time) []task {
125130
var newtasks []task
126131
isDialing := func(id discover.NodeID) bool {

‎p2p/server.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ type Server struct {
142142

143143
quit chan struct{}
144144
addstatic chan *discover.Node
145+
removestatic chan *discover.Node
145146
posthandshake chan *conn
146147
addpeer chan *conn
147148
delpeer chan *Peer
@@ -257,6 +258,14 @@ func (srv *Server) AddPeer(node *discover.Node) {
257258
}
258259
}
259260

261+
// RemovePeer disconnects from the given node
262+
func (srv *Server) RemovePeer(node *discover.Node) {
263+
select {
264+
case srv.removestatic <- node:
265+
case <-srv.quit:
266+
}
267+
}
268+
260269
// Self returns the local node's endpoint information.
261270
func (srv *Server) Self() *discover.Node {
262271
srv.lock.Lock()
@@ -327,6 +336,7 @@ func (srv *Server) Start() (err error) {
327336
srv.delpeer = make(chan *Peer)
328337
srv.posthandshake = make(chan *conn)
329338
srv.addstatic = make(chan *discover.Node)
339+
srv.removestatic = make(chan *discover.Node)
330340
srv.peerOp = make(chan peerOpFunc)
331341
srv.peerOpDone = make(chan struct{})
332342

@@ -395,6 +405,7 @@ type dialer interface {
395405
newTasks(running int, peers map[discover.NodeID]*Peer, now time.Time) []task
396406
taskDone(task, time.Time)
397407
addStatic(*discover.Node)
408+
removeStatic(*discover.Node)
398409
}
399410

400411
func (srv *Server) run(dialstate dialer) {
@@ -458,6 +469,15 @@ running:
458469
// it will keep the node connected.
459470
glog.V(logger.Detail).Infoln("<-addstatic:", n)
460471
dialstate.addStatic(n)
472+
case n := <-srv.removestatic:
473+
// This channel is used by RemovePeer to send a
474+
// disconnect request to a peer and begin the
475+
// stop keeping the node connected
476+
glog.V(logger.Detail).Infoln("<-removestatic:", n)
477+
dialstate.removeStatic(n)
478+
if p, ok := peers[n.ID]; ok {
479+
p.Disconnect(DiscRequested)
480+
}
461481
case op := <-srv.peerOp:
462482
// This channel is used by Peers and PeerCount.
463483
op(peers)

‎p2p/server_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ func (tg taskgen) taskDone(t task, now time.Time) {
301301
}
302302
func (tg taskgen) addStatic(*discover.Node) {
303303
}
304+
func (tg taskgen) removeStatic(*discover.Node) {
305+
}
304306

305307
type testTask struct {
306308
index int

0 commit comments

Comments
 (0)