Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
Luchuan committed Nov 19, 2019
1 parent 9b28079 commit 6b9b849
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions neo.UnitTests/Ledger/UT_MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.Plugins;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using System;
using System.Collections;
Expand Down Expand Up @@ -92,6 +93,31 @@ private Transaction CreateTransactionWithFee(long fee)
return mock.Object;
}

private Transaction CreateTransactionWithFeeAndBalanceVerify(long fee)
{
Random random = new Random();
var randomBytes = new byte[16];
random.NextBytes(randomBytes);
Mock<Transaction> mock = new Mock<Transaction>();
UInt160 sender = UInt160.Zero;
mock.Setup(p => p.Reverify(It.IsAny<Snapshot>(), It.IsAny<BigInteger>())).Returns(((Snapshot snapshot, BigInteger amount) => NativeContract.GAS.BalanceOf(snapshot, sender) >= amount + fee));
mock.Setup(p => p.Verify(It.IsAny<Snapshot>(), It.IsAny<BigInteger>())).Returns(true);
mock.Object.Script = randomBytes;
mock.Object.Sender = sender;
mock.Object.NetworkFee = fee;
mock.Object.Attributes = new TransactionAttribute[0];
mock.Object.Cosigners = new Cosigner[0];
mock.Object.Witnesses = new[]
{
new Witness
{
InvocationScript = new byte[0],
VerificationScript = new byte[0]
}
};
return mock.Object;
}

private Transaction CreateTransaction(long fee = -1)
{
if (fee != -1)
Expand All @@ -115,6 +141,17 @@ private void AddTransaction(Transaction txToAdd)
_unit.TryAdd(txToAdd.Hash, txToAdd);
}

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

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

[TestMethod]
public void CapacityTest()
{
Expand Down Expand Up @@ -171,6 +208,36 @@ public void BlockPersistMovesTxToUnverifiedAndReverification()
_unit.UnverifiedSortedTxCount.Should().Be(0);
}

[TestMethod]
public void BlockPersistAndReverificationWillAbandonTxAsBalanceTransfered()
{
AddTransactionsWithBalanceVerify(70, 1);

_unit.SortedTxCount.Should().Be(70);

var block = new Block
{
Transactions = _unit.GetSortedVerifiedTransactions().Take(10).ToArray()
};

// Simulate the transfer process in tx by burning the balance
UInt160 sender = block.Transactions[0].Sender;
Snapshot snapshot = Blockchain.Singleton.GetSnapshot();
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, sender);
ApplicationEngine applicationEngine = new ApplicationEngine(TriggerType.All, block, snapshot, (long)balance);
NativeContract.GAS.Burn(applicationEngine, sender, balance);
snapshot.Commit();

// Persist block and reverify all the txs in mempool, but all the txs will be discarded
_unit.UpdatePoolForBlockPersisted(block, snapshot);
_unit.SortedTxCount.Should().Be(0);
_unit.UnverifiedSortedTxCount.Should().Be(0);

// Withdraw the GAS by minting gas to the sender
NativeContract.GAS.Mint(applicationEngine, sender, balance);
snapshot.Commit();
}

private void VerifyTransactionsSortedDescending(IEnumerable<Transaction> transactions)
{
Transaction lastTransaction = null;
Expand Down

0 comments on commit 6b9b849

Please sign in to comment.