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

Fix ut #1759

Merged
merged 1 commit into from
Jul 9, 2020
Merged

Fix ut #1759

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 tests/neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class UT_Blockchain : TestKit
public void Initialize()
{
system = TestBlockchain.TheNeoSystem;
Blockchain.Singleton.MemPool.TryAdd(txSample.Hash, txSample);
Blockchain.Singleton.MemPool.TryAdd(txSample, Blockchain.Singleton.GetSnapshot());
}

[TestMethod]
Expand Down
39 changes: 27 additions & 12 deletions tests/neo.UnitTests/Ledger/UT_MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ public void TransactionsRemoved(MemoryPoolTxRemovalReason reason, IEnumerable<Tr
[TestClass]
public class UT_MemoryPool
{
private static NeoSystem testBlockchain;

private const byte Prefix_MaxTransactionsPerBlock = 23;
private const byte Prefix_FeePerByte = 10;
private MemoryPool _unit;
private MemoryPool _unit2;
private TestIMemoryPoolTxObserverPlugin plugin;

[ClassInitialize]
public static void TestSetup(TestContext ctx)
{
testBlockchain = TestBlockchain.TheNeoSystem;
}

[TestInitialize]
public void TestSetup()
{
Expand Down Expand Up @@ -98,7 +106,7 @@ private Transaction CreateTransactionWithFeeAndBalanceVerify(long fee)
random.NextBytes(randomBytes);
Mock<Transaction> mock = new Mock<Transaction>();
UInt160 sender = UInt160.Zero;
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>(), It.IsAny<TransactionVerificationContext>())).Returns((StoreView snapshot, BigInteger amount) => NativeContract.GAS.BalanceOf(snapshot, sender) >= amount + fee ? VerifyResult.Succeed : VerifyResult.InsufficientFunds);
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>(), It.IsAny<TransactionVerificationContext>())).Returns((StoreView snapshot, TransactionVerificationContext context) => context.CheckTransaction(mock.Object, snapshot) ? VerifyResult.Succeed : VerifyResult.InsufficientFunds);
mock.Setup(p => p.Verify(It.IsAny<StoreView>(), It.IsAny<TransactionVerificationContext>())).Returns(VerifyResult.Succeed);
mock.Object.Script = randomBytes;
mock.Object.Sender = sender;
Expand All @@ -124,26 +132,29 @@ private Transaction CreateTransaction(long fee = -1)

private void AddTransactions(int count)
{
var snapshot = Blockchain.Singleton.GetSnapshot();
for (int i = 0; i < count; i++)
{
var txToAdd = CreateTransaction();
_unit.TryAdd(txToAdd.Hash, txToAdd);
_unit.TryAdd(txToAdd, snapshot);
}

Console.WriteLine($"created {count} tx");
}

private void AddTransaction(Transaction txToAdd)
{
_unit.TryAdd(txToAdd.Hash, txToAdd);
var snapshot = Blockchain.Singleton.GetSnapshot();
_unit.TryAdd(txToAdd, snapshot);
}

private void AddTransactionsWithBalanceVerify(int count, long fee)
{
var snapshot = Blockchain.Singleton.GetSnapshot();
for (int i = 0; i < count; i++)
{
var txToAdd = CreateTransactionWithFeeAndBalanceVerify(fee);
_unit.TryAdd(txToAdd.Hash, txToAdd);
_unit.TryAdd(txToAdd, snapshot);
}

Console.WriteLine($"created {count} tx");
Expand Down Expand Up @@ -355,10 +366,11 @@ public void TestInvalidateAll()
[TestMethod]
public void TestContainsKey()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
AddTransactions(10);

var txToAdd = CreateTransaction();
_unit.TryAdd(txToAdd.Hash, txToAdd);
_unit.TryAdd(txToAdd, snapshot);
_unit.ContainsKey(txToAdd.Hash).Should().BeTrue();
_unit.InvalidateVerifiedTransactions();
_unit.ContainsKey(txToAdd.Hash).Should().BeTrue();
Expand Down Expand Up @@ -394,11 +406,12 @@ public void TestIEnumerableGetEnumerator()
[TestMethod]
public void TestGetVerifiedTransactions()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
var tx1 = CreateTransaction();
var tx2 = CreateTransaction();
_unit.TryAdd(tx1.Hash, tx1);
_unit.TryAdd(tx1, snapshot);
_unit.InvalidateVerifiedTransactions();
_unit.TryAdd(tx2.Hash, tx2);
_unit.TryAdd(tx2, snapshot);
IEnumerable<Transaction> enumerable = _unit.GetVerifiedTransactions();
enumerable.Count().Should().Be(1);
var enumerator = enumerable.GetEnumerator();
Expand Down Expand Up @@ -445,17 +458,19 @@ public void TestReVerifyTopUnverifiedTransactionsIfNeeded()
[TestMethod]
public void TestTryAdd()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
var tx1 = CreateTransaction();
_unit.TryAdd(tx1.Hash, tx1).Should().BeTrue();
_unit.TryAdd(tx1.Hash, tx1).Should().BeFalse();
_unit2.TryAdd(tx1.Hash, tx1).Should().BeFalse();
_unit.TryAdd(tx1, snapshot).Should().Be(VerifyResult.Succeed);
_unit.TryAdd(tx1, snapshot).Should().NotBe(VerifyResult.Succeed);
_unit2.TryAdd(tx1, snapshot).Should().NotBe(VerifyResult.Succeed);
}

