Replace ByteQueue with its simplified version, InstructionQueue, since it's used...
[NovacoinLibrary.git] / Novacoin / CBlockHeader.cs
index d7c1dc6..29abc7d 100644 (file)
@@ -19,6 +19,8 @@
 using System;
 using System.Text;
 using System.Collections.Generic;
+using System.Diagnostics.Contracts;
+using System.IO;
 
 namespace Novacoin
 {
@@ -30,58 +32,85 @@ namespace Novacoin
                /// <summary>
                /// Version of block schema.
                /// </summary>
-               public uint nVersion = 6;
+               public uint nVersion;
 
                /// <summary>
                /// Previous block hash.
                /// </summary>
-               public Hash256 prevHash = new Hash256();
+               public ScryptHash256 prevHash;
 
                /// <summary>
                /// Merkle root hash.
                /// </summary>
-               public Hash256 merkleRoot = new Hash256();
+               public Hash256 merkleRoot;
 
                /// <summary>
                /// Block timestamp.
                /// </summary>
-               public uint nTime = 0;
+               public uint nTime;
 
                /// <summary>
                /// Compressed difficulty representation.
                /// </summary>
-               public uint nBits = 0;
+               public uint nBits;
 
                /// <summary>
                /// Nonce counter.
                /// </summary>
-               public uint nNonce = 0;
+               public uint nNonce;
 
         /// <summary>
         /// Initialize an empty instance
         /// </summary>
                public CBlockHeader ()
                {
+            nVersion = 6;
+            prevHash = new ScryptHash256();
+            merkleRoot = new Hash256();
+            nTime = Interop.GetTime();
+            nBits = 0;
+            nNonce = 0;
                }
 
         public CBlockHeader(CBlockHeader h)
         {
             nVersion = h.nVersion;
-            prevHash = new Hash256(h.prevHash);
+            prevHash = new ScryptHash256(h.prevHash);
             merkleRoot = new Hash256(h.merkleRoot);
             nTime = h.nTime;
             nBits = h.nBits;
             nNonce = h.nNonce;
         }
 
+        internal CBlockHeader(ref BinaryReader reader)
+        {
+            nVersion = reader.ReadUInt32();
+            prevHash = new ScryptHash256(reader.ReadBytes(32));
+            merkleRoot = new Hash256(reader.ReadBytes(32));
+            nTime = reader.ReadUInt32();
+            nBits = reader.ReadUInt32();
+            nNonce = reader.ReadUInt32();
+        }
+
+        /// <summary>
+        /// Init block header with bytes.
+        /// </summary>
+        /// <param name="bytes">Byte array.</param>
         public CBlockHeader(byte[] bytes)
         {
-            nVersion = BitConverter.ToUInt32(bytes, 0);
-            prevHash = new Hash256(bytes, 4);
-            merkleRoot = new Hash256(bytes, 36);
-            nTime = BitConverter.ToUInt32(bytes, 68);
-            nBits = BitConverter.ToUInt32(bytes, 72);
-            nNonce = BitConverter.ToUInt32(bytes, 76);
+            Contract.Requires<ArgumentException>(bytes.Length == 80, "Any valid block header is exactly 80 bytes long.");
+
+            var stream = new MemoryStream(bytes);
+            var reader = new BinaryReader(stream);
+
+            nVersion = reader.ReadUInt32();
+            prevHash = new ScryptHash256(reader.ReadBytes(32));
+            merkleRoot = new Hash256(reader.ReadBytes(32));
+            nTime = reader.ReadUInt32();
+            nBits = reader.ReadUInt32();
+            nNonce = reader.ReadUInt32();
+
+            reader.Close();
         }
 
         /// <summary>
@@ -90,16 +119,21 @@ namespace Novacoin
         /// <returns>Byte sequence</returns>
         public static implicit operator byte[] (CBlockHeader h)
         {
-            var r = new List<byte>();
+            var stream = new MemoryStream();
+            var writer = new BinaryWriter(stream);
+
+            writer.Write(h.nVersion);
+            writer.Write(h.prevHash);
+            writer.Write(h.merkleRoot);
+            writer.Write(h.nTime);
+            writer.Write(h.nBits);
+            writer.Write(h.nNonce);
+
+            var resultBytes = stream.ToArray();
 
-            r.AddRange(BitConverter.GetBytes(h.nVersion));
-            r.AddRange((byte[])h.prevHash);
-            r.AddRange((byte[])h.merkleRoot);
-            r.AddRange(BitConverter.GetBytes(h.nTime));
-            r.AddRange(BitConverter.GetBytes(h.nBits));
-            r.AddRange(BitConverter.GetBytes(h.nNonce));
+            writer.Close();
 
-            return r.ToArray();
+            return resultBytes;
         }
 
         public ScryptHash256 Hash