Implementation of new Size property for CScript, CTxIn, CTxOut, CTransaction and...
authorCryptoManiac <balthazar@yandex.ru>
Fri, 28 Aug 2015 22:28:26 +0000 (01:28 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Fri, 28 Aug 2015 22:28:26 +0000 (01:28 +0300)
Novacoin/CBlock.cs
Novacoin/COutPoint.cs
Novacoin/CScript.cs
Novacoin/CTransaction.cs
Novacoin/CTxIn.cs
Novacoin/CTxOut.cs

index 856358f..d3b4022 100644 (file)
@@ -21,6 +21,7 @@ using System.Linq;
 using System.Text;
 using System.Collections.Generic;
 using System.Security.Cryptography;
+using System.Diagnostics.Contracts;
 
 namespace Novacoin
 {
@@ -166,6 +167,45 @@ namespace Novacoin
         }
 
         /// <summary>
+        /// Serialized size
+        /// </summary>
+        public int Size
+        {
+            get
+            {
+                int nSize = 80 + VarInt.GetEncodedSize(vtx.Length); // CBlockHeader + NumTx
+
+                foreach (var tx in vtx)
+                {
+                    nSize += tx.Size;
+                }
+
+                nSize += VarInt.GetEncodedSize(signature.Length) + signature.Length;
+
+                return nSize;
+            }
+        }
+
+        /// <summary>
+        /// Get transaction offset inside block.
+        /// </summary>
+        /// <param name="nTx">Transaction index.</param>
+        /// <returns>Offset in bytes from the beginning of block header.</returns>
+        public int GetTxOffset(int nTx)
+        {
+            Contract.Requires<ArgumentException>(nTx >= 0 && nTx < vtx.Length, "Transaction index you've specified is incorrect.");
+
+            int nOffset = 80 + VarInt.GetEncodedSize(vtx.Length); // CBlockHeader + NumTx
+
+            for (int i = 0; i < nTx; i++)
+            {
+                nOffset += vtx[nTx].Size;
+            }
+
+            return nOffset;
+        }
+
+        /// <summary>
         /// Merkle root
         /// </summary>
         public Hash256 hashMerkleRoot
index a65204e..9d1dbb5 100644 (file)
@@ -35,6 +35,11 @@ namespace Novacoin
         /// </summary>
         public uint n;
 
+        /// <summary>
+        /// Out reference is always 36 bytes long.
+        /// </summary>
+        public const int Size = 36;
+
         public COutPoint()
         {
             hash = new Hash256();
index 902e3b2..e2e17e0 100644 (file)
@@ -493,6 +493,14 @@ namespace Novacoin
             return script.codeBytes.ToArray();
         }
 
+        /// <summary>
+        /// Script size
+        /// </summary>
+        public int Size
+        {
+            get { return codeBytes.Count; }
+        }
+
         public CScriptID ScriptID
         {
             get { return new CScriptID(Hash160.Compute160(codeBytes.ToArray())); }
index d8572eb..733ef53 100644 (file)
@@ -102,7 +102,7 @@ namespace Novacoin
             nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
             nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
 
-            int nInputs = (int)(int)wBytes.GetVarInt();
+            int nInputs = (int)wBytes.GetVarInt();
             vin = new CTxIn[nInputs];
 
             for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
@@ -135,6 +135,32 @@ namespace Novacoin
         }
 
         /// <summary>
+        /// Serialized size
+        /// </summary>
+        public int Size
+        {
+            get
+            {
+                int nSize = 12; // nVersion, nTime, nLockLime
+
+                nSize += VarInt.GetEncodedSize(vin.Length);
+                nSize += VarInt.GetEncodedSize(vout.Length);
+
+                foreach (var input in vin)
+                {
+                    nSize += input.Size;
+                }
+
+                foreach (var output in vout)
+                {
+                    nSize += output.Size;
+                }
+
+                return nSize;
+            }
+        }
+
+        /// <summary>
         /// Read transactions array which is encoded in the block body.
         /// </summary>
         /// <param name="wTxBytes">Bytes sequence</param>
index a33606a..937b367 100644 (file)
@@ -87,6 +87,20 @@ namespace Novacoin
         }
 
         /// <summary>
+        /// Serialized size
+        /// </summary>
+        public int Size
+        {
+            get {
+                int nSize = 40; // COutPoint, nSequence
+                nSize += VarInt.GetEncodedSize(scriptSig.Size);
+                nSize += scriptSig.Size;
+
+                return nSize;
+            }
+        }
+
+        /// <summary>
         /// Get raw bytes representation of our input.
         /// </summary>
         /// <returns>Byte sequence.</returns>
index c5adf1c..ceea2a4 100644 (file)
@@ -88,7 +88,7 @@ namespace Novacoin
 
                 resultBytes.AddRange(BitConverter.GetBytes(output.nValue)); // txout value
 
-                var s = (byte[])output.scriptPubKey;
+                byte[] s = output.scriptPubKey;
                 resultBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptPubKey length
                 resultBytes.AddRange(s); // scriptPubKey
 
@@ -123,7 +123,19 @@ namespace Novacoin
            get { return nValue == 0 && scriptPubKey.IsNull; }
         }
 
-               public override string ToString ()
+        /// <summary>
+        /// Serialized size
+        /// </summary>
+        public int Size
+        {
+            get
+            {
+                var nScriptSize = scriptPubKey.Size;
+                return 8 + VarInt.GetEncodedSize(nScriptSize) + nScriptSize;
+            }
+        }
+
+        public override string ToString ()
                {
                        var sb = new StringBuilder ();
                        sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());