Skip to content

Commit

Permalink
[master-2.x] Add try-catch when receive a new tx (#2066)
Browse files Browse the repository at this point in the history
* fix tx.verify

* format

* fix

* catch tx.verify exception to avoid state state influence

* add RelayResultReason.Error

Co-authored-by: Tommo-L <luchuan@neo.org>
Co-authored-by: Shargon <shargon@gmail.com>
  • Loading branch information
3 people authored Nov 20, 2020
1 parent 05c02ef commit 6e7c32a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
32 changes: 19 additions & 13 deletions neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,26 @@ private void OnNewHeaders(Header[] headers)

private RelayResultReason OnNewTransaction(Transaction transaction)
{
if (transaction.Type == TransactionType.MinerTransaction)
return RelayResultReason.Invalid;
if (ContainsTransaction(transaction.Hash))
return RelayResultReason.AlreadyExists;
if (!MemPool.CanTransactionFitInPool(transaction))
return RelayResultReason.OutOfMemory;
if (!transaction.Verify(currentSnapshot, MemPool.GetVerifiedTransactions()))
return RelayResultReason.Invalid;
if (!Plugin.CheckPolicy(transaction))
return RelayResultReason.PolicyFail;

if (!MemPool.TryAdd(transaction.Hash, transaction))
return RelayResultReason.OutOfMemory;
try
{
if (transaction.Type == TransactionType.MinerTransaction)
return RelayResultReason.Invalid;
if (ContainsTransaction(transaction.Hash))
return RelayResultReason.AlreadyExists;
if (!MemPool.CanTransactionFitInPool(transaction))
return RelayResultReason.OutOfMemory;
if (!transaction.Verify(currentSnapshot, MemPool.GetVerifiedTransactions()))
return RelayResultReason.Invalid;
if (!Plugin.CheckPolicy(transaction))
return RelayResultReason.PolicyFail;

if (!MemPool.TryAdd(transaction.Hash, transaction))
return RelayResultReason.OutOfMemory;
}
catch
{
return RelayResultReason.Error;
}
system.LocalNode.Tell(new LocalNode.RelayDirectly { Inventory = transaction });
return RelayResultReason.Succeed;
}
Expand Down
3 changes: 2 additions & 1 deletion neo/Ledger/RelayResultReason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum RelayResultReason : byte
UnableToVerify,
Invalid,
PolicyFail,
Unknown
Unknown,
Error
}
}

0 comments on commit 6e7c32a

Please sign in to comment.