Generic class was a bit excessive for our purposes
authorCryptoManiac <balthazar@yandex.ru>
Fri, 21 Aug 2015 18:58:19 +0000 (21:58 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Fri, 21 Aug 2015 18:58:19 +0000 (21:58 +0300)
Novacoin/CBlock.cs
Novacoin/CScript.cs
Novacoin/CTransaction.cs
Novacoin/CTxIn.cs
Novacoin/CTxOut.cs
Novacoin/ScriptCode.cs
Novacoin/VarInt.cs
Novacoin/WrappedList.cs

index 8974cef..8f6fb76 100644 (file)
@@ -42,16 +42,16 @@ namespace Novacoin
         /// <param name="blockBytes"></param>
                public CBlock (IList<byte> blockBytes)
                {
-            WrappedList<byte> wBytes = new WrappedList<byte>(blockBytes);
+            ByteQueue wBytes = new ByteQueue(blockBytes);
 
             // Fill the block header fields
-            header = new CBlockHeader(wBytes.GetItems(80));
+            header = new CBlockHeader(wBytes.Get(80));
 
             // Parse transactions list
             vtx = CTransaction.ReadTransactionsList(ref wBytes);
 
             // Read block signature
-            signature = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
+            signature = wBytes.Get((int)VarInt.ReadVarInt(ref wBytes));
                }
 
         public CBlock()
index 4223791..777793b 100644 (file)
@@ -47,12 +47,12 @@ namespace Novacoin
         }
 
         /// <summary>
-        /// Return a new instance of WrappedList object for current code bytes
+        /// Return a new instance of ByteQueue object for current code bytes
         /// </summary>
         /// <returns></returns>
-        public WrappedList<byte> GetWrappedList()
+        public ByteQueue GetWrappedList()
         {
-             return new WrappedList<byte>(codeBytes);
+             return new ByteQueue(codeBytes);
         }
 
         /// <summary>
@@ -178,7 +178,7 @@ namespace Novacoin
         {
             get
             {
-                WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
+                ByteQueue wCodeBytes = new ByteQueue(codeBytes);
 
                 opcodetype opcode; // Current opcode
                 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
@@ -204,7 +204,7 @@ namespace Novacoin
         {
             get
             {
-                WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
+                ByteQueue wCodeBytes = new ByteQueue(codeBytes);
 
                 opcodetype opcode; // Current opcode
                 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
@@ -293,7 +293,7 @@ namespace Novacoin
         /// <returns>Amount of sigops</returns>
         public int GetSigOpCount(bool fAccurate)
         {
-            WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
+            ByteQueue wCodeBytes = new ByteQueue(codeBytes);
 
             opcodetype opcode; // Current opcode
             IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
@@ -340,7 +340,7 @@ namespace Novacoin
             // This is a pay-to-script-hash scriptPubKey;
             // get the last item that the scriptSig
             // pushes onto the stack:
-            WrappedList<byte> wScriptSig = scriptSig.GetWrappedList();
+            ByteQueue wScriptSig = scriptSig.GetWrappedList();
 
             opcodetype opcode; // Current opcode
             IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
@@ -444,7 +444,7 @@ namespace Novacoin
                public override string ToString()
                {
                        StringBuilder sb = new StringBuilder();
-            WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
+            ByteQueue wCodeBytes = new ByteQueue(codeBytes);
 
             opcodetype opcode; // Current opcode
             IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
index 8e1f871..cd66ada 100644 (file)
@@ -79,10 +79,10 @@ namespace Novacoin
         /// <param name="txBytes">Byte sequence</param>
                public CTransaction(IList<byte> txBytes)
         {
-            WrappedList<byte> wBytes = new WrappedList<byte>(txBytes);
+            ByteQueue wBytes = new ByteQueue(txBytes);
 
-            nVersion = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
-            nTime = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+            nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
+            nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
 
             int nInputs = (int)VarInt.ReadVarInt(ref wBytes);
             vin = new CTxIn[nInputs];
@@ -92,12 +92,12 @@ namespace Novacoin
                 // Fill inputs array
                 vin[nCurrentInput] = new CTxIn();
                 
-                vin[nCurrentInput].prevout = new COutPoint(wBytes.GetItems(36));
+                vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
 
                 int nScriptSigLen = (int)VarInt.ReadVarInt(ref wBytes);
-                vin[nCurrentInput].scriptSig = new CScript(wBytes.GetItems(nScriptSigLen));
+                vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
 
-                vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+                vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
             }
 
             int nOutputs = (int)VarInt.ReadVarInt(ref wBytes);
@@ -107,13 +107,13 @@ namespace Novacoin
             {
                 // Fill outputs array
                 vout[nCurrentOutput] = new CTxOut();
-                vout[nCurrentOutput].nValue = BitConverter.ToInt64(wBytes.GetItems(8), 0);
+                vout[nCurrentOutput].nValue = BitConverter.ToInt64(wBytes.Get(8), 0);
 
                 int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes);
-                vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.GetItems(nScriptPKLen));
+                vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
             }
 
-            nLockTime = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+            nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
         }
 
         /// <summary>
@@ -121,7 +121,7 @@ namespace Novacoin
         /// </summary>
         /// <param name="wTxBytes">Bytes sequence</param>
         /// <returns>Transactions array</returns>
-        public static CTransaction[] ReadTransactionsList(ref WrappedList<byte> wTxBytes)
+        public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
         {
             CTransaction[] tx;
 
@@ -134,8 +134,8 @@ namespace Novacoin
                 // Fill the transactions array
                 tx[nTx] = new CTransaction();
 
-                tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.GetItems(4), 0);
-                tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.GetItems(4), 0);
+                tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+                tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
 
                 // Inputs array
                 tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
@@ -143,7 +143,7 @@ namespace Novacoin
                 // outputs array
                 tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
 
-                tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.GetItems(4), 0);
+                tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
             }
 
             return tx;
