From d594e4dba0ffa4414741069eb90a8754a119a819 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Fri, 21 Aug 2015 21:58:19 +0300 Subject: [PATCH] Generic class was a bit excessive for our purposes --- Novacoin/CBlock.cs | 6 +++--- Novacoin/CScript.cs | 16 ++++++++-------- Novacoin/CTransaction.cs | 26 +++++++++++++------------- Novacoin/CTxIn.cs | 8 ++++---- Novacoin/CTxOut.cs | 6 +++--- Novacoin/ScriptCode.cs | 20 ++++++++++---------- Novacoin/VarInt.cs | 10 +++++----- Novacoin/WrappedList.cs | 28 ++++++++++++++-------------- 8 files changed, 60 insertions(+), 60 deletions(-) diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index 8974cef..8f6fb76 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -42,16 +42,16 @@ namespace Novacoin /// public CBlock (IList blockBytes) { - WrappedList wBytes = new WrappedList(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() diff --git a/Novacoin/CScript.cs b/Novacoin/CScript.cs index 4223791..777793b 100644 --- a/Novacoin/CScript.cs +++ b/Novacoin/CScript.cs @@ -47,12 +47,12 @@ namespace Novacoin } /// - /// Return a new instance of WrappedList object for current code bytes + /// Return a new instance of ByteQueue object for current code bytes /// /// - public WrappedList GetWrappedList() + public ByteQueue GetWrappedList() { - return new WrappedList(codeBytes); + return new ByteQueue(codeBytes); } /// @@ -178,7 +178,7 @@ namespace Novacoin { get { - WrappedList wCodeBytes = new WrappedList(codeBytes); + ByteQueue wCodeBytes = new ByteQueue(codeBytes); opcodetype opcode; // Current opcode IEnumerable pushArgs; // OP_PUSHDATAn argument @@ -204,7 +204,7 @@ namespace Novacoin { get { - WrappedList wCodeBytes = new WrappedList(codeBytes); + ByteQueue wCodeBytes = new ByteQueue(codeBytes); opcodetype opcode; // Current opcode IEnumerable pushArgs; // OP_PUSHDATAn argument @@ -293,7 +293,7 @@ namespace Novacoin /// Amount of sigops public int GetSigOpCount(bool fAccurate) { - WrappedList wCodeBytes = new WrappedList(codeBytes); + ByteQueue wCodeBytes = new ByteQueue(codeBytes); opcodetype opcode; // Current opcode IEnumerable 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 wScriptSig = scriptSig.GetWrappedList(); + ByteQueue wScriptSig = scriptSig.GetWrappedList(); opcodetype opcode; // Current opcode IEnumerable pushArgs; // OP_PUSHDATAn argument @@ -444,7 +444,7 @@ namespace Novacoin public override string ToString() { StringBuilder sb = new StringBuilder(); - WrappedList wCodeBytes = new WrappedList(codeBytes); + ByteQueue wCodeBytes = new ByteQueue(codeBytes); opcodetype opcode; // Current opcode IEnumerable pushArgs; // OP_PUSHDATAn argument diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index 8e1f871..cd66ada 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -79,10 +79,10 @@ namespace Novacoin /// Byte sequence public CTransaction(IList txBytes) { - WrappedList wBytes = new WrappedList(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); } /// @@ -121,7 +121,7 @@ namespace Novacoin /// /// Bytes sequence /// Transactions array - public static CTransaction[] ReadTransactionsList(ref WrappedList 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; diff --git a/Novacoin/CTxIn.cs b/Novacoin/CTxIn.cs index fad0d7e..8cff600 100644 --- a/Novacoin/CTxIn.cs +++ b/Novacoin/CTxIn.cs @@ -49,7 +49,7 @@ namespace Novacoin /// /// Reference to byte sequence /// Inputs array - public static CTxIn[] ReadTxInList(ref WrappedList 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 diff --git a/Novacoin/CTxOut.cs b/Novacoin/CTxOut.cs index 8c8ad4f..8acab9b 100644 --- a/Novacoin/CTxOut.cs +++ b/Novacoin/CTxOut.cs @@ -42,7 +42,7 @@ namespace Novacoin /// /// Reference to byte sequence /// Outputs array - public static CTxOut[] ReadTxOutList(ref WrappedList 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; diff --git a/Novacoin/ScriptCode.cs b/Novacoin/ScriptCode.cs index c5b92a3..17d79a8 100644 --- a/Novacoin/ScriptCode.cs +++ b/Novacoin/ScriptCode.cs @@ -459,11 +459,11 @@ namespace Novacoin /// /// Get next opcode from passed list of bytes and extract push arguments if there are some. /// - /// WrappedList reference. + /// ByteQueue reference. /// Found opcode. /// IEnumerable out param which is used to get the push arguments. /// Result of operation - public static bool GetOp(ref WrappedList codeBytes, out opcodetype opcodeRet, out IEnumerable bytesRet) + public static bool GetOp(ref ByteQueue codeBytes, out opcodetype opcodeRet, out IEnumerable bytesRet) { bytesRet = new List(); 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 wl1 = script1.GetWrappedList(); - WrappedList wl2 = script2.GetWrappedList(); + ByteQueue wl1 = script1.GetWrappedList(); + ByteQueue wl2 = script2.GetWrappedList(); IEnumerable 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; diff --git a/Novacoin/VarInt.cs b/Novacoin/VarInt.cs index 5fa2685..35e8e8a 100644 --- a/Novacoin/VarInt.cs +++ b/Novacoin/VarInt.cs @@ -102,18 +102,18 @@ namespace Novacoin /// /// /// - public static ulong ReadVarInt(ref WrappedList 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; } diff --git a/Novacoin/WrappedList.cs b/Novacoin/WrappedList.cs index 89c5cce..ff50096 100644 --- a/Novacoin/WrappedList.cs +++ b/Novacoin/WrappedList.cs @@ -23,24 +23,24 @@ namespace Novacoin } } - public class WrappedList + public class ByteQueue { private int Index; - private List Elements; + private List Elements; - public WrappedList(IList List, int Start) + public ByteQueue(IList List, int Start) { - Elements = new List(List); + Elements = new List(List); Index = Start; } - public WrappedList(IList List) + public ByteQueue(IList List) { - Elements = new List(List); + Elements = new List(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 GetEnumerableItems(int Count) + public IEnumerable GetEnumerable(int Count) { if (Elements.Count - Index < Count) { throw new WrappedListException("Unable to read requested amount of data."); } - IEnumerable result = Elements.Skip(Index).Take(Count); + IEnumerable result = Elements.Skip(Index).Take(Count); Index += Count; return result; -- 1.7.1