diff --git a/go.mod b/go.mod index 01cae11fd..d1fdcd9d1 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/go-pg/pg/v10 v10.13.0 github.com/golang/glog v1.2.2 github.com/google/uuid v1.6.0 + github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/mitchellh/go-homedir v1.1.0 github.com/oleiade/lane v1.0.1 github.com/onflow/crypto v0.25.2 diff --git a/go.sum b/go.sum index 95bcc7be4..e80f4dabc 100644 --- a/go.sum +++ b/go.sum @@ -207,6 +207,8 @@ github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 h1:kes8mmyCpxJsI7FTwtzRqEy9 github.com/hashicorp/go-secure-stdlib/strutil v0.1.2/go.mod h1:Gou2R9+il93BqX25LAKCLuM+y9U2T4hlwvT1yprcna4= github.com/hashicorp/go-sockaddr v1.0.7 h1:G+pTkSO01HpR5qCxg7lxfsFEZaG+C0VssTy/9dbT+Fw= github.com/hashicorp/go-sockaddr v1.0.7/go.mod h1:FZQbEYa1pxkQ7WLpyXJ6cbjpT8q0YgQaK/JakXqGyWw= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/hcl v1.0.1-vault-5 h1:kI3hhbbyzr4dldA8UdTb7ZlVVlI2DACdCfz31RPDgJM= github.com/hashicorp/hcl v1.0.1-vault-5/go.mod h1:XYhtn6ijBSAj6n4YqAaf7RBPS4I06AItNorpy+MoQNM= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= diff --git a/lib/peer.go b/lib/peer.go index ed28ab5d0..9296db5bf 100644 --- a/lib/peer.go +++ b/lib/peer.go @@ -9,7 +9,7 @@ import ( "sync/atomic" "time" - "github.com/decred/dcrd/container/lru" + "github.com/hashicorp/golang-lru/v2" "github.com/btcsuite/btcd/wire" "github.com/golang/glog" @@ -111,7 +111,7 @@ type Peer struct { // Inventory stuff. // The inventory that we know the peer already has. - knownInventory *lru.Set[InvVect] + knownInventory *lru.Cache[InvVect, struct{}] // Whether the peer is ready to receive INV messages. For a peer that // still needs a mempool download, this is false. @@ -292,7 +292,7 @@ func (pp *Peer) HelpHandleInv(msg *MsgDeSoInv) { for _, invVect := range msg.InvList { // No matter what, add the inv to the peer's known inventory. - pp.knownInventory.Put(*invVect) + pp.knownInventory.Add(*invVect, struct{}{}) // If this is a hash we are currently processing, no need to do anything. // This check serves to fill the gap between the time when we've decided @@ -640,6 +640,8 @@ func NewPeer(_id uint64, _conn net.Conn, _isOutbound bool, _netAddr *wire.NetAdd _syncType NodeSyncType, peerDisconnectedChan chan *Peer) *Peer { + knownInventoryCache, _ := lru.New[InvVect, struct{}](maxKnownInventory) + pp := Peer{ ID: _id, cmgr: _cmgr, @@ -652,7 +654,7 @@ func NewPeer(_id uint64, _conn net.Conn, _isOutbound bool, _netAddr *wire.NetAdd outputQueueChan: make(chan DeSoMessage), peerDisconnectedChan: peerDisconnectedChan, quit: make(chan interface{}), - knownInventory: lru.NewSet[InvVect](maxKnownInventory), + knownInventory: knownInventoryCache, blocksToSend: make(map[BlockHash]bool), stallTimeoutSeconds: _stallTimeoutSeconds, minTxFeeRateNanosPerKB: _minFeeRateNanosPerKB, @@ -978,7 +980,7 @@ out: // Add the new inventory to the peer's knownInventory. for _, invVect := range invMsg.InvList { - pp.knownInventory.Put(*invVect) + pp.knownInventory.Add(*invVect, struct{}{}) } } @@ -1366,8 +1368,7 @@ func (pp *Peer) Disconnect(reason string) { close(pp.quit) // Free the cache of known inventory. - pp.knownInventory.Clear() - pp.knownInventory = nil + pp.knownInventory.Purge() // Add the Peer to donePeers so that the ConnectionManager and Server can do any // cleanup they need to do. diff --git a/lib/server.go b/lib/server.go index 16f2231ae..f5083a1b8 100644 --- a/lib/server.go +++ b/lib/server.go @@ -2012,7 +2012,7 @@ func (srv *Server) _relayTransactions() { // Add the transaction to the peer's known inventory. We do // it here when we enqueue the message to the peers outgoing // message queue so that we don't have remember to do it later. - pp.knownInventory.Put(*invVect) + pp.knownInventory.Add(*invVect, struct{}{}) invMsg.InvList = append(invMsg.InvList, invVect) } if len(invMsg.InvList) > 0 {