Skip to content

Commit

Permalink
fix bug of tx verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Zhang committed Nov 11, 2016
1 parent 080ae0e commit 6dbc415
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 32 deletions.
5 changes: 2 additions & 3 deletions src/AntShares/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ public ConsensusService(LocalNode localNode, Wallet wallet, string log_dictionar

private bool AddTransaction(Transaction tx)
{
if (context.Transactions.SelectMany(p => p.Value.GetAllInputs()).Intersect(tx.GetAllInputs()).Count() > 0 ||
Blockchain.Default.ContainsTransaction(tx.Hash) ||
!tx.Verify() ||
if (Blockchain.Default.ContainsTransaction(tx.Hash) ||
!tx.Verify(context.Transactions.Values) ||
!CheckPolicy(tx))
{
Log($"reject tx: {tx.Hash}{Environment.NewLine}{tx.ToArray().ToHexString()}");
Expand Down
4 changes: 2 additions & 2 deletions src/AntShares/Core/AgencyTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ protected override void SerializeExclusiveData(BinaryWriter writer)
/// 验证交易
/// </summary>
/// <returns>返回验证的结果</returns>
public override bool Verify()
public override bool Verify(IEnumerable<Transaction> mempool)
{
if (!base.Verify()) return false;
if (!base.Verify(mempool)) return false;
foreach (Order order in Orders)
if (!order.VerifySignature())
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/AntShares/Core/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public bool Verify(bool completely)
if (NextMiner != Blockchain.GetMinerAddress(Blockchain.Default.GetMiners(Transactions).ToArray()))
return false;
foreach (Transaction tx in Transactions)
if (!tx.Verify()) return false;
if (!tx.Verify(Transactions.Where(p => !p.Hash.Equals(tx.Hash)))) return false;
Transaction tx_gen = Transactions.FirstOrDefault(p => p.Type == TransactionType.MinerTransaction);
if (tx_gen?.Outputs.Sum(p => p.Value) != CalculateNetFee(Transactions)) return false;
}
Expand Down
6 changes: 4 additions & 2 deletions src/AntShares/Core/ClaimTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ public override JObject ToJson()
/// 验证交易
/// </summary>
/// <returns>返回验证结果</returns>
public override bool Verify()
public override bool Verify(IEnumerable<Transaction> mempool)
{
if (!base.Verify()) return false;
if (!base.Verify(mempool)) return false;
if (mempool.OfType<ClaimTransaction>().SelectMany(p => p.Claims).Intersect(Claims).Count() > 0)
return false;
TransactionResult result = GetTransactionResults().FirstOrDefault(p => p.AssetId == Blockchain.AntCoin.Hash);
if (result == null || result.Amount > Fixed8.Zero) return false;
try
Expand Down
15 changes: 4 additions & 11 deletions src/AntShares/Core/IssueTransaction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AntShares.Network;
using System;
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -52,14 +51,9 @@ public override UInt160[] GetScriptHashesForVerifying()
/// 验证交易
/// </summary>
/// <returns>返回验证后的结果</returns>
public override bool Verify()
public override bool Verify(IEnumerable<Transaction> mempool)
{
return Verify(false);
}

internal bool Verify(bool mempool)
{
if (!base.Verify()) return false;
if (!base.Verify(mempool)) return false;
TransactionResult[] results = GetTransactionResults()?.Where(p => p.Amount < Fixed8.Zero).ToArray();
if (results == null) return false;
foreach (TransactionResult r in results)
Expand All @@ -70,8 +64,7 @@ internal bool Verify(bool mempool)
if (!Blockchain.Default.Ability.HasFlag(BlockchainAbility.Statistics))
return false;
Fixed8 quantity_issued = Blockchain.Default.GetQuantityIssued(r.AssetId);
if (mempool)
quantity_issued += LocalNode.GetMemoryPool().OfType<IssueTransaction>().SelectMany(p => p.Outputs).Where(p => p.AssetId == r.AssetId).Sum(p => p.Value);
quantity_issued += mempool.OfType<IssueTransaction>().SelectMany(p => p.Outputs).Where(p => p.AssetId == r.AssetId).Sum(p => p.Value);
if (tx.Amount - quantity_issued < -r.Amount) return false;
}
return true;
Expand Down
9 changes: 8 additions & 1 deletion src/AntShares/Core/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,22 @@ public virtual JObject ToJson()
return json;
}

bool IInventory.Verify()
{
return Verify(Enumerable.Empty<Transaction>());
}

/// <summary>
/// 验证交易
/// </summary>
/// <returns>返回验证的结果</returns>
public virtual bool Verify()
public virtual bool Verify(IEnumerable<Transaction> mempool)
{
if (Blockchain.Default.ContainsTransaction(Hash)) return true;
if (!Blockchain.Default.Ability.HasFlag(BlockchainAbility.UnspentIndexes) || !Blockchain.Default.Ability.HasFlag(BlockchainAbility.TransactionIndexes))
return false;
if (mempool.SelectMany(p => p.GetAllInputs()).Intersect(GetAllInputs()).Count() > 0)
return false;
if (Blockchain.Default.IsDoubleSpend(this))
return false;
foreach (var group in Outputs.GroupBy(p => p.AssetId))
Expand Down
12 changes: 1 addition & 11 deletions src/AntShares/Network/LocalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,8 @@ private bool AddTransaction(Transaction tx)
lock (MemoryPool)
{
if (MemoryPool.ContainsKey(tx.Hash)) return false;
if (MemoryPool.Values.SelectMany(p => p.GetAllInputs()).Intersect(tx.GetAllInputs()).Count() > 0)
return false;
if (Blockchain.Default.ContainsTransaction(tx.Hash)) return false;
if (tx is IssueTransaction)
{
IssueTransaction issue = (IssueTransaction)tx;
if (!issue.Verify(true)) return false;
}
else
{
if (!tx.Verify()) return false;
}
if (!tx.Verify(MemoryPool.Values)) return false;
AddingTransactionEventArgs args = new AddingTransactionEventArgs(tx);
AddingTransaction?.Invoke(this, args);
if (!args.Cancel) MemoryPool.Add(tx.Hash, tx);
Expand Down
2 changes: 1 addition & 1 deletion src/AntShares/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"authors": [ "The Antshares team" ],
"copyright": "2015-2016 The Antshares team",
"title": "AntShares",
"version": "1.2.0",
"version": "1.3.1",
"buildOptions": {
"allowUnsafe": true,
"copyToOutput": {
Expand Down

0 comments on commit 6dbc415

Please sign in to comment.