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 1244 Mempool.ReverifyTransactions #1248

Merged
merged 35 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
93060a2
Merge pull request #1 from neo-project/master
Tommo-L Jun 4, 2019
f4371d3
Merge pull request #3 from neo-project/master
Tommo-L Jun 20, 2019
5784ac5
Merge pull request #5 from neo-project/master
Tommo-L Jun 28, 2019
d8ab5e2
Merge pull request #6 from neo-project/master
Tommo-L Jul 8, 2019
9a1f07a
Merge pull request #7 from neo-project/master
Tommo-L Jul 16, 2019
ee20bc9
add internal to DB and WriteBatch
eryeer Jul 16, 2019
4d873e2
Merge branch 'master' into master
eryeer Jul 17, 2019
81dd495
Merge branch 'master' into master
lock9 Jul 18, 2019
5518fbd
Merge branch 'master' into master
eryeer Jul 22, 2019
60a1845
Merge pull request #8 from neo-project/master
Tommo-L Sep 23, 2019
97c2f34
Merge pull request #9 from neo-project/master
Tommo-L Sep 26, 2019
639c739
Merge remote-tracking branch 'upstream/master'
Sep 27, 2019
6376be1
Merge remote-tracking branch 'upstream/master'
Oct 9, 2019
b76b04d
reset db.cs writebatch.cs
Oct 12, 2019
a34254b
Merge remote-tracking branch 'upstream/master'
Oct 18, 2019
aee96f8
Merge remote-tracking branch 'upstream/master'
Oct 22, 2019
6636ce8
Merge remote-tracking branch 'upstream/master'
Oct 25, 2019
b0a4165
Merge remote-tracking branch 'upstream/master'
Nov 4, 2019
298c7da
Merge remote-tracking branch 'upstream/master'
Nov 8, 2019
4dd37bb
Merge remote-tracking branch 'upstream/master'
hope2028 Nov 11, 2019
a156012
Merge remote-tracking branch 'upstream/master'
Nov 13, 2019
59432fc
Merge remote-tracking branch 'upstream/master'
Nov 15, 2019
1c743e0
fix Nep5Token.Burn method
Nov 15, 2019
bb9a379
format
Nov 15, 2019
43985bd
format
Nov 15, 2019
c18c09d
reset and fix memorypool.ReverifyTransactions
Nov 18, 2019
22c87f2
Merge branch 'fix_1244_sysfee' of https://github.com/Tommo-L/neo into…
Nov 18, 2019
922c7a4
reset and fix ReverifyTransactions
Nov 18, 2019
9b28079
Merge branch 'master' into fix_1244_sysfee
Nov 18, 2019
6b9b849
add ut
Nov 19, 2019
c1bec9e
remove commit
Nov 19, 2019
6b30c05
fix BlockPersistAndReverificationWillAbandonTxAsBalanceTransfered.bal…
Nov 20, 2019
e8fd7b9
update comments
Nov 20, 2019
d15781d
Merge branch 'master' into fix_1244_sysfee
Nov 20, 2019
ea3b18d
Merge branch 'master' into fix_1244_sysfee
shargon Nov 21, 2019
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
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()
Tommo-L marked this conversation as resolved.
Show resolved Hide resolved
{
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();
Tommo-L marked this conversation as resolved.
Show resolved Hide resolved
}

private void VerifyTransactionsSortedDescending(IEnumerable<Transaction> transactions)
{
Transaction lastTransaction = null;
Expand Down
6 changes: 5 additions & 1 deletion neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,10 @@ internal void InvalidateAllTransactions()
foreach (PoolItem item in unverifiedSortedTxPool.Reverse().Take(count))
{
if (item.Tx.Reverify(snapshot, SendersFeeMonitor.GetSenderFee(item.Tx.Sender)))
{
reverifiedItems.Add(item);
SendersFeeMonitor.AddSenderFee(item.Tx);
}
else // Transaction no longer valid -- it will be removed from unverifiedTxPool.
invalidItems.Add(item);

Expand All @@ -438,7 +441,6 @@ internal void InvalidateAllTransactions()
{
if (_unsortedTransactions.TryAdd(item.Tx.Hash, item))
{
SendersFeeMonitor.AddSenderFee(item.Tx);
verifiedSortedTxPool.Add(item);

if (item.LastBroadcastTimestamp < rebroadcastCutOffTime)
Expand All @@ -447,6 +449,8 @@ internal void InvalidateAllTransactions()
item.LastBroadcastTimestamp = DateTime.UtcNow;
}
}
else
SendersFeeMonitor.RemoveSenderFee(item.Tx);

_unverifiedTransactions.Remove(item.Tx.Hash);
unverifiedSortedTxPool.Remove(item);
Expand Down