using System;
using System.Collections.Generic;
+using System.Diagnostics.Contracts;
+using System.IO;
using System.Text;
namespace Novacoin
{
- public class COutPoint
+ public class COutPoint : IComparable<COutPoint>, IEquatable<COutPoint>
{
/// <summary>
/// Hash of parent transaction.
/// </summary>
public uint n;
+ /// <summary>
+ /// Out reference is always 36 bytes long.
+ /// </summary>
+ public const int Size = 36;
+
public COutPoint()
{
hash = new Hash256();
public COutPoint(byte[] bytes)
{
+ Contract.Requires<ArgumentException>(bytes.Length == 36, "Any valid outpoint reference data item is exactly 36 bytes long.");
+
hash = new Hash256(bytes);
n = BitConverter.ToUInt32(bytes, 32);
}
public static implicit operator byte[] (COutPoint o)
{
- var r = new List<byte>();
- r.AddRange((byte[])o.hash);
- r.AddRange(BitConverter.GetBytes(o.n));
+ var stream = new MemoryStream();
+ var writer = new BinaryWriter(stream);
+
+ writer.Write(o.hash);
+ writer.Write(o.n);
- return r.ToArray();
+ var outBytes = stream.ToArray();
+
+ writer.Close();
+
+ return outBytes;
}
public override string ToString()
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);
+ }
}
}