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

[master-2.x] Fix Transaction verify #2009

Merged
merged 2 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions neo.UnitTests/TestBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static NeoSystem InitializeMockNeoSystem()
mockStore.Setup(p => p.GetContracts()).Returns(new TestDataCache<UInt160, ContractState>());
mockStore.Setup(p => p.GetStorages()).Returns(new TestDataCache<StorageKey, StorageItem>());
mockStore.Setup(p => p.GetHeaderHashList()).Returns(new TestDataCache<UInt32Wrapper, HeaderHashList>());
mockStore.Setup(p => p.GetStateRoots()).Returns(new TestDataCache<UInt32Wrapper, StateRootState>());
mockStore.Setup(p => p.GetValidatorsCount()).Returns(new TestMetaDataCache<ValidatorsCountState>());
mockStore.Setup(p => p.GetBlockHashIndex()).Returns(new TestMetaDataCache<HashIndexState>());
mockStore.Setup(p => p.GetHeaderHashIndex()).Returns(new TestMetaDataCache<HashIndexState>());
Expand Down
9 changes: 1 addition & 8 deletions neo/Network/P2P/Payloads/InvocationTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,7 @@ public override JObject ToJson()
public override bool Verify(Snapshot snapshot, IEnumerable<Transaction> mempool)
{
if (Gas.GetData() % 100000000 != 0) return false;
try
{
if (References is null || NetworkFee < ProtocolSettings.Default.MinimumNetworkFee) return false;
}
catch (IndexOutOfRangeException)
{
return false;
}
if (References is null || NetworkFee < ProtocolSettings.Default.MinimumNetworkFee) return false;
return base.Verify(snapshot, mempool);
}
}
Expand Down
107 changes: 51 additions & 56 deletions neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,26 @@ public virtual Fixed8 NetworkFee
}

