From e5e369a5ec196c2f22e62872fbeba434ff6ed3fe Mon Sep 17 00:00:00 2001 From: Vitor Date: Sun, 27 Jan 2019 18:34:47 -0200 Subject: [PATCH 1/3] Adding tests and fixing typo --- SimplePolicy.UnitTests/UT_SimplePolicy.cs | 23 +++++++++++++++++++++-- SimplePolicy/SimplePolicyPlugin.cs | 4 ++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/SimplePolicy.UnitTests/UT_SimplePolicy.cs b/SimplePolicy.UnitTests/UT_SimplePolicy.cs index 550b5825b..fac613149 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] @@ -261,14 +264,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) { 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(); From c00bda94dcdbab0a8ab8573fb331398878884ab3 Mon Sep 17 00:00:00 2001 From: Vitor Date: Sun, 27 Jan 2019 18:54:03 -0200 Subject: [PATCH 2/3] adding random attribute to claim --- SimplePolicy.UnitTests/UT_SimplePolicy.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/SimplePolicy.UnitTests/UT_SimplePolicy.cs b/SimplePolicy.UnitTests/UT_SimplePolicy.cs index fac613149..4ffeb1a4c 100644 --- a/SimplePolicy.UnitTests/UT_SimplePolicy.cs +++ b/SimplePolicy.UnitTests/UT_SimplePolicy.cs @@ -43,14 +43,14 @@ public void TestFilterForBlock_ClaimHasPriority() Settings.Default.HighPriorityTxType.Contains(TransactionType.ClaimTransaction).Should().Be(true); ClaimTransaction claimTxZero = GetClaimTransaction(0); - claimTxZero.Size.Should().Be(7); // 7 + claimTxZero.Size.Should().Be(7 + 21); // 7 + 21 ClaimTransaction claimTxOne = GetClaimTransaction(1); - claimTxOne.Size.Should().Be(41); // 34 + 7 + claimTxOne.Size.Should().Be(41 + 21); // 34 + 7 + 21 ClaimTransaction claimTxTwo = GetClaimTransaction(2); - claimTxTwo.Size.Should().Be(75); // 2*34 + 7 + 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! @@ -297,10 +297,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] From 05a60b381308ab766b2e21f2e5fa7690ab8be418 Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Sun, 27 Jan 2019 19:23:05 -0200 Subject: [PATCH 3/3] unit tests for hash compare --- SimplePolicy.UnitTests/UT_SimplePolicy.cs | 45 ++++++++++++++++++----- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/SimplePolicy.UnitTests/UT_SimplePolicy.cs b/SimplePolicy.UnitTests/UT_SimplePolicy.cs index 4ffeb1a4c..f22d6aff9 100644 --- a/SimplePolicy.UnitTests/UT_SimplePolicy.cs +++ b/SimplePolicy.UnitTests/UT_SimplePolicy.cs @@ -42,12 +42,18 @@ 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 + 21); // 7 + 21 - ClaimTransaction claimTxOne = GetClaimTransaction(1); - claimTxOne.Size.Should().Be(41 + 21); // 34 + 7 + 21 - ClaimTransaction claimTxTwo = GetClaimTransaction(2); - claimTxTwo.Size.Should().Be(75 + 21); // 2*34 + 7 + 21 + 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 + 21); // 30*34 + 7 + 21 @@ -56,11 +62,16 @@ public void TestFilterForBlock_ClaimHasPriority() //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 @@ -121,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}" ); + }