Add license header.
[NovacoinLibrary.git] / Novacoin / CBlockHeader.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using System;
20 using System.Linq;
21 using System.Text;
22 using System.Collections.Generic;
23
24 namespace Novacoin
25 {
26         /// <summary>
27         /// Block header
28         /// </summary>
29         public class CBlockHeader
30         {
31                 /// <summary>
32                 /// Version of block schema.
33                 /// </summary>
34                 public uint nVersion = 6;
35
36                 /// <summary>
37                 /// Previous block hash.
38                 /// </summary>
39                 public Hash256 prevHash = new Hash256();
40
41                 /// <summary>
42                 /// Merkle root hash.
43                 /// </summary>
44                 public Hash256 merkleRoot = new Hash256();
45
46                 /// <summary>
47                 /// Block timestamp.
48                 /// </summary>
49                 public uint nTime = 0;
50
51                 /// <summary>
52                 /// Compressed difficulty representation.
53                 /// </summary>
54                 public uint nBits = 0;
55
56                 /// <summary>
57                 /// Nonce counter.
58                 /// </summary>
59                 public uint nNonce = 0;
60
61         /// <summary>
62         /// Initialize an empty instance
63         /// </summary>
64                 public CBlockHeader ()
65                 {
66                 }
67
68         public CBlockHeader(CBlockHeader h)
69         {
70             nVersion = h.nVersion;
71             prevHash = new Hash256(h.prevHash);
72             merkleRoot = new Hash256(h.merkleRoot);
73             nTime = h.nTime;
74             nBits = h.nBits;
75             nNonce = h.nNonce;
76         }
77
78         public CBlockHeader(byte[] bytes)
79         {
80             nVersion = BitConverter.ToUInt32(bytes, 0);
81             prevHash = new Hash256(bytes, 4);
82             merkleRoot = new Hash256(bytes, 36);
83             nTime = BitConverter.ToUInt32(bytes, 68);
84             nBits = BitConverter.ToUInt32(bytes, 72);
85             nNonce = BitConverter.ToUInt32(bytes, 76);
86         }
87
88         /// <summary>
89         /// Convert current block header instance into sequence of bytes
90         /// </summary>
91         /// <returns>Byte sequence</returns>
92         public IList<byte> Bytes
93         {
94             get
95             {
96                 List<byte> r = new List<byte>();
97
98                 r.AddRange(BitConverter.GetBytes(nVersion));
99                 r.AddRange(prevHash.hashBytes);
100                 r.AddRange(merkleRoot.hashBytes);
101                 r.AddRange(BitConverter.GetBytes(nTime));
102                 r.AddRange(BitConverter.GetBytes(nBits));
103                 r.AddRange(BitConverter.GetBytes(nNonce));
104
105                 return r;
106             }
107         }
108
109         public ScryptHash256 Hash
110         {
111             get {
112                 return ScryptHash256.Compute256(Bytes);
113             }
114         }
115
116         public override string ToString()
117         {
118             StringBuilder sb = new StringBuilder();
119             sb.AppendFormat("CBlockHeader(nVersion={0}, prevHash={1}, merkleRoot={2}, nTime={3}, nBits={4}, nNonce={5})", nVersion, prevHash.ToString(), merkleRoot.ToString(), nTime, nBits, nNonce);
120             return sb.ToString();
121         }
122         }
123 }