diff --git a/SimplePolicy.UnitTests/UT_SimplePolicy.cs b/SimplePolicy.UnitTests/UT_SimplePolicy.cs index 550b5825b..f22d6aff9 100644 --- a/SimplePolicy.UnitTests/UT_SimplePolicy.cs +++ b/SimplePolicy.UnitTests/UT_SimplePolicy.cs @@ -3,6 +3,7 @@ using Neo.Plugins; using Neo.Network.P2P.Payloads; using Neo; +using Neo.Persistence; using Settings = Neo.Plugins.Settings; using System.Collections.Generic; using Neo.Cryptography; @@ -17,6 +18,8 @@ namespace SimplePolicy.UnitTests [TestClass] public class UT_SimplePolicy { + private static Random _random = new Random(11121990); + SimplePolicyPlugin uut; [TestInitialize] @@ -39,25 +42,36 @@ public void TestFilterForBlock_ClaimHasPriority() // Should contain "ClaimTransaction" in "HighPriorityTxType" Settings.Default.HighPriorityTxType.Contains(TransactionType.ClaimTransaction).Should().Be(true); - ClaimTransaction claimTxZero = GetClaimTransaction(0); - claimTxZero.Size.Should().Be(7); // 7 - ClaimTransaction claimTxOne = GetClaimTransaction(1); - claimTxOne.Size.Should().Be(41); // 34 + 7 - ClaimTransaction claimTxTwo = GetClaimTransaction(2); - claimTxTwo.Size.Should().Be(75); // 2*34 + 7 + ClaimTransaction claimTxZero1 = GetClaimTransaction(0); + claimTxZero1.Size.Should().Be(7 + 21); // 7 + 21 (random script) + claimTxZero1.Hash.ToString().Should().Be("0x60037520be0fd903703c2b67973296f22cac8932db07a2723addf79478aea75f"); + ClaimTransaction claimTxZero2 = GetClaimTransaction(0); + claimTxZero2.Size.Should().Be(7 + 21); // 7 + 21 (random script) + claimTxZero2.Hash.ToString().Should().Be("0xb29426673b3ef5c226bd35d53c2cb2242e09c06f0efe9c0d5be2034f41cb85ba"); + ClaimTransaction claimTxZero3 = GetClaimTransaction(0); + claimTxZero3.Size.Should().Be(7 + 21); // 7 + 21 (random script) + claimTxZero3.Hash.ToString().Should().Be("0x01027faead9a0538048db7ac5657172f6e2240bff3f7d902e490bb1bd75c2df7"); + + //ClaimTransaction claimTxTwo = GetClaimTransaction(2); + //claimTxTwo.Size.Should().Be(75 + 21); // 2*34 + 7 + 21 ClaimTransaction claimTx30 = GetClaimTransaction(30); - claimTx30.Size.Should().Be(1027); // 30*34 + 7 + claimTx30.Size.Should().Be(1027 + 21); // 30*34 + 7 + 21 claimTx30.NetworkFee.Should().Be(Fixed8.Zero); claimTx30.IsLowPriority.Should().Be(true); // by default is Low Priority, but plugin makes it High Priority //uut.IsLowPriority -> cannot inspect because it's private... no problem! List TxList = new List(); - TxList.Insert(0, claimTxZero); - TxList.Insert(0, claimTxOne); - TxList.Insert(0, claimTxTwo); + TxList.Insert(0, claimTxZero1); + TxList.Insert(0, claimTxZero2); + TxList.Insert(0, claimTxZero3); TxList.Insert(0, claimTx30); + //Console.WriteLine("Tx List Claim"); + //foreach(var tx in TxList) + // Console.WriteLine($"Claim TX fee: {tx.NetworkFee} size: {tx.Size} ratio: {tx.FeePerByte} hash: {tx.Hash}" ); + + // ======================== BEGIN TESTS ============================ // insert 100 paid invocation transactions @@ -118,6 +132,22 @@ public void TestFilterForBlock_ClaimHasPriority() // will still select Claim Transactions vx = filteredTxList.Where(tx => tx.Type == TransactionType.ClaimTransaction); vx.Count().Should().Be(2); + + // there are 3 tied Claim tx, will solve it based on smaller hash (0x01, 0x60) => 0xb2 is excluded + // 0x01027faead9a0538048db7ac5657172f6e2240bff3f7d902e490bb1bd75c2df7 + // 0x60037520be0fd903703c2b67973296f22cac8932db07a2723addf79478aea75f + // 0xb29426673b3ef5c226bd35d53c2cb2242e09c06f0efe9c0d5be2034f41cb85ba + vx = filteredTxList.Where(tx => tx.Hash.ToString() == "0x01027faead9a0538048db7ac5657172f6e2240bff3f7d902e490bb1bd75c2df7"); + vx.Count().Should().Be(1); + vx = filteredTxList.Where(tx => tx.Hash.ToString() == "0x60037520be0fd903703c2b67973296f22cac8932db07a2723addf79478aea75f"); + vx.Count().Should().Be(1); + vx = filteredTxList.Where(tx => tx.Hash.ToString() == "0xb29426673b3ef5c226bd35d53c2cb2242e09c06f0efe9c0d5be2034f41cb85ba"); + vx.Count().Should().Be(0); + + //Console.WriteLine("filtered"); + //foreach(var tx in filteredTxList) + // Console.WriteLine($"TX fee: {tx.NetworkFee} size: {tx.Size} ratio: {tx.FeePerByte} hash: {tx.Hash}" ); + } @@ -261,14 +291,30 @@ public void TestMock_GenerateInvocationTransaction() } // Generate Mock InvocationTransaction with different sizes and prices - public static Mock MockGenerateInvocationTransaction(Fixed8 networkFee, int size) + public static Mock MockGenerateInvocationTransaction(Fixed8 networkFee, int size) { - var mockTx = new Mock(TransactionType.InvocationTransaction); + var mockTx = new Mock(); mockTx.SetupGet(mr => mr.NetworkFee).Returns(networkFee); mockTx.SetupGet(mr => mr.Size).Returns(size); + + //============================== + //=== Generating random Hash === + mockTx.CallBase = true; + mockTx.Setup(p => p.Verify(It.IsAny(), It.IsAny>())).Returns(true); + var tx = mockTx.Object; + var randomBytes = new byte[16]; + _random.NextBytes(randomBytes); + tx.Script = randomBytes; + tx.Attributes = new TransactionAttribute[0]; + tx.Inputs = new CoinReference[0]; + tx.Outputs = new TransactionOutput[0]; + tx.Witnesses = new Witness[0]; + //============================== + return mockTx; } + // Create ClaimTransaction with 'countRefs' CoinReferences public static ClaimTransaction GetClaimTransaction(int countRefs) { @@ -278,10 +324,15 @@ public static ClaimTransaction GetClaimTransaction(int countRefs) refs[i] = GetCoinReference(new UInt256(Crypto.Default.Hash256(new BigInteger(i).ToByteArray()))); } + //============================== + //=== Generating random Hash === + var randomBytes = new byte[20]; + _random.NextBytes(randomBytes); + //============================== return new ClaimTransaction { Claims = refs, - Attributes = new TransactionAttribute[0], + Attributes = new TransactionAttribute[]{new TransactionAttribute{Usage = TransactionAttributeUsage.Script, Data = randomBytes} }, Inputs = new CoinReference[0], Outputs = new TransactionOutput[0], Witnesses = new Witness[0] diff --git a/SimplePolicy/SimplePolicyPlugin.cs b/SimplePolicy/SimplePolicyPlugin.cs index 7aec73d53..f6cfc487a 100644 --- a/SimplePolicy/SimplePolicyPlugin.cs +++ b/SimplePolicy/SimplePolicyPlugin.cs @@ -67,14 +67,14 @@ private static IEnumerable FilterForBlock_Policy2(IEnumerable p.NetworkFee / p.Size) .ThenByDescending(p => p.NetworkFee) .ThenByDescending(p => InHigherLowPriorityList(p)) - .ThenByAscending(p => p.Transaction.Hash) + .ThenBy(p => p.Hash) .Take(Settings.Default.MaxFreeTransactionsPerBlock) .ToArray(); Transaction[] non_free = tx_list.Where(p => !p.IsLowPriority) .OrderByDescending(p => p.NetworkFee / p.Size) .ThenByDescending(p => p.NetworkFee) - .ThenByAscending(p => p.Transaction.Hash) + .ThenBy(p => p.Hash) .Take(Settings.Default.MaxTransactionsPerBlock - free.Length - 1) .ToArray();