private IReadOnlyDictionary<CoinReference, TransactionOutput> _references;
private bool hasCalculatedReferences = false;
public IReadOnlyDictionary<CoinReference, TransactionOutput> References
{
get
{
if (_references == null)
if (!hasCalculatedReferences && _references == null)
{
hasCalculatedReferences = true;
Dictionary<CoinReference, TransactionOutput> dictionary = new Dictionary<CoinReference, TransactionOutput>();
foreach (var group in Inputs.GroupBy(p => p.PrevHash))
{
Transaction tx = Blockchain.Singleton.Store.GetTransaction(group.Key);
if (tx == null) return null;
foreach (var reference in group.Select(p => new
foreach (var reference in group)
{
Input = p,
Output = tx.Outputs[p.PrevIndex]
}))
{
dictionary.Add(reference.Input, reference.Output);
if (reference.PrevIndex >= Outputs.Length)
{
return null;
}
dictionary.Add(reference, tx.Outputs[reference.PrevIndex]);
}
}
_references = dictionary;
Expand Down Expand Up @@ -269,61 +271,54 @@ bool IInventory.Verify(Snapshot snapshot)

public virtual bool Verify(Snapshot snapshot, IEnumerable<Transaction> mempool)
{
try
if (Size > MaxTransactionSize) return false;
for (int i = 1; i < Inputs.Length; i++)
for (int j = 0; j < i; j++)
if (Inputs[i].PrevHash == Inputs[j].PrevHash && Inputs[i].PrevIndex == Inputs[j].PrevIndex)
return false;
if (mempool.Where(p => p != this).SelectMany(p => p.Inputs).Intersect(Inputs).Count() > 0)
return false;
if (snapshot.IsDoubleSpend(this))
return false;
foreach (var group in Outputs.GroupBy(p => p.AssetId))
{
if (Size > MaxTransactionSize) return false;
for (int i = 1; i < Inputs.Length; i++)
for (int j = 0; j < i; j++)
if (Inputs[i].PrevHash == Inputs[j].PrevHash && Inputs[i].PrevIndex == Inputs[j].PrevIndex)
return false;
if (mempool.Where(p => p != this).SelectMany(p => p.Inputs).Intersect(Inputs).Count() > 0)
return false;
if (snapshot.IsDoubleSpend(this))
AssetState asset = snapshot.Assets.TryGet(group.Key);
if (asset == null) return false;
if (asset.Expiration <= snapshot.Height + 1 && asset.AssetType != AssetType.GoverningToken && asset.AssetType != AssetType.UtilityToken)
return false;
foreach (var group in Outputs.GroupBy(p => p.AssetId))
{
AssetState asset = snapshot.Assets.TryGet(group.Key);
if (asset == null) return false;
if (asset.Expiration <= snapshot.Height + 1 && asset.AssetType != AssetType.GoverningToken && asset.AssetType != AssetType.UtilityToken)
foreach (TransactionOutput output in group)
if (output.Value.GetData() % (long)Math.Pow(10, 8 - asset.Precision) != 0)
return false;
foreach (TransactionOutput output in group)
if (output.Value.GetData() % (long)Math.Pow(10, 8 - asset.Precision) != 0)
return false;
}
TransactionResult[] results = GetTransactionResults()?.ToArray();
if (results == null) return false;
TransactionResult[] results_destroy = results.Where(p => p.Amount > Fixed8.Zero).ToArray();
if (results_destroy.Length > 1) return false;
if (results_destroy.Length == 1 && results_destroy[0].AssetId != Blockchain.UtilityToken.Hash)
return false;
if (SystemFee > Fixed8.Zero && (results_destroy.Length == 0 || results_destroy[0].Amount < SystemFee))
return false;
TransactionResult[] results_issue = results.Where(p => p.Amount < Fixed8.Zero).ToArray();
switch (Type)
{
case TransactionType.MinerTransaction:
case TransactionType.ClaimTransaction:
if (results_issue.Any(p => p.AssetId != Blockchain.UtilityToken.Hash))
return false;
break;
case TransactionType.IssueTransaction:
if (results_issue.Any(p => p.AssetId == Blockchain.UtilityToken.Hash))
return false;
break;
default:
if (results_issue.Length > 0)
return false;
break;
}
if (Attributes.Count(p => p.Usage == TransactionAttributeUsage.ECDH02 || p.Usage == TransactionAttributeUsage.ECDH03) > 1)
return false;
if (!VerifyReceivingScripts()) return false;
return this.VerifyWitnesses(snapshot);
}
catch (Exception)
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
TransactionResult[] results = GetTransactionResults()?.ToArray();
if (results == null) return false;
TransactionResult[] results_destroy = results.Where(p => p.Amount > Fixed8.Zero).ToArray();
if (results_destroy.Length > 1) return false;
if (results_destroy.Length == 1 && results_destroy[0].AssetId != Blockchain.UtilityToken.Hash)
return false;
if (SystemFee > Fixed8.Zero && (results_destroy.Length == 0 || results_destroy[0].Amount < SystemFee))
return false;
TransactionResult[] results_issue = results.Where(p => p.Amount < Fixed8.Zero).ToArray();
switch (Type)
{
case TransactionType.MinerTransaction:
case TransactionType.ClaimTransaction:
if (results_issue.Any(p => p.AssetId != Blockchain.UtilityToken.Hash))
return false;
break;
case TransactionType.IssueTransaction:
if (results_issue.Any(p => p.AssetId == Blockchain.UtilityToken.Hash))
return false;
break;
default:
if (results_issue.Length > 0)
return false;
break;
}
if (Attributes.Count(p => p.Usage == TransactionAttributeUsage.ECDH02 || p.Usage == TransactionAttributeUsage.ECDH03) > 1)
return false;
if (!VerifyReceivingScripts()) return false;
return this.VerifyWitnesses(snapshot);
}

private bool VerifyReceivingScripts()
Expand Down