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/>.
22 using System.Collections.Generic;
29 public class CBlockHeader
32 /// Version of block schema.
34 public uint nVersion = 6;
37 /// Previous block hash.
39 public Hash256 prevHash = new Hash256();
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 Hash256(h.prevHash);
72 merkleRoot = new Hash256(h.merkleRoot);
78 public CBlockHeader(byte[] bytes)
80 nVersion = BitConverter.ToUInt32(bytes, 0);
81 prevHash = new Hash256(bytes, 4);
82 merkleRoot = new Hash256(bytes, 36);
83 nTime = BitConverter.ToUInt32(bytes, 68);
84 nBits = BitConverter.ToUInt32(bytes, 72);
85 nNonce = BitConverter.ToUInt32(bytes, 76);
89 /// Convert current block header instance into sequence of bytes
91 /// <returns>Byte sequence</returns>
92 public IList<byte> Bytes
96 List<byte> r = new List<byte>();
98 r.AddRange(BitConverter.GetBytes(nVersion));
99 r.AddRange(prevHash.hashBytes);
100 r.AddRange(merkleRoot.hashBytes);
101 r.AddRange(BitConverter.GetBytes(nTime));
102 r.AddRange(BitConverter.GetBytes(nBits));
103 r.AddRange(BitConverter.GetBytes(nNonce));
109 public ScryptHash256 Hash
112 return ScryptHash256.Compute256(Bytes);
116 public override string ToString()
118 StringBuilder sb = new StringBuilder();
119 sb.AppendFormat("CBlockHeader(nVersion={0}, prevHash={1}, merkleRoot={2}, nTime={3}, nBits={4}, nNonce={5})", nVersion, prevHash.ToString(), merkleRoot.ToString(), nTime, nBits, nNonce);
120 return sb.ToString();