X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCBlock.cs;h=6ff2da3ffdf4c95c45ab3f2e960dac1218607d7f;hb=f1d2e217ae6e24a84edfa98ca3471a8a836ecaa7;hp=d3b402225f69b0c2dbc3d3623951cb94de380608;hpb=892af99303ad6d6ece3e57bebd36b521102df838;p=NovacoinLibrary.git diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index d3b4022..6ff2da3 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -17,18 +17,34 @@ */ using System; -using System.Linq; using System.Text; using System.Collections.Generic; -using System.Security.Cryptography; using System.Diagnostics.Contracts; namespace Novacoin { - /// - /// Represents the block. Block consists of header, transaction array and header signature. - /// - public class CBlock + [Serializable] + public class BlockConstructorException : Exception + { + public BlockConstructorException() + { + } + + public BlockConstructorException(string message) + : base(message) + { + } + + public BlockConstructorException(string message, Exception inner) + : base(message, inner) + { + } + } + + /// + /// Represents the block. Block consists of header, transaction array and header signature. + /// + public class CBlock { /// /// Block header. @@ -45,6 +61,10 @@ namespace Novacoin /// public byte[] signature = new byte[0]; + /// + /// Copy constructor. + /// + /// CBlock instance. public CBlock(CBlock b) { header = new CBlockHeader(b.header); @@ -61,19 +81,26 @@ namespace Novacoin /// /// Parse byte sequence and initialize new block instance /// - /// + /// Bytes sequence. public CBlock (byte[] blockBytes) { - ByteQueue wBytes = new ByteQueue(blockBytes); + try + { + ByteQueue wBytes = new ByteQueue(blockBytes); - // Fill the block header fields - header = new CBlockHeader(wBytes.Get(80)); + // Fill the block header fields + header = new CBlockHeader(wBytes.Get(80)); - // Parse transactions list - vtx = CTransaction.ReadTransactionsList(ref wBytes); + // Parse transactions list + vtx = CTransaction.ReadTransactionsList(ref wBytes); - // Read block signature - signature = wBytes.Get((int)wBytes.GetVarInt()); + // Read block signature + signature = wBytes.Get((int)wBytes.GetVarInt()); + } + catch (Exception e) + { + throw new BlockConstructorException("Deserialization failed", e); + } } public CBlock() @@ -199,7 +226,7 @@ namespace Novacoin for (int i = 0; i < nTx; i++) { - nOffset += vtx[nTx].Size; + nOffset += vtx[i].Size; } return nOffset;