Remove Hash, Hash256, Hash160 and ScryptHash256 classes.
[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 using System.IO;
24
25 namespace Novacoin
26 {
27     /// <summary>
28     /// Block header
29     /// </summary>
30     public class CBlockHeader
31         {
32                 /// <summary>
33                 /// Version of block schema.
34                 /// </summary>
35                 public uint nVersion;
36
37                 /// <summary>
38                 /// Previous block hash.
39                 /// </summary>
40                 public uint256 prevHash;
41
42                 /// <summary>
43                 /// Merkle root hash.
44                 /// </summary>
45                 public uint256 merkleRoot;
46
47                 /// <summary>
48                 /// Block timestamp.
49                 /// </summary>
50                 public uint nTime;
51
52                 /// <summary>
53                 /// Compressed difficulty representation.
54                 /// </summary>
55                 public uint nBits;
56
57                 /// <summary>
58                 /// Nonce counter.
59                 /// </summary>
60                 public uint nNonce;
61
62         /// <summary>
63         /// Initialize an empty instance
64         /// </summary>
65                 public CBlockHeader ()
66                 {
67             nVersion = 6;
68             prevHash = new uint256();
69             merkleRoot = new uint256();
70             nTime = Interop.GetTime();
71             nBits = 0;
72             nNonce = 0;
73                 }
74
75         public CBlockHeader(CBlockHeader header)
76         {
77             nVersion = header.nVersion;
78             prevHash = header.prevHash;
79             merkleRoot = header.merkleRoot;
80             nTime = header.nTime;
81             nBits = header.nBits;
82             nNonce = header.nNonce;
83         }
84
85         internal CBlockHeader(ref BinaryReader reader)
86         {
87             nVersion = reader.ReadUInt32();
88             prevHash = reader.ReadBytes(32);
89             merkleRoot = reader.ReadBytes(32);
90             nTime = reader.ReadUInt32();
91             nBits = reader.ReadUInt32();
92             nNonce = reader.ReadUInt32();
93         }
94
95         /// <summary>
96         /// Init block header with bytes.
97         /// </summary>
98         /// <param name="bytes">Byte array.</param>
99         public CBlockHeader(byte[] bytes)
100         {
101             Contract.Requires<ArgumentException>(bytes.Length == 80, "Any valid block header is exactly 80 bytes long.");
102
103             var stream = new MemoryStream(bytes);
104             var reader = new BinaryReader(stream);
105
106             nVersion = reader.ReadUInt32();
107             prevHash = reader.ReadBytes(32);
108             merkleRoot = reader.ReadBytes(32);
109             nTime = reader.ReadUInt32();
110             nBits = reader.ReadUInt32();
111             nNonce = reader.ReadUInt32();
112
113             reader.Close();
114         }
115
116         /// <summary>
117         /// Convert current block header instance into sequence of bytes
118         /// </summary>
119         /// <returns>Byte sequence</returns>
120         public static implicit operator byte[] (CBlockHeader header)
121         {
122             var stream = new MemoryStream();
123             var writer = new BinaryWriter(stream);
124
125             writer.Write(header.nVersion);
126             writer.Write(header.prevHash);
127             writer.Write(header.merkleRoot);
128             writer.Write(header.nTime);
129             writer.Write(header.nBits);
130             writer.Write(header.nNonce);
131
132             var resultBytes = stream.ToArray();
133
134             writer.Close();
135
136             return resultBytes;
137         }
138
139         public uint256 Hash
140         {
141             get {
142                 return CryptoUtils.ComputeScryptHash256(this);
143             }
144         }
145
146         public override string ToString()
147         {
148             var sb = new StringBuilder();
149             sb.AppendFormat("CBlockHeader(nVersion={0}, prevHash={1}, merkleRoot={2}, nTime={3}, nBits={4}, nNonce={5})", nVersion, prevHash, merkleRoot, nTime, nBits, nNonce);
150             return sb.ToString();
151         }
152         }
153 }