Skip to content

Commit 3dd5a09

Browse files
mergify[bot]ninabarbakadzecmwaters
authored
fix: cat not purging txs from ordered cache (backport #2327) (#2344)
Fixes #2325 <hr>This is an automatic backport of pull request #2327 done by [Mergify](https://mergify.com). --------- Co-authored-by: nina / ნინა <[email protected]> Co-authored-by: Callum Waters <[email protected]>
1 parent 0f6a0e1 commit 3dd5a09

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

mempool/cat/store.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ func (s *store) purgeExpiredTxs(expirationHeight int64, expirationAge time.Time)
163163
for key, tx := range s.txs {
164164
if tx.height < expirationHeight || tx.timestamp.Before(expirationAge) {
165165
s.bytes -= tx.size()
166+
if err := s.deleteOrderedTx(tx); err != nil {
167+
panic(err)
168+
}
166169
delete(s.txs, key)
167170
purgedTxs = append(purgedTxs, tx)
168171
counter++
@@ -188,12 +191,16 @@ func (s *store) deleteOrderedTx(tx *wrappedTx) error {
188191
if len(s.orderedTxs) == 0 {
189192
return fmt.Errorf("ordered transactions list is empty")
190193
}
191-
idx := s.getTxOrder(tx) - 1
192-
if idx >= len(s.orderedTxs) || s.orderedTxs[idx] != tx {
193-
return fmt.Errorf("transaction %X not found in ordered list", tx.key())
194+
195+
// Find by direct iteration, binary search is not reliable after modification
196+
for i, orderedTx := range s.orderedTxs {
197+
if orderedTx == tx {
198+
s.orderedTxs = append(s.orderedTxs[:i], s.orderedTxs[i+1:]...)
199+
return nil
200+
}
194201
}
195-
s.orderedTxs = append(s.orderedTxs[:idx], s.orderedTxs[idx+1:]...)
196-
return nil
202+
203+
return fmt.Errorf("transaction %X not found in ordered list", tx.key())
197204
}
198205

199206
func (s *store) getTxOrder(tx *wrappedTx) int {

mempool/cat/store_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,14 @@ func TestStoreExpiredTxs(t *testing.T) {
259259
require.GreaterOrEqual(t, tx.height, int64(numTxs/2))
260260
}
261261

262+
// They should also be removed from orderedTxs
263+
for _, tx := range store.orderedTxs {
264+
require.GreaterOrEqual(t, tx.height, int64(numTxs/2))
265+
}
266+
262267
store.purgeExpiredTxs(int64(0), time.Now().Add(time.Second))
263268
require.Empty(t, store.getAllTxs())
269+
require.Empty(t, store.getOrderedTxs())
264270
}
265271

266272
func TestStoreGetOrderedTxs(t *testing.T) {

0 commit comments

Comments
 (0)