Use implicit type casting operator for 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
23 namespace Novacoin
24 {
25     /// <summary>
26     /// Block header
27     /// </summary>
28     public class CBlockHeader
29         {
30                 /// <summary>
31                 /// Version of block schema.
32                 /// </summary>
33                 public uint nVersion = 6;
34
35                 /// <summary>
36                 /// Previous block hash.
37                 /// </summary>
38                 public Hash256 prevHash = new Hash256();
39
40                 /// <summary>
41                 /// Merkle root hash.
42                 /// </summary>
43                 public Hash256 merkleRoot = new Hash256();
44
45                 /// <summary>
46                 /// Block timestamp.
47                 /// </summary>
48                 public uint nTime = 0;
49
50                 /// <summary>
51                 /// Compressed difficulty representation.
52                 /// </summary>
53                 public uint nBits = 0;
54
55                 /// <summary>
56                 /// Nonce counter.
57                 /// </summary>
58                 public uint nNonce = 0;
59
60         /// <summary>
61         /// Initialize an empty instance
62         /// </summary>
63                 public CBlockHeader ()
64                 {
65                 }
66
67         public CBlockHeader(CBlockHeader h)
68         {
69             nVersion = h.nVersion;
70             prevHash = new Hash256(h.prevHash);
71             merkleRoot = new Hash256(h.merkleRoot);
72             nTime = h.nTime;
73             nBits = h.nBits;
74             nNonce = h.nNonce;
75         }
76
77         public CBlockHeader(byte[] bytes)
78         {
79             nVersion = BitConverter.ToUInt32(bytes, 0);
80             prevHash = new Hash256(bytes, 4);
81             merkleRoot = new Hash256(bytes, 36);
82             nTime = BitConverter.ToUInt32(bytes, 68);
83             nBits = BitConverter.ToUInt32(bytes, 72);
84             nNonce = BitConverter.ToUInt32(bytes, 76);
85         }
86
87         /// <summary>
88         /// Convert current block header instance into sequence of bytes
89         /// </summary>
90         /// <returns>Byte sequence</returns>
91         public static implicit operator byte[] (CBlockHeader h)
92         {
93             var r = new List<byte>();
94
95             r.AddRange(BitConverter.GetBytes(h.nVersion));
96             r.AddRange((byte[])h.prevHash);
97             r.AddRange((byte[])h.merkleRoot);
98             r.AddRange(BitConverter.GetBytes(h.nTime));
99             r.AddRange(BitConverter.GetBytes(h.nBits));
100             r.AddRange(BitConverter.GetBytes(h.nNonce));
101
102             return r.ToArray();
103         }
104
105         public ScryptHash256 Hash
106         {
107             get {
108                 return ScryptHash256.Compute256(this);
109             }
110         }
111
112         public override string ToString()
113         {
114             var sb = new StringBuilder();
115             sb.AppendFormat("CBlockHeader(nVersion={0}, prevHash={1}, merkleRoot={2}, nTime={3}, nBits={4}, nNonce={5})", nVersion, prevHash.ToString(), merkleRoot.ToString(), nTime, nBits, nNonce);
116             return sb.ToString();
117         }
118         }
119 }