X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCOutPoint.cs;h=09d0690a1d7b5428f6c8a7539a00b55414ad82d7;hb=1dcac5faa2b1477034f82466ffb16170fa2e9bb6;hp=ae6c14be20b0cb7973cbb44ca518494eb35d1a7c;hpb=4426ee1dc8ae6733d46b5413d3bce28333792d22;p=NovacoinLibrary.git diff --git a/Novacoin/COutPoint.cs b/Novacoin/COutPoint.cs index ae6c14b..09d0690 100644 --- a/Novacoin/COutPoint.cs +++ b/Novacoin/COutPoint.cs @@ -18,30 +18,37 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.IO; using System.Linq; using System.Text; namespace Novacoin { - public class COutPoint + public class COutPoint : IComparable, IEquatable { /// /// Hash of parent transaction. /// - public Hash256 hash; + public uint256 hash; /// /// Parent input number. /// public uint n; + /// + /// Out reference is always 36 bytes long. + /// + 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; @@ -49,42 +56,75 @@ namespace Novacoin public COutPoint(COutPoint o) { - hash = new Hash256(o.hash); + hash = o.hash; n = o.n; } - public COutPoint(IEnumerable bytes) + public COutPoint(byte[] bytes) { - hash = new Hash256(bytes); - n = BitConverter.ToUInt32(bytes.ToArray(), 32); + Contract.Requires(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 Bytes + public static implicit operator byte[] (COutPoint o) { - get - { - List r = new List(); - 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(); } - + /// + /// 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); + } } }