Copy constructors for block, block header and hash classes
[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             header = new CBlockHeader();
46
47             WrappedList<byte> wBytes = new WrappedList<byte>(blockBytes);
48
49             // Fill the block header fields
50             header.nVersion = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
51             header.prevHash = new Hash256(wBytes.GetItems(32));
52             header.merkleRoot = new Hash256(wBytes.GetItems(32));
53             header.nTime = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
54             header.nBits = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
55             header.nNonce = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
56
57             // Parse transactions list
58             vtx = CTransaction.ReadTransactionsList(ref wBytes);
59
60             // Read block signature
61             signature = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
62                 }
63
64         public CBlock()
65         {
66             // Initialize empty array of transactions. Please note that such 
67             // configuration is not valid real block since it has to provide 
68             // at least one transaction.
69             vtx = new CTransaction[0];
70         }
71
72         /// <summary>
73         /// Convert current instance into sequence of bytes
74         /// </summary>
75         /// <returns>Byte sequence</returns>
76         public IList<byte> Bytes 
77         {
78             get
79             {
80                 List<byte> r = new List<byte>();
81
82                 r.AddRange(header.Bytes);
83                 r.AddRange(VarInt.EncodeVarInt(vtx.LongLength)); // transactions count
84
85                 foreach (CTransaction tx in vtx)
86                 {
87                     r.AddRange(tx.Bytes);
88                 }
89
90                 r.AddRange(VarInt.EncodeVarInt(signature.LongLength));
91                 r.AddRange(signature);
92
93                 return r;
94             }
95         }
96
97         public override string ToString()
98         {
99             StringBuilder sb = new StringBuilder();
100
101             sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
102
103             foreach(CTransaction tx in vtx)
104             {
105                 sb.AppendFormat("{0},\n", tx.ToString());
106             }
107
108             sb.AppendFormat("signature={0})\n", Interop.ToHex(signature));
109             
110             // TODO
111             return sb.ToString();
112         }
113         }
114 }
115