Skip to content

Commit e48501f

Browse files
committed
Without event
1 parent eede491 commit e48501f

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/Neo/Ledger/MemoryPool.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using System;
1616
using System.Collections;
1717
using System.Collections.Generic;
18-
using System.ComponentModel;
1918
using System.Diagnostics.CodeAnalysis;
2019
using System.Linq;
2120
using System.Runtime.CompilerServices;
@@ -28,7 +27,6 @@ namespace Neo.Ledger
2827
/// </summary>
2928
public class MemoryPool : IReadOnlyCollection<Transaction>
3029
{
31-
public event CancelEventHandler? TransactionNew;
3230
public event EventHandler<Transaction>? TransactionAdded;
3331
public event EventHandler<TransactionRemovedEventArgs>? TransactionRemoved;
3432

@@ -119,6 +117,13 @@ public int Count
119117
/// </summary>
120118
public int UnVerifiedCount => _unverifiedTransactions.Count;
121119

120+
/// <summary>
121+
/// Transaction policy validator function.
122+
/// This function will be called to validate each transaction before adding it to the pool.
123+
/// If the function returns false, the transaction will be rejected.
124+
/// </summary>
125+
public Func<Transaction, IReadOnlyStore, bool>? PolicyValidator { get; set; }
126+
122127
/// <summary>
123128
/// Initializes a new instance of the <see cref="MemoryPool"/> class.
124129
/// </summary>
@@ -309,11 +314,9 @@ internal bool CanTransactionFitInPool(Transaction tx)
309314

310315
internal VerifyResult TryAdd(Transaction tx, DataCache snapshot)
311316
{
312-
if (TransactionNew != null)
317+
if (PolicyValidator != null)
313318
{
314-
var args = new CancelEventArgs();
315-
TransactionNew.Invoke(this, args);
316-
if (args.Cancel) return VerifyResult.PolicyFail;
319+
if (!PolicyValidator(tx, snapshot)) return VerifyResult.PolicyFail;
317320
}
318321

319322
var poolItem = new PoolItem(tx);

tests/Neo.UnitTests/Ledger/UT_MemoryPool.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,9 @@ public void CancelTest()
164164
{
165165
// Add over the capacity items, verify that the verified count increases each time
166166

167-
var cancel = new CancelEventHandler((o, c) => { c.Cancel = true; });
168-
_unit.TransactionNew += cancel;
167+
_unit.PolicyValidator = new Func<Transaction, IReadOnlyStore, bool>((tx, sn) => false);
169168
AddTransactions(1);
170-
_unit.TransactionNew -= cancel;
169+
_unit.PolicyValidator = null;
171170

172171
Assert.AreEqual(0, _unit.SortedTxCount);
173172
Assert.AreEqual(0, _unit.VerifiedCount);

0 commit comments

Comments
 (0)