From 1f0b7fb3d5fc3969952610cebe1b173dbece722b Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Mon, 31 Aug 2015 21:29:23 +0300 Subject: [PATCH] CBlock: serialization --- Novacoin/CBlock.cs | 20 ++++++++++----- Novacoin/CBlockHeader.cs | 61 +++++++++++++++++++++++++++++++--------------- Novacoin/CTransaction.cs | 2 - 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index 4c87806..79d07d4 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -20,6 +20,7 @@ using System; using System.Text; using System.Collections.Generic; using System.Diagnostics.Contracts; +using System.IO; namespace Novacoin { @@ -328,20 +329,25 @@ namespace Novacoin /// Byte sequence public static implicit operator byte[] (CBlock b) { - var r = new List(); + var stream = new MemoryStream(); + var writer = new BinaryWriter(stream); - r.AddRange((byte[])b.header); - r.AddRange(VarInt.EncodeVarInt(b.vtx.LongLength)); // transactions count + writer.Write(b.header); + writer.Write(VarInt.EncodeVarInt(b.vtx.LongLength)); foreach (var tx in b.vtx) { - r.AddRange((byte[])tx); + writer.Write(tx); } - r.AddRange(VarInt.EncodeVarInt(b.signature.LongLength)); - r.AddRange(b.signature); + writer.Write(VarInt.EncodeVarInt(b.signature.LongLength)); + writer.Write(b.signature); - return r.ToArray(); + var resultBytes = stream.ToArray(); + + writer.Close(); + + return resultBytes; } /// diff --git a/Novacoin/CBlockHeader.cs b/Novacoin/CBlockHeader.cs index 8e151e9..d56df33 100644 --- a/Novacoin/CBlockHeader.cs +++ b/Novacoin/CBlockHeader.cs @@ -20,6 +20,7 @@ using System; using System.Text; using System.Collections.Generic; using System.Diagnostics.Contracts; +using System.IO; namespace Novacoin { @@ -31,38 +32,44 @@ namespace Novacoin /// /// Version of block schema. /// - public uint nVersion = 6; + public uint nVersion; /// /// Previous block hash. /// - public ScryptHash256 prevHash = new ScryptHash256(); + public ScryptHash256 prevHash; /// /// Merkle root hash. /// - public Hash256 merkleRoot = new Hash256(); + public Hash256 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 ScryptHash256(); + merkleRoot = new Hash256(); + nTime = Interop.GetTime(); + nBits = 0; + nNonce = 0; } public CBlockHeader(CBlockHeader h) @@ -75,16 +82,25 @@ namespace Novacoin nNonce = h.nNonce; } + /// + /// 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."); - nVersion = BitConverter.ToUInt32(bytes, 0); - prevHash = new ScryptHash256(bytes, 4); - merkleRoot = new Hash256(bytes, 36); - nTime = BitConverter.ToUInt32(bytes, 68); - nBits = BitConverter.ToUInt32(bytes, 72); - nNonce = BitConverter.ToUInt32(bytes, 76); + var stream = new MemoryStream(bytes); + var reader = new BinaryReader(stream); + + nVersion = reader.ReadUInt32(); + prevHash = new ScryptHash256(reader.ReadBytes(32)); + merkleRoot = new Hash256(reader.ReadBytes(32)); + nTime = reader.ReadUInt32(); + nBits = reader.ReadUInt32(); + nNonce = reader.ReadUInt32(); + + reader.Close(); } /// @@ -93,16 +109,21 @@ namespace Novacoin /// Byte sequence public static implicit operator byte[] (CBlockHeader h) { - var r = new List(); + var stream = new MemoryStream(); + var writer = new BinaryWriter(stream); + + writer.Write(h.nVersion); + writer.Write(h.prevHash); + writer.Write(h.merkleRoot); + writer.Write(h.nTime); + writer.Write(h.nBits); + writer.Write(h.nNonce); + + var resultBytes = stream.ToArray(); - r.AddRange(BitConverter.GetBytes(h.nVersion)); - r.AddRange((byte[])h.prevHash); - r.AddRange((byte[])h.merkleRoot); - r.AddRange(BitConverter.GetBytes(h.nTime)); - r.AddRange(BitConverter.GetBytes(h.nBits)); - r.AddRange(BitConverter.GetBytes(h.nNonce)); + writer.Close(); - return r.ToArray(); + return resultBytes; } public ScryptHash256 Hash diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index 979b689..d1cc3dd 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -422,9 +422,7 @@ namespace Novacoin } writer.Write(tx.nLockTime); - var resultBytes = stream.ToArray(); - writer.Close(); return resultBytes; -- 1.7.1