[TestMethod]
public void TestTryGetValue()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
var tx1 = CreateTransaction();
_unit.TryAdd(tx1.Hash, tx1);
_unit.TryAdd(tx1, snapshot);
_unit.TryGetValue(tx1.Hash, out Transaction tx).Should().BeTrue();
tx.Should().BeEquivalentTo(tx1);

Expand Down Expand Up @@ -491,7 +506,7 @@ public void TestUpdatePoolForBlockPersisted()
var tx1 = CreateTransaction();
var tx2 = CreateTransaction();
Transaction[] transactions = { tx1, tx2 };
_unit.TryAdd(tx1.Hash, tx1);
_unit.TryAdd(tx1, snapshot);

var block = new Block { Transactions = transactions };

Expand Down
39 changes: 28 additions & 11 deletions tests/neo.UnitTests/Ledger/UT_TransactionVerificationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using System;
using System.Numerics;

namespace Neo.UnitTests.Ledger
{
[TestClass]
public class UT_TransactionVerificationContext
{
private static NeoSystem testBlockchain;

[ClassInitialize]
public static void TestSetup(TestContext ctx)
{
testBlockchain = TestBlockchain.TheNeoSystem;
}

private Transaction CreateTransactionWithFee(long networkFee, long systemFee)
{
Random random = new Random();
Expand All @@ -36,19 +47,25 @@ private Transaction CreateTransactionWithFee(long networkFee, long systemFee)
}

[TestMethod]
public void TestMemPoolSenderFee()
public void TestTransactionSenderFee()
{
Transaction transaction = CreateTransactionWithFee(1, 2);
var snapshot = Blockchain.Singleton.GetSnapshot();
ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, null, snapshot, long.MaxValue);
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero);
NativeContract.GAS.Burn(engine, UInt160.Zero, balance);
NativeContract.GAS.Mint(engine, UInt160.Zero, 8);

TransactionVerificationContext verificationContext = new TransactionVerificationContext();
verificationContext.GetSenderFee(transaction.Sender).Should().Be(0);
verificationContext.AddTransaction(transaction);
verificationContext.GetSenderFee(transaction.Sender).Should().Be(3);
verificationContext.AddTransaction(transaction);
verificationContext.GetSenderFee(transaction.Sender).Should().Be(6);
verificationContext.RemoveTransaction(transaction);
verificationContext.GetSenderFee(transaction.Sender).Should().Be(3);
verificationContext.RemoveTransaction(transaction);
verificationContext.GetSenderFee(transaction.Sender).Should().Be(0);
var tx = CreateTransactionWithFee(1, 2);
verificationContext.CheckTransaction(tx, snapshot).Should().BeTrue();
verificationContext.AddTransaction(tx);
verificationContext.CheckTransaction(tx, snapshot).Should().BeTrue();
verificationContext.AddTransaction(tx);
verificationContext.CheckTransaction(tx, snapshot).Should().BeFalse();
verificationContext.RemoveTransaction(tx);
verificationContext.CheckTransaction(tx, snapshot).Should().BeTrue();
verificationContext.AddTransaction(tx);
verificationContext.CheckTransaction(tx, snapshot).Should().BeFalse();
}
}
}
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ public void Transaction_Reverify_Hashes_Length_Unequal_To_Witnesses_Length()
};
UInt160[] hashes = txSimple.GetScriptHashesForVerifying(snapshot);
Assert.AreEqual(2, hashes.Length);
Assert.AreNotEqual(VerifyResult.Succeed, txSimple.VerifyForEachBlock(snapshot, BigInteger.Zero));
Assert.AreNotEqual(VerifyResult.Succeed, txSimple.VerifyForEachBlock(snapshot, new TransactionVerificationContext()));
}

[TestMethod]
Expand Down