github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/cache/rcvcache.go (about) 1 package netcache 2 3 import ( 4 "github.com/rs/zerolog" 5 6 "github.com/onflow/flow-go/model/flow" 7 "github.com/onflow/flow-go/module" 8 herocache "github.com/onflow/flow-go/module/mempool/herocache/backdata" 9 "github.com/onflow/flow-go/module/mempool/herocache/backdata/heropool" 10 "github.com/onflow/flow-go/module/mempool/stdmap" 11 ) 12 13 // ReceiveCache implements an LRU cache of the received eventIDs that delivered to their engines 14 type ReceiveCache struct { 15 c *stdmap.Backend 16 } 17 18 // receiveCacheEntry represents an entry for the ReceiveCache 19 type receiveCacheEntry struct { 20 eventID flow.Identifier 21 } 22 23 func (r receiveCacheEntry) ID() flow.Identifier { 24 return r.eventID 25 } 26 27 func (r receiveCacheEntry) Checksum() flow.Identifier { 28 return r.eventID 29 } 30 31 // NewHeroReceiveCache returns a new HeroCache-based receive cache. 32 func NewHeroReceiveCache(sizeLimit uint32, logger zerolog.Logger, collector module.HeroCacheMetrics, 33 ) *ReceiveCache { 34 backData := herocache.NewCache(sizeLimit, 35 herocache.DefaultOversizeFactor, 36 heropool.LRUEjection, // receive cache must be LRU. 37 logger.With().Str("mempool", "receive-cache").Logger(), 38 collector) 39 backend := stdmap.NewBackend(stdmap.WithBackData(backData)) 40 return NewReceiveCache(uint(sizeLimit), func(cache *ReceiveCache) { 41 cache.c = backend 42 }) 43 } 44 45 // NewReceiveCache creates and returns a new ReceiveCache 46 func NewReceiveCache(sizeLimit uint, opts ...func(cache *ReceiveCache)) *ReceiveCache { 47 cache := &ReceiveCache{ 48 c: stdmap.NewBackend(stdmap.WithLimit(sizeLimit)), 49 } 50 51 for _, opt := range opts { 52 opt(cache) 53 } 54 55 return cache 56 } 57 58 // Add adds a new message to the cache if not already present. Returns true if the message is new and unseen, and false if message is duplicate, and 59 // already has been seen by the node. 60 func (r *ReceiveCache) Add(eventID []byte) bool { 61 return r.c.Add(receiveCacheEntry{eventID: flow.HashToID(eventID)}) // ignore eviction status 62 } 63 64 func (r *ReceiveCache) Size() uint { 65 return r.c.Size() 66 }