Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

Commit

Permalink
P2P memory consuming bug fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainDero committed Jul 3, 2020
1 parent 0f2e589 commit 4dde5a1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion p2p/chain_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion p2p/chain_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion p2p/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions p2p/peer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down

1 comment on commit 4dde5a1

@kryptopwn
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

500 dero for the bug. Thank you Captain.

Please # to comment.