X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCOutPoint.cs;h=ddb40e8cfce9cc080c26d40aca9f30663add746e;hb=be9d844557911f95165d2c9875c4f5b2822cfc92;hp=4c9f17a9b80cde87d15b53ee81a9cff649359d52;hpb=12a559c6cb61ae1c22e2859742ea0edd552eca07;p=NovacoinLibrary.git diff --git a/Novacoin/COutPoint.cs b/Novacoin/COutPoint.cs index 4c9f17a..ddb40e8 100644 --- a/Novacoin/COutPoint.cs +++ b/Novacoin/COutPoint.cs @@ -1,12 +1,30 @@ -using System; +/** + * 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 . + */ + +using System; using System.Collections.Generic; -using System.Linq; +using System.Diagnostics.Contracts; +using System.IO; using System.Text; -using System.Threading.Tasks; namespace Novacoin { - public class COutPoint + public class COutPoint : IComparable, IEquatable { /// /// Hash of parent transaction. @@ -18,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(); @@ -36,10 +59,12 @@ namespace Novacoin n = o.n; } - public COutPoint(IEnumerable bytes) + public COutPoint(byte[] bytes) { - hash = new Hash256(bytes.Take(32)); - n = BitConverter.ToUInt32(bytes.Skip(32).Take(4).ToArray(), 0); + 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); } public bool IsNull @@ -47,27 +72,58 @@ namespace Novacoin get { return hash.IsZero && 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(); + var sb = new StringBuilder(); sb.AppendFormat("COutPoint({0}, {1})", hash.ToString(), 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); + } } }