Add new internal constructor for block header class.
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);
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>
/// 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>
}
var count = 0;
- var bq1 = new ByteQueue(ref codeBytes);
+ var bq1 = new InstructionQueue(ref codeBytes);
byte[] pushData;
instruction opcode;
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))
{
{
get
{
- var wCodeBytes = new ByteQueue(ref codeBytes);
+ var wCodeBytes = new InstructionQueue(ref codeBytes);
instruction opcode; // Current instruction
byte[] pushArgs; // OP_PUSHDATAn argument
{
get
{
- var wCodeBytes = new ByteQueue(ref codeBytes);
+ var wCodeBytes = new InstructionQueue(ref codeBytes);
byte[] pushArgs; // OP_PUSHDATAn argument
instruction opcode; // Current instruction
/// <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
// 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
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
return resultBytes;
}
-
-
public override string ToString()
{
var sb = new StringBuilder();
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);
}
{
if (_Stream.Position == _Stream.Length)
{
- throw new ByteQueueException("No elements left.");
+ throw new InstructionQueueException("No instructions left.");
}
return _Reader.ReadByte();
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);
}
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);
- }
- }
}
}
</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" />
/// <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;
nSize = BitConverter.ToInt32(codeBytes.Get(4), 0);
}
}
- catch (ByteQueueException)
+ catch (InstructionQueueException)
{
// Unable to read operand length
return false;
instruction opcode1, opcode2;
// Compare
- var bq1 = script1.GetByteQueue();
- var bq2 = script2.GetByteQueue();
+ var bq1 = script1.GetInstructionQueue();
+ var bq2 = script2.GetInstructionQueue();
byte[] args1, args2;
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