Skip to content

Commit

Permalink
Adding tests and fixing typo (#41)
Browse files Browse the repository at this point in the history
* Adding tests and fixing typo

* adding random attribute to claim

* unit tests for hash compare
  • Loading branch information
vncoelho committed Jan 27, 2019
1 parent bab9d09 commit 69782e9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
77 changes: 64 additions & 13 deletions SimplePolicy.UnitTests/UT_SimplePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +18,8 @@ namespace SimplePolicy.UnitTests
[TestClass]
public class UT_SimplePolicy
{
private static Random _random = new Random(11121990);

SimplePolicyPlugin uut;

[TestInitialize]
Expand All @@ -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<Transaction> TxList = new List<Transaction>();
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
Expand Down Expand Up @@ -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}" );

}


Expand Down Expand Up @@ -261,14 +291,30 @@ public void TestMock_GenerateInvocationTransaction()
}

// Generate Mock InvocationTransaction with different sizes and prices
public static Mock<Transaction> MockGenerateInvocationTransaction(Fixed8 networkFee, int size)
public static Mock<InvocationTransaction> MockGenerateInvocationTransaction(Fixed8 networkFee, int size)
{
var mockTx = new Mock<Transaction>(TransactionType.InvocationTransaction);
var mockTx = new Mock<InvocationTransaction>();
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<Snapshot>(), It.IsAny<IEnumerable<Transaction>>())).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)
{
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions SimplePolicy/SimplePolicyPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ private static IEnumerable<Transaction> FilterForBlock_Policy2(IEnumerable<Trans
.OrderByDescending(p => 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();

Expand Down

0 comments on commit 69782e9

Please sign in to comment.