Skip to content

Commit

Permalink
Update txPool worst value if removal unsuccessful (#6702)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Feb 12, 2024
1 parent 045658f commit f262cb2
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/Nethermind/Nethermind.TxPool/Collections/SortedPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,9 @@ public virtual bool TryInsert(TKey key, TValue value, out TValue? removed)

if (_cacheMap.Count > _capacity)
{
RemoveLast(out removed);

// Self-recovery in case of exceeding collection's max capacity.
// If capacity is exceeded by more than 1 (1 is normal situation), then remove one more item.
// Every time when we will add 1 item, we will remove 2, until we will drop below max cap.
if (_cacheMap.Count > _capacity)
if (!RemoveLast(out removed) || _cacheMap.Count > _capacity)
{
UpdateWorstValue();
RemoveLast(out removed);
}

Expand All @@ -334,16 +330,17 @@ public virtual bool TryInsert(TKey key, TValue value, out TValue? removed)

public bool TryInsert(TKey key, TValue value) => TryInsert(key, value, out _);

private void RemoveLast(out TValue? removed)
private bool RemoveLast(out TValue? removed)
{
TKey? key = _worstValue.GetValueOrDefault().Value;
if (key is not null)
{
TryRemoveNonLocked(key, true, out removed, out _);
return TryRemoveNonLocked(key, true, out removed, out _);
}
else
{
removed = default;
return false;
}
}

Expand Down

0 comments on commit f262cb2

Please sign in to comment.