Replace ByteQueue with its simplified version, InstructionQueue, since it's used...
authorCryptoManiac <balthazar@yandex.ru>
Mon, 31 Aug 2015 23:00:37 +0000 (02:00 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Mon, 31 Aug 2015 23:00:37 +0000 (02:00 +0300)
Add new internal constructor for block header class.

Novacoin/CBlock.cs
Novacoin/CBlockHeader.cs
Novacoin/CScript.cs
Novacoin/CTransaction.cs
Novacoin/InstructionQueue.cs [moved from Novacoin/ByteQueue.cs with 64% similarity]
Novacoin/Novacoin.csproj
Novacoin/ScriptCode.cs

index 101d20e..d8d7a75 100644 (file)
@@ -92,13 +92,7 @@ namespace Novacoin
                 var reader = new BinaryReader(stream);
 
                 // Fill the block header fields
-                header = new CBlockHeader();
-                header.nVersion = reader.ReadUInt32();
-                header.prevHash = new ScryptHash256(reader.ReadBytes(32));
-                header.merkleRoot = new Hash256(reader.ReadBytes(32));
-                header.nTime = reader.ReadUInt32();
-                header.nBits = reader.ReadUInt32();
-                header.nNonce = reader.ReadUInt32();                
+                header = new CBlockHeader(ref reader);               
 
                 // Parse transactions list
                 vtx = CTransaction.ReadTransactionsList(ref reader);
index d56df33..29abc7d 100644 (file)
@@ -82,6 +82,16 @@ namespace Novacoin
             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>
index 229b60b..1418a25 100644 (file)
@@ -52,9 +52,9 @@ namespace Novacoin
         /// Return a new instance of ByteQueue object for current code bytes
         /// </summary>
         /// <returns></returns>
-        public ByteQueue GetByteQueue()
+        public InstructionQueue GetInstructionQueue()
         {
-             return new ByteQueue(ref codeBytes);
+             return new InstructionQueue(ref codeBytes);
         }
 
         /// <summary>
@@ -157,7 +157,7 @@ namespace Novacoin
             }
 
             var count = 0;
-            var bq1 = new ByteQueue(ref codeBytes);
+            var bq1 = new InstructionQueue(ref codeBytes);
 
             byte[] pushData;
             instruction opcode;
@@ -203,7 +203,7 @@ namespace Novacoin
 
             var count = 0;
             var newScript = new CScript();
