Skip to content

Commit

Permalink
Add: add a new verify result status code (#3076)
Browse files Browse the repository at this point in the history
* add a new verify result status code

* Use AlreadyInPool

* More fixes

* Rename

* Rename

* fix rename ...

* Fix UT

---------

Co-authored-by: Fernando Diaz Toledano <shargon@gmail.com>
  • Loading branch information
Jim8y and shargon authored Jan 10, 2024
1 parent cfffe4f commit ff989a9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
20 changes: 20 additions & 0 deletions src/Neo/ContainsTransactionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// ContainsTransactionType.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

namespace Neo
{
public enum ContainsTransactionType
{
NotExist,
ExistsInPool,
ExistsInLedger
}
}
27 changes: 21 additions & 6 deletions src/Neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,11 @@ private VerifyResult OnNewExtensiblePayload(ExtensiblePayload payload)

private VerifyResult OnNewTransaction(Transaction transaction)
{
if (system.ContainsTransaction(transaction.Hash)) return VerifyResult.AlreadyExists;
switch (system.ContainsTransaction(transaction.Hash))
{
case ContainsTransactionType.ExistsInPool: return VerifyResult.AlreadyInPool;
case ContainsTransactionType.ExistsInLedger: return VerifyResult.AlreadyExists;
}
if (system.ContainsConflictHash(transaction.Hash, transaction.Signers.Select(s => s.Account))) return VerifyResult.HasConflicts;
return system.MemPool.TryAdd(transaction, system.StoreView);
}
Expand Down Expand Up @@ -393,11 +397,22 @@ protected override void OnReceive(object message)

private void OnTransaction(Transaction tx)
{
if (system.ContainsTransaction(tx.Hash))
SendRelayResult(tx, VerifyResult.AlreadyExists);
else if (system.ContainsConflictHash(tx.Hash, tx.Signers.Select(s => s.Account)))
SendRelayResult(tx, VerifyResult.HasConflicts);
else system.TxRouter.Forward(new TransactionRouter.Preverify(tx, true));
switch (system.ContainsTransaction(tx.Hash))
{
case ContainsTransactionType.ExistsInPool:
SendRelayResult(tx, VerifyResult.AlreadyInPool);
break;
case ContainsTransactionType.ExistsInLedger:
SendRelayResult(tx, VerifyResult.AlreadyExists);
break;
default:
{
if (system.ContainsConflictHash(tx.Hash, tx.Signers.Select(s => s.Account)))
SendRelayResult(tx, VerifyResult.HasConflicts);
else system.TxRouter.Forward(new TransactionRouter.Preverify(tx, true));
break;
}
}
}

private void Persist(Block block)
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ internal VerifyResult TryAdd(Transaction tx, DataCache snapshot)
{
var poolItem = new PoolItem(tx);

if (_unsortedTransactions.ContainsKey(tx.Hash)) return VerifyResult.AlreadyExists;
if (_unsortedTransactions.ContainsKey(tx.Hash)) return VerifyResult.AlreadyInPool;

List<Transaction> removedTransactions = null;
_txRwLock.EnterWriteLock();
Expand Down
5 changes: 5 additions & 0 deletions src/Neo/Ledger/VerifyResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public enum VerifyResult : byte
/// </summary>
AlreadyExists,

/// <summary>
/// Indicates that an <see cref="IInventory"/> with the same hash already exists in the memory pool.
/// </summary>
AlreadyInPool,

/// <summary>
/// Indicates that the <see cref="MemoryPool"/> is full and the transaction cannot be verified.
/// </summary>
Expand Down
7 changes: 4 additions & 3 deletions src/Neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,11 @@ public SnapshotCache GetSnapshot()
/// </summary>
/// <param name="hash">The hash of the transaction</param>
/// <returns><see langword="true"/> if the transaction exists; otherwise, <see langword="false"/>.</returns>
public bool ContainsTransaction(UInt256 hash)
public ContainsTransactionType ContainsTransaction(UInt256 hash)
{
if (MemPool.ContainsKey(hash)) return true;
return NativeContract.Ledger.ContainsTransaction(StoreView, hash);
if (MemPool.ContainsKey(hash)) return ContainsTransactionType.ExistsInPool;
return NativeContract.Ledger.ContainsTransaction(StoreView, hash) ?
ContainsTransactionType.ExistsInLedger : ContainsTransactionType.NotExist;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private void OnInventoryReceived(IInventory inventory)
switch (inventory)
{
case Transaction transaction:
if (!(system.ContainsTransaction(transaction.Hash) || system.ContainsConflictHash(transaction.Hash, transaction.Signers.Select(s => s.Account))))
if (!(system.ContainsTransaction(transaction.Hash) != ContainsTransactionType.NotExist || system.ContainsConflictHash(transaction.Hash, transaction.Signers.Select(s => s.Account))))
system.TxRouter.Tell(new TransactionRouter.Preverify(transaction, true));
break;
case Block block:
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void TestValidTransaction()
senderProbe.ExpectMsg<Blockchain.RelayResult>(p => p.Result == VerifyResult.Succeed);

senderProbe.Send(system.Blockchain, tx);
senderProbe.ExpectMsg<Blockchain.RelayResult>(p => p.Result == VerifyResult.AlreadyExists);
senderProbe.ExpectMsg<Blockchain.RelayResult>(p => p.Result == VerifyResult.AlreadyInPool);
}

internal static StorageKey CreateStorageKey(byte prefix, byte[] key = null)
Expand Down

0 comments on commit ff989a9

Please sign in to comment.