X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCBlockHeader.cs;h=09cbd1ba7e7eba8c545bd6dfc7a2aa936ffaa91d;hb=1dcac5faa2b1477034f82466ffb16170fa2e9bb6;hp=b0759ce032acde2f5900593c1a0349c47e1e6925;hpb=945a3c1a277d09ab04c5422874db03fa313c9fa7;p=NovacoinLibrary.git
diff --git a/Novacoin/CBlockHeader.cs b/Novacoin/CBlockHeader.cs
index b0759ce..09cbd1b 100644
--- a/Novacoin/CBlockHeader.cs
+++ b/Novacoin/CBlockHeader.cs
@@ -1,52 +1,152 @@
-using System;
+/**
+ * Novacoin classes library
+ * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ */
+
+using System;
using System.Text;
using System.Collections.Generic;
+using System.Diagnostics.Contracts;
+using System.IO;
namespace Novacoin
{
- ///
- /// Block header
- ///
- public class CBlockHeader
+ ///
+ /// Block header
+ ///
+ public class CBlockHeader
{
///
/// Version of block schema.
///
- public uint nVersion = 6;
+ public uint nVersion;
///
/// Previous block hash.
///
- public Hash256 prevHash = new Hash256();
+ public uint256 prevHash;
///
/// Merkle root hash.
///
- public Hash256 merkleRoot = new Hash256();
+ public uint256 merkleRoot;
///
/// Block timestamp.
///
- public uint nTime = 0;
+ public uint nTime;
///
/// Compressed difficulty representation.
///
- public uint nBits = 0;
+ public uint nBits;
///
/// Nonce counter.
///
- public uint nNonce = 0;
+ public uint nNonce;
+ ///
+ /// Initialize an empty instance
+ ///
public CBlockHeader ()
{
+ nVersion = 6;
+ prevHash = new uint256();
+ merkleRoot = new uint256();
+ nTime = Interop.GetTime();
+ nBits = 0;
+ nNonce = 0;
}
+ public CBlockHeader(CBlockHeader header)
+ {
+ nVersion = header.nVersion;
+ prevHash = header.prevHash;
+ merkleRoot = header.merkleRoot;
+ nTime = header.nTime;
+ nBits = header.nBits;
+ nNonce = header.nNonce;
+ }
+
+ internal CBlockHeader(ref BinaryReader reader)
+ {
+ nVersion = reader.ReadUInt32();
+ prevHash = reader.ReadBytes(32);
+ merkleRoot = reader.ReadBytes(32);
+ nTime = reader.ReadUInt32();
+ nBits = reader.ReadUInt32();
+ nNonce = reader.ReadUInt32();
+ }
+
+ ///
+ /// Init block header with bytes.
+ ///
+ /// Byte array.
+ public CBlockHeader(byte[] bytes)
+ {
+ Contract.Requires(bytes.Length == 80, "Any valid block header is exactly 80 bytes long.");
+
+ var stream = new MemoryStream(bytes);
+ var reader = new BinaryReader(stream);
+
+ nVersion = reader.ReadUInt32();
+ prevHash = reader.ReadBytes(32);
+ merkleRoot = reader.ReadBytes(32);
+ nTime = reader.ReadUInt32();
+ nBits = reader.ReadUInt32();
+ nNonce = reader.ReadUInt32();
+
+ reader.Close();
+ }
+
+ ///
+ /// Convert current block header instance into sequence of bytes
+ ///
+ /// Byte sequence
+ public static implicit operator byte[] (CBlockHeader header)
+ {
+ var stream = new MemoryStream();
+ var writer = new BinaryWriter(stream);
+
+ writer.Write(header.nVersion);
+ writer.Write(header.prevHash);
+ writer.Write(header.merkleRoot);
+ writer.Write(header.nTime);
+ writer.Write(header.nBits);
+ writer.Write(header.nNonce);
+
+ var resultBytes = stream.ToArray();
+
+ writer.Close();
+
+ return resultBytes;
+ }
+
+ public uint256 Hash
+ {
+ get {
+ return CryptoUtils.ComputeScryptHash256(this);
+ }
+ }
+
public override string ToString()
{
- StringBuilder sb = new StringBuilder();
- sb.AppendFormat("CBlockHeader(nVersion={0}, prevHash={1}, merkleRoot={2}, nTime={3}, nBits={4}, nNonce={5})", nVersion, prevHash.ToString(), merkleRoot.ToString(), nTime, nBits, nNonce);
+ var sb = new StringBuilder();
+ sb.AppendFormat("CBlockHeader(nVersion={0}, prevHash={1}, merkleRoot={2}, nTime={3}, nBits={4}, nNonce={5})", nVersion, prevHash, merkleRoot, nTime, nBits, nNonce);
return sb.ToString();
}
}