multi: switch to github.com/decred/dcrd/container/lru
What changed, and why it matters
This is a routine dependency update. The project switched from an older, deprecated Decred LRU cache package to a newer typed/generic version. The code behavior is intended to stay the same; no security vulnerability is visible in the change.
No security action required. Treat as normal maintenance; verify tests pass and that the new LRU package's eviction/Contains/Get semantics match the old package as claimed in the commit message.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit replaces github.com/decred/dcrd/lru with github.com/decred/dcrd/container/lru across six files. The old package provided untyped Cache/KVCache types; the new package provides generic Set and Map types. Call sites are updated accordingly: known inventory, sent nonces, known addresses, and v1 downgrade addresses become lru.Set
Changed components
go.mod dependency: github.com/decred/dcrd/lru → github.com/decred/dcrd/container/lruinternal/inbound/admission.go: v2SourceLimiters cachepeer/p2pdowngrader.go: downgrade address cachepeer/peer.go: knownInventory and sentNonces cachesserver.go: knownAddresses cacheInspect captured patch +28 / −26
### go.mod
@@ -13,7 +13,7 @@ require (
github.com/btcsuite/btcd/wire/v2 v2.0.1
github.com/btcsuite/btclog v1.0.0
github.com/davecgh/go-spew v1.1.1
- github.com/decred/dcrd/lru v1.1.3
+ github.com/decred/dcrd/container/lru v1.0.0
github.com/gorilla/websocket v1.5.3
github.com/jessevdk/go-flags v1.6.1
github.com/jrick/logrotate v1.1.2
### go.sum
@@ -24,12 +24,12 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/decred/dcrd/container/lru v1.0.0 h1:7foQymtbu18aQWYiY9RnNIeE+kvpiN+fiBQ3+viyJjI=
+github.com/decred/dcrd/container/lru v1.0.0/go.mod h1:vlPwj0l+IzAHhQSsbgQnJgO5Cte78+yI065V+Mc5PRQ=
github.com/decred/dcrd/crypto/blake256 v1.1.0 h1:zPMNGQCm0g4QTY27fOCorQW7EryeQ/U0x++OzVrdms8=
github.com/decred/dcrd/crypto/blake256 v1.1.0/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.1 h1:5RVFMOWjMyRy8cARdy79nAmgYw3hK/4HUq48LQ6Wwqo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.1/go.mod h1:ZXNYxsqcloTdSy/rNShjYzMhyjf0LaoftYK0p+A3h40=
-github.com/decred/dcrd/lru v1.1.3 h1:w9EAbvGLyzm6jTjF83UKuqZEiUtJmvRhQDOCEIvSuE0=
-github.com/decred/dcrd/lru v1.1.3/go.mod h1:Tw0i0pJyiLEx/oZdHLe1Wdv/Y7EGzAX+sYftnmxBR4o=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
### internal/inbound/admission.go
@@ -9,7 +9,7 @@ import (
"sync/atomic"
"time"
- "github.com/decred/dcrd/lru"
+ "github.com/decred/dcrd/container/lru"
"golang.org/x/time/rate"
)
@@ -89,7 +89,7 @@ type Admission struct {
now func() time.Time
v2SourceMu sync.Mutex
- v2SourceLimiters lru.KVCache
+ v2SourceLimiters *lru.Map[netip.Prefix, *rate.Limiter]
v2SourceRate rate.Limit
v2SourceBurst int
@@ -158,9 +158,11 @@ func newAdmission(cfg admissionConfig) *Admission {
v2Limiter: rate.NewLimiter(cfg.v2Rate, cfg.v2Burst),
v2Slots: make(chan struct{}, cfg.v2Concurrency),
now: cfg.now,
- v2SourceLimiters: lru.NewKVCache(cfg.v2SourceCacheSize),
- v2SourceRate: cfg.v2SourceRate,
- v2SourceBurst: cfg.v2SourceBurst,
+ v2SourceLimiters: lru.NewMap[netip.Prefix, *rate.Limiter](
+ uint32(cfg.v2SourceCacheSize),
+ ),
+ v2SourceRate: cfg.v2SourceRate,
+ v2SourceBurst: cfg.v2SourceBurst,
sourceLog: rate.Sometimes{
First: 3,
Interval: 30 * time.Second,
@@ -388,14 +390,14 @@ func (a *Admission) reserveV2Source(
) (*rate.Reservation, bool) {
a.v2SourceMu.Lock()
- value, ok := a.v2SourceLimiters.Lookup(prefix)
+ limiter, ok := a.v2SourceLimiters.Get(prefix)
if !ok {
- value = rate.NewLimiter(a.v2SourceRate, a.v2SourceBurst)
- a.v2SourceLimiters.Add(prefix, value)
+ limiter = rate.NewLimiter(a.v2SourceRate, a.v2SourceBurst)
+ a.v2SourceLimiters.Put(prefix, limiter)
}
a.v2SourceMu.Unlock()
- return reserveImmediate(value.(*rate.Limiter), now)
+ return reserveImmediate(limiter, now)
}
// logV2Rejection records and occasionally logs a rejected v2 handshake.
### peer/p2pdowngrader.go
@@ -1,7 +1,7 @@
package peer
import (
- "github.com/decred/dcrd/lru"
+ "github.com/decred/dcrd/container/lru"
)
const (
@@ -14,7 +14,7 @@ const (
// with the v1 P2P protocol on their next connection, typically after a v2
// handshake failure.
type P2PDowngrader struct {
- cache lru.Cache
+ cache *lru.Set[string]
}
// NewP2PDowngrader returns a new P2PDowngrader instance.
@@ -24,14 +24,14 @@ func NewP2PDowngrader(cacheSize uint) *P2PDowngrader {
cacheSize = defaultDowngradeCacheSize
}
return &P2PDowngrader{
- cache: lru.NewCache(cacheSize),
+ cache: lru.NewSet[string](uint32(cacheSize)),
}
}
// MarkForDowngrade flags an address so that the next outbound connection
// attempt to it will use the v1 P2P protocol.
func (pd *P2PDowngrader) MarkForDowngrade(addr string) {
- pd.cache.Add(addr)
+ pd.cache.Put(addr)
log.Debugf("P2PDowngrader: Marked %s for v1 downgrade", addr)
}
### peer/peer.go
@@ -26,7 +26,7 @@ import (
"github.com/btcsuite/btcd/v2transport"
"github.com/btcsuite/btcd/wire/v2"
"github.com/davecgh/go-spew/spew"
- "github.com/decred/dcrd/lru"
+ "github.com/decred/dcrd/container/lru"
)
const (
@@ -85,7 +85,7 @@ var (
// sentNonces houses the unique nonces that are generated when pushing
// version messages that are used to detect self connections.
- sentNonces = lru.NewCache(50)
+ sentNonces = lru.NewSet[uint64](50)
)
// MessageListeners defines callback function pointers to invoke with message
@@ -478,7 +478,7 @@ type Peer struct {
wireEncoding wire.MessageEncoding
- knownInventory lru.Cache
+ knownInventory *lru.Set[wire.InvVect]
prevGetBlocksMtx sync.Mutex
prevGetBlocksBegin *chainhash.Hash
prevGetBlocksStop *chainhash.Hash
@@ -549,7 +549,7 @@ func (p *Peer) UpdateLastAnnouncedBlock(blkHash *chainhash.Hash) {
//
// This function is safe for concurrent access.
func (p *Peer) AddKnownInventory(invVect *wire.InvVect) {
- p.knownInventory.Add(*invVect)
+ p.knownInventory.Put(*invVect)
}
// knowsInventory returns whether the peer is known to have the passed
@@ -2198,7 +2198,7 @@ func (p *Peer) localVersionMsg() (*wire.MsgVersion, error) {
// detected. This is accomplished by adding it to a size-limited map of
// recently seen nonces.
nonce := uint64(rand.Int63())
- sentNonces.Add(nonce)
+ sentNonces.Put(nonce)
// Version message.
msg := wire.NewMsgVersion(ourNA, theirNA, nonce, blockNum)
@@ -2567,7 +2567,7 @@ func newPeerBase(origCfg *Config, inbound bool) *Peer {
p := Peer{
inbound: inbound,
wireEncoding: wire.BaseEncoding,
- knownInventory: lru.NewCache(maxKnownInventory),
+ knownInventory: lru.NewSet[wire.InvVect](maxKnownInventory),
stallControl: make(chan stallControlMsg, 1), // nonblocking sync
outputQueue: make(chan outMsg, outputBufferSize),
sendQueue: make(chan outMsg, 1), // nonblocking sync
### server.go
@@ -39,7 +39,7 @@ import (
"github.com/btcsuite/btcd/peer"
"github.com/btcsuite/btcd/txscript/v2"
"github.com/btcsuite/btcd/wire/v2"
- "github.com/decred/dcrd/lru"
+ "github.com/decred/dcrd/container/lru"
)
const (
@@ -342,7 +342,7 @@ type serverPeer struct {
isWhitelisted bool
filter *bloom.Filter
addressesMtx sync.RWMutex
- knownAddresses lru.Cache
+ knownAddresses *lru.Set[string]
banScore connmgr.DynamicBanScore
quit chan struct{}
@@ -376,7 +376,7 @@ func newServerPeer(s *server, isPersistent bool) *serverPeer {
server: s,
persistent: isPersistent,
filter: bloom.LoadFilter(nil),
- knownAddresses: lru.NewCache(5000),
+ knownAddresses: lru.NewSet[string](5000),
quit: make(chan struct{}),
verAckCh: make(chan struct{}),
txProcessed: make(chan struct{}, 1),
@@ -405,7 +405,7 @@ func (sp *serverPeer) newestBlock() (*chainhash.Hash, int32, error) {
func (sp *serverPeer) addKnownAddresses(addresses []*wire.NetAddressV2) {
sp.addressesMtx.Lock()
for _, na := range addresses {
- sp.knownAddresses.Add(addrmgr.NetAddressKey(na))
+ sp.knownAddresses.Put(addrmgr.NetAddressKey(na))
}
sp.addressesMtx.Unlock()
}Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.