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 5 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 (View.ContainsTransaction(parallelVerified.Transaction.Hash))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been check in L403.

Copy link
Contributor Author

@eryeer eryeer Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikzhang But it cannot guarantee that transactions are not duplicated after asynchronous verification, because during the asynchronous verification process, there may be blocks containing the transaction that are persisted to storage.

Copy link
Member

@vncoelho vncoelho Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that he mentioned that it checks but meanwhile since it not thread safe a Block can be Persisted during the Asynchronous loop. But I am not sure if I understood correctly.

Copy link
Member

@vncoelho vncoelho Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aehuahuea, we sent almost together, @eryeer.

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
46 changes: 46 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,51 @@ 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);
}
}

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