Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update from master branch #2

Merged
merged 2 commits into from
Feb 25, 2019
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dist: trusty
osx_image: xcode9.1

mono: none
dotnet: 2.0.0
dotnet: 2.1.502

before_install:
- cd neo.UnitTests
Expand Down
16 changes: 14 additions & 2 deletions neo.UnitTests/TestDataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ public class TestDataCache<TKey, TValue> : DataCache<TKey, TValue>
where TKey : IEquatable<TKey>, ISerializable
where TValue : class, ICloneable<TValue>, ISerializable, new()
{
private readonly TValue _defaultValue;

public TestDataCache()
{
_defaultValue = null;
}

public TestDataCache(TValue defaultValue)
{
this._defaultValue = defaultValue;
}
public override void DeleteInternal(TKey key)
{
}
Expand All @@ -25,12 +36,13 @@ protected override void AddInternal(TKey key, TValue value)

protected override TValue GetInternal(TKey key)
{
throw new NotImplementedException();
if (_defaultValue == null) throw new NotImplementedException();
return _defaultValue;
}

protected override TValue TryGetInternal(TKey key)
{
return null;
return _defaultValue;
}

protected override void UpdateInternal(TKey key, TValue value)
Expand Down
401 changes: 401 additions & 0 deletions neo.UnitTests/UT_MemoryPool.cs

Large diffs are not rendered by default.

178 changes: 178 additions & 0 deletions neo.UnitTests/UT_PoolItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Neo.Ledger;
using FluentAssertions;
using Neo.Network.P2P.Payloads;

namespace Neo.UnitTests
{
[TestClass]
public class UT_PoolItem
{
//private PoolItem uut;
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism

[TestInitialize]
public void TestSetup()
{
var timeValues = new[] {
new DateTime(1968, 06, 01, 0, 0, 1, DateTimeKind.Utc),
};

var timeMock = new Mock<TimeProvider>();
timeMock.SetupGet(tp => tp.UtcNow).Returns(() => timeValues[0])
.Callback(() => timeValues[0] = timeValues[0].Add(TimeSpan.FromSeconds(1)));
TimeProvider.Current = timeMock.Object;
}

[TestCleanup]
public void TestCleanup()
{
// important to leave TimeProvider correct
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()
{
int size1 = 50;
int netFeeSatoshi1 = 1;
var tx1 = MockGenerateInvocationTx(new Fixed8(netFeeSatoshi1), size1);
int size2 = 50;
int netFeeSatoshi2 = 2;
var tx2 = MockGenerateInvocationTx(new Fixed8(netFeeSatoshi2), size2);

PoolItem pitem1 = new PoolItem(tx1.Object);
PoolItem pitem2 = new PoolItem(tx2.Object);

Console.WriteLine($"item1 time {pitem1.Timestamp} item2 time {pitem2.Timestamp}");
// pitem1 < pitem2 (fee) => -1
pitem1.CompareTo(pitem2).Should().Be(-1);
// pitem2 > pitem1 (fee) => 1
pitem2.CompareTo(pitem1).Should().Be(1);
}

[TestMethod]
public void PoolItem_CompareTo_Hash()
{
int sizeFixed = 50;
int netFeeSatoshiFixed = 1;

for (int testRuns = 0; testRuns < 30; testRuns++)
{
var tx1 = GenerateMockTxWithFirstByteOfHashGreaterThanOrEqualTo(0x80, new Fixed8(netFeeSatoshiFixed), sizeFixed);
var tx2 = GenerateMockTxWithFirstByteOfHashLessThanOrEqualTo(0x79,new Fixed8(netFeeSatoshiFixed), sizeFixed);

PoolItem pitem1 = new PoolItem(tx1.Object);
PoolItem pitem2 = new PoolItem(tx2.Object);

// pitem2 < pitem1 (fee) => -1
pitem2.CompareTo(pitem1).Should().Be(-1);

// pitem1 > pitem2 (fee) => 1
pitem1.CompareTo(pitem2).Should().Be(1);
}

// equal hashes should be equal
var tx3 = MockGenerateInvocationTx(new Fixed8(netFeeSatoshiFixed), sizeFixed, new byte[] {0x13, 0x37});
var tx4 = MockGenerateInvocationTx(new Fixed8(netFeeSatoshiFixed), sizeFixed, new byte[] {0x13, 0x37});
PoolItem pitem3 = new PoolItem(tx3.Object);
PoolItem pitem4 = new PoolItem(tx4.Object);

pitem3.CompareTo(pitem4).Should().Be(0);
pitem4.CompareTo(pitem3).Should().Be(0);
}

[TestMethod]
public void PoolItem_CompareTo_Equals()
{
int sizeFixed = 500;
int netFeeSatoshiFixed = 10;
var tx1 = MockGenerateInvocationTx(new Fixed8(netFeeSatoshiFixed), sizeFixed, new byte[] {0x13, 0x37});
var tx2 = MockGenerateInvocationTx(new Fixed8(netFeeSatoshiFixed), sizeFixed, new byte[] {0x13, 0x37});

PoolItem pitem1 = new PoolItem(tx1.Object);
PoolItem pitem2 = new PoolItem(tx2.Object);

// pitem1 == pitem2 (fee) => 0
pitem1.CompareTo(pitem2).Should().Be(0);
pitem2.CompareTo(pitem1).Should().Be(0);
}

public Mock<InvocationTransaction> GenerateMockTxWithFirstByteOfHashGreaterThanOrEqualTo(byte firstHashByte, Fixed8 networkFee, int size)
{
Mock<InvocationTransaction> mockTx;
do
{
mockTx = MockGenerateInvocationTx(networkFee, size);
} while (mockTx.Object.Hash >= new UInt256(TestUtils.GetByteArray(32, firstHashByte)));

return mockTx;
}

public Mock<InvocationTransaction> GenerateMockTxWithFirstByteOfHashLessThanOrEqualTo(byte firstHashByte, Fixed8 networkFee, int size)
{
Mock<InvocationTransaction> mockTx;
do
{
mockTx = MockGenerateInvocationTx(networkFee, size);
} while (mockTx.Object.Hash <= new UInt256(TestUtils.GetByteArray(32, firstHashByte)));

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)
{
var mockTx = new Mock<InvocationTransaction>();
mockTx.CallBase = true;
mockTx.SetupGet(mr => mr.NetworkFee).Returns(networkFee);
mockTx.SetupGet(mr => mr.Size).Returns(size);

var tx = mockTx.Object;
// use random bytes in the script to get a different hash since we cannot mock the Hash
byte[] randomBytes;
if (overrideScriptBytes != null)
randomBytes = overrideScriptBytes;
else
{
randomBytes = new byte[16];
TestRandom.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;
}
}
}
Loading