b9c9a4cb4e3c4dc3d2f759a1ae7e53acba812f64
[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.Text;
21 using System.Collections.Generic;
22 using System.Diagnostics.Contracts;
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             Contract.Requires<ArgumentException>(bytes.Length == 80, "Any valid block header is exactly 80 bytes long.");
81
82             nVersion = BitConverter.ToUInt32(bytes, 0);
83             prevHash = new Hash256(bytes, 4);
84             merkleRoot = new Hash256(bytes, 36);
85             nTime = BitConverter.ToUInt32(bytes, 68);
86             nBits = BitConverter.ToUInt32(bytes, 72);
87             nNonce = BitConverter.ToUInt32(bytes, 76);
88         }
89
90         /// <summary>
91         /// Convert current block header instance into sequence of bytes
92         /// </summary>
93         /// <returns>Byte sequence</returns>
94         public static implicit operator byte[] (CBlockHeader h)
95         {
96             var r = new List<byte>();
97
98             r.AddRange(BitConverter.GetBytes(h.nVersion));
99             r.AddRange((byte[])h.prevHash);
100             r.AddRange((byte[])h.merkleRoot);
101             r.AddRange(BitConverter.GetBytes(h.nTime));
102             r.AddRange(BitConverter.GetBytes(h.nBits));
103             r.AddRange(BitConverter.GetBytes(h.nNonce));
104
105             return r.ToArray();
106         }
107
108         public ScryptHash256 Hash
109         {
110             get {
111                 return ScryptHash256.Compute256(this);
112             }
113         }
114
115         public override string ToString()
116         {
117             var sb = new StringBuilder();
118             sb.AppendFormat("CBlockHeader(nVersion={0}, prevHash={1}, merkleRoot={2}, nTime={3}, nBits={4}, nNonce={5})", nVersion, prevHash.ToString(), merkleRoot.ToString(), nTime, nBits, nNonce);
119             return sb.ToString();
120         }
121         }
122 }