Copy&paste typo fix
[NovacoinLibrary.git] / Novacoin / CTransaction.cs
index d8572eb..ab1a5ef 100644 (file)
@@ -22,6 +22,24 @@ using System.Collections.Generic;
 
 namespace Novacoin
 {
+    [Serializable]
+    public class TransactionConstructorException : Exception
+    {
+        public TransactionConstructorException()
+        {
+        }
+
+        public TransactionConstructorException(string message)
+                : base(message)
+        {
+        }
+
+        public TransactionConstructorException(string message, Exception inner)
+                : base(message, inner)
+        {
+        }
+    }
+    
     /// <summary>
     /// Represents the transaction. Any transaction must provide one input and one output at least.
     /// </summary>
@@ -30,12 +48,12 @@ namespace Novacoin
         /// <summary>
         /// Version of transaction schema.
         /// </summary>
-        public uint nVersion = 1;
+        public uint nVersion;
 
         /// <summary>
         /// Transaction timestamp.
         /// </summary>
-        public uint nTime = 0;
+        public uint nTime;
 
         /// <summary>
         /// Array of transaction inputs
@@ -50,7 +68,7 @@ namespace Novacoin
         /// <summary>
         /// Block height or timestamp when transaction is final
         /// </summary>
-        public uint nLockTime = 0;
+        public uint nLockTime;
 
         /// <summary>
         /// Initialize an empty instance
@@ -60,8 +78,11 @@ namespace Novacoin
             // 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.
+            nVersion = 1;
+            nTime = 0;
             vin = new CTxIn[0];
             vout = new CTxOut[0];
+            nLockTime = 0;
         }
 
         /// <summary>
@@ -90,6 +111,27 @@ namespace Novacoin
             nLockTime = tx.nLockTime;
         }
 
+        public bool VerifyScripts()
+        {
+            if (IsCoinBase)
+            {
+                return true;
+            }
+
+            CTransaction txPrev = null;
+            for (int i = 0; i < vin.Length; i++)
+            {
+                var outpoint = vin[i].prevout;
+
+                if (!CBlockStore.Instance.GetTransaction(outpoint.hash, ref txPrev))
+                    return false;
+
+                if (!ScriptCode.VerifyScript(vin[i].scriptSig, txPrev.vout[outpoint.n].scriptPubKey, this, i, (int)scriptflag.SCRIPT_VERIFY_P2SH, 0))
+                    return false;
+            }
+
+            return true;
+        }
 
         /// <summary>
         /// Parse byte sequence and initialize new instance of CTransaction
@@ -97,41 +139,74 @@ namespace Novacoin
         /// <param name="txBytes">Byte sequence</param>
                public CTransaction(byte[] txBytes)
         {
-            var wBytes = new ByteQueue(txBytes);
+            try
+            {
+                var wBytes = new ByteQueue(txBytes);
 
-            nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
-            nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
+                nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
+                nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
 
-            int nInputs = (int)(int)wBytes.GetVarInt();
-            vin = new CTxIn[nInputs];
+                int nInputs = (int)wBytes.GetVarInt();
+                vin = new CTxIn[nInputs];
 
-            for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
-            {
-                // Fill inputs array
-                vin[nCurrentInput] = new CTxIn();
-                
-                vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
+                for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
+                {
+                    // Fill inputs array
+                    vin[nCurrentInput] = new CTxIn();
 
-                int nScriptSigLen = (int)wBytes.GetVarInt();
-                vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
+                    vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
 
-                vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
-            }
+                    int nScriptSigLen = (int)wBytes.GetVarInt();
+                    vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
 
-            int nOutputs = (int)wBytes.GetVarInt();
-            vout = new CTxOut[nOutputs];
+                    vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
+                }
 
-            for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++)
-            {
-                // Fill outputs array
-                vout[nCurrentOutput] = new CTxOut();
-                vout[nCurrentOutput].nValue = BitConverter.ToInt64(wBytes.Get(8), 0);
+                int nOutputs = (int)wBytes.GetVarInt();
+                vout = new CTxOut[nOutputs];
+
+                for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++)
+                {
+                    // Fill outputs array
+                    vout[nCurrentOutput] = new CTxOut();
+                    vout[nCurrentOutput].nValue = BitConverter.ToUInt64(wBytes.Get(8), 0);
+
+                    int nScriptPKLen = (int)wBytes.GetVarInt();
+                    vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
+                }
 
-                int nScriptPKLen = (int)wBytes.GetVarInt();
-                vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
+                nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
             }
+            catch (Exception e)
+            {
+                throw new TransactionConstructorException("Deserialization failed", e);
+            }
+        }
+
+        /// <summary>
+        /// Serialized size
+        /// </summary>
+        public int Size
+        {
+            get
+            {
+                int nSize = 12; // nVersion, nTime, nLockLime
 
-            nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
+                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>
@@ -141,28 +216,36 @@ namespace Novacoin
         /// <returns>Transactions array</returns>
         public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
         {
-            // Read amount of transactions
-            int nTransactions = (int)wTxBytes.GetVarInt();
-            var tx = new CTransaction[nTransactions];
-
-            for (int nTx = 0; nTx < nTransactions; nTx++)
+            try
             {
-                // Fill the transactions array
-                tx[nTx] = new CTransaction();
+                // Read amount of transactions
+                int nTransactions = (int)wTxBytes.GetVarInt();
+                var tx = new CTransaction[nTransactions];
 
-                tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
-                tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+                for (int nTx = 0; nTx < nTransactions; nTx++)
+                {
+                    // Fill the transactions array
+                    tx[nTx] = new CTransaction();
 
-                // Inputs array
-                tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
+                    tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+                    tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
 
-                // outputs array
-                tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
+                    // Inputs array
+                    tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
 
-                tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
-            }
+                    // outputs array
+                    tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
 
-            return tx;
+                    tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+                }
+
+                return tx;
+
+            }
+            catch (Exception e)
+            {
+                throw new TransactionConstructorException("Deserialization failed", e);
+            }
         }
 
         public bool IsCoinBase