Limit duplicity on stake & Process orphans only if there are some.
[NovacoinLibrary.git] / Novacoin / COutPoint.cs
index 9e628e9..07c6552 100644 (file)
@@ -20,16 +20,17 @@ using System;
 using System.Collections.Generic;
 using System.Diagnostics.Contracts;
 using System.IO;
+using System.Linq;
 using System.Text;
 
 namespace Novacoin
 {
-    public class COutPoint : IComparable<COutPoint>, IEquatable<COutPoint>
+    public class COutPoint : IComparable<COutPoint>, IEquatable<COutPoint>, IEqualityComparer<COutPoint>
     {
         /// <summary>
         /// Hash of parent transaction.
         /// </summary>
-        public Hash256 hash;
+        public uint256 hash;
 
         /// <summary>
         /// Parent input number.
@@ -43,11 +44,11 @@ namespace Novacoin
 
         public COutPoint()
         {
-            hash = new Hash256();
+            hash = new uint256();
             n = uint.MaxValue;
         }
 
-        public COutPoint(Hash256 hashIn, uint nIn)
+        public COutPoint(uint256 hashIn, uint nIn)
         {
             hash = hashIn;
             n = nIn;
@@ -55,7 +56,7 @@ namespace Novacoin
 
         public COutPoint(COutPoint o)
         {
-            hash = new Hash256(o.hash);
+            hash = o.hash;
             n = o.n;
         }
 
@@ -63,13 +64,13 @@ namespace Novacoin
         {
             Contract.Requires<ArgumentException>(bytes.Length == 36, "Any valid outpoint reference data item is exactly 36 bytes long.");
 
-            hash = new Hash256(bytes);
+            hash = bytes.Take(32).ToArray();
             n = BitConverter.ToUInt32(bytes, 32);
         }
 
         public bool IsNull
         {
-            get { return hash.IsZero && n == uint.MaxValue; }
+            get { return !hash && n == uint.MaxValue; }
         }
 
         public static implicit operator byte[] (COutPoint o)
@@ -87,6 +88,11 @@ namespace Novacoin
             return outBytes;
         }
 
+        public static implicit operator COutPoint(byte[] b)
+        {
+            return new COutPoint(b);
+        }
+
         public override string ToString()
         {
             var sb = new StringBuilder();
@@ -122,8 +128,34 @@ namespace Novacoin
         /// <returns>Result of comparison.</returns>
         public bool Equals(COutPoint o)
         {
+            if (object.ReferenceEquals(o, null))
+            {
+                return false;
+            }
+
             return (o.n == n) && (o.hash == hash);
         }
+
+        /// <summary>
+        /// Equality comparer for outpoints.
+        /// </summary>
+        /// <param name="x">First outpoint.</param>
+        /// <param name="y">Second outpoint.</param>
+        /// <returns>Result of comparison.</returns>
+        public bool Equals(COutPoint x, COutPoint y)
+        {
+            return (x.n == y.n) && (x.hash == y.hash);
+        }
+
+        public override int GetHashCode()
+        {
+            return n.GetHashCode() ^ hash.GetHashCode();
+        }
+
+        public int GetHashCode(COutPoint obj)
+        {
+            return obj.GetHashCode();
+        }
     }
 
 }