From 6b9b8498749d88f965cfcbd9a739d914097f8461 Mon Sep 17 00:00:00 2001 From: Luchuan Date: Tue, 19 Nov 2019 17:07:57 +0800 Subject: [PATCH] add ut --- neo.UnitTests/Ledger/UT_MemoryPool.cs | 67 +++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/neo.UnitTests/Ledger/UT_MemoryPool.cs b/neo.UnitTests/Ledger/UT_MemoryPool.cs index 6f9022bef1..33ee665eed 100644 --- a/neo.UnitTests/Ledger/UT_MemoryPool.cs +++ b/neo.UnitTests/Ledger/UT_MemoryPool.cs @@ -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; @@ -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 mock = new Mock(); + UInt160 sender = UInt160.Zero; + mock.Setup(p => p.Reverify(It.IsAny(), It.IsAny())).Returns(((Snapshot snapshot, BigInteger amount) => NativeContract.GAS.BalanceOf(snapshot, sender) >= amount + fee)); + mock.Setup(p => p.Verify(It.IsAny(), It.IsAny())).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) @@ -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() { @@ -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 transactions) { Transaction lastTransaction = null;