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

Add ContainsTransaction Recheck when ParallelVerifiedTransaction Received #1408

Merged
merged 7 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Import { public IEnumerable<Block> Blocks; }
public class ImportCompleted { }
public class FillMemoryPool { public IEnumerable<Transaction> Transactions; }
public class FillCompleted { }
private class ParallelVerified { public Transaction Transaction; public bool ShouldRelay; public RelayResultReason VerifyResult; }
public class ParallelVerified { public Transaction Transaction; public bool ShouldRelay; public RelayResultReason VerifyResult; }
eryeer marked this conversation as resolved.
Show resolved Hide resolved

public static readonly uint MillisecondsPerBlock = ProtocolSettings.Default.MillisecondsPerBlock;
public const uint DecrementInterval = 2000000;
Expand Down Expand Up @@ -429,7 +429,9 @@ private void OnParallelVerified(ParallelVerified parallelVerified)
RelayResultReason reason = parallelVerified.VerifyResult;
if (reason == RelayResultReason.Succeed)
{
if (!MemPool.CanTransactionFitInPool(parallelVerified.Transaction))
if (ContainsTransaction(parallelVerified.Transaction.Hash))
vncoelho marked this conversation as resolved.
Show resolved Hide resolved
reason = RelayResultReason.AlreadyExists;
else if (!MemPool.CanTransactionFitInPool(parallelVerified.Transaction))
reason = RelayResultReason.OutOfMemory;
else if (!MemPool.TryAdd(parallelVerified.Transaction.Hash, parallelVerified.Transaction))
reason = RelayResultReason.OutOfMemory;
Expand Down
49 changes: 49 additions & 0 deletions tests/neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Neo.Wallets.NEP6;
using System.Linq;
using System.Reflection;
using static Neo.Ledger.Blockchain;

namespace Neo.UnitTests.Ledger
{
Expand Down Expand Up @@ -142,6 +143,54 @@ public void TestValidTransaction()
}
}

[TestMethod]
public void TestOnParallelVerified()
{
var senderProbe = CreateTestProbe();
var snapshot = Blockchain.Singleton.GetSnapshot();
var walletA = TestUtils.GenerateTestWallet();

using (var unlockA = walletA.Unlock("123"))
{
var acc = walletA.CreateAccount();

// Fake balance

var key = NativeContract.GAS.CreateStorageKey(20, acc.ScriptHash);
var entry = snapshot.Storages.GetAndChange(key, () => new StorageItem
{
Value = new Nep5AccountState().ToByteArray()
});

entry.Value = new Nep5AccountState()
{
Balance = 100_000_000 * NativeContract.GAS.Factor
}
.ToByteArray();

snapshot.Commit();

typeof(Blockchain)
.GetMethod("UpdateCurrentSnapshot", BindingFlags.Instance | BindingFlags.NonPublic)
.Invoke(Blockchain.Singleton, null);

// Make parallelVerified transaction

var tx = CreateValidTx(walletA, acc.ScriptHash, 0);
var parallelVerified = new ParallelVerified
{
Transaction = tx,
ShouldRelay = true,
VerifyResult = tx.VerifyParallelParts(snapshot)
};
senderProbe.Send(system.Blockchain, parallelVerified);
senderProbe.ExpectMsg(RelayResultReason.Succeed);

senderProbe.Send(system.Blockchain, parallelVerified);
senderProbe.ExpectMsg(RelayResultReason.AlreadyExists);
}
}

private Transaction CreateValidTx(NEP6Wallet wallet, UInt160 account, uint nonce)
{
var tx = wallet.MakeTransaction(new TransferOutput[]
Expand Down