Don't check proof-of-stake signature twice.
[NovacoinLibrary.git] / Novacoin / CBlockStore.cs
index f44a93d..6b03f08 100644 (file)
@@ -214,7 +214,7 @@ namespace Novacoin
             }
         }
 
-        public bool GetTxOutCursor(COutPoint outpoint, ref TxOutItem txOutCursor)
+        public bool GetTxOutCursor(COutPoint outpoint, out TxOutItem txOutCursor)
         {
             var queryResults = dbConn.Query<TxOutItem>("select o.* from [Outputs] o left join [MerkleNodes] m on (m.nMerkleNodeID = o.nMerkleNodeID) where m.[TransactionHash] = ?", (byte[])outpoint.hash);
 
@@ -227,10 +227,12 @@ namespace Novacoin
 
             // Tx not found
 
+            txOutCursor = null;
+
             return false;
         }
         
-        public bool FetchInputs(CTransaction tx, ref Dictionary<COutPoint, TxOutItem> queued, ref Dictionary<COutPoint, TxOutItem> inputs, bool IsBlock, out bool Invalid)
+        public bool FetchInputs(ref CTransaction tx, ref Dictionary<COutPoint, TxOutItem> queued, ref Dictionary<COutPoint, TxOutItem> inputs, bool IsBlock, out bool Invalid)
         {
             Invalid = false;
 
@@ -348,17 +350,6 @@ namespace Novacoin
                 return false; // SetStakeEntropyBit() failed
             }
 
-            // Save proof-of-stake hash value
-            if (itemTemplate.IsProofOfStake)
-            {
-                uint256 hashProofOfStake;
-                if (!GetProofOfStakeHash(blockHash, out hashProofOfStake))
-                {
-                    return false;  // hashProofOfStake not found 
-                }
-                itemTemplate.hashProofOfStake = hashProofOfStake;
-            }
-
             // compute stake modifier
             long nStakeModifier = 0;
             bool fGeneratedStakeModifier = false;
@@ -368,7 +359,7 @@ namespace Novacoin
             }
 
             itemTemplate.SetStakeModifier(nStakeModifier, fGeneratedStakeModifier);
-            itemTemplate.nStakeModifierChecksum = StakeModifier.GetStakeModifierChecksum(itemTemplate);
+            itemTemplate.nStakeModifierChecksum = StakeModifier.GetStakeModifierChecksum(ref itemTemplate);
 
             // TODO: verify stake modifier checkpoints
 
@@ -379,6 +370,14 @@ namespace Novacoin
 
                 itemTemplate.prevoutStake = block.vtx[1].vin[0].prevout;
                 itemTemplate.nStakeTime = block.vtx[1].nTime;
+
+                // Save proof-of-stake hash value
+                uint256 hashProofOfStake;
+                if (!GetProofOfStakeHash(ref blockHash, out hashProofOfStake))
+                {
+                    return false;  // hashProofOfStake not found 
+                }
+                itemTemplate.hashProofOfStake = hashProofOfStake;
             }
 
             if (!itemTemplate.WriteToFile(ref fStreamReadWrite, ref block))
@@ -554,7 +553,6 @@ namespace Novacoin
                 }
             }
 
-
             // Connect longer branch
             var txDelete = new List<CTransaction>();
             foreach (var cursor in connect)
@@ -694,7 +692,7 @@ namespace Novacoin
                 else
                 {
                     bool Invalid;
-                    if (!FetchInputs(tx, ref queuedOutputs, ref inputs, true, out Invalid))
+                    if (!FetchInputs(ref tx, ref queuedOutputs, ref inputs, true, out Invalid))
                     {
                         return false; // Unable to fetch some inputs.
                     }
@@ -719,7 +717,7 @@ namespace Novacoin
                         nFees += nTxValueIn - nTxValueOut;
                     }
 
-                    if (!ConnectInputs(tx, ref inputs, ref queuedOutputs, ref cursor, true, fScriptChecks, scriptFlags))
+                    if (!ConnectInputs(ref tx, ref inputs, ref queuedOutputs, ref cursor, true, fScriptChecks, scriptFlags))
                     {
                         return false;
                     }
@@ -853,7 +851,7 @@ namespace Novacoin
             return true;
         }
 
-        private bool ConnectInputs(CTransaction tx, ref Dictionary<COutPoint, TxOutItem> inputs, ref Dictionary<COutPoint, TxOutItem> queued, ref CBlockStoreItem cursorBlock, bool fBlock, bool fScriptChecks, scriptflag scriptFlags)
+        private bool ConnectInputs(ref CTransaction tx, ref Dictionary<COutPoint, TxOutItem> inputs, ref Dictionary<COutPoint, TxOutItem> queued, ref CBlockStoreItem cursorBlock, bool fBlock, bool fScriptChecks, scriptflag scriptFlags)
         {
             // Take over previous transactions' spent items
             // fBlock is true when this is called from AcceptBlock when a new best-block is added to the blockchain
@@ -1031,7 +1029,7 @@ namespace Novacoin
         /// <param name="blockHash">Block hash</param>
         /// <param name="hashProofOfStake">Proof-of-stake hash</param>
         /// <returns>Proof-of-Stake hash value</returns>
-        private bool GetProofOfStakeHash(uint256 blockHash, out uint256 hashProofOfStake)
+        private bool GetProofOfStakeHash(ref uint256 blockHash, out uint256 hashProofOfStake)
         {
             return mapProofOfStake.TryGetValue(blockHash, out hashProofOfStake);
         }
@@ -1046,7 +1044,7 @@ namespace Novacoin
                 return false;
             }
 
-            CBlockStoreItem prevBlockCursor = null;
+            CBlockStoreItem prevBlockCursor;
             if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor))
             {
                 // Unable to get the cursor.
@@ -1283,12 +1281,6 @@ namespace Novacoin
 
             if (block.IsProofOfStake)
             {
-                if (!block.SignatureOK)
-                {
-                    // Proof-of-Stake signature validation failure.
-                    return false;
-                }
-
                 // TODO: proof-of-stake validation
 
                 uint256 hashProofOfStake = 0, targetProofOfStake = 0;
@@ -1411,15 +1403,7 @@ namespace Novacoin
                 }
 
                 int nCount = blockMap.Count;
-                Console.WriteLine("nCount={0}, Hash={1}, NumTx={2}, Time={3}", nCount, block.header.Hash, block.vtx.Length, DateTime.Now); // Commit on each 100th block
-
-                /*
-                if (nCount % 100 == 0 && nCount != 0)
-                {
-                    Console.WriteLine("Commit...");
-                    dbConn.Commit();
-                    dbConn.BeginTransaction();
-                }*/
+                Console.WriteLine("nCount={0}, Hash={1}, NumTx={2}, Time={3}", nCount, block.header.Hash, block.vtx.Length, DateTime.Now);
             }
 
             return true;