Skip to content

Commit

Permalink
core: Remove redundant txLookup.Find and improve comments on txLook…
Browse files Browse the repository at this point in the history
…up methods.
  • Loading branch information
ryanschneider committed May 22, 2018
1 parent de60031 commit b01a68d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions core/tx_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func (l *txPricedList) Cap(threshold *big.Int, local *accountSet) types.Transact
for len(*l.items) > 0 {
// Discard stale transactions if found during cleanup
tx := heap.Pop(l.items).(*types.Transaction)
if _, ok := l.all.Find(tx.Hash()); !ok {
if l.all.Get(tx.Hash()) == nil {
l.stales--
continue
}
Expand Down Expand Up @@ -476,7 +476,7 @@ func (l *txPricedList) Underpriced(tx *types.Transaction, local *accountSet) boo
// Discard stale price points if found at the heap start
for len(*l.items) > 0 {
head := []*types.Transaction(*l.items)[0]
if _, ok := l.all.Find(head.Hash()); !ok {
if l.all.Get(head.Hash()) == nil {
l.stales--
heap.Pop(l.items)
continue
Expand All @@ -501,7 +501,7 @@ func (l *txPricedList) Discard(count int, local *accountSet) types.Transactions
for len(*l.items) > 0 && count > 0 {
// Discard stale transactions if found during cleanup
tx := heap.Pop(l.items).(*types.Transaction)
if _, ok := l.all.Find(tx.Hash()); !ok {
if l.all.Get(tx.Hash()) == nil {
l.stales--
continue
}
Expand Down
30 changes: 15 additions & 15 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,19 +1160,28 @@ func (as *accountSet) add(addr common.Address) {
as.accounts[addr] = struct{}{}
}

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

// newTxLookup returns a new txLookup structure.
func newTxLookup() *txLookup {
return &txLookup{
all: make(map[common.Hash]*types.Transaction),
}
}

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

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

return t.all[hash]
}

// returns a transaction if it exists in the lookup, and a bool indicating if it was found
func (t *txLookup) Find(hash common.Hash) (*types.Transaction, bool) {
t.lock.RLock()
defer t.lock.RUnlock()

value, ok := t.all[hash]
return value, ok
}

// returns the current number of items in the lookup
// Count returns the current number of items in the lookup.
func (t *txLookup) Count() int {
t.lock.RLock()
defer t.lock.RUnlock()

return len(t.all)
}

// add a transaction to the lookup
// Add adds a transaction to the lookup.
func (t *txLookup) Add(tx *types.Transaction) {
t.lock.Lock()
defer t.lock.Unlock()

t.all[tx.Hash()] = tx
}

// remove a transaction from the lookup
// Remove removes a transaction from the lookup.
func (t *txLookup) Remove(hash common.Hash) {
t.lock.Lock()
defer t.lock.Unlock()
Expand Down

0 comments on commit b01a68d

Please sign in to comment.