using System.Text;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
+using System.IO;
namespace Novacoin
{
{
try
{
- ByteQueue wBytes = new ByteQueue(blockBytes);
+ var stream = new MemoryStream(blockBytes);
+ var reader = new BinaryReader(stream);
// Fill the block header fields
- header = new CBlockHeader(wBytes.Get(80));
+ 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();
// Parse transactions list
- vtx = CTransaction.ReadTransactionsList(ref wBytes);
+ vtx = CTransaction.ReadTransactionsList(ref reader);
// Read block signature
- signature = wBytes.Get((int)wBytes.GetVarInt());
+ signature = reader.ReadBytes((int)VarInt.ReadVarInt(ref reader));
+
+ reader.Close();
}
catch (Exception e)
{
/// <returns>Byte sequence</returns>
public static implicit operator byte[] (CBlock b)
{
- var r = new List<byte>();
+ var stream = new MemoryStream();
+ var writer = new BinaryWriter(stream);
- r.AddRange((byte[])b.header);
- r.AddRange(VarInt.EncodeVarInt(b.vtx.LongLength)); // transactions count
+ writer.Write(b.header);
+ writer.Write(VarInt.EncodeVarInt(b.vtx.LongLength));
foreach (var tx in b.vtx)
{
- r.AddRange((byte[])tx);
+ writer.Write(tx);
}
- r.AddRange(VarInt.EncodeVarInt(b.signature.LongLength));
- r.AddRange(b.signature);
+ writer.Write(VarInt.EncodeVarInt(b.signature.LongLength));
+ writer.Write(b.signature);
+
+ var resultBytes = stream.ToArray();
+
+ writer.Close();
- return r.ToArray();
+ return resultBytes;
}
/// <summary>