Block and transaction verifications
[NovacoinLibrary.git] / Novacoin / CBlockHeader.cs
index 1612ee1..8e151e9 100644 (file)
  */
 
 using System;
-using System.Linq;
 using System.Text;
 using System.Collections.Generic;
+using System.Diagnostics.Contracts;
 
 namespace Novacoin
 {
-       /// <summary>
-       /// Block header
-       /// </summary>
-       public class CBlockHeader
+    /// <summary>
+    /// Block header
+    /// </summary>
+    public class CBlockHeader
        {
                /// <summary>
                /// Version of block schema.
@@ -36,7 +36,7 @@ namespace Novacoin
                /// <summary>
                /// Previous block hash.
                /// </summary>
-               public Hash256 prevHash = new Hash256();
+               public ScryptHash256 prevHash = new ScryptHash256();
 
                /// <summary>
                /// Merkle root hash.
@@ -68,7 +68,7 @@ namespace Novacoin
         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;
@@ -77,8 +77,10 @@ namespace Novacoin
 
         public CBlockHeader(byte[] bytes)
         {
+            Contract.Requires<ArgumentException>(bytes.Length == 80, "Any valid block header is exactly 80 bytes long.");
+
             nVersion = BitConverter.ToUInt32(bytes, 0);
-            prevHash = new Hash256(bytes, 4);
+            prevHash = new ScryptHash256(bytes, 4);
             merkleRoot = new Hash256(bytes, 36);
             nTime = BitConverter.ToUInt32(bytes, 68);
             nBits = BitConverter.ToUInt32(bytes, 72);
@@ -89,33 +91,30 @@ namespace Novacoin
         /// Convert current block header instance into sequence of bytes
         /// </summary>
         /// <returns>Byte sequence</returns>
-        public IList<byte> Bytes
+        public static implicit operator byte[] (CBlockHeader h)
         {
-            get
-            {
-                List<byte> r = new List<byte>();
-
-                r.AddRange(BitConverter.GetBytes(nVersion));
-                r.AddRange(prevHash.hashBytes);
-                r.AddRange(merkleRoot.hashBytes);
-                r.AddRange(BitConverter.GetBytes(nTime));
-                r.AddRange(BitConverter.GetBytes(nBits));
-                r.AddRange(BitConverter.GetBytes(nNonce));
-
-                return r;
-            }
+            var r = new List<byte>();
+
+            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));
+
+            return r.ToArray();
         }
 
         public ScryptHash256 Hash
         {
             get {
-                return ScryptHash256.Compute256(Bytes);
+                return ScryptHash256.Compute256(this);
             }
         }
 
         public override string ToString()
         {
-            StringBuilder sb = new StringBuilder();
+            var sb = new StringBuilder();
             sb.AppendFormat("CBlockHeader(nVersion={0}, prevHash={1}, merkleRoot={2}, nTime={3}, nBits={4}, nNonce={5})", nVersion, prevHash.ToString(), merkleRoot.ToString(), nTime, nBits, nNonce);
             return sb.ToString();
         }