Skip to content

Commit

Permalink
Treat Claim transactions as the highest low priority transactions. (n…
Browse files Browse the repository at this point in the history
  • Loading branch information
jsolman authored and rodoufu committed Mar 3, 2019
1 parent 688de1c commit 518a59e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
26 changes: 26 additions & 0 deletions neo.UnitTests/UT_PoolItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public void TestCleanup()
TimeProvider.ResetToDefault();
}

[TestMethod]
public void PoolItem_CompareTo_ClaimTx()
{
var tx1 = GenerateClaimTx();
// Non-free low-priority transaction
var tx2 = MockGenerateInvocationTx(new Fixed8(99999), 50).Object;

var poolItem1 = new PoolItem(tx1);
var poolItem2 = new PoolItem(tx2);
poolItem1.CompareTo(poolItem2).Should().Be(1);
poolItem2.CompareTo(poolItem1).Should().Be(-1);
}

[TestMethod]
public void PoolItem_CompareTo_Fee()
{
Expand Down Expand Up @@ -122,6 +135,19 @@ public Mock<InvocationTransaction> GenerateMockTxWithFirstByteOfHashLessThanOrEq
return mockTx;
}

public static Transaction GenerateClaimTx()
{
var mockTx = new Mock<ClaimTransaction>();
mockTx.CallBase = true;
mockTx.SetupGet(mr => mr.NetworkFee).Returns(Fixed8.Zero);
mockTx.SetupGet(mr => mr.Size).Returns(50);
var tx = mockTx.Object;
tx.Attributes = new TransactionAttribute[0];
tx.Inputs = new CoinReference[0];
tx.Outputs = new TransactionOutput[0];
tx.Witnesses = new Witness[0];
return mockTx.Object;
}

// Generate Mock InvocationTransaction with different sizes and prices
public static Mock<InvocationTransaction> MockGenerateInvocationTx(Fixed8 networkFee, int size, byte[] overrideScriptBytes=null)
Expand Down
25 changes: 14 additions & 11 deletions neo/Ledger/PoolItem.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
using Neo.Network.P2P.Payloads;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using Akka.Util.Internal;
using Neo.Network.P2P;
using Neo.Persistence;
using Neo.Plugins;

namespace Neo.Ledger
{
/// <summary>
/// Represents an item in the Memory Pool.
/// Represents an item in the Memory Pool.
///
// Note: PoolItem objects don't consider transaction priority (low or high) in their compare CompareTo method.
/// This is because items of differing priority are never added to the same sorted set in MemoryPool.
Expand All @@ -31,7 +22,7 @@ internal class PoolItem : IComparable<PoolItem>
public readonly DateTime Timestamp;

/// <summary>
/// Timestamp where this transaction was last broadcast to other nodes
/// Timestamp when this transaction was last broadcast to other nodes
/// </summary>
public DateTime LastBroadcastTimestamp;

Expand All @@ -45,6 +36,18 @@ internal PoolItem(Transaction tx)
public int CompareTo(Transaction otherTx)
{
if (otherTx == null) return 1;
if (Tx.IsLowPriority && otherTx.IsLowPriority)
{
bool thisIsClaimTx = Tx is ClaimTransaction;
bool otherIsClaimTx = otherTx is ClaimTransaction;
if (thisIsClaimTx != otherIsClaimTx)
{
// This is a claim Tx and other isn't.
if (thisIsClaimTx) return 1;
// The other is claim Tx and this isn't.
return -1;
}
}
// Fees sorted ascending
int ret = Tx.FeePerByte.CompareTo(otherTx.FeePerByte);
if (ret != 0) return ret;
Expand Down

0 comments on commit 518a59e

Please sign in to comment.