Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Aug 30, 2024
1 parent fc99c9c commit 1548baa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 40 deletions.
21 changes: 7 additions & 14 deletions types/mempool/priority_nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,13 @@ func (i *PriorityNonceIterator[C]) Tx() Tx {
//
// NOTE: It is not safe to use this iterator while removing transactions from
// the underlying mempool.
func (mp *PriorityNonceMempool[C]) Select(_ context.Context, _ [][]byte) Iterator {
func (mp *PriorityNonceMempool[C]) Select(ctx context.Context, txs [][]byte) Iterator {
mp.mtx.Lock()
defer mp.mtx.Unlock()
return mp.doSelect(ctx, txs)
}

func (mp *PriorityNonceMempool[C]) doSelect(_ context.Context, _ [][]byte) Iterator {
if mp.priorityIndex.Len() == 0 {
return nil
}
Expand All @@ -378,22 +382,11 @@ func (mp *PriorityNonceMempool[C]) Select(_ context.Context, _ [][]byte) Iterato
return iterator.iteratePriority()
}

func (mp *PriorityNonceMempool[C]) SelectBy(_ context.Context, _ [][]byte, callback func(Tx) bool) {
func (mp *PriorityNonceMempool[C]) SelectBy(ctx context.Context, txs [][]byte, callback func(Tx) bool) {
mp.mtx.Lock()
defer mp.mtx.Unlock()

if mp.priorityIndex.Len() == 0 {
return
}

mp.reorderPriorityTies()

iterator := &PriorityNonceIterator[C]{
mempool: mp,
senderCursors: make(map[string]*skiplist.Element),
}

iter := iterator.iteratePriority()
iter := mp.doSelect(ctx, txs)
for iter != nil && callback(iter.Tx()) {
iter = iter.Next()
}
Expand Down
33 changes: 7 additions & 26 deletions types/mempool/sender_nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,13 @@ func (snm *SenderNonceMempool) Insert(ctx context.Context, tx sdk.Tx) error {
//
// NOTE: It is not safe to use this iterator while removing transactions from
// the underlying mempool.
func (snm *SenderNonceMempool) Select(_ context.Context, _ [][]byte) Iterator {
func (snm *SenderNonceMempool) Select(ctx context.Context, txs [][]byte) Iterator {
snm.mtx.Lock()
defer snm.mtx.Unlock()
return snm.doSelect(ctx, txs)
}

func (snm *SenderNonceMempool) doSelect(_ context.Context, _ [][]byte) Iterator {
var senders []string

senderCursors := make(map[string]*skiplist.Element)
Expand Down Expand Up @@ -199,34 +203,11 @@ func (snm *SenderNonceMempool) Select(_ context.Context, _ [][]byte) Iterator {
return iter.Next()
}

func (snm *SenderNonceMempool) SelectBy(_ context.Context, _ [][]byte, callback func(Tx) bool) {
func (snm *SenderNonceMempool) SelectBy(ctx context.Context, txs [][]byte, callback func(Tx) bool) {
snm.mtx.Lock()
defer snm.mtx.Unlock()
var senders []string

senderCursors := make(map[string]*skiplist.Element)
orderedSenders := skiplist.New(skiplist.String)

// #nosec
for s := range snm.senders {
orderedSenders.Set(s, s)
}

s := orderedSenders.Front()
for s != nil {
sender := s.Value.(string)
senders = append(senders, sender)
senderCursors[sender] = snm.senders[sender].Front()
s = s.Next()
}

iterator := &senderNonceMempoolIterator{
senders: senders,
rnd: snm.rnd,
senderCursors: senderCursors,
}

iter := iterator.Next()
iter := snm.doSelect(ctx, txs)
for iter != nil && callback(iter.Tx()) {
iter = iter.Next()
}
Expand Down

0 comments on commit 1548baa

Please sign in to comment.