X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCOutPoint.cs;h=ddb40e8cfce9cc080c26d40aca9f30663add746e;hb=be9d844557911f95165d2c9875c4f5b2822cfc92;hp=a8db03db0b6d8f1e59e80623682479a730f6d97b;hpb=0279648337efe9bbe32b5204e247529243805484;p=NovacoinLibrary.git diff --git a/Novacoin/COutPoint.cs b/Novacoin/COutPoint.cs index a8db03d..ddb40e8 100644 --- a/Novacoin/COutPoint.cs +++ b/Novacoin/COutPoint.cs @@ -18,11 +18,13 @@ 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, IEquatable { /// /// Hash of parent transaction. @@ -34,6 +36,11 @@ namespace Novacoin /// public uint n; + /// + /// Out reference is always 36 bytes long. + /// + public const int Size = 36; + public COutPoint() { hash = new Hash256(); @@ -54,6 +61,8 @@ namespace Novacoin public COutPoint(byte[] bytes) { + Contract.Requires(bytes.Length == 36, "Any valid outpoint reference data item is exactly 36 bytes long."); + hash = new Hash256(bytes); n = BitConverter.ToUInt32(bytes, 32); } @@ -65,11 +74,17 @@ namespace Novacoin public static implicit operator byte[] (COutPoint o) { - var r = new List(); - 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() @@ -80,7 +95,35 @@ namespace Novacoin return sb.ToString(); } - + /// + /// Compare this outpoint with some other. + /// + /// Other outpoint. + /// Result of comparison. + public int CompareTo(COutPoint o) + { + if (n > o.n) + { + return 1; + } + else if (n < o.n) + { + return -1; + } + + return 0; + + } + + /// + /// Equality comparer for outpoints. + /// + /// Other outpoint. + /// Result of comparison. + public bool Equals(COutPoint o) + { + return (o.n == n) && (o.hash == hash); + } } }