From 9558aa4b58373db5fb7ba81f1449d05b0be08fa1 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Fri, 21 Aug 2015 10:48:25 +0300 Subject: [PATCH] Copy constructors for block, block header and hash classes --- Novacoin/CBlock.cs | 20 ++++++++++++++++++++ Novacoin/CBlockHeader.cs | 10 ++++++++++ Novacoin/CTransaction.cs | 10 +++++----- Novacoin/Hash.cs | 5 +++++ Novacoin/Hash160.cs | 1 + Novacoin/Hash256.cs | 2 ++ Novacoin/ScryptHash256.cs | 1 + 7 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index 17ffe6a..5e47b08 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -24,6 +24,18 @@ namespace Novacoin /// public byte[] signature; + public CBlock(CBlock b) + { + header = new CBlockHeader(b.header); + + for (int i = 0; i < b.vtx.Length; i++) + { + vtx[i] = new CTransaction(b.vtx[i]); + } + + b.signature.CopyTo(signature, 0); + } + /// /// Parse byte sequence and initialize new block instance /// @@ -49,6 +61,14 @@ namespace Novacoin signature = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)); } + public CBlock() + { + // Initialize empty array of transactions. Please note that such + // configuration is not valid real block since it has to provide + // at least one transaction. + vtx = new CTransaction[0]; + } + /// /// Convert current instance into sequence of bytes /// diff --git a/Novacoin/CBlockHeader.cs b/Novacoin/CBlockHeader.cs index d85f34f..e8b257f 100644 --- a/Novacoin/CBlockHeader.cs +++ b/Novacoin/CBlockHeader.cs @@ -46,6 +46,16 @@ namespace Novacoin { } + public CBlockHeader(CBlockHeader h) + { + nVersion = h.nVersion; + prevHash = new Hash256(h.prevHash); + merkleRoot = new Hash256(h.merkleRoot); + nTime = h.nTime; + nBits = h.nBits; + nNonce = h.nNonce; + } + /// /// Convert current block header instance into sequence of bytes /// diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index 88ea4a2..8fba084 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -39,11 +39,11 @@ namespace Novacoin /// public CTransaction() { - vin = new CTxIn[1]; /// Any transaction must provide at least one input ... - vin[0] = new CTxIn(); - - vout = new CTxOut[1]; // ... and output. - vout[0] = new CTxOut(); + // Initialize empty input and output arrays. Please note that such + // configuration is not valid for real transaction, you have to supply + // at least one input and one output. + vin = new CTxIn[0]; + vout = new CTxOut[0]; } /// diff --git a/Novacoin/Hash.cs b/Novacoin/Hash.cs index 14c19d9..8cf0b55 100644 --- a/Novacoin/Hash.cs +++ b/Novacoin/Hash.cs @@ -51,6 +51,11 @@ namespace Novacoin _hashBytes = bytes; } + public Hash(Hash h) + { + h._hashBytes.CopyTo(_hashBytes, 0); + } + public bool IsZero { get { return !_hashBytes.Any(b => b != 0); } diff --git a/Novacoin/Hash160.cs b/Novacoin/Hash160.cs index 7d59f5c..5b23d60 100644 --- a/Novacoin/Hash160.cs +++ b/Novacoin/Hash160.cs @@ -25,6 +25,7 @@ namespace Novacoin public Hash160() : base() { } public Hash160(byte[] bytes) : base(bytes) { } public Hash160(IEnumerable bytes) : base(bytes) { } + public Hash160(Hash160 h) : base(h) { } public static Hash160 Compute160(IEnumerable inputBytes) { diff --git a/Novacoin/Hash256.cs b/Novacoin/Hash256.cs index a862cd5..f571f3d 100644 --- a/Novacoin/Hash256.cs +++ b/Novacoin/Hash256.cs @@ -23,6 +23,8 @@ namespace Novacoin public Hash256() : base() { } public Hash256(byte[] bytes) : base(bytes) { } public Hash256(IEnumerable bytes) : base(bytes) { } + public Hash256(Hash256 h) : base(h) { } + public static Hash256 Compute256(IEnumerable inputBytes) { diff --git a/Novacoin/ScryptHash256.cs b/Novacoin/ScryptHash256.cs index c479b0d..ca804fd 100644 --- a/Novacoin/ScryptHash256.cs +++ b/Novacoin/ScryptHash256.cs @@ -18,6 +18,7 @@ namespace Novacoin public ScryptHash256() : base() { } public ScryptHash256(byte[] bytes) : base(bytes) { } public ScryptHash256(IEnumerable bytes) : base(bytes) { } + public ScryptHash256(ScryptHash256 h) : base(h) { } /// /// Calculate scrypt hash and return new instance of ScryptHash256 class -- 1.7.1