index fad0d7e..8cff600 100644 (file)
@@ -49,7 +49,7 @@ namespace Novacoin
         /// </summary>
         /// <param name="wBytes">Reference to byte sequence</param>
         /// <returns>Inputs array</returns>
-        public static CTxIn[] ReadTxInList(ref WrappedList<byte> wBytes)
+        public static CTxIn[] ReadTxInList(ref ByteQueue wBytes)
         {
             CTxIn[] vin;
 
@@ -61,9 +61,9 @@ namespace Novacoin
             {
                 // Fill inputs array
                 vin[nIndex] = new CTxIn();
-                vin[nIndex].prevout = new COutPoint(wBytes.GetItems(36));
-                vin[nIndex].scriptSig = new CScript(wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)));
-                vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+                vin[nIndex].prevout = new COutPoint(wBytes.Get(36));
+                vin[nIndex].scriptSig = new CScript(wBytes.Get((int)VarInt.ReadVarInt(ref wBytes)));
+                vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
             }
 
             // Return inputs array
index 8c8ad4f..8acab9b 100644 (file)
@@ -42,7 +42,7 @@ namespace Novacoin
         /// </summary>
         /// <param name="wBytes">Reference to byte sequence</param>
         /// <returns>Outputs array</returns>
-        public static CTxOut[] ReadTxOutList(ref WrappedList<byte> wBytes)
+        public static CTxOut[] ReadTxOutList(ref ByteQueue wBytes)
         {
             int nOutputs = (int)VarInt.ReadVarInt(ref wBytes);
             CTxOut[] vout =new CTxOut[nOutputs];
@@ -51,10 +51,10 @@ namespace Novacoin
             {
                 // Fill outputs array
                 vout[nIndex] = new CTxOut();
-                vout[nIndex].nValue = BitConverter.ToUInt32(wBytes.GetItems(8), 0);
+                vout[nIndex].nValue = BitConverter.ToUInt32(wBytes.Get(8), 0);
 
                 int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes);
-                vout[nIndex].scriptPubKey = new CScript(wBytes.GetItems(nScriptPKLen));
+                vout[nIndex].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
             }
 
             return vout;
index c5b92a3..17d79a8 100644 (file)
@@ -459,11 +459,11 @@ namespace Novacoin
         /// <summary>
         /// Get next opcode from passed list of bytes and extract push arguments if there are some.
         /// </summary>
-        /// <param name="codeBytes">WrappedList reference.</param>
+        /// <param name="codeBytes">ByteQueue reference.</param>
         /// <param name="opcodeRet">Found opcode.</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 WrappedList<byte> codeBytes, out opcodetype opcodeRet, out IEnumerable<byte> bytesRet)
