Skip to content

Commit

Permalink
fix: concurrency conflict in MemPool.TryRemoveUnVerified (#3500)
Browse files Browse the repository at this point in the history
* fix: concurrency conflict in MemPool.TryRemoveUnVerified

* Remove method

* Update src/Neo/Ledger/MemoryPool.cs

* clean

* Apply suggestions from code review

* reformat

---------

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: Jimmy <jinghui@wayne.edu>
  • Loading branch information
3 people authored Sep 30, 2024
1 parent 33d0913 commit 6e74e33
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,20 @@ private void RemoveConflictsOfVerified(PoolItem item)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool TryRemoveUnVerified(UInt256 hash, [MaybeNullWhen(false)] out PoolItem? item)
{
if (!_unverifiedTransactions.TryGetValue(hash, out item))
return false;
_txRwLock.EnterWriteLock();
try
{
if (!_unverifiedTransactions.TryGetValue(hash, out item))
return false;

_unverifiedTransactions.Remove(hash);
_unverifiedSortedTransactions.Remove(item);
return true;
_unverifiedTransactions.Remove(hash);
_unverifiedSortedTransactions.Remove(item);
return true;
}
finally
{
_txRwLock.ExitWriteLock();
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
19 changes: 19 additions & 0 deletions tests/Neo.UnitTests/Ledger/UT_MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,25 @@ public void TestUpdatePoolForBlockPersisted()
_unit.VerifiedCount.Should().Be(0);
}

[TestMethod]
public void TestTryRemoveUnVerified()
{
AddTransactions(32);
_unit.SortedTxCount.Should().Be(32);

var txs = _unit.GetSortedVerifiedTransactions().ToArray();
_unit.InvalidateVerifiedTransactions();

_unit.SortedTxCount.Should().Be(0);

foreach (var tx in txs)
{
_unit.TryRemoveUnVerified(tx.Hash, out _);
}

_unit.UnVerifiedCount.Should().Be(0);
}

public static StorageKey CreateStorageKey(int id, byte prefix, byte[] key = null)
{
byte[] buffer = GC.AllocateUninitializedArray<byte>(sizeof(byte) + (key?.Length ?? 0));
Expand Down

0 comments on commit 6e74e33

Please sign in to comment.