Skip to content

Commit

Permalink
incorporate suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
cmwaters committed Jan 26, 2023
1 parent 6bc8c4c commit 08f8785
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
21 changes: 14 additions & 7 deletions mempool/cat/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
type LRUTxCache struct {
staticSize int

mtx tmsync.Mutex
mtx tmsync.Mutex
// cacheMap is used as a quick look up table
cacheMap map[types.TxKey]*list.Element
list *list.List
// list is a doubly linked list used to capture the FIFO nature of the cache
list *list.List
}

func NewLRUTxCache(cacheSize int) *LRUTxCache {
Expand Down Expand Up @@ -66,6 +68,10 @@ func (c *LRUTxCache) Push(txKey types.TxKey) bool {
}

func (c *LRUTxCache) Remove(txKey types.TxKey) {
if c.staticSize == 0 {
return
}

c.mtx.Lock()
defer c.mtx.Unlock()

Expand Down Expand Up @@ -107,7 +113,7 @@ type EvictedTxCache struct {
func NewEvictedTxCache(size int) *EvictedTxCache {
return &EvictedTxCache{
staticSize: size,
cache: make(map[types.TxKey]*EvictedTxInfo),
cache: make(map[types.TxKey]*EvictedTxInfo, size+1),
}
}

Expand Down Expand Up @@ -183,7 +189,7 @@ type SeenTxSet struct {
}

type timestampedPeerSet struct {
peers map[uint16]bool
peers map[uint16]struct{}
time time.Time
}

Expand All @@ -202,11 +208,11 @@ func (s *SeenTxSet) Add(txKey types.TxKey, peer uint16) {
seenSet, exists := s.set[txKey]
if !exists {
s.set[txKey] = timestampedPeerSet{
peers: map[uint16]bool{peer: true},
peers: map[uint16]struct{}{peer: struct{}{}},
time: time.Now().UTC(),
}
} else {
seenSet.peers[peer] = true
seenSet.peers[peer] = struct{}{}
}
}

Expand Down Expand Up @@ -259,7 +265,8 @@ func (s *SeenTxSet) Has(txKey types.TxKey, peer uint16) bool {
if !exists {
return false
}
return seenSet.peers[peer]
_, has := seenSet.peers[peer]
return has
}

func (s *SeenTxSet) Get(txKey types.TxKey) map[uint16]struct{} {
Expand Down
10 changes: 5 additions & 5 deletions mempool/cat/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestSeenTxSetConcurrency(t *testing.T) {
tx := types.Tx([]byte(fmt.Sprintf("tx%d", i)))
seenSet.Add(tx.Key(), peer)
}
}(uint16(i%2))
}(uint16(i % 2))
}
time.Sleep(time.Millisecond)
for i := 0; i < concurrency; i++ {
Expand All @@ -138,18 +138,18 @@ func TestSeenTxSetConcurrency(t *testing.T) {
tx := types.Tx([]byte(fmt.Sprintf("tx%d", i)))
seenSet.Has(tx.Key(), peer)
}
}(uint16(i%2))
}(uint16(i % 2))
}
time.Sleep(time.Millisecond)
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func(peer uint16) {
defer wg.Done()
for i := numTx-1; i >= 0; i-- {
for i := numTx - 1; i >= 0; i-- {
tx := types.Tx([]byte(fmt.Sprintf("tx%d", i)))
seenSet.RemoveKey(tx.Key())
}
}(uint16(i%2))
}(uint16(i % 2))
}
wg.Wait()
}
Expand All @@ -175,7 +175,7 @@ func TestLRUTxCacheConcurrency(t *testing.T) {
tx := types.Tx([]byte(fmt.Sprintf("tx%d", i)))
cache.Has(tx.Key())
}
for i := numTx-1; i >= 0; i-- {
for i := numTx - 1; i >= 0; i-- {
tx := types.Tx([]byte(fmt.Sprintf("tx%d", i)))
cache.Remove(tx.Key())
}
Expand Down
8 changes: 4 additions & 4 deletions mempool/cat/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"github.com/tendermint/tendermint/p2p"
)

const firstPeerID = 1
const firstPeerID = mempool.UnknownPeerID + 1

// mempoolIDs is a thread-safe map of peer IDs to short IDs used for tracking what peers have sent what
// NOTE: taken from mempool/v1/reactor.go
// mempoolIDs is a thread-safe map of peer IDs to shorter uint16 IDs used by the Reactor for tracking peer
// messages and peer state such as what transactions peers have seen
type mempoolIDs struct {
mtx tmsync.RWMutex
peerMap map[p2p.ID]uint16
peerMap map[p2p.ID]uint16 // quick lookup table for peer ID to short ID
nextID uint16 // assumes that a node will never have over 65536 active peers
activeIDs map[uint16]p2p.Peer // used to check if a given peerID key is used, the value doesn't matter
}
Expand Down

0 comments on commit 08f8785

Please sign in to comment.