Removal of ReadVarInt
[NovacoinLibrary.git] / Novacoin / CBlock.cs
1 \feffusing System;
2 using System.Text;
3 using System.Collections.Generic;
4
5 namespace Novacoin
6 {
7         /// <summary>
8         /// Represents the block. Block consists of header, transaction array and header signature.
9         /// </summary>
10         public class CBlock
11         {
12                 /// <summary>
13                 /// Block header.
14                 /// </summary>
15                 public CBlockHeader header;
16
17                 /// <summary>
18                 /// Transactions array.
19                 /// </summary>
20                 public CTransaction[] vtx;
21
22                 /// <summary>
23                 /// Block header signature.
24                 /// </summary>
25                 public byte[] signature;
26
27         public CBlock(CBlock b)
28         {
29             header = new CBlockHeader(b.header);
30
31             for (int i = 0; i < b.vtx.Length; i++)
32             {
33                 vtx[i] = new CTransaction(b.vtx[i]);
34             }
35
36             b.signature.CopyTo(signature, 0);
37         }
38
39         /// <summary>
40         /// Parse byte sequence and initialize new block instance
41         /// </summary>
42         /// <param name="blockBytes"></param>
43                 public CBlock (IList<byte> blockBytes)
44                 {
45             ByteQueue wBytes = new ByteQueue(blockBytes);
46
47             // Fill the block header fields
48             header = new CBlockHeader(wBytes.Get(80));
49
50             // Parse transactions list
51             vtx = CTransaction.ReadTransactionsList(ref wBytes);
52
53             // Read block signature
54             signature = wBytes.Get((int)wBytes.GetVarInt());
55                 }
56
57         public CBlock()
58         {
59             // Initialize empty array of transactions. Please note that such 
60             // configuration is not valid real block since it has to provide 
61             // at least one transaction.
62             vtx = new CTransaction[0];
63         }
64
65         /// <summary>
66         /// Convert current instance into sequence of bytes
67         /// </summary>
68         /// <returns>Byte sequence</returns>
69         public IList<byte> Bytes 
70         {
71             get
72             {
73                 List<byte> r = new List<byte>();
74
75                 r.AddRange(header.Bytes);
76                 r.AddRange(VarInt.EncodeVarInt(vtx.LongLength)); // transactions count
77
78                 foreach (CTransaction tx in vtx)
79                 {
80                     r.AddRange(tx.Bytes);
81                 }
82
83                 r.AddRange(VarInt.EncodeVarInt(signature.LongLength));
84                 r.AddRange(signature);
85
86                 return r;
87             }
88         }
89
90         public override string ToString()
91         {
92             StringBuilder sb = new StringBuilder();
93
94             sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
95
96             foreach(CTransaction tx in vtx)
97             {
98                 sb.AppendFormat("{0},\n", tx.ToString());
99             }
100
101             sb.AppendFormat("signature={0})\n", Interop.ToHex(signature));
102             
103             // TODO
104             return sb.ToString();
105         }
106         }
107 }
108