+        public static bool GetOp(ref ByteQueue codeBytes, out opcodetype opcodeRet, out IEnumerable<byte> bytesRet)
         {
             bytesRet = new List<byte>();
             opcodeRet = opcodetype.OP_INVALIDOPCODE;
@@ -473,7 +473,7 @@ namespace Novacoin
             try
             {
                 // Read instruction
-                opcode = (opcodetype)codeBytes.GetItem();
+                opcode = (opcodetype)codeBytes.Get();
             }
             catch (WrappedListException)
             {
@@ -497,19 +497,19 @@ namespace Novacoin
                     {
                         // The next byte contains the number of bytes to be pushed onto the stack, 
                         //    i.e. you have something like OP_PUSHDATA1 0x01 [0x5a]
-                        szBytes[3] = (byte)codeBytes.GetItem();
+                        szBytes[3] = (byte)codeBytes.Get();
                     }
                     else if (opcode == opcodetype.OP_PUSHDATA2)
                     {
                         // The next two bytes contain the number of bytes to be pushed onto the stack,
                         //    i.e. now your operation will seem like this: OP_PUSHDATA2 0x00 0x01 [0x5a]
-                        codeBytes.GetItems(2).CopyTo(szBytes, 2);
+                        codeBytes.Get(2).CopyTo(szBytes, 2);
                     }
                     else if (opcode == opcodetype.OP_PUSHDATA4)
                     {
                         // The next four bytes contain the number of bytes to be pushed onto the stack,
                         //   OP_PUSHDATA4 0x00 0x00 0x00 0x01 [0x5a]
-                        szBytes = codeBytes.GetItems(4);
+                        szBytes = codeBytes.Get(4);
                     }
                 }
                 catch (WrappedListException)
@@ -526,7 +526,7 @@ namespace Novacoin
                     try
                     {
                         // Read found number of bytes into list of OP_PUSHDATAn arguments.
-                        bytesRet = codeBytes.GetEnumerableItems(nSize);
+                        bytesRet = codeBytes.GetEnumerable(nSize);
                     }
                     catch (WrappedListException)
                     {
@@ -747,8 +747,8 @@ namespace Novacoin
                 opcodetype opcode1, opcode2;
 
                 // Compare
-                WrappedList<byte> wl1 = script1.GetWrappedList();
-                WrappedList<byte> wl2 = script2.GetWrappedList();
+                ByteQueue wl1 = script1.GetWrappedList();
+                ByteQueue wl2 = script2.GetWrappedList();
 
                 IEnumerable<byte> args1, args2;
 
@@ -757,7 +757,7 @@ namespace Novacoin
 
                 while (true)
                 {
-                    if (wl1.GetCurrentItem() == last1 && wl2.GetCurrentItem() == last2)
+                    if (wl1.GetCurrent() == last1 && wl2.GetCurrent() == last2)
                     {
                         // Found a match
                         typeRet = templateTuple.Item1;
index 5fa2685..35e8e8a 100644 (file)
@@ -102,18 +102,18 @@ namespace Novacoin
         /// </summary>
         /// <param name="wBytes"></param>
         /// <returns></returns>
-        public static ulong ReadVarInt(ref WrappedList<byte> wBytes)
+        public static ulong ReadVarInt(ref ByteQueue wBytes)
         {
-            byte prefix = wBytes.GetItem();
+            byte prefix = wBytes.Get();
 
             switch (prefix)
             {
                 case 0xfd: // ushort
-                    return BitConverter.ToUInt16(wBytes.GetItems(2), 0);
+                    return BitConverter.ToUInt16(wBytes.Get(2), 0);
                 case 0xfe: // uint
-                    return BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+                    return BitConverter.ToUInt32(wBytes.Get(4), 0);
                 case 0xff: // ulong
-                    return BitConverter.ToUInt64(wBytes.GetItems(8), 0);
+                    return BitConverter.ToUInt64(wBytes.Get(8), 0);
                 default:
                     return prefix;
             }
index 89c5cce..ff50096 100644 (file)
@@ -23,24 +23,24 @@ namespace Novacoin
         }
     }
 
-    public class WrappedList<T>
+    public class ByteQueue
     {
         private int Index;
-        private List<T> Elements;
+        private List<byte> Elements;
 
-        public WrappedList(IList<T> List, int Start)
+        public ByteQueue(IList<byte> List, int Start)
         {
-            Elements = new List<T>(List);
+            Elements = new List<byte>(List);
             Index = Start;
         }
 
-        public WrappedList(IList<T> List)
+        public ByteQueue(IList<byte> List)
         {
-            Elements = new List<T>(List);
+            Elements = new List<byte>(List);
             Index = 0;
         }
 
-        public T GetItem()
+        public byte Get()
         {
             if (Elements.Count <= Index)
             {
@@ -50,44 +50,44 @@ namespace Novacoin
             return Elements[Index++];
         }
 
-        public T GetCurrentItem()
+        public byte GetCurrent()
         {
             return Elements[Index];
         }
 
-        public T[] GetItems(int Count)
+        public byte[] Get(int Count)
         {
             if (Elements.Count - Index < Count)
             {
                 throw new WrappedListException("Unable to read requested amount of data.");
             }
 
-            T[] result = Elements.Skip(Index).Take(Count).ToArray();
+            byte[] result = Elements.Skip(Index).Take(Count).ToArray();
             Index += Count;
 
             return result;
         }
 
-        public T[] GetCurrentItems(int Count)
+        public byte[] GetCurrent(int Count)
         {
             if (Elements.Count - Index < Count)
             {
                 throw new WrappedListException("Unable to read requested amount of data.");
             }
 
-            T[] result = Elements.Skip(Index).Take(Count).ToArray();
+            byte[] result = Elements.Skip(Index).Take(Count).ToArray();
 
             return result;
         }
 
-        public IEnumerable<T> GetEnumerableItems(int Count)
+        public IEnumerable<byte> GetEnumerable(int Count)
         {
             if (Elements.Count - Index < Count)
             {
                 throw new WrappedListException("Unable to read requested amount of data.");
             }
 
-            IEnumerable<T> result = Elements.Skip(Index).Take(Count);
+            IEnumerable<byte> result = Elements.Skip(Index).Take(Count);
             Index += Count;
 
             return result;