From 4dde5a16e6fd9470843d45871ae03bc3e2fd43d4 Mon Sep 17 00:00:00 2001 From: Captain Date: Fri, 3 Jul 2020 12:43:11 +0000 Subject: [PATCH] P2P memory consuming bug fixed. --- p2p/chain_request.go | 2 +- p2p/chain_response.go | 2 +- p2p/handshake.go | 4 +++- p2p/peer_pool.go | 11 +++++++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/p2p/chain_request.go b/p2p/chain_request.go index 867e7bf..674d520 100644 --- a/p2p/chain_request.go +++ b/p2p/chain_request.go @@ -99,7 +99,7 @@ func (connection *Connection) Handle_ChainRequest(buf []byte) { return } - if len(request.Block_list) != len(request.TopoHeights) { + if len(request.Block_list) != len(request.TopoHeights) || len(request.Block_list) > 1024 { rlog.Warnf("Peer chain request has %d block %d topos, therefore invalid", len(request.Block_list), len(request.TopoHeights)) connection.Exit() return diff --git a/p2p/chain_response.go b/p2p/chain_response.go index 7829be3..7be31e1 100644 --- a/p2p/chain_response.go +++ b/p2p/chain_response.go @@ -62,7 +62,7 @@ func (connection *Connection) Handle_ChainResponse(buf []byte) { } // we were expecting something else ban - if len(response.Block_list) < 1 { + if len(response.Block_list) < 1 || len(response.TopBlocks) > 100 { rlog.Warnf("Malformed chain response %s", err, connection.logid) connection.Exit() return diff --git a/p2p/handshake.go b/p2p/handshake.go index e4f3ec6..fdd7b80 100644 --- a/p2p/handshake.go +++ b/p2p/handshake.go @@ -181,7 +181,9 @@ func (connection *Connection) Handle_Handshake(buf []byte) { // parse delivered peer list as grey list rlog.Debugf("Peer provides %d peers", len(handshake.PeerList)) for i := range handshake.PeerList { - Peer_Add(&Peer{Address: handshake.PeerList[i].Addr}) + if i < 13 { + Peer_Add(&Peer{Address: handshake.PeerList[i].Addr}) + } } atomic.StoreUint32(&connection.State, ACTIVE) diff --git a/p2p/peer_pool.go b/p2p/peer_pool.go index 62e1439..e89253d 100644 --- a/p2p/peer_pool.go +++ b/p2p/peer_pool.go @@ -118,11 +118,17 @@ func clean_up() { peer_mutex.Lock() defer peer_mutex.Unlock() for k, v := range peer_map { - if v.FailCount >= 16 { // roughly 16 tries, 18 hrs before we discard the peer + if v.FailCount >= 8 { // roughly 16 tries, 18 hrs before we discard the peer delete(peer_map, k) } - } + if v.LastConnected == 0 { // if never connected, purge the peer + delete(peer_map, k) + } + if uint64(time.Now().UTC().Unix()) > ( v.LastConnected + 42000) { // purge all peers which were not connected in + delete(peer_map, k) + } + } } // check whether an IP is in the map already @@ -147,6 +153,7 @@ func GetPeerInList(address string) *Peer { // add connection to map func Peer_Add(p *Peer) { + clean_up(); peer_mutex.Lock() defer peer_mutex.Unlock()