Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ public int Count
/// </summary>
public int UnVerifiedCount => _unverifiedTransactions.Count;

/// <summary>
/// Transaction policy validator function.
/// This function will be called to validate each transaction before adding it to the pool.
/// If the function returns false, the transaction will be rejected.
/// </summary>
public Func<Transaction, IReadOnlyStore, bool>? PolicyValidator { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="MemoryPool"/> class.
/// </summary>
Expand Down Expand Up @@ -307,6 +314,11 @@ internal bool CanTransactionFitInPool(Transaction tx)

internal VerifyResult TryAdd(Transaction tx, DataCache snapshot)
{
if (PolicyValidator != null)
{
if (!PolicyValidator(tx, snapshot)) return VerifyResult.PolicyFail;
}
Comment on lines +317 to +320
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (PolicyValidator != null)
{
if (!PolicyValidator(tx, snapshot)) return VerifyResult.PolicyFail;
}
if (PolicyValidator != null && !PolicyValidator(tx, snapshot)) return VerifyResult.PolicyFail;


var poolItem = new PoolItem(tx);

if (_unsortedTransactions.ContainsKey(tx.Hash)) return VerifyResult.AlreadyInPool;
Expand Down
16 changes: 16 additions & 0 deletions tests/Neo.UnitTests/Ledger/UT_MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
Expand Down Expand Up @@ -158,6 +159,21 @@ public void CapacityTest()
Assert.HasCount(100, _unit);
}

[TestMethod]
public void CancelTest()
{
// Add over the capacity items, verify that the verified count increases each time

_unit.PolicyValidator = new Func<Transaction, IReadOnlyStore, bool>((tx, sn) => false);
AddTransactions(1);
_unit.PolicyValidator = null;

Assert.AreEqual(0, _unit.SortedTxCount);
Assert.AreEqual(0, _unit.VerifiedCount);
Assert.AreEqual(0, _unit.UnVerifiedCount);
Assert.HasCount(0, _unit);
}

[TestMethod]
public void BlockPersistMovesTxToUnverifiedAndReverification()
{
Expand Down