Add ToBytes() implementation for CBlock and CBlockHeader
[NovacoinLibrary.git] / Novacoin / CBlockHeader.cs
1 \feffusing System;
2 using System.Text;
3 using System.Collections.Generic;
4
5 namespace Novacoin
6 {
7         /// <summary>
8         /// Block header
9         /// </summary>
10         public class CBlockHeader
11         {
12                 /// <summary>
13                 /// Version of block schema.
14                 /// </summary>
15                 public uint nVersion = 6;
16
17                 /// <summary>
18                 /// Previous block hash.
19                 /// </summary>
20                 public Hash256 prevHash = new Hash256();
21
22                 /// <summary>
23                 /// Merkle root hash.
24                 /// </summary>
25                 public Hash256 merkleRoot = new Hash256();
26
27                 /// <summary>
28                 /// Block timestamp.
29                 /// </summary>
30                 public uint nTime = 0;
31
32                 /// <summary>
33                 /// Compressed difficulty representation.
34                 /// </summary>
35                 public uint nBits = 0;
36
37                 /// <summary>
38                 /// Nonce counter.
39                 /// </summary>
40                 public uint nNonce = 0;
41
42         /// <summary>
43         /// Initialize an empty instance
44         /// </summary>
45                 public CBlockHeader ()
46                 {
47                 }
48
49         /// <summary>
50         /// Convert current block header instance into sequence of bytes
51         /// </summary>
52         /// <returns>Byte sequence</returns>
53         public IList<byte> ToBytes()
54         {
55             List<byte> r = new List<byte>();
56
57             r.AddRange(Interop.LEBytes(nVersion));
58             r.AddRange(prevHash.hashBytes);
59             r.AddRange(merkleRoot.hashBytes);
60             r.AddRange(Interop.LEBytes(nTime));
61             r.AddRange(Interop.LEBytes(nBits));
62             r.AddRange(Interop.LEBytes(nNonce));
63
64             return r;
65         }
66
67         public override string ToString()
68         {
69             StringBuilder sb = new StringBuilder();
70             sb.AppendFormat("CBlockHeader(nVersion={0}, prevHash={1}, merkleRoot={2}, nTime={3}, nBits={4}, nNonce={5})", nVersion, prevHash.ToString(), merkleRoot.ToString(), nTime, nBits, nNonce);
71             return sb.ToString();
72         }
73         }
74 }