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;
22 using System.Diagnostics.Contracts;
29 public class CBlockHeader
32 /// Version of block schema.
34 public uint nVersion = 6;
37 /// Previous block hash.
39 public ScryptHash256 prevHash = new ScryptHash256();
44 public Hash256 merkleRoot = new Hash256();
49 public uint nTime = 0;
52 /// Compressed difficulty representation.
54 public uint nBits = 0;
59 public uint nNonce = 0;
62 /// Initialize an empty instance
64 public CBlockHeader ()
68 public CBlockHeader(CBlockHeader h)
70 nVersion = h.nVersion;
71 prevHash = new ScryptHash256(h.prevHash);
72 merkleRoot = new Hash256(h.merkleRoot);
78 public CBlockHeader(byte[] bytes)
80 Contract.Requires<ArgumentException>(bytes.Length == 80, "Any valid block header is exactly 80 bytes long.");
82 nVersion = BitConverter.ToUInt32(bytes, 0);
83 prevHash = new ScryptHash256(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);
91 /// Convert current block header instance into sequence of bytes
93 /// <returns>Byte sequence</returns>
94 public static implicit operator byte[] (CBlockHeader h)
96 var r = new List<byte>();
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));
108 public ScryptHash256 Hash
111 return ScryptHash256.Compute256(this);
115 public override string ToString()
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();