Skip to content

Commit

Permalink
Optimize attributes (neo-project#1774)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Jul 18, 2020
1 parent da6be43 commit 85543c7
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ public class Transaction : IEquatable<Transaction>, IInventory, IInteroperable
sizeof(long) + //NetworkFee
sizeof(uint); //ValidUntilBlock

private Dictionary<Type, TransactionAttribute[]> _attributesCache;
public TransactionAttribute[] Attributes
{
get => attributes;
set { attributes = value; _hash = null; _size = 0; }
set { attributes = value; _attributesCache = null; _hash = null; _size = 0; }
}

/// <summary>
Expand Down Expand Up @@ -219,6 +220,18 @@ void IInteroperable.FromStackItem(StackItem stackItem)
throw new NotSupportedException();
}

public T GetAttribute<T>() where T : TransactionAttribute
{
return GetAttributes<T>()?.First();
}

public T[] GetAttributes<T>() where T : TransactionAttribute
{
_attributesCache ??= attributes.GroupBy(p => p.GetType()).ToDictionary(p => p.Key, p => (TransactionAttribute[])p.OfType<T>().ToArray());
_attributesCache.TryGetValue(typeof(T), out var result);
return (T[])result;
}

public override int GetHashCode()
{
return Hash.GetHashCode();
Expand Down

0 comments on commit 85543c7

Please sign in to comment.