nMaxSigOps constant.
[NovacoinLibrary.git] / Novacoin / CBlock.cs
index 01bdb27..3f72c68 100644 (file)
@@ -20,6 +20,7 @@ using System;
 using System.Text;
 using System.Collections.Generic;
 using System.Diagnostics.Contracts;
+using System.IO;
 
 namespace Novacoin
 {
@@ -46,6 +47,16 @@ namespace Novacoin
     /// </summary>
     public class CBlock
        {
+        /// <summary>
+        /// Maximum block size is 1Mb.
+        /// </summary>
+        public const uint nMaxBlockSize = 1000000;
+
+        /// <summary>
+        /// Sanity threshold for amount of sigops.
+        /// </summary>
+        public const uint nMaxSigOps = 20000;
+
                /// <summary>
                /// Block header.
                /// </summary>
@@ -87,16 +98,19 @@ namespace Novacoin
                {
             try
             {
-                ByteQueue wBytes = new ByteQueue(blockBytes);
+                var stream = new MemoryStream(blockBytes);
+                var reader = new BinaryReader(stream);
 
                 // Fill the block header fields
-                header = new CBlockHeader(wBytes.Get(80));
+                header = new CBlockHeader(ref reader);               
 
                 // Parse transactions list
-                vtx = CTransaction.ReadTransactionsList(ref wBytes);
+                vtx = CTransaction.ReadTransactionsList(ref reader);
 
                 // Read block signature
-                signature = wBytes.Get((int)wBytes.GetVarInt());
+                signature = reader.ReadBytes((int)VarInt.ReadVarInt(ref reader));
+
+                reader.Close();
             }
             catch (Exception e)
             {
@@ -118,7 +132,7 @@ namespace Novacoin
             uint nSigOps = 0; // total sigops
 
             // Basic sanity checkings
-            if (vtx.Length == 0 || Size > 1000000)
+            if (vtx.Length == 0 || Size > nMaxBlockSize)
             {
                 return false;
             }
@@ -241,7 +255,7 @@ namespace Novacoin
             }
 
             // Reject block if validation would consume too much resources.
-            if (nSigOps > 50000)
+            if (nSigOps > nMaxSigOps)
             {
                 return false;
             }
@@ -328,20 +342,25 @@ namespace Novacoin
         /// <returns>Byte sequence</returns>
         public static implicit operator byte[] (CBlock b)
         {
-            var r = new List<byte>();
+            var stream = new MemoryStream();
+            var writer = new BinaryWriter(stream);
 
-            r.AddRange((byte[])b.header);
-            r.AddRange(VarInt.EncodeVarInt(b.vtx.LongLength)); // transactions count
+            writer.Write(b.header);
+            writer.Write(VarInt.EncodeVarInt(b.vtx.LongLength));
 
             foreach (var tx in b.vtx)
             {
-                r.AddRange((byte[])tx);
+                writer.Write(tx);
             }
 
-            r.AddRange(VarInt.EncodeVarInt(b.signature.LongLength));
-            r.AddRange(b.signature);
+            writer.Write(VarInt.EncodeVarInt(b.signature.LongLength));
+            writer.Write(b.signature);
+
+            var resultBytes = stream.ToArray();
+
+            writer.Close();
 
-            return r.ToArray();
+            return resultBytes;
         }
 
         /// <summary>