diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index 8c4071379c..1671b70085 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -42,10 +42,11 @@ public class Transaction : IEquatable, IInventory, IInteroperable sizeof(long) + //NetworkFee sizeof(uint); //ValidUntilBlock + private Dictionary _attributesCache; public TransactionAttribute[] Attributes { get => attributes; - set { attributes = value; _hash = null; _size = 0; } + set { attributes = value; _attributesCache = null; _hash = null; _size = 0; } } /// @@ -219,6 +220,18 @@ void IInteroperable.FromStackItem(StackItem stackItem) throw new NotSupportedException(); } + public T GetAttribute() where T : TransactionAttribute + { + return GetAttributes()?.First(); + } + + public T[] GetAttributes() where T : TransactionAttribute + { + _attributesCache ??= attributes.GroupBy(p => p.GetType()).ToDictionary(p => p.Key, p => (TransactionAttribute[])p.OfType().ToArray()); + _attributesCache.TryGetValue(typeof(T), out var result); + return (T[])result; + } + public override int GetHashCode() { return Hash.GetHashCode();