Skip to content

Commit

Permalink
Merge pull request #17 from preminem/xiaohe/v0.32.1
Browse files Browse the repository at this point in the history
P2P: Remove deleted peers after commit
  • Loading branch information
wukongcheng authored Dec 26, 2019
2 parents 89f0d3a + ae431a3 commit 0be442e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ var (
msgQueueSize = 1000
)

var (
Switch *p2p.Switch
)

// msgs from the reactor which may update the state
type msgInfo struct {
Msg ConsensusMessage `json:"msg"`
Expand Down Expand Up @@ -1267,6 +1271,9 @@ func (cs *ConsensusState) tryFinalizeCommit(height int64) {

// go
cs.finalizeCommit(height)

// check if peer needs to be removed
Switch.CheckPeers()
}

// Increment height and goto cstypes.RoundStepNewHeight
Expand Down
2 changes: 2 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,8 @@ func NewNode(config *cfg.Config,
consensusReactor, evidenceReactor, nodeInfo, nodeKey, p2pLogger,
)

consensus.Switch = sw

err = sw.AddPersistentPeers(splitAndTrimEmpty(config.P2P.PersistentPeers, ",", " "))
if err != nil {
return nil, errors.Wrap(err, "could not add peers from persistent_peers field")
Expand Down
24 changes: 24 additions & 0 deletions p2p/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,27 @@ func (sw *Switch) addPeer(p Peer, inBound bool) error {

return nil
}

// check if peer needs to be removed
func (sw *Switch) CheckPeers() {
for _, p := range sw.peers.List() {
errc := make(chan error, len(sw.peerFilters))

for _, f := range sw.peerFilters {
go func(f PeerFilterFunc, p Peer, errc chan<- error) {
errc <- f(sw.peers, p)
}(f, p, errc)
}

for i := 0; i < cap(errc); i++ {
select {
case err := <-errc:
if err != nil {
p.CloseConn()
sw.StopPeerGracefully(p)
sw.addrBook.RemoveAddress(p.SocketAddr())
}
}
}
}
}

0 comments on commit 0be442e

Please sign in to comment.