CBlock: serialization
[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 ScryptHash256 prevHash;
41
42                 /// <summary>
43                 /// Merkle root hash.
44                 /// </summary>
45                 public Hash256 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 ScryptHash256();
69             merkleRoot = new Hash256();
70             nTime = Interop.GetTime();
71             nBits = 0;
72             nNonce = 0;
73                 }
74
75         public CBlockHeader(CBlockHeader h)
76         {
77             nVersion = h.nVersion;
78             prevHash = new ScryptHash256(h.prevHash);
79             merkleRoot = new Hash256(h.merkleRoot);
80             nTime = h.nTime;
81             nBits = h.nBits;
82             nNonce = h.nNonce;
83         }
84
85         /// <summary>
86         /// Init block header with bytes.
87         /// </summary>
88         /// <param name="bytes">Byte array.</param>
89         public CBlockHeader(byte[] bytes)
90         {
91             Contract.Requires<ArgumentException>(bytes.Length == 80, "Any valid block header is exactly 80 bytes long.");
92
93             var stream = new MemoryStream(bytes);
94             var reader = new BinaryReader(stream);
95
96             nVersion = reader.ReadUInt32();
97             prevHash = new ScryptHash256(reader.ReadBytes(32));
98             merkleRoot = new Hash256(reader.ReadBytes(32));
99             nTime = reader.ReadUInt32();
100             nBits = reader.ReadUInt32();
101             nNonce = reader.ReadUInt32();
102
103             reader.Close();
104         }
105
106         /// <summary>
107         /// Convert current block header instance into sequence of bytes
108         /// </summary>
109         /// <returns>Byte sequence</returns>
110         public static implicit operator byte[] (CBlockHeader h)
111         {
112             var stream = new MemoryStream();
113             var writer = new BinaryWriter(stream);
114
115             writer.Write(h.nVersion);
116             writer.Write(h.prevHash);
117             writer.Write(h.merkleRoot);
118             writer.Write(h.nTime);
119             writer.Write(h.nBits);
120             writer.Write(h.nNonce);
121
122             var resultBytes = stream.ToArray();
123
124             writer.Close();
125
126             return resultBytes;
127         }
128
129         public ScryptHash256 Hash
130         {
131             get {
132                 return ScryptHash256.Compute256(this);
133             }
134         }
135
136         public override string ToString()
137         {
138             var sb = new StringBuilder();
139             sb.AppendFormat("CBlockHeader(nVersion={0}, prevHash={1}, merkleRoot={2}, nTime={3}, nBits={4}, nNonce={5})", nVersion, prevHash.ToString(), merkleRoot.ToString(), nTime, nBits, nNonce);
140             return sb.ToString();
141         }
142         }
143 }