From 6d5714e1b7affd4295cf3f3a91244da4e91fca31 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 28 Oct 2016 12:49:26 -0500 Subject: [PATCH] server/mempool: Evict orphans on peer disconnect. This removes any remaining orphan transactions that were sent by a peer when it disconnects since it is extremely unlikely that the missing parents will ever materialize from elsewhere. --- log.go | 9 +++++++++ server.go | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/log.go b/log.go index ebdddeac..ae58547d 100644 --- a/log.go +++ b/log.go @@ -220,3 +220,12 @@ func directionString(inbound bool) string { } return "outbound" } + +// pickNoun returns the singular or plural form of a noun depending +// on the count n. +func pickNoun(n uint64, singular, plural string) string { + if n == 1 { + return singular + } + return plural +} diff --git a/server.go b/server.go index 63a52fea..ff70b21a 100644 --- a/server.go +++ b/server.go @@ -1596,7 +1596,7 @@ func (s *server) outboundPeerConnected(c *connmgr.ConnReq, conn net.Conn) { } // peerDoneHandler handles peer disconnects by notifiying the server that it's -// done. +// done along with other performing other desirable cleanup. func (s *server) peerDoneHandler(sp *serverPeer) { sp.WaitForDisconnect() s.donePeers <- sp @@ -1604,6 +1604,14 @@ func (s *server) peerDoneHandler(sp *serverPeer) { // Only tell block manager we are gone if we ever told it we existed. if sp.VersionKnown() { s.blockManager.DonePeer(sp) + + // Evict any remaining orphans that were sent by the peer. + numEvicted := s.txMemPool.RemoveOrphansByTag(mempool.Tag(sp.ID())) + if numEvicted > 0 { + txmpLog.Debugf("Evicted %d %s from peer %v (id %d)", + numEvicted, pickNoun(numEvicted, "orphan", + "orphans"), sp, sp.ID()) + } } close(sp.quit) }