From 892af99303ad6d6ece3e57bebd36b521102df838 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Sat, 29 Aug 2015 01:28:26 +0300 Subject: [PATCH] Implementation of new Size property for CScript, CTxIn, CTxOut, CTransaction and CBlock. --- Novacoin/CBlock.cs | 40 ++++++++++++++++++++++++++++++++++++++++ Novacoin/COutPoint.cs | 5 +++++ Novacoin/CScript.cs | 8 ++++++++ Novacoin/CTransaction.cs | 28 +++++++++++++++++++++++++++- Novacoin/CTxIn.cs | 14 ++++++++++++++ Novacoin/CTxOut.cs | 16 ++++++++++++++-- 6 files changed, 108 insertions(+), 3 deletions(-) diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index 856358f..d3b4022 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -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 } /// + /// Serialized size + /// + 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; + } + } + + /// + /// Get transaction offset inside block. + /// + /// Transaction index. + /// Offset in bytes from the beginning of block header. + public int GetTxOffset(int nTx) + { + Contract.Requires(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; + } + + /// /// Merkle root /// public Hash256 hashMerkleRoot diff --git a/Novacoin/COutPoint.cs b/Novacoin/COutPoint.cs index a65204e..9d1dbb5 100644 --- a/Novacoin/COutPoint.cs +++ b/Novacoin/COutPoint.cs @@ -35,6 +35,11 @@ namespace Novacoin /// public uint n; + /// + /// Out reference is always 36 bytes long. + /// + public const int Size = 36; + public COutPoint() { hash = new Hash256(); diff --git a/Novacoin/CScript.cs b/Novacoin/CScript.cs index 902e3b2..e2e17e0 100644 --- a/Novacoin/CScript.cs +++ b/Novacoin/CScript.cs @@ -493,6 +493,14 @@ namespace Novacoin return script.codeBytes.ToArray(); } + /// + /// Script size + /// + public int Size + { + get { return codeBytes.Count; } + } + public CScriptID ScriptID { get { return new CScriptID(Hash160.Compute160(codeBytes.ToArray())); } diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index d8572eb..733ef53 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -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 } /// + /// Serialized size + /// + 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; + } + } + + /// /// Read transactions array which is encoded in the block body. /// /// Bytes sequence diff --git a/Novacoin/CTxIn.cs b/Novacoin/CTxIn.cs index a33606a..937b367 100644 --- a/Novacoin/CTxIn.cs +++ b/Novacoin/CTxIn.cs @@ -87,6 +87,20 @@ namespace Novacoin } /// + /// Serialized size + /// + public int Size + { + get { + int nSize = 40; // COutPoint, nSequence + nSize += VarInt.GetEncodedSize(scriptSig.Size); + nSize += scriptSig.Size; + + return nSize; + } + } + + /// /// Get raw bytes representation of our input. /// /// Byte sequence. diff --git a/Novacoin/CTxOut.cs b/Novacoin/CTxOut.cs index c5adf1c..ceea2a4 100644 --- a/Novacoin/CTxOut.cs +++ b/Novacoin/CTxOut.cs @@ -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 () + /// + /// Serialized size + /// + 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()); -- 1.7.1