2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
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.
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.
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/>.
21 using System.Collections.Generic;
28 public class CBlockHeader
31 /// Version of block schema.
33 public uint nVersion = 6;
36 /// Previous block hash.
38 public Hash256 prevHash = new Hash256();
43 public Hash256 merkleRoot = new Hash256();
48 public uint nTime = 0;
51 /// Compressed difficulty representation.
53 public uint nBits = 0;
58 public uint nNonce = 0;
61 /// Initialize an empty instance
63 public CBlockHeader ()
67 public CBlockHeader(CBlockHeader h)
69 nVersion = h.nVersion;
70 prevHash = new Hash256(h.prevHash);
71 merkleRoot = new Hash256(h.merkleRoot);
77 public CBlockHeader(byte[] bytes)
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);
88 /// Convert current block header instance into sequence of bytes
90 /// <returns>Byte sequence</returns>
91 public static implicit operator byte[] (CBlockHeader h)
93 var r = new List<byte>();
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));
105 public ScryptHash256 Hash
108 return ScryptHash256.Compute256(this);
112 public override string ToString()
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();