Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions p2p/conns.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,12 @@ func (c *Conns) PeekTxsWithHashes(hashes []common.Hash) ([]common.Hash, []*types
return c.txs.PeekManyWithKeys(hashes)
}

// FilterUnknownTxHashes returns only the hashes that are NOT in the transaction cache.
// Uses a single read lock for efficient batch checking.
func (c *Conns) FilterUnknownTxHashes(hashes []common.Hash) []common.Hash {
return c.txs.FilterMissing(hashes)
}

// Blocks returns the global blocks cache.
func (c *Conns) Blocks() *ds.LRU[common.Hash, BlockCache] {
return c.blocks
Expand Down
29 changes: 29 additions & 0 deletions p2p/datastructures/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,35 @@ func (c *LRU[K, V]) PeekManyWithKeys(keys []K) ([]K, []V) {
return foundKeys, foundValues
}

// FilterMissing returns keys that are NOT present in the cache (or are expired).
// Uses a single read lock for all lookups.
func (c *LRU[K, V]) FilterMissing(keys []K) []K {
if len(keys) == 0 {
return nil
}

c.mu.RLock()
defer c.mu.RUnlock()

now := time.Now()
missing := make([]K, 0, len(keys))

for _, key := range keys {
elem, ok := c.items[key]
if !ok {
missing = append(missing, key)
continue
}

e := elem.Value.(*entry[K, V])
if e.expiresAt != nil && now.After(*e.expiresAt) {
missing = append(missing, key)
}
}

return missing
}

// Update atomically updates a value in the cache using the provided update function.
// The update function receives the current value (or zero value if not found) and
// returns the new value to store. Returns true if the key already existed (and was
Expand Down
10 changes: 8 additions & 2 deletions p2p/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -1251,8 +1251,14 @@ func (c *conn) handleNewPooledTransactionHashes(version uint, msg ethp2p.Msg) er
return nil
}

request := &eth.GetPooledTransactionsPacket{GetPooledTransactionsRequest: hashes}
c.countMsgSent(request.Name(), float64(len(hashes)))
// Filter out transactions we already have in cache to avoid redundant fetches.
unknown := c.conns.FilterUnknownTxHashes(hashes)
if len(unknown) == 0 {
return nil
}

request := &eth.GetPooledTransactionsPacket{GetPooledTransactionsRequest: unknown}
c.countMsgSent(request.Name(), float64(len(unknown)))
return ethp2p.Send(c.rw, eth.GetPooledTransactionsMsg, request)
}

Expand Down