We're at little-endian only
[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         /// <summary>
28         /// Parse byte sequence and initialize new block instance
29         /// </summary>
30         /// <param name="blockBytes"></param>
31                 public CBlock (List<byte> blockBytes)
32                 {
33             header = new CBlockHeader();
34
35             WrappedList<byte> wBytes = new WrappedList<byte>(blockBytes);
36
37             // Fill the block header fields
38             header.nVersion = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
39             header.prevHash = new Hash256(wBytes.GetItems(32));
40             header.merkleRoot = new Hash256(wBytes.GetItems(32));
41             header.nTime = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
42             header.nBits = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
43             header.nNonce = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
44
45             // Parse transactions list
46             vtx = CTransaction.ReadTransactionsList(ref wBytes);
47
48             // Read block signature
49             signature = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
50                 }
51
52         /// <summary>
53         /// Convert current instance into sequence of bytes
54         /// </summary>
55         /// <returns>Byte sequence</returns>
56         public IList<byte> ToBytes()
57         {
58             List<byte> r = new List<byte>();
59
60             r.AddRange(header.ToBytes());
61             r.AddRange(VarInt.EncodeVarInt(vtx.LongLength)); // transactions count
62
63             foreach (CTransaction tx in vtx)
64             {
65                 r.AddRange(tx.ToBytes());
66             }
67
68             r.AddRange(VarInt.EncodeVarInt(signature.LongLength));
69             r.AddRange(signature);
70
71             return r;
72         }
73
74         public override string ToString()
75         {
76             StringBuilder sb = new StringBuilder();
77
78             sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
79
80             foreach(CTransaction tx in vtx)
81             {
82                 sb.AppendFormat("{0},\n", tx.ToString());
83             }
84
85             sb.AppendFormat("signature={0})\n", Interop.ToHex(signature));
86             
87             // TODO
88             return sb.ToString();
89         }
90         }
91 }
92