Skip to content

Commit

Permalink
Bound signed peer records
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Sep 14, 2024
1 parent d5a88cd commit f7be8ee
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions p2p/host/peerstore/pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
ma "github.com/multiformats/go-multiaddr"
)

var SignedPeerRecordBound = 1_000

var log = logging.Logger("peerstore")

type expiringAddr struct {
Expand Down Expand Up @@ -131,9 +133,8 @@ func (rc realclock) Now() time.Time {
// memoryAddrBook manages addresses.
type memoryAddrBook struct {
mu sync.RWMutex
// TODO bound this
addrs *peerAddrs
// TODO bound this
// TODO bound the number of not connected addresses we store.
addrs *peerAddrs
signedPeerRecords map[peer.ID]*peerRecordState

refCount sync.WaitGroup
Expand Down Expand Up @@ -227,6 +228,8 @@ func (mab *memoryAddrBook) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
mab.addAddrs(p, addrs, ttl)
}

var ErrTooManyRecords = fmt.Errorf("too many signed peer records. Dropping this one")

// ConsumePeerRecord adds addresses from a signed peer.PeerRecord (contained in
// a record.Envelope), which will expire after the given TTL.
// See https://godoc.org/github.com/libp2p/go-libp2p/core/peerstore#CertifiedAddrBook for more details.
Expand All @@ -246,6 +249,10 @@ func (mab *memoryAddrBook) ConsumePeerRecord(recordEnvelope *record.Envelope, tt
// ensure seq is greater than, or equal to, the last received
mab.mu.Lock()
defer mab.mu.Unlock()
if (len(mab.signedPeerRecords)) >= SignedPeerRecordBound {
return false, ErrTooManyRecords
}

lastState, found := mab.signedPeerRecords[rec.PeerID]
if found && lastState.Seq > rec.Seq {
return false, nil
Expand Down

0 comments on commit f7be8ee

Please sign in to comment.