-            var bq1 = new ByteQueue(ref codeBytes);
+            var bq1 = new InstructionQueue(ref codeBytes);
 
             while (ScriptCode.GetOp(ref bq1, out opcode, out pushData))
             {
@@ -239,7 +239,7 @@ namespace Novacoin
         {
             get
             {
-                var wCodeBytes = new ByteQueue(ref codeBytes);
+                var wCodeBytes = new InstructionQueue(ref codeBytes);
 
                 instruction opcode; // Current instruction
                 byte[] pushArgs; // OP_PUSHDATAn argument
@@ -265,7 +265,7 @@ namespace Novacoin
         {
             get
             {
-                var wCodeBytes = new ByteQueue(ref codeBytes);
+                var wCodeBytes = new InstructionQueue(ref codeBytes);
 
                 byte[] pushArgs; // OP_PUSHDATAn argument
                 instruction opcode; // Current instruction
@@ -354,7 +354,7 @@ namespace Novacoin
         /// <returns>Amount of sigops</returns>
         public uint GetSigOpCount(bool fAccurate)
         {
-            var wCodeBytes = new ByteQueue(ref codeBytes);
+            var wCodeBytes = new InstructionQueue(ref codeBytes);
 
             instruction opcode; // Current instruction
             byte[] pushArgs; // OP_PUSHDATAn argument
@@ -401,7 +401,7 @@ namespace Novacoin
             // This is a pay-to-script-hash scriptPubKey;
             // get the last item that the scriptSig
             // pushes onto the stack:
-            ByteQueue wScriptSig = scriptSig.GetByteQueue();
+            InstructionQueue wScriptSig = scriptSig.GetInstructionQueue();
             int nScriptSigSize = scriptSig.Size;
 
             instruction opcode; // Current instruction
@@ -520,7 +520,7 @@ namespace Novacoin
                public override string ToString()
                {
                        var sb = new StringBuilder();
-            var wCodeBytes = new ByteQueue(ref codeBytes);
+            var wCodeBytes = new InstructionQueue(ref codeBytes);
 
             instruction opcode; // Current instruction
             byte[] pushArgs; // OP_PUSHDATAn argument
index 5cbd54c..ef444f2 100644 (file)
@@ -429,8 +429,6 @@ namespace Novacoin
             return resultBytes;
         }
 
-
-
         public override string ToString()
         {
             var sb = new StringBuilder();
similarity index 64%
rename from Novacoin/ByteQueue.cs
rename to Novacoin/InstructionQueue.cs
index 8af351f..599f525 100644 (file)
@@ -24,62 +24,47 @@ using System.IO;
 namespace Novacoin
 {
     [Serializable]
-    public class ByteQueueException : Exception
+    public class InstructionQueueException : Exception
     {
-        public ByteQueueException()
+        public InstructionQueueException()
         {
         }
 
-        public ByteQueueException(string message)
+        public InstructionQueueException(string message)
             : base(message)
         {
         }
 
-        public ByteQueueException(string message, Exception inner)
+        public InstructionQueueException(string message, Exception inner)
             : base(message, inner)
         {
         }
     }
 
     /// <summary>
-    /// Stream of bytes.
-    /// 
-    /// TODO: rewrite using MemoryStream
+    /// Stream of instructions.
     /// </summary>
-    public class ByteQueue : IDisposable
+    public class InstructionQueue : IDisposable
     {
         private bool disposed = false;
 
         private MemoryStream _Stream;
         private BinaryReader _Reader;
 
-        public ByteQueue(ref byte[] buffer, int Start)
-        {
-            _Stream = new MemoryStream(buffer);
-            _Stream.Seek(Start, SeekOrigin.Begin);
-            _Reader = new BinaryReader(_Stream);
-        }
-
-        public ByteQueue(ref byte[] buffer)
-        {
-            _Stream = new MemoryStream(buffer);
-            _Reader = new BinaryReader(_Stream);
-        }
-
-        public ByteQueue(ref List<byte> List, int Start)
+        public InstructionQueue(ref List<byte> List, int Start)
         {
             _Stream = new MemoryStream(List.ToArray());
             _Stream.Seek(Start, SeekOrigin.Begin);
             _Reader = new BinaryReader(_Stream);
         }
 
-        public ByteQueue(ref List<byte> List)
+        public InstructionQueue(ref List<byte> List)
         {
             _Stream = new MemoryStream(List.ToArray());
             _Reader = new BinaryReader(_Stream);
         }
 
-        ~ByteQueue()
+        ~InstructionQueue()
         {
             Dispose(false);
         }
@@ -108,7 +93,7 @@ namespace Novacoin
         {
             if (_Stream.Position == _Stream.Length)
             {
-                throw new ByteQueueException("No elements left.");
+                throw new InstructionQueueException("No instructions left.");
             }
 
             return _Reader.ReadByte();
@@ -128,7 +113,7 @@ namespace Novacoin
 
         public byte[] Get(int nCount)
         {
-            Contract.Requires<ArgumentException>(Count - Index >= nCount, "nCount is greater than amount of elements.");
+            Contract.Requires<ArgumentException>(Count - Index >= nCount, "nCount is greater than amount of instructions.");
 
             return _Reader.ReadBytes(nCount);
         }
@@ -147,34 +132,13 @@ namespace Novacoin
             get { return (int)_Stream.Position; }
         }
 
+        /// <summary>
+        /// Stream length
+        /// </summary>
         public int Count
         {
             get { return (int)_Stream.Length; }
         }
-
-        public ulong GetVarInt()
-        {
-            try
-            {
-                byte prefix = _Reader.ReadByte();
-
-                switch (prefix)
-                {
-                    case 0xfd: // ushort
-                        return _Reader.ReadUInt16();
-                    case 0xfe: // uint
-                        return _Reader.ReadUInt32();
-                    case 0xff: // ulong
-                        return _Reader.ReadUInt64();
-                    default:
-                        return prefix;
-                }
-            }
-            catch (EndOfStreamException e)
-            {
-                throw new ByteQueueException("No elements left.", e);
-            }
-        }
     }
 }
 
index c15b923..6d483af 100644 (file)
   </ItemGroup>
   <ItemGroup>
     <Compile Include="AddressTools.cs" />
-    <Compile Include="ByteQueue.cs" />
+    <Compile Include="InstructionQueue.cs" />
     <Compile Include="CBlockStore.cs" />
     <Compile Include="CKey.cs" />
     <Compile Include="CKeyID.cs" />
index f1e6efd..df1d773 100644 (file)
@@ -247,7 +247,7 @@ namespace Novacoin
         /// <param name="opcodeRet">Found instruction.</param>
         /// <param name="bytesRet">IEnumerable out param which is used to get the push arguments.</param>
         /// <returns>Result of operation</returns>
-        public static bool GetOp(ref ByteQueue codeBytes, out instruction opcodeRet, out byte[] bytesRet)
+        public static bool GetOp(ref InstructionQueue codeBytes, out instruction opcodeRet, out byte[] bytesRet)
         {
             bytesRet = new byte[0];
             instruction opcode = opcodeRet = instruction.OP_INVALIDOPCODE;
@@ -292,7 +292,7 @@ namespace Novacoin
                         nSize = BitConverter.ToInt32(codeBytes.Get(4), 0);
                     }
                 }
-                catch (ByteQueueException)
+                catch (InstructionQueueException)
                 {
                     // Unable to read operand length
                     return false;
@@ -541,8 +541,8 @@ namespace Novacoin
                 instruction opcode1, opcode2;
 
                 // Compare
-                var bq1 = script1.GetByteQueue();
-                var bq2 = script2.GetByteQueue();
+                var bq1 = script1.GetInstructionQueue();
+                var bq2 = script2.GetInstructionQueue();
 
                 byte[] args1, args2;
 
@@ -844,7 +844,7 @@ namespace Novacoin
             var falseBytes = new byte[0];
             var trueBytes = new byte[] { 0x01 };
 
-            var CodeQueue = script.GetByteQueue();
+            var CodeQueue = script.GetInstructionQueue();
             var altStack = new List<byte[]>();
 
 #if !DEBUG