Remove Hash, Hash256, Hash160 and ScryptHash256 classes.
[NovacoinLibrary.git] / Novacoin / COutPoint.cs
index 71f2bda..09d0690 100644 (file)
@@ -1,30 +1,54 @@
-\feffusing System;
+\feff/**
+ *  Novacoin classes library
+ *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as
+ *  published by the Free Software Foundation, either version 3 of the
+ *  License, or (at your option) any later version.
+
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using System;
 using System.Collections.Generic;
+using System.Diagnostics.Contracts;
+using System.IO;
 using System.Linq;
 using System.Text;
-using System.Threading.Tasks;
 
 namespace Novacoin
 {
-    public class COutPoint
+    public class COutPoint : IComparable<COutPoint>, IEquatable<COutPoint>
     {
         /// <summary>
         /// Hash of parent transaction.
         /// </summary>
-        public Hash256 hash;
+        public uint256 hash;
 
         /// <summary>
         /// Parent input number.
         /// </summary>
         public uint n;
 
+        /// <summary>
+        /// Out reference is always 36 bytes long.
+        /// </summary>
+        public const int Size = 36;
+
         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;
@@ -32,42 +56,75 @@ namespace Novacoin
 
         public COutPoint(COutPoint o)
         {
-            hash = new Hash256(o.hash);
+            hash = o.hash;
             n = o.n;
         }
 
-        public COutPoint(IEnumerable<byte> bytes)
+        public COutPoint(byte[] bytes)
         {
-            hash = new Hash256(bytes);
-            n = BitConverter.ToUInt32(bytes.ToArray(), 32);
+            Contract.Requires<ArgumentException>(bytes.Length == 36, "Any valid outpoint reference data item is exactly 36 bytes long.");
+
+            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 IList<byte> Bytes
+        public static implicit operator byte[] (COutPoint o)
         {
-            get
-            {
-                List<byte> r = new List<byte>();
-                r.AddRange(hash.hashBytes);
-                r.AddRange(BitConverter.GetBytes(n));
+            var stream = new MemoryStream();
+            var writer = new BinaryWriter(stream);
 
-                return r;
-            }
+            writer.Write(o.hash);
+            writer.Write(o.n);
+
+            var outBytes = stream.ToArray();
+
+            writer.Close();
+
+            return outBytes;
         }
 
         public override string ToString()
         {
-            StringBuilder sb = new StringBuilder();
-            sb.AppendFormat("COutPoint({0}, {1})", hash.ToString(), n);
+            var sb = new StringBuilder();
+            sb.AppendFormat("COutPoint({0}, {1})", hash, n);
 
             return sb.ToString();
         }
 
-        
+        /// <summary>
+        /// Compare this outpoint with some other.
+        /// </summary>
+        /// <param name="o">Other outpoint.</param>
+        /// <returns>Result of comparison.</returns>
+        public int CompareTo(COutPoint o)
+        {
+            if (n > o.n)
+            {
+                return 1;
+            }
+            else if (n < o.n)
+            {
+                return -1;
+            }
+
+            return 0;
+
+        }
+
+        /// <summary>
+        /// Equality comparer for outpoints.
+        /// </summary>
+        /// <param name="o">Other outpoint.</param>
+        /// <returns>Result of comparison.</returns>
+        public bool Equals(COutPoint o)
+        {
+            return (o.n == n) && (o.hash == hash);
+        }
     }
 
 }