diff --git a/src/Neo.VM/Types/Map.cs b/src/Neo.VM/Types/Map.cs index 2db1aa567b..6538820be3 100644 --- a/src/Neo.VM/Types/Map.cs +++ b/src/Neo.VM/Types/Map.cs @@ -89,6 +89,20 @@ public StackItem this[PrimitiveType key] /// The reference counter to be used. public Map(IReferenceCounter? referenceCounter = null) : base(referenceCounter) { } + /// + /// Create a new map with the specified dictionary and reference counter. + /// + /// Dictionary + /// Reference Counter + public Map(IDictionary dictionary, IReferenceCounter? referenceCounter = null) + : this(referenceCounter) + { + foreach (var (k, v) in dictionary) + { + this[k] = v; + } + } + public override void Clear() { if (IsReadOnly) throw new InvalidOperationException("The map is readonly, can not clear."); diff --git a/tests/Neo.VM.Tests/UT_StackItem.cs b/tests/Neo.VM.Tests/UT_StackItem.cs index ee4568caad..9f1ac0f724 100644 --- a/tests/Neo.VM.Tests/UT_StackItem.cs +++ b/tests/Neo.VM.Tests/UT_StackItem.cs @@ -12,6 +12,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.VM; using Neo.VM.Types; +using System.Collections.Generic; using System.Numerics; namespace Neo.Test @@ -93,10 +94,11 @@ public void TestHashCode() itemA = new Map { [true] = false, [0] = 1 }; itemB = new Map { [true] = false, [0] = 1 }; - itemC = new Map { [true] = false, [0] = 2 }; + itemC = new Map(new Dictionary() { [true] = false, [0] = 2 }); Assert.AreEqual(itemB.GetHashCode(), itemA.GetHashCode()); Assert.AreNotEqual(itemC.GetHashCode(), itemA.GetHashCode()); + Assert.HasCount(2, itemC as Map); // Test CompoundType GetHashCode for subitems var junk = new Array { true, false, 0 };