Skip to content

Commit b01a68d

Browse files
committed
core: Remove redundant txLookup.Find and improve comments on txLookup methods.
1 parent de60031 commit b01a68d

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

core/tx_list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ func (l *txPricedList) Cap(threshold *big.Int, local *accountSet) types.Transact
444444
for len(*l.items) > 0 {
445445
// Discard stale transactions if found during cleanup
446446
tx := heap.Pop(l.items).(*types.Transaction)
447-
if _, ok := l.all.Find(tx.Hash()); !ok {
447+
if l.all.Get(tx.Hash()) == nil {
448448
l.stales--
449449
continue
450450
}
@@ -476,7 +476,7 @@ func (l *txPricedList) Underpriced(tx *types.Transaction, local *accountSet) boo
476476
// Discard stale price points if found at the heap start
477477
for len(*l.items) > 0 {
478478
head := []*types.Transaction(*l.items)[0]
479-
if _, ok := l.all.Find(head.Hash()); !ok {
479+
if l.all.Get(head.Hash()) == nil {
480480
l.stales--
481481
heap.Pop(l.items)
482482
continue
@@ -501,7 +501,7 @@ func (l *txPricedList) Discard(count int, local *accountSet) types.Transactions
501501
for len(*l.items) > 0 && count > 0 {
502502
// Discard stale transactions if found during cleanup
503503
tx := heap.Pop(l.items).(*types.Transaction)
504-
if _, ok := l.all.Find(tx.Hash()); !ok {
504+
if l.all.Get(tx.Hash()) == nil {
505505
l.stales--
506506
continue
507507
}

core/tx_pool.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,19 +1160,28 @@ func (as *accountSet) add(addr common.Address) {
11601160
as.accounts[addr] = struct{}{}
11611161
}
11621162

1163-
// txLookup is used to track all transactions to allow lookups without contention
1163+
// txLookup is used internally by TxPool to track transactions while allowing lookup without
1164+
// mutex contention.
1165+
//
1166+
// Note, although this type is properly protected against concurrent access, it
1167+
// is **not** a type that should ever be mutated or even exposed outside of the
1168+
// transaction pool, since its internal state is tightly coupled with the pools
1169+
// internal mechanisms. The sole purpose of the type is to permit out-of-bound
1170+
// peeking into the pool in TxPool.Get without having to acquire the widely scoped
1171+
// TxPool.mu mutex.
11641172
type txLookup struct {
11651173
all map[common.Hash]*types.Transaction
11661174
lock sync.RWMutex
11671175
}
11681176

1177+
// newTxLookup returns a new txLookup structure.
11691178
func newTxLookup() *txLookup {
11701179
return &txLookup{
11711180
all: make(map[common.Hash]*types.Transaction),
11721181
}
11731182
}
11741183

1175-
// calls f on each key and value present in the map
1184+
// Range calls f on each key and value present in the map.
11761185
func (t *txLookup) Range(f func(hash common.Hash, tx *types.Transaction) bool) {
11771186
t.lock.RLock()
11781187
defer t.lock.RUnlock()
@@ -1184,40 +1193,31 @@ func (t *txLookup) Range(f func(hash common.Hash, tx *types.Transaction) bool) {
11841193
}
11851194
}
11861195

1187-
// returns a transaction if it exists in the lookup, or nil if not found
1196+
// Get returns a transaction if it exists in the lookup, or nil if not found.
11881197
func (t *txLookup) Get(hash common.Hash) *types.Transaction {
11891198
t.lock.RLock()
11901199
defer t.lock.RUnlock()
11911200

11921201
return t.all[hash]
11931202
}
11941203

1195-
// returns a transaction if it exists in the lookup, and a bool indicating if it was found
1196-
func (t *txLookup) Find(hash common.Hash) (*types.Transaction, bool) {
1197-
t.lock.RLock()
1198-
defer t.lock.RUnlock()
1199-
1200-
value, ok := t.all[hash]
1201-
return value, ok
1202-
}
1203-
1204-
// returns the current number of items in the lookup
1204+
// Count returns the current number of items in the lookup.
12051205
func (t *txLookup) Count() int {
12061206
t.lock.RLock()
12071207
defer t.lock.RUnlock()
12081208

12091209
return len(t.all)
12101210
}
12111211

1212-
// add a transaction to the lookup
1212+
// Add adds a transaction to the lookup.
12131213
func (t *txLookup) Add(tx *types.Transaction) {
12141214
t.lock.Lock()
12151215
defer t.lock.Unlock()
12161216

12171217
t.all[tx.Hash()] = tx
12181218
}
12191219

1220-
// remove a transaction from the lookup
1220+
// Remove removes a transaction from the lookup.
12211221
func (t *txLookup) Remove(hash common.Hash) {
12221222
t.lock.Lock()
12231223
defer t.lock.Unlock()

0 commit comments

Comments
 (0)