From 031d9e2250687721838d86647625a6c949f91de6 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Tue, 1 Sep 2015 02:00:37 +0300 Subject: [PATCH] Replace ByteQueue with its simplified version, InstructionQueue, since it's used for scripts only. Add new internal constructor for block header class. --- Novacoin/ByteQueue.cs | 181 ------------------------------------------ Novacoin/CBlock.cs | 8 +-- Novacoin/CBlockHeader.cs | 10 +++ Novacoin/CScript.cs | 18 ++-- Novacoin/CTransaction.cs | 2 - Novacoin/InstructionQueue.cs | 145 +++++++++++++++++++++++++++++++++ Novacoin/Novacoin.csproj | 2 +- Novacoin/ScriptCode.cs | 10 +- 8 files changed, 171 insertions(+), 205 deletions(-) delete mode 100644 Novacoin/ByteQueue.cs create mode 100644 Novacoin/InstructionQueue.cs diff --git a/Novacoin/ByteQueue.cs b/Novacoin/ByteQueue.cs deleted file mode 100644 index 8af351f..0000000 --- a/Novacoin/ByteQueue.cs +++ /dev/null @@ -1,181 +0,0 @@ -/** - * Novacoin classes library - * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com) - - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -using System; -using System.Collections.Generic; -using System.Diagnostics.Contracts; -using System.IO; - -namespace Novacoin -{ - [Serializable] - public class ByteQueueException : Exception - { - public ByteQueueException() - { - } - - public ByteQueueException(string message) - : base(message) - { - } - - public ByteQueueException(string message, Exception inner) - : base(message, inner) - { - } - } - - /// - /// Stream of bytes. - /// - /// TODO: rewrite using MemoryStream - /// - public class ByteQueue : 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 List, int Start) - { - _Stream = new MemoryStream(List.ToArray()); - _Stream.Seek(Start, SeekOrigin.Begin); - _Reader = new BinaryReader(_Stream); - } - - public ByteQueue(ref List List) - { - _Stream = new MemoryStream(List.ToArray()); - _Reader = new BinaryReader(_Stream); - } - - ~ByteQueue() - { - Dispose(false); - } - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - protected virtual void Dispose(bool disposing) - { - if (!disposed) - { - if (disposing) - { - _Reader.Dispose(); - _Stream.Dispose(); - } - - disposed = true; - } - } - - public byte Get() - { - if (_Stream.Position == _Stream.Length) - { - throw new ByteQueueException("No elements left."); - } - - return _Reader.ReadByte(); - } - - public bool TryGet(ref byte Element) - { - if (_Stream.Position == _Stream.Length) - { - return false; - } - - Element = _Reader.ReadByte(); - - return true; - } - - public byte[] Get(int nCount) - { - Contract.Requires(Count - Index >= nCount, "nCount is greater than amount of elements."); - - return _Reader.ReadBytes(nCount); - } - - public bool TryGet(int nCount, ref byte[] Elements) - { - Elements = _Reader.ReadBytes(nCount); - return (Elements.Length == nCount); - } - - /// - /// Current index value - /// - public int Index - { - get { return (int)_Stream.Position; } - } - - 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); - } - } - } -} - - diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index 101d20e..d8d7a75 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -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); diff --git a/Novacoin/CBlockHeader.cs b/Novacoin/CBlockHeader.cs index d56df33..29abc7d 100644 --- a/Novacoin/CBlockHeader.cs +++ b/Novacoin/CBlockHeader.cs @@ -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(); + } + /// /// Init block header with bytes. /// diff --git a/Novacoin/CScript.cs b/Novacoin/CScript.cs index 229b60b..1418a25 100644 --- a/Novacoin/CScript.cs +++ b/Novacoin/CScript.cs @@ -52,9 +52,9 @@ namespace Novacoin /// Return a new instance of ByteQueue object for current code bytes /// /// - public ByteQueue GetByteQueue() + public InstructionQueue GetInstructionQueue() { - return new ByteQueue(ref codeBytes); + return new InstructionQueue(ref codeBytes); } /// @@ -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 /// Amount of sigops 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 diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index 5cbd54c..ef444f2 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -429,8 +429,6 @@ namespace Novacoin return resultBytes; } - - public override string ToString() { var sb = new StringBuilder(); diff --git a/Novacoin/InstructionQueue.cs b/Novacoin/InstructionQueue.cs new file mode 100644 index 0000000..599f525 --- /dev/null +++ b/Novacoin/InstructionQueue.cs @@ -0,0 +1,145 @@ +/** + * Novacoin classes library + * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com) + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +using System; +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.IO; + +namespace Novacoin +{ + [Serializable] + public class InstructionQueueException : Exception + { + public InstructionQueueException() + { + } + + public InstructionQueueException(string message) + : base(message) + { + } + + public InstructionQueueException(string message, Exception inner) + : base(message, inner) + { + } + } + + /// + /// Stream of instructions. + /// + public class InstructionQueue : IDisposable + { + private bool disposed = false; + + private MemoryStream _Stream; + private BinaryReader _Reader; + + public InstructionQueue(ref List List, int Start) + { + _Stream = new MemoryStream(List.ToArray()); + _Stream.Seek(Start, SeekOrigin.Begin); + _Reader = new BinaryReader(_Stream); + } + + public InstructionQueue(ref List List) + { + _Stream = new MemoryStream(List.ToArray()); + _Reader = new BinaryReader(_Stream); + } + + ~InstructionQueue() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (!disposed) + { + if (disposing) + { + _Reader.Dispose(); + _Stream.Dispose(); + } + + disposed = true; + } + } + + public byte Get() + { + if (_Stream.Position == _Stream.Length) + { + throw new InstructionQueueException("No instructions left."); + } + + return _Reader.ReadByte(); + } + + public bool TryGet(ref byte Element) + { + if (_Stream.Position == _Stream.Length) + { + return false; + } + + Element = _Reader.ReadByte(); + + return true; + } + + public byte[] Get(int nCount) + { + Contract.Requires(Count - Index >= nCount, "nCount is greater than amount of instructions."); + + return _Reader.ReadBytes(nCount); + } + + public bool TryGet(int nCount, ref byte[] Elements) + { + Elements = _Reader.ReadBytes(nCount); + return (Elements.Length == nCount); + } + + /// + /// Current index value + /// + public int Index + { + get { return (int)_Stream.Position; } + } + + /// + /// Stream length + /// + public int Count + { + get { return (int)_Stream.Length; } + } + } +} + + diff --git a/Novacoin/Novacoin.csproj b/Novacoin/Novacoin.csproj index c15b923..6d483af 100644 --- a/Novacoin/Novacoin.csproj +++ b/Novacoin/Novacoin.csproj @@ -107,7 +107,7 @@ - + diff --git a/Novacoin/ScriptCode.cs b/Novacoin/ScriptCode.cs index f1e6efd..df1d773 100644 --- a/Novacoin/ScriptCode.cs +++ b/Novacoin/ScriptCode.cs @@ -247,7 +247,7 @@ namespace Novacoin /// Found instruction. /// IEnumerable out param which is used to get the push arguments. /// Result of operation - 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(); #if !DEBUG -- 1.7.1