X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCBlock.cs;h=101d20ee6a613c3217e0922a131bff2e1de46ba9;hb=161220f258cf9eb640d354951a48b45598301b1a;hp=30350b71ce333e9209f74faf66c081dde341b904;hpb=4c45f0efb0bd8451c8b9a02833ce007477534751;p=NovacoinLibrary.git diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index 30350b7..101d20e 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -20,6 +20,7 @@ using System; using System.Text; using System.Collections.Generic; using System.Diagnostics.Contracts; +using System.IO; namespace Novacoin { @@ -87,16 +88,25 @@ 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) { @@ -185,13 +195,13 @@ namespace Novacoin } // Check timestamp - if (header.nTime > NetInfo.FutureDrift(NetInfo.GetAdjustedTime())) + if (header.nTime > NetUtils.FutureDrift(NetUtils.GetAdjustedTime())) { return false; } // Check coinbase timestamp - if (header.nTime < NetInfo.PastDrift(vtx[0].nTime)) + if (header.nTime < NetUtils.PastDrift(vtx[0].nTime)) { return false; } @@ -328,20 +338,25 @@ namespace Novacoin /// Byte sequence public static implicit operator byte[] (CBlock b) { - var r = new List(); + 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; } ///