X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCBlock.cs;h=3f72c68117fd4288b4f426f094d68f016d8a7b2e;hb=8975fa73c3cd21a502eef8eaa0471dd6524d317f;hp=01bdb2774264450aefd5042f08fe082812f4c02d;hpb=6c6e3b0c68e764ea520dde48d5e44c757f1dabbb;p=NovacoinLibrary.git diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index 01bdb27..3f72c68 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 { @@ -46,6 +47,16 @@ namespace Novacoin /// public class CBlock { + /// + /// Maximum block size is 1Mb. + /// + public const uint nMaxBlockSize = 1000000; + + /// + /// Sanity threshold for amount of sigops. + /// + public const uint nMaxSigOps = 20000; + /// /// Block header. /// @@ -87,16 +98,19 @@ 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(ref reader); // 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) { @@ -118,7 +132,7 @@ namespace Novacoin uint nSigOps = 0; // total sigops // Basic sanity checkings - if (vtx.Length == 0 || Size > 1000000) + if (vtx.Length == 0 || Size > nMaxBlockSize) { return false; } @@ -241,7 +255,7 @@ namespace Novacoin } // Reject block if validation would consume too much resources. - if (nSigOps > 50000) + if (nSigOps > nMaxSigOps) { return false; } @@ -328,20 +342,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; } ///