CBlockHeader hash property
[NovacoinLibrary.git] / Novacoin / CBlockHeader.cs
1 \feffusing System;
2 using System.Linq;
3 using System.Text;
4 using System.Collections.Generic;
5
6 namespace Novacoin
7 {
8         /// <summary>
9         /// Block header
10         /// </summary>
11         public class CBlockHeader
12         {
13                 /// <summary>
14                 /// Version of block schema.
15                 /// </summary>
16                 public uint nVersion = 6;
17
18                 /// <summary>
19                 /// Previous block hash.
20                 /// </summary>
21                 public Hash256 prevHash = new Hash256();
22
23                 /// <summary>
24                 /// Merkle root hash.
25                 /// </summary>
26                 public Hash256 merkleRoot = new Hash256();
27
28                 /// <summary>
29                 /// Block timestamp.
30                 /// </summary>
31                 public uint nTime = 0;
32
33                 /// <summary>
34                 /// Compressed difficulty representation.
35                 /// </summary>
36                 public uint nBits = 0;
37
38                 /// <summary>
39                 /// Nonce counter.
40                 /// </summary>
41                 public uint nNonce = 0;
42
43         /// <summary>
44         /// Initialize an empty instance
45         /// </summary>
46                 public CBlockHeader ()
47                 {
48                 }
49
50         public CBlockHeader(CBlockHeader h)
51         {
52             nVersion = h.nVersion;
53             prevHash = new Hash256(h.prevHash);
54             merkleRoot = new Hash256(h.merkleRoot);
55             nTime = h.nTime;
56             nBits = h.nBits;
57             nNonce = h.nNonce;
58         }
59
60         public CBlockHeader(byte[] bytes)
61         {
62             nVersion = BitConverter.ToUInt32(bytes, 0);
63             prevHash = new Hash256(bytes, 4);
64             merkleRoot = new Hash256(bytes, 36);
65             nTime = BitConverter.ToUInt32(bytes, 68);
66             nBits = BitConverter.ToUInt32(bytes, 72);
67             nNonce = BitConverter.ToUInt32(bytes, 76);
68         }
69
70         /// <summary>
71         /// Convert current block header instance into sequence of bytes
72         /// </summary>
73         /// <returns>Byte sequence</returns>
74         public IList<byte> Bytes
75         {
76             get
77             {
78                 List<byte> r = new List<byte>();
79
80                 r.AddRange(BitConverter.GetBytes(nVersion));
81                 r.AddRange(prevHash.hashBytes);
82                 r.AddRange(merkleRoot.hashBytes);
83                 r.AddRange(BitConverter.GetBytes(nTime));
84                 r.AddRange(BitConverter.GetBytes(nBits));
85                 r.AddRange(BitConverter.GetBytes(nNonce));
86
87                 return r;
88             }
89         }
90
91         public ScryptHash256 Hash
92         {
93             get {
94                 return ScryptHash256.Compute256(Bytes);
95             }
96         }
97
98         public override string ToString()
99         {
100             StringBuilder sb = new StringBuilder();
101             sb.AppendFormat("CBlockHeader(nVersion={0}, prevHash={1}, merkleRoot={2}, nTime={3}, nBits={4}, nNonce={5})", nVersion, prevHash.ToString(), merkleRoot.ToString(), nTime, nBits, nNonce);
102             return sb.ToString();
103         }
104         }
105 }